blob: e333a39008f227c59b8e75ce63b3a85e9b821e06 [file] [log] [blame]
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001/*
2 * xmllint.c : a small tester program for XML input.
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Daniel Veillardce8b83b2000-04-05 18:38:42 +00007 */
8
Bjorn Reese70a9da52001-04-21 16:57:29 +00009#include "libxml.h"
Daniel Veillardce8b83b2000-04-05 18:38:42 +000010
Daniel Veillardce8b83b2000-04-05 18:38:42 +000011#include <string.h>
Daniel Veillardce8b83b2000-04-05 18:38:42 +000012#include <stdarg.h>
Daniel Veillard8a1b1852003-01-05 22:37:17 +000013#include <assert.h>
14
Daniel Veillard3c5ed912002-01-08 10:36:16 +000015#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillard07cb8222003-09-10 10:51:05 +000016#if defined (_MSC_VER) || defined(__BORLANDC__)
Daniel Veillard2d90de42001-04-16 17:46:18 +000017#include <winsock2.h>
18#pragma comment(lib, "ws2_32.lib")
19#define gettimeofday(p1,p2)
20#endif /* _MSC_VER */
Igor Zlatkovic19b87642003-08-28 12:32:04 +000021#endif /* _WIN32 */
22
Daniel Veillarded472f32001-12-13 08:48:14 +000023#ifdef HAVE_SYS_TIME_H
Daniel Veillard48b2f892001-02-25 16:11:03 +000024#include <sys/time.h>
Daniel Veillarded472f32001-12-13 08:48:14 +000025#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +000026#ifdef HAVE_TIME_H
27#include <time.h>
28#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +000029
Daniel Veillard1638a472003-08-14 01:23:25 +000030#ifdef __MINGW32__
31#define _WINSOCKAPI_
32#include <wsockcompat.h>
33#include <winsock2.h>
34#undef SOCKLEN_T
35#define SOCKLEN_T unsigned int
36#endif
37
Daniel Veillard90bc3712002-03-07 15:12:58 +000038#ifdef HAVE_SYS_TIMEB_H
39#include <sys/timeb.h>
40#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000041
42#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_SYS_STAT_H
46#include <sys/stat.h>
47#endif
48#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
50#endif
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000054#ifdef HAVE_SYS_MMAN_H
55#include <sys/mman.h>
Daniel Veillard87b95392000-08-12 21:12:04 +000056/* seems needed for Solaris */
57#ifndef MAP_FAILED
58#define MAP_FAILED ((void *) -1)
59#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000060#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000061#ifdef HAVE_STDLIB_H
62#include <stdlib.h>
63#endif
64#ifdef HAVE_LIBREADLINE
65#include <readline/readline.h>
66#ifdef HAVE_LIBHISTORY
67#include <readline/history.h>
68#endif
69#endif
70
71#include <libxml/xmlmemory.h>
72#include <libxml/parser.h>
73#include <libxml/parserInternals.h>
74#include <libxml/HTMLparser.h>
75#include <libxml/HTMLtree.h>
76#include <libxml/tree.h>
77#include <libxml/xpath.h>
78#include <libxml/debugXML.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000079#include <libxml/xmlerror.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000080#ifdef LIBXML_XINCLUDE_ENABLED
81#include <libxml/xinclude.h>
82#endif
Daniel Veillard81418e32001-05-22 15:08:55 +000083#ifdef LIBXML_CATALOG_ENABLED
84#include <libxml/catalog.h>
85#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000086#include <libxml/globals.h>
Daniel Veillard7704fb12003-01-03 16:19:51 +000087#include <libxml/xmlreader.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000088#ifdef LIBXML_SCHEMAS_ENABLED
89#include <libxml/relaxng.h>
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000090#include <libxml/xmlschemas.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000091#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +000092#ifdef LIBXML_PATTERN_ENABLED
93#include <libxml/pattern.h>
94#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000095
Daniel Veillard3be27512003-01-26 19:49:04 +000096#ifndef XML_XML_DEFAULT_CATALOG
97#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
98#endif
99
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000100#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000101static int shell = 0;
102static int debugent = 0;
103#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000104static int debug = 0;
Daniel Veillard87076042004-05-03 22:54:49 +0000105static int maxmem = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000106#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000107static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000108#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000109static int recovery = 0;
110static int noent = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000111static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000112static int noout = 0;
113static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000114#ifdef LIBXML_OUTPUT_ENABLED
115static int format = 0;
116static const char *output = NULL;
117static int compress = 0;
118#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000119#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000120static int valid = 0;
121static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000122static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000123static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000124#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000125#ifdef LIBXML_SCHEMAS_ENABLED
126static char * relaxng = NULL;
127static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000128static char * schema = NULL;
129static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000130#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000131static int repeat = 0;
132static int insert = 0;
Daniel Veillard656ce942004-04-30 23:11:45 +0000133#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000134static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000135static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000136#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000137static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000138#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000139static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000140#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000141#ifdef HAVE_SYS_MMAN_H
142static int memory = 0;
143#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000144static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000145static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000146#ifdef LIBXML_XINCLUDE_ENABLED
147static int xinclude = 0;
148#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000149static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000150static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000151static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000152static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000153static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000154static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000155#ifdef LIBXML_CATALOG_ENABLED
156static int catalogs = 0;
157static int nocatalogs = 0;
158#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000159#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000160static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000161static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000162#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000163static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000164static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000165#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000166static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000167#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000168#ifdef LIBXML_PATTERN_ENABLED
169static const char *pattern = NULL;
170static xmlPatternPtr patternc = NULL;
171#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000172static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000173
Daniel Veillard87076042004-05-03 22:54:49 +0000174/************************************************************************
175 * *
176 * Memory allocation consumption debugging *
177 * *
178 ************************************************************************/
179
Daniel Veillard3af3b592004-05-05 19:22:30 +0000180static void
181OOM(void)
182{
Daniel Veillard87076042004-05-03 22:54:49 +0000183 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
184 progresult = 9;
185}
186
Daniel Veillard3af3b592004-05-05 19:22:30 +0000187static void
188myFreeFunc(void *mem)
189{
Daniel Veillard87076042004-05-03 22:54:49 +0000190 xmlMemFree(mem);
191}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000192static void *
193myMallocFunc(size_t size)
194{
Daniel Veillard87076042004-05-03 22:54:49 +0000195 void *ret;
196
197 ret = xmlMemMalloc(size);
198 if (ret != NULL) {
199 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000200 OOM();
201 xmlMemFree(ret);
202 return (NULL);
203 }
Daniel Veillard87076042004-05-03 22:54:49 +0000204 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000205 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000206}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000207static void *
208myReallocFunc(void *mem, size_t size)
209{
Daniel Veillard87076042004-05-03 22:54:49 +0000210 void *ret;
211
212 ret = xmlMemRealloc(mem, size);
213 if (ret != NULL) {
214 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000215 OOM();
216 xmlMemFree(ret);
217 return (NULL);
218 }
Daniel Veillard87076042004-05-03 22:54:49 +0000219 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000220 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000221}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000222static char *
223myStrdupFunc(const char *str)
224{
Daniel Veillard87076042004-05-03 22:54:49 +0000225 char *ret;
226
227 ret = xmlMemoryStrdup(str);
228 if (ret != NULL) {
229 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000230 OOM();
231 xmlFree(ret);
232 return (NULL);
233 }
Daniel Veillard87076042004-05-03 22:54:49 +0000234 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000235 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000236}
Daniel Veillard87076042004-05-03 22:54:49 +0000237/************************************************************************
238 * *
239 * Internal timing routines to remove the necessity to have *
240 * unix-specific function calls. *
241 * *
242 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000243
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000244#ifndef HAVE_GETTIMEOFDAY
245#ifdef HAVE_SYS_TIMEB_H
246#ifdef HAVE_SYS_TIME_H
247#ifdef HAVE_FTIME
248
Daniel Veillard01c13b52002-12-10 15:19:08 +0000249static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000250my_gettimeofday(struct timeval *tvp, void *tzp)
251{
252 struct timeb timebuffer;
253
254 ftime(&timebuffer);
255 if (tvp) {
256 tvp->tv_sec = timebuffer.time;
257 tvp->tv_usec = timebuffer.millitm * 1000L;
258 }
259 return (0);
260}
261#define HAVE_GETTIMEOFDAY 1
262#define gettimeofday my_gettimeofday
263
264#endif /* HAVE_FTIME */
265#endif /* HAVE_SYS_TIME_H */
266#endif /* HAVE_SYS_TIMEB_H */
267#endif /* !HAVE_GETTIMEOFDAY */
268
Daniel Veillard01db67c2001-12-18 07:09:59 +0000269#if defined(HAVE_GETTIMEOFDAY)
270static struct timeval begin, end;
271
272/*
273 * startTimer: call where you want to start timing
274 */
275static void
276startTimer(void)
277{
278 gettimeofday(&begin, NULL);
279}
280
281/*
282 * endTimer: call where you want to stop timing and to print out a
283 * message about the timing performed; format is a printf
284 * type argument
285 */
286static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000287endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000288{
289 long msec;
290 va_list ap;
291
292 gettimeofday(&end, NULL);
293 msec = end.tv_sec - begin.tv_sec;
294 msec *= 1000;
295 msec += (end.tv_usec - begin.tv_usec) / 1000;
296
297#ifndef HAVE_STDARG_H
298#error "endTimer required stdarg functions"
299#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000300 va_start(ap, fmt);
301 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000302 va_end(ap);
303
304 fprintf(stderr, " took %ld ms\n", msec);
305}
306#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000307/*
308 * No gettimeofday function, so we have to make do with calling clock.
309 * This is obviously less accurate, but there's little we can do about
310 * that.
311 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000312#ifndef CLOCKS_PER_SEC
313#define CLOCKS_PER_SEC 100
314#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000315
316static clock_t begin, end;
317static void
318startTimer(void)
319{
320 begin = clock();
321}
322static void
323endTimer(const char *fmt, ...)
324{
325 long msec;
326 va_list ap;
327
328 end = clock();
329 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
330
331#ifndef HAVE_STDARG_H
332#error "endTimer required stdarg functions"
333#endif
334 va_start(ap, fmt);
335 vfprintf(stderr, fmt, ap);
336 va_end(ap);
337 fprintf(stderr, " took %ld ms\n", msec);
338}
339#else
340
341/*
342 * We don't have a gettimeofday or time.h, so we just don't do timing
343 */
344static void
345startTimer(void)
346{
347 /*
348 * Do nothing
349 */
350}
351static void
352endTimer(char *format, ...)
353{
354 /*
355 * We cannot do anything because we don't have a timing function
356 */
357#ifdef HAVE_STDARG_H
358 va_start(ap, format);
359 vfprintf(stderr, format, ap);
360 va_end(ap);
361 fprintf(stderr, " was not timed\n", msec);
362#else
363 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
364 * this ?!
365 */
366#endif
367}
368#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000369/************************************************************************
370 * *
371 * HTML ouput *
372 * *
373 ************************************************************************/
374char buffer[50000];
375
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000376static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000377xmlHTMLEncodeSend(void) {
378 char *result;
379
380 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
381 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000382 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000383 xmlFree(result);
384 }
385 buffer[0] = 0;
386}
387
388/**
389 * xmlHTMLPrintFileInfo:
390 * @input: an xmlParserInputPtr input
391 *
392 * Displays the associated file and line informations for the current input
393 */
394
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000395static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000396xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000397 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000398 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000399
400 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000401 if (input != NULL) {
402 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000403 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000404 input->line);
405 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000406 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000407 }
408 }
409 xmlHTMLEncodeSend();
410}
411
412/**
413 * xmlHTMLPrintFileContext:
414 * @input: an xmlParserInputPtr input
415 *
416 * Displays current context within the input content for error tracking
417 */
418
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000419static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000420xmlHTMLPrintFileContext(xmlParserInputPtr input) {
421 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000422 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000423 int n;
424
425 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000426 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000427 cur = input->cur;
428 base = input->base;
429 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
430 cur--;
431 }
432 n = 0;
433 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
434 cur--;
435 if ((*cur == '\n') || (*cur == '\r')) cur++;
436 base = cur;
437 n = 0;
438 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000439 len = strlen(buffer);
440 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
441 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000442 n++;
443 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000444 len = strlen(buffer);
445 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000446 cur = input->cur;
447 while ((*cur == '\n') || (*cur == '\r'))
448 cur--;
449 n = 0;
450 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000451 len = strlen(buffer);
452 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000453 base++;
454 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000455 len = strlen(buffer);
456 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000457 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000458 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000459}
460
461/**
462 * xmlHTMLError:
463 * @ctx: an XML parser context
464 * @msg: the message to display/transmit
465 * @...: extra parameters for the message display
466 *
467 * Display and format an error messages, gives file, line, position and
468 * extra parameters.
469 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000470static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000471xmlHTMLError(void *ctx, const char *msg, ...)
472{
473 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
474 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000475 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000476 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000477
478 buffer[0] = 0;
479 input = ctxt->input;
480 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000481 input = ctxt->inputTab[ctxt->inputNr - 2];
482 }
483
484 xmlHTMLPrintFileInfo(input);
485
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000486 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000487 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000488 len = strlen(buffer);
489 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000490 va_end(args);
491 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000492 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000493
494 xmlHTMLPrintFileContext(input);
495 xmlHTMLEncodeSend();
496}
497
498/**
499 * xmlHTMLWarning:
500 * @ctx: an XML parser context
501 * @msg: the message to display/transmit
502 * @...: extra parameters for the message display
503 *
504 * Display and format a warning messages, gives file, line, position and
505 * extra parameters.
506 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000507static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000508xmlHTMLWarning(void *ctx, const char *msg, ...)
509{
510 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
511 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000512 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000513 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000514
515 buffer[0] = 0;
516 input = ctxt->input;
517 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000518 input = ctxt->inputTab[ctxt->inputNr - 2];
519 }
520
521
522 xmlHTMLPrintFileInfo(input);
523
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000524 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000525 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000526 len = strlen(buffer);
527 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000528 va_end(args);
529 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000530 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000531
532 xmlHTMLPrintFileContext(input);
533 xmlHTMLEncodeSend();
534}
535
536/**
537 * xmlHTMLValidityError:
538 * @ctx: an XML parser context
539 * @msg: the message to display/transmit
540 * @...: extra parameters for the message display
541 *
542 * Display and format an validity error messages, gives file,
543 * line, position and extra parameters.
544 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000545static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000546xmlHTMLValidityError(void *ctx, const char *msg, ...)
547{
548 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
549 xmlParserInputPtr input;
550 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000551 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000552
553 buffer[0] = 0;
554 input = ctxt->input;
555 if ((input->filename == NULL) && (ctxt->inputNr > 1))
556 input = ctxt->inputTab[ctxt->inputNr - 2];
557
558 xmlHTMLPrintFileInfo(input);
559
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000560 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000561 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000562 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000563 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000564 va_end(args);
565 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000566 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000567
568 xmlHTMLPrintFileContext(input);
569 xmlHTMLEncodeSend();
570}
571
572/**
573 * xmlHTMLValidityWarning:
574 * @ctx: an XML parser context
575 * @msg: the message to display/transmit
576 * @...: extra parameters for the message display
577 *
578 * Display and format a validity warning messages, gives file, line,
579 * position and extra parameters.
580 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000581static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000582xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
583{
584 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
585 xmlParserInputPtr input;
586 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000587 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000588
589 buffer[0] = 0;
590 input = ctxt->input;
591 if ((input->filename == NULL) && (ctxt->inputNr > 1))
592 input = ctxt->inputTab[ctxt->inputNr - 2];
593
594 xmlHTMLPrintFileInfo(input);
595
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000596 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000597 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000598 len = strlen(buffer);
599 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000600 va_end(args);
601 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000602 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000603
604 xmlHTMLPrintFileContext(input);
605 xmlHTMLEncodeSend();
606}
607
608/************************************************************************
609 * *
610 * Shell Interface *
611 * *
612 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000613#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000614/**
615 * xmlShellReadline:
616 * @prompt: the prompt value
617 *
618 * Read a string
619 *
620 * Returns a pointer to it or NULL on EOF the caller is expected to
621 * free the returned string.
622 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000623static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000624xmlShellReadline(char *prompt) {
625#ifdef HAVE_LIBREADLINE
626 char *line_read;
627
628 /* Get a line from the user. */
629 line_read = readline (prompt);
630
631 /* If the line has any text in it, save it on the history. */
632 if (line_read && *line_read)
633 add_history (line_read);
634
635 return (line_read);
636#else
637 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000638 char *ret;
639 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000640
641 if (prompt != NULL)
642 fprintf(stdout, "%s", prompt);
643 if (!fgets(line_read, 500, stdin))
644 return(NULL);
645 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000646 len = strlen(line_read);
647 ret = (char *) malloc(len + 1);
648 if (ret != NULL) {
649 memcpy (ret, line_read, len + 1);
650 }
651 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000652#endif
653}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000654#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000655
656/************************************************************************
657 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000658 * I/O Interfaces *
659 * *
660 ************************************************************************/
661
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000662static int myRead(FILE *f, char * buf, int len) {
663 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000664}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000665static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000666 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000667 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000668 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000669}
670
Daniel Veillard81273902003-09-30 00:43:48 +0000671#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000672/************************************************************************
673 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000674 * Stream Test processing *
675 * *
676 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000677static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000678 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000679
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000680 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000681 if (name == NULL)
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000682 name = BAD_CAST "--";
Daniel Veillard99737f52003-03-22 14:55:50 +0000683
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000684 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000685
Daniel Veillard99737f52003-03-22 14:55:50 +0000686 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000687 xmlTextReaderDepth(reader),
688 xmlTextReaderNodeType(reader),
689 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000690 xmlTextReaderIsEmptyElement(reader),
691 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000692 if (value == NULL)
693 printf("\n");
694 else {
695 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000696 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000697#ifdef LIBXML_PATTERN_ENABLED
698 if (patternc) {
699 if (xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader)) == 1) {
700 printf("Node matches pattern %s\n", pattern);
701 }
702 }
703#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000704}
705
706static void streamFile(char *filename) {
707 xmlTextReaderPtr reader;
708 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000709#ifdef HAVE_SYS_MMAN_H
710 int fd = -1;
711 struct stat info;
712 const char *base = NULL;
713 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000714
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000715 if (memory) {
716 if (stat(filename, &info) < 0)
717 return;
718 if ((fd = open(filename, O_RDONLY)) < 0)
719 return;
720 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
721 if (base == (void *) MAP_FAILED)
722 return;
723
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000724 reader = xmlReaderForMemory(base, info.st_size, filename,
725 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000726 } else
727#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000728 reader = xmlReaderForFile(filename, NULL, options);
729
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000730
Daniel Veillard7704fb12003-01-03 16:19:51 +0000731 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000732#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000733 if (valid)
734 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000735 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000736#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000737 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000738#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000739 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000740 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000741 startTimer();
742 }
743 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
744 if (ret < 0) {
745 xmlGenericError(xmlGenericErrorContext,
746 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000747 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000748 relaxng = NULL;
749 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000750 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000751 endTimer("Compiling the schemas");
752 }
753 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000754#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000755
756 /*
757 * Process all nodes in sequence
758 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000759 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000760 startTimer();
761 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000762 ret = xmlTextReaderRead(reader);
763 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000764 if ((debug)
765#ifdef LIBXML_PATTERN_ENABLED
766 || (patternc)
767#endif
768 )
Daniel Veillard7704fb12003-01-03 16:19:51 +0000769 processNode(reader);
770 ret = xmlTextReaderRead(reader);
771 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000772 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000773#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000774 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +0000775 endTimer("Parsing and validating");
776 else
Daniel Veillardf54cd532004-02-25 11:52:31 +0000777#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000778#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000779 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000780 endTimer("Parsing and validating");
781 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000782#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +0000783 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +0000784 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000785
Daniel Veillard4432df22003-09-28 18:58:27 +0000786#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000787 if (valid) {
788 if (xmlTextReaderIsValid(reader) != 1) {
789 xmlGenericError(xmlGenericErrorContext,
790 "Document %s does not validate\n", filename);
791 progresult = 3;
792 }
793 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000794#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000795#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000796 if (relaxng != NULL) {
797 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000798 fprintf(stderr, "%s fails to validate\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +0000799 progresult = 3;
800 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000801 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +0000802 }
803 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000804#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000805 /*
806 * Done, cleanup and status
807 */
808 xmlFreeTextReader(reader);
809 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000810 fprintf(stderr, "%s : failed to parse\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000811 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000812 }
813 } else {
814 fprintf(stderr, "Unable to open %s\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000815 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000816 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000817#ifdef HAVE_SYS_MMAN_H
818 if (memory) {
819 xmlFreeParserInputBuffer(input);
820 munmap((char *) base, info.st_size);
821 close(fd);
822 }
823#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000824}
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000825
826static void walkDoc(xmlDocPtr doc) {
827 xmlTextReaderPtr reader;
828 int ret;
829
830 reader = xmlReaderWalker(doc);
831 if (reader != NULL) {
832 if ((timing) && (!repeat)) {
833 startTimer();
834 }
835 ret = xmlTextReaderRead(reader);
836 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000837 if ((debug)
838#ifdef LIBXML_PATTERN_ENABLED
839 || (patternc)
840#endif
841 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000842 processNode(reader);
843 ret = xmlTextReaderRead(reader);
844 }
845 if ((timing) && (!repeat)) {
846 endTimer("walking through the doc");
847 }
848 xmlFreeTextReader(reader);
849 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000850 fprintf(stderr, "failed to walk through the doc\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000851 progresult = 1;
852 }
853 } else {
854 fprintf(stderr, "Failed to crate a reader from the document\n");
855 progresult = 1;
856 }
857}
Daniel Veillard81273902003-09-30 00:43:48 +0000858#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000859
860/************************************************************************
861 * *
862 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000863 * *
864 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000865static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000866 xmlDocPtr doc = NULL;
867#ifdef LIBXML_TREE_ENABLED
868 xmlDocPtr tmp;
869#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000870
Daniel Veillard48b2f892001-02-25 16:11:03 +0000871 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000872 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000873
874
Daniel Veillard652327a2003-09-29 18:02:38 +0000875#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000876 if (filename == NULL) {
877 if (generate) {
878 xmlNodePtr n;
879
880 doc = xmlNewDoc(BAD_CAST "1.0");
881 n = xmlNewNode(NULL, BAD_CAST "info");
882 xmlNodeSetContent(n, BAD_CAST "abc");
883 xmlDocSetRootElement(doc, n);
884 }
885 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000886#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000887#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000888#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000889 else if ((html) && (push)) {
890 FILE *f;
891
892 f = fopen(filename, "r");
893 if (f != NULL) {
894 int res, size = 3;
895 char chars[4096];
896 htmlParserCtxtPtr ctxt;
897
898 /* if (repeat) */
899 size = 4096;
900 res = fread(chars, 1, 4, f);
901 if (res > 0) {
902 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000903 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000904 while ((res = fread(chars, 1, size, f)) > 0) {
905 htmlParseChunk(ctxt, chars, res, 0);
906 }
907 htmlParseChunk(ctxt, chars, 0, 1);
908 doc = ctxt->myDoc;
909 htmlFreeParserCtxt(ctxt);
910 }
911 fclose(f);
912 }
913 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000914#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000915 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000916 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000917 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000918#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000919 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000920#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000921 /*
922 * build an XML tree from a string;
923 */
924 if (push) {
925 FILE *f;
926
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000927 /* '-' Usually means stdin -<sven@zen.org> */
928 if ((filename[0] == '-') && (filename[1] == 0)) {
929 f = stdin;
930 } else {
931 f = fopen(filename, "r");
932 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000933 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000934 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000935 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000936 char chars[1024];
937 xmlParserCtxtPtr ctxt;
938
Daniel Veillarda880b122003-04-21 21:36:41 +0000939 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000940 res = fread(chars, 1, 4, f);
941 if (res > 0) {
942 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
943 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +0000944 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000945 while ((res = fread(chars, 1, size, f)) > 0) {
946 xmlParseChunk(ctxt, chars, res, 0);
947 }
948 xmlParseChunk(ctxt, chars, 0, 1);
949 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000950 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000951 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000952 if (!ret) {
953 xmlFreeDoc(doc);
954 doc = NULL;
955 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000956 }
957 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000958 } else
959#endif /* LIBXML_PUSH_ENABLED */
960 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000961 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000962 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000963 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000964 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000965
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000966 f = fopen(filename, "r");
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000967 if (f != NULL) {
968 if (rectxt == NULL)
969 doc = xmlReadIO((xmlInputReadCallback) myRead,
970 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000971 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000972 else
973 doc = xmlCtxtReadIO(rectxt,
974 (xmlInputReadCallback) myRead,
975 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000976 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000977 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +0000978 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000979 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000980 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000981 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000982
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000983 if (rectxt == NULL)
984 ctxt = xmlNewParserCtxt();
985 else
986 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000987 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000988 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000989 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000990 ctxt->sax->error = xmlHTMLError;
991 ctxt->sax->warning = xmlHTMLWarning;
992 ctxt->vctxt.error = xmlHTMLValidityError;
993 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000994
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000995 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000996
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000997 if (rectxt == NULL)
998 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000999 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00001000#ifdef HAVE_SYS_MMAN_H
1001 } else if (memory) {
1002 int fd;
1003 struct stat info;
1004 const char *base;
1005 if (stat(filename, &info) < 0)
1006 return;
1007 if ((fd = open(filename, O_RDONLY)) < 0)
1008 return;
1009 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00001010 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00001011 return;
1012
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001013 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00001014 doc = xmlReadMemory((char *) base, info.st_size,
1015 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001016 else
Daniel Veillard60942de2003-09-25 21:05:58 +00001017 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
1018 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001019
Daniel Veillard46e370e2000-07-21 20:32:03 +00001020 munmap((char *) base, info.st_size);
1021#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001022#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00001023 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001024 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001025
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001026 if (rectxt == NULL)
1027 ctxt = xmlNewParserCtxt();
1028 else
1029 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001030 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001031 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001032 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001033 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
1034
1035 if (ctxt->valid == 0)
1036 progresult = 4;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001037 if (rectxt == NULL)
1038 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001039 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001040#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001041 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001042 if (rectxt != NULL)
1043 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
1044 else
1045 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001046 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001047 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001048
Daniel Veillard88a172f2000-08-04 18:23:10 +00001049 /*
1050 * If we don't have a document we might as well give up. Do we
1051 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001052 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001053 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001054 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001055 }
1056
Daniel Veillard48b2f892001-02-25 16:11:03 +00001057 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001058 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001059 }
1060
Daniel Veillard29e43992001-12-13 22:21:58 +00001061 /*
1062 * Remove DOCTYPE nodes
1063 */
1064 if (dropdtd) {
1065 xmlDtdPtr dtd;
1066
1067 dtd = xmlGetIntSubset(doc);
1068 if (dtd != NULL) {
1069 xmlUnlinkNode((xmlNodePtr)dtd);
1070 xmlFreeDtd(dtd);
1071 }
1072 }
1073
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001074#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00001075 if (xinclude) {
1076 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001077 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001078 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +00001079 xmlXIncludeProcessFlags(doc, options);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001080 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001081 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001082 }
1083 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001084#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00001085
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001086#ifdef LIBXML_DEBUG_ENABLED
1087 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001088 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001089 */
1090 if (shell)
1091 xmlShell(doc, filename, xmlShellReadline, stdout);
1092#endif
1093
Daniel Veillard652327a2003-09-29 18:02:38 +00001094#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001095 /*
1096 * test intermediate copy if needed.
1097 */
1098 if (copy) {
1099 tmp = doc;
1100 doc = xmlCopyDoc(doc, 1);
1101 xmlFreeDoc(tmp);
1102 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001103#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001104
Daniel Veillard4432df22003-09-28 18:58:27 +00001105#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001106 if ((insert) && (!html)) {
1107 const xmlChar* list[256];
1108 int nb, i;
1109 xmlNodePtr node;
1110
1111 if (doc->children != NULL) {
1112 node = doc->children;
1113 while ((node != NULL) && (node->last == NULL)) node = node->next;
1114 if (node != NULL) {
1115 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1116 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001117 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001118 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001119 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001120 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001121 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001122 nb);
1123 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001124 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001125 }
1126 }
1127 }
1128 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001129 }else
1130#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001131#ifdef LIBXML_READER_ENABLED
1132 if (walker) {
1133 walkDoc(doc);
1134 }
1135#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001136#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001137 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001138 int ret;
1139
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001140 /*
1141 * print it.
1142 */
1143#ifdef LIBXML_DEBUG_ENABLED
1144 if (!debug) {
1145#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001146 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001147 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001148 }
Daniel Veillard656ce942004-04-30 23:11:45 +00001149#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00001150 if ((html) && (!xmlout)) {
1151 if (compress) {
1152 htmlSaveFile(output ? output : "-", doc);
1153 }
1154 else if (encoding != NULL) {
1155 if ( format ) {
1156 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1157 }
1158 else {
1159 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1160 }
1161 }
1162 else if (format) {
1163 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1164 }
1165 else {
1166 FILE *out;
1167 if (output == NULL)
1168 out = stdout;
1169 else {
1170 out = fopen(output,"wb");
1171 }
1172 if (out != NULL) {
1173 if (htmlDocDump(out, doc) < 0)
1174 progresult = 6;
1175
1176 if (output != NULL)
1177 fclose(out);
1178 } else {
1179 fprintf(stderr, "failed to open %s\n", output);
1180 progresult = 6;
1181 }
1182 }
1183 if ((timing) && (!repeat)) {
1184 endTimer("Saving");
1185 }
1186 } else
1187#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001188#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001189 if (memory) {
1190 xmlChar *result;
1191 int len;
1192
1193 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001194 if ( format ) {
1195 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1196 } else {
1197 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1198 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001199 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001200 if (format)
1201 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1202 else
1203 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001204 }
1205 if (result == NULL) {
1206 fprintf(stderr, "Failed to save\n");
1207 } else {
1208 write(1, result, len);
1209 xmlFree(result);
1210 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001211 } else
1212#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001213 if (compress) {
1214 xmlSaveFile(output ? output : "-", doc);
1215 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001216 else if (encoding != NULL) {
1217 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001218 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
1219 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001220 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001221 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00001222 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
1223 }
1224 if (ret < 0) {
1225 fprintf(stderr, "failed save to %s\n",
1226 output ? output : "-");
1227 progresult = 6;
Daniel Veillardd536f702001-11-08 17:32:47 +00001228 }
1229 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001230 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001231 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
1232 if (ret < 0) {
1233 fprintf(stderr, "failed save to %s\n",
1234 output ? output : "-");
1235 progresult = 6;
1236 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001237 }
1238 else {
1239 FILE *out;
1240 if (output == NULL)
1241 out = stdout;
1242 else {
1243 out = fopen(output,"wb");
1244 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001245 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001246 if (xmlDocDump(out, doc) < 0)
1247 progresult = 6;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001248
Daniel Veillard05d987b2003-10-08 11:54:57 +00001249 if (output != NULL)
1250 fclose(out);
1251 } else {
1252 fprintf(stderr, "failed to open %s\n", output);
1253 progresult = 6;
1254 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001255 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001256 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001257 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001258 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001259#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001260 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001261 FILE *out;
1262 if (output == NULL)
1263 out = stdout;
1264 else {
1265 out = fopen(output,"wb");
1266 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001267 if (out != NULL) {
1268 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001269
Daniel Veillard05d987b2003-10-08 11:54:57 +00001270 if (output != NULL)
1271 fclose(out);
1272 } else {
1273 fprintf(stderr, "failed to open %s\n", output);
1274 progresult = 6;
1275 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001276 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001277#endif
1278 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001279#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001280
Daniel Veillard4432df22003-09-28 18:58:27 +00001281#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001282 /*
1283 * A posteriori validation test
1284 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001285 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001286 xmlDtdPtr dtd;
1287
Daniel Veillard48b2f892001-02-25 16:11:03 +00001288 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001289 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001290 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001291 if (dtdvalid != NULL)
1292 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1293 else
1294 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001295 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001296 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001297 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001298 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001299 if (dtdvalid != NULL)
1300 xmlGenericError(xmlGenericErrorContext,
1301 "Could not parse DTD %s\n", dtdvalid);
1302 else
1303 xmlGenericError(xmlGenericErrorContext,
1304 "Could not parse DTD %s\n", dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001305 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001306 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001307 xmlValidCtxtPtr cvp;
1308
1309 if ((cvp = xmlNewValidCtxt()) == NULL) {
1310 xmlGenericError(xmlGenericErrorContext,
1311 "Couldn't allocate validation context\n");
1312 exit(-1);
1313 }
1314 cvp->userData = (void *) stderr;
1315 cvp->error = (xmlValidityErrorFunc) fprintf;
1316 cvp->warning = (xmlValidityWarningFunc) fprintf;
1317
Daniel Veillard48b2f892001-02-25 16:11:03 +00001318 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001319 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001320 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001321 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001322 if (dtdvalid != NULL)
1323 xmlGenericError(xmlGenericErrorContext,
1324 "Document %s does not validate against %s\n",
1325 filename, dtdvalid);
1326 else
1327 xmlGenericError(xmlGenericErrorContext,
1328 "Document %s does not validate against %s\n",
1329 filename, dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001330 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001331 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001332 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001333 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001334 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001335 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001336 xmlFreeDtd(dtd);
1337 }
1338 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001339 xmlValidCtxtPtr cvp;
1340
1341 if ((cvp = xmlNewValidCtxt()) == NULL) {
1342 xmlGenericError(xmlGenericErrorContext,
1343 "Couldn't allocate validation context\n");
1344 exit(-1);
1345 }
1346
Daniel Veillard48b2f892001-02-25 16:11:03 +00001347 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001348 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001349 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001350 cvp->userData = (void *) stderr;
1351 cvp->error = (xmlValidityErrorFunc) fprintf;
1352 cvp->warning = (xmlValidityWarningFunc) fprintf;
1353 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001354 xmlGenericError(xmlGenericErrorContext,
1355 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001356 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001357 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001358 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001359 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001360 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001361 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001362 }
1363#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001364#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001365 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001366 xmlRelaxNGValidCtxtPtr ctxt;
1367 int ret;
1368
Daniel Veillard42f12e92003-03-07 18:32:59 +00001369 if ((timing) && (!repeat)) {
1370 startTimer();
1371 }
1372
Daniel Veillard71531f32003-02-05 13:19:53 +00001373 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1374 xmlRelaxNGSetValidErrors(ctxt,
1375 (xmlRelaxNGValidityErrorFunc) fprintf,
1376 (xmlRelaxNGValidityWarningFunc) fprintf,
1377 stderr);
1378 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1379 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001380 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00001381 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001382 fprintf(stderr, "%s fails to validate\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00001383 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001384 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00001385 filename);
1386 }
1387 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001388 if ((timing) && (!repeat)) {
1389 endTimer("Validating");
1390 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001391 } else if (wxschemas != NULL) {
1392 xmlSchemaValidCtxtPtr ctxt;
1393 int ret;
1394
1395 if ((timing) && (!repeat)) {
1396 startTimer();
1397 }
1398
1399 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1400 xmlSchemaSetValidErrors(ctxt,
1401 (xmlSchemaValidityErrorFunc) fprintf,
1402 (xmlSchemaValidityWarningFunc) fprintf,
1403 stderr);
1404 ret = xmlSchemaValidateDoc(ctxt, doc);
1405 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001406 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001407 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001408 fprintf(stderr, "%s fails to validate\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001409 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001410 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001411 filename);
1412 }
1413 xmlSchemaFreeValidCtxt(ctxt);
1414 if ((timing) && (!repeat)) {
1415 endTimer("Validating");
1416 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001417 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001418#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001419
1420#ifdef LIBXML_DEBUG_ENABLED
1421 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001422 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001423#endif
1424
1425 /*
1426 * free it.
1427 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001428 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001429 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001430 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001431 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001432 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001433 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001434 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001435}
1436
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001437/************************************************************************
1438 * *
1439 * Usage and Main *
1440 * *
1441 ************************************************************************/
1442
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001443static void showVersion(const char *name) {
1444 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1445 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001446#ifdef LIBXML_VALID_ENABLED
1447 fprintf(stderr, "DTDValid ");
1448#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001449#ifdef LIBXML_FTP_ENABLED
1450 fprintf(stderr, "FTP ");
1451#endif
1452#ifdef LIBXML_HTTP_ENABLED
1453 fprintf(stderr, "HTTP ");
1454#endif
1455#ifdef LIBXML_HTML_ENABLED
1456 fprintf(stderr, "HTML ");
1457#endif
1458#ifdef LIBXML_C14N_ENABLED
1459 fprintf(stderr, "C14N ");
1460#endif
1461#ifdef LIBXML_CATALOG_ENABLED
1462 fprintf(stderr, "Catalog ");
1463#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001464#ifdef LIBXML_XPATH_ENABLED
1465 fprintf(stderr, "XPath ");
1466#endif
1467#ifdef LIBXML_XPTR_ENABLED
1468 fprintf(stderr, "XPointer ");
1469#endif
1470#ifdef LIBXML_XINCLUDE_ENABLED
1471 fprintf(stderr, "XInclude ");
1472#endif
1473#ifdef LIBXML_ICONV_ENABLED
1474 fprintf(stderr, "Iconv ");
1475#endif
1476#ifdef DEBUG_MEMORY_LOCATION
1477 fprintf(stderr, "MemDebug ");
1478#endif
1479#ifdef LIBXML_UNICODE_ENABLED
1480 fprintf(stderr, "Unicode ");
1481#endif
1482#ifdef LIBXML_REGEXP_ENABLED
1483 fprintf(stderr, "Regexps ");
1484#endif
1485#ifdef LIBXML_AUTOMATA_ENABLED
1486 fprintf(stderr, "Automata ");
1487#endif
1488#ifdef LIBXML_SCHEMAS_ENABLED
1489 fprintf(stderr, "Schemas ");
1490#endif
1491 fprintf(stderr, "\n");
1492}
1493
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001494static void usage(const char *name) {
1495 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001496#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001497 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001498#else
1499 printf("\tParse the XML files\n");
1500#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001501 printf("\t--version : display the version of the XML library used\n");
1502#ifdef LIBXML_DEBUG_ENABLED
1503 printf("\t--debug : dump a debug tree of the in-memory document\n");
1504 printf("\t--shell : run a navigating shell\n");
1505 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001506#else
Daniel Veillard81273902003-09-30 00:43:48 +00001507#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001508 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001509#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001510#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001511#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001512 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001513#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001514 printf("\t--recover : output what was parsable on broken XML documents\n");
1515 printf("\t--noent : substitute entity references by their value\n");
1516 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001517 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001518 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001519 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001520#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001521 printf("\t--valid : validate the document in addition to std well-formed check\n");
1522 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1523 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001524 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001525#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001526 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001527 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001528 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1529 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001530#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001531#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001532 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001533#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001534#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001535#ifdef LIBXML_HTML_ENABLED
1536 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001537 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001538#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001539#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001540 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001541#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001542#ifdef HAVE_SYS_MMAN_H
1543 printf("\t--memory : parse from memory\n");
1544#endif
Daniel Veillard87076042004-05-03 22:54:49 +00001545 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001546 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1547 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001548 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001549#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001550 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001551 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001552 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1553#endif /* LIBXML_OUTPUT_ENABLED */
1554 printf("\t--nsclean : remove redundant namespace declarations\n");
1555 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001556#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001557 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1558 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001559 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00001560 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001561#endif
1562 printf("\t--auto : generate a small doc on the fly\n");
1563#ifdef LIBXML_XINCLUDE_ENABLED
1564 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001565#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001566 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001567 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001568#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001569 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001570 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001571#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001572#ifdef LIBXML_PATTERN_ENABLED
1573 printf("\t--pattern pattern_value : test the pattern support\n");
1574#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001575 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001576#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001577 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001578 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001579#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001580 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1581 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001582}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001583
1584static void registerNode(xmlNodePtr node)
1585{
1586 node->_private = malloc(sizeof(long));
1587 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001588 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001589}
1590
1591static void deregisterNode(xmlNodePtr node)
1592{
1593 assert(node->_private != NULL);
1594 assert(*(long*)node->_private == (long) 0x81726354);
1595 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001596 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001597}
1598
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001599int
1600main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001601 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001602 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001603 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001604 const char* indent;
1605
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001606 if (argc <= 1) {
1607 usage(argv[0]);
1608 return(1);
1609 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001610 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001611 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001612 if (!strcmp(argv[i], "-"))
1613 break;
1614
1615 if (argv[i][0] != '-')
1616 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001617 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1618 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001619 else
1620#ifdef LIBXML_DEBUG_ENABLED
1621 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001622 (!strcmp(argv[i], "--shell"))) {
1623 shell++;
1624 noout = 1;
1625 } else
1626#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001627#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001628 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1629 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001630 else
1631#endif /* LIBXML_TREE_ENABLED */
1632 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001633 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001634 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001635 options |= XML_PARSE_RECOVER;
1636 } else if ((!strcmp(argv[i], "-noent")) ||
1637 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001638 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001639 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001640 } else if ((!strcmp(argv[i], "-nsclean")) ||
1641 (!strcmp(argv[i], "--nsclean"))) {
1642 options |= XML_PARSE_NSCLEAN;
1643 } else if ((!strcmp(argv[i], "-nocdata")) ||
1644 (!strcmp(argv[i], "--nocdata"))) {
1645 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001646 } else if ((!strcmp(argv[i], "-nodict")) ||
1647 (!strcmp(argv[i], "--nodict"))) {
1648 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001649 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001650 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001651 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001652 version = 1;
1653 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001654 (!strcmp(argv[i], "--noout")))
1655 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001656#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001657 else if ((!strcmp(argv[i], "-o")) ||
1658 (!strcmp(argv[i], "-output")) ||
1659 (!strcmp(argv[i], "--output"))) {
1660 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001661 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001662 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001663#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001664 else if ((!strcmp(argv[i], "-htmlout")) ||
1665 (!strcmp(argv[i], "--htmlout")))
1666 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001667 else if ((!strcmp(argv[i], "-nowrap")) ||
1668 (!strcmp(argv[i], "--nowrap")))
1669 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001670#ifdef LIBXML_HTML_ENABLED
1671 else if ((!strcmp(argv[i], "-html")) ||
1672 (!strcmp(argv[i], "--html"))) {
1673 html++;
1674 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001675 else if ((!strcmp(argv[i], "-xmlout")) ||
1676 (!strcmp(argv[i], "--xmlout"))) {
1677 xmlout++;
1678 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001679#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001680 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001681 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001682 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001683 options |= XML_PARSE_DTDLOAD;
1684 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001685 (!strcmp(argv[i], "--dtdattr"))) {
1686 loaddtd++;
1687 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001688 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001689 }
1690#ifdef LIBXML_VALID_ENABLED
1691 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001692 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001693 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001694 options |= XML_PARSE_DTDVALID;
1695 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001696 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001697 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001698 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001699 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001700 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001701 (!strcmp(argv[i], "--dtdvalid"))) {
1702 i++;
1703 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001704 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001705 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001706 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1707 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1708 i++;
1709 dtdvalidfpi = argv[i];
1710 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001711 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001712 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001713#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001714 else if ((!strcmp(argv[i], "-dropdtd")) ||
1715 (!strcmp(argv[i], "--dropdtd")))
1716 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001717 else if ((!strcmp(argv[i], "-insert")) ||
1718 (!strcmp(argv[i], "--insert")))
1719 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001720 else if ((!strcmp(argv[i], "-timing")) ||
1721 (!strcmp(argv[i], "--timing")))
1722 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001723 else if ((!strcmp(argv[i], "-auto")) ||
1724 (!strcmp(argv[i], "--auto")))
1725 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001726 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001727 (!strcmp(argv[i], "--repeat"))) {
1728 if (repeat)
1729 repeat *= 10;
1730 else
1731 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001732 }
1733#ifdef LIBXML_PUSH_ENABLED
1734 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001735 (!strcmp(argv[i], "--push")))
1736 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001737#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001738#ifdef HAVE_SYS_MMAN_H
1739 else if ((!strcmp(argv[i], "-memory")) ||
1740 (!strcmp(argv[i], "--memory")))
1741 memory++;
1742#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001743 else if ((!strcmp(argv[i], "-testIO")) ||
1744 (!strcmp(argv[i], "--testIO")))
1745 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001746#ifdef LIBXML_XINCLUDE_ENABLED
1747 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001748 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001749 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001750 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001751 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001752#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001753#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001754#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001755 else if ((!strcmp(argv[i], "-compress")) ||
1756 (!strcmp(argv[i], "--compress"))) {
1757 compress++;
1758 xmlSetCompressMode(9);
1759 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001760#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001761#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001762 else if ((!strcmp(argv[i], "-nowarning")) ||
1763 (!strcmp(argv[i], "--nowarning"))) {
1764 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001765 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001766 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001767 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001768 else if ((!strcmp(argv[i], "-pedantic")) ||
1769 (!strcmp(argv[i], "--pedantic"))) {
1770 xmlGetWarningsDefaultValue = 1;
1771 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001772 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001773 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001774#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001775 else if ((!strcmp(argv[i], "-debugent")) ||
1776 (!strcmp(argv[i], "--debugent"))) {
1777 debugent++;
1778 xmlParserDebugEntities = 1;
1779 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001780#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001781#ifdef LIBXML_CATALOG_ENABLED
1782 else if ((!strcmp(argv[i], "-catalogs")) ||
1783 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001784 catalogs++;
1785 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1786 (!strcmp(argv[i], "--nocatalogs"))) {
1787 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001788 }
1789#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001790 else if ((!strcmp(argv[i], "-encode")) ||
1791 (!strcmp(argv[i], "--encode"))) {
1792 i++;
1793 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001794 /*
1795 * OK it's for testing purposes
1796 */
1797 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001798 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001799 else if ((!strcmp(argv[i], "-noblanks")) ||
1800 (!strcmp(argv[i], "--noblanks"))) {
1801 noblanks++;
1802 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001803 }
Daniel Veillard87076042004-05-03 22:54:49 +00001804 else if ((!strcmp(argv[i], "-maxmem")) ||
1805 (!strcmp(argv[i], "--maxmem"))) {
1806 i++;
1807 if (sscanf(argv[i], "%d", &maxmem) == 1) {
1808 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
1809 myStrdupFunc);
1810 } else {
1811 maxmem = 0;
1812 }
1813 }
Daniel Veillard90493a92001-08-14 14:12:47 +00001814 else if ((!strcmp(argv[i], "-format")) ||
1815 (!strcmp(argv[i], "--format"))) {
1816 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001817#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001818 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001819#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001820 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001821 }
Daniel Veillard81273902003-09-30 00:43:48 +00001822#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001823 else if ((!strcmp(argv[i], "-stream")) ||
1824 (!strcmp(argv[i], "--stream"))) {
1825 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001826 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001827 else if ((!strcmp(argv[i], "-walker")) ||
1828 (!strcmp(argv[i], "--walker"))) {
1829 walker++;
1830 noout++;
1831 }
Daniel Veillard81273902003-09-30 00:43:48 +00001832#endif /* LIBXML_READER_ENABLED */
1833#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001834 else if ((!strcmp(argv[i], "-sax1")) ||
1835 (!strcmp(argv[i], "--sax1"))) {
1836 sax1++;
1837 }
Daniel Veillard81273902003-09-30 00:43:48 +00001838#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001839 else if ((!strcmp(argv[i], "-chkregister")) ||
1840 (!strcmp(argv[i], "--chkregister"))) {
1841 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001842#ifdef LIBXML_SCHEMAS_ENABLED
1843 } else if ((!strcmp(argv[i], "-relaxng")) ||
1844 (!strcmp(argv[i], "--relaxng"))) {
1845 i++;
1846 relaxng = argv[i];
1847 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001848 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001849 } else if ((!strcmp(argv[i], "-schema")) ||
1850 (!strcmp(argv[i], "--schema"))) {
1851 i++;
1852 schema = argv[i];
1853 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001854#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001855 } else if ((!strcmp(argv[i], "-nonet")) ||
1856 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00001857 options |= XML_PARSE_NONET;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001858#ifdef LIBXML_PATTERN_ENABLED
1859 } else if ((!strcmp(argv[i], "-pattern")) ||
1860 (!strcmp(argv[i], "--pattern"))) {
1861 i++;
1862 pattern = argv[i];
1863#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001864 } else {
1865 fprintf(stderr, "Unknown option %s\n", argv[i]);
1866 usage(argv[0]);
1867 return(1);
1868 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001869 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001870
1871#ifdef LIBXML_CATALOG_ENABLED
1872 if (nocatalogs == 0) {
1873 if (catalogs) {
1874 const char *catal;
1875
1876 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001877 if (catal != NULL) {
1878 xmlLoadCatalogs(catal);
1879 } else {
1880 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1881 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001882 }
1883 }
1884#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001885
Daniel Veillard81273902003-09-30 00:43:48 +00001886#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001887 if (sax1)
1888 xmlSAXDefaultVersion(1);
1889 else
1890 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001891#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001892
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001893 if (chkregister) {
1894 xmlRegisterNodeDefault(registerNode);
1895 xmlDeregisterNodeDefault(deregisterNode);
1896 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001897
1898 indent = getenv("XMLLINT_INDENT");
1899 if(indent != NULL) {
1900 xmlTreeIndentString = indent;
1901 }
1902
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001903
Daniel Veillardd9bad132001-07-23 19:39:43 +00001904 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001905 if (loaddtd != 0)
1906 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1907 if (dtdattrs)
1908 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001909 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001910#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001911 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001912#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001913 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001914 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001915 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001916 xmlGenericError(xmlGenericErrorContext,
1917 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1918 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001919 "<html><head><title>%s output</title></head>\n",
1920 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001921 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001922 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1923 argv[0]);
1924 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001925
1926#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001927 if ((relaxng != NULL)
1928#ifdef LIBXML_READER_ENABLED
1929 && (stream == 0)
1930#endif /* LIBXML_READER_ENABLED */
1931 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001932 xmlRelaxNGParserCtxtPtr ctxt;
1933
Daniel Veillardce192eb2003-04-16 15:58:05 +00001934 /* forces loading the DTDs */
1935 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001936 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001937 if (timing) {
1938 startTimer();
1939 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001940 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1941 xmlRelaxNGSetParserErrors(ctxt,
1942 (xmlRelaxNGValidityErrorFunc) fprintf,
1943 (xmlRelaxNGValidityWarningFunc) fprintf,
1944 stderr);
1945 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001946 if (relaxngschemas == NULL) {
1947 xmlGenericError(xmlGenericErrorContext,
1948 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001949 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001950 relaxng = NULL;
1951 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001952 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001953 if (timing) {
1954 endTimer("Compiling the schemas");
1955 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00001956 } else if ((schema != NULL)
1957#ifdef LIBXML_READER_ENABLED
1958 && (stream == 0)
1959#endif
1960 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001961 xmlSchemaParserCtxtPtr ctxt;
1962
1963 if (timing) {
1964 startTimer();
1965 }
1966 ctxt = xmlSchemaNewParserCtxt(schema);
1967 xmlSchemaSetParserErrors(ctxt,
1968 (xmlSchemaValidityErrorFunc) fprintf,
1969 (xmlSchemaValidityWarningFunc) fprintf,
1970 stderr);
1971 wxschemas = xmlSchemaParse(ctxt);
1972 if (wxschemas == NULL) {
1973 xmlGenericError(xmlGenericErrorContext,
1974 "WXS schema %s failed to compile\n", schema);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001975 progresult = 5;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001976 schema = NULL;
1977 }
1978 xmlSchemaFreeParserCtxt(ctxt);
1979 if (timing) {
1980 endTimer("Compiling the schemas");
1981 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001982 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001983#endif /* LIBXML_SCHEMAS_ENABLED */
1984#ifdef LIBXML_PATTERN_ENABLED
1985 if (pattern != NULL) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00001986 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001987 if (patternc == NULL) {
1988 xmlGenericError(xmlGenericErrorContext,
1989 "Pattern %s failed to compile\n", pattern);
1990 progresult = 7;
1991 pattern = NULL;
1992 }
1993 }
1994#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001995 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001996 if ((!strcmp(argv[i], "-encode")) ||
1997 (!strcmp(argv[i], "--encode"))) {
1998 i++;
1999 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002000 } else if ((!strcmp(argv[i], "-o")) ||
2001 (!strcmp(argv[i], "-output")) ||
2002 (!strcmp(argv[i], "--output"))) {
2003 i++;
2004 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00002005 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002006#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00002007 if ((!strcmp(argv[i], "-dtdvalid")) ||
2008 (!strcmp(argv[i], "--dtdvalid"))) {
2009 i++;
2010 continue;
2011 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002012 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2013 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2014 i++;
2015 continue;
2016 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002017#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002018 if ((!strcmp(argv[i], "-relaxng")) ||
2019 (!strcmp(argv[i], "--relaxng"))) {
2020 i++;
2021 continue;
2022 }
Daniel Veillard87076042004-05-03 22:54:49 +00002023 if ((!strcmp(argv[i], "-maxmem")) ||
2024 (!strcmp(argv[i], "--maxmem"))) {
2025 i++;
2026 continue;
2027 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002028 if ((!strcmp(argv[i], "-schema")) ||
2029 (!strcmp(argv[i], "--schema"))) {
2030 i++;
2031 continue;
2032 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002033#ifdef LIBXML_PATTERN_ENABLED
2034 if ((!strcmp(argv[i], "-pattern")) ||
2035 (!strcmp(argv[i], "--pattern"))) {
2036 i++;
2037 continue;
2038 }
2039#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002040 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002041 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002042 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002043 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002044 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002045 xmlParserCtxtPtr ctxt = NULL;
2046
2047 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00002048#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002049 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002050 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002051 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00002052#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002053 if (ctxt == NULL)
2054 ctxt = xmlNewParserCtxt();
2055 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00002056#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002057 }
Daniel Veillard81273902003-09-30 00:43:48 +00002058#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002059 }
2060 if (ctxt != NULL)
2061 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00002062 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002063 nbregister = 0;
2064
Daniel Veillard81273902003-09-30 00:43:48 +00002065#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002066 if (stream != 0)
2067 streamFile(argv[i]);
2068 else
Daniel Veillard81273902003-09-30 00:43:48 +00002069#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002070 parseAndPrintFile(argv[i], NULL);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002071
2072 if ((chkregister) && (nbregister != 0)) {
2073 fprintf(stderr, "Registration count off: %d\n", nbregister);
2074 progresult = 8;
2075 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00002076 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002077 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00002078 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002079 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00002080 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002081 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002082 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002083 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002084 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002085 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002086 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002087 }
Daniel Veillard845cce42002-01-09 11:51:37 +00002088 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002089 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002090 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002091#ifdef LIBXML_SCHEMAS_ENABLED
2092 if (relaxngschemas != NULL)
2093 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002094 if (wxschemas != NULL)
2095 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00002096 xmlRelaxNGCleanupTypes();
2097#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002098#ifdef LIBXML_PATTERN_ENABLED
2099 if (patternc != NULL)
2100 xmlFreePattern(patternc);
2101#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002102 xmlCleanupParser();
2103 xmlMemoryDump();
2104
Daniel Veillardf7cd4812001-02-23 18:44:52 +00002105 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002106}
Daniel Veillard88a172f2000-08-04 18:23:10 +00002107