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 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * daniel@veillard.com |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 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 | 28ae636 | 2001-07-14 16:44:32 +0000 | [diff] [blame] | 13 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | |
Daniel Veillard | 3c5ed91 | 2002-01-08 10:36:16 +0000 | [diff] [blame] | 16 | #if defined (_WIN32) && !defined(__CYGWIN__) |
Daniel Veillard | 2d90de4 | 2001-04-16 17:46:18 +0000 | [diff] [blame] | 17 | #ifdef _MSC_VER |
| 18 | #include <winsock2.h> |
| 19 | #pragma comment(lib, "ws2_32.lib") |
| 20 | #define gettimeofday(p1,p2) |
Daniel Veillard | f216d46 | 2002-02-08 13:44:24 +0000 | [diff] [blame] | 21 | #include <time.h> |
Daniel Veillard | 28ae636 | 2001-07-14 16:44:32 +0000 | [diff] [blame] | 22 | #else /* _MSC_VER */ |
| 23 | #include <sys/time.h> |
Daniel Veillard | 2d90de4 | 2001-04-16 17:46:18 +0000 | [diff] [blame] | 24 | #endif /* _MSC_VER */ |
| 25 | #else /* _WIN32 */ |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 26 | #ifdef HAVE_SYS_TIME_H |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 27 | #include <sys/time.h> |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 28 | #endif |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 29 | #ifdef HAVE_TIME_H |
| 30 | #include <time.h> |
| 31 | #endif |
Daniel Veillard | 2d90de4 | 2001-04-16 17:46:18 +0000 | [diff] [blame] | 32 | #endif /* _WIN32 */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 33 | |
Daniel Veillard | 90bc371 | 2002-03-07 15:12:58 +0000 | [diff] [blame] | 34 | #ifdef HAVE_SYS_TIMEB_H |
| 35 | #include <sys/timeb.h> |
| 36 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 37 | |
| 38 | #ifdef HAVE_SYS_TYPES_H |
| 39 | #include <sys/types.h> |
| 40 | #endif |
| 41 | #ifdef HAVE_SYS_STAT_H |
| 42 | #include <sys/stat.h> |
| 43 | #endif |
| 44 | #ifdef HAVE_FCNTL_H |
| 45 | #include <fcntl.h> |
| 46 | #endif |
| 47 | #ifdef HAVE_UNISTD_H |
| 48 | #include <unistd.h> |
| 49 | #endif |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 50 | #ifdef HAVE_SYS_MMAN_H |
| 51 | #include <sys/mman.h> |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 52 | /* seems needed for Solaris */ |
| 53 | #ifndef MAP_FAILED |
| 54 | #define MAP_FAILED ((void *) -1) |
| 55 | #endif |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 56 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 57 | #ifdef HAVE_STDLIB_H |
| 58 | #include <stdlib.h> |
| 59 | #endif |
| 60 | #ifdef HAVE_LIBREADLINE |
| 61 | #include <readline/readline.h> |
| 62 | #ifdef HAVE_LIBHISTORY |
| 63 | #include <readline/history.h> |
| 64 | #endif |
| 65 | #endif |
| 66 | |
| 67 | #include <libxml/xmlmemory.h> |
| 68 | #include <libxml/parser.h> |
| 69 | #include <libxml/parserInternals.h> |
| 70 | #include <libxml/HTMLparser.h> |
| 71 | #include <libxml/HTMLtree.h> |
| 72 | #include <libxml/tree.h> |
| 73 | #include <libxml/xpath.h> |
| 74 | #include <libxml/debugXML.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 75 | #include <libxml/xmlerror.h> |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 76 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 77 | #include <libxml/xinclude.h> |
| 78 | #endif |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 79 | #ifdef LIBXML_CATALOG_ENABLED |
| 80 | #include <libxml/catalog.h> |
| 81 | #endif |
Daniel Veillard | 89cad53 | 2001-10-22 09:46:13 +0000 | [diff] [blame] | 82 | #ifdef LIBXML_DOCB_ENABLED |
| 83 | #include <libxml/DOCBparser.h> |
| 84 | #endif |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 85 | #include <libxml/globals.h> |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 86 | #include <libxml/xmlreader.h> |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 87 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 88 | #include <libxml/relaxng.h> |
| 89 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 90 | |
Daniel Veillard | 3be2751 | 2003-01-26 19:49:04 +0000 | [diff] [blame] | 91 | #ifndef XML_XML_DEFAULT_CATALOG |
| 92 | #define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog" |
| 93 | #endif |
| 94 | |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 95 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 96 | static int shell = 0; |
| 97 | static int debugent = 0; |
| 98 | #endif |
Daniel Veillard | 8326e73 | 2003-01-07 00:19:07 +0000 | [diff] [blame] | 99 | static int debug = 0; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 100 | static int copy = 0; |
| 101 | static int recovery = 0; |
| 102 | static int noent = 0; |
| 103 | static int noout = 0; |
| 104 | static int nowrap = 0; |
| 105 | static int valid = 0; |
| 106 | static int postvalid = 0; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 107 | static char * dtdvalid = NULL; |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 108 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 109 | static char * relaxng = NULL; |
| 110 | static xmlRelaxNGPtr relaxngschemas = NULL; |
| 111 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 112 | static int repeat = 0; |
| 113 | static int insert = 0; |
| 114 | static int compress = 0; |
Daniel Veillard | 89cad53 | 2001-10-22 09:46:13 +0000 | [diff] [blame] | 115 | #ifdef LIBXML_DOCB_ENABLED |
| 116 | static int sgml = 0; |
| 117 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 118 | static int html = 0; |
| 119 | static int htmlout = 0; |
| 120 | static int push = 0; |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 121 | #ifdef HAVE_SYS_MMAN_H |
| 122 | static int memory = 0; |
| 123 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 124 | static int noblanks = 0; |
Daniel Veillard | 90493a9 | 2001-08-14 14:12:47 +0000 | [diff] [blame] | 125 | static int format = 0; |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 126 | static int testIO = 0; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 127 | static char *encoding = NULL; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 128 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 129 | static int xinclude = 0; |
| 130 | #endif |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 131 | static int dtdattrs = 0; |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 132 | static int loaddtd = 0; |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 133 | static int progresult = 0; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 134 | static int timing = 0; |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 135 | static int generate = 0; |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 136 | static int dropdtd = 0; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 137 | #ifdef LIBXML_CATALOG_ENABLED |
| 138 | static int catalogs = 0; |
| 139 | static int nocatalogs = 0; |
| 140 | #endif |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 141 | static int stream = 0; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 142 | static int chkregister = 0; |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 143 | static const char *output = NULL; |
| 144 | |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 145 | |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 146 | /* |
| 147 | * Internal timing routines to remove the necessity to have unix-specific |
| 148 | * function calls |
| 149 | */ |
| 150 | |
Daniel Veillard | 8c1ae60 | 2002-03-07 11:21:00 +0000 | [diff] [blame] | 151 | #ifndef HAVE_GETTIMEOFDAY |
| 152 | #ifdef HAVE_SYS_TIMEB_H |
| 153 | #ifdef HAVE_SYS_TIME_H |
| 154 | #ifdef HAVE_FTIME |
| 155 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 156 | static int |
Daniel Veillard | 8c1ae60 | 2002-03-07 11:21:00 +0000 | [diff] [blame] | 157 | my_gettimeofday(struct timeval *tvp, void *tzp) |
| 158 | { |
| 159 | struct timeb timebuffer; |
| 160 | |
| 161 | ftime(&timebuffer); |
| 162 | if (tvp) { |
| 163 | tvp->tv_sec = timebuffer.time; |
| 164 | tvp->tv_usec = timebuffer.millitm * 1000L; |
| 165 | } |
| 166 | return (0); |
| 167 | } |
| 168 | #define HAVE_GETTIMEOFDAY 1 |
| 169 | #define gettimeofday my_gettimeofday |
| 170 | |
| 171 | #endif /* HAVE_FTIME */ |
| 172 | #endif /* HAVE_SYS_TIME_H */ |
| 173 | #endif /* HAVE_SYS_TIMEB_H */ |
| 174 | #endif /* !HAVE_GETTIMEOFDAY */ |
| 175 | |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 176 | #if defined(HAVE_GETTIMEOFDAY) |
| 177 | static struct timeval begin, end; |
| 178 | |
| 179 | /* |
| 180 | * startTimer: call where you want to start timing |
| 181 | */ |
| 182 | static void |
| 183 | startTimer(void) |
| 184 | { |
| 185 | gettimeofday(&begin, NULL); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * endTimer: call where you want to stop timing and to print out a |
| 190 | * message about the timing performed; format is a printf |
| 191 | * type argument |
| 192 | */ |
| 193 | static void |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 194 | endTimer(const char *fmt, ...) |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 195 | { |
| 196 | long msec; |
| 197 | va_list ap; |
| 198 | |
| 199 | gettimeofday(&end, NULL); |
| 200 | msec = end.tv_sec - begin.tv_sec; |
| 201 | msec *= 1000; |
| 202 | msec += (end.tv_usec - begin.tv_usec) / 1000; |
| 203 | |
| 204 | #ifndef HAVE_STDARG_H |
| 205 | #error "endTimer required stdarg functions" |
| 206 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 207 | va_start(ap, fmt); |
| 208 | vfprintf(stderr, fmt, ap); |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 209 | va_end(ap); |
| 210 | |
| 211 | fprintf(stderr, " took %ld ms\n", msec); |
| 212 | } |
| 213 | #elif defined(HAVE_TIME_H) |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 214 | /* |
| 215 | * No gettimeofday function, so we have to make do with calling clock. |
| 216 | * This is obviously less accurate, but there's little we can do about |
| 217 | * that. |
| 218 | */ |
Daniel Veillard | 90bc371 | 2002-03-07 15:12:58 +0000 | [diff] [blame] | 219 | #ifndef CLOCKS_PER_SEC |
| 220 | #define CLOCKS_PER_SEC 100 |
| 221 | #endif |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 222 | |
| 223 | static clock_t begin, end; |
| 224 | static void |
| 225 | startTimer(void) |
| 226 | { |
| 227 | begin = clock(); |
| 228 | } |
| 229 | static void |
| 230 | endTimer(const char *fmt, ...) |
| 231 | { |
| 232 | long msec; |
| 233 | va_list ap; |
| 234 | |
| 235 | end = clock(); |
| 236 | msec = ((end - begin) * 1000) / CLOCKS_PER_SEC; |
| 237 | |
| 238 | #ifndef HAVE_STDARG_H |
| 239 | #error "endTimer required stdarg functions" |
| 240 | #endif |
| 241 | va_start(ap, fmt); |
| 242 | vfprintf(stderr, fmt, ap); |
| 243 | va_end(ap); |
| 244 | fprintf(stderr, " took %ld ms\n", msec); |
| 245 | } |
| 246 | #else |
| 247 | |
| 248 | /* |
| 249 | * We don't have a gettimeofday or time.h, so we just don't do timing |
| 250 | */ |
| 251 | static void |
| 252 | startTimer(void) |
| 253 | { |
| 254 | /* |
| 255 | * Do nothing |
| 256 | */ |
| 257 | } |
| 258 | static void |
| 259 | endTimer(char *format, ...) |
| 260 | { |
| 261 | /* |
| 262 | * We cannot do anything because we don't have a timing function |
| 263 | */ |
| 264 | #ifdef HAVE_STDARG_H |
| 265 | va_start(ap, format); |
| 266 | vfprintf(stderr, format, ap); |
| 267 | va_end(ap); |
| 268 | fprintf(stderr, " was not timed\n", msec); |
| 269 | #else |
| 270 | /* We don't have gettimeofday, time or stdarg.h, what crazy world is |
| 271 | * this ?! |
| 272 | */ |
| 273 | #endif |
| 274 | } |
| 275 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 276 | /************************************************************************ |
| 277 | * * |
| 278 | * HTML ouput * |
| 279 | * * |
| 280 | ************************************************************************/ |
| 281 | char buffer[50000]; |
| 282 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 283 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 284 | xmlHTMLEncodeSend(void) { |
| 285 | char *result; |
| 286 | |
| 287 | result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer); |
| 288 | if (result) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 289 | xmlGenericError(xmlGenericErrorContext, "%s", result); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 290 | xmlFree(result); |
| 291 | } |
| 292 | buffer[0] = 0; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * xmlHTMLPrintFileInfo: |
| 297 | * @input: an xmlParserInputPtr input |
| 298 | * |
| 299 | * Displays the associated file and line informations for the current input |
| 300 | */ |
| 301 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 302 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 303 | xmlHTMLPrintFileInfo(xmlParserInputPtr input) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 304 | int len; |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 305 | xmlGenericError(xmlGenericErrorContext, "<p>"); |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 306 | |
| 307 | len = strlen(buffer); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 308 | if (input != NULL) { |
| 309 | if (input->filename) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 310 | snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename, |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 311 | input->line); |
| 312 | } else { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 313 | snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | xmlHTMLEncodeSend(); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * xmlHTMLPrintFileContext: |
| 321 | * @input: an xmlParserInputPtr input |
| 322 | * |
| 323 | * Displays current context within the input content for error tracking |
| 324 | */ |
| 325 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 326 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 327 | xmlHTMLPrintFileContext(xmlParserInputPtr input) { |
| 328 | const xmlChar *cur, *base; |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 329 | int len; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 330 | int n; |
| 331 | |
| 332 | if (input == NULL) return; |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 333 | xmlGenericError(xmlGenericErrorContext, "<pre>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 334 | cur = input->cur; |
| 335 | base = input->base; |
| 336 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
| 337 | cur--; |
| 338 | } |
| 339 | n = 0; |
| 340 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
| 341 | cur--; |
| 342 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
| 343 | base = cur; |
| 344 | n = 0; |
| 345 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 346 | len = strlen(buffer); |
| 347 | snprintf(&buffer[len], sizeof(buffer) - len, "%c", |
| 348 | (unsigned char) *cur++); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 349 | n++; |
| 350 | } |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 351 | len = strlen(buffer); |
| 352 | snprintf(&buffer[len], sizeof(buffer) - len, "\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 353 | cur = input->cur; |
| 354 | while ((*cur == '\n') || (*cur == '\r')) |
| 355 | cur--; |
| 356 | n = 0; |
| 357 | while ((cur != base) && (n++ < 80)) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 358 | len = strlen(buffer); |
| 359 | snprintf(&buffer[len], sizeof(buffer) - len, " "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 360 | base++; |
| 361 | } |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 362 | len = strlen(buffer); |
| 363 | snprintf(&buffer[len], sizeof(buffer) - len, "^\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 364 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 365 | xmlGenericError(xmlGenericErrorContext, "</pre>"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
| 369 | * xmlHTMLError: |
| 370 | * @ctx: an XML parser context |
| 371 | * @msg: the message to display/transmit |
| 372 | * @...: extra parameters for the message display |
| 373 | * |
| 374 | * Display and format an error messages, gives file, line, position and |
| 375 | * extra parameters. |
| 376 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 377 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 378 | xmlHTMLError(void *ctx, const char *msg, ...) |
| 379 | { |
| 380 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 381 | xmlParserInputPtr input; |
| 382 | xmlParserInputPtr cur = NULL; |
| 383 | va_list args; |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 384 | int len; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 385 | |
| 386 | buffer[0] = 0; |
| 387 | input = ctxt->input; |
| 388 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
| 389 | cur = input; |
| 390 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 391 | } |
| 392 | |
| 393 | xmlHTMLPrintFileInfo(input); |
| 394 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 395 | xmlGenericError(xmlGenericErrorContext, "<b>error</b>: "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 396 | va_start(args, msg); |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 397 | len = strlen(buffer); |
| 398 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 399 | va_end(args); |
| 400 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 401 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 402 | |
| 403 | xmlHTMLPrintFileContext(input); |
| 404 | xmlHTMLEncodeSend(); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * xmlHTMLWarning: |
| 409 | * @ctx: an XML parser context |
| 410 | * @msg: the message to display/transmit |
| 411 | * @...: extra parameters for the message display |
| 412 | * |
| 413 | * Display and format a warning messages, gives file, line, position and |
| 414 | * extra parameters. |
| 415 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 416 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 417 | xmlHTMLWarning(void *ctx, const char *msg, ...) |
| 418 | { |
| 419 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 420 | xmlParserInputPtr input; |
| 421 | xmlParserInputPtr cur = NULL; |
| 422 | va_list args; |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 423 | int len; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 424 | |
| 425 | buffer[0] = 0; |
| 426 | input = ctxt->input; |
| 427 | if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) { |
| 428 | cur = input; |
| 429 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | xmlHTMLPrintFileInfo(input); |
| 434 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 435 | xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 436 | va_start(args, msg); |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 437 | len = strlen(buffer); |
| 438 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 439 | va_end(args); |
| 440 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 441 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 442 | |
| 443 | xmlHTMLPrintFileContext(input); |
| 444 | xmlHTMLEncodeSend(); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * xmlHTMLValidityError: |
| 449 | * @ctx: an XML parser context |
| 450 | * @msg: the message to display/transmit |
| 451 | * @...: extra parameters for the message display |
| 452 | * |
| 453 | * Display and format an validity error messages, gives file, |
| 454 | * line, position and extra parameters. |
| 455 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 456 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 457 | xmlHTMLValidityError(void *ctx, const char *msg, ...) |
| 458 | { |
| 459 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 460 | xmlParserInputPtr input; |
| 461 | va_list args; |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 462 | int len; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 463 | |
| 464 | buffer[0] = 0; |
| 465 | input = ctxt->input; |
| 466 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 467 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 468 | |
| 469 | xmlHTMLPrintFileInfo(input); |
| 470 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 471 | xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: "); |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 472 | len = strlen(buffer); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 473 | va_start(args, msg); |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 474 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 475 | va_end(args); |
| 476 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 477 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 478 | |
| 479 | xmlHTMLPrintFileContext(input); |
| 480 | xmlHTMLEncodeSend(); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * xmlHTMLValidityWarning: |
| 485 | * @ctx: an XML parser context |
| 486 | * @msg: the message to display/transmit |
| 487 | * @...: extra parameters for the message display |
| 488 | * |
| 489 | * Display and format a validity warning messages, gives file, line, |
| 490 | * position and extra parameters. |
| 491 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 492 | static void |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 493 | xmlHTMLValidityWarning(void *ctx, const char *msg, ...) |
| 494 | { |
| 495 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 496 | xmlParserInputPtr input; |
| 497 | va_list args; |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 498 | int len; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 499 | |
| 500 | buffer[0] = 0; |
| 501 | input = ctxt->input; |
| 502 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 503 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 504 | |
| 505 | xmlHTMLPrintFileInfo(input); |
| 506 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 507 | xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: "); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 508 | va_start(args, msg); |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 509 | len = strlen(buffer); |
| 510 | vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 511 | va_end(args); |
| 512 | xmlHTMLEncodeSend(); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 513 | xmlGenericError(xmlGenericErrorContext, "</p>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 514 | |
| 515 | xmlHTMLPrintFileContext(input); |
| 516 | xmlHTMLEncodeSend(); |
| 517 | } |
| 518 | |
| 519 | /************************************************************************ |
| 520 | * * |
| 521 | * Shell Interface * |
| 522 | * * |
| 523 | ************************************************************************/ |
Daniel Veillard | 56ada1d | 2003-01-07 11:17:25 +0000 | [diff] [blame] | 524 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 525 | /** |
| 526 | * xmlShellReadline: |
| 527 | * @prompt: the prompt value |
| 528 | * |
| 529 | * Read a string |
| 530 | * |
| 531 | * Returns a pointer to it or NULL on EOF the caller is expected to |
| 532 | * free the returned string. |
| 533 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 534 | static char * |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 535 | xmlShellReadline(char *prompt) { |
| 536 | #ifdef HAVE_LIBREADLINE |
| 537 | char *line_read; |
| 538 | |
| 539 | /* Get a line from the user. */ |
| 540 | line_read = readline (prompt); |
| 541 | |
| 542 | /* If the line has any text in it, save it on the history. */ |
| 543 | if (line_read && *line_read) |
| 544 | add_history (line_read); |
| 545 | |
| 546 | return (line_read); |
| 547 | #else |
| 548 | char line_read[501]; |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 549 | char *ret; |
| 550 | int len; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 551 | |
| 552 | if (prompt != NULL) |
| 553 | fprintf(stdout, "%s", prompt); |
| 554 | if (!fgets(line_read, 500, stdin)) |
| 555 | return(NULL); |
| 556 | line_read[500] = 0; |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 557 | len = strlen(line_read); |
| 558 | ret = (char *) malloc(len + 1); |
| 559 | if (ret != NULL) { |
| 560 | memcpy (ret, line_read, len + 1); |
| 561 | } |
| 562 | return(ret); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 563 | #endif |
| 564 | } |
Daniel Veillard | 56ada1d | 2003-01-07 11:17:25 +0000 | [diff] [blame] | 565 | #endif /* LIBXML_DEBUG_ENABLED */ |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 566 | |
| 567 | /************************************************************************ |
| 568 | * * |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 569 | * I/O Interfaces * |
| 570 | * * |
| 571 | ************************************************************************/ |
| 572 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 573 | static int myRead(FILE *f, char * buf, int len) { |
| 574 | return(fread(buf, 1, len, f)); |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 575 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 576 | static void myClose(FILE *f) { |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 577 | if (f != stdin) { |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 578 | fclose(f); |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 579 | } |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /************************************************************************ |
| 583 | * * |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 584 | * Stream Test processing * |
| 585 | * * |
| 586 | ************************************************************************/ |
| 587 | static int count = 0; |
| 588 | static int elem, attrs; |
| 589 | |
| 590 | static void processNode(xmlTextReaderPtr reader) { |
Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 591 | xmlChar *name, *value; |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 592 | |
Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 593 | name = xmlTextReaderName(reader); |
| 594 | if (name == NULL) |
| 595 | name = xmlStrdup(BAD_CAST "--"); |
Daniel Veillard | 99737f5 | 2003-03-22 14:55:50 +0000 | [diff] [blame] | 596 | |
Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 597 | value = xmlTextReaderValue(reader); |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 598 | |
Daniel Veillard | 99737f5 | 2003-03-22 14:55:50 +0000 | [diff] [blame] | 599 | printf("%d %d %s %d %d", |
Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 600 | xmlTextReaderDepth(reader), |
| 601 | xmlTextReaderNodeType(reader), |
| 602 | name, |
Daniel Veillard | 99737f5 | 2003-03-22 14:55:50 +0000 | [diff] [blame] | 603 | xmlTextReaderIsEmptyElement(reader), |
| 604 | xmlTextReaderHasValue(reader)); |
Daniel Veillard | e59494f | 2003-01-04 16:35:29 +0000 | [diff] [blame] | 605 | xmlFree(name); |
| 606 | if (value == NULL) |
| 607 | printf("\n"); |
| 608 | else { |
| 609 | printf(" %s\n", value); |
| 610 | xmlFree(value); |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
| 614 | static void streamFile(char *filename) { |
| 615 | xmlTextReaderPtr reader; |
| 616 | int ret; |
| 617 | |
| 618 | if (count) { |
| 619 | elem = 0; |
| 620 | attrs = 0; |
| 621 | } |
| 622 | |
| 623 | reader = xmlNewTextReaderFilename(filename); |
| 624 | if (reader != NULL) { |
| 625 | if (valid) |
| 626 | xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 627 | else |
| 628 | xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1); |
| 629 | if (relaxng != NULL) { |
| 630 | if (timing) { |
| 631 | startTimer(); |
| 632 | } |
| 633 | ret = xmlTextReaderRelaxNGValidate(reader, relaxng); |
| 634 | if (ret < 0) { |
| 635 | xmlGenericError(xmlGenericErrorContext, |
| 636 | "Relax-NG schema %s failed to compile\n", relaxng); |
| 637 | relaxng = NULL; |
| 638 | } |
| 639 | if (timing) { |
| 640 | endTimer("Compiling the schemas"); |
| 641 | } |
| 642 | } |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Process all nodes in sequence |
| 646 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 647 | if (timing) { |
| 648 | startTimer(); |
| 649 | } |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 650 | ret = xmlTextReaderRead(reader); |
| 651 | while (ret == 1) { |
| 652 | if (debug) |
| 653 | processNode(reader); |
| 654 | ret = xmlTextReaderRead(reader); |
| 655 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 656 | if (timing) { |
| 657 | if ((valid) || (relaxng != NULL)) |
| 658 | endTimer("Parsing and validating"); |
| 659 | else |
| 660 | endTimer("Parsing"); |
| 661 | } |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 662 | |
Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 663 | if (valid) { |
| 664 | if (xmlTextReaderIsValid(reader) != 1) { |
| 665 | xmlGenericError(xmlGenericErrorContext, |
| 666 | "Document %s does not validate\n", filename); |
| 667 | progresult = 3; |
| 668 | } |
| 669 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 670 | if (relaxng != NULL) { |
| 671 | if (xmlTextReaderIsValid(reader) != 1) { |
| 672 | printf("%s fails to validate\n", filename); |
| 673 | progresult = 3; |
| 674 | } else { |
| 675 | printf("%s validates\n", filename); |
| 676 | } |
| 677 | } |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 678 | /* |
| 679 | * Done, cleanup and status |
| 680 | */ |
| 681 | xmlFreeTextReader(reader); |
| 682 | if (ret != 0) { |
| 683 | printf("%s : failed to parse\n", filename); |
Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 684 | progresult = 1; |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 685 | } |
| 686 | } else { |
| 687 | fprintf(stderr, "Unable to open %s\n", filename); |
Daniel Veillard | f6bad79 | 2003-04-11 19:38:54 +0000 | [diff] [blame] | 688 | progresult = 1; |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | /************************************************************************ |
| 693 | * * |
| 694 | * Tree Test processing * |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 695 | * * |
| 696 | ************************************************************************/ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 697 | static void parseAndPrintFile(char *filename) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 698 | xmlDocPtr doc = NULL, tmp; |
| 699 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 700 | if ((timing) && (!repeat)) |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 701 | startTimer(); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 702 | |
| 703 | |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 704 | if (filename == NULL) { |
| 705 | if (generate) { |
| 706 | xmlNodePtr n; |
| 707 | |
| 708 | doc = xmlNewDoc(BAD_CAST "1.0"); |
| 709 | n = xmlNewNode(NULL, BAD_CAST "info"); |
| 710 | xmlNodeSetContent(n, BAD_CAST "abc"); |
| 711 | xmlDocSetRootElement(doc, n); |
| 712 | } |
| 713 | } |
Daniel Veillard | 89cad53 | 2001-10-22 09:46:13 +0000 | [diff] [blame] | 714 | #ifdef LIBXML_DOCB_ENABLED |
| 715 | /* |
| 716 | * build an SGML tree from a string; |
| 717 | */ |
| 718 | else if ((sgml) && (push)) { |
| 719 | FILE *f; |
| 720 | |
| 721 | f = fopen(filename, "r"); |
| 722 | if (f != NULL) { |
| 723 | int res, size = 3; |
| 724 | char chars[4096]; |
| 725 | docbParserCtxtPtr ctxt; |
| 726 | |
| 727 | /* if (repeat) */ |
| 728 | size = 4096; |
| 729 | res = fread(chars, 1, 4, f); |
| 730 | if (res > 0) { |
| 731 | ctxt = docbCreatePushParserCtxt(NULL, NULL, |
| 732 | chars, res, filename, 0); |
| 733 | while ((res = fread(chars, 1, size, f)) > 0) { |
| 734 | docbParseChunk(ctxt, chars, res, 0); |
| 735 | } |
| 736 | docbParseChunk(ctxt, chars, 0, 1); |
| 737 | doc = ctxt->myDoc; |
| 738 | docbFreeParserCtxt(ctxt); |
| 739 | } |
| 740 | fclose(f); |
| 741 | } |
| 742 | } else if (sgml) { |
| 743 | doc = docbParseFile(filename, NULL); |
| 744 | } |
| 745 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 746 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 747 | else if (html) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 748 | doc = htmlParseFile(filename, NULL); |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 749 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 750 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 751 | else { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 752 | /* |
| 753 | * build an XML tree from a string; |
| 754 | */ |
| 755 | if (push) { |
| 756 | FILE *f; |
| 757 | |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 758 | /* '-' Usually means stdin -<sven@zen.org> */ |
| 759 | if ((filename[0] == '-') && (filename[1] == 0)) { |
| 760 | f = stdin; |
| 761 | } else { |
| 762 | f = fopen(filename, "r"); |
| 763 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 764 | if (f != NULL) { |
Daniel Veillard | e715dd2 | 2000-08-29 18:29:38 +0000 | [diff] [blame] | 765 | int ret; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 766 | int res, size = 3; |
| 767 | char chars[1024]; |
| 768 | xmlParserCtxtPtr ctxt; |
| 769 | |
| 770 | if (repeat) |
| 771 | size = 1024; |
| 772 | res = fread(chars, 1, 4, f); |
| 773 | if (res > 0) { |
| 774 | ctxt = xmlCreatePushParserCtxt(NULL, NULL, |
| 775 | chars, res, filename); |
| 776 | while ((res = fread(chars, 1, size, f)) > 0) { |
| 777 | xmlParseChunk(ctxt, chars, res, 0); |
| 778 | } |
| 779 | xmlParseChunk(ctxt, chars, 0, 1); |
| 780 | doc = ctxt->myDoc; |
Daniel Veillard | e715dd2 | 2000-08-29 18:29:38 +0000 | [diff] [blame] | 781 | ret = ctxt->wellFormed; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 782 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e715dd2 | 2000-08-29 18:29:38 +0000 | [diff] [blame] | 783 | if (!ret) { |
| 784 | xmlFreeDoc(doc); |
| 785 | doc = NULL; |
| 786 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 789 | } else if (testIO) { |
| 790 | int ret; |
| 791 | FILE *f; |
| 792 | |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 793 | /* '-' Usually means stdin -<sven@zen.org> */ |
| 794 | if ((filename[0] == '-') && (filename[1] == 0)) { |
| 795 | f = stdin; |
| 796 | } else { |
| 797 | f = fopen(filename, "r"); |
| 798 | } |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 799 | if (f != NULL) { |
| 800 | xmlParserCtxtPtr ctxt; |
| 801 | |
| 802 | ctxt = xmlCreateIOParserCtxt(NULL, NULL, |
| 803 | (xmlInputReadCallback) myRead, |
| 804 | (xmlInputCloseCallback) myClose, |
| 805 | f, XML_CHAR_ENCODING_NONE); |
| 806 | xmlParseDocument(ctxt); |
| 807 | |
| 808 | ret = ctxt->wellFormed; |
| 809 | doc = ctxt->myDoc; |
| 810 | xmlFreeParserCtxt(ctxt); |
| 811 | if (!ret) { |
| 812 | xmlFreeDoc(doc); |
| 813 | doc = NULL; |
| 814 | } |
| 815 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 816 | } else if (recovery) { |
| 817 | doc = xmlRecoverFile(filename); |
| 818 | } else if (htmlout) { |
| 819 | int ret; |
| 820 | xmlParserCtxtPtr ctxt; |
| 821 | xmlSAXHandler silent, *old; |
| 822 | |
| 823 | ctxt = xmlCreateFileParserCtxt(filename); |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 824 | |
| 825 | if (ctxt == NULL) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 826 | /* If xmlCreateFileParserCtxt() return NULL something |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 827 | strange happened so we don't want to do anything. Do |
| 828 | we want to print an error message here? |
| 829 | <sven@zen.org> */ |
Daniel Veillard | 7ebb1ee | 2000-08-04 18:24:45 +0000 | [diff] [blame] | 830 | doc = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 831 | } else { |
| 832 | memcpy(&silent, ctxt->sax, sizeof(silent)); |
| 833 | old = ctxt->sax; |
| 834 | silent.error = xmlHTMLError; |
| 835 | if (xmlGetWarningsDefaultValue) |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 836 | silent.warning = xmlHTMLWarning; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 837 | else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 838 | silent.warning = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 839 | silent.fatalError = xmlHTMLError; |
| 840 | ctxt->sax = &silent; |
| 841 | ctxt->vctxt.error = xmlHTMLValidityError; |
| 842 | if (xmlGetWarningsDefaultValue) |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 843 | ctxt->vctxt.warning = xmlHTMLValidityWarning; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 844 | else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 845 | ctxt->vctxt.warning = NULL; |
| 846 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 847 | xmlParseDocument(ctxt); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 848 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 849 | ret = ctxt->wellFormed; |
| 850 | doc = ctxt->myDoc; |
| 851 | ctxt->sax = old; |
| 852 | xmlFreeParserCtxt(ctxt); |
| 853 | if (!ret) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 854 | xmlFreeDoc(doc); |
| 855 | doc = NULL; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 856 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 857 | } |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 858 | #ifdef HAVE_SYS_MMAN_H |
| 859 | } else if (memory) { |
| 860 | int fd; |
| 861 | struct stat info; |
| 862 | const char *base; |
| 863 | if (stat(filename, &info) < 0) |
| 864 | return; |
| 865 | if ((fd = open(filename, O_RDONLY)) < 0) |
| 866 | return; |
| 867 | base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ; |
Daniel Veillard | 2957936 | 2000-08-14 17:57:48 +0000 | [diff] [blame] | 868 | if (base == (void *) MAP_FAILED) |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 869 | return; |
| 870 | |
| 871 | doc = xmlParseMemory((char *) base, info.st_size); |
| 872 | munmap((char *) base, info.st_size); |
| 873 | #endif |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 874 | } else if (valid) { |
| 875 | int ret; |
| 876 | xmlParserCtxtPtr ctxt; |
| 877 | |
| 878 | ctxt = xmlCreateFileParserCtxt(filename); |
| 879 | |
| 880 | if (ctxt == NULL) { |
| 881 | doc = NULL; |
| 882 | } else { |
| 883 | xmlParseDocument(ctxt); |
| 884 | if (ctxt->valid == 0) |
| 885 | progresult = 4; |
| 886 | ret = ctxt->wellFormed; |
| 887 | doc = ctxt->myDoc; |
| 888 | xmlFreeParserCtxt(ctxt); |
| 889 | if (!ret) { |
| 890 | xmlFreeDoc(doc); |
| 891 | doc = NULL; |
| 892 | } |
| 893 | } |
| 894 | } else { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 895 | doc = xmlParseFile(filename); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 896 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 897 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 898 | |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 899 | /* |
| 900 | * If we don't have a document we might as well give up. Do we |
| 901 | * want an error message here? <sven@zen.org> */ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 902 | if (doc == NULL) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 903 | progresult = 1; |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 904 | return; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 907 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 908 | endTimer("Parsing"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 911 | /* |
| 912 | * Remove DOCTYPE nodes |
| 913 | */ |
| 914 | if (dropdtd) { |
| 915 | xmlDtdPtr dtd; |
| 916 | |
| 917 | dtd = xmlGetIntSubset(doc); |
| 918 | if (dtd != NULL) { |
| 919 | xmlUnlinkNode((xmlNodePtr)dtd); |
| 920 | xmlFreeDtd(dtd); |
| 921 | } |
| 922 | } |
| 923 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 924 | #ifdef LIBXML_XINCLUDE_ENABLED |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 925 | if (xinclude) { |
| 926 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 927 | startTimer(); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 928 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 929 | xmlXIncludeProcess(doc); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 930 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 931 | endTimer("Xinclude processing"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 934 | #endif |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 935 | |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 936 | #ifdef LIBXML_DEBUG_ENABLED |
| 937 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 938 | * shell interaction |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 939 | */ |
| 940 | if (shell) |
| 941 | xmlShell(doc, filename, xmlShellReadline, stdout); |
| 942 | #endif |
| 943 | |
| 944 | /* |
| 945 | * test intermediate copy if needed. |
| 946 | */ |
| 947 | if (copy) { |
| 948 | tmp = doc; |
| 949 | doc = xmlCopyDoc(doc, 1); |
| 950 | xmlFreeDoc(tmp); |
| 951 | } |
| 952 | |
| 953 | if ((insert) && (!html)) { |
| 954 | const xmlChar* list[256]; |
| 955 | int nb, i; |
| 956 | xmlNodePtr node; |
| 957 | |
| 958 | if (doc->children != NULL) { |
| 959 | node = doc->children; |
| 960 | while ((node != NULL) && (node->last == NULL)) node = node->next; |
| 961 | if (node != NULL) { |
| 962 | nb = xmlValidGetValidElements(node->last, NULL, list, 256); |
| 963 | if (nb < 0) { |
| 964 | printf("could not get valid list of elements\n"); |
| 965 | } else if (nb == 0) { |
MDT 2001 John Fleck | bbb9e43 | 2001-09-24 03:08:43 +0000 | [diff] [blame] | 966 | printf("No element can be inserted under root\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 967 | } else { |
MDT 2001 John Fleck | bbb9e43 | 2001-09-24 03:08:43 +0000 | [diff] [blame] | 968 | printf("%d element types can be inserted under root:\n", |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 969 | nb); |
| 970 | for (i = 0;i < nb;i++) { |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 971 | printf("%s\n", (char *) list[i]); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | }else if (noout == 0) { |
| 977 | /* |
| 978 | * print it. |
| 979 | */ |
| 980 | #ifdef LIBXML_DEBUG_ENABLED |
| 981 | if (!debug) { |
| 982 | #endif |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 983 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 984 | startTimer(); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 985 | } |
Daniel Veillard | 3b2c261 | 2001-04-04 00:09:00 +0000 | [diff] [blame] | 986 | #ifdef HAVE_SYS_MMAN_H |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 987 | if (memory) { |
| 988 | xmlChar *result; |
| 989 | int len; |
| 990 | |
| 991 | if (encoding != NULL) { |
Daniel Veillard | d536f70 | 2001-11-08 17:32:47 +0000 | [diff] [blame] | 992 | if ( format ) { |
| 993 | xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1); |
| 994 | } else { |
| 995 | xmlDocDumpMemoryEnc(doc, &result, &len, encoding); |
| 996 | } |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 997 | } else { |
Daniel Veillard | 90493a9 | 2001-08-14 14:12:47 +0000 | [diff] [blame] | 998 | if (format) |
| 999 | xmlDocDumpFormatMemory(doc, &result, &len, 1); |
| 1000 | else |
| 1001 | xmlDocDumpMemory(doc, &result, &len); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1002 | } |
| 1003 | if (result == NULL) { |
| 1004 | fprintf(stderr, "Failed to save\n"); |
| 1005 | } else { |
| 1006 | write(1, result, len); |
| 1007 | xmlFree(result); |
| 1008 | } |
Daniel Veillard | 3b2c261 | 2001-04-04 00:09:00 +0000 | [diff] [blame] | 1009 | } else |
| 1010 | #endif /* HAVE_SYS_MMAN_H */ |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1011 | if (compress) { |
| 1012 | xmlSaveFile(output ? output : "-", doc); |
| 1013 | } |
Daniel Veillard | d536f70 | 2001-11-08 17:32:47 +0000 | [diff] [blame] | 1014 | else if (encoding != NULL) { |
| 1015 | if ( format ) { |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1016 | xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1); |
| 1017 | } |
Daniel Veillard | d536f70 | 2001-11-08 17:32:47 +0000 | [diff] [blame] | 1018 | else { |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1019 | xmlSaveFileEnc(output ? output : "-", doc, encoding); |
Daniel Veillard | d536f70 | 2001-11-08 17:32:47 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1022 | else if (format) { |
| 1023 | xmlSaveFormatFile(output ? output : "-", doc, 1); |
| 1024 | } |
| 1025 | else { |
| 1026 | FILE *out; |
| 1027 | if (output == NULL) |
| 1028 | out = stdout; |
| 1029 | else { |
| 1030 | out = fopen(output,"wb"); |
| 1031 | } |
| 1032 | xmlDocDump(out, doc); |
| 1033 | |
| 1034 | if (output) |
| 1035 | fclose(out); |
| 1036 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1037 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1038 | endTimer("Saving"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1039 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1040 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1041 | } else { |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1042 | FILE *out; |
| 1043 | if (output == NULL) |
| 1044 | out = stdout; |
| 1045 | else { |
| 1046 | out = fopen(output,"wb"); |
| 1047 | } |
| 1048 | xmlDebugDumpDocument(out, doc); |
| 1049 | |
| 1050 | if (output) |
| 1051 | fclose(out); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1052 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1053 | #endif |
| 1054 | } |
| 1055 | |
| 1056 | /* |
| 1057 | * A posteriori validation test |
| 1058 | */ |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1059 | if (dtdvalid != NULL) { |
| 1060 | xmlDtdPtr dtd; |
| 1061 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1062 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1063 | startTimer(); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1064 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1065 | dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1066 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1067 | endTimer("Parsing DTD"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1068 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1069 | if (dtd == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1070 | xmlGenericError(xmlGenericErrorContext, |
| 1071 | "Could not parse DTD %s\n", dtdvalid); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1072 | progresult = 2; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | xmlValidCtxt cvp; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1075 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1076 | startTimer(); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1077 | } |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1078 | cvp.userData = (void *) stderr; |
| 1079 | cvp.error = (xmlValidityErrorFunc) fprintf; |
| 1080 | cvp.warning = (xmlValidityWarningFunc) fprintf; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1081 | if (!xmlValidateDtd(&cvp, doc, dtd)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1082 | xmlGenericError(xmlGenericErrorContext, |
| 1083 | "Document %s does not validate against %s\n", |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1084 | filename, dtdvalid); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1085 | progresult = 3; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1086 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1087 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1088 | endTimer("Validating against DTD"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1089 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1090 | xmlFreeDtd(dtd); |
| 1091 | } |
| 1092 | } else if (postvalid) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1093 | xmlValidCtxt cvp; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1094 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1095 | startTimer(); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1096 | } |
Daniel Veillard | b7664f4 | 2001-08-19 13:00:43 +0000 | [diff] [blame] | 1097 | cvp.userData = (void *) stderr; |
| 1098 | cvp.error = (xmlValidityErrorFunc) fprintf; |
| 1099 | cvp.warning = (xmlValidityWarningFunc) fprintf; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1100 | if (!xmlValidateDocument(&cvp, doc)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1101 | xmlGenericError(xmlGenericErrorContext, |
| 1102 | "Document %s does not validate\n", filename); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1103 | progresult = 3; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1104 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1105 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1106 | endTimer("Validating"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1107 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1108 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 1109 | } else if (relaxngschemas != NULL) { |
| 1110 | xmlRelaxNGValidCtxtPtr ctxt; |
| 1111 | int ret; |
| 1112 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1113 | if ((timing) && (!repeat)) { |
| 1114 | startTimer(); |
| 1115 | } |
| 1116 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1117 | ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas); |
| 1118 | xmlRelaxNGSetValidErrors(ctxt, |
| 1119 | (xmlRelaxNGValidityErrorFunc) fprintf, |
| 1120 | (xmlRelaxNGValidityWarningFunc) fprintf, |
| 1121 | stderr); |
| 1122 | ret = xmlRelaxNGValidateDoc(ctxt, doc); |
| 1123 | if (ret == 0) { |
| 1124 | printf("%s validates\n", filename); |
| 1125 | } else if (ret > 0) { |
| 1126 | printf("%s fails to validate\n", filename); |
| 1127 | } else { |
| 1128 | printf("%s validation generated an internal error\n", |
| 1129 | filename); |
| 1130 | } |
| 1131 | xmlRelaxNGFreeValidCtxt(ctxt); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1132 | if ((timing) && (!repeat)) { |
| 1133 | endTimer("Validating"); |
| 1134 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1135 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | #ifdef LIBXML_DEBUG_ENABLED |
| 1139 | if ((debugent) && (!html)) |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1140 | xmlDebugDumpEntities(stderr, doc); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1141 | #endif |
| 1142 | |
| 1143 | /* |
| 1144 | * free it. |
| 1145 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1146 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1147 | startTimer(); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1148 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1149 | xmlFreeDoc(doc); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1150 | if ((timing) && (!repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1151 | endTimer("Freeing"); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1152 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1155 | /************************************************************************ |
| 1156 | * * |
| 1157 | * Usage and Main * |
| 1158 | * * |
| 1159 | ************************************************************************/ |
| 1160 | |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 1161 | static void showVersion(const char *name) { |
| 1162 | fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion); |
| 1163 | fprintf(stderr, " compiled with: "); |
| 1164 | #ifdef LIBXML_FTP_ENABLED |
| 1165 | fprintf(stderr, "FTP "); |
| 1166 | #endif |
| 1167 | #ifdef LIBXML_HTTP_ENABLED |
| 1168 | fprintf(stderr, "HTTP "); |
| 1169 | #endif |
| 1170 | #ifdef LIBXML_HTML_ENABLED |
| 1171 | fprintf(stderr, "HTML "); |
| 1172 | #endif |
| 1173 | #ifdef LIBXML_C14N_ENABLED |
| 1174 | fprintf(stderr, "C14N "); |
| 1175 | #endif |
| 1176 | #ifdef LIBXML_CATALOG_ENABLED |
| 1177 | fprintf(stderr, "Catalog "); |
| 1178 | #endif |
| 1179 | #ifdef LIBXML_DOCB_ENABLED |
| 1180 | fprintf(stderr, "DocBook "); |
| 1181 | #endif |
| 1182 | #ifdef LIBXML_XPATH_ENABLED |
| 1183 | fprintf(stderr, "XPath "); |
| 1184 | #endif |
| 1185 | #ifdef LIBXML_XPTR_ENABLED |
| 1186 | fprintf(stderr, "XPointer "); |
| 1187 | #endif |
| 1188 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 1189 | fprintf(stderr, "XInclude "); |
| 1190 | #endif |
| 1191 | #ifdef LIBXML_ICONV_ENABLED |
| 1192 | fprintf(stderr, "Iconv "); |
| 1193 | #endif |
| 1194 | #ifdef DEBUG_MEMORY_LOCATION |
| 1195 | fprintf(stderr, "MemDebug "); |
| 1196 | #endif |
| 1197 | #ifdef LIBXML_UNICODE_ENABLED |
| 1198 | fprintf(stderr, "Unicode "); |
| 1199 | #endif |
| 1200 | #ifdef LIBXML_REGEXP_ENABLED |
| 1201 | fprintf(stderr, "Regexps "); |
| 1202 | #endif |
| 1203 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 1204 | fprintf(stderr, "Automata "); |
| 1205 | #endif |
| 1206 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 1207 | fprintf(stderr, "Schemas "); |
| 1208 | #endif |
| 1209 | fprintf(stderr, "\n"); |
| 1210 | } |
| 1211 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1212 | static void usage(const char *name) { |
| 1213 | printf("Usage : %s [options] XMLfiles ...\n", name); |
| 1214 | printf("\tParse the XML files and output the result of the parsing\n"); |
| 1215 | printf("\t--version : display the version of the XML library used\n"); |
| 1216 | #ifdef LIBXML_DEBUG_ENABLED |
| 1217 | printf("\t--debug : dump a debug tree of the in-memory document\n"); |
| 1218 | printf("\t--shell : run a navigating shell\n"); |
| 1219 | printf("\t--debugent : debug the entities defined in the document\n"); |
Daniel Veillard | 8326e73 | 2003-01-07 00:19:07 +0000 | [diff] [blame] | 1220 | #else |
| 1221 | printf("\t--debug : dump the nodes content when using --stream\n"); |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1222 | #endif |
| 1223 | printf("\t--copy : used to test the internal copy implementation\n"); |
| 1224 | printf("\t--recover : output what was parsable on broken XML documents\n"); |
| 1225 | printf("\t--noent : substitute entity references by their value\n"); |
| 1226 | printf("\t--noout : don't output the result tree\n"); |
| 1227 | printf("\t--htmlout : output results as HTML\n"); |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 1228 | printf("\t--nowrap : do not put HTML doc wrapper\n"); |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1229 | printf("\t--valid : validate the document in addition to std well-formed check\n"); |
| 1230 | printf("\t--postvalid : do a posteriori validation, i.e after parsing\n"); |
| 1231 | printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n"); |
| 1232 | printf("\t--timing : print some timings\n"); |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1233 | printf("\t--output file or -o file: save to a given file\n"); |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1234 | printf("\t--repeat : repeat 100 times, for timing or profiling\n"); |
| 1235 | printf("\t--insert : ad-hoc test for valid insertions\n"); |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1236 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1237 | printf("\t--compress : turn on gzip compression of output\n"); |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1238 | #endif |
Daniel Veillard | 89cad53 | 2001-10-22 09:46:13 +0000 | [diff] [blame] | 1239 | #ifdef LIBXML_DOCB_ENABLED |
| 1240 | printf("\t--sgml : use the DocBook SGML parser\n"); |
| 1241 | #endif |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1242 | #ifdef LIBXML_HTML_ENABLED |
| 1243 | printf("\t--html : use the HTML parser\n"); |
| 1244 | #endif |
| 1245 | printf("\t--push : use the push mode of the parser\n"); |
| 1246 | #ifdef HAVE_SYS_MMAN_H |
| 1247 | printf("\t--memory : parse from memory\n"); |
| 1248 | #endif |
| 1249 | printf("\t--nowarning : do not emit warnings from parser/validator\n"); |
| 1250 | printf("\t--noblanks : drop (ignorable?) blanks spaces\n"); |
Daniel Veillard | 90493a9 | 2001-08-14 14:12:47 +0000 | [diff] [blame] | 1251 | printf("\t--format : reformat/reindent the input\n"); |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1252 | printf("\t--testIO : test user I/O support\n"); |
| 1253 | printf("\t--encode encoding : output in the given encoding\n"); |
| 1254 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | bd9b0e8 | 2001-11-26 10:32:08 +0000 | [diff] [blame] | 1255 | printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n"); |
| 1256 | printf("\t otherwise XML Catalogs starting from \n"); |
Daniel Veillard | 3be2751 | 2003-01-26 19:49:04 +0000 | [diff] [blame] | 1257 | printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n"); |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 1258 | printf("\t--nocatalogs: deactivate all catalogs\n"); |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1259 | #endif |
| 1260 | printf("\t--auto : generate a small doc on the fly\n"); |
| 1261 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 1262 | printf("\t--xinclude : do XInclude processing\n"); |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1263 | #endif |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1264 | printf("\t--loaddtd : fetch external DTD\n"); |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1265 | printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n"); |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 1266 | printf("\t--dropdtd : remove the DOCTYPE of the input docs\n"); |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 1267 | printf("\t--stream : use the streaming interface to process very large files\n"); |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1268 | printf("\t--chkregister : verify the node registration code\n"); |
Daniel Veillard | ef4d3bc | 2003-02-07 12:38:22 +0000 | [diff] [blame] | 1269 | #ifdef LIBXML_SCHEMAS_ENABLED |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1270 | printf("\t--relaxng schema : do RelaxNG validation against the schema\n"); |
| 1271 | #endif |
Daniel Veillard | a42f25f | 2002-01-25 14:15:40 +0000 | [diff] [blame] | 1272 | printf("\nLibxml project home page: http://xmlsoft.org/\n"); |
| 1273 | printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n"); |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1274 | } |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1275 | |
| 1276 | static void registerNode(xmlNodePtr node) |
| 1277 | { |
| 1278 | node->_private = malloc(sizeof(long)); |
| 1279 | *(long*)node->_private = (long) 0x81726354; |
| 1280 | } |
| 1281 | |
| 1282 | static void deregisterNode(xmlNodePtr node) |
| 1283 | { |
| 1284 | assert(node->_private != NULL); |
| 1285 | assert(*(long*)node->_private == (long) 0x81726354); |
| 1286 | free(node->_private); |
| 1287 | } |
| 1288 | |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 1289 | int |
| 1290 | main(int argc, char **argv) { |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 1291 | int i, acount; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1292 | int files = 0; |
Daniel Veillard | 845cce4 | 2002-01-09 11:51:37 +0000 | [diff] [blame] | 1293 | int version = 0; |
Aleksey Sanin | 693c9bc | 2003-03-09 22:36:52 +0000 | [diff] [blame] | 1294 | const char* indent; |
| 1295 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1296 | if (argc <= 1) { |
| 1297 | usage(argv[0]); |
| 1298 | return(1); |
| 1299 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1300 | LIBXML_TEST_VERSION |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1301 | for (i = 1; i < argc ; i++) { |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1302 | if (!strcmp(argv[i], "-")) |
| 1303 | break; |
| 1304 | |
| 1305 | if (argv[i][0] != '-') |
| 1306 | continue; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1307 | if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) |
| 1308 | debug++; |
Daniel Veillard | 56ada1d | 2003-01-07 11:17:25 +0000 | [diff] [blame] | 1309 | else |
| 1310 | #ifdef LIBXML_DEBUG_ENABLED |
| 1311 | if ((!strcmp(argv[i], "-shell")) || |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1312 | (!strcmp(argv[i], "--shell"))) { |
| 1313 | shell++; |
| 1314 | noout = 1; |
| 1315 | } else |
| 1316 | #endif |
| 1317 | if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy"))) |
| 1318 | copy++; |
| 1319 | else if ((!strcmp(argv[i], "-recover")) || |
| 1320 | (!strcmp(argv[i], "--recover"))) |
| 1321 | recovery++; |
| 1322 | else if ((!strcmp(argv[i], "-noent")) || |
| 1323 | (!strcmp(argv[i], "--noent"))) |
| 1324 | noent++; |
Daniel Veillard | 4ec885a | 2001-06-17 10:31:07 +0000 | [diff] [blame] | 1325 | else if ((!strcmp(argv[i], "-version")) || |
Daniel Veillard | 845cce4 | 2002-01-09 11:51:37 +0000 | [diff] [blame] | 1326 | (!strcmp(argv[i], "--version"))) { |
Daniel Veillard | 0f04f8e | 2002-09-17 23:04:40 +0000 | [diff] [blame] | 1327 | showVersion(argv[0]); |
Daniel Veillard | 845cce4 | 2002-01-09 11:51:37 +0000 | [diff] [blame] | 1328 | version = 1; |
| 1329 | } else if ((!strcmp(argv[i], "-noout")) || |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1330 | (!strcmp(argv[i], "--noout"))) |
| 1331 | noout++; |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1332 | else if ((!strcmp(argv[i], "-o")) || |
| 1333 | (!strcmp(argv[i], "-output")) || |
| 1334 | (!strcmp(argv[i], "--output"))) { |
| 1335 | i++; |
Daniel Veillard | 6e4f1c0 | 2002-04-09 09:55:20 +0000 | [diff] [blame] | 1336 | output = argv[i]; |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1337 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1338 | else if ((!strcmp(argv[i], "-htmlout")) || |
| 1339 | (!strcmp(argv[i], "--htmlout"))) |
| 1340 | htmlout++; |
Daniel Veillard | 89cad53 | 2001-10-22 09:46:13 +0000 | [diff] [blame] | 1341 | #ifdef LIBXML_DOCB_ENABLED |
| 1342 | else if ((!strcmp(argv[i], "-sgml")) || |
| 1343 | (!strcmp(argv[i], "--sgml"))) { |
| 1344 | sgml++; |
| 1345 | } |
| 1346 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1347 | #ifdef LIBXML_HTML_ENABLED |
| 1348 | else if ((!strcmp(argv[i], "-html")) || |
| 1349 | (!strcmp(argv[i], "--html"))) { |
| 1350 | html++; |
| 1351 | } |
| 1352 | #endif /* LIBXML_HTML_ENABLED */ |
| 1353 | else if ((!strcmp(argv[i], "-nowrap")) || |
| 1354 | (!strcmp(argv[i], "--nowrap"))) |
| 1355 | nowrap++; |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1356 | else if ((!strcmp(argv[i], "-loaddtd")) || |
| 1357 | (!strcmp(argv[i], "--loaddtd"))) |
| 1358 | loaddtd++; |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1359 | else if ((!strcmp(argv[i], "-dtdattr")) || |
| 1360 | (!strcmp(argv[i], "--dtdattr"))) { |
| 1361 | loaddtd++; |
| 1362 | dtdattrs++; |
| 1363 | } else if ((!strcmp(argv[i], "-valid")) || |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1364 | (!strcmp(argv[i], "--valid"))) |
| 1365 | valid++; |
| 1366 | else if ((!strcmp(argv[i], "-postvalid")) || |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1367 | (!strcmp(argv[i], "--postvalid"))) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1368 | postvalid++; |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1369 | loaddtd++; |
| 1370 | } else if ((!strcmp(argv[i], "-dtdvalid")) || |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1371 | (!strcmp(argv[i], "--dtdvalid"))) { |
| 1372 | i++; |
| 1373 | dtdvalid = argv[i]; |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1374 | loaddtd++; |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1375 | } |
Daniel Veillard | 29e4399 | 2001-12-13 22:21:58 +0000 | [diff] [blame] | 1376 | else if ((!strcmp(argv[i], "-dropdtd")) || |
| 1377 | (!strcmp(argv[i], "--dropdtd"))) |
| 1378 | dropdtd++; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1379 | else if ((!strcmp(argv[i], "-insert")) || |
| 1380 | (!strcmp(argv[i], "--insert"))) |
| 1381 | insert++; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1382 | else if ((!strcmp(argv[i], "-timing")) || |
| 1383 | (!strcmp(argv[i], "--timing"))) |
| 1384 | timing++; |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1385 | else if ((!strcmp(argv[i], "-auto")) || |
| 1386 | (!strcmp(argv[i], "--auto"))) |
| 1387 | generate++; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1388 | else if ((!strcmp(argv[i], "-repeat")) || |
| 1389 | (!strcmp(argv[i], "--repeat"))) |
| 1390 | repeat++; |
| 1391 | else if ((!strcmp(argv[i], "-push")) || |
| 1392 | (!strcmp(argv[i], "--push"))) |
| 1393 | push++; |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 1394 | #ifdef HAVE_SYS_MMAN_H |
| 1395 | else if ((!strcmp(argv[i], "-memory")) || |
| 1396 | (!strcmp(argv[i], "--memory"))) |
| 1397 | memory++; |
| 1398 | #endif |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 1399 | else if ((!strcmp(argv[i], "-testIO")) || |
| 1400 | (!strcmp(argv[i], "--testIO"))) |
| 1401 | testIO++; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1402 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 1403 | else if ((!strcmp(argv[i], "-xinclude")) || |
| 1404 | (!strcmp(argv[i], "--xinclude"))) |
| 1405 | xinclude++; |
| 1406 | #endif |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1407 | #ifdef HAVE_ZLIB_H |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1408 | else if ((!strcmp(argv[i], "-compress")) || |
| 1409 | (!strcmp(argv[i], "--compress"))) { |
| 1410 | compress++; |
| 1411 | xmlSetCompressMode(9); |
| 1412 | } |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1413 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1414 | else if ((!strcmp(argv[i], "-nowarning")) || |
| 1415 | (!strcmp(argv[i], "--nowarning"))) { |
| 1416 | xmlGetWarningsDefaultValue = 0; |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1417 | xmlPedanticParserDefault(0); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1418 | } |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1419 | else if ((!strcmp(argv[i], "-pedantic")) || |
| 1420 | (!strcmp(argv[i], "--pedantic"))) { |
| 1421 | xmlGetWarningsDefaultValue = 1; |
| 1422 | xmlPedanticParserDefault(1); |
| 1423 | } |
Daniel Veillard | 64c20ed | 2000-09-22 16:07:02 +0000 | [diff] [blame] | 1424 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1425 | else if ((!strcmp(argv[i], "-debugent")) || |
| 1426 | (!strcmp(argv[i], "--debugent"))) { |
| 1427 | debugent++; |
| 1428 | xmlParserDebugEntities = 1; |
| 1429 | } |
Daniel Veillard | 64c20ed | 2000-09-22 16:07:02 +0000 | [diff] [blame] | 1430 | #endif |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 1431 | #ifdef LIBXML_CATALOG_ENABLED |
| 1432 | else if ((!strcmp(argv[i], "-catalogs")) || |
| 1433 | (!strcmp(argv[i], "--catalogs"))) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1434 | catalogs++; |
| 1435 | } else if ((!strcmp(argv[i], "-nocatalogs")) || |
| 1436 | (!strcmp(argv[i], "--nocatalogs"))) { |
| 1437 | nocatalogs++; |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 1438 | } |
| 1439 | #endif |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1440 | else if ((!strcmp(argv[i], "-encode")) || |
| 1441 | (!strcmp(argv[i], "--encode"))) { |
| 1442 | i++; |
| 1443 | encoding = argv[i]; |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1444 | /* |
| 1445 | * OK it's for testing purposes |
| 1446 | */ |
| 1447 | xmlAddEncodingAlias("UTF-8", "DVEnc"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1448 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1449 | else if ((!strcmp(argv[i], "-noblanks")) || |
| 1450 | (!strcmp(argv[i], "--noblanks"))) { |
| 1451 | noblanks++; |
| 1452 | xmlKeepBlanksDefault(0); |
Daniel Veillard | 90493a9 | 2001-08-14 14:12:47 +0000 | [diff] [blame] | 1453 | } |
| 1454 | else if ((!strcmp(argv[i], "-format")) || |
| 1455 | (!strcmp(argv[i], "--format"))) { |
| 1456 | noblanks++; |
| 1457 | format++; |
| 1458 | xmlKeepBlanksDefault(0); |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 1459 | } |
| 1460 | else if ((!strcmp(argv[i], "-stream")) || |
| 1461 | (!strcmp(argv[i], "--stream"))) { |
| 1462 | stream++; |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1463 | } |
| 1464 | else if ((!strcmp(argv[i], "-chkregister")) || |
| 1465 | (!strcmp(argv[i], "--chkregister"))) { |
| 1466 | chkregister++; |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1467 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 1468 | } else if ((!strcmp(argv[i], "-relaxng")) || |
| 1469 | (!strcmp(argv[i], "--relaxng"))) { |
| 1470 | i++; |
| 1471 | relaxng = argv[i]; |
| 1472 | noent++; |
| 1473 | #endif |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1474 | } else { |
| 1475 | fprintf(stderr, "Unknown option %s\n", argv[i]); |
| 1476 | usage(argv[0]); |
| 1477 | return(1); |
| 1478 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1479 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1480 | |
| 1481 | #ifdef LIBXML_CATALOG_ENABLED |
| 1482 | if (nocatalogs == 0) { |
| 1483 | if (catalogs) { |
| 1484 | const char *catal; |
| 1485 | |
| 1486 | catal = getenv("SGML_CATALOG_FILES"); |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 1487 | if (catal != NULL) { |
| 1488 | xmlLoadCatalogs(catal); |
| 1489 | } else { |
| 1490 | fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n"); |
| 1491 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | #endif |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1495 | |
| 1496 | if (chkregister) { |
| 1497 | xmlRegisterNodeDefault(registerNode); |
| 1498 | xmlDeregisterNodeDefault(deregisterNode); |
| 1499 | } |
Aleksey Sanin | 693c9bc | 2003-03-09 22:36:52 +0000 | [diff] [blame] | 1500 | |
| 1501 | indent = getenv("XMLLINT_INDENT"); |
| 1502 | if(indent != NULL) { |
| 1503 | xmlTreeIndentString = indent; |
| 1504 | } |
| 1505 | |
Daniel Veillard | 8a1b185 | 2003-01-05 22:37:17 +0000 | [diff] [blame] | 1506 | |
Daniel Veillard | d9bad13 | 2001-07-23 19:39:43 +0000 | [diff] [blame] | 1507 | xmlLineNumbersDefault(1); |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1508 | if (loaddtd != 0) |
| 1509 | xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; |
| 1510 | if (dtdattrs) |
| 1511 | xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS; |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1512 | if (noent != 0) xmlSubstituteEntitiesDefault(1); |
| 1513 | if (valid != 0) xmlDoValidityCheckingDefaultValue = 1; |
| 1514 | if ((htmlout) && (!nowrap)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1515 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1516 | "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n"); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1517 | xmlGenericError(xmlGenericErrorContext, |
| 1518 | "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"); |
| 1519 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1520 | "<html><head><title>%s output</title></head>\n", |
| 1521 | argv[0]); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1522 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1523 | "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n", |
| 1524 | argv[0]); |
| 1525 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1526 | |
| 1527 | #ifdef LIBXML_SCHEMAS_ENABLED |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 1528 | if ((relaxng != NULL) && (stream == 0)) { |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1529 | xmlRelaxNGParserCtxtPtr ctxt; |
| 1530 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 1531 | /* forces loading the DTDs */ |
| 1532 | xmlLoadExtDtdDefaultValue |= 1; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1533 | if (timing) { |
| 1534 | startTimer(); |
| 1535 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1536 | ctxt = xmlRelaxNGNewParserCtxt(relaxng); |
| 1537 | xmlRelaxNGSetParserErrors(ctxt, |
| 1538 | (xmlRelaxNGValidityErrorFunc) fprintf, |
| 1539 | (xmlRelaxNGValidityWarningFunc) fprintf, |
| 1540 | stderr); |
| 1541 | relaxngschemas = xmlRelaxNGParse(ctxt); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 1542 | if (relaxngschemas == NULL) { |
| 1543 | xmlGenericError(xmlGenericErrorContext, |
| 1544 | "Relax-NG schema %s failed to compile\n", relaxng); |
| 1545 | relaxng = NULL; |
| 1546 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1547 | xmlRelaxNGFreeParserCtxt(ctxt); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1548 | if (timing) { |
| 1549 | endTimer("Compiling the schemas"); |
| 1550 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1551 | } |
| 1552 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1553 | for (i = 1; i < argc ; i++) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1554 | if ((!strcmp(argv[i], "-encode")) || |
| 1555 | (!strcmp(argv[i], "--encode"))) { |
| 1556 | i++; |
| 1557 | continue; |
Daniel Veillard | 1df3dfc | 2001-12-18 11:14:16 +0000 | [diff] [blame] | 1558 | } else if ((!strcmp(argv[i], "-o")) || |
| 1559 | (!strcmp(argv[i], "-output")) || |
| 1560 | (!strcmp(argv[i], "--output"))) { |
| 1561 | i++; |
| 1562 | continue; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1563 | } |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 1564 | if ((!strcmp(argv[i], "-dtdvalid")) || |
| 1565 | (!strcmp(argv[i], "--dtdvalid"))) { |
| 1566 | i++; |
| 1567 | continue; |
| 1568 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1569 | if ((!strcmp(argv[i], "-relaxng")) || |
| 1570 | (!strcmp(argv[i], "--relaxng"))) { |
| 1571 | i++; |
| 1572 | continue; |
| 1573 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1574 | if ((timing) && (repeat)) |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1575 | startTimer(); |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1576 | /* Remember file names. "-" means stdin. <sven@zen.org> */ |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 1577 | if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) { |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1578 | if (repeat) { |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 1579 | for (acount = 0;acount < 100 * repeat;acount++) |
| 1580 | if (stream != 0) |
| 1581 | streamFile(argv[i]); |
| 1582 | else |
| 1583 | parseAndPrintFile(argv[i]); |
| 1584 | } else { |
| 1585 | if (stream != 0) |
| 1586 | streamFile(argv[i]); |
| 1587 | else |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1588 | parseAndPrintFile(argv[i]); |
Daniel Veillard | 7704fb1 | 2003-01-03 16:19:51 +0000 | [diff] [blame] | 1589 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1590 | files ++; |
Daniel Veillard | a786693 | 2001-12-04 13:14:44 +0000 | [diff] [blame] | 1591 | if ((timing) && (repeat)) { |
Daniel Veillard | 01db67c | 2001-12-18 07:09:59 +0000 | [diff] [blame] | 1592 | endTimer("100 iterations"); |
Daniel Veillard | a786693 | 2001-12-04 13:14:44 +0000 | [diff] [blame] | 1593 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1594 | } |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1595 | } |
Daniel Veillard | d2f3ec7 | 2001-04-11 07:50:02 +0000 | [diff] [blame] | 1596 | if (generate) |
| 1597 | parseAndPrintFile(NULL); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1598 | if ((htmlout) && (!nowrap)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1599 | xmlGenericError(xmlGenericErrorContext, "</body></html>\n"); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1600 | } |
Daniel Veillard | 845cce4 | 2002-01-09 11:51:37 +0000 | [diff] [blame] | 1601 | if ((files == 0) && (!generate) && (version == 0)) { |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 1602 | usage(argv[0]); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1603 | } |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 1604 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 1605 | if (relaxngschemas != NULL) |
| 1606 | xmlRelaxNGFree(relaxngschemas); |
| 1607 | xmlRelaxNGCleanupTypes(); |
| 1608 | #endif |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1609 | xmlCleanupParser(); |
| 1610 | xmlMemoryDump(); |
| 1611 | |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 1612 | return(progresult); |
Daniel Veillard | ce8b83b | 2000-04-05 18:38:42 +0000 | [diff] [blame] | 1613 | } |
Daniel Veillard | 88a172f | 2000-08-04 18:23:10 +0000 | [diff] [blame] | 1614 | |