blob: 118a6919f2beebe0408028b9bfee87721157ddd3 [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 *
6 * Daniel.Veillard@w3.org
7 */
8
9#ifdef WIN32
10#include "win32config.h"
11#else
12#include "config.h"
13#endif
14
15#include <stdio.h>
16#include <string.h>
17#include <stdio.h>
18#include <stdarg.h>
19
20#ifdef HAVE_SYS_TYPES_H
21#include <sys/types.h>
22#endif
23#ifdef HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26#ifdef HAVE_FCNTL_H
27#include <fcntl.h>
28#endif
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000032#ifdef HAVE_SYS_MMAN_H
33#include <sys/mman.h>
Daniel Veillard87b95392000-08-12 21:12:04 +000034/* seems needed for Solaris */
35#ifndef MAP_FAILED
36#define MAP_FAILED ((void *) -1)
37#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000038#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000039#ifdef HAVE_STDLIB_H
40#include <stdlib.h>
41#endif
42#ifdef HAVE_LIBREADLINE
43#include <readline/readline.h>
44#ifdef HAVE_LIBHISTORY
45#include <readline/history.h>
46#endif
47#endif
48
49#include <libxml/xmlmemory.h>
50#include <libxml/parser.h>
51#include <libxml/parserInternals.h>
52#include <libxml/HTMLparser.h>
53#include <libxml/HTMLtree.h>
54#include <libxml/tree.h>
55#include <libxml/xpath.h>
56#include <libxml/debugXML.h>
57
58#ifdef LIBXML_DEBUG_ENABLED
59static int debug = 0;
60static int shell = 0;
61static int debugent = 0;
62#endif
63static int copy = 0;
64static int recovery = 0;
65static int noent = 0;
66static int noout = 0;
67static int nowrap = 0;
68static int valid = 0;
69static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +000070static char * dtdvalid = NULL;
Daniel Veillardce8b83b2000-04-05 18:38:42 +000071static int repeat = 0;
72static int insert = 0;
73static int compress = 0;
74static int html = 0;
75static int htmlout = 0;
76static int push = 0;
Daniel Veillard46e370e2000-07-21 20:32:03 +000077#ifdef HAVE_SYS_MMAN_H
78static int memory = 0;
79#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000080static int noblanks = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +000081static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +000082static char *encoding = NULL;
Daniel Veillardce8b83b2000-04-05 18:38:42 +000083
84extern int xmlDoValidityCheckingDefaultValue;
85extern int xmlGetWarningsDefaultValue;
86
87/************************************************************************
88 * *
89 * HTML ouput *
90 * *
91 ************************************************************************/
92char buffer[50000];
93
94void
95xmlHTMLEncodeSend(void) {
96 char *result;
97
98 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
99 if (result) {
100 fprintf(stderr, "%s", result);
101 xmlFree(result);
102 }
103 buffer[0] = 0;
104}
105
106/**
107 * xmlHTMLPrintFileInfo:
108 * @input: an xmlParserInputPtr input
109 *
110 * Displays the associated file and line informations for the current input
111 */
112
113void
114xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
115 fprintf(stderr, "<p>");
116 if (input != NULL) {
117 if (input->filename) {
118 sprintf(&buffer[strlen(buffer)], "%s:%d: ", input->filename,
119 input->line);
120 } else {
121 sprintf(&buffer[strlen(buffer)], "Entity: line %d: ", input->line);
122 }
123 }
124 xmlHTMLEncodeSend();
125}
126
127/**
128 * xmlHTMLPrintFileContext:
129 * @input: an xmlParserInputPtr input
130 *
131 * Displays current context within the input content for error tracking
132 */
133
134void
135xmlHTMLPrintFileContext(xmlParserInputPtr input) {
136 const xmlChar *cur, *base;
137 int n;
138
139 if (input == NULL) return;
140 fprintf(stderr, "<pre>\n");
141 cur = input->cur;
142 base = input->base;
143 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
144 cur--;
145 }
146 n = 0;
147 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
148 cur--;
149 if ((*cur == '\n') || (*cur == '\r')) cur++;
150 base = cur;
151 n = 0;
152 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
153 sprintf(&buffer[strlen(buffer)], "%c", (unsigned char) *cur++);
154 n++;
155 }
156 sprintf(&buffer[strlen(buffer)], "\n");
157 cur = input->cur;
158 while ((*cur == '\n') || (*cur == '\r'))
159 cur--;
160 n = 0;
161 while ((cur != base) && (n++ < 80)) {
162 sprintf(&buffer[strlen(buffer)], " ");
163 base++;
164 }
165 sprintf(&buffer[strlen(buffer)],"^\n");
166 xmlHTMLEncodeSend();
167 fprintf(stderr, "</pre>");
168}
169
170/**
171 * xmlHTMLError:
172 * @ctx: an XML parser context
173 * @msg: the message to display/transmit
174 * @...: extra parameters for the message display
175 *
176 * Display and format an error messages, gives file, line, position and
177 * extra parameters.
178 */
179void
180xmlHTMLError(void *ctx, const char *msg, ...)
181{
182 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
183 xmlParserInputPtr input;
184 xmlParserInputPtr cur = NULL;
185 va_list args;
186
187 buffer[0] = 0;
188 input = ctxt->input;
189 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
190 cur = input;
191 input = ctxt->inputTab[ctxt->inputNr - 2];
192 }
193
194 xmlHTMLPrintFileInfo(input);
195
196 fprintf(stderr, "<b>error</b>: ");
197 va_start(args, msg);
198 vsprintf(&buffer[strlen(buffer)], msg, args);
199 va_end(args);
200 xmlHTMLEncodeSend();
201 fprintf(stderr, "</p>\n");
202
203 xmlHTMLPrintFileContext(input);
204 xmlHTMLEncodeSend();
205}
206
207/**
208 * xmlHTMLWarning:
209 * @ctx: an XML parser context
210 * @msg: the message to display/transmit
211 * @...: extra parameters for the message display
212 *
213 * Display and format a warning messages, gives file, line, position and
214 * extra parameters.
215 */
216void
217xmlHTMLWarning(void *ctx, const char *msg, ...)
218{
219 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
220 xmlParserInputPtr input;
221 xmlParserInputPtr cur = NULL;
222 va_list args;
223
224 buffer[0] = 0;
225 input = ctxt->input;
226 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
227 cur = input;
228 input = ctxt->inputTab[ctxt->inputNr - 2];
229 }
230
231
232 xmlHTMLPrintFileInfo(input);
233
234 fprintf(stderr, "<b>warning</b>: ");
235 va_start(args, msg);
236 vsprintf(&buffer[strlen(buffer)], msg, args);
237 va_end(args);
238 xmlHTMLEncodeSend();
239 fprintf(stderr, "</p>\n");
240
241 xmlHTMLPrintFileContext(input);
242 xmlHTMLEncodeSend();
243}
244
245/**
246 * xmlHTMLValidityError:
247 * @ctx: an XML parser context
248 * @msg: the message to display/transmit
249 * @...: extra parameters for the message display
250 *
251 * Display and format an validity error messages, gives file,
252 * line, position and extra parameters.
253 */
254void
255xmlHTMLValidityError(void *ctx, const char *msg, ...)
256{
257 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
258 xmlParserInputPtr input;
259 va_list args;
260
261 buffer[0] = 0;
262 input = ctxt->input;
263 if ((input->filename == NULL) && (ctxt->inputNr > 1))
264 input = ctxt->inputTab[ctxt->inputNr - 2];
265
266 xmlHTMLPrintFileInfo(input);
267
268 fprintf(stderr, "<b>validity error</b>: ");
269 va_start(args, msg);
270 vsprintf(&buffer[strlen(buffer)], msg, args);
271 va_end(args);
272 xmlHTMLEncodeSend();
273 fprintf(stderr, "</p>\n");
274
275 xmlHTMLPrintFileContext(input);
276 xmlHTMLEncodeSend();
277}
278
279/**
280 * xmlHTMLValidityWarning:
281 * @ctx: an XML parser context
282 * @msg: the message to display/transmit
283 * @...: extra parameters for the message display
284 *
285 * Display and format a validity warning messages, gives file, line,
286 * position and extra parameters.
287 */
288void
289xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
290{
291 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
292 xmlParserInputPtr input;
293 va_list args;
294
295 buffer[0] = 0;
296 input = ctxt->input;
297 if ((input->filename == NULL) && (ctxt->inputNr > 1))
298 input = ctxt->inputTab[ctxt->inputNr - 2];
299
300 xmlHTMLPrintFileInfo(input);
301
302 fprintf(stderr, "<b>validity warning</b>: ");
303 va_start(args, msg);
304 vsprintf(&buffer[strlen(buffer)], msg, args);
305 va_end(args);
306 xmlHTMLEncodeSend();
307 fprintf(stderr, "</p>\n");
308
309 xmlHTMLPrintFileContext(input);
310 xmlHTMLEncodeSend();
311}
312
313/************************************************************************
314 * *
315 * Shell Interface *
316 * *
317 ************************************************************************/
318/**
319 * xmlShellReadline:
320 * @prompt: the prompt value
321 *
322 * Read a string
323 *
324 * Returns a pointer to it or NULL on EOF the caller is expected to
325 * free the returned string.
326 */
327char *
328xmlShellReadline(char *prompt) {
329#ifdef HAVE_LIBREADLINE
330 char *line_read;
331
332 /* Get a line from the user. */
333 line_read = readline (prompt);
334
335 /* If the line has any text in it, save it on the history. */
336 if (line_read && *line_read)
337 add_history (line_read);
338
339 return (line_read);
340#else
341 char line_read[501];
342
343 if (prompt != NULL)
344 fprintf(stdout, "%s", prompt);
345 if (!fgets(line_read, 500, stdin))
346 return(NULL);
347 line_read[500] = 0;
348 return(strdup(line_read));
349#endif
350}
351
352/************************************************************************
353 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000354 * I/O Interfaces *
355 * *
356 ************************************************************************/
357
358int myRead(FILE *f, char * buffer, int len) {
359 return(fread(buffer, 1, len, f));
360}
361void myClose(FILE *f) {
362 fclose(f);
363}
364
365/************************************************************************
366 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000367 * Test processing *
368 * *
369 ************************************************************************/
370void parseAndPrintFile(char *filename) {
371 xmlDocPtr doc = NULL, tmp;
372
373#ifdef LIBXML_HTML_ENABLED
374 if (html) {
375 doc = htmlParseFile(filename, NULL);
376 } else {
377#endif /* LIBXML_HTML_ENABLED */
378 /*
379 * build an XML tree from a string;
380 */
381 if (push) {
382 FILE *f;
383
384 f = fopen(filename, "r");
385 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000386 int ret;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000387 int res, size = 3;
388 char chars[1024];
389 xmlParserCtxtPtr ctxt;
390
391 if (repeat)
392 size = 1024;
393 res = fread(chars, 1, 4, f);
394 if (res > 0) {
395 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
396 chars, res, filename);
397 while ((res = fread(chars, 1, size, f)) > 0) {
398 xmlParseChunk(ctxt, chars, res, 0);
399 }
400 xmlParseChunk(ctxt, chars, 0, 1);
401 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000402 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000403 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000404 if (!ret) {
405 xmlFreeDoc(doc);
406 doc = NULL;
407 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000408 }
409 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000410 } else if (testIO) {
411 int ret;
412 FILE *f;
413
414 f = fopen(filename, "r");
415 if (f != NULL) {
416 xmlParserCtxtPtr ctxt;
417
418 ctxt = xmlCreateIOParserCtxt(NULL, NULL,
419 (xmlInputReadCallback) myRead,
420 (xmlInputCloseCallback) myClose,
421 f, XML_CHAR_ENCODING_NONE);
422 xmlParseDocument(ctxt);
423
424 ret = ctxt->wellFormed;
425 doc = ctxt->myDoc;
426 xmlFreeParserCtxt(ctxt);
427 if (!ret) {
428 xmlFreeDoc(doc);
429 doc = NULL;
430 }
431 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000432 } else if (recovery) {
433 doc = xmlRecoverFile(filename);
434 } else if (htmlout) {
435 int ret;
436 xmlParserCtxtPtr ctxt;
437 xmlSAXHandler silent, *old;
438
439 ctxt = xmlCreateFileParserCtxt(filename);
Daniel Veillard88a172f2000-08-04 18:23:10 +0000440
441 if (ctxt == NULL) {
442 /* If xmlCreateFileParseCtxt() return NULL something
443 strange happened so we don't want to do anything. Do
444 we want to print an error message here?
445 <sven@zen.org> */
Daniel Veillard7ebb1ee2000-08-04 18:24:45 +0000446 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000447 } else {
448 memcpy(&silent, ctxt->sax, sizeof(silent));
449 old = ctxt->sax;
450 silent.error = xmlHTMLError;
451 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000452 silent.warning = xmlHTMLWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000453 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000454 silent.warning = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000455 silent.fatalError = xmlHTMLError;
456 ctxt->sax = &silent;
457 ctxt->vctxt.error = xmlHTMLValidityError;
458 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000459 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000460 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000461 ctxt->vctxt.warning = NULL;
462
Daniel Veillard88a172f2000-08-04 18:23:10 +0000463 xmlParseDocument(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000464
Daniel Veillard88a172f2000-08-04 18:23:10 +0000465 ret = ctxt->wellFormed;
466 doc = ctxt->myDoc;
467 ctxt->sax = old;
468 xmlFreeParserCtxt(ctxt);
469 if (!ret) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000470 xmlFreeDoc(doc);
471 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000472 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000473 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000474#ifdef HAVE_SYS_MMAN_H
475 } else if (memory) {
476 int fd;
477 struct stat info;
478 const char *base;
479 if (stat(filename, &info) < 0)
480 return;
481 if ((fd = open(filename, O_RDONLY)) < 0)
482 return;
483 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000484 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000485 return;
486
487 doc = xmlParseMemory((char *) base, info.st_size);
488 munmap((char *) base, info.st_size);
489#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000490 } else
491 doc = xmlParseFile(filename);
492#ifdef LIBXML_HTML_ENABLED
493 }
494#endif
495
Daniel Veillard88a172f2000-08-04 18:23:10 +0000496 /*
497 * If we don't have a document we might as well give up. Do we
498 * want an error message here? <sven@zen.org> */
499 if (doc == NULL)
500 {
501 return;
502 }
503
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000504#ifdef LIBXML_DEBUG_ENABLED
505 /*
506 * shell interraction
507 */
508 if (shell)
509 xmlShell(doc, filename, xmlShellReadline, stdout);
510#endif
511
512 /*
513 * test intermediate copy if needed.
514 */
515 if (copy) {
516 tmp = doc;
517 doc = xmlCopyDoc(doc, 1);
518 xmlFreeDoc(tmp);
519 }
520
521 if ((insert) && (!html)) {
522 const xmlChar* list[256];
523 int nb, i;
524 xmlNodePtr node;
525
526 if (doc->children != NULL) {
527 node = doc->children;
528 while ((node != NULL) && (node->last == NULL)) node = node->next;
529 if (node != NULL) {
530 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
531 if (nb < 0) {
532 printf("could not get valid list of elements\n");
533 } else if (nb == 0) {
534 printf("No element can be indersted under root\n");
535 } else {
536 printf("%d element types can be indersted under root:\n",
537 nb);
538 for (i = 0;i < nb;i++) {
539 printf("%s\n", list[i]);
540 }
541 }
542 }
543 }
544 }else if (noout == 0) {
545 /*
546 * print it.
547 */
548#ifdef LIBXML_DEBUG_ENABLED
549 if (!debug) {
550#endif
551 if (compress)
552 xmlSaveFile("-", doc);
Daniel Veillardbe803962000-06-28 23:40:59 +0000553 else if (encoding != NULL)
554 xmlSaveFileEnc("-", doc, encoding);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000555 else
556 xmlDocDump(stdout, doc);
557#ifdef LIBXML_DEBUG_ENABLED
558 } else
559 xmlDebugDumpDocument(stdout, doc);
560#endif
561 }
562
563 /*
564 * A posteriori validation test
565 */
Daniel Veillardcd429612000-10-11 15:57:05 +0000566 if (dtdvalid != NULL) {
567 xmlDtdPtr dtd;
568
569 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
570 if (dtd == NULL) {
571 fprintf(stderr, "Could not parse DTD %s\n", dtdvalid);
572 } else {
573 xmlValidCtxt cvp;
574 cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf;
575 if (!xmlValidateDtd(&cvp, doc, dtd)) {
576 fprintf(stderr, "Document %s does not validate against %s\n",
577 filename, dtdvalid);
578 }
579 xmlFreeDtd(dtd);
580 }
581 } else if (postvalid) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000582 xmlValidCtxt cvp;
583 cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf;
Daniel Veillardcd429612000-10-11 15:57:05 +0000584 if (!xmlValidateDocument(&cvp, doc)) {
585 fprintf(stderr, "Document %s does not validate\n", filename);
586 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000587 }
588
589#ifdef LIBXML_DEBUG_ENABLED
590 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +0000591 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000592#endif
593
594 /*
595 * free it.
596 */
597 xmlFreeDoc(doc);
598}
599
600int main(int argc, char **argv) {
601 int i, count;
602 int files = 0;
603
Daniel Veillardbe803962000-06-28 23:40:59 +0000604 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000605 for (i = 1; i < argc ; i++) {
606#ifdef LIBXML_DEBUG_ENABLED
607 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
608 debug++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000609 else if ((!strcmp(argv[i], "-shell")) ||
610 (!strcmp(argv[i], "--shell"))) {
611 shell++;
612 noout = 1;
613 } else
614#endif
615 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
616 copy++;
617 else if ((!strcmp(argv[i], "-recover")) ||
618 (!strcmp(argv[i], "--recover")))
619 recovery++;
620 else if ((!strcmp(argv[i], "-noent")) ||
621 (!strcmp(argv[i], "--noent")))
622 noent++;
623 else if ((!strcmp(argv[i], "-noout")) ||
624 (!strcmp(argv[i], "--noout")))
625 noout++;
626 else if ((!strcmp(argv[i], "-htmlout")) ||
627 (!strcmp(argv[i], "--htmlout")))
628 htmlout++;
629#ifdef LIBXML_HTML_ENABLED
630 else if ((!strcmp(argv[i], "-html")) ||
631 (!strcmp(argv[i], "--html"))) {
632 html++;
633 }
634#endif /* LIBXML_HTML_ENABLED */
635 else if ((!strcmp(argv[i], "-nowrap")) ||
636 (!strcmp(argv[i], "--nowrap")))
637 nowrap++;
638 else if ((!strcmp(argv[i], "-valid")) ||
639 (!strcmp(argv[i], "--valid")))
640 valid++;
641 else if ((!strcmp(argv[i], "-postvalid")) ||
642 (!strcmp(argv[i], "--postvalid")))
643 postvalid++;
Daniel Veillardcd429612000-10-11 15:57:05 +0000644 else if ((!strcmp(argv[i], "-dtdvalid")) ||
645 (!strcmp(argv[i], "--dtdvalid"))) {
646 i++;
647 dtdvalid = argv[i];
648 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000649 else if ((!strcmp(argv[i], "-insert")) ||
650 (!strcmp(argv[i], "--insert")))
651 insert++;
652 else if ((!strcmp(argv[i], "-repeat")) ||
653 (!strcmp(argv[i], "--repeat")))
654 repeat++;
655 else if ((!strcmp(argv[i], "-push")) ||
656 (!strcmp(argv[i], "--push")))
657 push++;
Daniel Veillard46e370e2000-07-21 20:32:03 +0000658#ifdef HAVE_SYS_MMAN_H
659 else if ((!strcmp(argv[i], "-memory")) ||
660 (!strcmp(argv[i], "--memory")))
661 memory++;
662#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000663 else if ((!strcmp(argv[i], "-testIO")) ||
664 (!strcmp(argv[i], "--testIO")))
665 testIO++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000666 else if ((!strcmp(argv[i], "-compress")) ||
667 (!strcmp(argv[i], "--compress"))) {
668 compress++;
669 xmlSetCompressMode(9);
670 }
671 else if ((!strcmp(argv[i], "-nowarning")) ||
672 (!strcmp(argv[i], "--nowarning"))) {
673 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +0000674 xmlPedanticParserDefault(0);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000675 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +0000676 else if ((!strcmp(argv[i], "-pedantic")) ||
677 (!strcmp(argv[i], "--pedantic"))) {
678 xmlGetWarningsDefaultValue = 1;
679 xmlPedanticParserDefault(1);
680 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +0000681#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +0000682 else if ((!strcmp(argv[i], "-debugent")) ||
683 (!strcmp(argv[i], "--debugent"))) {
684 debugent++;
685 xmlParserDebugEntities = 1;
686 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +0000687#endif
Daniel Veillardbe803962000-06-28 23:40:59 +0000688 else if ((!strcmp(argv[i], "-encode")) ||
689 (!strcmp(argv[i], "--encode"))) {
690 i++;
691 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +0000692 /*
693 * OK it's for testing purposes
694 */
695 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +0000696 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000697 else if ((!strcmp(argv[i], "-noblanks")) ||
698 (!strcmp(argv[i], "--noblanks"))) {
699 noblanks++;
700 xmlKeepBlanksDefault(0);
701 }
702 }
703 if (noent != 0) xmlSubstituteEntitiesDefault(1);
704 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
705 if ((htmlout) && (!nowrap)) {
706 fprintf(stderr,
707 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
708 fprintf(stderr, "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
709 fprintf(stderr,
710 "<html><head><title>%s output</title></head>\n",
711 argv[0]);
712 fprintf(stderr,
713 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
714 argv[0]);
715 }
716 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +0000717 if ((!strcmp(argv[i], "-encode")) ||
718 (!strcmp(argv[i], "--encode"))) {
719 i++;
720 continue;
721 }
Daniel Veillardcd429612000-10-11 15:57:05 +0000722 if ((!strcmp(argv[i], "-dtdvalid")) ||
723 (!strcmp(argv[i], "--dtdvalid"))) {
724 i++;
725 continue;
726 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000727 if (argv[i][0] != '-') {
728 if (repeat) {
729 for (count = 0;count < 100 * repeat;count++)
730 parseAndPrintFile(argv[i]);
731 } else
732 parseAndPrintFile(argv[i]);
733 files ++;
734 }
735 }
736 if ((htmlout) && (!nowrap)) {
737 fprintf(stderr, "</body></html>\n");
738 }
739 if (files == 0) {
740 printf("Usage : %s [--debug] [--debugent] [--copy] [--recover] [--noent] [--noout] [--valid] [--repeat] XMLfiles ...\n",
741 argv[0]);
742 printf("\tParse the XML files and output the result of the parsing\n");
743#ifdef LIBXML_DEBUG_ENABLED
744 printf("\t--debug : dump a debug tree of the in-memory document\n");
745 printf("\t--shell : run a navigating shell\n");
746 printf("\t--debugent : debug the entities defined in the document\n");
747#endif
748 printf("\t--copy : used to test the internal copy implementation\n");
749 printf("\t--recover : output what was parsable on broken XML documents\n");
750 printf("\t--noent : substitute entity references by their value\n");
751 printf("\t--noout : don't output the result tree\n");
752 printf("\t--htmlout : output results as HTML\n");
753 printf("\t--nowarp : do not put HTML doc wrapper\n");
754 printf("\t--valid : validate the document in addition to std well-formed check\n");
755 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
Daniel Veillardcd429612000-10-11 15:57:05 +0000756 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000757 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
758 printf("\t--insert : ad-hoc test for valid insertions\n");
759 printf("\t--compress : turn on gzip compression of output\n");
760#ifdef LIBXML_HTML_ENABLED
761 printf("\t--html : use the HTML parser\n");
762#endif
763 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard46e370e2000-07-21 20:32:03 +0000764#ifdef HAVE_SYS_MMAN_H
765 printf("\t--memory : parse from memory\n");
766#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000767 printf("\t--nowarning : do not emit warnings from parser/validator\n");
768 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillard5e873c42000-04-12 13:27:38 +0000769 printf("\t--testIO : test user I/O support\n");
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000770 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000771 }
772 xmlCleanupParser();
773 xmlMemoryDump();
774
775 return(0);
776}
Daniel Veillard88a172f2000-08-04 18:23:10 +0000777