blob: fce097f12345c26ece53555654085cbf6f67e9d6 [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 Veillard3c5ed912002-01-08 10:36:16 +000014#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillard2d90de42001-04-16 17:46:18 +000015#ifdef _MSC_VER
16#include <winsock2.h>
17#pragma comment(lib, "ws2_32.lib")
18#define gettimeofday(p1,p2)
Daniel Veillardf216d462002-02-08 13:44:24 +000019#include <time.h>
Daniel Veillard28ae6362001-07-14 16:44:32 +000020#else /* _MSC_VER */
21#include <sys/time.h>
Daniel Veillard2d90de42001-04-16 17:46:18 +000022#endif /* _MSC_VER */
23#else /* _WIN32 */
Daniel Veillarded472f32001-12-13 08:48:14 +000024#ifdef HAVE_SYS_TIME_H
Daniel Veillard48b2f892001-02-25 16:11:03 +000025#include <sys/time.h>
Daniel Veillarded472f32001-12-13 08:48:14 +000026#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +000027#ifdef HAVE_TIME_H
28#include <time.h>
29#endif
Daniel Veillard2d90de42001-04-16 17:46:18 +000030#endif /* _WIN32 */
Daniel Veillard48b2f892001-02-25 16:11:03 +000031
Daniel Veillard90bc3712002-03-07 15:12:58 +000032#ifdef HAVE_SYS_TIMEB_H
33#include <sys/timeb.h>
34#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000035
36#ifdef HAVE_SYS_TYPES_H
37#include <sys/types.h>
38#endif
39#ifdef HAVE_SYS_STAT_H
40#include <sys/stat.h>
41#endif
42#ifdef HAVE_FCNTL_H
43#include <fcntl.h>
44#endif
45#ifdef HAVE_UNISTD_H
46#include <unistd.h>
47#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000048#ifdef HAVE_SYS_MMAN_H
49#include <sys/mman.h>
Daniel Veillard87b95392000-08-12 21:12:04 +000050/* seems needed for Solaris */
51#ifndef MAP_FAILED
52#define MAP_FAILED ((void *) -1)
53#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000054#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000055#ifdef HAVE_STDLIB_H
56#include <stdlib.h>
57#endif
58#ifdef HAVE_LIBREADLINE
59#include <readline/readline.h>
60#ifdef HAVE_LIBHISTORY
61#include <readline/history.h>
62#endif
63#endif
64
65#include <libxml/xmlmemory.h>
66#include <libxml/parser.h>
67#include <libxml/parserInternals.h>
68#include <libxml/HTMLparser.h>
69#include <libxml/HTMLtree.h>
70#include <libxml/tree.h>
71#include <libxml/xpath.h>
72#include <libxml/debugXML.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000073#include <libxml/xmlerror.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000074#ifdef LIBXML_XINCLUDE_ENABLED
75#include <libxml/xinclude.h>
76#endif
Daniel Veillard81418e32001-05-22 15:08:55 +000077#ifdef LIBXML_CATALOG_ENABLED
78#include <libxml/catalog.h>
79#endif
Daniel Veillard89cad532001-10-22 09:46:13 +000080#ifdef LIBXML_DOCB_ENABLED
81#include <libxml/DOCBparser.h>
82#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000083#include <libxml/globals.h>
Daniel Veillardce8b83b2000-04-05 18:38:42 +000084
85#ifdef LIBXML_DEBUG_ENABLED
86static int debug = 0;
87static int shell = 0;
88static int debugent = 0;
89#endif
90static int copy = 0;
91static int recovery = 0;
92static int noent = 0;
93static int noout = 0;
94static int nowrap = 0;
95static int valid = 0;
96static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +000097static char * dtdvalid = NULL;
Daniel Veillardce8b83b2000-04-05 18:38:42 +000098static int repeat = 0;
99static int insert = 0;
100static int compress = 0;
Daniel Veillard89cad532001-10-22 09:46:13 +0000101#ifdef LIBXML_DOCB_ENABLED
102static int sgml = 0;
103#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000104static int html = 0;
105static int htmlout = 0;
106static int push = 0;
Daniel Veillard46e370e2000-07-21 20:32:03 +0000107#ifdef HAVE_SYS_MMAN_H
108static int memory = 0;
109#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000110static int noblanks = 0;
Daniel Veillard90493a92001-08-14 14:12:47 +0000111static int format = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000112static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000113static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000114#ifdef LIBXML_XINCLUDE_ENABLED
115static int xinclude = 0;
116#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000117static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000118static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000119static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000120static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000121static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000122static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000123#ifdef LIBXML_CATALOG_ENABLED
124static int catalogs = 0;
125static int nocatalogs = 0;
126#endif
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000127static const char *output = NULL;
128
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000129
Daniel Veillard01db67c2001-12-18 07:09:59 +0000130/*
131 * Internal timing routines to remove the necessity to have unix-specific
132 * function calls
133 */
134
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000135#ifndef HAVE_GETTIMEOFDAY
136#ifdef HAVE_SYS_TIMEB_H
137#ifdef HAVE_SYS_TIME_H
138#ifdef HAVE_FTIME
139
140int
141my_gettimeofday(struct timeval *tvp, void *tzp)
142{
143 struct timeb timebuffer;
144
145 ftime(&timebuffer);
146 if (tvp) {
147 tvp->tv_sec = timebuffer.time;
148 tvp->tv_usec = timebuffer.millitm * 1000L;
149 }
150 return (0);
151}
152#define HAVE_GETTIMEOFDAY 1
153#define gettimeofday my_gettimeofday
154
155#endif /* HAVE_FTIME */
156#endif /* HAVE_SYS_TIME_H */
157#endif /* HAVE_SYS_TIMEB_H */
158#endif /* !HAVE_GETTIMEOFDAY */
159
Daniel Veillard01db67c2001-12-18 07:09:59 +0000160#if defined(HAVE_GETTIMEOFDAY)
161static struct timeval begin, end;
162
163/*
164 * startTimer: call where you want to start timing
165 */
166static void
167startTimer(void)
168{
169 gettimeofday(&begin, NULL);
170}
171
172/*
173 * endTimer: call where you want to stop timing and to print out a
174 * message about the timing performed; format is a printf
175 * type argument
176 */
177static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000178endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000179{
180 long msec;
181 va_list ap;
182
183 gettimeofday(&end, NULL);
184 msec = end.tv_sec - begin.tv_sec;
185 msec *= 1000;
186 msec += (end.tv_usec - begin.tv_usec) / 1000;
187
188#ifndef HAVE_STDARG_H
189#error "endTimer required stdarg functions"
190#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000191 va_start(ap, fmt);
192 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000193 va_end(ap);
194
195 fprintf(stderr, " took %ld ms\n", msec);
196}
197#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000198/*
199 * No gettimeofday function, so we have to make do with calling clock.
200 * This is obviously less accurate, but there's little we can do about
201 * that.
202 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000203#ifndef CLOCKS_PER_SEC
204#define CLOCKS_PER_SEC 100
205#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000206
207static clock_t begin, end;
208static void
209startTimer(void)
210{
211 begin = clock();
212}
213static void
214endTimer(const char *fmt, ...)
215{
216 long msec;
217 va_list ap;
218
219 end = clock();
220 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
221
222#ifndef HAVE_STDARG_H
223#error "endTimer required stdarg functions"
224#endif
225 va_start(ap, fmt);
226 vfprintf(stderr, fmt, ap);
227 va_end(ap);
228 fprintf(stderr, " took %ld ms\n", msec);
229}
230#else
231
232/*
233 * We don't have a gettimeofday or time.h, so we just don't do timing
234 */
235static void
236startTimer(void)
237{
238 /*
239 * Do nothing
240 */
241}
242static void
243endTimer(char *format, ...)
244{
245 /*
246 * We cannot do anything because we don't have a timing function
247 */
248#ifdef HAVE_STDARG_H
249 va_start(ap, format);
250 vfprintf(stderr, format, ap);
251 va_end(ap);
252 fprintf(stderr, " was not timed\n", msec);
253#else
254 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
255 * this ?!
256 */
257#endif
258}
259#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000260/************************************************************************
261 * *
262 * HTML ouput *
263 * *
264 ************************************************************************/
265char buffer[50000];
266
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000267static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000268xmlHTMLEncodeSend(void) {
269 char *result;
270
271 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
272 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000273 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000274 xmlFree(result);
275 }
276 buffer[0] = 0;
277}
278
279/**
280 * xmlHTMLPrintFileInfo:
281 * @input: an xmlParserInputPtr input
282 *
283 * Displays the associated file and line informations for the current input
284 */
285
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000286static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000287xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000288 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000289 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000290
291 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000292 if (input != NULL) {
293 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000294 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000295 input->line);
296 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000297 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000298 }
299 }
300 xmlHTMLEncodeSend();
301}
302
303/**
304 * xmlHTMLPrintFileContext:
305 * @input: an xmlParserInputPtr input
306 *
307 * Displays current context within the input content for error tracking
308 */
309
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000310static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000311xmlHTMLPrintFileContext(xmlParserInputPtr input) {
312 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000313 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000314 int n;
315
316 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000317 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000318 cur = input->cur;
319 base = input->base;
320 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
321 cur--;
322 }
323 n = 0;
324 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
325 cur--;
326 if ((*cur == '\n') || (*cur == '\r')) cur++;
327 base = cur;
328 n = 0;
329 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000330 len = strlen(buffer);
331 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
332 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000333 n++;
334 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000335 len = strlen(buffer);
336 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000337 cur = input->cur;
338 while ((*cur == '\n') || (*cur == '\r'))
339 cur--;
340 n = 0;
341 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000342 len = strlen(buffer);
343 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000344 base++;
345 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000346 len = strlen(buffer);
347 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000348 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000349 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000350}
351
352/**
353 * xmlHTMLError:
354 * @ctx: an XML parser context
355 * @msg: the message to display/transmit
356 * @...: extra parameters for the message display
357 *
358 * Display and format an error messages, gives file, line, position and
359 * extra parameters.
360 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000361static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000362xmlHTMLError(void *ctx, const char *msg, ...)
363{
364 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
365 xmlParserInputPtr input;
366 xmlParserInputPtr cur = NULL;
367 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000368 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000369
370 buffer[0] = 0;
371 input = ctxt->input;
372 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
373 cur = input;
374 input = ctxt->inputTab[ctxt->inputNr - 2];
375 }
376
377 xmlHTMLPrintFileInfo(input);
378
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000379 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000380 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000381 len = strlen(buffer);
382 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000383 va_end(args);
384 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000385 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000386
387 xmlHTMLPrintFileContext(input);
388 xmlHTMLEncodeSend();
389}
390
391/**
392 * xmlHTMLWarning:
393 * @ctx: an XML parser context
394 * @msg: the message to display/transmit
395 * @...: extra parameters for the message display
396 *
397 * Display and format a warning messages, gives file, line, position and
398 * extra parameters.
399 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000400static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000401xmlHTMLWarning(void *ctx, const char *msg, ...)
402{
403 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
404 xmlParserInputPtr input;
405 xmlParserInputPtr cur = NULL;
406 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000407 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000408
409 buffer[0] = 0;
410 input = ctxt->input;
411 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
412 cur = input;
413 input = ctxt->inputTab[ctxt->inputNr - 2];
414 }
415
416
417 xmlHTMLPrintFileInfo(input);
418
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000419 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000420 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000421 len = strlen(buffer);
422 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000423 va_end(args);
424 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000425 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000426
427 xmlHTMLPrintFileContext(input);
428 xmlHTMLEncodeSend();
429}
430
431/**
432 * xmlHTMLValidityError:
433 * @ctx: an XML parser context
434 * @msg: the message to display/transmit
435 * @...: extra parameters for the message display
436 *
437 * Display and format an validity error messages, gives file,
438 * line, position and extra parameters.
439 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000440static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000441xmlHTMLValidityError(void *ctx, const char *msg, ...)
442{
443 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
444 xmlParserInputPtr input;
445 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000446 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000447
448 buffer[0] = 0;
449 input = ctxt->input;
450 if ((input->filename == NULL) && (ctxt->inputNr > 1))
451 input = ctxt->inputTab[ctxt->inputNr - 2];
452
453 xmlHTMLPrintFileInfo(input);
454
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000455 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000456 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000457 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000458 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000459 va_end(args);
460 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000461 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000462
463 xmlHTMLPrintFileContext(input);
464 xmlHTMLEncodeSend();
465}
466
467/**
468 * xmlHTMLValidityWarning:
469 * @ctx: an XML parser context
470 * @msg: the message to display/transmit
471 * @...: extra parameters for the message display
472 *
473 * Display and format a validity warning messages, gives file, line,
474 * position and extra parameters.
475 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000476static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000477xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
478{
479 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
480 xmlParserInputPtr input;
481 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000482 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000483
484 buffer[0] = 0;
485 input = ctxt->input;
486 if ((input->filename == NULL) && (ctxt->inputNr > 1))
487 input = ctxt->inputTab[ctxt->inputNr - 2];
488
489 xmlHTMLPrintFileInfo(input);
490
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000491 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000492 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000493 len = strlen(buffer);
494 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000495 va_end(args);
496 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000497 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000498
499 xmlHTMLPrintFileContext(input);
500 xmlHTMLEncodeSend();
501}
502
503/************************************************************************
504 * *
505 * Shell Interface *
506 * *
507 ************************************************************************/
508/**
509 * xmlShellReadline:
510 * @prompt: the prompt value
511 *
512 * Read a string
513 *
514 * Returns a pointer to it or NULL on EOF the caller is expected to
515 * free the returned string.
516 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000517static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000518xmlShellReadline(char *prompt) {
519#ifdef HAVE_LIBREADLINE
520 char *line_read;
521
522 /* Get a line from the user. */
523 line_read = readline (prompt);
524
525 /* If the line has any text in it, save it on the history. */
526 if (line_read && *line_read)
527 add_history (line_read);
528
529 return (line_read);
530#else
531 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000532 char *ret;
533 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000534
535 if (prompt != NULL)
536 fprintf(stdout, "%s", prompt);
537 if (!fgets(line_read, 500, stdin))
538 return(NULL);
539 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000540 len = strlen(line_read);
541 ret = (char *) malloc(len + 1);
542 if (ret != NULL) {
543 memcpy (ret, line_read, len + 1);
544 }
545 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000546#endif
547}
548
549/************************************************************************
550 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000551 * I/O Interfaces *
552 * *
553 ************************************************************************/
554
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000555static int myRead(FILE *f, char * buf, int len) {
556 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000557}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000558static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000559 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000560 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000561 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000562}
563
564/************************************************************************
565 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000566 * Test processing *
567 * *
568 ************************************************************************/
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000569static void parseAndPrintFile(char *filename) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000570 xmlDocPtr doc = NULL, tmp;
571
Daniel Veillard48b2f892001-02-25 16:11:03 +0000572 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000573 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000574
575
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000576 if (filename == NULL) {
577 if (generate) {
578 xmlNodePtr n;
579
580 doc = xmlNewDoc(BAD_CAST "1.0");
581 n = xmlNewNode(NULL, BAD_CAST "info");
582 xmlNodeSetContent(n, BAD_CAST "abc");
583 xmlDocSetRootElement(doc, n);
584 }
585 }
Daniel Veillard89cad532001-10-22 09:46:13 +0000586#ifdef LIBXML_DOCB_ENABLED
587 /*
588 * build an SGML tree from a string;
589 */
590 else if ((sgml) && (push)) {
591 FILE *f;
592
593 f = fopen(filename, "r");
594 if (f != NULL) {
595 int res, size = 3;
596 char chars[4096];
597 docbParserCtxtPtr ctxt;
598
599 /* if (repeat) */
600 size = 4096;
601 res = fread(chars, 1, 4, f);
602 if (res > 0) {
603 ctxt = docbCreatePushParserCtxt(NULL, NULL,
604 chars, res, filename, 0);
605 while ((res = fread(chars, 1, size, f)) > 0) {
606 docbParseChunk(ctxt, chars, res, 0);
607 }
608 docbParseChunk(ctxt, chars, 0, 1);
609 doc = ctxt->myDoc;
610 docbFreeParserCtxt(ctxt);
611 }
612 fclose(f);
613 }
614 } else if (sgml) {
615 doc = docbParseFile(filename, NULL);
616 }
617#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000618#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000619 else if (html) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000620 doc = htmlParseFile(filename, NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000621 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000622#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000623 else {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000624 /*
625 * build an XML tree from a string;
626 */
627 if (push) {
628 FILE *f;
629
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000630 /* '-' Usually means stdin -<sven@zen.org> */
631 if ((filename[0] == '-') && (filename[1] == 0)) {
632 f = stdin;
633 } else {
634 f = fopen(filename, "r");
635 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000636 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000637 int ret;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000638 int res, size = 3;
639 char chars[1024];
640 xmlParserCtxtPtr ctxt;
641
642 if (repeat)
643 size = 1024;
644 res = fread(chars, 1, 4, f);
645 if (res > 0) {
646 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
647 chars, res, filename);
648 while ((res = fread(chars, 1, size, f)) > 0) {
649 xmlParseChunk(ctxt, chars, res, 0);
650 }
651 xmlParseChunk(ctxt, chars, 0, 1);
652 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000653 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000654 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000655 if (!ret) {
656 xmlFreeDoc(doc);
657 doc = NULL;
658 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000659 }
660 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000661 } else if (testIO) {
662 int ret;
663 FILE *f;
664
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000665 /* '-' Usually means stdin -<sven@zen.org> */
666 if ((filename[0] == '-') && (filename[1] == 0)) {
667 f = stdin;
668 } else {
669 f = fopen(filename, "r");
670 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000671 if (f != NULL) {
672 xmlParserCtxtPtr ctxt;
673
674 ctxt = xmlCreateIOParserCtxt(NULL, NULL,
675 (xmlInputReadCallback) myRead,
676 (xmlInputCloseCallback) myClose,
677 f, XML_CHAR_ENCODING_NONE);
678 xmlParseDocument(ctxt);
679
680 ret = ctxt->wellFormed;
681 doc = ctxt->myDoc;
682 xmlFreeParserCtxt(ctxt);
683 if (!ret) {
684 xmlFreeDoc(doc);
685 doc = NULL;
686 }
687 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000688 } else if (recovery) {
689 doc = xmlRecoverFile(filename);
690 } else if (htmlout) {
691 int ret;
692 xmlParserCtxtPtr ctxt;
693 xmlSAXHandler silent, *old;
694
695 ctxt = xmlCreateFileParserCtxt(filename);
Daniel Veillard88a172f2000-08-04 18:23:10 +0000696
697 if (ctxt == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000698 /* If xmlCreateFileParserCtxt() return NULL something
Daniel Veillard88a172f2000-08-04 18:23:10 +0000699 strange happened so we don't want to do anything. Do
700 we want to print an error message here?
701 <sven@zen.org> */
Daniel Veillard7ebb1ee2000-08-04 18:24:45 +0000702 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000703 } else {
704 memcpy(&silent, ctxt->sax, sizeof(silent));
705 old = ctxt->sax;
706 silent.error = xmlHTMLError;
707 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000708 silent.warning = xmlHTMLWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000709 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000710 silent.warning = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000711 silent.fatalError = xmlHTMLError;
712 ctxt->sax = &silent;
713 ctxt->vctxt.error = xmlHTMLValidityError;
714 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000715 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000716 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000717 ctxt->vctxt.warning = NULL;
718
Daniel Veillard88a172f2000-08-04 18:23:10 +0000719 xmlParseDocument(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000720
Daniel Veillard88a172f2000-08-04 18:23:10 +0000721 ret = ctxt->wellFormed;
722 doc = ctxt->myDoc;
723 ctxt->sax = old;
724 xmlFreeParserCtxt(ctxt);
725 if (!ret) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000726 xmlFreeDoc(doc);
727 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000728 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000729 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000730#ifdef HAVE_SYS_MMAN_H
731 } else if (memory) {
732 int fd;
733 struct stat info;
734 const char *base;
735 if (stat(filename, &info) < 0)
736 return;
737 if ((fd = open(filename, O_RDONLY)) < 0)
738 return;
739 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000740 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000741 return;
742
743 doc = xmlParseMemory((char *) base, info.st_size);
744 munmap((char *) base, info.st_size);
745#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000746 } else
747 doc = xmlParseFile(filename);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000748 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000749
Daniel Veillard88a172f2000-08-04 18:23:10 +0000750 /*
751 * If we don't have a document we might as well give up. Do we
752 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000753 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000754 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000755 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000756 }
757
Daniel Veillard48b2f892001-02-25 16:11:03 +0000758 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000759 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000760 }
761
Daniel Veillard29e43992001-12-13 22:21:58 +0000762 /*
763 * Remove DOCTYPE nodes
764 */
765 if (dropdtd) {
766 xmlDtdPtr dtd;
767
768 dtd = xmlGetIntSubset(doc);
769 if (dtd != NULL) {
770 xmlUnlinkNode((xmlNodePtr)dtd);
771 xmlFreeDtd(dtd);
772 }
773 }
774
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000775#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000776 if (xinclude) {
777 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000778 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000779 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000780 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000781 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000782 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000783 }
784 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000785#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000786
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000787#ifdef LIBXML_DEBUG_ENABLED
788 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000789 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000790 */
791 if (shell)
792 xmlShell(doc, filename, xmlShellReadline, stdout);
793#endif
794
795 /*
796 * test intermediate copy if needed.
797 */
798 if (copy) {
799 tmp = doc;
800 doc = xmlCopyDoc(doc, 1);
801 xmlFreeDoc(tmp);
802 }
803
804 if ((insert) && (!html)) {
805 const xmlChar* list[256];
806 int nb, i;
807 xmlNodePtr node;
808
809 if (doc->children != NULL) {
810 node = doc->children;
811 while ((node != NULL) && (node->last == NULL)) node = node->next;
812 if (node != NULL) {
813 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
814 if (nb < 0) {
815 printf("could not get valid list of elements\n");
816 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000817 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000818 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000819 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000820 nb);
821 for (i = 0;i < nb;i++) {
822 printf("%s\n", list[i]);
823 }
824 }
825 }
826 }
827 }else if (noout == 0) {
828 /*
829 * print it.
830 */
831#ifdef LIBXML_DEBUG_ENABLED
832 if (!debug) {
833#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +0000834 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000835 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000836 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +0000837#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000838 if (memory) {
839 xmlChar *result;
840 int len;
841
842 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +0000843 if ( format ) {
844 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
845 } else {
846 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
847 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000848 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +0000849 if (format)
850 xmlDocDumpFormatMemory(doc, &result, &len, 1);
851 else
852 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000853 }
854 if (result == NULL) {
855 fprintf(stderr, "Failed to save\n");
856 } else {
857 write(1, result, len);
858 xmlFree(result);
859 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +0000860 } else
861#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000862 if (compress) {
863 xmlSaveFile(output ? output : "-", doc);
864 }
Daniel Veillardd536f702001-11-08 17:32:47 +0000865 else if (encoding != NULL) {
866 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000867 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
868 }
Daniel Veillardd536f702001-11-08 17:32:47 +0000869 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000870 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +0000871 }
872 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000873 else if (format) {
874 xmlSaveFormatFile(output ? output : "-", doc, 1);
875 }
876 else {
877 FILE *out;
878 if (output == NULL)
879 out = stdout;
880 else {
881 out = fopen(output,"wb");
882 }
883 xmlDocDump(out, doc);
884
885 if (output)
886 fclose(out);
887 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000888 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000889 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000890 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000891#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000892 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000893 FILE *out;
894 if (output == NULL)
895 out = stdout;
896 else {
897 out = fopen(output,"wb");
898 }
899 xmlDebugDumpDocument(out, doc);
900
901 if (output)
902 fclose(out);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000903 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000904#endif
905 }
906
907 /*
908 * A posteriori validation test
909 */
Daniel Veillardcd429612000-10-11 15:57:05 +0000910 if (dtdvalid != NULL) {
911 xmlDtdPtr dtd;
912
Daniel Veillard48b2f892001-02-25 16:11:03 +0000913 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000914 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000915 }
Daniel Veillardcd429612000-10-11 15:57:05 +0000916 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000917 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000918 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000919 }
Daniel Veillardcd429612000-10-11 15:57:05 +0000920 if (dtd == NULL) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000921 xmlGenericError(xmlGenericErrorContext,
922 "Could not parse DTD %s\n", dtdvalid);
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000923 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +0000924 } else {
925 xmlValidCtxt cvp;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000926 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000927 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000928 }
Daniel Veillard01db67c2001-12-18 07:09:59 +0000929 cvp.userData = (void *) stderr;
930 cvp.error = (xmlValidityErrorFunc) fprintf;
931 cvp.warning = (xmlValidityWarningFunc) fprintf;
Daniel Veillardcd429612000-10-11 15:57:05 +0000932 if (!xmlValidateDtd(&cvp, doc, dtd)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000933 xmlGenericError(xmlGenericErrorContext,
934 "Document %s does not validate against %s\n",
Daniel Veillardcd429612000-10-11 15:57:05 +0000935 filename, dtdvalid);
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000936 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +0000937 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000938 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000939 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000940 }
Daniel Veillardcd429612000-10-11 15:57:05 +0000941 xmlFreeDtd(dtd);
942 }
943 } else if (postvalid) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000944 xmlValidCtxt cvp;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000945 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000946 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000947 }
Daniel Veillardb7664f42001-08-19 13:00:43 +0000948 cvp.userData = (void *) stderr;
949 cvp.error = (xmlValidityErrorFunc) fprintf;
950 cvp.warning = (xmlValidityWarningFunc) fprintf;
Daniel Veillardcd429612000-10-11 15:57:05 +0000951 if (!xmlValidateDocument(&cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000952 xmlGenericError(xmlGenericErrorContext,
953 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000954 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +0000955 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000956 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000957 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000958 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000959 }
960
961#ifdef LIBXML_DEBUG_ENABLED
962 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +0000963 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000964#endif
965
966 /*
967 * free it.
968 */
Daniel Veillard48b2f892001-02-25 16:11:03 +0000969 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000970 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000971 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000972 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000973 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000974 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000975 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000976}
977
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000978/************************************************************************
979 * *
980 * Usage and Main *
981 * *
982 ************************************************************************/
983
Daniel Veillard0f04f8e2002-09-17 23:04:40 +0000984static void showVersion(const char *name) {
985 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
986 fprintf(stderr, " compiled with: ");
987#ifdef LIBXML_FTP_ENABLED
988 fprintf(stderr, "FTP ");
989#endif
990#ifdef LIBXML_HTTP_ENABLED
991 fprintf(stderr, "HTTP ");
992#endif
993#ifdef LIBXML_HTML_ENABLED
994 fprintf(stderr, "HTML ");
995#endif
996#ifdef LIBXML_C14N_ENABLED
997 fprintf(stderr, "C14N ");
998#endif
999#ifdef LIBXML_CATALOG_ENABLED
1000 fprintf(stderr, "Catalog ");
1001#endif
1002#ifdef LIBXML_DOCB_ENABLED
1003 fprintf(stderr, "DocBook ");
1004#endif
1005#ifdef LIBXML_XPATH_ENABLED
1006 fprintf(stderr, "XPath ");
1007#endif
1008#ifdef LIBXML_XPTR_ENABLED
1009 fprintf(stderr, "XPointer ");
1010#endif
1011#ifdef LIBXML_XINCLUDE_ENABLED
1012 fprintf(stderr, "XInclude ");
1013#endif
1014#ifdef LIBXML_ICONV_ENABLED
1015 fprintf(stderr, "Iconv ");
1016#endif
1017#ifdef DEBUG_MEMORY_LOCATION
1018 fprintf(stderr, "MemDebug ");
1019#endif
1020#ifdef LIBXML_UNICODE_ENABLED
1021 fprintf(stderr, "Unicode ");
1022#endif
1023#ifdef LIBXML_REGEXP_ENABLED
1024 fprintf(stderr, "Regexps ");
1025#endif
1026#ifdef LIBXML_AUTOMATA_ENABLED
1027 fprintf(stderr, "Automata ");
1028#endif
1029#ifdef LIBXML_SCHEMAS_ENABLED
1030 fprintf(stderr, "Schemas ");
1031#endif
1032 fprintf(stderr, "\n");
1033}
1034
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001035static void usage(const char *name) {
1036 printf("Usage : %s [options] XMLfiles ...\n", name);
1037 printf("\tParse the XML files and output the result of the parsing\n");
1038 printf("\t--version : display the version of the XML library used\n");
1039#ifdef LIBXML_DEBUG_ENABLED
1040 printf("\t--debug : dump a debug tree of the in-memory document\n");
1041 printf("\t--shell : run a navigating shell\n");
1042 printf("\t--debugent : debug the entities defined in the document\n");
1043#endif
1044 printf("\t--copy : used to test the internal copy implementation\n");
1045 printf("\t--recover : output what was parsable on broken XML documents\n");
1046 printf("\t--noent : substitute entity references by their value\n");
1047 printf("\t--noout : don't output the result tree\n");
1048 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001049 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001050 printf("\t--valid : validate the document in addition to std well-formed check\n");
1051 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1052 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
1053 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001054 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001055 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1056 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001057#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001058 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001059#endif
Daniel Veillard89cad532001-10-22 09:46:13 +00001060#ifdef LIBXML_DOCB_ENABLED
1061 printf("\t--sgml : use the DocBook SGML parser\n");
1062#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001063#ifdef LIBXML_HTML_ENABLED
1064 printf("\t--html : use the HTML parser\n");
1065#endif
1066 printf("\t--push : use the push mode of the parser\n");
1067#ifdef HAVE_SYS_MMAN_H
1068 printf("\t--memory : parse from memory\n");
1069#endif
1070 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1071 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillard90493a92001-08-14 14:12:47 +00001072 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001073 printf("\t--testIO : test user I/O support\n");
1074 printf("\t--encode encoding : output in the given encoding\n");
1075#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001076 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1077 printf("\t otherwise XML Catalogs starting from \n");
1078 printf("\t file:///etc/xml/catalog are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001079 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001080#endif
1081 printf("\t--auto : generate a small doc on the fly\n");
1082#ifdef LIBXML_XINCLUDE_ENABLED
1083 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001084#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001085 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001086 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard29e43992001-12-13 22:21:58 +00001087 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001088 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1089 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001090}
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001091int
1092main(int argc, char **argv) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001093 int i, count;
1094 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001095 int version = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001096
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001097 if (argc <= 1) {
1098 usage(argv[0]);
1099 return(1);
1100 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001101 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001102 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001103 if (!strcmp(argv[i], "-"))
1104 break;
1105
1106 if (argv[i][0] != '-')
1107 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001108#ifdef LIBXML_DEBUG_ENABLED
1109 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1110 debug++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001111 else if ((!strcmp(argv[i], "-shell")) ||
1112 (!strcmp(argv[i], "--shell"))) {
1113 shell++;
1114 noout = 1;
1115 } else
1116#endif
1117 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1118 copy++;
1119 else if ((!strcmp(argv[i], "-recover")) ||
1120 (!strcmp(argv[i], "--recover")))
1121 recovery++;
1122 else if ((!strcmp(argv[i], "-noent")) ||
1123 (!strcmp(argv[i], "--noent")))
1124 noent++;
Daniel Veillard4ec885a2001-06-17 10:31:07 +00001125 else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001126 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001127 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001128 version = 1;
1129 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001130 (!strcmp(argv[i], "--noout")))
1131 noout++;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001132 else if ((!strcmp(argv[i], "-o")) ||
1133 (!strcmp(argv[i], "-output")) ||
1134 (!strcmp(argv[i], "--output"))) {
1135 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001136 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001137 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001138 else if ((!strcmp(argv[i], "-htmlout")) ||
1139 (!strcmp(argv[i], "--htmlout")))
1140 htmlout++;
Daniel Veillard89cad532001-10-22 09:46:13 +00001141#ifdef LIBXML_DOCB_ENABLED
1142 else if ((!strcmp(argv[i], "-sgml")) ||
1143 (!strcmp(argv[i], "--sgml"))) {
1144 sgml++;
1145 }
1146#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001147#ifdef LIBXML_HTML_ENABLED
1148 else if ((!strcmp(argv[i], "-html")) ||
1149 (!strcmp(argv[i], "--html"))) {
1150 html++;
1151 }
1152#endif /* LIBXML_HTML_ENABLED */
1153 else if ((!strcmp(argv[i], "-nowrap")) ||
1154 (!strcmp(argv[i], "--nowrap")))
1155 nowrap++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001156 else if ((!strcmp(argv[i], "-loaddtd")) ||
1157 (!strcmp(argv[i], "--loaddtd")))
1158 loaddtd++;
Daniel Veillard48da9102001-08-07 01:10:10 +00001159 else if ((!strcmp(argv[i], "-dtdattr")) ||
1160 (!strcmp(argv[i], "--dtdattr"))) {
1161 loaddtd++;
1162 dtdattrs++;
1163 } else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001164 (!strcmp(argv[i], "--valid")))
1165 valid++;
1166 else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001167 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001168 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001169 loaddtd++;
1170 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001171 (!strcmp(argv[i], "--dtdvalid"))) {
1172 i++;
1173 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001174 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001175 }
Daniel Veillard29e43992001-12-13 22:21:58 +00001176 else if ((!strcmp(argv[i], "-dropdtd")) ||
1177 (!strcmp(argv[i], "--dropdtd")))
1178 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001179 else if ((!strcmp(argv[i], "-insert")) ||
1180 (!strcmp(argv[i], "--insert")))
1181 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001182 else if ((!strcmp(argv[i], "-timing")) ||
1183 (!strcmp(argv[i], "--timing")))
1184 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001185 else if ((!strcmp(argv[i], "-auto")) ||
1186 (!strcmp(argv[i], "--auto")))
1187 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001188 else if ((!strcmp(argv[i], "-repeat")) ||
1189 (!strcmp(argv[i], "--repeat")))
1190 repeat++;
1191 else if ((!strcmp(argv[i], "-push")) ||
1192 (!strcmp(argv[i], "--push")))
1193 push++;
Daniel Veillard46e370e2000-07-21 20:32:03 +00001194#ifdef HAVE_SYS_MMAN_H
1195 else if ((!strcmp(argv[i], "-memory")) ||
1196 (!strcmp(argv[i], "--memory")))
1197 memory++;
1198#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001199 else if ((!strcmp(argv[i], "-testIO")) ||
1200 (!strcmp(argv[i], "--testIO")))
1201 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001202#ifdef LIBXML_XINCLUDE_ENABLED
1203 else if ((!strcmp(argv[i], "-xinclude")) ||
1204 (!strcmp(argv[i], "--xinclude")))
1205 xinclude++;
1206#endif
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001207#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001208 else if ((!strcmp(argv[i], "-compress")) ||
1209 (!strcmp(argv[i], "--compress"))) {
1210 compress++;
1211 xmlSetCompressMode(9);
1212 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001213#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001214 else if ((!strcmp(argv[i], "-nowarning")) ||
1215 (!strcmp(argv[i], "--nowarning"))) {
1216 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001217 xmlPedanticParserDefault(0);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001218 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001219 else if ((!strcmp(argv[i], "-pedantic")) ||
1220 (!strcmp(argv[i], "--pedantic"))) {
1221 xmlGetWarningsDefaultValue = 1;
1222 xmlPedanticParserDefault(1);
1223 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001224#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001225 else if ((!strcmp(argv[i], "-debugent")) ||
1226 (!strcmp(argv[i], "--debugent"))) {
1227 debugent++;
1228 xmlParserDebugEntities = 1;
1229 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001230#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001231#ifdef LIBXML_CATALOG_ENABLED
1232 else if ((!strcmp(argv[i], "-catalogs")) ||
1233 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001234 catalogs++;
1235 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1236 (!strcmp(argv[i], "--nocatalogs"))) {
1237 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001238 }
1239#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001240 else if ((!strcmp(argv[i], "-encode")) ||
1241 (!strcmp(argv[i], "--encode"))) {
1242 i++;
1243 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001244 /*
1245 * OK it's for testing purposes
1246 */
1247 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001248 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001249 else if ((!strcmp(argv[i], "-noblanks")) ||
1250 (!strcmp(argv[i], "--noblanks"))) {
1251 noblanks++;
1252 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001253 }
1254 else if ((!strcmp(argv[i], "-format")) ||
1255 (!strcmp(argv[i], "--format"))) {
1256 noblanks++;
1257 format++;
1258 xmlKeepBlanksDefault(0);
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001259 } else {
1260 fprintf(stderr, "Unknown option %s\n", argv[i]);
1261 usage(argv[0]);
1262 return(1);
1263 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001264 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001265
1266#ifdef LIBXML_CATALOG_ENABLED
1267 if (nocatalogs == 0) {
1268 if (catalogs) {
1269 const char *catal;
1270
1271 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001272 if (catal != NULL) {
1273 xmlLoadCatalogs(catal);
1274 } else {
1275 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1276 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001277 }
1278 }
1279#endif
Daniel Veillardd9bad132001-07-23 19:39:43 +00001280 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001281 if (loaddtd != 0)
1282 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1283 if (dtdattrs)
1284 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001285 if (noent != 0) xmlSubstituteEntitiesDefault(1);
1286 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
1287 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001288 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001289 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001290 xmlGenericError(xmlGenericErrorContext,
1291 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1292 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001293 "<html><head><title>%s output</title></head>\n",
1294 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001295 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001296 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1297 argv[0]);
1298 }
1299 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001300 if ((!strcmp(argv[i], "-encode")) ||
1301 (!strcmp(argv[i], "--encode"))) {
1302 i++;
1303 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001304 } else if ((!strcmp(argv[i], "-o")) ||
1305 (!strcmp(argv[i], "-output")) ||
1306 (!strcmp(argv[i], "--output"))) {
1307 i++;
1308 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001309 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001310 if ((!strcmp(argv[i], "-dtdvalid")) ||
1311 (!strcmp(argv[i], "--dtdvalid"))) {
1312 i++;
1313 continue;
1314 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001315 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001316 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001317 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001318 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001319 if (repeat) {
1320 for (count = 0;count < 100 * repeat;count++)
1321 parseAndPrintFile(argv[i]);
1322 } else
1323 parseAndPrintFile(argv[i]);
1324 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001325 if ((timing) && (repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001326 endTimer("100 iterations");
Daniel Veillarda7866932001-12-04 13:14:44 +00001327 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001328 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001329 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001330 if (generate)
1331 parseAndPrintFile(NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001332 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001333 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001334 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001335 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001336 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001337 }
1338 xmlCleanupParser();
1339 xmlMemoryDump();
1340
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001341 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001342}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001343