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