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> |
Daniel Veillard | 2d90de4 | 2001-04-16 17:46:18 +0000 | [diff] [blame^] | 19 | #ifdef _WIN32 |
| 20 | #ifdef _MSC_VER |
| 21 | #include <winsock2.h> |
| 22 | #pragma comment(lib, "ws2_32.lib") |
| 23 | #define gettimeofday(p1,p2) |
| 24 | #endif /* _MSC_VER */ |
| 25 | #else /* _WIN32 */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 26 | #include <sys/time.h> |
Daniel Veillard | 2d90de4 | 2001-04-16 17:46:18 +0000 | [diff] [blame^] | 27 | #endif /* _WIN32 */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 28 | |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef HAVE_SYS_TYPES_H |
| 31 | #include <sys/types.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_SYS_STAT_H |
| 34 | #include <sys/stat.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_FCNTL_H |
| 37 | #include <fcntl.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_UNISTD_H |
| 40 | #include <unistd.h> |
| 41 | #endif |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 42 | #ifdef HAVE_SYS_MMAN_H |
| 43 | #include <sys/mman.h> |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 44 | /* seems needed for Solaris */ |
| 45 | #ifndef MAP_FAILED |
| 46 | #define MAP_FAILED ((void *) -1) |
| 47 | #endif |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 48 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 49 | #ifdef HAVE_STDLIB_H |
| 50 | #include <stdlib.h> |
| 51 | #endif |
| 52 | #ifdef HAVE_LIBREADLINE |
| 53 | #include <readline/readline.h> |
| 54 | #ifdef HAVE_LIBHISTORY |
| 55 | #include <readline/history.h> |
| 56 | #endif |
| 57 | #endif |
| 58 | |
| 59 | #include <libxml/xmlmemory.h> |
| 60 | #include <libxml/parser.h> |
| 61 | #include <libxml/parserInternals.h> |
| 62 | #include <libxml/HTMLparser.h> |
| 63 | #include <libxml/HTMLtree.h> |
| 64 | #include <libxml/tree.h> |
| 65 | #include <libxml/xpath.h> |
| 66 | #include <libxml/debugXML.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 67 | #include <libxml/xmlerror.h> |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 68 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 69 | #include <libxml/xinclude.h> |
| 70 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 71 | |
| 72 | #ifdef LIBXML_DEBUG_ENABLED |
| 73 | static int debug = 0; |
| 74 | static int shell = 0; |
| 75 | static int debugent = 0; |
| 76 | #endif |
| 77 | static int copy = 0; |
| 78 | static int recovery = 0; |
| 79 | static int noent = 0; |
| 80 | static int noout = 0; |
| 81 | static int nowrap = 0; |
| 82 | static int valid = 0; |
| 83 | static int postvalid = 0; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 84 | static char * dtdvalid = NULL; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 85 | static int repeat = 0; |
| 86 | static int insert = 0; |
| 87 | static int compress = 0; |
| 88 | static int html = 0; |
| 89 | static int htmlout = 0; |
| 90 | static int push = 0; |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 91 | #ifdef HAVE_SYS_MMAN_H |
| 92 | static int memory = 0; |
| 93 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 94 | static int noblanks = 0; |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 95 | static int testIO = 0; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 96 | static char *encoding = NULL; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 97 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 98 | static int xinclude = 0; |
| 99 | #endif |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 100 | static int progresult = 0; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 101 | static int timing = 0; |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 102 | static int generate = 0; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 103 | static struct timeval begin, end; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 104 | |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 105 | |
| 106 | #ifdef VMS |
| 107 | extern int xmlDoValidityCheckingDefaultVal; |
| 108 | #define xmlDoValidityCheckingDefaultValue xmlDoValidityCheckingDefaultVal |
| 109 | #else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 110 | extern int xmlDoValidityCheckingDefaultValue; |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 111 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 112 | extern int xmlGetWarningsDefaultValue; |
| 113 | |
| 114 | /************************************************************************ |
| 115 | * * |
| 116 | * HTML ouput * |
| 117 | * * |
| 118 | ************************************************************************/ |
| 119 | char buffer[50000]; |
| 120 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 121 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 122 | xmlHTMLEncodeSend(void) { |
| 123 | char *result; |
| 124 | |
| 125 | result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer); |
| 126 | if (result) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 127 | xmlGenericError(xmlGenericErrorContext, "%s", result); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 128 | xmlFree(result); |
| 129 | } |
| 130 | buffer[0] = 0; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * xmlHTMLPrintFileInfo: |
| 135 | * @input: an xmlParserInputPtr input |
| 136 | * |
| 137 | * Displays the associated file and line informations for the current input |
| 138 | */ |
| 139 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 140 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 141 | xmlHTMLPrintFileInfo(xmlParserInputPtr input) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 142 | xmlGenericError(xmlGenericErrorContext, "<p>"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 143 | if (input != NULL) { |
| 144 | if (input->filename) { |
| 145 | sprintf(&buffer[strlen(buffer)], "%s:%d: ", input->filename, |
| 146 | input->line); |
| 147 | } else { |
| 148 | sprintf(&buffer[strlen(buffer)], "Entity: line %d: ", input->line); |
| 149 | } |
| 150 | } |
| 151 | xmlHTMLEncodeSend(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * xmlHTMLPrintFileContext: |
| 156 | * @input: an xmlParserInputPtr input |
| 157 | * |
| 158 | * Displays current context within the input content for error tracking |
| 159 | */ |
| 160 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 161 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 162 | xmlHTMLPrintFileContext(xmlParserInputPtr input) { |
| 163 | const xmlChar *cur, *base; |
| 164 | int n; |
| 165 | |
| 166 | if (input == NULL) return; |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 167 | xmlGenericError(xmlGenericErrorContext, "<pre>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 168 | cur = input->cur; |
| 169 | base = input->base; |
| 170 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
| 171 | cur--; |
| 172 | } |
| 173 | n = 0; |
| 174 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
| 175 | cur--; |
| 176 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
| 177 | base = cur; |
| 178 | n = 0; |
| 179 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
| 180 | sprintf(&buffer[strlen(buffer)], "%c", (unsigned char) *cur++); |
| 181 | n++; |
| 182 | } |
| 183 | sprintf(&buffer[strlen(buffer)], "\n"); |
| 184 | cur = input->cur; |
| 185 | while ((*cur == '\n') || (*cur == '\r')) |
| 186 | cur--; |
| 187 | n = 0; |
| 188 | while ((cur != base) && (n++ < 80)) { |
| 189 | sprintf(&buffer[strlen(buffer)], " "); |
| 190 | base++; |
| 191 | } |
| 192 | sprintf(&buffer[strlen(buffer)],"^\n"); |
| 193 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 194 | xmlGenericError(xmlGenericErrorContext, "</pre>"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * xmlHTMLError: |
| 199 | * @ctx: an XML parser context |
| 200 | * @msg: the message to display/transmit |
| 201 | * @...: extra parameters for the message display |
| 202 | * |
| 203 | * Display and format an error messages, gives file, line, position and |
| 204 | * extra parameters. |
| 205 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 206 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 207 | xmlHTMLError(void *ctx, const char *msg, ...) |
| 208 | { |
| 209 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 210 | xmlParserInputPtr input; |
| 211 | xmlParserInputPtr cur = NULL; |
| 212 | va_list args; |
| 213 | |
| 214 | buffer[0] = 0; |
| 215 | input = ctxt->input; |
| 216 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
| 217 | cur = input; |
| 218 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 219 | } |
| 220 | |
| 221 | xmlHTMLPrintFileInfo(input); |
| 222 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 223 | xmlGenericError(xmlGenericErrorContext, "<b>error</b>: "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 224 | va_start(args, msg); |
| 225 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 226 | va_end(args); |
| 227 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 228 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 229 | |
| 230 | xmlHTMLPrintFileContext(input); |
| 231 | xmlHTMLEncodeSend(); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * xmlHTMLWarning: |
| 236 | * @ctx: an XML parser context |
| 237 | * @msg: the message to display/transmit |
| 238 | * @...: extra parameters for the message display |
| 239 | * |
| 240 | * Display and format a warning messages, gives file, line, position and |
| 241 | * extra parameters. |
| 242 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 243 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 244 | xmlHTMLWarning(void *ctx, const char *msg, ...) |
| 245 | { |
| 246 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 247 | xmlParserInputPtr input; |
| 248 | xmlParserInputPtr cur = NULL; |
| 249 | va_list args; |
| 250 | |
| 251 | buffer[0] = 0; |
| 252 | input = ctxt->input; |
| 253 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
| 254 | cur = input; |
| 255 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | xmlHTMLPrintFileInfo(input); |
| 260 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 261 | xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 262 | va_start(args, msg); |
| 263 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 264 | va_end(args); |
| 265 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 266 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 267 | |
| 268 | xmlHTMLPrintFileContext(input); |
| 269 | xmlHTMLEncodeSend(); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * xmlHTMLValidityError: |
| 274 | * @ctx: an XML parser context |
| 275 | * @msg: the message to display/transmit |
| 276 | * @...: extra parameters for the message display |
| 277 | * |
| 278 | * Display and format an validity error messages, gives file, |
| 279 | * line, position and extra parameters. |
| 280 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 281 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 282 | xmlHTMLValidityError(void *ctx, const char *msg, ...) |
| 283 | { |
| 284 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 285 | xmlParserInputPtr input; |
| 286 | va_list args; |
| 287 | |
| 288 | buffer[0] = 0; |
| 289 | input = ctxt->input; |
| 290 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 291 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 292 | |
| 293 | xmlHTMLPrintFileInfo(input); |
| 294 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 295 | xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 296 | va_start(args, msg); |
| 297 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 298 | va_end(args); |
| 299 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 300 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 301 | |
| 302 | xmlHTMLPrintFileContext(input); |
| 303 | xmlHTMLEncodeSend(); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * xmlHTMLValidityWarning: |
| 308 | * @ctx: an XML parser context |
| 309 | * @msg: the message to display/transmit |
| 310 | * @...: extra parameters for the message display |
| 311 | * |
| 312 | * Display and format a validity warning messages, gives file, line, |
| 313 | * position and extra parameters. |
| 314 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 315 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 316 | xmlHTMLValidityWarning(void *ctx, const char *msg, ...) |
| 317 | { |
| 318 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 319 | xmlParserInputPtr input; |
| 320 | va_list args; |
| 321 | |
| 322 | buffer[0] = 0; |
| 323 | input = ctxt->input; |
| 324 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 325 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 326 | |
| 327 | xmlHTMLPrintFileInfo(input); |
| 328 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 329 | xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 330 | va_start(args, msg); |
| 331 | vsprintf(&buffer[strlen(buffer)], msg, args); |
| 332 | va_end(args); |
| 333 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 334 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 335 | |
| 336 | xmlHTMLPrintFileContext(input); |
| 337 | xmlHTMLEncodeSend(); |
| 338 | } |
| 339 | |
| 340 | /************************************************************************ |
| 341 | * * |
| 342 | * Shell Interface * |
| 343 | * * |
| 344 | ************************************************************************/ |
| 345 | /** |
| 346 | * xmlShellReadline: |
| 347 | * @prompt: the prompt value |
| 348 | * |
| 349 | * Read a string |
| 350 | * |
| 351 | * Returns a pointer to it or NULL on EOF the caller is expected to |
| 352 | * free the returned string. |
| 353 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 354 | static char * |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 355 | xmlShellReadline(char *prompt) { |
| 356 | #ifdef HAVE_LIBREADLINE |
| 357 | char *line_read; |
| 358 | |
| 359 | /* Get a line from the user. */ |
| 360 | line_read = readline (prompt); |
| 361 | |
| 362 | /* If the line has any text in it, save it on the history. */ |
| 363 | if (line_read && *line_read) |
| 364 | add_history (line_read); |
| 365 | |
| 366 | return (line_read); |
| 367 | #else |
| 368 | char line_read[501]; |
| 369 | |
| 370 | if (prompt != NULL) |
| 371 | fprintf(stdout, "%s", prompt); |
| 372 | if (!fgets(line_read, 500, stdin)) |
| 373 | return(NULL); |
| 374 | line_read[500] = 0; |
| 375 | return(strdup(line_read)); |
| 376 | #endif |
| 377 | } |
| 378 | |
| 379 | /************************************************************************ |
| 380 | * * |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 381 | * I/O Interfaces * |
| 382 | * * |
| 383 | ************************************************************************/ |
| 384 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 385 | static int myRead(FILE *f, char * buf, int len) { |
| 386 | return(fread(buf, 1, len, f)); |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 387 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 388 | static void myClose(FILE *f) { |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 389 | if (f != stdin) { |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 390 | fclose(f); |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 391 | } |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /************************************************************************ |
| 395 | * * |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 396 | * Test processing * |
| 397 | * * |
| 398 | ************************************************************************/ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 399 | static void parseAndPrintFile(char *filename) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 400 | xmlDocPtr doc = NULL, tmp; |
| 401 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 402 | if ((timing) && (!repeat)) |
| 403 | gettimeofday(&begin, NULL); |
| 404 | |
| 405 | |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 406 | if (filename == NULL) { |
| 407 | if (generate) { |
| 408 | xmlNodePtr n; |
| 409 | |
| 410 | doc = xmlNewDoc(BAD_CAST "1.0"); |
| 411 | n = xmlNewNode(NULL, BAD_CAST "info"); |
| 412 | xmlNodeSetContent(n, BAD_CAST "abc"); |
| 413 | xmlDocSetRootElement(doc, n); |
| 414 | } |
| 415 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 416 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 417 | else if (html) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 418 | doc = htmlParseFile(filename, NULL); |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 419 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 420 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 421 | else { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 422 | /* |
| 423 | * build an XML tree from a string; |
| 424 | */ |
| 425 | if (push) { |
| 426 | FILE *f; |
| 427 | |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 428 | /* '-' Usually means stdin -<sven@zen.org> */ |
| 429 | if ((filename[0] == '-') && (filename[1] == 0)) { |
| 430 | f = stdin; |
| 431 | } else { |
| 432 | f = fopen(filename, "r"); |
| 433 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 434 | if (f != NULL) { |
Daniel Veillard | e715dd2 | 2000-08-29 18:29:38 +0000 | [diff] [blame] | 435 | int ret; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 436 | int res, size = 3; |
| 437 | char chars[1024]; |
| 438 | xmlParserCtxtPtr ctxt; |
| 439 | |
| 440 | if (repeat) |
| 441 | size = 1024; |
| 442 | res = fread(chars, 1, 4, f); |
| 443 | if (res > 0) { |
| 444 | ctxt = xmlCreatePushParserCtxt(NULL, NULL, |
| 445 | chars, res, filename); |
| 446 | while ((res = fread(chars, 1, size, f)) > 0) { |
| 447 | xmlParseChunk(ctxt, chars, res, 0); |
| 448 | } |
| 449 | xmlParseChunk(ctxt, chars, 0, 1); |
| 450 | doc = ctxt->myDoc; |
Daniel Veillard | e715dd2 | 2000-08-29 18:29:38 +0000 | [diff] [blame] | 451 | ret = ctxt->wellFormed; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 452 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e715dd2 | 2000-08-29 18:29:38 +0000 | [diff] [blame] | 453 | if (!ret) { |
| 454 | xmlFreeDoc(doc); |
| 455 | doc = NULL; |
| 456 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 459 | } else if (testIO) { |
| 460 | int ret; |
| 461 | FILE *f; |
| 462 | |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 463 | /* '-' Usually means stdin -<sven@zen.org> */ |
| 464 | if ((filename[0] == '-') && (filename[1] == 0)) { |
| 465 | f = stdin; |
| 466 | } else { |
| 467 | f = fopen(filename, "r"); |
| 468 | } |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 469 | if (f != NULL) { |
| 470 | xmlParserCtxtPtr ctxt; |
| 471 | |
| 472 | ctxt = xmlCreateIOParserCtxt(NULL, NULL, |
| 473 | (xmlInputReadCallback) myRead, |
| 474 | (xmlInputCloseCallback) myClose, |
| 475 | f, XML_CHAR_ENCODING_NONE); |
| 476 | xmlParseDocument(ctxt); |
| 477 | |
| 478 | ret = ctxt->wellFormed; |
| 479 | doc = ctxt->myDoc; |
| 480 | xmlFreeParserCtxt(ctxt); |
| 481 | if (!ret) { |
| 482 | xmlFreeDoc(doc); |
| 483 | doc = NULL; |
| 484 | } |
| 485 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 486 | } else if (recovery) { |
| 487 | doc = xmlRecoverFile(filename); |
| 488 | } else if (htmlout) { |
| 489 | int ret; |
| 490 | xmlParserCtxtPtr ctxt; |
| 491 | xmlSAXHandler silent, *old; |
| 492 | |
| 493 | ctxt = xmlCreateFileParserCtxt(filename); |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 494 | |
| 495 | if (ctxt == NULL) { |
| 496 | /* If xmlCreateFileParseCtxt() return NULL something |
| 497 | strange happened so we don't want to do anything. Do |
| 498 | we want to print an error message here? |
| 499 | <sven@zen.org> */ |
Daniel Veillard | 7ebb1ee | 2000-08-04 18:24:45 +0000 | [diff] [blame] | 500 | doc = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 501 | } else { |
| 502 | memcpy(&silent, ctxt->sax, sizeof(silent)); |
| 503 | old = ctxt->sax; |
| 504 | silent.error = xmlHTMLError; |
| 505 | if (xmlGetWarningsDefaultValue) |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 506 | silent.warning = xmlHTMLWarning; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 507 | else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 508 | silent.warning = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 509 | silent.fatalError = xmlHTMLError; |
| 510 | ctxt->sax = &silent; |
| 511 | ctxt->vctxt.error = xmlHTMLValidityError; |
| 512 | if (xmlGetWarningsDefaultValue) |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 513 | ctxt->vctxt.warning = xmlHTMLValidityWarning; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 514 | else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 515 | ctxt->vctxt.warning = NULL; |
| 516 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 517 | xmlParseDocument(ctxt); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 518 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 519 | ret = ctxt->wellFormed; |
| 520 | doc = ctxt->myDoc; |
| 521 | ctxt->sax = old; |
| 522 | xmlFreeParserCtxt(ctxt); |
| 523 | if (!ret) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 524 | xmlFreeDoc(doc); |
| 525 | doc = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 526 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 527 | } |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 528 | #ifdef HAVE_SYS_MMAN_H |
| 529 | } else if (memory) { |
| 530 | int fd; |
| 531 | struct stat info; |
| 532 | const char *base; |
| 533 | if (stat(filename, &info) < 0) |
| 534 | return; |
| 535 | if ((fd = open(filename, O_RDONLY)) < 0) |
| 536 | return; |
| 537 | base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ; |
Daniel Veillard | 2957936 | 2000-08-14 17:57:48 +0000 | [diff] [blame] | 538 | if (base == (void *) MAP_FAILED) |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 539 | return; |
| 540 | |
| 541 | doc = xmlParseMemory((char *) base, info.st_size); |
| 542 | munmap((char *) base, info.st_size); |
| 543 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 544 | } else |
| 545 | doc = xmlParseFile(filename); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 546 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 547 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 548 | /* |
| 549 | * If we don't have a document we might as well give up. Do we |
| 550 | * want an error message here? <sven@zen.org> */ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 551 | if (doc == NULL) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 552 | progresult = 1; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 553 | return; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 556 | if ((timing) && (!repeat)) { |
| 557 | long msec; |
| 558 | gettimeofday(&end, NULL); |
| 559 | msec = end.tv_sec - begin.tv_sec; |
| 560 | msec *= 1000; |
| 561 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 562 | fprintf(stderr, "Parsing took %ld ms\n", msec); |
| 563 | } |
| 564 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 565 | #ifdef LIBXML_XINCLUDE_ENABLED |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 566 | if (xinclude) { |
| 567 | if ((timing) && (!repeat)) { |
| 568 | gettimeofday(&begin, NULL); |
| 569 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 570 | xmlXIncludeProcess(doc); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 571 | if ((timing) && (!repeat)) { |
| 572 | long msec; |
| 573 | gettimeofday(&end, NULL); |
| 574 | msec = end.tv_sec - begin.tv_sec; |
| 575 | msec *= 1000; |
| 576 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 577 | fprintf(stderr, "Xinclude processing took %ld ms\n", msec); |
| 578 | } |
| 579 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 580 | #endif |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 581 | |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 582 | #ifdef LIBXML_DEBUG_ENABLED |
| 583 | /* |
| 584 | * shell interraction |
| 585 | */ |
| 586 | if (shell) |
| 587 | xmlShell(doc, filename, xmlShellReadline, stdout); |
| 588 | #endif |
| 589 | |
| 590 | /* |
| 591 | * test intermediate copy if needed. |
| 592 | */ |
| 593 | if (copy) { |
| 594 | tmp = doc; |
| 595 | doc = xmlCopyDoc(doc, 1); |
| 596 | xmlFreeDoc(tmp); |
| 597 | } |
| 598 | |
| 599 | if ((insert) && (!html)) { |
| 600 | const xmlChar* list[256]; |
| 601 | int nb, i; |
| 602 | xmlNodePtr node; |
| 603 | |
| 604 | if (doc->children != NULL) { |
| 605 | node = doc->children; |
| 606 | while ((node != NULL) && (node->last == NULL)) node = node->next; |
| 607 | if (node != NULL) { |
| 608 | nb = xmlValidGetValidElements(node->last, NULL, list, 256); |
| 609 | if (nb < 0) { |
| 610 | printf("could not get valid list of elements\n"); |
| 611 | } else if (nb == 0) { |
| 612 | printf("No element can be indersted under root\n"); |
| 613 | } else { |
| 614 | printf("%d element types can be indersted under root:\n", |
| 615 | nb); |
| 616 | for (i = 0;i < nb;i++) { |
| 617 | printf("%s\n", list[i]); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | }else if (noout == 0) { |
| 623 | /* |
| 624 | * print it. |
| 625 | */ |
| 626 | #ifdef LIBXML_DEBUG_ENABLED |
| 627 | if (!debug) { |
| 628 | #endif |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 629 | if ((timing) && (!repeat)) { |
| 630 | gettimeofday(&begin, NULL); |
| 631 | } |
Daniel Veillard | 3b2c261 | 2001-04-04 00:09:00 +0000 | [diff] [blame] | 632 | #ifdef HAVE_SYS_MMAN_H |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 633 | if (memory) { |
| 634 | xmlChar *result; |
| 635 | int len; |
| 636 | |
| 637 | if (encoding != NULL) { |
| 638 | xmlDocDumpMemoryEnc(doc, &result, &len, encoding); |
| 639 | } else { |
| 640 | xmlDocDumpMemory(doc, &result, &len); |
| 641 | } |
| 642 | if (result == NULL) { |
| 643 | fprintf(stderr, "Failed to save\n"); |
| 644 | } else { |
| 645 | write(1, result, len); |
| 646 | xmlFree(result); |
| 647 | } |
Daniel Veillard | 3b2c261 | 2001-04-04 00:09:00 +0000 | [diff] [blame] | 648 | } else |
| 649 | #endif /* HAVE_SYS_MMAN_H */ |
| 650 | if (compress) |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 651 | xmlSaveFile("-", doc); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 652 | else if (encoding != NULL) |
| 653 | xmlSaveFileEnc("-", doc, encoding); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 654 | else |
| 655 | xmlDocDump(stdout, doc); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 656 | if ((timing) && (!repeat)) { |
| 657 | long msec; |
| 658 | gettimeofday(&end, NULL); |
| 659 | msec = end.tv_sec - begin.tv_sec; |
| 660 | msec *= 1000; |
| 661 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 662 | fprintf(stderr, "Saving took %ld ms\n", msec); |
| 663 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 664 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 665 | } else { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 666 | xmlDebugDumpDocument(stdout, doc); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 667 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 668 | #endif |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * A posteriori validation test |
| 673 | */ |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 674 | if (dtdvalid != NULL) { |
| 675 | xmlDtdPtr dtd; |
| 676 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 677 | if ((timing) && (!repeat)) { |
| 678 | gettimeofday(&begin, NULL); |
| 679 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 680 | dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 681 | if ((timing) && (!repeat)) { |
| 682 | long msec; |
| 683 | gettimeofday(&end, NULL); |
| 684 | msec = end.tv_sec - begin.tv_sec; |
| 685 | msec *= 1000; |
| 686 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 687 | fprintf(stderr, "Parsing DTD took %ld ms\n", msec); |
| 688 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 689 | if (dtd == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 690 | xmlGenericError(xmlGenericErrorContext, |
| 691 | "Could not parse DTD %s\n", dtdvalid); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 692 | progresult = 2; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 693 | } else { |
| 694 | xmlValidCtxt cvp; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 695 | if ((timing) && (!repeat)) { |
| 696 | gettimeofday(&begin, NULL); |
| 697 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 698 | cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf; |
| 699 | if (!xmlValidateDtd(&cvp, doc, dtd)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 700 | xmlGenericError(xmlGenericErrorContext, |
| 701 | "Document %s does not validate against %s\n", |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 702 | filename, dtdvalid); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 703 | progresult = 3; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 704 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 705 | if ((timing) && (!repeat)) { |
| 706 | long msec; |
| 707 | gettimeofday(&end, NULL); |
| 708 | msec = end.tv_sec - begin.tv_sec; |
| 709 | msec *= 1000; |
| 710 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 711 | fprintf(stderr, "Validating against DTD took %ld ms\n", msec); |
| 712 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 713 | xmlFreeDtd(dtd); |
| 714 | } |
| 715 | } else if (postvalid) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 716 | xmlValidCtxt cvp; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 717 | if ((timing) && (!repeat)) { |
| 718 | gettimeofday(&begin, NULL); |
| 719 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 720 | cvp.userData = (void *) stderr; cvp.error = (xmlValidityErrorFunc) fprintf; cvp.warning = (xmlValidityWarningFunc) fprintf; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 721 | if (!xmlValidateDocument(&cvp, doc)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 722 | xmlGenericError(xmlGenericErrorContext, |
| 723 | "Document %s does not validate\n", filename); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 724 | progresult = 3; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 725 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 726 | if ((timing) && (!repeat)) { |
| 727 | long msec; |
| 728 | gettimeofday(&end, NULL); |
| 729 | msec = end.tv_sec - begin.tv_sec; |
| 730 | msec *= 1000; |
| 731 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 732 | fprintf(stderr, "Validating took %ld ms\n", msec); |
| 733 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | #ifdef LIBXML_DEBUG_ENABLED |
| 737 | if ((debugent) && (!html)) |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 738 | xmlDebugDumpEntities(stderr, doc); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 739 | #endif |
| 740 | |
| 741 | /* |
| 742 | * free it. |
| 743 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 744 | if ((timing) && (!repeat)) { |
| 745 | gettimeofday(&begin, NULL); |
| 746 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 747 | xmlFreeDoc(doc); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 748 | if ((timing) && (!repeat)) { |
| 749 | long msec; |
| 750 | gettimeofday(&end, NULL); |
| 751 | msec = end.tv_sec - begin.tv_sec; |
| 752 | msec *= 1000; |
| 753 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 754 | fprintf(stderr, "Freeing took %ld ms\n", msec); |
| 755 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 758 | int |
| 759 | main(int argc, char **argv) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 760 | int i, count; |
| 761 | int files = 0; |
| 762 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 763 | LIBXML_TEST_VERSION |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 764 | for (i = 1; i < argc ; i++) { |
| 765 | #ifdef LIBXML_DEBUG_ENABLED |
| 766 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 767 | debug++; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 768 | else if ((!strcmp(argv[i], "-shell")) || |
| 769 | (!strcmp(argv[i], "--shell"))) { |
| 770 | shell++; |
| 771 | noout = 1; |
| 772 | } else |
| 773 | #endif |
| 774 | if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 775 | copy++; |
| 776 | else if ((!strcmp(argv[i], "-recover")) || |
| 777 | (!strcmp(argv[i], "--recover"))) |
| 778 | recovery++; |
| 779 | else if ((!strcmp(argv[i], "-noent")) || |
| 780 | (!strcmp(argv[i], "--noent"))) |
| 781 | noent++; |
| 782 | else if ((!strcmp(argv[i], "-noout")) || |
| 783 | (!strcmp(argv[i], "--noout"))) |
| 784 | noout++; |
| 785 | else if ((!strcmp(argv[i], "-htmlout")) || |
| 786 | (!strcmp(argv[i], "--htmlout"))) |
| 787 | htmlout++; |
| 788 | #ifdef LIBXML_HTML_ENABLED |
| 789 | else if ((!strcmp(argv[i], "-html")) || |
| 790 | (!strcmp(argv[i], "--html"))) { |
| 791 | html++; |
| 792 | } |
| 793 | #endif /* LIBXML_HTML_ENABLED */ |
| 794 | else if ((!strcmp(argv[i], "-nowrap")) || |
| 795 | (!strcmp(argv[i], "--nowrap"))) |
| 796 | nowrap++; |
| 797 | else if ((!strcmp(argv[i], "-valid")) || |
| 798 | (!strcmp(argv[i], "--valid"))) |
| 799 | valid++; |
| 800 | else if ((!strcmp(argv[i], "-postvalid")) || |
| 801 | (!strcmp(argv[i], "--postvalid"))) |
| 802 | postvalid++; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 803 | else if ((!strcmp(argv[i], "-dtdvalid")) || |
| 804 | (!strcmp(argv[i], "--dtdvalid"))) { |
| 805 | i++; |
| 806 | dtdvalid = argv[i]; |
| 807 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 808 | else if ((!strcmp(argv[i], "-insert")) || |
| 809 | (!strcmp(argv[i], "--insert"))) |
| 810 | insert++; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 811 | else if ((!strcmp(argv[i], "-timing")) || |
| 812 | (!strcmp(argv[i], "--timing"))) |
| 813 | timing++; |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 814 | else if ((!strcmp(argv[i], "-auto")) || |
| 815 | (!strcmp(argv[i], "--auto"))) |
| 816 | generate++; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 817 | else if ((!strcmp(argv[i], "-repeat")) || |
| 818 | (!strcmp(argv[i], "--repeat"))) |
| 819 | repeat++; |
| 820 | else if ((!strcmp(argv[i], "-push")) || |
| 821 | (!strcmp(argv[i], "--push"))) |
| 822 | push++; |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 823 | #ifdef HAVE_SYS_MMAN_H |
| 824 | else if ((!strcmp(argv[i], "-memory")) || |
| 825 | (!strcmp(argv[i], "--memory"))) |
| 826 | memory++; |
| 827 | #endif |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 828 | else if ((!strcmp(argv[i], "-testIO")) || |
| 829 | (!strcmp(argv[i], "--testIO"))) |
| 830 | testIO++; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 831 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 832 | else if ((!strcmp(argv[i], "-xinclude")) || |
| 833 | (!strcmp(argv[i], "--xinclude"))) |
| 834 | xinclude++; |
| 835 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 836 | else if ((!strcmp(argv[i], "-compress")) || |
| 837 | (!strcmp(argv[i], "--compress"))) { |
| 838 | compress++; |
| 839 | xmlSetCompressMode(9); |
| 840 | } |
| 841 | else if ((!strcmp(argv[i], "-nowarning")) || |
| 842 | (!strcmp(argv[i], "--nowarning"))) { |
| 843 | xmlGetWarningsDefaultValue = 0; |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 844 | xmlPedanticParserDefault(0); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 845 | } |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 846 | else if ((!strcmp(argv[i], "-pedantic")) || |
| 847 | (!strcmp(argv[i], "--pedantic"))) { |
| 848 | xmlGetWarningsDefaultValue = 1; |
| 849 | xmlPedanticParserDefault(1); |
| 850 | } |
Daniel Veillard | 64c20ed | 2000-09-22 16:07:02 +0000 | [diff] [blame] | 851 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 852 | else if ((!strcmp(argv[i], "-debugent")) || |
| 853 | (!strcmp(argv[i], "--debugent"))) { |
| 854 | debugent++; |
| 855 | xmlParserDebugEntities = 1; |
| 856 | } |
Daniel Veillard | 64c20ed | 2000-09-22 16:07:02 +0000 | [diff] [blame] | 857 | #endif |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 858 | else if ((!strcmp(argv[i], "-encode")) || |
| 859 | (!strcmp(argv[i], "--encode"))) { |
| 860 | i++; |
| 861 | encoding = argv[i]; |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 862 | /* |
| 863 | * OK it's for testing purposes |
| 864 | */ |
| 865 | xmlAddEncodingAlias("UTF-8", "DVEnc"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 866 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 867 | else if ((!strcmp(argv[i], "-noblanks")) || |
| 868 | (!strcmp(argv[i], "--noblanks"))) { |
| 869 | noblanks++; |
| 870 | xmlKeepBlanksDefault(0); |
| 871 | } |
| 872 | } |
| 873 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
| 874 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
| 875 | if ((htmlout) && (!nowrap)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 876 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 877 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n"); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 878 | xmlGenericError(xmlGenericErrorContext, |
| 879 | "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"); |
| 880 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 881 | "<html><head><title>%s output</title></head>\n", |
| 882 | argv[0]); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 883 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 884 | "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n", |
| 885 | argv[0]); |
| 886 | } |
| 887 | for (i = 1; i < argc ; i++) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 888 | if ((!strcmp(argv[i], "-encode")) || |
| 889 | (!strcmp(argv[i], "--encode"))) { |
| 890 | i++; |
| 891 | continue; |
| 892 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 893 | if ((!strcmp(argv[i], "-dtdvalid")) || |
| 894 | (!strcmp(argv[i], "--dtdvalid"))) { |
| 895 | i++; |
| 896 | continue; |
| 897 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 898 | if ((timing) && (repeat)) |
| 899 | gettimeofday(&begin, NULL); |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 900 | /* Remember file names. "-" means stding. <sven@zen.org> */ |
| 901 | if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 902 | if (repeat) { |
| 903 | for (count = 0;count < 100 * repeat;count++) |
| 904 | parseAndPrintFile(argv[i]); |
| 905 | } else |
| 906 | parseAndPrintFile(argv[i]); |
| 907 | files ++; |
| 908 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 909 | if ((timing) && (repeat)) { |
| 910 | long msec; |
| 911 | gettimeofday(&end, NULL); |
| 912 | msec = end.tv_sec - begin.tv_sec; |
| 913 | msec *= 1000; |
| 914 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 915 | fprintf(stderr, "100 iteration took %ld ms\n", msec); |
| 916 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 917 | } |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 918 | if (generate) |
| 919 | parseAndPrintFile(NULL); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 920 | if ((htmlout) && (!nowrap)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 921 | xmlGenericError(xmlGenericErrorContext, "</body></html>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 922 | } |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 923 | if ((files == 0) && (!generate)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 924 | printf("Usage : %s [options] XMLfiles ...\n", |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 925 | argv[0]); |
| 926 | printf("\tParse the XML files and output the result of the parsing\n"); |
| 927 | #ifdef LIBXML_DEBUG_ENABLED |
| 928 | printf("\t--debug : dump a debug tree of the in-memory document\n"); |
| 929 | printf("\t--shell : run a navigating shell\n"); |
| 930 | printf("\t--debugent : debug the entities defined in the document\n"); |
| 931 | #endif |
| 932 | printf("\t--copy : used to test the internal copy implementation\n"); |
| 933 | printf("\t--recover : output what was parsable on broken XML documents\n"); |
| 934 | printf("\t--noent : substitute entity references by their value\n"); |
| 935 | printf("\t--noout : don't output the result tree\n"); |
| 936 | printf("\t--htmlout : output results as HTML\n"); |
| 937 | printf("\t--nowarp : do not put HTML doc wrapper\n"); |
| 938 | printf("\t--valid : validate the document in addition to std well-formed check\n"); |
| 939 | printf("\t--postvalid : do a posteriori validation, i.e after parsing\n"); |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 940 | printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 941 | printf("\t--timing : print some timings\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 942 | printf("\t--repeat : repeat 100 times, for timing or profiling\n"); |
| 943 | printf("\t--insert : ad-hoc test for valid insertions\n"); |
| 944 | printf("\t--compress : turn on gzip compression of output\n"); |
| 945 | #ifdef LIBXML_HTML_ENABLED |
| 946 | printf("\t--html : use the HTML parser\n"); |
| 947 | #endif |
| 948 | printf("\t--push : use the push mode of the parser\n"); |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 949 | #ifdef HAVE_SYS_MMAN_H |
| 950 | printf("\t--memory : parse from memory\n"); |
| 951 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 952 | printf("\t--nowarning : do not emit warnings from parser/validator\n"); |
| 953 | printf("\t--noblanks : drop (ignorable?) blanks spaces\n"); |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 954 | printf("\t--testIO : test user I/O support\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 955 | printf("\t--encode encoding : output in the given encoding\n"); |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 956 | printf("\t--auto : generate a small doc on the fly\n"); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 957 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 958 | printf("\t--xinclude : do XInclude processing\n"); |
| 959 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 960 | } |
| 961 | xmlCleanupParser(); |
| 962 | xmlMemoryDump(); |
| 963 | |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 964 | return(progresult); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 965 | } |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 966 | |