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