blob: 0effd3d0f6d8e30fdd7fdbb8138ff632ab2bd07e [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 Veillardce8b83b2000-04-05 18:38:42 +000087
88#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +000089static int shell = 0;
90static int debugent = 0;
91#endif
Daniel Veillard8326e732003-01-07 00:19:07 +000092static int debug = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +000093static int copy = 0;
94static int recovery = 0;
95static int noent = 0;
96static int noout = 0;
97static int nowrap = 0;
98static int valid = 0;
99static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000100static char * dtdvalid = NULL;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000101static int repeat = 0;
102static int insert = 0;
103static int compress = 0;
Daniel Veillard89cad532001-10-22 09:46:13 +0000104#ifdef LIBXML_DOCB_ENABLED
105static int sgml = 0;
106#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000107static int html = 0;
108static int htmlout = 0;
109static int push = 0;
Daniel Veillard46e370e2000-07-21 20:32:03 +0000110#ifdef HAVE_SYS_MMAN_H
111static int memory = 0;
112#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000113static int noblanks = 0;
Daniel Veillard90493a92001-08-14 14:12:47 +0000114static int format = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000115static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000116static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000117#ifdef LIBXML_XINCLUDE_ENABLED
118static int xinclude = 0;
119#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000120static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000121static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000122static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000123static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000124static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000125static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000126#ifdef LIBXML_CATALOG_ENABLED
127static int catalogs = 0;
128static int nocatalogs = 0;
129#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000130static int stream = 0;
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000131static int chkregister = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000132static const char *output = NULL;
133
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000134
Daniel Veillard01db67c2001-12-18 07:09:59 +0000135/*
136 * Internal timing routines to remove the necessity to have unix-specific
137 * function calls
138 */
139
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000140#ifndef HAVE_GETTIMEOFDAY
141#ifdef HAVE_SYS_TIMEB_H
142#ifdef HAVE_SYS_TIME_H
143#ifdef HAVE_FTIME
144
Daniel Veillard01c13b52002-12-10 15:19:08 +0000145static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000146my_gettimeofday(struct timeval *tvp, void *tzp)
147{
148 struct timeb timebuffer;
149
150 ftime(&timebuffer);
151 if (tvp) {
152 tvp->tv_sec = timebuffer.time;
153 tvp->tv_usec = timebuffer.millitm * 1000L;
154 }
155 return (0);
156}
157#define HAVE_GETTIMEOFDAY 1
158#define gettimeofday my_gettimeofday
159
160#endif /* HAVE_FTIME */
161#endif /* HAVE_SYS_TIME_H */
162#endif /* HAVE_SYS_TIMEB_H */
163#endif /* !HAVE_GETTIMEOFDAY */
164
Daniel Veillard01db67c2001-12-18 07:09:59 +0000165#if defined(HAVE_GETTIMEOFDAY)
166static struct timeval begin, end;
167
168/*
169 * startTimer: call where you want to start timing
170 */
171static void
172startTimer(void)
173{
174 gettimeofday(&begin, NULL);
175}
176
177/*
178 * endTimer: call where you want to stop timing and to print out a
179 * message about the timing performed; format is a printf
180 * type argument
181 */
182static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000183endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000184{
185 long msec;
186 va_list ap;
187
188 gettimeofday(&end, NULL);
189 msec = end.tv_sec - begin.tv_sec;
190 msec *= 1000;
191 msec += (end.tv_usec - begin.tv_usec) / 1000;
192
193#ifndef HAVE_STDARG_H
194#error "endTimer required stdarg functions"
195#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000196 va_start(ap, fmt);
197 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000198 va_end(ap);
199
200 fprintf(stderr, " took %ld ms\n", msec);
201}
202#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000203/*
204 * No gettimeofday function, so we have to make do with calling clock.
205 * This is obviously less accurate, but there's little we can do about
206 * that.
207 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000208#ifndef CLOCKS_PER_SEC
209#define CLOCKS_PER_SEC 100
210#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000211
212static clock_t begin, end;
213static void
214startTimer(void)
215{
216 begin = clock();
217}
218static void
219endTimer(const char *fmt, ...)
220{
221 long msec;
222 va_list ap;
223
224 end = clock();
225 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
226
227#ifndef HAVE_STDARG_H
228#error "endTimer required stdarg functions"
229#endif
230 va_start(ap, fmt);
231 vfprintf(stderr, fmt, ap);
232 va_end(ap);
233 fprintf(stderr, " took %ld ms\n", msec);
234}
235#else
236
237/*
238 * We don't have a gettimeofday or time.h, so we just don't do timing
239 */
240static void
241startTimer(void)
242{
243 /*
244 * Do nothing
245 */
246}
247static void
248endTimer(char *format, ...)
249{
250 /*
251 * We cannot do anything because we don't have a timing function
252 */
253#ifdef HAVE_STDARG_H
254 va_start(ap, format);
255 vfprintf(stderr, format, ap);
256 va_end(ap);
257 fprintf(stderr, " was not timed\n", msec);
258#else
259 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
260 * this ?!
261 */
262#endif
263}
264#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000265/************************************************************************
266 * *
267 * HTML ouput *
268 * *
269 ************************************************************************/
270char buffer[50000];
271
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000272static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000273xmlHTMLEncodeSend(void) {
274 char *result;
275
276 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
277 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000278 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000279 xmlFree(result);
280 }
281 buffer[0] = 0;
282}
283
284/**
285 * xmlHTMLPrintFileInfo:
286 * @input: an xmlParserInputPtr input
287 *
288 * Displays the associated file and line informations for the current input
289 */
290
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000291static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000292xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000293 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000294 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000295
296 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000297 if (input != NULL) {
298 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000299 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000300 input->line);
301 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000302 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000303 }
304 }
305 xmlHTMLEncodeSend();
306}
307
308/**
309 * xmlHTMLPrintFileContext:
310 * @input: an xmlParserInputPtr input
311 *
312 * Displays current context within the input content for error tracking
313 */
314
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000315static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000316xmlHTMLPrintFileContext(xmlParserInputPtr input) {
317 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000318 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000319 int n;
320
321 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000322 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000323 cur = input->cur;
324 base = input->base;
325 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
326 cur--;
327 }
328 n = 0;
329 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
330 cur--;
331 if ((*cur == '\n') || (*cur == '\r')) cur++;
332 base = cur;
333 n = 0;
334 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000335 len = strlen(buffer);
336 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
337 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000338 n++;
339 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000340 len = strlen(buffer);
341 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000342 cur = input->cur;
343 while ((*cur == '\n') || (*cur == '\r'))
344 cur--;
345 n = 0;
346 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000347 len = strlen(buffer);
348 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000349 base++;
350 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000351 len = strlen(buffer);
352 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000353 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000354 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000355}
356
357/**
358 * xmlHTMLError:
359 * @ctx: an XML parser context
360 * @msg: the message to display/transmit
361 * @...: extra parameters for the message display
362 *
363 * Display and format an error messages, gives file, line, position and
364 * extra parameters.
365 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000366static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000367xmlHTMLError(void *ctx, const char *msg, ...)
368{
369 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
370 xmlParserInputPtr input;
371 xmlParserInputPtr cur = NULL;
372 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000373 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000374
375 buffer[0] = 0;
376 input = ctxt->input;
377 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
378 cur = input;
379 input = ctxt->inputTab[ctxt->inputNr - 2];
380 }
381
382 xmlHTMLPrintFileInfo(input);
383
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000384 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000385 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000386 len = strlen(buffer);
387 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000388 va_end(args);
389 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000390 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000391
392 xmlHTMLPrintFileContext(input);
393 xmlHTMLEncodeSend();
394}
395
396/**
397 * xmlHTMLWarning:
398 * @ctx: an XML parser context
399 * @msg: the message to display/transmit
400 * @...: extra parameters for the message display
401 *
402 * Display and format a warning messages, gives file, line, position and
403 * extra parameters.
404 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000405static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000406xmlHTMLWarning(void *ctx, const char *msg, ...)
407{
408 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
409 xmlParserInputPtr input;
410 xmlParserInputPtr cur = NULL;
411 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000412 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000413
414 buffer[0] = 0;
415 input = ctxt->input;
416 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
417 cur = input;
418 input = ctxt->inputTab[ctxt->inputNr - 2];
419 }
420
421
422 xmlHTMLPrintFileInfo(input);
423
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000424 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000425 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000426 len = strlen(buffer);
427 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000428 va_end(args);
429 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000430 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000431
432 xmlHTMLPrintFileContext(input);
433 xmlHTMLEncodeSend();
434}
435
436/**
437 * xmlHTMLValidityError:
438 * @ctx: an XML parser context
439 * @msg: the message to display/transmit
440 * @...: extra parameters for the message display
441 *
442 * Display and format an validity error messages, gives file,
443 * line, position and extra parameters.
444 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000445static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000446xmlHTMLValidityError(void *ctx, const char *msg, ...)
447{
448 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
449 xmlParserInputPtr input;
450 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000451 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000452
453 buffer[0] = 0;
454 input = ctxt->input;
455 if ((input->filename == NULL) && (ctxt->inputNr > 1))
456 input = ctxt->inputTab[ctxt->inputNr - 2];
457
458 xmlHTMLPrintFileInfo(input);
459
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000460 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000461 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000462 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000463 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000464 va_end(args);
465 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000466 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000467
468 xmlHTMLPrintFileContext(input);
469 xmlHTMLEncodeSend();
470}
471
472/**
473 * xmlHTMLValidityWarning:
474 * @ctx: an XML parser context
475 * @msg: the message to display/transmit
476 * @...: extra parameters for the message display
477 *
478 * Display and format a validity warning messages, gives file, line,
479 * position and extra parameters.
480 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000481static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000482xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
483{
484 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
485 xmlParserInputPtr input;
486 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000487 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000488
489 buffer[0] = 0;
490 input = ctxt->input;
491 if ((input->filename == NULL) && (ctxt->inputNr > 1))
492 input = ctxt->inputTab[ctxt->inputNr - 2];
493
494 xmlHTMLPrintFileInfo(input);
495
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000496 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000497 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000498 len = strlen(buffer);
499 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000500 va_end(args);
501 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000502 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000503
504 xmlHTMLPrintFileContext(input);
505 xmlHTMLEncodeSend();
506}
507
508/************************************************************************
509 * *
510 * Shell Interface *
511 * *
512 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000513#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000514/**
515 * xmlShellReadline:
516 * @prompt: the prompt value
517 *
518 * Read a string
519 *
520 * Returns a pointer to it or NULL on EOF the caller is expected to
521 * free the returned string.
522 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000523static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000524xmlShellReadline(char *prompt) {
525#ifdef HAVE_LIBREADLINE
526 char *line_read;
527
528 /* Get a line from the user. */
529 line_read = readline (prompt);
530
531 /* If the line has any text in it, save it on the history. */
532 if (line_read && *line_read)
533 add_history (line_read);
534
535 return (line_read);
536#else
537 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000538 char *ret;
539 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000540
541 if (prompt != NULL)
542 fprintf(stdout, "%s", prompt);
543 if (!fgets(line_read, 500, stdin))
544 return(NULL);
545 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000546 len = strlen(line_read);
547 ret = (char *) malloc(len + 1);
548 if (ret != NULL) {
549 memcpy (ret, line_read, len + 1);
550 }
551 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000552#endif
553}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000554#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000555
556/************************************************************************
557 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000558 * I/O Interfaces *
559 * *
560 ************************************************************************/
561
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000562static int myRead(FILE *f, char * buf, int len) {
563 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000564}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000565static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000566 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000567 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000568 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000569}
570
571/************************************************************************
572 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000573 * Stream Test processing *
574 * *
575 ************************************************************************/
576static int count = 0;
577static int elem, attrs;
578
579static void processNode(xmlTextReaderPtr reader) {
Daniel Veillarde59494f2003-01-04 16:35:29 +0000580 xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000581
Daniel Veillarde59494f2003-01-04 16:35:29 +0000582 name = xmlTextReaderName(reader);
583 if (name == NULL)
584 name = xmlStrdup(BAD_CAST "--");
585 value = xmlTextReaderValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000586
Daniel Veillarde59494f2003-01-04 16:35:29 +0000587 printf("%d %d %s %d",
588 xmlTextReaderDepth(reader),
589 xmlTextReaderNodeType(reader),
590 name,
591 xmlTextReaderIsEmptyElement(reader));
592 xmlFree(name);
593 if (value == NULL)
594 printf("\n");
595 else {
596 printf(" %s\n", value);
597 xmlFree(value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000598 }
599}
600
601static void streamFile(char *filename) {
602 xmlTextReaderPtr reader;
603 int ret;
604
605 if (count) {
606 elem = 0;
607 attrs = 0;
608 }
609
610 reader = xmlNewTextReaderFilename(filename);
611 if (reader != NULL) {
612 if (valid)
613 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
614
615 /*
616 * Process all nodes in sequence
617 */
618 ret = xmlTextReaderRead(reader);
619 while (ret == 1) {
620 if (debug)
621 processNode(reader);
622 ret = xmlTextReaderRead(reader);
623 }
624
625 /*
626 * Done, cleanup and status
627 */
628 xmlFreeTextReader(reader);
629 if (ret != 0) {
630 printf("%s : failed to parse\n", filename);
631 }
632 } else {
633 fprintf(stderr, "Unable to open %s\n", filename);
634 }
635}
636
637/************************************************************************
638 * *
639 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000640 * *
641 ************************************************************************/
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000642static void parseAndPrintFile(char *filename) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000643 xmlDocPtr doc = NULL, tmp;
644
Daniel Veillard48b2f892001-02-25 16:11:03 +0000645 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000646 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000647
648
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000649 if (filename == NULL) {
650 if (generate) {
651 xmlNodePtr n;
652
653 doc = xmlNewDoc(BAD_CAST "1.0");
654 n = xmlNewNode(NULL, BAD_CAST "info");
655 xmlNodeSetContent(n, BAD_CAST "abc");
656 xmlDocSetRootElement(doc, n);
657 }
658 }
Daniel Veillard89cad532001-10-22 09:46:13 +0000659#ifdef LIBXML_DOCB_ENABLED
660 /*
661 * build an SGML tree from a string;
662 */
663 else if ((sgml) && (push)) {
664 FILE *f;
665
666 f = fopen(filename, "r");
667 if (f != NULL) {
668 int res, size = 3;
669 char chars[4096];
670 docbParserCtxtPtr ctxt;
671
672 /* if (repeat) */
673 size = 4096;
674 res = fread(chars, 1, 4, f);
675 if (res > 0) {
676 ctxt = docbCreatePushParserCtxt(NULL, NULL,
677 chars, res, filename, 0);
678 while ((res = fread(chars, 1, size, f)) > 0) {
679 docbParseChunk(ctxt, chars, res, 0);
680 }
681 docbParseChunk(ctxt, chars, 0, 1);
682 doc = ctxt->myDoc;
683 docbFreeParserCtxt(ctxt);
684 }
685 fclose(f);
686 }
687 } else if (sgml) {
688 doc = docbParseFile(filename, NULL);
689 }
690#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000691#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000692 else if (html) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000693 doc = htmlParseFile(filename, NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000694 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000695#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000696 else {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000697 /*
698 * build an XML tree from a string;
699 */
700 if (push) {
701 FILE *f;
702
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000703 /* '-' Usually means stdin -<sven@zen.org> */
704 if ((filename[0] == '-') && (filename[1] == 0)) {
705 f = stdin;
706 } else {
707 f = fopen(filename, "r");
708 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000709 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000710 int ret;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000711 int res, size = 3;
712 char chars[1024];
713 xmlParserCtxtPtr ctxt;
714
715 if (repeat)
716 size = 1024;
717 res = fread(chars, 1, 4, f);
718 if (res > 0) {
719 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
720 chars, res, filename);
721 while ((res = fread(chars, 1, size, f)) > 0) {
722 xmlParseChunk(ctxt, chars, res, 0);
723 }
724 xmlParseChunk(ctxt, chars, 0, 1);
725 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000726 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000727 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000728 if (!ret) {
729 xmlFreeDoc(doc);
730 doc = NULL;
731 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000732 }
733 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000734 } else if (testIO) {
735 int ret;
736 FILE *f;
737
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000738 /* '-' Usually means stdin -<sven@zen.org> */
739 if ((filename[0] == '-') && (filename[1] == 0)) {
740 f = stdin;
741 } else {
742 f = fopen(filename, "r");
743 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000744 if (f != NULL) {
745 xmlParserCtxtPtr ctxt;
746
747 ctxt = xmlCreateIOParserCtxt(NULL, NULL,
748 (xmlInputReadCallback) myRead,
749 (xmlInputCloseCallback) myClose,
750 f, XML_CHAR_ENCODING_NONE);
751 xmlParseDocument(ctxt);
752
753 ret = ctxt->wellFormed;
754 doc = ctxt->myDoc;
755 xmlFreeParserCtxt(ctxt);
756 if (!ret) {
757 xmlFreeDoc(doc);
758 doc = NULL;
759 }
760 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000761 } else if (recovery) {
762 doc = xmlRecoverFile(filename);
763 } else if (htmlout) {
764 int ret;
765 xmlParserCtxtPtr ctxt;
766 xmlSAXHandler silent, *old;
767
768 ctxt = xmlCreateFileParserCtxt(filename);
Daniel Veillard88a172f2000-08-04 18:23:10 +0000769
770 if (ctxt == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000771 /* If xmlCreateFileParserCtxt() return NULL something
Daniel Veillard88a172f2000-08-04 18:23:10 +0000772 strange happened so we don't want to do anything. Do
773 we want to print an error message here?
774 <sven@zen.org> */
Daniel Veillard7ebb1ee2000-08-04 18:24:45 +0000775 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000776 } else {
777 memcpy(&silent, ctxt->sax, sizeof(silent));
778 old = ctxt->sax;
779 silent.error = xmlHTMLError;
780 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000781 silent.warning = xmlHTMLWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000782 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000783 silent.warning = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000784 silent.fatalError = xmlHTMLError;
785 ctxt->sax = &silent;
786 ctxt->vctxt.error = xmlHTMLValidityError;
787 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000788 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000789 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000790 ctxt->vctxt.warning = NULL;
791
Daniel Veillard88a172f2000-08-04 18:23:10 +0000792 xmlParseDocument(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000793
Daniel Veillard88a172f2000-08-04 18:23:10 +0000794 ret = ctxt->wellFormed;
795 doc = ctxt->myDoc;
796 ctxt->sax = old;
797 xmlFreeParserCtxt(ctxt);
798 if (!ret) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000799 xmlFreeDoc(doc);
800 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000801 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000802 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000803#ifdef HAVE_SYS_MMAN_H
804 } else if (memory) {
805 int fd;
806 struct stat info;
807 const char *base;
808 if (stat(filename, &info) < 0)
809 return;
810 if ((fd = open(filename, O_RDONLY)) < 0)
811 return;
812 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000813 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000814 return;
815
816 doc = xmlParseMemory((char *) base, info.st_size);
817 munmap((char *) base, info.st_size);
818#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000819 } else if (valid) {
820 int ret;
821 xmlParserCtxtPtr ctxt;
822
823 ctxt = xmlCreateFileParserCtxt(filename);
824
825 if (ctxt == NULL) {
826 doc = NULL;
827 } else {
828 xmlParseDocument(ctxt);
829 if (ctxt->valid == 0)
830 progresult = 4;
831 ret = ctxt->wellFormed;
832 doc = ctxt->myDoc;
833 xmlFreeParserCtxt(ctxt);
834 if (!ret) {
835 xmlFreeDoc(doc);
836 doc = NULL;
837 }
838 }
839 } else {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000840 doc = xmlParseFile(filename);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000841 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000842 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000843
Daniel Veillard88a172f2000-08-04 18:23:10 +0000844 /*
845 * If we don't have a document we might as well give up. Do we
846 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000847 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000848 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000849 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000850 }
851
Daniel Veillard48b2f892001-02-25 16:11:03 +0000852 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000853 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000854 }
855
Daniel Veillard29e43992001-12-13 22:21:58 +0000856 /*
857 * Remove DOCTYPE nodes
858 */
859 if (dropdtd) {
860 xmlDtdPtr dtd;
861
862 dtd = xmlGetIntSubset(doc);
863 if (dtd != NULL) {
864 xmlUnlinkNode((xmlNodePtr)dtd);
865 xmlFreeDtd(dtd);
866 }
867 }
868
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000869#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000870 if (xinclude) {
871 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000872 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000873 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000874 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000875 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000876 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000877 }
878 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000879#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000880
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000881#ifdef LIBXML_DEBUG_ENABLED
882 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000883 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000884 */
885 if (shell)
886 xmlShell(doc, filename, xmlShellReadline, stdout);
887#endif
888
889 /*
890 * test intermediate copy if needed.
891 */
892 if (copy) {
893 tmp = doc;
894 doc = xmlCopyDoc(doc, 1);
895 xmlFreeDoc(tmp);
896 }
897
898 if ((insert) && (!html)) {
899 const xmlChar* list[256];
900 int nb, i;
901 xmlNodePtr node;
902
903 if (doc->children != NULL) {
904 node = doc->children;
905 while ((node != NULL) && (node->last == NULL)) node = node->next;
906 if (node != NULL) {
907 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
908 if (nb < 0) {
909 printf("could not get valid list of elements\n");
910 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000911 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000912 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000913 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000914 nb);
915 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000916 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000917 }
918 }
919 }
920 }
921 }else if (noout == 0) {
922 /*
923 * print it.
924 */
925#ifdef LIBXML_DEBUG_ENABLED
926 if (!debug) {
927#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +0000928 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000929 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000930 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +0000931#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000932 if (memory) {
933 xmlChar *result;
934 int len;
935
936 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +0000937 if ( format ) {
938 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
939 } else {
940 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
941 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000942 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +0000943 if (format)
944 xmlDocDumpFormatMemory(doc, &result, &len, 1);
945 else
946 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000947 }
948 if (result == NULL) {
949 fprintf(stderr, "Failed to save\n");
950 } else {
951 write(1, result, len);
952 xmlFree(result);
953 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +0000954 } else
955#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000956 if (compress) {
957 xmlSaveFile(output ? output : "-", doc);
958 }
Daniel Veillardd536f702001-11-08 17:32:47 +0000959 else if (encoding != NULL) {
960 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000961 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
962 }
Daniel Veillardd536f702001-11-08 17:32:47 +0000963 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000964 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +0000965 }
966 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000967 else if (format) {
968 xmlSaveFormatFile(output ? output : "-", doc, 1);
969 }
970 else {
971 FILE *out;
972 if (output == NULL)
973 out = stdout;
974 else {
975 out = fopen(output,"wb");
976 }
977 xmlDocDump(out, doc);
978
979 if (output)
980 fclose(out);
981 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000982 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000983 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000984 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000985#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000986 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000987 FILE *out;
988 if (output == NULL)
989 out = stdout;
990 else {
991 out = fopen(output,"wb");
992 }
993 xmlDebugDumpDocument(out, doc);
994
995 if (output)
996 fclose(out);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000997 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000998#endif
999 }
1000
1001 /*
1002 * A posteriori validation test
1003 */
Daniel Veillardcd429612000-10-11 15:57:05 +00001004 if (dtdvalid != NULL) {
1005 xmlDtdPtr dtd;
1006
Daniel Veillard48b2f892001-02-25 16:11:03 +00001007 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001008 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001009 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001010 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001011 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001012 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001013 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001014 if (dtd == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001015 xmlGenericError(xmlGenericErrorContext,
1016 "Could not parse DTD %s\n", dtdvalid);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001017 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001018 } else {
1019 xmlValidCtxt cvp;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001020 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001021 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001022 }
Daniel Veillard01db67c2001-12-18 07:09:59 +00001023 cvp.userData = (void *) stderr;
1024 cvp.error = (xmlValidityErrorFunc) fprintf;
1025 cvp.warning = (xmlValidityWarningFunc) fprintf;
Daniel Veillardcd429612000-10-11 15:57:05 +00001026 if (!xmlValidateDtd(&cvp, doc, dtd)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001027 xmlGenericError(xmlGenericErrorContext,
1028 "Document %s does not validate against %s\n",
Daniel Veillardcd429612000-10-11 15:57:05 +00001029 filename, dtdvalid);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001030 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001031 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001032 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001033 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001034 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001035 xmlFreeDtd(dtd);
1036 }
1037 } else if (postvalid) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001038 xmlValidCtxt cvp;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001039 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001040 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001041 }
Daniel Veillardb7664f42001-08-19 13:00:43 +00001042 cvp.userData = (void *) stderr;
1043 cvp.error = (xmlValidityErrorFunc) fprintf;
1044 cvp.warning = (xmlValidityWarningFunc) fprintf;
Daniel Veillardcd429612000-10-11 15:57:05 +00001045 if (!xmlValidateDocument(&cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001046 xmlGenericError(xmlGenericErrorContext,
1047 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001048 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001049 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001050 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001051 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001052 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001053 }
1054
1055#ifdef LIBXML_DEBUG_ENABLED
1056 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001057 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001058#endif
1059
1060 /*
1061 * free it.
1062 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001063 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001064 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001065 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001066 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001067 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001068 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001069 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001070}
1071
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001072/************************************************************************
1073 * *
1074 * Usage and Main *
1075 * *
1076 ************************************************************************/
1077
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001078static void showVersion(const char *name) {
1079 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1080 fprintf(stderr, " compiled with: ");
1081#ifdef LIBXML_FTP_ENABLED
1082 fprintf(stderr, "FTP ");
1083#endif
1084#ifdef LIBXML_HTTP_ENABLED
1085 fprintf(stderr, "HTTP ");
1086#endif
1087#ifdef LIBXML_HTML_ENABLED
1088 fprintf(stderr, "HTML ");
1089#endif
1090#ifdef LIBXML_C14N_ENABLED
1091 fprintf(stderr, "C14N ");
1092#endif
1093#ifdef LIBXML_CATALOG_ENABLED
1094 fprintf(stderr, "Catalog ");
1095#endif
1096#ifdef LIBXML_DOCB_ENABLED
1097 fprintf(stderr, "DocBook ");
1098#endif
1099#ifdef LIBXML_XPATH_ENABLED
1100 fprintf(stderr, "XPath ");
1101#endif
1102#ifdef LIBXML_XPTR_ENABLED
1103 fprintf(stderr, "XPointer ");
1104#endif
1105#ifdef LIBXML_XINCLUDE_ENABLED
1106 fprintf(stderr, "XInclude ");
1107#endif
1108#ifdef LIBXML_ICONV_ENABLED
1109 fprintf(stderr, "Iconv ");
1110#endif
1111#ifdef DEBUG_MEMORY_LOCATION
1112 fprintf(stderr, "MemDebug ");
1113#endif
1114#ifdef LIBXML_UNICODE_ENABLED
1115 fprintf(stderr, "Unicode ");
1116#endif
1117#ifdef LIBXML_REGEXP_ENABLED
1118 fprintf(stderr, "Regexps ");
1119#endif
1120#ifdef LIBXML_AUTOMATA_ENABLED
1121 fprintf(stderr, "Automata ");
1122#endif
1123#ifdef LIBXML_SCHEMAS_ENABLED
1124 fprintf(stderr, "Schemas ");
1125#endif
1126 fprintf(stderr, "\n");
1127}
1128
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001129static void usage(const char *name) {
1130 printf("Usage : %s [options] XMLfiles ...\n", name);
1131 printf("\tParse the XML files and output the result of the parsing\n");
1132 printf("\t--version : display the version of the XML library used\n");
1133#ifdef LIBXML_DEBUG_ENABLED
1134 printf("\t--debug : dump a debug tree of the in-memory document\n");
1135 printf("\t--shell : run a navigating shell\n");
1136 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001137#else
1138 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001139#endif
1140 printf("\t--copy : used to test the internal copy implementation\n");
1141 printf("\t--recover : output what was parsable on broken XML documents\n");
1142 printf("\t--noent : substitute entity references by their value\n");
1143 printf("\t--noout : don't output the result tree\n");
1144 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001145 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001146 printf("\t--valid : validate the document in addition to std well-formed check\n");
1147 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1148 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
1149 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001150 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001151 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1152 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001153#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001154 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001155#endif
Daniel Veillard89cad532001-10-22 09:46:13 +00001156#ifdef LIBXML_DOCB_ENABLED
1157 printf("\t--sgml : use the DocBook SGML parser\n");
1158#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001159#ifdef LIBXML_HTML_ENABLED
1160 printf("\t--html : use the HTML parser\n");
1161#endif
1162 printf("\t--push : use the push mode of the parser\n");
1163#ifdef HAVE_SYS_MMAN_H
1164 printf("\t--memory : parse from memory\n");
1165#endif
1166 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1167 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillard90493a92001-08-14 14:12:47 +00001168 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001169 printf("\t--testIO : test user I/O support\n");
1170 printf("\t--encode encoding : output in the given encoding\n");
1171#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001172 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1173 printf("\t otherwise XML Catalogs starting from \n");
1174 printf("\t file:///etc/xml/catalog are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001175 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001176#endif
1177 printf("\t--auto : generate a small doc on the fly\n");
1178#ifdef LIBXML_XINCLUDE_ENABLED
1179 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001180#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001181 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001182 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard29e43992001-12-13 22:21:58 +00001183 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
Daniel Veillard7704fb12003-01-03 16:19:51 +00001184 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001185 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001186 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1187 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001188}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001189
1190static void registerNode(xmlNodePtr node)
1191{
1192 node->_private = malloc(sizeof(long));
1193 *(long*)node->_private = (long) 0x81726354;
1194}
1195
1196static void deregisterNode(xmlNodePtr node)
1197{
1198 assert(node->_private != NULL);
1199 assert(*(long*)node->_private == (long) 0x81726354);
1200 free(node->_private);
1201}
1202
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001203int
1204main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001205 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001206 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001207 int version = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001208
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001209 if (argc <= 1) {
1210 usage(argv[0]);
1211 return(1);
1212 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001213 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001214 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001215 if (!strcmp(argv[i], "-"))
1216 break;
1217
1218 if (argv[i][0] != '-')
1219 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001220 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1221 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001222 else
1223#ifdef LIBXML_DEBUG_ENABLED
1224 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001225 (!strcmp(argv[i], "--shell"))) {
1226 shell++;
1227 noout = 1;
1228 } else
1229#endif
1230 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1231 copy++;
1232 else if ((!strcmp(argv[i], "-recover")) ||
1233 (!strcmp(argv[i], "--recover")))
1234 recovery++;
1235 else if ((!strcmp(argv[i], "-noent")) ||
1236 (!strcmp(argv[i], "--noent")))
1237 noent++;
Daniel Veillard4ec885a2001-06-17 10:31:07 +00001238 else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001239 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001240 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001241 version = 1;
1242 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001243 (!strcmp(argv[i], "--noout")))
1244 noout++;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001245 else if ((!strcmp(argv[i], "-o")) ||
1246 (!strcmp(argv[i], "-output")) ||
1247 (!strcmp(argv[i], "--output"))) {
1248 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001249 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001250 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001251 else if ((!strcmp(argv[i], "-htmlout")) ||
1252 (!strcmp(argv[i], "--htmlout")))
1253 htmlout++;
Daniel Veillard89cad532001-10-22 09:46:13 +00001254#ifdef LIBXML_DOCB_ENABLED
1255 else if ((!strcmp(argv[i], "-sgml")) ||
1256 (!strcmp(argv[i], "--sgml"))) {
1257 sgml++;
1258 }
1259#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001260#ifdef LIBXML_HTML_ENABLED
1261 else if ((!strcmp(argv[i], "-html")) ||
1262 (!strcmp(argv[i], "--html"))) {
1263 html++;
1264 }
1265#endif /* LIBXML_HTML_ENABLED */
1266 else if ((!strcmp(argv[i], "-nowrap")) ||
1267 (!strcmp(argv[i], "--nowrap")))
1268 nowrap++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001269 else if ((!strcmp(argv[i], "-loaddtd")) ||
1270 (!strcmp(argv[i], "--loaddtd")))
1271 loaddtd++;
Daniel Veillard48da9102001-08-07 01:10:10 +00001272 else if ((!strcmp(argv[i], "-dtdattr")) ||
1273 (!strcmp(argv[i], "--dtdattr"))) {
1274 loaddtd++;
1275 dtdattrs++;
1276 } else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001277 (!strcmp(argv[i], "--valid")))
1278 valid++;
1279 else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001280 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001281 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001282 loaddtd++;
1283 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001284 (!strcmp(argv[i], "--dtdvalid"))) {
1285 i++;
1286 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001287 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001288 }
Daniel Veillard29e43992001-12-13 22:21:58 +00001289 else if ((!strcmp(argv[i], "-dropdtd")) ||
1290 (!strcmp(argv[i], "--dropdtd")))
1291 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001292 else if ((!strcmp(argv[i], "-insert")) ||
1293 (!strcmp(argv[i], "--insert")))
1294 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001295 else if ((!strcmp(argv[i], "-timing")) ||
1296 (!strcmp(argv[i], "--timing")))
1297 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001298 else if ((!strcmp(argv[i], "-auto")) ||
1299 (!strcmp(argv[i], "--auto")))
1300 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001301 else if ((!strcmp(argv[i], "-repeat")) ||
1302 (!strcmp(argv[i], "--repeat")))
1303 repeat++;
1304 else if ((!strcmp(argv[i], "-push")) ||
1305 (!strcmp(argv[i], "--push")))
1306 push++;
Daniel Veillard46e370e2000-07-21 20:32:03 +00001307#ifdef HAVE_SYS_MMAN_H
1308 else if ((!strcmp(argv[i], "-memory")) ||
1309 (!strcmp(argv[i], "--memory")))
1310 memory++;
1311#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001312 else if ((!strcmp(argv[i], "-testIO")) ||
1313 (!strcmp(argv[i], "--testIO")))
1314 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001315#ifdef LIBXML_XINCLUDE_ENABLED
1316 else if ((!strcmp(argv[i], "-xinclude")) ||
1317 (!strcmp(argv[i], "--xinclude")))
1318 xinclude++;
1319#endif
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001320#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001321 else if ((!strcmp(argv[i], "-compress")) ||
1322 (!strcmp(argv[i], "--compress"))) {
1323 compress++;
1324 xmlSetCompressMode(9);
1325 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001326#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001327 else if ((!strcmp(argv[i], "-nowarning")) ||
1328 (!strcmp(argv[i], "--nowarning"))) {
1329 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001330 xmlPedanticParserDefault(0);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001331 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001332 else if ((!strcmp(argv[i], "-pedantic")) ||
1333 (!strcmp(argv[i], "--pedantic"))) {
1334 xmlGetWarningsDefaultValue = 1;
1335 xmlPedanticParserDefault(1);
1336 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001337#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001338 else if ((!strcmp(argv[i], "-debugent")) ||
1339 (!strcmp(argv[i], "--debugent"))) {
1340 debugent++;
1341 xmlParserDebugEntities = 1;
1342 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001343#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001344#ifdef LIBXML_CATALOG_ENABLED
1345 else if ((!strcmp(argv[i], "-catalogs")) ||
1346 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001347 catalogs++;
1348 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1349 (!strcmp(argv[i], "--nocatalogs"))) {
1350 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001351 }
1352#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001353 else if ((!strcmp(argv[i], "-encode")) ||
1354 (!strcmp(argv[i], "--encode"))) {
1355 i++;
1356 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001357 /*
1358 * OK it's for testing purposes
1359 */
1360 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001361 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001362 else if ((!strcmp(argv[i], "-noblanks")) ||
1363 (!strcmp(argv[i], "--noblanks"))) {
1364 noblanks++;
1365 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001366 }
1367 else if ((!strcmp(argv[i], "-format")) ||
1368 (!strcmp(argv[i], "--format"))) {
1369 noblanks++;
1370 format++;
1371 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001372 }
1373 else if ((!strcmp(argv[i], "-stream")) ||
1374 (!strcmp(argv[i], "--stream"))) {
1375 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001376 }
1377 else if ((!strcmp(argv[i], "-chkregister")) ||
1378 (!strcmp(argv[i], "--chkregister"))) {
1379 chkregister++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001380 } else {
1381 fprintf(stderr, "Unknown option %s\n", argv[i]);
1382 usage(argv[0]);
1383 return(1);
1384 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001385 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001386
1387#ifdef LIBXML_CATALOG_ENABLED
1388 if (nocatalogs == 0) {
1389 if (catalogs) {
1390 const char *catal;
1391
1392 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001393 if (catal != NULL) {
1394 xmlLoadCatalogs(catal);
1395 } else {
1396 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1397 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001398 }
1399 }
1400#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001401
1402 if (chkregister) {
1403 xmlRegisterNodeDefault(registerNode);
1404 xmlDeregisterNodeDefault(deregisterNode);
1405 }
1406
Daniel Veillardd9bad132001-07-23 19:39:43 +00001407 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001408 if (loaddtd != 0)
1409 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1410 if (dtdattrs)
1411 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001412 if (noent != 0) xmlSubstituteEntitiesDefault(1);
1413 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
1414 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001415 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001416 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001417 xmlGenericError(xmlGenericErrorContext,
1418 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1419 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001420 "<html><head><title>%s output</title></head>\n",
1421 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001422 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001423 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1424 argv[0]);
1425 }
1426 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001427 if ((!strcmp(argv[i], "-encode")) ||
1428 (!strcmp(argv[i], "--encode"))) {
1429 i++;
1430 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001431 } else if ((!strcmp(argv[i], "-o")) ||
1432 (!strcmp(argv[i], "-output")) ||
1433 (!strcmp(argv[i], "--output"))) {
1434 i++;
1435 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001436 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001437 if ((!strcmp(argv[i], "-dtdvalid")) ||
1438 (!strcmp(argv[i], "--dtdvalid"))) {
1439 i++;
1440 continue;
1441 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001442 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001443 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001444 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001445 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001446 if (repeat) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001447 for (acount = 0;acount < 100 * repeat;acount++)
1448 if (stream != 0)
1449 streamFile(argv[i]);
1450 else
1451 parseAndPrintFile(argv[i]);
1452 } else {
1453 if (stream != 0)
1454 streamFile(argv[i]);
1455 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001456 parseAndPrintFile(argv[i]);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001457 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001458 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001459 if ((timing) && (repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001460 endTimer("100 iterations");
Daniel Veillarda7866932001-12-04 13:14:44 +00001461 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001462 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001463 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001464 if (generate)
1465 parseAndPrintFile(NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001466 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001467 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001468 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001469 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001470 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001471 }
1472 xmlCleanupParser();
1473 xmlMemoryDump();
1474
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001475 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001476}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001477