Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 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@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 Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 32 | #ifdef HAVE_SYS_MMAN_H |
| 33 | #include <sys/mman.h> |
| 34 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 35 | #ifdef HAVE_STDLIB_H |
| 36 | #include <stdlib.h> |
| 37 | #endif |
| 38 | #ifdef HAVE_LIBREADLINE |
| 39 | #include <readline/readline.h> |
| 40 | #ifdef HAVE_LIBHISTORY |
| 41 | #include <readline/history.h> |
| 42 | #endif |
| 43 | #endif |
| 44 | |
| 45 | #include <libxml/xmlmemory.h> |
| 46 | #include <libxml/parser.h> |
| 47 | #include <libxml/parserInternals.h> |
| 48 | #include <libxml/HTMLparser.h> |
| 49 | #include <libxml/HTMLtree.h> |
| 50 | #include <libxml/tree.h> |
| 51 | #include <libxml/xpath.h> |
| 52 | #include <libxml/debugXML.h> |
| 53 | |
| 54 | #ifdef LIBXML_DEBUG_ENABLED |
| 55 | static int debug = 0; |
| 56 | static int shell = 0; |
| 57 | static int debugent = 0; |
| 58 | #endif |
| 59 | static int copy = 0; |
| 60 | static int recovery = 0; |
| 61 | static int noent = 0; |
| 62 | static int noout = 0; |
| 63 | static int nowrap = 0; |
| 64 | static int valid = 0; |
| 65 | static int postvalid = 0; |
| 66 | static int repeat = 0; |
| 67 | static int insert = 0; |
| 68 | static int compress = 0; |
| 69 | static int html = 0; |
| 70 | static int htmlout = 0; |
| 71 | static int push = 0; |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 72 | #ifdef HAVE_SYS_MMAN_H |
| 73 | static int memory = 0; |
| 74 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 75 | static int noblanks = 0; |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 76 | static int testIO = 0; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 77 | static char *encoding = NULL; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 78 | |
| 79 | extern int xmlDoValidityCheckingDefaultValue; |
| 80 | extern int xmlGetWarningsDefaultValue; |
| 81 | |
| 82 | /************************************************************************ |
| 83 | * * |
| 84 | * HTML ouput * |
| 85 | * * |
| 86 | ************************************************************************/ |
| 87 | char buffer[50000]; |
| 88 | |
| 89 | void |
| 90 | xmlHTMLEncodeSend(void) { |
| 91 | char *result; |
| 92 | |
| 93 | result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer); |
| 94 | if (result) { |
| 95 | fprintf(stderr, "%s", result); |
| 96 | xmlFree(result); |
| 97 | } |
| 98 | buffer[0] = 0; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * xmlHTMLPrintFileInfo: |
| 103 | * @input: an xmlParserInputPtr input |
| 104 | * |
| 105 | * Displays the associated file and line informations for the current input |
| 106 | */ |
| 107 | |
| 108 | void |
| 109 | xmlHTMLPrintFileInfo(xmlParserInputPtr input) { |
| 110 | fprintf(stderr, "<p>"); |
| 111 | if (input != NULL) { |
| 112 | if (input->filename) { |
| 113 | sprintf(&buffer[strlen(buffer)], "%s:%d: ", input->filename, |
| 114 | input->line); |
| 115 | } else { |
| 116 | sprintf(&buffer[strlen(buffer)], "Entity: line %d: ", input->line); |
| 117 | } |
| 118 | } |
| 119 | xmlHTMLEncodeSend(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * xmlHTMLPrintFileContext: |
| 124 | * @input: an xmlParserInputPtr input |
| 125 | * |
| 126 | * Displays current context within the input content for error tracking |
| 127 | */ |
| 128 | |
| 129 | void |
| 130 | xmlHTMLPrintFileContext(xmlParserInputPtr input) { |
| 131 | const xmlChar *cur, *base; |
| 132 | int n; |
| 133 | |
| 134 | if (input == NULL) return; |
| 135 | fprintf(stderr, "<pre>\n"); |
| 136 | cur = input->cur; |
| 137 | base = input->base; |
| 138 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
| 139 | cur--; |
| 140 | } |
| 141 | n = 0; |
| 142 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
| 143 | cur--; |
| 144 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
| 145 | base = cur; |
| 146 | n = 0; |
| 147 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
| 148 | sprintf(&buffer[strlen(buffer)], "%c", (unsigned char) *cur++); |
| 149 | n++; |
| 150 | } |
| 151 | sprintf(&buffer[strlen(buffer)], "\n"); |
| 152 | cur = input->cur; |
| 153 | while ((*cur == '\n') || (*cur == '\r')) |
| 154 | cur--; |
| 155 | n = 0; |
| 156 | while ((cur != base) && (n++ < 80)) { |
| 157 | sprintf(&buffer[strlen(buffer)], " "); |
| 158 | base++; |
| 159 | } |
| 160 | sprintf(&buffer[strlen(buffer)],"^\n"); |
| 161 | xmlHTMLEncodeSend(); |
| 162 | fprintf(stderr, "</pre>"); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * xmlHTMLError: |
| 167 | * @ctx: an XML parser context |
| 168 | * @msg: the message to display/transmit |
| 169 | * @...: extra parameters for the message display |
| 170 | * |
| 171 | * Display and format an error messages, gives file, line, position and |
| 172 | * extra parameters. |
| 173 | */ |
| 174 | void |
| 175 | xmlHTMLError(void *ctx, const char *msg, ...) |
| 176 | { |
| 177 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 178 | xmlParserInputPtr input; |
| 179 | xmlParserInputPtr cur = NULL; |
| 180 | va_list args; |
| 181 | |
| 182 | buffer[0] = 0; |
| 183 | input = ctxt->input; |
| 184 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
| 185 | cur = input; |
| 186 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 187 | } |
| 188 | |
| 189 | xmlHTMLPrintFileInfo(input); |
| 190 | |
| 191 | fprintf(stderr, "<b>error</b>: "); |
| 192 | va_start(args, msg); |
| 193 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 194 | va_end(args); |
| 195 | xmlHTMLEncodeSend(); |
| 196 | fprintf(stderr, "</p>\n"); |
| 197 | |
| 198 | xmlHTMLPrintFileContext(input); |
| 199 | xmlHTMLEncodeSend(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * xmlHTMLWarning: |
| 204 | * @ctx: an XML parser context |
| 205 | * @msg: the message to display/transmit |
| 206 | * @...: extra parameters for the message display |
| 207 | * |
| 208 | * Display and format a warning messages, gives file, line, position and |
| 209 | * extra parameters. |
| 210 | */ |
| 211 | void |
| 212 | xmlHTMLWarning(void *ctx, const char *msg, ...) |
| 213 | { |
| 214 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 215 | xmlParserInputPtr input; |
| 216 | xmlParserInputPtr cur = NULL; |
| 217 | va_list args; |
| 218 | |
| 219 | buffer[0] = 0; |
| 220 | input = ctxt->input; |
| 221 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
| 222 | cur = input; |
| 223 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | xmlHTMLPrintFileInfo(input); |
| 228 | |
| 229 | fprintf(stderr, "<b>warning</b>: "); |
| 230 | va_start(args, msg); |
| 231 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 232 | va_end(args); |
| 233 | xmlHTMLEncodeSend(); |
| 234 | fprintf(stderr, "</p>\n"); |
| 235 | |
| 236 | xmlHTMLPrintFileContext(input); |
| 237 | xmlHTMLEncodeSend(); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * xmlHTMLValidityError: |
| 242 | * @ctx: an XML parser context |
| 243 | * @msg: the message to display/transmit |
| 244 | * @...: extra parameters for the message display |
| 245 | * |
| 246 | * Display and format an validity error messages, gives file, |
| 247 | * line, position and extra parameters. |
| 248 | */ |
| 249 | void |
| 250 | xmlHTMLValidityError(void *ctx, const char *msg, ...) |
| 251 | { |
| 252 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 253 | xmlParserInputPtr input; |
| 254 | va_list args; |
| 255 | |
| 256 | buffer[0] = 0; |
| 257 | input = ctxt->input; |
| 258 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 259 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 260 | |
| 261 | xmlHTMLPrintFileInfo(input); |
| 262 | |
| 263 | fprintf(stderr, "<b>validity error</b>: "); |
| 264 | va_start(args, msg); |
| 265 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 266 | va_end(args); |
| 267 | xmlHTMLEncodeSend(); |
| 268 | fprintf(stderr, "</p>\n"); |
| 269 | |
| 270 | xmlHTMLPrintFileContext(input); |
| 271 | xmlHTMLEncodeSend(); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * xmlHTMLValidityWarning: |
| 276 | * @ctx: an XML parser context |
| 277 | * @msg: the message to display/transmit |
| 278 | * @...: extra parameters for the message display |
| 279 | * |
| 280 | * Display and format a validity warning messages, gives file, line, |
| 281 | * position and extra parameters. |
| 282 | */ |
| 283 | void |
| 284 | xmlHTMLValidityWarning(void *ctx, const char *msg, ...) |
| 285 | { |
| 286 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 287 | xmlParserInputPtr input; |
| 288 | va_list args; |
| 289 | |
| 290 | buffer[0] = 0; |
| 291 | input = ctxt->input; |
| 292 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 293 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 294 | |
| 295 | xmlHTMLPrintFileInfo(input); |
| 296 | |
| 297 | fprintf(stderr, "<b>validity warning</b>: "); |
| 298 | va_start(args, msg); |
| 299 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 300 | va_end(args); |
| 301 | xmlHTMLEncodeSend(); |
| 302 | fprintf(stderr, "</p>\n"); |
| 303 | |
| 304 | xmlHTMLPrintFileContext(input); |
| 305 | xmlHTMLEncodeSend(); |
| 306 | } |
| 307 | |
| 308 | /************************************************************************ |
| 309 | * * |
| 310 | * Shell Interface * |
| 311 | * * |
| 312 | ************************************************************************/ |
| 313 | /** |
| 314 | * xmlShellReadline: |
| 315 | * @prompt: the prompt value |
| 316 | * |
| 317 | * Read a string |
| 318 | * |
| 319 | * Returns a pointer to it or NULL on EOF the caller is expected to |
| 320 | * free the returned string. |
| 321 | */ |
| 322 | char * |
| 323 | xmlShellReadline(char *prompt) { |
| 324 | #ifdef HAVE_LIBREADLINE |
| 325 | char *line_read; |
| 326 | |
| 327 | /* Get a line from the user. */ |
| 328 | line_read = readline (prompt); |
| 329 | |
| 330 | /* If the line has any text in it, save it on the history. */ |
| 331 | if (line_read && *line_read) |
| 332 | add_history (line_read); |
| 333 | |
| 334 | return (line_read); |
| 335 | #else |
| 336 | char line_read[501]; |
| 337 | |
| 338 | if (prompt != NULL) |
| 339 | fprintf(stdout, "%s", prompt); |
| 340 | if (!fgets(line_read, 500, stdin)) |
| 341 | return(NULL); |
| 342 | line_read[500] = 0; |
| 343 | return(strdup(line_read)); |
| 344 | #endif |
| 345 | } |
| 346 | |
| 347 | /************************************************************************ |
| 348 | * * |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 349 | * I/O Interfaces * |
| 350 | * * |
| 351 | ************************************************************************/ |
| 352 | |
| 353 | int myRead(FILE *f, char * buffer, int len) { |
| 354 | return(fread(buffer, 1, len, f)); |
| 355 | } |
| 356 | void myClose(FILE *f) { |
| 357 | fclose(f); |
| 358 | } |
| 359 | |
| 360 | /************************************************************************ |
| 361 | * * |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 362 | * Test processing * |
| 363 | * * |
| 364 | ************************************************************************/ |
| 365 | void parseAndPrintFile(char *filename) { |
| 366 | xmlDocPtr doc = NULL, tmp; |
| 367 | |
| 368 | #ifdef LIBXML_HTML_ENABLED |
| 369 | if (html) { |
| 370 | doc = htmlParseFile(filename, NULL); |
| 371 | } else { |
| 372 | #endif /* LIBXML_HTML_ENABLED */ |
| 373 | /* |
| 374 | * build an XML tree from a string; |
| 375 | */ |
| 376 | if (push) { |
| 377 | FILE *f; |
| 378 | |
| 379 | f = fopen(filename, "r"); |
| 380 | if (f != NULL) { |
| 381 | int res, size = 3; |
| 382 | char chars[1024]; |
| 383 | xmlParserCtxtPtr ctxt; |
| 384 | |
| 385 | if (repeat) |
| 386 | size = 1024; |
| 387 | res = fread(chars, 1, 4, f); |
| 388 | if (res > 0) { |
| 389 | ctxt = xmlCreatePushParserCtxt(NULL, NULL, |
| 390 | chars, res, filename); |
| 391 | while ((res = fread(chars, 1, size, f)) > 0) { |
| 392 | xmlParseChunk(ctxt, chars, res, 0); |
| 393 | } |
| 394 | xmlParseChunk(ctxt, chars, 0, 1); |
| 395 | doc = ctxt->myDoc; |
| 396 | xmlFreeParserCtxt(ctxt); |
| 397 | } |
| 398 | } |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 399 | } else if (testIO) { |
| 400 | int ret; |
| 401 | FILE *f; |
| 402 | |
| 403 | f = fopen(filename, "r"); |
| 404 | if (f != NULL) { |
| 405 | xmlParserCtxtPtr ctxt; |
| 406 | |
| 407 | ctxt = xmlCreateIOParserCtxt(NULL, NULL, |
| 408 | (xmlInputReadCallback) myRead, |
| 409 | (xmlInputCloseCallback) myClose, |
| 410 | f, XML_CHAR_ENCODING_NONE); |
| 411 | xmlParseDocument(ctxt); |
| 412 | |
| 413 | ret = ctxt->wellFormed; |
| 414 | doc = ctxt->myDoc; |
| 415 | xmlFreeParserCtxt(ctxt); |
| 416 | if (!ret) { |
| 417 | xmlFreeDoc(doc); |
| 418 | doc = NULL; |
| 419 | } |
| 420 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 421 | } else if (recovery) { |
| 422 | doc = xmlRecoverFile(filename); |
| 423 | } else if (htmlout) { |
| 424 | int ret; |
| 425 | xmlParserCtxtPtr ctxt; |
| 426 | xmlSAXHandler silent, *old; |
| 427 | |
| 428 | ctxt = xmlCreateFileParserCtxt(filename); |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 429 | |
| 430 | if (ctxt == NULL) { |
| 431 | /* If xmlCreateFileParseCtxt() return NULL something |
| 432 | strange happened so we don't want to do anything. Do |
| 433 | we want to print an error message here? |
| 434 | <sven@zen.org> */ |
Daniel Veillard | 7ebb1ee | 2000-08-04 18:24:45 +0000 | [diff] [blame^] | 435 | doc = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 436 | } else { |
| 437 | memcpy(&silent, ctxt->sax, sizeof(silent)); |
| 438 | old = ctxt->sax; |
| 439 | silent.error = xmlHTMLError; |
| 440 | if (xmlGetWarningsDefaultValue) |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 441 | silent.warning = xmlHTMLWarning; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 442 | else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 443 | silent.warning = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 444 | silent.fatalError = xmlHTMLError; |
| 445 | ctxt->sax = &silent; |
| 446 | ctxt->vctxt.error = xmlHTMLValidityError; |
| 447 | if (xmlGetWarningsDefaultValue) |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 448 | ctxt->vctxt.warning = xmlHTMLValidityWarning; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 449 | else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 450 | ctxt->vctxt.warning = NULL; |
| 451 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 452 | xmlParseDocument(ctxt); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 453 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 454 | ret = ctxt->wellFormed; |
| 455 | doc = ctxt->myDoc; |
| 456 | ctxt->sax = old; |
| 457 | xmlFreeParserCtxt(ctxt); |
| 458 | if (!ret) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 459 | xmlFreeDoc(doc); |
| 460 | doc = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 461 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 462 | } |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 463 | #ifdef HAVE_SYS_MMAN_H |
| 464 | } else if (memory) { |
| 465 | int fd; |
| 466 | struct stat info; |
| 467 | const char *base; |
| 468 | if (stat(filename, &info) < 0) |
| 469 | return; |
| 470 | if ((fd = open(filename, O_RDONLY)) < 0) |
| 471 | return; |
| 472 | base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ; |
| 473 | if (base == MAP_FAILED) |
| 474 | return; |
| 475 | |
| 476 | doc = xmlParseMemory((char *) base, info.st_size); |
| 477 | munmap((char *) base, info.st_size); |
| 478 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 479 | } else |
| 480 | doc = xmlParseFile(filename); |
| 481 | #ifdef LIBXML_HTML_ENABLED |
| 482 | } |
| 483 | #endif |
| 484 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 485 | /* |
| 486 | * If we don't have a document we might as well give up. Do we |
| 487 | * want an error message here? <sven@zen.org> */ |
| 488 | if (doc == NULL) |
| 489 | { |
| 490 | return; |
| 491 | } |
| 492 | |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 493 | #ifdef LIBXML_DEBUG_ENABLED |
| 494 | /* |
| 495 | * shell interraction |
| 496 | */ |
| 497 | if (shell) |
| 498 | xmlShell(doc, filename, xmlShellReadline, stdout); |
| 499 | #endif |
| 500 | |
| 501 | /* |
| 502 | * test intermediate copy if needed. |
| 503 | */ |
| 504 | if (copy) { |
| 505 | tmp = doc; |
| 506 | doc = xmlCopyDoc(doc, 1); |
| 507 | xmlFreeDoc(tmp); |
| 508 | } |
| 509 | |
| 510 | if ((insert) && (!html)) { |
| 511 | const xmlChar* list[256]; |
| 512 | int nb, i; |
| 513 | xmlNodePtr node; |
| 514 | |
| 515 | if (doc->children != NULL) { |
| 516 | node = doc->children; |
| 517 | while ((node != NULL) && (node->last == NULL)) node = node->next; |
| 518 | if (node != NULL) { |
| 519 | nb = xmlValidGetValidElements(node->last, NULL, list, 256); |
| 520 | if (nb < 0) { |
| 521 | printf("could not get valid list of elements\n"); |
| 522 | } else if (nb == 0) { |
| 523 | printf("No element can be indersted under root\n"); |
| 524 | } else { |
| 525 | printf("%d element types can be indersted under root:\n", |
| 526 | nb); |
| 527 | for (i = 0;i < nb;i++) { |
| 528 | printf("%s\n", list[i]); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | }else if (noout == 0) { |
| 534 | /* |
| 535 | * print it. |
| 536 | */ |
| 537 | #ifdef LIBXML_DEBUG_ENABLED |
| 538 | if (!debug) { |
| 539 | #endif |
| 540 | if (compress) |
| 541 | xmlSaveFile("-", doc); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 542 | else if (encoding != NULL) |
| 543 | xmlSaveFileEnc("-", doc, encoding); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 544 | else |
| 545 | xmlDocDump(stdout, doc); |
| 546 | #ifdef LIBXML_DEBUG_ENABLED |
| 547 | } else |
| 548 | xmlDebugDumpDocument(stdout, doc); |
| 549 | #endif |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * A posteriori validation test |
| 554 | */ |
| 555 | if (postvalid) { |
| 556 | xmlValidCtxt cvp; |
| 557 | cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf; |
| 558 | xmlValidateDocument(&cvp, doc); |
| 559 | } |
| 560 | |
| 561 | #ifdef LIBXML_DEBUG_ENABLED |
| 562 | if ((debugent) && (!html)) |
| 563 | xmlDebugDumpEntities(stdout, doc); |
| 564 | #endif |
| 565 | |
| 566 | /* |
| 567 | * free it. |
| 568 | */ |
| 569 | xmlFreeDoc(doc); |
| 570 | } |
| 571 | |
| 572 | int main(int argc, char **argv) { |
| 573 | int i, count; |
| 574 | int files = 0; |
| 575 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 576 | LIBXML_TEST_VERSION |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 577 | for (i = 1; i < argc ; i++) { |
| 578 | #ifdef LIBXML_DEBUG_ENABLED |
| 579 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 580 | debug++; |
| 581 | else if ((!strcmp(argv[i], "-debugent")) || (!strcmp(argv[i], "--debugent"))) |
| 582 | debugent++; |
| 583 | else if ((!strcmp(argv[i], "-shell")) || |
| 584 | (!strcmp(argv[i], "--shell"))) { |
| 585 | shell++; |
| 586 | noout = 1; |
| 587 | } else |
| 588 | #endif |
| 589 | if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 590 | copy++; |
| 591 | else if ((!strcmp(argv[i], "-recover")) || |
| 592 | (!strcmp(argv[i], "--recover"))) |
| 593 | recovery++; |
| 594 | else if ((!strcmp(argv[i], "-noent")) || |
| 595 | (!strcmp(argv[i], "--noent"))) |
| 596 | noent++; |
| 597 | else if ((!strcmp(argv[i], "-noout")) || |
| 598 | (!strcmp(argv[i], "--noout"))) |
| 599 | noout++; |
| 600 | else if ((!strcmp(argv[i], "-htmlout")) || |
| 601 | (!strcmp(argv[i], "--htmlout"))) |
| 602 | htmlout++; |
| 603 | #ifdef LIBXML_HTML_ENABLED |
| 604 | else if ((!strcmp(argv[i], "-html")) || |
| 605 | (!strcmp(argv[i], "--html"))) { |
| 606 | html++; |
| 607 | } |
| 608 | #endif /* LIBXML_HTML_ENABLED */ |
| 609 | else if ((!strcmp(argv[i], "-nowrap")) || |
| 610 | (!strcmp(argv[i], "--nowrap"))) |
| 611 | nowrap++; |
| 612 | else if ((!strcmp(argv[i], "-valid")) || |
| 613 | (!strcmp(argv[i], "--valid"))) |
| 614 | valid++; |
| 615 | else if ((!strcmp(argv[i], "-postvalid")) || |
| 616 | (!strcmp(argv[i], "--postvalid"))) |
| 617 | postvalid++; |
| 618 | else if ((!strcmp(argv[i], "-insert")) || |
| 619 | (!strcmp(argv[i], "--insert"))) |
| 620 | insert++; |
| 621 | else if ((!strcmp(argv[i], "-repeat")) || |
| 622 | (!strcmp(argv[i], "--repeat"))) |
| 623 | repeat++; |
| 624 | else if ((!strcmp(argv[i], "-push")) || |
| 625 | (!strcmp(argv[i], "--push"))) |
| 626 | push++; |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 627 | #ifdef HAVE_SYS_MMAN_H |
| 628 | else if ((!strcmp(argv[i], "-memory")) || |
| 629 | (!strcmp(argv[i], "--memory"))) |
| 630 | memory++; |
| 631 | #endif |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 632 | else if ((!strcmp(argv[i], "-testIO")) || |
| 633 | (!strcmp(argv[i], "--testIO"))) |
| 634 | testIO++; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 635 | else if ((!strcmp(argv[i], "-compress")) || |
| 636 | (!strcmp(argv[i], "--compress"))) { |
| 637 | compress++; |
| 638 | xmlSetCompressMode(9); |
| 639 | } |
| 640 | else if ((!strcmp(argv[i], "-nowarning")) || |
| 641 | (!strcmp(argv[i], "--nowarning"))) { |
| 642 | xmlGetWarningsDefaultValue = 0; |
| 643 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 644 | else if ((!strcmp(argv[i], "-encode")) || |
| 645 | (!strcmp(argv[i], "--encode"))) { |
| 646 | i++; |
| 647 | encoding = argv[i]; |
| 648 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 649 | else if ((!strcmp(argv[i], "-noblanks")) || |
| 650 | (!strcmp(argv[i], "--noblanks"))) { |
| 651 | noblanks++; |
| 652 | xmlKeepBlanksDefault(0); |
| 653 | } |
| 654 | } |
| 655 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
| 656 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
| 657 | if ((htmlout) && (!nowrap)) { |
| 658 | fprintf(stderr, |
| 659 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n"); |
| 660 | fprintf(stderr, "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"); |
| 661 | fprintf(stderr, |
| 662 | "<html><head><title>%s output</title></head>\n", |
| 663 | argv[0]); |
| 664 | fprintf(stderr, |
| 665 | "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n", |
| 666 | argv[0]); |
| 667 | } |
| 668 | for (i = 1; i < argc ; i++) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 669 | if ((!strcmp(argv[i], "-encode")) || |
| 670 | (!strcmp(argv[i], "--encode"))) { |
| 671 | i++; |
| 672 | continue; |
| 673 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 674 | if (argv[i][0] != '-') { |
| 675 | if (repeat) { |
| 676 | for (count = 0;count < 100 * repeat;count++) |
| 677 | parseAndPrintFile(argv[i]); |
| 678 | } else |
| 679 | parseAndPrintFile(argv[i]); |
| 680 | files ++; |
| 681 | } |
| 682 | } |
| 683 | if ((htmlout) && (!nowrap)) { |
| 684 | fprintf(stderr, "</body></html>\n"); |
| 685 | } |
| 686 | if (files == 0) { |
| 687 | printf("Usage : %s [--debug] [--debugent] [--copy] [--recover] [--noent] [--noout] [--valid] [--repeat] XMLfiles ...\n", |
| 688 | argv[0]); |
| 689 | printf("\tParse the XML files and output the result of the parsing\n"); |
| 690 | #ifdef LIBXML_DEBUG_ENABLED |
| 691 | printf("\t--debug : dump a debug tree of the in-memory document\n"); |
| 692 | printf("\t--shell : run a navigating shell\n"); |
| 693 | printf("\t--debugent : debug the entities defined in the document\n"); |
| 694 | #endif |
| 695 | printf("\t--copy : used to test the internal copy implementation\n"); |
| 696 | printf("\t--recover : output what was parsable on broken XML documents\n"); |
| 697 | printf("\t--noent : substitute entity references by their value\n"); |
| 698 | printf("\t--noout : don't output the result tree\n"); |
| 699 | printf("\t--htmlout : output results as HTML\n"); |
| 700 | printf("\t--nowarp : do not put HTML doc wrapper\n"); |
| 701 | printf("\t--valid : validate the document in addition to std well-formed check\n"); |
| 702 | printf("\t--postvalid : do a posteriori validation, i.e after parsing\n"); |
| 703 | printf("\t--repeat : repeat 100 times, for timing or profiling\n"); |
| 704 | printf("\t--insert : ad-hoc test for valid insertions\n"); |
| 705 | printf("\t--compress : turn on gzip compression of output\n"); |
| 706 | #ifdef LIBXML_HTML_ENABLED |
| 707 | printf("\t--html : use the HTML parser\n"); |
| 708 | #endif |
| 709 | printf("\t--push : use the push mode of the parser\n"); |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 710 | #ifdef HAVE_SYS_MMAN_H |
| 711 | printf("\t--memory : parse from memory\n"); |
| 712 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 713 | printf("\t--nowarning : do not emit warnings from parser/validator\n"); |
| 714 | printf("\t--noblanks : drop (ignorable?) blanks spaces\n"); |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 715 | printf("\t--testIO : test user I/O support\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 716 | printf("\t--encode encoding : output in the given encoding\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 717 | } |
| 718 | xmlCleanupParser(); |
| 719 | xmlMemoryDump(); |
| 720 | |
| 721 | return(0); |
| 722 | } |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 723 | |