blob: 00d806dabf9d745e9905b56910d71ce310e1e492 [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 Veillard28ae6362001-07-14 16:44:32 +000013
Daniel Veillard8a1b1852003-01-05 22:37:17 +000014#include <assert.h>
15
Daniel Veillard3c5ed912002-01-08 10:36:16 +000016#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillard2d90de42001-04-16 17:46:18 +000017#ifdef _MSC_VER
18#include <winsock2.h>
19#pragma comment(lib, "ws2_32.lib")
20#define gettimeofday(p1,p2)
Daniel Veillardf216d462002-02-08 13:44:24 +000021#include <time.h>
Daniel Veillard28ae6362001-07-14 16:44:32 +000022#else /* _MSC_VER */
23#include <sys/time.h>
Daniel Veillard2d90de42001-04-16 17:46:18 +000024#endif /* _MSC_VER */
25#else /* _WIN32 */
Daniel Veillarded472f32001-12-13 08:48:14 +000026#ifdef HAVE_SYS_TIME_H
Daniel Veillard48b2f892001-02-25 16:11:03 +000027#include <sys/time.h>
Daniel Veillarded472f32001-12-13 08:48:14 +000028#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +000029#ifdef HAVE_TIME_H
30#include <time.h>
31#endif
Daniel Veillard2d90de42001-04-16 17:46:18 +000032#endif /* _WIN32 */
Daniel Veillard48b2f892001-02-25 16:11:03 +000033
Daniel Veillard90bc3712002-03-07 15:12:58 +000034#ifdef HAVE_SYS_TIMEB_H
35#include <sys/timeb.h>
36#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000037
38#ifdef HAVE_SYS_TYPES_H
39#include <sys/types.h>
40#endif
41#ifdef HAVE_SYS_STAT_H
42#include <sys/stat.h>
43#endif
44#ifdef HAVE_FCNTL_H
45#include <fcntl.h>
46#endif
47#ifdef HAVE_UNISTD_H
48#include <unistd.h>
49#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000050#ifdef HAVE_SYS_MMAN_H
51#include <sys/mman.h>
Daniel Veillard87b95392000-08-12 21:12:04 +000052/* seems needed for Solaris */
53#ifndef MAP_FAILED
54#define MAP_FAILED ((void *) -1)
55#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000056#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000057#ifdef HAVE_STDLIB_H
58#include <stdlib.h>
59#endif
60#ifdef HAVE_LIBREADLINE
61#include <readline/readline.h>
62#ifdef HAVE_LIBHISTORY
63#include <readline/history.h>
64#endif
65#endif
66
67#include <libxml/xmlmemory.h>
68#include <libxml/parser.h>
69#include <libxml/parserInternals.h>
70#include <libxml/HTMLparser.h>
71#include <libxml/HTMLtree.h>
72#include <libxml/tree.h>
73#include <libxml/xpath.h>
74#include <libxml/debugXML.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000075#include <libxml/xmlerror.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000076#ifdef LIBXML_XINCLUDE_ENABLED
77#include <libxml/xinclude.h>
78#endif
Daniel Veillard81418e32001-05-22 15:08:55 +000079#ifdef LIBXML_CATALOG_ENABLED
80#include <libxml/catalog.h>
81#endif
Daniel Veillard89cad532001-10-22 09:46:13 +000082#ifdef LIBXML_DOCB_ENABLED
83#include <libxml/DOCBparser.h>
84#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000085#include <libxml/globals.h>
Daniel Veillard7704fb12003-01-03 16:19:51 +000086#include <libxml/xmlreader.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000087#ifdef LIBXML_SCHEMAS_ENABLED
88#include <libxml/relaxng.h>
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000089#include <libxml/xmlschemas.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000090#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000091
Daniel Veillard3be27512003-01-26 19:49:04 +000092#ifndef XML_XML_DEFAULT_CATALOG
93#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
94#endif
95
Daniel Veillardce8b83b2000-04-05 18:38:42 +000096#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +000097static int shell = 0;
98static int debugent = 0;
99#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000100static int debug = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000101static int copy = 0;
102static int recovery = 0;
103static int noent = 0;
104static int noout = 0;
105static int nowrap = 0;
106static int valid = 0;
107static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000108static char * dtdvalid = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000109#ifdef LIBXML_SCHEMAS_ENABLED
110static char * relaxng = NULL;
111static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000112static char * schema = NULL;
113static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000114#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000115static int repeat = 0;
116static int insert = 0;
117static int compress = 0;
Daniel Veillard89cad532001-10-22 09:46:13 +0000118#ifdef LIBXML_DOCB_ENABLED
119static int sgml = 0;
120#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000121static int html = 0;
122static int htmlout = 0;
123static int push = 0;
Daniel Veillard46e370e2000-07-21 20:32:03 +0000124#ifdef HAVE_SYS_MMAN_H
125static int memory = 0;
126#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000127static int noblanks = 0;
Daniel Veillard90493a92001-08-14 14:12:47 +0000128static int format = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000129static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000130static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000131#ifdef LIBXML_XINCLUDE_ENABLED
132static int xinclude = 0;
133#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000134static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000135static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000136static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000137static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000138static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000139static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000140#ifdef LIBXML_CATALOG_ENABLED
141static int catalogs = 0;
142static int nocatalogs = 0;
143#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000144static int stream = 0;
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000145static int chkregister = 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 Veillard89cad532001-10-22 09:46:13 +0000712#ifdef LIBXML_DOCB_ENABLED
713 /*
714 * build an SGML tree from a string;
715 */
716 else if ((sgml) && (push)) {
717 FILE *f;
718
719 f = fopen(filename, "r");
720 if (f != NULL) {
721 int res, size = 3;
722 char chars[4096];
723 docbParserCtxtPtr ctxt;
724
725 /* if (repeat) */
726 size = 4096;
727 res = fread(chars, 1, 4, f);
728 if (res > 0) {
729 ctxt = docbCreatePushParserCtxt(NULL, NULL,
William M. Brack779af002003-08-01 15:55:39 +0000730 chars, res, filename, XML_CHAR_ENCODING_NONE);
Daniel Veillard89cad532001-10-22 09:46:13 +0000731 while ((res = fread(chars, 1, size, f)) > 0) {
732 docbParseChunk(ctxt, chars, res, 0);
733 }
734 docbParseChunk(ctxt, chars, 0, 1);
735 doc = ctxt->myDoc;
736 docbFreeParserCtxt(ctxt);
737 }
738 fclose(f);
739 }
740 } else if (sgml) {
741 doc = docbParseFile(filename, NULL);
742 }
743#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000744#ifdef LIBXML_HTML_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000745 else if ((html) && (push)) {
746 FILE *f;
747
748 f = fopen(filename, "r");
749 if (f != NULL) {
750 int res, size = 3;
751 char chars[4096];
752 htmlParserCtxtPtr ctxt;
753
754 /* if (repeat) */
755 size = 4096;
756 res = fread(chars, 1, 4, f);
757 if (res > 0) {
758 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
759 chars, res, filename, 0);
760 while ((res = fread(chars, 1, size, f)) > 0) {
761 htmlParseChunk(ctxt, chars, res, 0);
762 }
763 htmlParseChunk(ctxt, chars, 0, 1);
764 doc = ctxt->myDoc;
765 htmlFreeParserCtxt(ctxt);
766 }
767 fclose(f);
768 }
769 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000770 else if (html) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000771 doc = htmlParseFile(filename, NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000772 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000773#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000774 else {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000775 /*
776 * build an XML tree from a string;
777 */
778 if (push) {
779 FILE *f;
780
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000781 /* '-' Usually means stdin -<sven@zen.org> */
782 if ((filename[0] == '-') && (filename[1] == 0)) {
783 f = stdin;
784 } else {
785 f = fopen(filename, "r");
786 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000787 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000788 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000789 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000790 char chars[1024];
791 xmlParserCtxtPtr ctxt;
792
Daniel Veillarda880b122003-04-21 21:36:41 +0000793 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000794 res = fread(chars, 1, 4, f);
795 if (res > 0) {
796 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
797 chars, res, filename);
798 while ((res = fread(chars, 1, size, f)) > 0) {
799 xmlParseChunk(ctxt, chars, res, 0);
800 }
801 xmlParseChunk(ctxt, chars, 0, 1);
802 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000803 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000804 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000805 if (!ret) {
806 xmlFreeDoc(doc);
807 doc = NULL;
808 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000809 }
810 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000811 } else if (testIO) {
812 int ret;
813 FILE *f;
814
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000815 /* '-' Usually means stdin -<sven@zen.org> */
816 if ((filename[0] == '-') && (filename[1] == 0)) {
817 f = stdin;
818 } else {
819 f = fopen(filename, "r");
820 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000821 if (f != NULL) {
822 xmlParserCtxtPtr ctxt;
823
824 ctxt = xmlCreateIOParserCtxt(NULL, NULL,
825 (xmlInputReadCallback) myRead,
826 (xmlInputCloseCallback) myClose,
827 f, XML_CHAR_ENCODING_NONE);
828 xmlParseDocument(ctxt);
829
830 ret = ctxt->wellFormed;
831 doc = ctxt->myDoc;
832 xmlFreeParserCtxt(ctxt);
833 if (!ret) {
834 xmlFreeDoc(doc);
835 doc = NULL;
836 }
837 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000838 } else if (recovery) {
839 doc = xmlRecoverFile(filename);
840 } else if (htmlout) {
841 int ret;
842 xmlParserCtxtPtr ctxt;
843 xmlSAXHandler silent, *old;
844
845 ctxt = xmlCreateFileParserCtxt(filename);
Daniel Veillard88a172f2000-08-04 18:23:10 +0000846
847 if (ctxt == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000848 /* If xmlCreateFileParserCtxt() return NULL something
Daniel Veillard88a172f2000-08-04 18:23:10 +0000849 strange happened so we don't want to do anything. Do
850 we want to print an error message here?
851 <sven@zen.org> */
Daniel Veillard7ebb1ee2000-08-04 18:24:45 +0000852 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000853 } else {
854 memcpy(&silent, ctxt->sax, sizeof(silent));
855 old = ctxt->sax;
856 silent.error = xmlHTMLError;
857 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000858 silent.warning = xmlHTMLWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000859 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000860 silent.warning = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000861 silent.fatalError = xmlHTMLError;
862 ctxt->sax = &silent;
863 ctxt->vctxt.error = xmlHTMLValidityError;
864 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000865 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000866 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000867 ctxt->vctxt.warning = NULL;
868
Daniel Veillard88a172f2000-08-04 18:23:10 +0000869 xmlParseDocument(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000870
Daniel Veillard88a172f2000-08-04 18:23:10 +0000871 ret = ctxt->wellFormed;
872 doc = ctxt->myDoc;
873 ctxt->sax = old;
874 xmlFreeParserCtxt(ctxt);
875 if (!ret) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000876 xmlFreeDoc(doc);
877 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000878 }
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
893 doc = xmlParseMemory((char *) base, info.st_size);
894 munmap((char *) base, info.st_size);
895#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000896 } else if (valid) {
897 int ret;
898 xmlParserCtxtPtr ctxt;
899
900 ctxt = xmlCreateFileParserCtxt(filename);
901
902 if (ctxt == NULL) {
903 doc = NULL;
904 } else {
905 xmlParseDocument(ctxt);
906 if (ctxt->valid == 0)
907 progresult = 4;
908 ret = ctxt->wellFormed;
909 doc = ctxt->myDoc;
910 xmlFreeParserCtxt(ctxt);
911 if (!ret) {
912 xmlFreeDoc(doc);
913 doc = NULL;
914 }
915 }
916 } else {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000917 doc = xmlParseFile(filename);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000918 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000919 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000920
Daniel Veillard88a172f2000-08-04 18:23:10 +0000921 /*
922 * If we don't have a document we might as well give up. Do we
923 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000924 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000925 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000926 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000927 }
928
Daniel Veillard48b2f892001-02-25 16:11:03 +0000929 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000930 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000931 }
932
Daniel Veillard29e43992001-12-13 22:21:58 +0000933 /*
934 * Remove DOCTYPE nodes
935 */
936 if (dropdtd) {
937 xmlDtdPtr dtd;
938
939 dtd = xmlGetIntSubset(doc);
940 if (dtd != NULL) {
941 xmlUnlinkNode((xmlNodePtr)dtd);
942 xmlFreeDtd(dtd);
943 }
944 }
945
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000946#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000947 if (xinclude) {
948 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000949 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000950 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000951 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000952 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000953 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000954 }
955 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000956#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000957
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000958#ifdef LIBXML_DEBUG_ENABLED
959 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000960 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000961 */
962 if (shell)
963 xmlShell(doc, filename, xmlShellReadline, stdout);
964#endif
965
966 /*
967 * test intermediate copy if needed.
968 */
969 if (copy) {
970 tmp = doc;
971 doc = xmlCopyDoc(doc, 1);
972 xmlFreeDoc(tmp);
973 }
974
975 if ((insert) && (!html)) {
976 const xmlChar* list[256];
977 int nb, i;
978 xmlNodePtr node;
979
980 if (doc->children != NULL) {
981 node = doc->children;
982 while ((node != NULL) && (node->last == NULL)) node = node->next;
983 if (node != NULL) {
984 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
985 if (nb < 0) {
986 printf("could not get valid list of elements\n");
987 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000988 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000989 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000990 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000991 nb);
992 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000993 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000994 }
995 }
996 }
997 }
998 }else if (noout == 0) {
999 /*
1000 * print it.
1001 */
1002#ifdef LIBXML_DEBUG_ENABLED
1003 if (!debug) {
1004#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001005 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001006 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001007 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001008#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001009 if (memory) {
1010 xmlChar *result;
1011 int len;
1012
1013 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001014 if ( format ) {
1015 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1016 } else {
1017 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1018 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001019 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001020 if (format)
1021 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1022 else
1023 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001024 }
1025 if (result == NULL) {
1026 fprintf(stderr, "Failed to save\n");
1027 } else {
1028 write(1, result, len);
1029 xmlFree(result);
1030 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001031 } else
1032#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001033 if (compress) {
1034 xmlSaveFile(output ? output : "-", doc);
1035 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001036 else if (encoding != NULL) {
1037 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001038 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
1039 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001040 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001041 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +00001042 }
1043 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001044 else if (format) {
1045 xmlSaveFormatFile(output ? output : "-", doc, 1);
1046 }
1047 else {
1048 FILE *out;
1049 if (output == NULL)
1050 out = stdout;
1051 else {
1052 out = fopen(output,"wb");
1053 }
1054 xmlDocDump(out, doc);
1055
1056 if (output)
1057 fclose(out);
1058 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001059 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001060 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001061 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001062#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001063 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001064 FILE *out;
1065 if (output == NULL)
1066 out = stdout;
1067 else {
1068 out = fopen(output,"wb");
1069 }
1070 xmlDebugDumpDocument(out, doc);
1071
1072 if (output)
1073 fclose(out);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001074 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001075#endif
1076 }
1077
1078 /*
1079 * A posteriori validation test
1080 */
Daniel Veillardcd429612000-10-11 15:57:05 +00001081 if (dtdvalid != NULL) {
1082 xmlDtdPtr dtd;
1083
Daniel Veillard48b2f892001-02-25 16:11:03 +00001084 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001085 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001086 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001087 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001088 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001089 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001090 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001091 if (dtd == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001092 xmlGenericError(xmlGenericErrorContext,
1093 "Could not parse DTD %s\n", dtdvalid);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001094 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001095 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001096 xmlValidCtxtPtr cvp;
1097
1098 if ((cvp = xmlNewValidCtxt()) == NULL) {
1099 xmlGenericError(xmlGenericErrorContext,
1100 "Couldn't allocate validation context\n");
1101 exit(-1);
1102 }
1103 cvp->userData = (void *) stderr;
1104 cvp->error = (xmlValidityErrorFunc) fprintf;
1105 cvp->warning = (xmlValidityWarningFunc) fprintf;
1106
Daniel Veillard48b2f892001-02-25 16:11:03 +00001107 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001108 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001109 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001110 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001111 xmlGenericError(xmlGenericErrorContext,
1112 "Document %s does not validate against %s\n",
Daniel Veillardcd429612000-10-11 15:57:05 +00001113 filename, dtdvalid);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001114 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001115 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001116 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001117 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001118 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001119 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001120 xmlFreeDtd(dtd);
1121 }
1122 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001123 xmlValidCtxtPtr cvp;
1124
1125 if ((cvp = xmlNewValidCtxt()) == NULL) {
1126 xmlGenericError(xmlGenericErrorContext,
1127 "Couldn't allocate validation context\n");
1128 exit(-1);
1129 }
1130
Daniel Veillard48b2f892001-02-25 16:11:03 +00001131 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001132 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001133 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001134 cvp->userData = (void *) stderr;
1135 cvp->error = (xmlValidityErrorFunc) fprintf;
1136 cvp->warning = (xmlValidityWarningFunc) fprintf;
1137 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001138 xmlGenericError(xmlGenericErrorContext,
1139 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001140 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001141 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001142 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001143 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001144 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001145 xmlFreeValidCtxt(cvp);
Daniel Veillard71531f32003-02-05 13:19:53 +00001146#ifdef LIBXML_SCHEMAS_ENABLED
1147 } else if (relaxngschemas != NULL) {
1148 xmlRelaxNGValidCtxtPtr ctxt;
1149 int ret;
1150
Daniel Veillard42f12e92003-03-07 18:32:59 +00001151 if ((timing) && (!repeat)) {
1152 startTimer();
1153 }
1154
Daniel Veillard71531f32003-02-05 13:19:53 +00001155 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1156 xmlRelaxNGSetValidErrors(ctxt,
1157 (xmlRelaxNGValidityErrorFunc) fprintf,
1158 (xmlRelaxNGValidityWarningFunc) fprintf,
1159 stderr);
1160 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1161 if (ret == 0) {
1162 printf("%s validates\n", filename);
1163 } else if (ret > 0) {
1164 printf("%s fails to validate\n", filename);
1165 } else {
1166 printf("%s validation generated an internal error\n",
1167 filename);
1168 }
1169 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001170 if ((timing) && (!repeat)) {
1171 endTimer("Validating");
1172 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001173 } else if (wxschemas != NULL) {
1174 xmlSchemaValidCtxtPtr ctxt;
1175 int ret;
1176
1177 if ((timing) && (!repeat)) {
1178 startTimer();
1179 }
1180
1181 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1182 xmlSchemaSetValidErrors(ctxt,
1183 (xmlSchemaValidityErrorFunc) fprintf,
1184 (xmlSchemaValidityWarningFunc) fprintf,
1185 stderr);
1186 ret = xmlSchemaValidateDoc(ctxt, doc);
1187 if (ret == 0) {
1188 printf("%s validates\n", filename);
1189 } else if (ret > 0) {
1190 printf("%s fails to validate\n", filename);
1191 } else {
1192 printf("%s validation generated an internal error\n",
1193 filename);
1194 }
1195 xmlSchemaFreeValidCtxt(ctxt);
1196 if ((timing) && (!repeat)) {
1197 endTimer("Validating");
1198 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001199#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001200 }
1201
1202#ifdef LIBXML_DEBUG_ENABLED
1203 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001204 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001205#endif
1206
1207 /*
1208 * free it.
1209 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001210 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001211 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001212 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001213 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001214 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001215 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001216 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001217}
1218
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001219/************************************************************************
1220 * *
1221 * Usage and Main *
1222 * *
1223 ************************************************************************/
1224
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001225static void showVersion(const char *name) {
1226 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1227 fprintf(stderr, " compiled with: ");
1228#ifdef LIBXML_FTP_ENABLED
1229 fprintf(stderr, "FTP ");
1230#endif
1231#ifdef LIBXML_HTTP_ENABLED
1232 fprintf(stderr, "HTTP ");
1233#endif
1234#ifdef LIBXML_HTML_ENABLED
1235 fprintf(stderr, "HTML ");
1236#endif
1237#ifdef LIBXML_C14N_ENABLED
1238 fprintf(stderr, "C14N ");
1239#endif
1240#ifdef LIBXML_CATALOG_ENABLED
1241 fprintf(stderr, "Catalog ");
1242#endif
1243#ifdef LIBXML_DOCB_ENABLED
1244 fprintf(stderr, "DocBook ");
1245#endif
1246#ifdef LIBXML_XPATH_ENABLED
1247 fprintf(stderr, "XPath ");
1248#endif
1249#ifdef LIBXML_XPTR_ENABLED
1250 fprintf(stderr, "XPointer ");
1251#endif
1252#ifdef LIBXML_XINCLUDE_ENABLED
1253 fprintf(stderr, "XInclude ");
1254#endif
1255#ifdef LIBXML_ICONV_ENABLED
1256 fprintf(stderr, "Iconv ");
1257#endif
1258#ifdef DEBUG_MEMORY_LOCATION
1259 fprintf(stderr, "MemDebug ");
1260#endif
1261#ifdef LIBXML_UNICODE_ENABLED
1262 fprintf(stderr, "Unicode ");
1263#endif
1264#ifdef LIBXML_REGEXP_ENABLED
1265 fprintf(stderr, "Regexps ");
1266#endif
1267#ifdef LIBXML_AUTOMATA_ENABLED
1268 fprintf(stderr, "Automata ");
1269#endif
1270#ifdef LIBXML_SCHEMAS_ENABLED
1271 fprintf(stderr, "Schemas ");
1272#endif
1273 fprintf(stderr, "\n");
1274}
1275
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001276static void usage(const char *name) {
1277 printf("Usage : %s [options] XMLfiles ...\n", name);
1278 printf("\tParse the XML files and output the result of the parsing\n");
1279 printf("\t--version : display the version of the XML library used\n");
1280#ifdef LIBXML_DEBUG_ENABLED
1281 printf("\t--debug : dump a debug tree of the in-memory document\n");
1282 printf("\t--shell : run a navigating shell\n");
1283 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001284#else
1285 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001286#endif
1287 printf("\t--copy : used to test the internal copy implementation\n");
1288 printf("\t--recover : output what was parsable on broken XML documents\n");
1289 printf("\t--noent : substitute entity references by their value\n");
1290 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001291 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001292 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001293 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001294 printf("\t--valid : validate the document in addition to std well-formed check\n");
1295 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1296 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
1297 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001298 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001299 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1300 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001301#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001302 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001303#endif
Daniel Veillard89cad532001-10-22 09:46:13 +00001304#ifdef LIBXML_DOCB_ENABLED
1305 printf("\t--sgml : use the DocBook SGML parser\n");
1306#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001307#ifdef LIBXML_HTML_ENABLED
1308 printf("\t--html : use the HTML parser\n");
1309#endif
1310 printf("\t--push : use the push mode of the parser\n");
1311#ifdef HAVE_SYS_MMAN_H
1312 printf("\t--memory : parse from memory\n");
1313#endif
1314 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1315 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillard90493a92001-08-14 14:12:47 +00001316 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001317 printf("\t--testIO : test user I/O support\n");
1318 printf("\t--encode encoding : output in the given encoding\n");
1319#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001320 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1321 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillard3be27512003-01-26 19:49:04 +00001322 printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001323 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001324#endif
1325 printf("\t--auto : generate a small doc on the fly\n");
1326#ifdef LIBXML_XINCLUDE_ENABLED
1327 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001328#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001329 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001330 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard29e43992001-12-13 22:21:58 +00001331 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
Daniel Veillard7704fb12003-01-03 16:19:51 +00001332 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001333 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001334#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001335 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001336 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001337#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001338 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1339 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001340}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001341
1342static void registerNode(xmlNodePtr node)
1343{
1344 node->_private = malloc(sizeof(long));
1345 *(long*)node->_private = (long) 0x81726354;
1346}
1347
1348static void deregisterNode(xmlNodePtr node)
1349{
1350 assert(node->_private != NULL);
1351 assert(*(long*)node->_private == (long) 0x81726354);
1352 free(node->_private);
1353}
1354
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001355int
1356main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001357 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001358 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001359 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001360 const char* indent;
1361
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001362 if (argc <= 1) {
1363 usage(argv[0]);
1364 return(1);
1365 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001366 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001367 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001368 if (!strcmp(argv[i], "-"))
1369 break;
1370
1371 if (argv[i][0] != '-')
1372 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001373 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1374 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001375 else
1376#ifdef LIBXML_DEBUG_ENABLED
1377 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001378 (!strcmp(argv[i], "--shell"))) {
1379 shell++;
1380 noout = 1;
1381 } else
1382#endif
1383 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1384 copy++;
1385 else if ((!strcmp(argv[i], "-recover")) ||
1386 (!strcmp(argv[i], "--recover")))
1387 recovery++;
1388 else if ((!strcmp(argv[i], "-noent")) ||
1389 (!strcmp(argv[i], "--noent")))
1390 noent++;
Daniel Veillard4ec885a2001-06-17 10:31:07 +00001391 else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001392 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001393 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001394 version = 1;
1395 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001396 (!strcmp(argv[i], "--noout")))
1397 noout++;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001398 else if ((!strcmp(argv[i], "-o")) ||
1399 (!strcmp(argv[i], "-output")) ||
1400 (!strcmp(argv[i], "--output"))) {
1401 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001402 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001403 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001404 else if ((!strcmp(argv[i], "-htmlout")) ||
1405 (!strcmp(argv[i], "--htmlout")))
1406 htmlout++;
Daniel Veillard89cad532001-10-22 09:46:13 +00001407#ifdef LIBXML_DOCB_ENABLED
1408 else if ((!strcmp(argv[i], "-sgml")) ||
1409 (!strcmp(argv[i], "--sgml"))) {
1410 sgml++;
1411 }
1412#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001413#ifdef LIBXML_HTML_ENABLED
1414 else if ((!strcmp(argv[i], "-html")) ||
1415 (!strcmp(argv[i], "--html"))) {
1416 html++;
1417 }
1418#endif /* LIBXML_HTML_ENABLED */
1419 else if ((!strcmp(argv[i], "-nowrap")) ||
1420 (!strcmp(argv[i], "--nowrap")))
1421 nowrap++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001422 else if ((!strcmp(argv[i], "-loaddtd")) ||
1423 (!strcmp(argv[i], "--loaddtd")))
1424 loaddtd++;
Daniel Veillard48da9102001-08-07 01:10:10 +00001425 else if ((!strcmp(argv[i], "-dtdattr")) ||
1426 (!strcmp(argv[i], "--dtdattr"))) {
1427 loaddtd++;
1428 dtdattrs++;
1429 } else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001430 (!strcmp(argv[i], "--valid")))
1431 valid++;
1432 else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001433 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001434 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001435 loaddtd++;
1436 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001437 (!strcmp(argv[i], "--dtdvalid"))) {
1438 i++;
1439 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001440 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001441 }
Daniel Veillard29e43992001-12-13 22:21:58 +00001442 else if ((!strcmp(argv[i], "-dropdtd")) ||
1443 (!strcmp(argv[i], "--dropdtd")))
1444 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001445 else if ((!strcmp(argv[i], "-insert")) ||
1446 (!strcmp(argv[i], "--insert")))
1447 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001448 else if ((!strcmp(argv[i], "-timing")) ||
1449 (!strcmp(argv[i], "--timing")))
1450 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001451 else if ((!strcmp(argv[i], "-auto")) ||
1452 (!strcmp(argv[i], "--auto")))
1453 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001454 else if ((!strcmp(argv[i], "-repeat")) ||
1455 (!strcmp(argv[i], "--repeat")))
1456 repeat++;
1457 else if ((!strcmp(argv[i], "-push")) ||
1458 (!strcmp(argv[i], "--push")))
1459 push++;
Daniel Veillard46e370e2000-07-21 20:32:03 +00001460#ifdef HAVE_SYS_MMAN_H
1461 else if ((!strcmp(argv[i], "-memory")) ||
1462 (!strcmp(argv[i], "--memory")))
1463 memory++;
1464#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001465 else if ((!strcmp(argv[i], "-testIO")) ||
1466 (!strcmp(argv[i], "--testIO")))
1467 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001468#ifdef LIBXML_XINCLUDE_ENABLED
1469 else if ((!strcmp(argv[i], "-xinclude")) ||
1470 (!strcmp(argv[i], "--xinclude")))
1471 xinclude++;
1472#endif
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001473#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001474 else if ((!strcmp(argv[i], "-compress")) ||
1475 (!strcmp(argv[i], "--compress"))) {
1476 compress++;
1477 xmlSetCompressMode(9);
1478 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001479#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001480 else if ((!strcmp(argv[i], "-nowarning")) ||
1481 (!strcmp(argv[i], "--nowarning"))) {
1482 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001483 xmlPedanticParserDefault(0);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001484 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001485 else if ((!strcmp(argv[i], "-pedantic")) ||
1486 (!strcmp(argv[i], "--pedantic"))) {
1487 xmlGetWarningsDefaultValue = 1;
1488 xmlPedanticParserDefault(1);
1489 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001490#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001491 else if ((!strcmp(argv[i], "-debugent")) ||
1492 (!strcmp(argv[i], "--debugent"))) {
1493 debugent++;
1494 xmlParserDebugEntities = 1;
1495 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001496#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001497#ifdef LIBXML_CATALOG_ENABLED
1498 else if ((!strcmp(argv[i], "-catalogs")) ||
1499 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001500 catalogs++;
1501 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1502 (!strcmp(argv[i], "--nocatalogs"))) {
1503 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001504 }
1505#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001506 else if ((!strcmp(argv[i], "-encode")) ||
1507 (!strcmp(argv[i], "--encode"))) {
1508 i++;
1509 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001510 /*
1511 * OK it's for testing purposes
1512 */
1513 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001514 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001515 else if ((!strcmp(argv[i], "-noblanks")) ||
1516 (!strcmp(argv[i], "--noblanks"))) {
1517 noblanks++;
1518 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001519 }
1520 else if ((!strcmp(argv[i], "-format")) ||
1521 (!strcmp(argv[i], "--format"))) {
1522 noblanks++;
1523 format++;
1524 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001525 }
1526 else if ((!strcmp(argv[i], "-stream")) ||
1527 (!strcmp(argv[i], "--stream"))) {
1528 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001529 }
1530 else if ((!strcmp(argv[i], "-chkregister")) ||
1531 (!strcmp(argv[i], "--chkregister"))) {
1532 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001533#ifdef LIBXML_SCHEMAS_ENABLED
1534 } else if ((!strcmp(argv[i], "-relaxng")) ||
1535 (!strcmp(argv[i], "--relaxng"))) {
1536 i++;
1537 relaxng = argv[i];
1538 noent++;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001539 } else if ((!strcmp(argv[i], "-schema")) ||
1540 (!strcmp(argv[i], "--schema"))) {
1541 i++;
1542 schema = argv[i];
1543 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001544#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001545 } else if ((!strcmp(argv[i], "-nonet")) ||
1546 (!strcmp(argv[i], "--nonet"))) {
1547 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001548 } else {
1549 fprintf(stderr, "Unknown option %s\n", argv[i]);
1550 usage(argv[0]);
1551 return(1);
1552 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001553 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001554
1555#ifdef LIBXML_CATALOG_ENABLED
1556 if (nocatalogs == 0) {
1557 if (catalogs) {
1558 const char *catal;
1559
1560 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001561 if (catal != NULL) {
1562 xmlLoadCatalogs(catal);
1563 } else {
1564 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1565 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001566 }
1567 }
1568#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001569
1570 if (chkregister) {
1571 xmlRegisterNodeDefault(registerNode);
1572 xmlDeregisterNodeDefault(deregisterNode);
1573 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001574
1575 indent = getenv("XMLLINT_INDENT");
1576 if(indent != NULL) {
1577 xmlTreeIndentString = indent;
1578 }
1579
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001580
Daniel Veillardd9bad132001-07-23 19:39:43 +00001581 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001582 if (loaddtd != 0)
1583 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1584 if (dtdattrs)
1585 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001586 if (noent != 0) xmlSubstituteEntitiesDefault(1);
1587 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
1588 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001589 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001590 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001591 xmlGenericError(xmlGenericErrorContext,
1592 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1593 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001594 "<html><head><title>%s output</title></head>\n",
1595 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001596 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001597 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1598 argv[0]);
1599 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001600
1601#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +00001602 if ((relaxng != NULL) && (stream == 0)) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001603 xmlRelaxNGParserCtxtPtr ctxt;
1604
Daniel Veillardce192eb2003-04-16 15:58:05 +00001605 /* forces loading the DTDs */
1606 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001607 if (timing) {
1608 startTimer();
1609 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001610 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1611 xmlRelaxNGSetParserErrors(ctxt,
1612 (xmlRelaxNGValidityErrorFunc) fprintf,
1613 (xmlRelaxNGValidityWarningFunc) fprintf,
1614 stderr);
1615 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001616 if (relaxngschemas == NULL) {
1617 xmlGenericError(xmlGenericErrorContext,
1618 "Relax-NG schema %s failed to compile\n", relaxng);
1619 relaxng = NULL;
1620 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001621 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001622 if (timing) {
1623 endTimer("Compiling the schemas");
1624 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001625 } else if ((schema != NULL) && (stream == 0)) {
1626 xmlSchemaParserCtxtPtr ctxt;
1627
1628 if (timing) {
1629 startTimer();
1630 }
1631 ctxt = xmlSchemaNewParserCtxt(schema);
1632 xmlSchemaSetParserErrors(ctxt,
1633 (xmlSchemaValidityErrorFunc) fprintf,
1634 (xmlSchemaValidityWarningFunc) fprintf,
1635 stderr);
1636 wxschemas = xmlSchemaParse(ctxt);
1637 if (wxschemas == NULL) {
1638 xmlGenericError(xmlGenericErrorContext,
1639 "WXS schema %s failed to compile\n", schema);
1640 schema = NULL;
1641 }
1642 xmlSchemaFreeParserCtxt(ctxt);
1643 if (timing) {
1644 endTimer("Compiling the schemas");
1645 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001646 }
1647#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001648 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001649 if ((!strcmp(argv[i], "-encode")) ||
1650 (!strcmp(argv[i], "--encode"))) {
1651 i++;
1652 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001653 } else if ((!strcmp(argv[i], "-o")) ||
1654 (!strcmp(argv[i], "-output")) ||
1655 (!strcmp(argv[i], "--output"))) {
1656 i++;
1657 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001658 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001659 if ((!strcmp(argv[i], "-dtdvalid")) ||
1660 (!strcmp(argv[i], "--dtdvalid"))) {
1661 i++;
1662 continue;
1663 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001664 if ((!strcmp(argv[i], "-relaxng")) ||
1665 (!strcmp(argv[i], "--relaxng"))) {
1666 i++;
1667 continue;
1668 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001669 if ((!strcmp(argv[i], "-schema")) ||
1670 (!strcmp(argv[i], "--schema"))) {
1671 i++;
1672 continue;
1673 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001674 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001675 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001676 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001677 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001678 if (repeat) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001679 for (acount = 0;acount < 100 * repeat;acount++)
1680 if (stream != 0)
1681 streamFile(argv[i]);
1682 else
1683 parseAndPrintFile(argv[i]);
1684 } else {
1685 if (stream != 0)
1686 streamFile(argv[i]);
1687 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001688 parseAndPrintFile(argv[i]);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001689 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001690 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001691 if ((timing) && (repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001692 endTimer("100 iterations");
Daniel Veillarda7866932001-12-04 13:14:44 +00001693 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001694 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001695 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001696 if (generate)
1697 parseAndPrintFile(NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001698 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001699 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001700 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001701 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001702 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001703 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001704#ifdef LIBXML_SCHEMAS_ENABLED
1705 if (relaxngschemas != NULL)
1706 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001707 if (wxschemas != NULL)
1708 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00001709 xmlRelaxNGCleanupTypes();
1710#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001711 xmlCleanupParser();
1712 xmlMemoryDump();
1713
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001714 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001715}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001716