1 | /* |
---|
2 | * xmllint.c : a small tester program for XML input. |
---|
3 | * |
---|
4 | * See Copyright for the status of this software. |
---|
5 | * |
---|
6 | * daniel@veillard.com |
---|
7 | */ |
---|
8 | |
---|
9 | #include "libxml.h" |
---|
10 | |
---|
11 | #include <string.h> |
---|
12 | #include <stdarg.h> |
---|
13 | |
---|
14 | #include <assert.h> |
---|
15 | |
---|
16 | #if defined (_WIN32) && !defined(__CYGWIN__) |
---|
17 | #ifdef _MSC_VER |
---|
18 | #include <winsock2.h> |
---|
19 | #pragma comment(lib, "ws2_32.lib") |
---|
20 | #define gettimeofday(p1,p2) |
---|
21 | #include <time.h> |
---|
22 | #else /* _MSC_VER */ |
---|
23 | #include <sys/time.h> |
---|
24 | #endif /* _MSC_VER */ |
---|
25 | #else /* _WIN32 */ |
---|
26 | #ifdef HAVE_SYS_TIME_H |
---|
27 | #include <sys/time.h> |
---|
28 | #endif |
---|
29 | #ifdef HAVE_TIME_H |
---|
30 | #include <time.h> |
---|
31 | #endif |
---|
32 | #endif /* _WIN32 */ |
---|
33 | |
---|
34 | #ifdef HAVE_SYS_TIMEB_H |
---|
35 | #include <sys/timeb.h> |
---|
36 | #endif |
---|
37 | |
---|
38 | #ifdef HAVE_SYS_TYPES_H |
---|
39 | #include <sys/types.h> |
---|
40 | #endif |
---|
41 | #ifdef HAVE_SYS_STAT_H |
---|
42 | #include <sys/stat.h> |
---|
43 | #endif |
---|
44 | #ifdef HAVE_FCNTL_H |
---|
45 | #include <fcntl.h> |
---|
46 | #endif |
---|
47 | #ifdef HAVE_UNISTD_H |
---|
48 | #include <unistd.h> |
---|
49 | #endif |
---|
50 | #ifdef HAVE_SYS_MMAN_H |
---|
51 | #include <sys/mman.h> |
---|
52 | /* seems needed for Solaris */ |
---|
53 | #ifndef MAP_FAILED |
---|
54 | #define MAP_FAILED ((void *) -1) |
---|
55 | #endif |
---|
56 | #endif |
---|
57 | #ifdef HAVE_STDLIB_H |
---|
58 | #include <stdlib.h> |
---|
59 | #endif |
---|
60 | #ifdef HAVE_LIBREADLINE |
---|
61 | #include <readline/readline.h> |
---|
62 | #ifdef HAVE_LIBHISTORY |
---|
63 | #include <readline/history.h> |
---|
64 | #endif |
---|
65 | #endif |
---|
66 | |
---|
67 | #include <libxml/xmlmemory.h> |
---|
68 | #include <libxml/parser.h> |
---|
69 | #include <libxml/parserInternals.h> |
---|
70 | #include <libxml/HTMLparser.h> |
---|
71 | #include <libxml/HTMLtree.h> |
---|
72 | #include <libxml/tree.h> |
---|
73 | #include <libxml/xpath.h> |
---|
74 | #include <libxml/debugXML.h> |
---|
75 | #include <libxml/xmlerror.h> |
---|
76 | #ifdef LIBXML_XINCLUDE_ENABLED |
---|
77 | #include <libxml/xinclude.h> |
---|
78 | #endif |
---|
79 | #ifdef LIBXML_CATALOG_ENABLED |
---|
80 | #include <libxml/catalog.h> |
---|
81 | #endif |
---|
82 | #ifdef LIBXML_DOCB_ENABLED |
---|
83 | #include <libxml/DOCBparser.h> |
---|
84 | #endif |
---|
85 | #include <libxml/globals.h> |
---|
86 | #include <libxml/xmlreader.h> |
---|
87 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
88 | #include <libxml/relaxng.h> |
---|
89 | #endif |
---|
90 | |
---|
91 | #ifndef XML_XML_DEFAULT_CATALOG |
---|
92 | #define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog" |
---|
93 | #endif |
---|
94 | |
---|
95 | #ifdef LIBXML_DEBUG_ENABLED |
---|
96 | static int shell = 0; |
---|
97 | static int debugent = 0; |
---|
98 | #endif |
---|
99 | static int debug = 0; |
---|
100 | static int copy = 0; |
---|
101 | static int recovery = 0; |
---|
102 | static int noent = 0; |
---|
103 | static int noout = 0; |
---|
104 | static int nowrap = 0; |
---|
105 | static int valid = 0; |
---|
106 | static int postvalid = 0; |
---|
107 | static char * dtdvalid = NULL; |
---|
108 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
109 | static char * relaxng = NULL; |
---|
110 | static xmlRelaxNGPtr relaxngschemas = NULL; |
---|
111 | #endif |
---|
112 | static int repeat = 0; |
---|
113 | static int insert = 0; |
---|
114 | static int compress = 0; |
---|
115 | #ifdef LIBXML_DOCB_ENABLED |
---|
116 | static int sgml = 0; |
---|
117 | #endif |
---|
118 | static int html = 0; |
---|
119 | static int htmlout = 0; |
---|
120 | static int push = 0; |
---|
121 | #ifdef HAVE_SYS_MMAN_H |
---|
122 | static int memory = 0; |
---|
123 | #endif |
---|
124 | static int noblanks = 0; |
---|
125 | static int format = 0; |
---|
126 | static int testIO = 0; |
---|
127 | static char *encoding = NULL; |
---|
128 | #ifdef LIBXML_XINCLUDE_ENABLED |
---|
129 | static int xinclude = 0; |
---|
130 | #endif |
---|
131 | static int dtdattrs = 0; |
---|
132 | static int loaddtd = 0; |
---|
133 | static int progresult = 0; |
---|
134 | static int timing = 0; |
---|
135 | static int generate = 0; |
---|
136 | static int dropdtd = 0; |
---|
137 | #ifdef LIBXML_CATALOG_ENABLED |
---|
138 | static int catalogs = 0; |
---|
139 | static int nocatalogs = 0; |
---|
140 | #endif |
---|
141 | static int stream = 0; |
---|
142 | static int chkregister = 0; |
---|
143 | static const char *output = NULL; |
---|
144 | |
---|
145 | |
---|
146 | /* |
---|
147 | * Internal timing routines to remove the necessity to have unix-specific |
---|
148 | * function calls |
---|
149 | */ |
---|
150 | |
---|
151 | #ifndef HAVE_GETTIMEOFDAY |
---|
152 | #ifdef HAVE_SYS_TIMEB_H |
---|
153 | #ifdef HAVE_SYS_TIME_H |
---|
154 | #ifdef HAVE_FTIME |
---|
155 | |
---|
156 | static int |
---|
157 | my_gettimeofday(struct timeval *tvp, void *tzp) |
---|
158 | { |
---|
159 | struct timeb timebuffer; |
---|
160 | |
---|
161 | ftime(&timebuffer); |
---|
162 | if (tvp) { |
---|
163 | tvp->tv_sec = timebuffer.time; |
---|
164 | tvp->tv_usec = timebuffer.millitm * 1000L; |
---|
165 | } |
---|
166 | return (0); |
---|
167 | } |
---|
168 | #define HAVE_GETTIMEOFDAY 1 |
---|
169 | #define gettimeofday my_gettimeofday |
---|
170 | |
---|
171 | #endif /* HAVE_FTIME */ |
---|
172 | #endif /* HAVE_SYS_TIME_H */ |
---|
173 | #endif /* HAVE_SYS_TIMEB_H */ |
---|
174 | #endif /* !HAVE_GETTIMEOFDAY */ |
---|
175 | |
---|
176 | #if defined(HAVE_GETTIMEOFDAY) |
---|
177 | static struct timeval begin, end; |
---|
178 | |
---|
179 | /* |
---|
180 | * startTimer: call where you want to start timing |
---|
181 | */ |
---|
182 | static void |
---|
183 | startTimer(void) |
---|
184 | { |
---|
185 | gettimeofday(&begin, NULL); |
---|
186 | } |
---|
187 | |
---|
188 | /* |
---|
189 | * endTimer: call where you want to stop timing and to print out a |
---|
190 | * message about the timing performed; format is a printf |
---|
191 | * type argument |
---|
192 | */ |
---|
193 | static void |
---|
194 | endTimer(const char *fmt, ...) |
---|
195 | { |
---|
196 | long msec; |
---|
197 | va_list ap; |
---|
198 | |
---|
199 | gettimeofday(&end, NULL); |
---|
200 | msec = end.tv_sec - begin.tv_sec; |
---|
201 | msec *= 1000; |
---|
202 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
---|
203 | |
---|
204 | #ifndef HAVE_STDARG_H |
---|
205 | #error "endTimer required stdarg functions" |
---|
206 | #endif |
---|
207 | va_start(ap, fmt); |
---|
208 | vfprintf(stderr, fmt, ap); |
---|
209 | va_end(ap); |
---|
210 | |
---|
211 | fprintf(stderr, " took %ld ms\n", msec); |
---|
212 | } |
---|
213 | #elif defined(HAVE_TIME_H) |
---|
214 | /* |
---|
215 | * No gettimeofday function, so we have to make do with calling clock. |
---|
216 | * This is obviously less accurate, but there's little we can do about |
---|
217 | * that. |
---|
218 | */ |
---|
219 | #ifndef CLOCKS_PER_SEC |
---|
220 | #define CLOCKS_PER_SEC 100 |
---|
221 | #endif |
---|
222 | |
---|
223 | static clock_t begin, end; |
---|
224 | static void |
---|
225 | startTimer(void) |
---|
226 | { |
---|
227 | begin = clock(); |
---|
228 | } |
---|
229 | static void |
---|
230 | endTimer(const char *fmt, ...) |
---|
231 | { |
---|
232 | long msec; |
---|
233 | va_list ap; |
---|
234 | |
---|
235 | end = clock(); |
---|
236 | msec = ((end - begin) * 1000) / CLOCKS_PER_SEC; |
---|
237 | |
---|
238 | #ifndef HAVE_STDARG_H |
---|
239 | #error "endTimer required stdarg functions" |
---|
240 | #endif |
---|
241 | va_start(ap, fmt); |
---|
242 | vfprintf(stderr, fmt, ap); |
---|
243 | va_end(ap); |
---|
244 | fprintf(stderr, " took %ld ms\n", msec); |
---|
245 | } |
---|
246 | #else |
---|
247 | |
---|
248 | /* |
---|
249 | * We don't have a gettimeofday or time.h, so we just don't do timing |
---|
250 | */ |
---|
251 | static void |
---|
252 | startTimer(void) |
---|
253 | { |
---|
254 | /* |
---|
255 | * Do nothing |
---|
256 | */ |
---|
257 | } |
---|
258 | static void |
---|
259 | endTimer(char *format, ...) |
---|
260 | { |
---|
261 | /* |
---|
262 | * We cannot do anything because we don't have a timing function |
---|
263 | */ |
---|
264 | #ifdef HAVE_STDARG_H |
---|
265 | va_start(ap, format); |
---|
266 | vfprintf(stderr, format, ap); |
---|
267 | va_end(ap); |
---|
268 | fprintf(stderr, " was not timed\n", msec); |
---|
269 | #else |
---|
270 | /* We don't have gettimeofday, time or stdarg.h, what crazy world is |
---|
271 | * this ?! |
---|
272 | */ |
---|
273 | #endif |
---|
274 | } |
---|
275 | #endif |
---|
276 | /************************************************************************ |
---|
277 | * * |
---|
278 | * HTML ouput * |
---|
279 | * * |
---|
280 | ************************************************************************/ |
---|
281 | char buffer[50000]; |
---|
282 | |
---|
283 | static void |
---|
284 | xmlHTMLEncodeSend(void) { |
---|
285 | char *result; |
---|
286 | |
---|
287 | result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer); |
---|
288 | if (result) { |
---|
289 | xmlGenericError(xmlGenericErrorContext, "%s", result); |
---|
290 | xmlFree(result); |
---|
291 | } |
---|
292 | buffer[0] = 0; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * xmlHTMLPrintFileInfo: |
---|
297 | * @input: an xmlParserInputPtr input |
---|
298 | * |
---|
299 | * Displays the associated file and line informations for the current input |
---|
300 | */ |
---|
301 | |
---|
302 | static void |
---|
303 | xmlHTMLPrintFileInfo(xmlParserInputPtr input) { |
---|
304 | int len; |
---|
305 | xmlGenericError(xmlGenericErrorContext, "<p>"); |
---|
306 | |
---|
307 | len = strlen(buffer); |
---|
308 | if (input != NULL) { |
---|
309 | if (input->filename) { |
---|
310 | snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename, |
---|
311 | input->line); |
---|
312 | } else { |
---|
313 | snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line); |
---|
314 | } |
---|
315 | } |
---|
316 | xmlHTMLEncodeSend(); |
---|
317 | } |
---|
318 | |
---|
319 | /** |
---|
320 | * xmlHTMLPrintFileContext: |
---|
321 | * @input: an xmlParserInputPtr input |
---|
322 | * |
---|
323 | * Displays current context within the input content for error tracking |
---|
324 | */ |
---|
325 | |
---|
326 | static void |
---|
327 | xmlHTMLPrintFileContext(xmlParserInputPtr input) { |
---|
328 | const xmlChar *cur, *base; |
---|
329 | int len; |
---|
330 | int n; |
---|
331 | |
---|
332 | if (input == NULL) return; |
---|
333 | xmlGenericError(xmlGenericErrorContext, "<pre>\n"); |
---|
334 | cur = input->cur; |
---|
335 | base = input->base; |
---|
336 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
---|
337 | cur--; |
---|
338 | } |
---|
339 | n = 0; |
---|
340 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
---|
341 | cur--; |
---|
342 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
---|
343 | base = cur; |
---|
344 | n = 0; |
---|
345 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
---|
346 | len = strlen(buffer); |
---|
347 | snprintf(&buffer[len], sizeof(buffer) - len, "%c", |
---|
348 | (unsigned char) *cur++); |
---|
349 | n++; |
---|
350 | } |
---|
351 | len = strlen(buffer); |
---|
352 | snprintf(&buffer[len], sizeof(buffer) - len, "\n"); |
---|
353 | cur = input->cur; |
---|
354 | while ((*cur == '\n') || (*cur == '\r')) |
---|
355 | cur--; |
---|
356 | n = 0; |
---|
357 | while ((cur != base) && (n++ < 80)) { |
---|
358 | len = strlen(buffer); |
---|
359 | snprintf(&buffer[len], sizeof(buffer) - len, " "); |
---|
360 | base++; |
---|
361 | } |
---|
362 | len = strlen(buffer); |
---|
363 | snprintf(&buffer[len], sizeof(buffer) - len, "^\n"); |
---|
364 | xmlHTMLEncodeSend(); |
---|
365 | xmlGenericError(xmlGenericErrorContext, "</pre>"); |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * xmlHTMLError: |
---|
370 | * @ctx: an XML parser context |
---|
371 | * @msg: the message to display/transmit |
---|
372 | * @...: extra parameters for the message display |
---|
373 | * |
---|
374 | * Display and format an error messages, gives file, line, position and |
---|
375 | * extra parameters. |
---|
376 | */ |
---|
377 | static void |
---|
378 | xmlHTMLError(void *ctx, const char *msg, ...) |
---|
379 | { |
---|
380 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
---|
381 | xmlParserInputPtr input; |
---|
382 | xmlParserInputPtr cur = NULL; |
---|
383 | va_list args; |
---|
384 | int len; |
---|
385 | |
---|
386 | buffer[0] = 0; |
---|
387 | input = ctxt->input; |
---|
388 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
---|
389 | cur = input; |
---|
390 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
---|
391 | } |
---|
392 | |
---|
393 | xmlHTMLPrintFileInfo(input); |
---|
394 | |
---|
395 | xmlGenericError(xmlGenericErrorContext, "<b>error</b>: "); |
---|
396 | va_start(args, msg); |
---|
397 | len = strlen(buffer); |
---|
398 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
---|
399 | va_end(args); |
---|
400 | xmlHTMLEncodeSend(); |
---|
401 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
---|
402 | |
---|
403 | xmlHTMLPrintFileContext(input); |
---|
404 | xmlHTMLEncodeSend(); |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | * xmlHTMLWarning: |
---|
409 | * @ctx: an XML parser context |
---|
410 | * @msg: the message to display/transmit |
---|
411 | * @...: extra parameters for the message display |
---|
412 | * |
---|
413 | * Display and format a warning messages, gives file, line, position and |
---|
414 | * extra parameters. |
---|
415 | */ |
---|
416 | static void |
---|
417 | xmlHTMLWarning(void *ctx, const char *msg, ...) |
---|
418 | { |
---|
419 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
---|
420 | xmlParserInputPtr input; |
---|
421 | xmlParserInputPtr cur = NULL; |
---|
422 | va_list args; |
---|
423 | int len; |
---|
424 | |
---|
425 | buffer[0] = 0; |
---|
426 | input = ctxt->input; |
---|
427 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
---|
428 | cur = input; |
---|
429 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
---|
430 | } |
---|
431 | |
---|
432 | |
---|
433 | xmlHTMLPrintFileInfo(input); |
---|
434 | |
---|
435 | xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: "); |
---|
436 | va_start(args, msg); |
---|
437 | len = strlen(buffer); |
---|
438 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
---|
439 | va_end(args); |
---|
440 | xmlHTMLEncodeSend(); |
---|
441 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
---|
442 | |
---|
443 | xmlHTMLPrintFileContext(input); |
---|
444 | xmlHTMLEncodeSend(); |
---|
445 | } |
---|
446 | |
---|
447 | /** |
---|
448 | * xmlHTMLValidityError: |
---|
449 | * @ctx: an XML parser context |
---|
450 | * @msg: the message to display/transmit |
---|
451 | * @...: extra parameters for the message display |
---|
452 | * |
---|
453 | * Display and format an validity error messages, gives file, |
---|
454 | * line, position and extra parameters. |
---|
455 | */ |
---|
456 | static void |
---|
457 | xmlHTMLValidityError(void *ctx, const char *msg, ...) |
---|
458 | { |
---|
459 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
---|
460 | xmlParserInputPtr input; |
---|
461 | va_list args; |
---|
462 | int len; |
---|
463 | |
---|
464 | buffer[0] = 0; |
---|
465 | input = ctxt->input; |
---|
466 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
---|
467 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
---|
468 | |
---|
469 | xmlHTMLPrintFileInfo(input); |
---|
470 | |
---|
471 | xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: "); |
---|
472 | len = strlen(buffer); |
---|
473 | va_start(args, msg); |
---|
474 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
---|
475 | va_end(args); |
---|
476 | xmlHTMLEncodeSend(); |
---|
477 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
---|
478 | |
---|
479 | xmlHTMLPrintFileContext(input); |
---|
480 | xmlHTMLEncodeSend(); |
---|
481 | } |
---|
482 | |
---|
483 | /** |
---|
484 | * xmlHTMLValidityWarning: |
---|
485 | * @ctx: an XML parser context |
---|
486 | * @msg: the message to display/transmit |
---|
487 | * @...: extra parameters for the message display |
---|
488 | * |
---|
489 | * Display and format a validity warning messages, gives file, line, |
---|
490 | * position and extra parameters. |
---|
491 | */ |
---|
492 | static void |
---|
493 | xmlHTMLValidityWarning(void *ctx, const char *msg, ...) |
---|
494 | { |
---|
495 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
---|
496 | xmlParserInputPtr input; |
---|
497 | va_list args; |
---|
498 | int len; |
---|
499 | |
---|
500 | buffer[0] = 0; |
---|
501 | input = ctxt->input; |
---|
502 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
---|
503 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
---|
504 | |
---|
505 | xmlHTMLPrintFileInfo(input); |
---|
506 | |
---|
507 | xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: "); |
---|
508 | va_start(args, msg); |
---|
509 | len = strlen(buffer); |
---|
510 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
---|
511 | va_end(args); |
---|
512 | xmlHTMLEncodeSend(); |
---|
513 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
---|
514 | |
---|
515 | xmlHTMLPrintFileContext(input); |
---|
516 | xmlHTMLEncodeSend(); |
---|
517 | } |
---|
518 | |
---|
519 | /************************************************************************ |
---|
520 | * * |
---|
521 | * Shell Interface * |
---|
522 | * * |
---|
523 | ************************************************************************/ |
---|
524 | #ifdef LIBXML_DEBUG_ENABLED |
---|
525 | /** |
---|
526 | * xmlShellReadline: |
---|
527 | * @prompt: the prompt value |
---|
528 | * |
---|
529 | * Read a string |
---|
530 | * |
---|
531 | * Returns a pointer to it or NULL on EOF the caller is expected to |
---|
532 | * free the returned string. |
---|
533 | */ |
---|
534 | static char * |
---|
535 | xmlShellReadline(char *prompt) { |
---|
536 | #ifdef HAVE_LIBREADLINE |
---|
537 | char *line_read; |
---|
538 | |
---|
539 | /* Get a line from the user. */ |
---|
540 | line_read = readline (prompt); |
---|
541 | |
---|
542 | /* If the line has any text in it, save it on the history. */ |
---|
543 | if (line_read && *line_read) |
---|
544 | add_history (line_read); |
---|
545 | |
---|
546 | return (line_read); |
---|
547 | #else |
---|
548 | char line_read[501]; |
---|
549 | char *ret; |
---|
550 | int len; |
---|
551 | |
---|
552 | if (prompt != NULL) |
---|
553 | fprintf(stdout, "%s", prompt); |
---|
554 | if (!fgets(line_read, 500, stdin)) |
---|
555 | return(NULL); |
---|
556 | line_read[500] = 0; |
---|
557 | len = strlen(line_read); |
---|
558 | ret = (char *) malloc(len + 1); |
---|
559 | if (ret != NULL) { |
---|
560 | memcpy (ret, line_read, len + 1); |
---|
561 | } |
---|
562 | return(ret); |
---|
563 | #endif |
---|
564 | } |
---|
565 | #endif /* LIBXML_DEBUG_ENABLED */ |
---|
566 | |
---|
567 | /************************************************************************ |
---|
568 | * * |
---|
569 | * I/O Interfaces * |
---|
570 | * * |
---|
571 | ************************************************************************/ |
---|
572 | |
---|
573 | static int myRead(FILE *f, char * buf, int len) { |
---|
574 | return(fread(buf, 1, len, f)); |
---|
575 | } |
---|
576 | static void myClose(FILE *f) { |
---|
577 | if (f != stdin) { |
---|
578 | fclose(f); |
---|
579 | } |
---|
580 | } |
---|
581 | |
---|
582 | /************************************************************************ |
---|
583 | * * |
---|
584 | * Stream Test processing * |
---|
585 | * * |
---|
586 | ************************************************************************/ |
---|
587 | static int count = 0; |
---|
588 | static int elem, attrs; |
---|
589 | |
---|
590 | static void processNode(xmlTextReaderPtr reader) { |
---|
591 | xmlChar *name, *value; |
---|
592 | |
---|
593 | name = xmlTextReaderName(reader); |
---|
594 | if (name == NULL) |
---|
595 | name = xmlStrdup(BAD_CAST "--"); |
---|
596 | value = xmlTextReaderValue(reader); |
---|
597 | |
---|
598 | printf("%d %d %s %d", |
---|
599 | xmlTextReaderDepth(reader), |
---|
600 | xmlTextReaderNodeType(reader), |
---|
601 | name, |
---|
602 | xmlTextReaderIsEmptyElement(reader)); |
---|
603 | xmlFree(name); |
---|
604 | if (value == NULL) |
---|
605 | printf("\n"); |
---|
606 | else { |
---|
607 | printf(" %s\n", value); |
---|
608 | xmlFree(value); |
---|
609 | } |
---|
610 | } |
---|
611 | |
---|
612 | static void streamFile(char *filename) { |
---|
613 | xmlTextReaderPtr reader; |
---|
614 | int ret; |
---|
615 | |
---|
616 | if (count) { |
---|
617 | elem = 0; |
---|
618 | attrs = 0; |
---|
619 | } |
---|
620 | |
---|
621 | reader = xmlNewTextReaderFilename(filename); |
---|
622 | if (reader != NULL) { |
---|
623 | if (valid) |
---|
624 | xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1); |
---|
625 | |
---|
626 | /* |
---|
627 | * Process all nodes in sequence |
---|
628 | */ |
---|
629 | ret = xmlTextReaderRead(reader); |
---|
630 | while (ret == 1) { |
---|
631 | if (debug) |
---|
632 | processNode(reader); |
---|
633 | ret = xmlTextReaderRead(reader); |
---|
634 | } |
---|
635 | |
---|
636 | /* |
---|
637 | * Done, cleanup and status |
---|
638 | */ |
---|
639 | xmlFreeTextReader(reader); |
---|
640 | if (ret != 0) { |
---|
641 | printf("%s : failed to parse\n", filename); |
---|
642 | } |
---|
643 | } else { |
---|
644 | fprintf(stderr, "Unable to open %s\n", filename); |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | /************************************************************************ |
---|
649 | * * |
---|
650 | * Tree Test processing * |
---|
651 | * * |
---|
652 | ************************************************************************/ |
---|
653 | static void parseAndPrintFile(char *filename) { |
---|
654 | xmlDocPtr doc = NULL, tmp; |
---|
655 | |
---|
656 | if ((timing) && (!repeat)) |
---|
657 | startTimer(); |
---|
658 | |
---|
659 | |
---|
660 | if (filename == NULL) { |
---|
661 | if (generate) { |
---|
662 | xmlNodePtr n; |
---|
663 | |
---|
664 | doc = xmlNewDoc(BAD_CAST "1.0"); |
---|
665 | n = xmlNewNode(NULL, BAD_CAST "info"); |
---|
666 | xmlNodeSetContent(n, BAD_CAST "abc"); |
---|
667 | xmlDocSetRootElement(doc, n); |
---|
668 | } |
---|
669 | } |
---|
670 | #ifdef LIBXML_DOCB_ENABLED |
---|
671 | /* |
---|
672 | * build an SGML tree from a string; |
---|
673 | */ |
---|
674 | else if ((sgml) && (push)) { |
---|
675 | FILE *f; |
---|
676 | |
---|
677 | f = fopen(filename, "r"); |
---|
678 | if (f != NULL) { |
---|
679 | int res, size = 3; |
---|
680 | char chars[4096]; |
---|
681 | docbParserCtxtPtr ctxt; |
---|
682 | |
---|
683 | /* if (repeat) */ |
---|
684 | size = 4096; |
---|
685 | res = fread(chars, 1, 4, f); |
---|
686 | if (res > 0) { |
---|
687 | ctxt = docbCreatePushParserCtxt(NULL, NULL, |
---|
688 | chars, res, filename, 0); |
---|
689 | while ((res = fread(chars, 1, size, f)) > 0) { |
---|
690 | docbParseChunk(ctxt, chars, res, 0); |
---|
691 | } |
---|
692 | docbParseChunk(ctxt, chars, 0, 1); |
---|
693 | doc = ctxt->myDoc; |
---|
694 | docbFreeParserCtxt(ctxt); |
---|
695 | } |
---|
696 | fclose(f); |
---|
697 | } |
---|
698 | } else if (sgml) { |
---|
699 | doc = docbParseFile(filename, NULL); |
---|
700 | } |
---|
701 | #endif |
---|
702 | #ifdef LIBXML_HTML_ENABLED |
---|
703 | else if (html) { |
---|
704 | doc = htmlParseFile(filename, NULL); |
---|
705 | } |
---|
706 | #endif /* LIBXML_HTML_ENABLED */ |
---|
707 | else { |
---|
708 | /* |
---|
709 | * build an XML tree from a string; |
---|
710 | */ |
---|
711 | if (push) { |
---|
712 | FILE *f; |
---|
713 | |
---|
714 | /* '-' Usually means stdin -<sven@zen.org> */ |
---|
715 | if ((filename[0] == '-') && (filename[1] == 0)) { |
---|
716 | f = stdin; |
---|
717 | } else { |
---|
718 | f = fopen(filename, "r"); |
---|
719 | } |
---|
720 | if (f != NULL) { |
---|
721 | int ret; |
---|
722 | int res, size = 3; |
---|
723 | char chars[1024]; |
---|
724 | xmlParserCtxtPtr ctxt; |
---|
725 | |
---|
726 | if (repeat) |
---|
727 | size = 1024; |
---|
728 | res = fread(chars, 1, 4, f); |
---|
729 | if (res > 0) { |
---|
730 | ctxt = xmlCreatePushParserCtxt(NULL, NULL, |
---|
731 | chars, res, filename); |
---|
732 | while ((res = fread(chars, 1, size, f)) > 0) { |
---|
733 | xmlParseChunk(ctxt, chars, res, 0); |
---|
734 | } |
---|
735 | xmlParseChunk(ctxt, chars, 0, 1); |
---|
736 | doc = ctxt->myDoc; |
---|
737 | ret = ctxt->wellFormed; |
---|
738 | xmlFreeParserCtxt(ctxt); |
---|
739 | if (!ret) { |
---|
740 | xmlFreeDoc(doc); |
---|
741 | doc = NULL; |
---|
742 | } |
---|
743 | } |
---|
744 | } |
---|
745 | } else if (testIO) { |
---|
746 | int ret; |
---|
747 | FILE *f; |
---|
748 | |
---|
749 | /* '-' Usually means stdin -<sven@zen.org> */ |
---|
750 | if ((filename[0] == '-') && (filename[1] == 0)) { |
---|
751 | f = stdin; |
---|
752 | } else { |
---|
753 | f = fopen(filename, "r"); |
---|
754 | } |
---|
755 | if (f != NULL) { |
---|
756 | xmlParserCtxtPtr ctxt; |
---|
757 | |
---|
758 | ctxt = xmlCreateIOParserCtxt(NULL, NULL, |
---|
759 | (xmlInputReadCallback) myRead, |
---|
760 | (xmlInputCloseCallback) myClose, |
---|
761 | f, XML_CHAR_ENCODING_NONE); |
---|
762 | xmlParseDocument(ctxt); |
---|
763 | |
---|
764 | ret = ctxt->wellFormed; |
---|
765 | doc = ctxt->myDoc; |
---|
766 | xmlFreeParserCtxt(ctxt); |
---|
767 | if (!ret) { |
---|
768 | xmlFreeDoc(doc); |
---|
769 | doc = NULL; |
---|
770 | } |
---|
771 | } |
---|
772 | } else if (recovery) { |
---|
773 | doc = xmlRecoverFile(filename); |
---|
774 | } else if (htmlout) { |
---|
775 | int ret; |
---|
776 | xmlParserCtxtPtr ctxt; |
---|
777 | xmlSAXHandler silent, *old; |
---|
778 | |
---|
779 | ctxt = xmlCreateFileParserCtxt(filename); |
---|
780 | |
---|
781 | if (ctxt == NULL) { |
---|
782 | /* If xmlCreateFileParserCtxt() return NULL something |
---|
783 | strange happened so we don't want to do anything. Do |
---|
784 | we want to print an error message here? |
---|
785 | <sven@zen.org> */ |
---|
786 | doc = NULL; |
---|
787 | } else { |
---|
788 | memcpy(&silent, ctxt->sax, sizeof(silent)); |
---|
789 | old = ctxt->sax; |
---|
790 | silent.error = xmlHTMLError; |
---|
791 | if (xmlGetWarningsDefaultValue) |
---|
792 | silent.warning = xmlHTMLWarning; |
---|
793 | else |
---|
794 | silent.warning = NULL; |
---|
795 | silent.fatalError = xmlHTMLError; |
---|
796 | ctxt->sax = &silent; |
---|
797 | ctxt->vctxt.error = xmlHTMLValidityError; |
---|
798 | if (xmlGetWarningsDefaultValue) |
---|
799 | ctxt->vctxt.warning = xmlHTMLValidityWarning; |
---|
800 | else |
---|
801 | ctxt->vctxt.warning = NULL; |
---|
802 | |
---|
803 | xmlParseDocument(ctxt); |
---|
804 | |
---|
805 | ret = ctxt->wellFormed; |
---|
806 | doc = ctxt->myDoc; |
---|
807 | ctxt->sax = old; |
---|
808 | xmlFreeParserCtxt(ctxt); |
---|
809 | if (!ret) { |
---|
810 | xmlFreeDoc(doc); |
---|
811 | doc = NULL; |
---|
812 | } |
---|
813 | } |
---|
814 | #ifdef HAVE_SYS_MMAN_H |
---|
815 | } else if (memory) { |
---|
816 | int fd; |
---|
817 | struct stat info; |
---|
818 | const char *base; |
---|
819 | if (stat(filename, &info) < 0) |
---|
820 | return; |
---|
821 | if ((fd = open(filename, O_RDONLY)) < 0) |
---|
822 | return; |
---|
823 | base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ; |
---|
824 | if (base == (void *) MAP_FAILED) |
---|
825 | return; |
---|
826 | |
---|
827 | doc = xmlParseMemory((char *) base, info.st_size); |
---|
828 | munmap((char *) base, info.st_size); |
---|
829 | #endif |
---|
830 | } else if (valid) { |
---|
831 | int ret; |
---|
832 | xmlParserCtxtPtr ctxt; |
---|
833 | |
---|
834 | ctxt = xmlCreateFileParserCtxt(filename); |
---|
835 | |
---|
836 | if (ctxt == NULL) { |
---|
837 | doc = NULL; |
---|
838 | } else { |
---|
839 | xmlParseDocument(ctxt); |
---|
840 | if (ctxt->valid == 0) |
---|
841 | progresult = 4; |
---|
842 | ret = ctxt->wellFormed; |
---|
843 | doc = ctxt->myDoc; |
---|
844 | xmlFreeParserCtxt(ctxt); |
---|
845 | if (!ret) { |
---|
846 | xmlFreeDoc(doc); |
---|
847 | doc = NULL; |
---|
848 | } |
---|
849 | } |
---|
850 | } else { |
---|
851 | doc = xmlParseFile(filename); |
---|
852 | } |
---|
853 | } |
---|
854 | |
---|
855 | /* |
---|
856 | * If we don't have a document we might as well give up. Do we |
---|
857 | * want an error message here? <sven@zen.org> */ |
---|
858 | if (doc == NULL) { |
---|
859 | progresult = 1; |
---|
860 | return; |
---|
861 | } |
---|
862 | |
---|
863 | if ((timing) && (!repeat)) { |
---|
864 | endTimer("Parsing"); |
---|
865 | } |
---|
866 | |
---|
867 | /* |
---|
868 | * Remove DOCTYPE nodes |
---|
869 | */ |
---|
870 | if (dropdtd) { |
---|
871 | xmlDtdPtr dtd; |
---|
872 | |
---|
873 | dtd = xmlGetIntSubset(doc); |
---|
874 | if (dtd != NULL) { |
---|
875 | xmlUnlinkNode((xmlNodePtr)dtd); |
---|
876 | xmlFreeDtd(dtd); |
---|
877 | } |
---|
878 | } |
---|
879 | |
---|
880 | #ifdef LIBXML_XINCLUDE_ENABLED |
---|
881 | if (xinclude) { |
---|
882 | if ((timing) && (!repeat)) { |
---|
883 | startTimer(); |
---|
884 | } |
---|
885 | xmlXIncludeProcess(doc); |
---|
886 | if ((timing) && (!repeat)) { |
---|
887 | endTimer("Xinclude processing"); |
---|
888 | } |
---|
889 | } |
---|
890 | #endif |
---|
891 | |
---|
892 | #ifdef LIBXML_DEBUG_ENABLED |
---|
893 | /* |
---|
894 | * shell interaction |
---|
895 | */ |
---|
896 | if (shell) |
---|
897 | xmlShell(doc, filename, xmlShellReadline, stdout); |
---|
898 | #endif |
---|
899 | |
---|
900 | /* |
---|
901 | * test intermediate copy if needed. |
---|
902 | */ |
---|
903 | if (copy) { |
---|
904 | tmp = doc; |
---|
905 | doc = xmlCopyDoc(doc, 1); |
---|
906 | xmlFreeDoc(tmp); |
---|
907 | } |
---|
908 | |
---|
909 | if ((insert) && (!html)) { |
---|
910 | const xmlChar* list[256]; |
---|
911 | int nb, i; |
---|
912 | xmlNodePtr node; |
---|
913 | |
---|
914 | if (doc->children != NULL) { |
---|
915 | node = doc->children; |
---|
916 | while ((node != NULL) && (node->last == NULL)) node = node->next; |
---|
917 | if (node != NULL) { |
---|
918 | nb = xmlValidGetValidElements(node->last, NULL, list, 256); |
---|
919 | if (nb < 0) { |
---|
920 | printf("could not get valid list of elements\n"); |
---|
921 | } else if (nb == 0) { |
---|
922 | printf("No element can be inserted under root\n"); |
---|
923 | } else { |
---|
924 | printf("%d element types can be inserted under root:\n", |
---|
925 | nb); |
---|
926 | for (i = 0;i < nb;i++) { |
---|
927 | printf("%s\n", (char *) list[i]); |
---|
928 | } |
---|
929 | } |
---|
930 | } |
---|
931 | } |
---|
932 | }else if (noout == 0) { |
---|
933 | /* |
---|
934 | * print it. |
---|
935 | */ |
---|
936 | #ifdef LIBXML_DEBUG_ENABLED |
---|
937 | if (!debug) { |
---|
938 | #endif |
---|
939 | if ((timing) && (!repeat)) { |
---|
940 | startTimer(); |
---|
941 | } |
---|
942 | #ifdef HAVE_SYS_MMAN_H |
---|
943 | if (memory) { |
---|
944 | xmlChar *result; |
---|
945 | int len; |
---|
946 | |
---|
947 | if (encoding != NULL) { |
---|
948 | if ( format ) { |
---|
949 | xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1); |
---|
950 | } else { |
---|
951 | xmlDocDumpMemoryEnc(doc, &result, &len, encoding); |
---|
952 | } |
---|
953 | } else { |
---|
954 | if (format) |
---|
955 | xmlDocDumpFormatMemory(doc, &result, &len, 1); |
---|
956 | else |
---|
957 | xmlDocDumpMemory(doc, &result, &len); |
---|
958 | } |
---|
959 | if (result == NULL) { |
---|
960 | fprintf(stderr, "Failed to save\n"); |
---|
961 | } else { |
---|
962 | write(1, result, len); |
---|
963 | xmlFree(result); |
---|
964 | } |
---|
965 | } else |
---|
966 | #endif /* HAVE_SYS_MMAN_H */ |
---|
967 | if (compress) { |
---|
968 | xmlSaveFile(output ? output : "-", doc); |
---|
969 | } |
---|
970 | else if (encoding != NULL) { |
---|
971 | if ( format ) { |
---|
972 | xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1); |
---|
973 | } |
---|
974 | else { |
---|
975 | xmlSaveFileEnc(output ? output : "-", doc, encoding); |
---|
976 | } |
---|
977 | } |
---|
978 | else if (format) { |
---|
979 | xmlSaveFormatFile(output ? output : "-", doc, 1); |
---|
980 | } |
---|
981 | else { |
---|
982 | FILE *out; |
---|
983 | if (output == NULL) |
---|
984 | out = stdout; |
---|
985 | else { |
---|
986 | out = fopen(output,"wb"); |
---|
987 | } |
---|
988 | xmlDocDump(out, doc); |
---|
989 | |
---|
990 | if (output) |
---|
991 | fclose(out); |
---|
992 | } |
---|
993 | if ((timing) && (!repeat)) { |
---|
994 | endTimer("Saving"); |
---|
995 | } |
---|
996 | #ifdef LIBXML_DEBUG_ENABLED |
---|
997 | } else { |
---|
998 | FILE *out; |
---|
999 | if (output == NULL) |
---|
1000 | out = stdout; |
---|
1001 | else { |
---|
1002 | out = fopen(output,"wb"); |
---|
1003 | } |
---|
1004 | xmlDebugDumpDocument(out, doc); |
---|
1005 | |
---|
1006 | if (output) |
---|
1007 | fclose(out); |
---|
1008 | } |
---|
1009 | #endif |
---|
1010 | } |
---|
1011 | |
---|
1012 | /* |
---|
1013 | * A posteriori validation test |
---|
1014 | */ |
---|
1015 | if (dtdvalid != NULL) { |
---|
1016 | xmlDtdPtr dtd; |
---|
1017 | |
---|
1018 | if ((timing) && (!repeat)) { |
---|
1019 | startTimer(); |
---|
1020 | } |
---|
1021 | dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid); |
---|
1022 | if ((timing) && (!repeat)) { |
---|
1023 | endTimer("Parsing DTD"); |
---|
1024 | } |
---|
1025 | if (dtd == NULL) { |
---|
1026 | xmlGenericError(xmlGenericErrorContext, |
---|
1027 | "Could not parse DTD %s\n", dtdvalid); |
---|
1028 | progresult = 2; |
---|
1029 | } else { |
---|
1030 | xmlValidCtxt cvp; |
---|
1031 | if ((timing) && (!repeat)) { |
---|
1032 | startTimer(); |
---|
1033 | } |
---|
1034 | cvp.userData = (void *) stderr; |
---|
1035 | cvp.error = (xmlValidityErrorFunc) fprintf; |
---|
1036 | cvp.warning = (xmlValidityWarningFunc) fprintf; |
---|
1037 | if (!xmlValidateDtd(&cvp, doc, dtd)) { |
---|
1038 | xmlGenericError(xmlGenericErrorContext, |
---|
1039 | "Document %s does not validate against %s\n", |
---|
1040 | filename, dtdvalid); |
---|
1041 | progresult = 3; |
---|
1042 | } |
---|
1043 | if ((timing) && (!repeat)) { |
---|
1044 | endTimer("Validating against DTD"); |
---|
1045 | } |
---|
1046 | xmlFreeDtd(dtd); |
---|
1047 | } |
---|
1048 | } else if (postvalid) { |
---|
1049 | xmlValidCtxt cvp; |
---|
1050 | if ((timing) && (!repeat)) { |
---|
1051 | startTimer(); |
---|
1052 | } |
---|
1053 | cvp.userData = (void *) stderr; |
---|
1054 | cvp.error = (xmlValidityErrorFunc) fprintf; |
---|
1055 | cvp.warning = (xmlValidityWarningFunc) fprintf; |
---|
1056 | if (!xmlValidateDocument(&cvp, doc)) { |
---|
1057 | xmlGenericError(xmlGenericErrorContext, |
---|
1058 | "Document %s does not validate\n", filename); |
---|
1059 | progresult = 3; |
---|
1060 | } |
---|
1061 | if ((timing) && (!repeat)) { |
---|
1062 | endTimer("Validating"); |
---|
1063 | } |
---|
1064 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
1065 | } else if (relaxngschemas != NULL) { |
---|
1066 | xmlRelaxNGValidCtxtPtr ctxt; |
---|
1067 | int ret; |
---|
1068 | |
---|
1069 | ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas); |
---|
1070 | xmlRelaxNGSetValidErrors(ctxt, |
---|
1071 | (xmlRelaxNGValidityErrorFunc) fprintf, |
---|
1072 | (xmlRelaxNGValidityWarningFunc) fprintf, |
---|
1073 | stderr); |
---|
1074 | ret = xmlRelaxNGValidateDoc(ctxt, doc); |
---|
1075 | if (ret == 0) { |
---|
1076 | printf("%s validates\n", filename); |
---|
1077 | } else if (ret > 0) { |
---|
1078 | printf("%s fails to validate\n", filename); |
---|
1079 | } else { |
---|
1080 | printf("%s validation generated an internal error\n", |
---|
1081 | filename); |
---|
1082 | } |
---|
1083 | xmlRelaxNGFreeValidCtxt(ctxt); |
---|
1084 | #endif |
---|
1085 | } |
---|
1086 | |
---|
1087 | #ifdef LIBXML_DEBUG_ENABLED |
---|
1088 | if ((debugent) && (!html)) |
---|
1089 | xmlDebugDumpEntities(stderr, doc); |
---|
1090 | #endif |
---|
1091 | |
---|
1092 | /* |
---|
1093 | * free it. |
---|
1094 | */ |
---|
1095 | if ((timing) && (!repeat)) { |
---|
1096 | startTimer(); |
---|
1097 | } |
---|
1098 | xmlFreeDoc(doc); |
---|
1099 | if ((timing) && (!repeat)) { |
---|
1100 | endTimer("Freeing"); |
---|
1101 | } |
---|
1102 | } |
---|
1103 | |
---|
1104 | /************************************************************************ |
---|
1105 | * * |
---|
1106 | * Usage and Main * |
---|
1107 | * * |
---|
1108 | ************************************************************************/ |
---|
1109 | |
---|
1110 | static void showVersion(const char *name) { |
---|
1111 | fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion); |
---|
1112 | fprintf(stderr, " compiled with: "); |
---|
1113 | #ifdef LIBXML_FTP_ENABLED |
---|
1114 | fprintf(stderr, "FTP "); |
---|
1115 | #endif |
---|
1116 | #ifdef LIBXML_HTTP_ENABLED |
---|
1117 | fprintf(stderr, "HTTP "); |
---|
1118 | #endif |
---|
1119 | #ifdef LIBXML_HTML_ENABLED |
---|
1120 | fprintf(stderr, "HTML "); |
---|
1121 | #endif |
---|
1122 | #ifdef LIBXML_C14N_ENABLED |
---|
1123 | fprintf(stderr, "C14N "); |
---|
1124 | #endif |
---|
1125 | #ifdef LIBXML_CATALOG_ENABLED |
---|
1126 | fprintf(stderr, "Catalog "); |
---|
1127 | #endif |
---|
1128 | #ifdef LIBXML_DOCB_ENABLED |
---|
1129 | fprintf(stderr, "DocBook "); |
---|
1130 | #endif |
---|
1131 | #ifdef LIBXML_XPATH_ENABLED |
---|
1132 | fprintf(stderr, "XPath "); |
---|
1133 | #endif |
---|
1134 | #ifdef LIBXML_XPTR_ENABLED |
---|
1135 | fprintf(stderr, "XPointer "); |
---|
1136 | #endif |
---|
1137 | #ifdef LIBXML_XINCLUDE_ENABLED |
---|
1138 | fprintf(stderr, "XInclude "); |
---|
1139 | #endif |
---|
1140 | #ifdef LIBXML_ICONV_ENABLED |
---|
1141 | fprintf(stderr, "Iconv "); |
---|
1142 | #endif |
---|
1143 | #ifdef DEBUG_MEMORY_LOCATION |
---|
1144 | fprintf(stderr, "MemDebug "); |
---|
1145 | #endif |
---|
1146 | #ifdef LIBXML_UNICODE_ENABLED |
---|
1147 | fprintf(stderr, "Unicode "); |
---|
1148 | #endif |
---|
1149 | #ifdef LIBXML_REGEXP_ENABLED |
---|
1150 | fprintf(stderr, "Regexps "); |
---|
1151 | #endif |
---|
1152 | #ifdef LIBXML_AUTOMATA_ENABLED |
---|
1153 | fprintf(stderr, "Automata "); |
---|
1154 | #endif |
---|
1155 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
1156 | fprintf(stderr, "Schemas "); |
---|
1157 | #endif |
---|
1158 | fprintf(stderr, "\n"); |
---|
1159 | } |
---|
1160 | |
---|
1161 | static void usage(const char *name) { |
---|
1162 | printf("Usage : %s [options] XMLfiles ...\n", name); |
---|
1163 | printf("\tParse the XML files and output the result of the parsing\n"); |
---|
1164 | printf("\t--version : display the version of the XML library used\n"); |
---|
1165 | #ifdef LIBXML_DEBUG_ENABLED |
---|
1166 | printf("\t--debug : dump a debug tree of the in-memory document\n"); |
---|
1167 | printf("\t--shell : run a navigating shell\n"); |
---|
1168 | printf("\t--debugent : debug the entities defined in the document\n"); |
---|
1169 | #else |
---|
1170 | printf("\t--debug : dump the nodes content when using --stream\n"); |
---|
1171 | #endif |
---|
1172 | printf("\t--copy : used to test the internal copy implementation\n"); |
---|
1173 | printf("\t--recover : output what was parsable on broken XML documents\n"); |
---|
1174 | printf("\t--noent : substitute entity references by their value\n"); |
---|
1175 | printf("\t--noout : don't output the result tree\n"); |
---|
1176 | printf("\t--htmlout : output results as HTML\n"); |
---|
1177 | printf("\t--nowrap : do not put HTML doc wrapper\n"); |
---|
1178 | printf("\t--valid : validate the document in addition to std well-formed check\n"); |
---|
1179 | printf("\t--postvalid : do a posteriori validation, i.e after parsing\n"); |
---|
1180 | printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n"); |
---|
1181 | printf("\t--timing : print some timings\n"); |
---|
1182 | printf("\t--output file or -o file: save to a given file\n"); |
---|
1183 | printf("\t--repeat : repeat 100 times, for timing or profiling\n"); |
---|
1184 | printf("\t--insert : ad-hoc test for valid insertions\n"); |
---|
1185 | #ifdef HAVE_ZLIB_H |
---|
1186 | printf("\t--compress : turn on gzip compression of output\n"); |
---|
1187 | #endif |
---|
1188 | #ifdef LIBXML_DOCB_ENABLED |
---|
1189 | printf("\t--sgml : use the DocBook SGML parser\n"); |
---|
1190 | #endif |
---|
1191 | #ifdef LIBXML_HTML_ENABLED |
---|
1192 | printf("\t--html : use the HTML parser\n"); |
---|
1193 | #endif |
---|
1194 | printf("\t--push : use the push mode of the parser\n"); |
---|
1195 | #ifdef HAVE_SYS_MMAN_H |
---|
1196 | printf("\t--memory : parse from memory\n"); |
---|
1197 | #endif |
---|
1198 | printf("\t--nowarning : do not emit warnings from parser/validator\n"); |
---|
1199 | printf("\t--noblanks : drop (ignorable?) blanks spaces\n"); |
---|
1200 | printf("\t--format : reformat/reindent the input\n"); |
---|
1201 | printf("\t--testIO : test user I/O support\n"); |
---|
1202 | printf("\t--encode encoding : output in the given encoding\n"); |
---|
1203 | #ifdef LIBXML_CATALOG_ENABLED |
---|
1204 | printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n"); |
---|
1205 | printf("\t otherwise XML Catalogs starting from \n"); |
---|
1206 | printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n"); |
---|
1207 | printf("\t--nocatalogs: deactivate all catalogs\n"); |
---|
1208 | #endif |
---|
1209 | printf("\t--auto : generate a small doc on the fly\n"); |
---|
1210 | #ifdef LIBXML_XINCLUDE_ENABLED |
---|
1211 | printf("\t--xinclude : do XInclude processing\n"); |
---|
1212 | #endif |
---|
1213 | printf("\t--loaddtd : fetch external DTD\n"); |
---|
1214 | printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n"); |
---|
1215 | printf("\t--dropdtd : remove the DOCTYPE of the input docs\n"); |
---|
1216 | printf("\t--stream : use the streaming interface to process very large files\n"); |
---|
1217 | printf("\t--chkregister : verify the node registration code\n"); |
---|
1218 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
1219 | printf("\t--relaxng schema : do RelaxNG validation against the schema\n"); |
---|
1220 | #endif |
---|
1221 | printf("\nLibxml project home page: http://xmlsoft.org/\n"); |
---|
1222 | printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n"); |
---|
1223 | } |
---|
1224 | |
---|
1225 | static void registerNode(xmlNodePtr node) |
---|
1226 | { |
---|
1227 | node->_private = malloc(sizeof(long)); |
---|
1228 | *(long*)node->_private = (long) 0x81726354; |
---|
1229 | } |
---|
1230 | |
---|
1231 | static void deregisterNode(xmlNodePtr node) |
---|
1232 | { |
---|
1233 | assert(node->_private != NULL); |
---|
1234 | assert(*(long*)node->_private == (long) 0x81726354); |
---|
1235 | free(node->_private); |
---|
1236 | } |
---|
1237 | |
---|
1238 | int |
---|
1239 | main(int argc, char **argv) { |
---|
1240 | int i, acount; |
---|
1241 | int files = 0; |
---|
1242 | int version = 0; |
---|
1243 | |
---|
1244 | if (argc <= 1) { |
---|
1245 | usage(argv[0]); |
---|
1246 | return(1); |
---|
1247 | } |
---|
1248 | LIBXML_TEST_VERSION |
---|
1249 | for (i = 1; i < argc ; i++) { |
---|
1250 | if (!strcmp(argv[i], "-")) |
---|
1251 | break; |
---|
1252 | |
---|
1253 | if (argv[i][0] != '-') |
---|
1254 | continue; |
---|
1255 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
---|
1256 | debug++; |
---|
1257 | else |
---|
1258 | #ifdef LIBXML_DEBUG_ENABLED |
---|
1259 | if ((!strcmp(argv[i], "-shell")) || |
---|
1260 | (!strcmp(argv[i], "--shell"))) { |
---|
1261 | shell++; |
---|
1262 | noout = 1; |
---|
1263 | } else |
---|
1264 | #endif |
---|
1265 | if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
---|
1266 | copy++; |
---|
1267 | else if ((!strcmp(argv[i], "-recover")) || |
---|
1268 | (!strcmp(argv[i], "--recover"))) |
---|
1269 | recovery++; |
---|
1270 | else if ((!strcmp(argv[i], "-noent")) || |
---|
1271 | (!strcmp(argv[i], "--noent"))) |
---|
1272 | noent++; |
---|
1273 | else if ((!strcmp(argv[i], "-version")) || |
---|
1274 | (!strcmp(argv[i], "--version"))) { |
---|
1275 | showVersion(argv[0]); |
---|
1276 | version = 1; |
---|
1277 | } else if ((!strcmp(argv[i], "-noout")) || |
---|
1278 | (!strcmp(argv[i], "--noout"))) |
---|
1279 | noout++; |
---|
1280 | else if ((!strcmp(argv[i], "-o")) || |
---|
1281 | (!strcmp(argv[i], "-output")) || |
---|
1282 | (!strcmp(argv[i], "--output"))) { |
---|
1283 | i++; |
---|
1284 | output = argv[i]; |
---|
1285 | } |
---|
1286 | else if ((!strcmp(argv[i], "-htmlout")) || |
---|
1287 | (!strcmp(argv[i], "--htmlout"))) |
---|
1288 | htmlout++; |
---|
1289 | #ifdef LIBXML_DOCB_ENABLED |
---|
1290 | else if ((!strcmp(argv[i], "-sgml")) || |
---|
1291 | (!strcmp(argv[i], "--sgml"))) { |
---|
1292 | sgml++; |
---|
1293 | } |
---|
1294 | #endif |
---|
1295 | #ifdef LIBXML_HTML_ENABLED |
---|
1296 | else if ((!strcmp(argv[i], "-html")) || |
---|
1297 | (!strcmp(argv[i], "--html"))) { |
---|
1298 | html++; |
---|
1299 | } |
---|
1300 | #endif /* LIBXML_HTML_ENABLED */ |
---|
1301 | else if ((!strcmp(argv[i], "-nowrap")) || |
---|
1302 | (!strcmp(argv[i], "--nowrap"))) |
---|
1303 | nowrap++; |
---|
1304 | else if ((!strcmp(argv[i], "-loaddtd")) || |
---|
1305 | (!strcmp(argv[i], "--loaddtd"))) |
---|
1306 | loaddtd++; |
---|
1307 | else if ((!strcmp(argv[i], "-dtdattr")) || |
---|
1308 | (!strcmp(argv[i], "--dtdattr"))) { |
---|
1309 | loaddtd++; |
---|
1310 | dtdattrs++; |
---|
1311 | } else if ((!strcmp(argv[i], "-valid")) || |
---|
1312 | (!strcmp(argv[i], "--valid"))) |
---|
1313 | valid++; |
---|
1314 | else if ((!strcmp(argv[i], "-postvalid")) || |
---|
1315 | (!strcmp(argv[i], "--postvalid"))) { |
---|
1316 | postvalid++; |
---|
1317 | loaddtd++; |
---|
1318 | } else if ((!strcmp(argv[i], "-dtdvalid")) || |
---|
1319 | (!strcmp(argv[i], "--dtdvalid"))) { |
---|
1320 | i++; |
---|
1321 | dtdvalid = argv[i]; |
---|
1322 | loaddtd++; |
---|
1323 | } |
---|
1324 | else if ((!strcmp(argv[i], "-dropdtd")) || |
---|
1325 | (!strcmp(argv[i], "--dropdtd"))) |
---|
1326 | dropdtd++; |
---|
1327 | else if ((!strcmp(argv[i], "-insert")) || |
---|
1328 | (!strcmp(argv[i], "--insert"))) |
---|
1329 | insert++; |
---|
1330 | else if ((!strcmp(argv[i], "-timing")) || |
---|
1331 | (!strcmp(argv[i], "--timing"))) |
---|
1332 | timing++; |
---|
1333 | else if ((!strcmp(argv[i], "-auto")) || |
---|
1334 | (!strcmp(argv[i], "--auto"))) |
---|
1335 | generate++; |
---|
1336 | else if ((!strcmp(argv[i], "-repeat")) || |
---|
1337 | (!strcmp(argv[i], "--repeat"))) |
---|
1338 | repeat++; |
---|
1339 | else if ((!strcmp(argv[i], "-push")) || |
---|
1340 | (!strcmp(argv[i], "--push"))) |
---|
1341 | push++; |
---|
1342 | #ifdef HAVE_SYS_MMAN_H |
---|
1343 | else if ((!strcmp(argv[i], "-memory")) || |
---|
1344 | (!strcmp(argv[i], "--memory"))) |
---|
1345 | memory++; |
---|
1346 | #endif |
---|
1347 | else if ((!strcmp(argv[i], "-testIO")) || |
---|
1348 | (!strcmp(argv[i], "--testIO"))) |
---|
1349 | testIO++; |
---|
1350 | #ifdef LIBXML_XINCLUDE_ENABLED |
---|
1351 | else if ((!strcmp(argv[i], "-xinclude")) || |
---|
1352 | (!strcmp(argv[i], "--xinclude"))) |
---|
1353 | xinclude++; |
---|
1354 | #endif |
---|
1355 | #ifdef HAVE_ZLIB_H |
---|
1356 | else if ((!strcmp(argv[i], "-compress")) || |
---|
1357 | (!strcmp(argv[i], "--compress"))) { |
---|
1358 | compress++; |
---|
1359 | xmlSetCompressMode(9); |
---|
1360 | } |
---|
1361 | #endif |
---|
1362 | else if ((!strcmp(argv[i], "-nowarning")) || |
---|
1363 | (!strcmp(argv[i], "--nowarning"))) { |
---|
1364 | xmlGetWarningsDefaultValue = 0; |
---|
1365 | xmlPedanticParserDefault(0); |
---|
1366 | } |
---|
1367 | else if ((!strcmp(argv[i], "-pedantic")) || |
---|
1368 | (!strcmp(argv[i], "--pedantic"))) { |
---|
1369 | xmlGetWarningsDefaultValue = 1; |
---|
1370 | xmlPedanticParserDefault(1); |
---|
1371 | } |
---|
1372 | #ifdef LIBXML_DEBUG_ENABLED |
---|
1373 | else if ((!strcmp(argv[i], "-debugent")) || |
---|
1374 | (!strcmp(argv[i], "--debugent"))) { |
---|
1375 | debugent++; |
---|
1376 | xmlParserDebugEntities = 1; |
---|
1377 | } |
---|
1378 | #endif |
---|
1379 | #ifdef LIBXML_CATALOG_ENABLED |
---|
1380 | else if ((!strcmp(argv[i], "-catalogs")) || |
---|
1381 | (!strcmp(argv[i], "--catalogs"))) { |
---|
1382 | catalogs++; |
---|
1383 | } else if ((!strcmp(argv[i], "-nocatalogs")) || |
---|
1384 | (!strcmp(argv[i], "--nocatalogs"))) { |
---|
1385 | nocatalogs++; |
---|
1386 | } |
---|
1387 | #endif |
---|
1388 | else if ((!strcmp(argv[i], "-encode")) || |
---|
1389 | (!strcmp(argv[i], "--encode"))) { |
---|
1390 | i++; |
---|
1391 | encoding = argv[i]; |
---|
1392 | /* |
---|
1393 | * OK it's for testing purposes |
---|
1394 | */ |
---|
1395 | xmlAddEncodingAlias("UTF-8", "DVEnc"); |
---|
1396 | } |
---|
1397 | else if ((!strcmp(argv[i], "-noblanks")) || |
---|
1398 | (!strcmp(argv[i], "--noblanks"))) { |
---|
1399 | noblanks++; |
---|
1400 | xmlKeepBlanksDefault(0); |
---|
1401 | } |
---|
1402 | else if ((!strcmp(argv[i], "-format")) || |
---|
1403 | (!strcmp(argv[i], "--format"))) { |
---|
1404 | noblanks++; |
---|
1405 | format++; |
---|
1406 | xmlKeepBlanksDefault(0); |
---|
1407 | } |
---|
1408 | else if ((!strcmp(argv[i], "-stream")) || |
---|
1409 | (!strcmp(argv[i], "--stream"))) { |
---|
1410 | stream++; |
---|
1411 | } |
---|
1412 | else if ((!strcmp(argv[i], "-chkregister")) || |
---|
1413 | (!strcmp(argv[i], "--chkregister"))) { |
---|
1414 | chkregister++; |
---|
1415 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
1416 | } else if ((!strcmp(argv[i], "-relaxng")) || |
---|
1417 | (!strcmp(argv[i], "--relaxng"))) { |
---|
1418 | i++; |
---|
1419 | relaxng = argv[i]; |
---|
1420 | noent++; |
---|
1421 | #endif |
---|
1422 | } else { |
---|
1423 | fprintf(stderr, "Unknown option %s\n", argv[i]); |
---|
1424 | usage(argv[0]); |
---|
1425 | return(1); |
---|
1426 | } |
---|
1427 | } |
---|
1428 | |
---|
1429 | #ifdef LIBXML_CATALOG_ENABLED |
---|
1430 | if (nocatalogs == 0) { |
---|
1431 | if (catalogs) { |
---|
1432 | const char *catal; |
---|
1433 | |
---|
1434 | catal = getenv("SGML_CATALOG_FILES"); |
---|
1435 | if (catal != NULL) { |
---|
1436 | xmlLoadCatalogs(catal); |
---|
1437 | } else { |
---|
1438 | fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n"); |
---|
1439 | } |
---|
1440 | } |
---|
1441 | } |
---|
1442 | #endif |
---|
1443 | |
---|
1444 | if (chkregister) { |
---|
1445 | xmlRegisterNodeDefault(registerNode); |
---|
1446 | xmlDeregisterNodeDefault(deregisterNode); |
---|
1447 | } |
---|
1448 | |
---|
1449 | xmlLineNumbersDefault(1); |
---|
1450 | if (loaddtd != 0) |
---|
1451 | xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; |
---|
1452 | if (dtdattrs) |
---|
1453 | xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS; |
---|
1454 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
---|
1455 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
---|
1456 | if ((htmlout) && (!nowrap)) { |
---|
1457 | xmlGenericError(xmlGenericErrorContext, |
---|
1458 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n"); |
---|
1459 | xmlGenericError(xmlGenericErrorContext, |
---|
1460 | "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"); |
---|
1461 | xmlGenericError(xmlGenericErrorContext, |
---|
1462 | "<html><head><title>%s output</title></head>\n", |
---|
1463 | argv[0]); |
---|
1464 | xmlGenericError(xmlGenericErrorContext, |
---|
1465 | "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n", |
---|
1466 | argv[0]); |
---|
1467 | } |
---|
1468 | |
---|
1469 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
1470 | if (relaxng != NULL) { |
---|
1471 | xmlRelaxNGParserCtxtPtr ctxt; |
---|
1472 | |
---|
1473 | ctxt = xmlRelaxNGNewParserCtxt(relaxng); |
---|
1474 | xmlRelaxNGSetParserErrors(ctxt, |
---|
1475 | (xmlRelaxNGValidityErrorFunc) fprintf, |
---|
1476 | (xmlRelaxNGValidityWarningFunc) fprintf, |
---|
1477 | stderr); |
---|
1478 | relaxngschemas = xmlRelaxNGParse(ctxt); |
---|
1479 | xmlRelaxNGFreeParserCtxt(ctxt); |
---|
1480 | } |
---|
1481 | #endif |
---|
1482 | for (i = 1; i < argc ; i++) { |
---|
1483 | if ((!strcmp(argv[i], "-encode")) || |
---|
1484 | (!strcmp(argv[i], "--encode"))) { |
---|
1485 | i++; |
---|
1486 | continue; |
---|
1487 | } else if ((!strcmp(argv[i], "-o")) || |
---|
1488 | (!strcmp(argv[i], "-output")) || |
---|
1489 | (!strcmp(argv[i], "--output"))) { |
---|
1490 | i++; |
---|
1491 | continue; |
---|
1492 | } |
---|
1493 | if ((!strcmp(argv[i], "-dtdvalid")) || |
---|
1494 | (!strcmp(argv[i], "--dtdvalid"))) { |
---|
1495 | i++; |
---|
1496 | continue; |
---|
1497 | } |
---|
1498 | if ((!strcmp(argv[i], "-relaxng")) || |
---|
1499 | (!strcmp(argv[i], "--relaxng"))) { |
---|
1500 | i++; |
---|
1501 | continue; |
---|
1502 | } |
---|
1503 | if ((timing) && (repeat)) |
---|
1504 | startTimer(); |
---|
1505 | /* Remember file names. "-" means stdin. <sven@zen.org> */ |
---|
1506 | if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) { |
---|
1507 | if (repeat) { |
---|
1508 | for (acount = 0;acount < 100 * repeat;acount++) |
---|
1509 | if (stream != 0) |
---|
1510 | streamFile(argv[i]); |
---|
1511 | else |
---|
1512 | parseAndPrintFile(argv[i]); |
---|
1513 | } else { |
---|
1514 | if (stream != 0) |
---|
1515 | streamFile(argv[i]); |
---|
1516 | else |
---|
1517 | parseAndPrintFile(argv[i]); |
---|
1518 | } |
---|
1519 | files ++; |
---|
1520 | if ((timing) && (repeat)) { |
---|
1521 | endTimer("100 iterations"); |
---|
1522 | } |
---|
1523 | } |
---|
1524 | } |
---|
1525 | if (generate) |
---|
1526 | parseAndPrintFile(NULL); |
---|
1527 | if ((htmlout) && (!nowrap)) { |
---|
1528 | xmlGenericError(xmlGenericErrorContext, "</body></html>\n"); |
---|
1529 | } |
---|
1530 | if ((files == 0) && (!generate) && (version == 0)) { |
---|
1531 | usage(argv[0]); |
---|
1532 | } |
---|
1533 | #ifdef LIBXML_SCHEMAS_ENABLED |
---|
1534 | if (relaxngschemas != NULL) |
---|
1535 | xmlRelaxNGFree(relaxngschemas); |
---|
1536 | xmlRelaxNGCleanupTypes(); |
---|
1537 | #endif |
---|
1538 | xmlCleanupParser(); |
---|
1539 | xmlMemoryDump(); |
---|
1540 | |
---|
1541 | return(progresult); |
---|
1542 | } |
---|
1543 | |
---|