Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * error.c: module displaying/handling XML parser errors |
| 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 <daniel@veillard.com> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 9 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | #include <stdarg.h> |
| 12 | #include <libxml/parser.h> |
| 13 | #include <libxml/xmlerror.h> |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 14 | #include <libxml/xmlmemory.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 15 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 16 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 17 | #define XML_GET_VAR_STR(msg, str) { \ |
| 18 | int size; \ |
| 19 | int chars; \ |
| 20 | char *larger; \ |
| 21 | va_list ap; \ |
| 22 | \ |
| 23 | str = (char *) xmlMalloc(150); \ |
| 24 | if (str == NULL) \ |
| 25 | return; \ |
| 26 | \ |
| 27 | size = 150; \ |
| 28 | \ |
| 29 | while (1) { \ |
| 30 | va_start(ap, msg); \ |
| 31 | chars = vsnprintf(str, size, msg, ap); \ |
| 32 | va_end(ap); \ |
| 33 | if ((chars > -1) && (chars < size)) \ |
| 34 | break; \ |
| 35 | if (chars > -1) \ |
| 36 | size += chars + 1; \ |
| 37 | else \ |
| 38 | size += 100; \ |
| 39 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\ |
| 40 | xmlFree(str); \ |
| 41 | return; \ |
| 42 | } \ |
| 43 | str = larger; \ |
| 44 | } \ |
| 45 | } |
Bjorn Reese | 570ff08 | 2001-06-05 12:45:55 +0000 | [diff] [blame] | 46 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 47 | /************************************************************************ |
| 48 | * * |
| 49 | * Handling of out of context errors * |
| 50 | * * |
| 51 | ************************************************************************/ |
| 52 | |
| 53 | /** |
| 54 | * xmlGenericErrorDefaultFunc: |
| 55 | * @ctx: an error context |
| 56 | * @msg: the message to display/transmit |
| 57 | * @...: extra parameters for the message display |
| 58 | * |
| 59 | * Default handler for out of context error messages. |
| 60 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 61 | static void |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 62 | xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 63 | va_list args; |
| 64 | |
| 65 | if (xmlGenericErrorContext == NULL) |
| 66 | xmlGenericErrorContext = (void *) stderr; |
| 67 | |
| 68 | va_start(args, msg); |
| 69 | vfprintf((FILE *)xmlGenericErrorContext, msg, args); |
| 70 | va_end(args); |
| 71 | } |
| 72 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 73 | void |
| 74 | initGenericErrorDefaultFunc(xmlGenericErrorFunc *handler) |
| 75 | { |
| 76 | if (handler == NULL) |
| 77 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 78 | else |
| 79 | (*handler) = xmlGenericErrorDefaultFunc; |
| 80 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * xmlSetGenericErrorFunc: |
| 84 | * @ctx: the new error handling context |
| 85 | * @handler: the new handler function |
| 86 | * |
| 87 | * Function to reset the handler and the error context for out of |
| 88 | * context error messages. |
| 89 | * This simply means that @handler will be called for subsequent |
| 90 | * error messages while not parsing nor validating. And @ctx will |
| 91 | * be passed as first argument to @handler |
| 92 | * One can simply force messages to be emitted to another FILE * than |
| 93 | * stderr by setting @ctx to this file handle and @handler to NULL. |
| 94 | */ |
| 95 | void |
| 96 | xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) { |
| 97 | xmlGenericErrorContext = ctx; |
| 98 | if (handler != NULL) |
| 99 | xmlGenericError = handler; |
| 100 | else |
| 101 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 102 | } |
| 103 | |
| 104 | /************************************************************************ |
| 105 | * * |
| 106 | * Handling of parsing errors * |
| 107 | * * |
| 108 | ************************************************************************/ |
| 109 | |
| 110 | /** |
| 111 | * xmlParserPrintFileInfo: |
| 112 | * @input: an xmlParserInputPtr input |
| 113 | * |
| 114 | * Displays the associated file and line informations for the current input |
| 115 | */ |
| 116 | |
| 117 | void |
| 118 | xmlParserPrintFileInfo(xmlParserInputPtr input) { |
| 119 | if (input != NULL) { |
| 120 | if (input->filename) |
| 121 | xmlGenericError(xmlGenericErrorContext, |
| 122 | "%s:%d: ", input->filename, |
| 123 | input->line); |
| 124 | else |
| 125 | xmlGenericError(xmlGenericErrorContext, |
| 126 | "Entity: line %d: ", input->line); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * xmlParserPrintFileContext: |
| 132 | * @input: an xmlParserInputPtr input |
| 133 | * |
| 134 | * Displays current context within the input content for error tracking |
| 135 | */ |
| 136 | |
| 137 | void |
| 138 | xmlParserPrintFileContext(xmlParserInputPtr input) { |
| 139 | const xmlChar *cur, *base; |
| 140 | int n; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 141 | xmlChar content[81]; |
| 142 | xmlChar *ctnt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 143 | |
| 144 | if (input == NULL) return; |
| 145 | cur = input->cur; |
| 146 | base = input->base; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 147 | /* skip backwards over any end-of-lines */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 148 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
| 149 | cur--; |
| 150 | } |
| 151 | n = 0; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 152 | /* search backwards for beginning-of-line maximum 80 characters */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 153 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
| 154 | cur--; |
| 155 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 156 | /* search forward for end-of-line maximum 80 characters */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 157 | n = 0; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 158 | ctnt = content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 159 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 160 | *ctnt++ = *cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 161 | n++; |
| 162 | } |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 163 | *ctnt = 0; |
| 164 | xmlGenericError(xmlGenericErrorContext,"%s\n", content); |
| 165 | /* create blank line with problem pointer */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 166 | cur = input->cur; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 167 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
| 168 | cur--; |
| 169 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 170 | n = 0; |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 171 | ctnt = content; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 172 | while ((n++ < 79) && (cur > base) && (*cur != '\n') && (*cur != '\r')) { |
| 173 | *ctnt++ = ' '; |
| 174 | cur--; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 175 | } |
Daniel Veillard | 5a7c345 | 2001-04-26 09:16:13 +0000 | [diff] [blame] | 176 | if (ctnt > content) { |
| 177 | *(--ctnt) = '^'; |
| 178 | *(++ctnt) = 0; |
| 179 | } else { |
| 180 | *ctnt = '^'; |
| 181 | *(++ctnt) = 0; |
| 182 | } |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 183 | xmlGenericError(xmlGenericErrorContext,"%s\n", content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 186 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 187 | /** |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 188 | * xmlGetVarStr: |
| 189 | * @msg: the message format |
| 190 | * @args: a va_list argument list |
| 191 | * |
| 192 | * SGS contribution |
| 193 | * Get an arbitrary-sized string for an error argument |
| 194 | * The caller must free() the returned string |
| 195 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 196 | static char * |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 197 | xmlGetVarStr(const char * msg, va_list args) { |
| 198 | int size; |
| 199 | int length; |
| 200 | int chars, left; |
| 201 | char *str, *larger; |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 202 | va_list ap; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 203 | |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 204 | str = (char *) xmlMalloc(150); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 205 | if (str == NULL) |
| 206 | return(NULL); |
| 207 | |
Daniel Veillard | 92ad210 | 2001-03-27 12:47:33 +0000 | [diff] [blame] | 208 | size = 150; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 209 | length = 0; |
| 210 | |
Daniel Veillard | 7d42b54 | 2001-03-20 13:22:46 +0000 | [diff] [blame] | 211 | while (1) { |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 212 | left = size - length; |
| 213 | /* Try to print in the allocated space. */ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 214 | va_start(msg, ap); |
| 215 | chars = vsnprintf(str + length, left, msg, ap); |
| 216 | va_end(ap); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 217 | /* If that worked, we're done. */ |
| 218 | if ((chars > -1) && (chars < left )) |
| 219 | break; |
| 220 | /* Else try again with more space. */ |
| 221 | if (chars > -1) /* glibc 2.1 */ |
| 222 | size += chars + 1; /* precisely what is needed */ |
| 223 | else /* glibc 2.0 */ |
| 224 | size += 100; |
| 225 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) { |
| 226 | xmlFree(str); |
| 227 | return(NULL); |
| 228 | } |
| 229 | str = larger; |
| 230 | } |
| 231 | return(str); |
| 232 | } |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 233 | #endif |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 234 | |
| 235 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 236 | * xmlParserError: |
| 237 | * @ctx: an XML parser context |
| 238 | * @msg: the message to display/transmit |
| 239 | * @...: extra parameters for the message display |
| 240 | * |
| 241 | * Display and format an error messages, gives file, line, position and |
| 242 | * extra parameters. |
| 243 | */ |
| 244 | void |
| 245 | xmlParserError(void *ctx, const char *msg, ...) |
| 246 | { |
| 247 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 248 | xmlParserInputPtr input = NULL; |
| 249 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 250 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 251 | |
| 252 | if (ctxt != NULL) { |
| 253 | input = ctxt->input; |
| 254 | if ((input != NULL) && (input->filename == NULL) && |
| 255 | (ctxt->inputNr > 1)) { |
| 256 | cur = input; |
| 257 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 258 | } |
| 259 | xmlParserPrintFileInfo(input); |
| 260 | } |
| 261 | |
| 262 | xmlGenericError(xmlGenericErrorContext, "error: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 263 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 264 | xmlGenericError(xmlGenericErrorContext, str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 265 | if (str != NULL) |
| 266 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 267 | |
| 268 | if (ctxt != NULL) { |
| 269 | xmlParserPrintFileContext(input); |
| 270 | if (cur != NULL) { |
| 271 | xmlParserPrintFileInfo(cur); |
| 272 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 273 | xmlParserPrintFileContext(cur); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * xmlParserWarning: |
| 280 | * @ctx: an XML parser context |
| 281 | * @msg: the message to display/transmit |
| 282 | * @...: extra parameters for the message display |
| 283 | * |
| 284 | * Display and format a warning messages, gives file, line, position and |
| 285 | * extra parameters. |
| 286 | */ |
| 287 | void |
| 288 | xmlParserWarning(void *ctx, const char *msg, ...) |
| 289 | { |
| 290 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 291 | xmlParserInputPtr input = NULL; |
| 292 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 293 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 294 | |
| 295 | if (ctxt != NULL) { |
| 296 | input = ctxt->input; |
| 297 | if ((input != NULL) && (input->filename == NULL) && |
| 298 | (ctxt->inputNr > 1)) { |
| 299 | cur = input; |
| 300 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 301 | } |
| 302 | xmlParserPrintFileInfo(input); |
| 303 | } |
| 304 | |
| 305 | xmlGenericError(xmlGenericErrorContext, "warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 306 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 307 | xmlGenericError(xmlGenericErrorContext, str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 308 | if (str != NULL) |
| 309 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 310 | |
| 311 | if (ctxt != NULL) { |
| 312 | xmlParserPrintFileContext(input); |
| 313 | if (cur != NULL) { |
| 314 | xmlParserPrintFileInfo(cur); |
| 315 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 316 | xmlParserPrintFileContext(cur); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /************************************************************************ |
| 322 | * * |
| 323 | * Handling of validation errors * |
| 324 | * * |
| 325 | ************************************************************************/ |
| 326 | |
| 327 | /** |
| 328 | * xmlParserValidityError: |
| 329 | * @ctx: an XML parser context |
| 330 | * @msg: the message to display/transmit |
| 331 | * @...: extra parameters for the message display |
| 332 | * |
| 333 | * Display and format an validity error messages, gives file, |
| 334 | * line, position and extra parameters. |
| 335 | */ |
| 336 | void |
| 337 | xmlParserValidityError(void *ctx, const char *msg, ...) |
| 338 | { |
| 339 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 340 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 341 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 342 | |
| 343 | if (ctxt != NULL) { |
| 344 | input = ctxt->input; |
| 345 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 346 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 347 | |
| 348 | xmlParserPrintFileInfo(input); |
| 349 | } |
| 350 | |
| 351 | xmlGenericError(xmlGenericErrorContext, "validity error: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 352 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 353 | xmlGenericError(xmlGenericErrorContext, str); |
| 354 | if (str != NULL) |
| 355 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 356 | |
| 357 | if (ctxt != NULL) { |
| 358 | xmlParserPrintFileContext(input); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * xmlParserValidityWarning: |
| 364 | * @ctx: an XML parser context |
| 365 | * @msg: the message to display/transmit |
| 366 | * @...: extra parameters for the message display |
| 367 | * |
| 368 | * Display and format a validity warning messages, gives file, line, |
| 369 | * position and extra parameters. |
| 370 | */ |
| 371 | void |
| 372 | xmlParserValidityWarning(void *ctx, const char *msg, ...) |
| 373 | { |
| 374 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 375 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 376 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 377 | |
| 378 | if (ctxt != NULL) { |
| 379 | input = ctxt->input; |
| 380 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 381 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 382 | |
| 383 | xmlParserPrintFileInfo(input); |
| 384 | } |
| 385 | |
| 386 | xmlGenericError(xmlGenericErrorContext, "validity warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 387 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 388 | xmlGenericError(xmlGenericErrorContext, str); |
| 389 | if (str != NULL) |
| 390 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 391 | |
| 392 | if (ctxt != NULL) { |
| 393 | xmlParserPrintFileContext(input); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |