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