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