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