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