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 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 9 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 12 | #include <string.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #include <stdarg.h> |
| 14 | #include <libxml/parser.h> |
| 15 | #include <libxml/xmlerror.h> |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 16 | #include <libxml/xmlmemory.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 17 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 18 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 19 | void xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED, |
| 20 | const char *msg, |
| 21 | ...); |
| 22 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 23 | #define XML_GET_VAR_STR(msg, str) { \ |
| 24 | int size; \ |
| 25 | int chars; \ |
| 26 | char *larger; \ |
| 27 | va_list ap; \ |
| 28 | \ |
| 29 | str = (char *) xmlMalloc(150); \ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 30 | if (str != NULL) { \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 31 | \ |
| 32 | size = 150; \ |
| 33 | \ |
| 34 | while (1) { \ |
| 35 | va_start(ap, msg); \ |
| 36 | chars = vsnprintf(str, size, msg, ap); \ |
| 37 | va_end(ap); \ |
| 38 | if ((chars > -1) && (chars < size)) \ |
| 39 | break; \ |
| 40 | if (chars > -1) \ |
| 41 | size += chars + 1; \ |
| 42 | else \ |
| 43 | size += 100; \ |
| 44 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 45 | break; \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 46 | } \ |
| 47 | str = larger; \ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 48 | }} \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 49 | } |
Bjorn Reese | 570ff08 | 2001-06-05 12:45:55 +0000 | [diff] [blame] | 50 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 51 | /************************************************************************ |
| 52 | * * |
| 53 | * Handling of out of context errors * |
| 54 | * * |
| 55 | ************************************************************************/ |
| 56 | |
| 57 | /** |
| 58 | * xmlGenericErrorDefaultFunc: |
| 59 | * @ctx: an error context |
| 60 | * @msg: the message to display/transmit |
| 61 | * @...: extra parameters for the message display |
| 62 | * |
| 63 | * Default handler for out of context error messages. |
| 64 | */ |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 65 | void |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 66 | xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 67 | va_list args; |
| 68 | |
| 69 | if (xmlGenericErrorContext == NULL) |
| 70 | xmlGenericErrorContext = (void *) stderr; |
| 71 | |
| 72 | va_start(args, msg); |
| 73 | vfprintf((FILE *)xmlGenericErrorContext, msg, args); |
| 74 | va_end(args); |
| 75 | } |
| 76 | |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 77 | /** |
| 78 | * initGenericErrorDefaultFunc: |
| 79 | * @handler: the handler |
| 80 | * |
| 81 | * Set or reset (if NULL) the default handler for generic errors |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 82 | * to the builtin error function. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 83 | */ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 84 | void |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 85 | initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler) |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 86 | { |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 87 | if (handler == NULL) |
| 88 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 89 | else |
| 90 | (*handler) = xmlGenericErrorDefaultFunc; |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 91 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * xmlSetGenericErrorFunc: |
| 95 | * @ctx: the new error handling context |
| 96 | * @handler: the new handler function |
| 97 | * |
| 98 | * Function to reset the handler and the error context for out of |
| 99 | * context error messages. |
| 100 | * This simply means that @handler will be called for subsequent |
| 101 | * error messages while not parsing nor validating. And @ctx will |
| 102 | * be passed as first argument to @handler |
| 103 | * One can simply force messages to be emitted to another FILE * than |
| 104 | * stderr by setting @ctx to this file handle and @handler to NULL. |
| 105 | */ |
| 106 | void |
| 107 | xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) { |
| 108 | xmlGenericErrorContext = ctx; |
| 109 | if (handler != NULL) |
| 110 | xmlGenericError = handler; |
| 111 | else |
| 112 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 113 | } |
| 114 | |
| 115 | /************************************************************************ |
| 116 | * * |
| 117 | * Handling of parsing errors * |
| 118 | * * |
| 119 | ************************************************************************/ |
| 120 | |
| 121 | /** |
| 122 | * xmlParserPrintFileInfo: |
| 123 | * @input: an xmlParserInputPtr input |
| 124 | * |
| 125 | * Displays the associated file and line informations for the current input |
| 126 | */ |
| 127 | |
| 128 | void |
| 129 | xmlParserPrintFileInfo(xmlParserInputPtr input) { |
| 130 | if (input != NULL) { |
| 131 | if (input->filename) |
| 132 | xmlGenericError(xmlGenericErrorContext, |
| 133 | "%s:%d: ", input->filename, |
| 134 | input->line); |
| 135 | else |
| 136 | xmlGenericError(xmlGenericErrorContext, |
| 137 | "Entity: line %d: ", input->line); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * xmlParserPrintFileContext: |
| 143 | * @input: an xmlParserInputPtr input |
| 144 | * |
| 145 | * Displays current context within the input content for error tracking |
| 146 | */ |
| 147 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 148 | static void |
| 149 | xmlParserPrintFileContextInternal(xmlParserInputPtr input , |
| 150 | xmlGenericErrorFunc channel, void *data ) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 151 | const xmlChar *cur, *base; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 152 | unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */ |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 153 | xmlChar content[81]; /* space for 80 chars + line terminator */ |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 154 | xmlChar *ctnt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 155 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 156 | if (input == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 157 | cur = input->cur; |
| 158 | base = input->base; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 159 | /* skip backwards over any end-of-lines */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 160 | while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 161 | cur--; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 162 | } |
| 163 | n = 0; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 164 | /* search backwards for beginning-of-line (to max buff size) */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 165 | while ((n++ < (sizeof(content)-1)) && (cur > base) && |
| 166 | (*(cur) != '\n') && (*(cur) != '\r')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 167 | cur--; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 168 | if ((*(cur) == '\n') || (*(cur) == '\r')) cur++; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 169 | /* calculate the error position in terms of the current position */ |
| 170 | col = input->cur - cur; |
| 171 | /* search forward for end-of-line (to max buff size) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 172 | n = 0; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 173 | ctnt = content; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 174 | /* copy selected text to our buffer */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 175 | while ((*cur != 0) && (*(cur) != '\n') && |
| 176 | (*(cur) != '\r') && (n < sizeof(content)-1)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 177 | *ctnt++ = *cur++; |
| 178 | n++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 179 | } |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 180 | *ctnt = 0; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 181 | /* print out the selected text */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 182 | channel(data ,"%s\n", content); |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 183 | /* create blank line with problem pointer */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 184 | n = 0; |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 185 | ctnt = content; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 186 | /* (leave buffer space for pointer + line terminator) */ |
| 187 | while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) { |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 188 | if (*(ctnt) != '\t') |
| 189 | *(ctnt) = ' '; |
William M. Brack | 6984830 | 2003-09-22 00:24:51 +0000 | [diff] [blame] | 190 | ctnt++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 191 | } |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 192 | *ctnt++ = '^'; |
| 193 | *ctnt = 0; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 194 | channel(data ,"%s\n", content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 197 | /** |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 198 | * xmlParserPrintFileContext: |
| 199 | * @input: an xmlParserInputPtr input |
| 200 | * |
| 201 | * Displays current context within the input content for error tracking |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 202 | */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 203 | void |
| 204 | xmlParserPrintFileContext(xmlParserInputPtr input) { |
| 205 | xmlParserPrintFileContextInternal(input, xmlGenericError, |
| 206 | xmlGenericErrorContext); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 207 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * xmlReportError: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 211 | * @err: the error |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 212 | * @ctx: the parser context or NULL |
| 213 | * @str: the formatted error message |
| 214 | * |
| 215 | * Report an erro with its context, replace the 4 old error/warning |
| 216 | * routines. |
| 217 | */ |
| 218 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 219 | xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str) |
| 220 | { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 221 | char *file = NULL; |
| 222 | int line = 0; |
| 223 | int code = -1; |
| 224 | int domain; |
| 225 | xmlErrorLevel level; |
| 226 | xmlGenericErrorFunc channel; |
| 227 | xmlParserInputPtr input = NULL; |
| 228 | xmlParserInputPtr cur = NULL; |
| 229 | void *data; |
| 230 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 231 | if (err == NULL) |
| 232 | return; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 233 | |
| 234 | channel = xmlGenericError; |
| 235 | data = xmlGenericErrorContext; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 236 | file = err->file; |
| 237 | line = err->line; |
| 238 | code = err->code; |
| 239 | domain = err->domain; |
| 240 | level = err->level; |
| 241 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 242 | if (code == XML_ERR_OK) |
| 243 | return; |
| 244 | |
| 245 | /* |
| 246 | * Maintain the compatibility with the legacy error handling |
| 247 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 248 | if (ctxt != NULL) { |
| 249 | input = ctxt->input; |
| 250 | if ((input != NULL) && (input->filename == NULL) && |
| 251 | (ctxt->inputNr > 1)) { |
| 252 | cur = input; |
| 253 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 254 | } |
| 255 | if (input != NULL) { |
| 256 | if (input->filename) |
| 257 | channel(data, "%s:%d: ", input->filename, input->line); |
| 258 | else |
| 259 | channel(data, "Entity: line %d: ", input->line); |
| 260 | } |
| 261 | } else { |
| 262 | if (file != NULL) |
| 263 | channel(data, "%s:%d: ", file, line); |
| 264 | else |
| 265 | channel(data, "Entity: line %d: ", line); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 266 | } |
| 267 | if (code == XML_ERR_OK) |
| 268 | return; |
| 269 | switch (domain) { |
| 270 | case XML_FROM_PARSER: |
| 271 | channel(data, "parser "); |
| 272 | break; |
| 273 | case XML_FROM_NAMESPACE: |
| 274 | channel(data, "namespace "); |
| 275 | break; |
| 276 | case XML_FROM_DTD: |
| 277 | channel(data, "validity "); |
| 278 | break; |
| 279 | case XML_FROM_HTML: |
| 280 | channel(data, "HTML parser "); |
| 281 | break; |
| 282 | case XML_FROM_MEMORY: |
| 283 | channel(data, "memory "); |
| 284 | break; |
| 285 | case XML_FROM_OUTPUT: |
| 286 | channel(data, "output "); |
| 287 | break; |
| 288 | case XML_FROM_IO: |
| 289 | channel(data, "I/O "); |
| 290 | break; |
| 291 | case XML_FROM_XINCLUDE: |
| 292 | channel(data, "XInclude "); |
| 293 | break; |
| 294 | case XML_FROM_XPATH: |
| 295 | channel(data, "XPath "); |
| 296 | break; |
| 297 | case XML_FROM_XPOINTER: |
| 298 | channel(data, "parser "); |
| 299 | break; |
| 300 | case XML_FROM_REGEXP: |
| 301 | channel(data, "regexp "); |
| 302 | break; |
| 303 | case XML_FROM_SCHEMAS: |
| 304 | channel(data, "Schemas "); |
| 305 | break; |
| 306 | case XML_FROM_RELAXNG: |
| 307 | channel(data, "Relax-NG "); |
| 308 | break; |
| 309 | case XML_FROM_CATALOG: |
| 310 | channel(data, "Catalog "); |
| 311 | break; |
| 312 | case XML_FROM_C14N: |
| 313 | channel(data, "C14N "); |
| 314 | break; |
| 315 | case XML_FROM_XSLT: |
| 316 | channel(data, "XSLT "); |
| 317 | break; |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | if (code == XML_ERR_OK) |
| 322 | return; |
| 323 | switch (level) { |
| 324 | case XML_ERR_NONE: |
| 325 | channel(data, ": "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 326 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 327 | case XML_ERR_WARNING: |
| 328 | channel(data, "warning : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 329 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 330 | case XML_ERR_ERROR: |
| 331 | channel(data, "error : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 332 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 333 | case XML_ERR_FATAL: |
| 334 | channel(data, "error : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 335 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 336 | } |
| 337 | if (code == XML_ERR_OK) |
| 338 | return; |
| 339 | if (str != NULL) { |
| 340 | channel(data, "%s", str); |
| 341 | } else { |
| 342 | channel(data, "%s", "out of memory error"); |
| 343 | } |
| 344 | if (code == XML_ERR_OK) |
| 345 | return; |
| 346 | |
| 347 | if (ctxt != NULL) { |
| 348 | xmlParserPrintFileContextInternal(input, channel, data); |
| 349 | if (cur != NULL) { |
| 350 | if (cur->filename) |
| 351 | channel(data, "%s:%d: \n", cur->filename, cur->line); |
| 352 | else |
| 353 | channel(data, "Entity: line %d: \n", cur->line); |
| 354 | xmlParserPrintFileContextInternal(cur, channel, data); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 360 | * __xmlRaiseError: |
| 361 | * @channel: the callback channel |
| 362 | * @data: the callback data |
| 363 | * @ctx: the parser context or NULL |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 364 | * @ctx: the parser context or NULL |
| 365 | * @domain: the domain for the error |
| 366 | * @code: the code for the error |
| 367 | * @level: the xmlErrorLevel for the error |
| 368 | * @file: the file source of the error (or NULL) |
| 369 | * @line: the line of the error or 0 if N/A |
| 370 | * @str1: extra string info |
| 371 | * @str2: extra string info |
| 372 | * @str3: extra string info |
| 373 | * @int1: extra int info |
| 374 | * @int2: extra int info |
| 375 | * @msg: the message to display/transmit |
| 376 | * @...: extra parameters for the message display |
| 377 | * |
| 378 | * Update teh appropriate global or contextual error structure, |
| 379 | * then forward the error message down the parser or generic |
| 380 | * error callback handler |
| 381 | */ |
| 382 | void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 383 | __xmlRaiseError(xmlGenericErrorFunc channel, void *data, void *ctx, |
| 384 | void *nod, int domain, int code, xmlErrorLevel level, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 385 | const char *file, int line, const char *str1, |
| 386 | const char *str2, const char *str3, int int1, int int2, |
| 387 | const char *msg, ...) |
| 388 | { |
| 389 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 390 | xmlNodePtr node = (xmlNodePtr) nod; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 391 | char *str = NULL; |
| 392 | xmlParserInputPtr input = NULL; |
| 393 | xmlErrorPtr to = &xmlLastError; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 394 | xmlChar *base = NULL; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 395 | |
| 396 | if (code == XML_ERR_OK) |
| 397 | return; |
| 398 | /* |
| 399 | * Formatting the message |
| 400 | */ |
| 401 | if (msg == NULL) { |
| 402 | str = (char *) xmlStrdup(BAD_CAST "No error message provided"); |
| 403 | } else { |
| 404 | XML_GET_VAR_STR(msg, str); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * specific processing if a parser context is provided |
| 409 | */ |
| 410 | if (ctxt != NULL) { |
| 411 | if (file == NULL) { |
| 412 | input = ctxt->input; |
| 413 | if ((input != NULL) && (input->filename == NULL) && |
| 414 | (ctxt->inputNr > 1)) { |
| 415 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 416 | } |
| 417 | if (input != NULL) { |
| 418 | file = input->filename; |
| 419 | line = input->line; |
| 420 | } |
| 421 | } |
| 422 | to = &ctxt->lastError; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 423 | } else if ((node != NULL) && (file == NULL)) { |
| 424 | int i; |
| 425 | base = xmlNodeGetBase(NULL, node); |
| 426 | for (i = 0; |
| 427 | ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE)); |
| 428 | i++) |
| 429 | node = node->parent; |
| 430 | if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) |
| 431 | line = (int) node->content; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Save the informations about the error |
| 436 | */ |
| 437 | xmlResetError(to); |
| 438 | to->domain = domain; |
| 439 | to->code = code; |
| 440 | to->message = str; |
| 441 | to->level = level; |
| 442 | if (file != NULL) |
| 443 | to->file = (char *) xmlStrdup((const xmlChar *) file); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 444 | else if (base != NULL) { |
| 445 | to->file = (char *) base; |
| 446 | file = (char *) base; |
| 447 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 448 | to->line = line; |
| 449 | if (str1 != NULL) |
| 450 | to->str1 = (char *) xmlStrdup((const xmlChar *) str1); |
| 451 | if (str2 != NULL) |
| 452 | to->str2 = (char *) xmlStrdup((const xmlChar *) str2); |
| 453 | if (str3 != NULL) |
| 454 | to->str3 = (char *) xmlStrdup((const xmlChar *) str3); |
| 455 | to->int1 = int1; |
| 456 | to->int2 = int2; |
| 457 | |
| 458 | /* |
| 459 | * Find the callback channel. |
| 460 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 461 | if ((ctxt != NULL) && (channel == NULL)) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 462 | if (level == XML_ERR_WARNING) |
| 463 | channel = ctxt->sax->warning; |
| 464 | else |
| 465 | channel = ctxt->sax->error; |
| 466 | data = ctxt; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 467 | } else if (channel == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 468 | channel = xmlGenericError; |
| 469 | data = xmlGenericErrorContext; |
| 470 | } |
| 471 | if (channel == NULL) |
| 472 | return; |
| 473 | |
| 474 | if ((channel == xmlParserError) || |
| 475 | (channel == xmlParserWarning) || |
| 476 | (channel == xmlParserValidityError) || |
| 477 | (channel == xmlParserValidityWarning)) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 478 | xmlReportError(to, ctxt, str); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 479 | else |
| 480 | channel(data, "%s", str); |
| 481 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 482 | |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 483 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 484 | * xmlParserError: |
| 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 an error messages, gives file, line, position and |
| 490 | * extra parameters. |
| 491 | */ |
| 492 | void |
| 493 | xmlParserError(void *ctx, const char *msg, ...) |
| 494 | { |
| 495 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 496 | xmlParserInputPtr input = NULL; |
| 497 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 498 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 499 | |
| 500 | if (ctxt != NULL) { |
| 501 | input = ctxt->input; |
| 502 | if ((input != NULL) && (input->filename == NULL) && |
| 503 | (ctxt->inputNr > 1)) { |
| 504 | cur = input; |
| 505 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 506 | } |
| 507 | xmlParserPrintFileInfo(input); |
| 508 | } |
| 509 | |
| 510 | xmlGenericError(xmlGenericErrorContext, "error: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 511 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 512 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 513 | if (str != NULL) |
| 514 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 515 | |
| 516 | if (ctxt != NULL) { |
| 517 | xmlParserPrintFileContext(input); |
| 518 | if (cur != NULL) { |
| 519 | xmlParserPrintFileInfo(cur); |
| 520 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 521 | xmlParserPrintFileContext(cur); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * xmlParserWarning: |
| 528 | * @ctx: an XML parser context |
| 529 | * @msg: the message to display/transmit |
| 530 | * @...: extra parameters for the message display |
| 531 | * |
| 532 | * Display and format a warning messages, gives file, line, position and |
| 533 | * extra parameters. |
| 534 | */ |
| 535 | void |
| 536 | xmlParserWarning(void *ctx, const char *msg, ...) |
| 537 | { |
| 538 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 539 | xmlParserInputPtr input = NULL; |
| 540 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 541 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 542 | |
| 543 | if (ctxt != NULL) { |
| 544 | input = ctxt->input; |
| 545 | if ((input != NULL) && (input->filename == NULL) && |
| 546 | (ctxt->inputNr > 1)) { |
| 547 | cur = input; |
| 548 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 549 | } |
| 550 | xmlParserPrintFileInfo(input); |
| 551 | } |
| 552 | |
| 553 | xmlGenericError(xmlGenericErrorContext, "warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 554 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 555 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 556 | if (str != NULL) |
| 557 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 558 | |
| 559 | if (ctxt != NULL) { |
| 560 | xmlParserPrintFileContext(input); |
| 561 | if (cur != NULL) { |
| 562 | xmlParserPrintFileInfo(cur); |
| 563 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 564 | xmlParserPrintFileContext(cur); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /************************************************************************ |
| 570 | * * |
| 571 | * Handling of validation errors * |
| 572 | * * |
| 573 | ************************************************************************/ |
| 574 | |
| 575 | /** |
| 576 | * xmlParserValidityError: |
| 577 | * @ctx: an XML parser context |
| 578 | * @msg: the message to display/transmit |
| 579 | * @...: extra parameters for the message display |
| 580 | * |
| 581 | * Display and format an validity error messages, gives file, |
| 582 | * line, position and extra parameters. |
| 583 | */ |
| 584 | void |
| 585 | xmlParserValidityError(void *ctx, const char *msg, ...) |
| 586 | { |
| 587 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 588 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 589 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 590 | int len = xmlStrlen((const xmlChar *) msg); |
| 591 | static int had_info = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 592 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 593 | if ((len > 1) && (msg[len - 2] != ':')) { |
| 594 | if (ctxt != NULL) { |
| 595 | input = ctxt->input; |
| 596 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 597 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 598 | |
| 599 | if (had_info == 0) { |
| 600 | xmlParserPrintFileInfo(input); |
| 601 | } |
| 602 | } |
| 603 | xmlGenericError(xmlGenericErrorContext, "validity error: "); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 604 | had_info = 0; |
| 605 | } else { |
| 606 | had_info = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 609 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 610 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 611 | if (str != NULL) |
| 612 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 613 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 614 | if ((ctxt != NULL) && (input != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 615 | xmlParserPrintFileContext(input); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * xmlParserValidityWarning: |
| 621 | * @ctx: an XML parser context |
| 622 | * @msg: the message to display/transmit |
| 623 | * @...: extra parameters for the message display |
| 624 | * |
| 625 | * Display and format a validity warning messages, gives file, line, |
| 626 | * position and extra parameters. |
| 627 | */ |
| 628 | void |
| 629 | xmlParserValidityWarning(void *ctx, const char *msg, ...) |
| 630 | { |
| 631 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 632 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 633 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 634 | int len = xmlStrlen((const xmlChar *) msg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 635 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 636 | if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 637 | input = ctxt->input; |
| 638 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 639 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 640 | |
| 641 | xmlParserPrintFileInfo(input); |
| 642 | } |
| 643 | |
| 644 | xmlGenericError(xmlGenericErrorContext, "validity warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 645 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 646 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 647 | if (str != NULL) |
| 648 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 649 | |
| 650 | if (ctxt != NULL) { |
| 651 | xmlParserPrintFileContext(input); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 656 | /************************************************************************ |
| 657 | * * |
| 658 | * Extended Error Handling * |
| 659 | * * |
| 660 | ************************************************************************/ |
| 661 | |
| 662 | /** |
| 663 | * xmlGetLastError: |
| 664 | * |
| 665 | * Get the last global error registered. This is per thread if compiled |
| 666 | * with thread support. |
| 667 | * |
| 668 | * Returns NULL if no error occured or a pointer to the error |
| 669 | */ |
| 670 | xmlErrorPtr |
| 671 | xmlGetLastError(void) |
| 672 | { |
| 673 | if (xmlLastError.code == XML_ERR_OK) |
| 674 | return (NULL); |
| 675 | return (&xmlLastError); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * xmlResetError: |
| 680 | * @err: pointer to the error. |
| 681 | * |
| 682 | * Cleanup the error. |
| 683 | */ |
| 684 | void |
| 685 | xmlResetError(xmlErrorPtr err) |
| 686 | { |
| 687 | if (err == NULL) |
| 688 | return; |
| 689 | if (err->code == XML_ERR_OK) |
| 690 | return; |
| 691 | if (err->message != NULL) |
| 692 | xmlFree(err->message); |
| 693 | if (err->file != NULL) |
| 694 | xmlFree(err->file); |
| 695 | if (err->str1 != NULL) |
| 696 | xmlFree(err->str1); |
| 697 | if (err->str2 != NULL) |
| 698 | xmlFree(err->str2); |
| 699 | if (err->str3 != NULL) |
| 700 | xmlFree(err->str3); |
| 701 | memset(err, 0, sizeof(xmlError)); |
| 702 | err->code = XML_ERR_OK; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * xmlResetLastError: |
| 707 | * |
| 708 | * Cleanup the last global error registered. For parsing error |
| 709 | * this does not change the well-formedness result. |
| 710 | */ |
| 711 | void |
| 712 | xmlResetLastError(void) |
| 713 | { |
| 714 | if (xmlLastError.code == XML_ERR_OK) |
| 715 | return; |
| 716 | xmlResetError(&xmlLastError); |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * xmlCtxtGetLastError: |
| 721 | * @ctx: an XML parser context |
| 722 | * |
| 723 | * Get the last parsing error registered. |
| 724 | * |
| 725 | * Returns NULL if no error occured or a pointer to the error |
| 726 | */ |
| 727 | xmlErrorPtr |
| 728 | xmlCtxtGetLastError(void *ctx) |
| 729 | { |
| 730 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 731 | |
| 732 | if (ctxt == NULL) |
| 733 | return (NULL); |
| 734 | if (ctxt->lastError.code == XML_ERR_OK) |
| 735 | return (NULL); |
| 736 | return (&ctxt->lastError); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * xmlCtxtResetLastError: |
| 741 | * @ctx: an XML parser context |
| 742 | * |
| 743 | * Cleanup the last global error registered. For parsing error |
| 744 | * this does not change the well-formedness result. |
| 745 | */ |
| 746 | void |
| 747 | xmlCtxtResetLastError(void *ctx) |
| 748 | { |
| 749 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 750 | |
| 751 | if (ctxt == NULL) |
| 752 | return; |
| 753 | if (ctxt->lastError.code == XML_ERR_OK) |
| 754 | return; |
| 755 | xmlResetError(&ctxt->lastError); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * xmlCopyError: |
| 760 | * @from: a source error |
| 761 | * @to: a target error |
| 762 | * |
| 763 | * Save the original error to the new place. |
| 764 | * |
| 765 | * Returns 0 in case of success and -1 in case of error. |
| 766 | */ |
| 767 | int |
| 768 | xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) { |
| 769 | if ((from == NULL) || (to == NULL)) |
| 770 | return(-1); |
| 771 | if (to->message != NULL) |
| 772 | xmlFree(to->message); |
| 773 | if (to->file != NULL) |
| 774 | xmlFree(to->file); |
| 775 | if (to->str1 != NULL) |
| 776 | xmlFree(to->str1); |
| 777 | if (to->str2 != NULL) |
| 778 | xmlFree(to->str2); |
| 779 | if (to->str3 != NULL) |
| 780 | xmlFree(to->str3); |
| 781 | to->domain = from->domain; |
| 782 | to->code = from->code; |
| 783 | to->level = from->level; |
| 784 | to->line = from->line; |
| 785 | to->int1 = from->int1; |
| 786 | to->int2 = from->int2; |
| 787 | if (from->message != NULL) |
| 788 | to->message = (char *) xmlStrdup((xmlChar *) from->message); |
| 789 | else |
| 790 | to->message = NULL; |
| 791 | if (from->file != NULL) |
| 792 | to->file = (char *) xmlStrdup((xmlChar *) from->file); |
| 793 | else |
| 794 | to->file = NULL; |
| 795 | if (from->str1 != NULL) |
| 796 | to->str1 = (char *) xmlStrdup((xmlChar *) from->str1); |
| 797 | else |
| 798 | to->str1 = NULL; |
| 799 | if (from->str2 != NULL) |
| 800 | to->str2 = (char *) xmlStrdup((xmlChar *) from->str2); |
| 801 | else |
| 802 | to->str2 = NULL; |
| 803 | if (from->str3 != NULL) |
| 804 | to->str3 = (char *) xmlStrdup((xmlChar *) from->str3); |
| 805 | else |
| 806 | to->str3 = NULL; |
| 807 | return(0); |
| 808 | } |
| 809 | |