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 | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 19 | void XMLCDECL xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED, |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 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) { \ |
Daniel Veillard | dbf7bfe | 2005-10-28 08:25:51 +0000 | [diff] [blame] | 24 | int size, prev_size = -1; \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 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 | \ |
Daniel Veillard | fa75097 | 2008-04-03 07:31:25 +0000 | [diff] [blame] | 34 | while (size < 64000) { \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 35 | va_start(ap, msg); \ |
| 36 | chars = vsnprintf(str, size, msg, ap); \ |
| 37 | va_end(ap); \ |
Daniel Veillard | dbf7bfe | 2005-10-28 08:25:51 +0000 | [diff] [blame] | 38 | if ((chars > -1) && (chars < size)) { \ |
| 39 | if (prev_size == chars) { \ |
| 40 | break; \ |
| 41 | } else { \ |
| 42 | prev_size = chars; \ |
| 43 | } \ |
| 44 | } \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 45 | if (chars > -1) \ |
| 46 | size += chars + 1; \ |
| 47 | else \ |
| 48 | size += 100; \ |
| 49 | if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 50 | break; \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 51 | } \ |
| 52 | str = larger; \ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 53 | }} \ |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 54 | } |
Bjorn Reese | 570ff08 | 2001-06-05 12:45:55 +0000 | [diff] [blame] | 55 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 56 | /************************************************************************ |
| 57 | * * |
| 58 | * Handling of out of context errors * |
| 59 | * * |
| 60 | ************************************************************************/ |
| 61 | |
| 62 | /** |
| 63 | * xmlGenericErrorDefaultFunc: |
| 64 | * @ctx: an error context |
| 65 | * @msg: the message to display/transmit |
| 66 | * @...: extra parameters for the message display |
| 67 | * |
| 68 | * Default handler for out of context error messages. |
| 69 | */ |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 70 | void XMLCDECL |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 71 | xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 72 | va_list args; |
| 73 | |
| 74 | if (xmlGenericErrorContext == NULL) |
| 75 | xmlGenericErrorContext = (void *) stderr; |
| 76 | |
| 77 | va_start(args, msg); |
| 78 | vfprintf((FILE *)xmlGenericErrorContext, msg, args); |
| 79 | va_end(args); |
| 80 | } |
| 81 | |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 82 | /** |
| 83 | * initGenericErrorDefaultFunc: |
| 84 | * @handler: the handler |
| 85 | * |
| 86 | * Set or reset (if NULL) the default handler for generic errors |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 87 | * to the builtin error function. |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 88 | */ |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 89 | void |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 90 | initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler) |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 91 | { |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 92 | if (handler == NULL) |
| 93 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 94 | else |
Daniel Veillard | da0ff5d | 2004-04-20 09:45:26 +0000 | [diff] [blame] | 95 | xmlGenericError = (*handler); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 96 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * xmlSetGenericErrorFunc: |
| 100 | * @ctx: the new error handling context |
| 101 | * @handler: the new handler function |
| 102 | * |
| 103 | * Function to reset the handler and the error context for out of |
| 104 | * context error messages. |
| 105 | * This simply means that @handler will be called for subsequent |
| 106 | * error messages while not parsing nor validating. And @ctx will |
| 107 | * be passed as first argument to @handler |
| 108 | * One can simply force messages to be emitted to another FILE * than |
| 109 | * stderr by setting @ctx to this file handle and @handler to NULL. |
Daniel Veillard | da3b29a | 2004-08-14 11:15:13 +0000 | [diff] [blame] | 110 | * For multi-threaded applications, this must be set separately for each thread. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 111 | */ |
| 112 | void |
| 113 | xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) { |
| 114 | xmlGenericErrorContext = ctx; |
| 115 | if (handler != NULL) |
| 116 | xmlGenericError = handler; |
| 117 | else |
| 118 | xmlGenericError = xmlGenericErrorDefaultFunc; |
| 119 | } |
| 120 | |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 121 | /** |
| 122 | * xmlSetStructuredErrorFunc: |
| 123 | * @ctx: the new error handling context |
| 124 | * @handler: the new handler function |
| 125 | * |
| 126 | * Function to reset the handler and the error context for out of |
| 127 | * context structured error messages. |
| 128 | * This simply means that @handler will be called for subsequent |
| 129 | * error messages while not parsing nor validating. And @ctx will |
| 130 | * be passed as first argument to @handler |
Daniel Veillard | da3b29a | 2004-08-14 11:15:13 +0000 | [diff] [blame] | 131 | * For multi-threaded applications, this must be set separately for each thread. |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 132 | */ |
| 133 | void |
| 134 | xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) { |
Wang Lam | 1de382e | 2009-08-24 17:34:25 +0200 | [diff] [blame] | 135 | xmlStructuredErrorContext = ctx; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 136 | xmlStructuredError = handler; |
| 137 | } |
| 138 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 139 | /************************************************************************ |
| 140 | * * |
| 141 | * Handling of parsing errors * |
| 142 | * * |
| 143 | ************************************************************************/ |
| 144 | |
| 145 | /** |
| 146 | * xmlParserPrintFileInfo: |
| 147 | * @input: an xmlParserInputPtr input |
| 148 | * |
| 149 | * Displays the associated file and line informations for the current input |
| 150 | */ |
| 151 | |
| 152 | void |
| 153 | xmlParserPrintFileInfo(xmlParserInputPtr input) { |
| 154 | if (input != NULL) { |
| 155 | if (input->filename) |
| 156 | xmlGenericError(xmlGenericErrorContext, |
| 157 | "%s:%d: ", input->filename, |
| 158 | input->line); |
| 159 | else |
| 160 | xmlGenericError(xmlGenericErrorContext, |
| 161 | "Entity: line %d: ", input->line); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * xmlParserPrintFileContext: |
| 167 | * @input: an xmlParserInputPtr input |
| 168 | * |
| 169 | * Displays current context within the input content for error tracking |
| 170 | */ |
| 171 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 172 | static void |
| 173 | xmlParserPrintFileContextInternal(xmlParserInputPtr input , |
| 174 | xmlGenericErrorFunc channel, void *data ) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 175 | const xmlChar *cur, *base; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 176 | 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] | 177 | xmlChar content[81]; /* space for 80 chars + line terminator */ |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 178 | xmlChar *ctnt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 179 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 180 | if (input == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 181 | cur = input->cur; |
| 182 | base = input->base; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 183 | /* skip backwards over any end-of-lines */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 184 | while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 185 | cur--; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 186 | } |
| 187 | n = 0; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 188 | /* search backwards for beginning-of-line (to max buff size) */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 189 | while ((n++ < (sizeof(content)-1)) && (cur > base) && |
| 190 | (*(cur) != '\n') && (*(cur) != '\r')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 191 | cur--; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 192 | if ((*(cur) == '\n') || (*(cur) == '\r')) cur++; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 193 | /* calculate the error position in terms of the current position */ |
| 194 | col = input->cur - cur; |
| 195 | /* search forward for end-of-line (to max buff size) */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 196 | n = 0; |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 197 | ctnt = content; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 198 | /* copy selected text to our buffer */ |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 199 | while ((*cur != 0) && (*(cur) != '\n') && |
| 200 | (*(cur) != '\r') && (n < sizeof(content)-1)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 201 | *ctnt++ = *cur++; |
| 202 | n++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 203 | } |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 204 | *ctnt = 0; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 205 | /* print out the selected text */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 206 | channel(data ,"%s\n", content); |
Daniel Veillard | 2be3064 | 2001-03-27 00:32:28 +0000 | [diff] [blame] | 207 | /* create blank line with problem pointer */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 208 | n = 0; |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 209 | ctnt = content; |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 210 | /* (leave buffer space for pointer + line terminator) */ |
| 211 | while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) { |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 212 | if (*(ctnt) != '\t') |
| 213 | *(ctnt) = ' '; |
William M. Brack | 6984830 | 2003-09-22 00:24:51 +0000 | [diff] [blame] | 214 | ctnt++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 215 | } |
William M. Brack | 3dd57f7 | 2003-05-13 02:06:18 +0000 | [diff] [blame] | 216 | *ctnt++ = '^'; |
| 217 | *ctnt = 0; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 218 | channel(data ,"%s\n", content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 221 | /** |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 222 | * xmlParserPrintFileContext: |
| 223 | * @input: an xmlParserInputPtr input |
| 224 | * |
| 225 | * Displays current context within the input content for error tracking |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 226 | */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 227 | void |
| 228 | xmlParserPrintFileContext(xmlParserInputPtr input) { |
| 229 | xmlParserPrintFileContextInternal(input, xmlGenericError, |
| 230 | xmlGenericErrorContext); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 231 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * xmlReportError: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 235 | * @err: the error |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 236 | * @ctx: the parser context or NULL |
| 237 | * @str: the formatted error message |
| 238 | * |
| 239 | * Report an erro with its context, replace the 4 old error/warning |
| 240 | * routines. |
| 241 | */ |
| 242 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 243 | xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str, |
| 244 | xmlGenericErrorFunc channel, void *data) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 245 | { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 246 | char *file = NULL; |
| 247 | int line = 0; |
| 248 | int code = -1; |
| 249 | int domain; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 250 | const xmlChar *name = NULL; |
| 251 | xmlNodePtr node; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 252 | xmlErrorLevel level; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 253 | xmlParserInputPtr input = NULL; |
| 254 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 255 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 256 | if (err == NULL) |
| 257 | return; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 258 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 259 | if (channel == NULL) { |
| 260 | channel = xmlGenericError; |
| 261 | data = xmlGenericErrorContext; |
| 262 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 263 | file = err->file; |
| 264 | line = err->line; |
| 265 | code = err->code; |
| 266 | domain = err->domain; |
| 267 | level = err->level; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 268 | node = err->node; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 269 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 270 | if (code == XML_ERR_OK) |
| 271 | return; |
| 272 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 273 | if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) |
| 274 | name = node->name; |
| 275 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 276 | /* |
| 277 | * Maintain the compatibility with the legacy error handling |
| 278 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 279 | if (ctxt != NULL) { |
| 280 | input = ctxt->input; |
| 281 | if ((input != NULL) && (input->filename == NULL) && |
| 282 | (ctxt->inputNr > 1)) { |
| 283 | cur = input; |
| 284 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 285 | } |
| 286 | if (input != NULL) { |
| 287 | if (input->filename) |
| 288 | channel(data, "%s:%d: ", input->filename, input->line); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 289 | else if ((line != 0) && (domain == XML_FROM_PARSER)) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 290 | channel(data, "Entity: line %d: ", input->line); |
| 291 | } |
| 292 | } else { |
| 293 | if (file != NULL) |
| 294 | channel(data, "%s:%d: ", file, line); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 295 | else if ((line != 0) && (domain == XML_FROM_PARSER)) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 296 | channel(data, "Entity: line %d: ", line); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 297 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 298 | if (name != NULL) { |
| 299 | channel(data, "element %s: ", name); |
| 300 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 301 | switch (domain) { |
| 302 | case XML_FROM_PARSER: |
| 303 | channel(data, "parser "); |
| 304 | break; |
| 305 | case XML_FROM_NAMESPACE: |
| 306 | channel(data, "namespace "); |
| 307 | break; |
| 308 | case XML_FROM_DTD: |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 309 | case XML_FROM_VALID: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 310 | channel(data, "validity "); |
| 311 | break; |
| 312 | case XML_FROM_HTML: |
| 313 | channel(data, "HTML parser "); |
| 314 | break; |
| 315 | case XML_FROM_MEMORY: |
| 316 | channel(data, "memory "); |
| 317 | break; |
| 318 | case XML_FROM_OUTPUT: |
| 319 | channel(data, "output "); |
| 320 | break; |
| 321 | case XML_FROM_IO: |
| 322 | channel(data, "I/O "); |
| 323 | break; |
| 324 | case XML_FROM_XINCLUDE: |
| 325 | channel(data, "XInclude "); |
| 326 | break; |
| 327 | case XML_FROM_XPATH: |
| 328 | channel(data, "XPath "); |
| 329 | break; |
| 330 | case XML_FROM_XPOINTER: |
| 331 | channel(data, "parser "); |
| 332 | break; |
| 333 | case XML_FROM_REGEXP: |
| 334 | channel(data, "regexp "); |
| 335 | break; |
Daniel Veillard | ce1648b | 2005-01-04 15:10:22 +0000 | [diff] [blame] | 336 | case XML_FROM_MODULE: |
| 337 | channel(data, "module "); |
| 338 | break; |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 339 | case XML_FROM_SCHEMASV: |
Daniel Veillard | 87db3a8 | 2003-10-10 10:52:58 +0000 | [diff] [blame] | 340 | channel(data, "Schemas validity "); |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 341 | break; |
| 342 | case XML_FROM_SCHEMASP: |
Daniel Veillard | 87db3a8 | 2003-10-10 10:52:58 +0000 | [diff] [blame] | 343 | channel(data, "Schemas parser "); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 344 | break; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 345 | case XML_FROM_RELAXNGP: |
| 346 | channel(data, "Relax-NG parser "); |
| 347 | break; |
| 348 | case XML_FROM_RELAXNGV: |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 349 | channel(data, "Relax-NG validity "); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 350 | break; |
| 351 | case XML_FROM_CATALOG: |
| 352 | channel(data, "Catalog "); |
| 353 | break; |
| 354 | case XML_FROM_C14N: |
| 355 | channel(data, "C14N "); |
| 356 | break; |
| 357 | case XML_FROM_XSLT: |
| 358 | channel(data, "XSLT "); |
| 359 | break; |
Daniel Veillard | 1fc3ed0 | 2005-08-24 12:46:09 +0000 | [diff] [blame] | 360 | case XML_FROM_I18N: |
| 361 | channel(data, "encoding "); |
| 362 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 363 | default: |
| 364 | break; |
| 365 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 366 | switch (level) { |
| 367 | case XML_ERR_NONE: |
| 368 | channel(data, ": "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 369 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 370 | case XML_ERR_WARNING: |
| 371 | channel(data, "warning : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 372 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 373 | case XML_ERR_ERROR: |
| 374 | channel(data, "error : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 375 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 376 | case XML_ERR_FATAL: |
| 377 | channel(data, "error : "); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 378 | break; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 379 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 380 | if (str != NULL) { |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 381 | int len; |
| 382 | len = xmlStrlen((const xmlChar *)str); |
| 383 | if ((len > 0) && (str[len - 1] != '\n')) |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 384 | channel(data, "%s\n", str); |
Daniel Veillard | a885622 | 2003-10-08 19:26:03 +0000 | [diff] [blame] | 385 | else |
| 386 | channel(data, "%s", str); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 387 | } else { |
Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 388 | channel(data, "%s\n", "out of memory error"); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 389 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 390 | |
| 391 | if (ctxt != NULL) { |
| 392 | xmlParserPrintFileContextInternal(input, channel, data); |
| 393 | if (cur != NULL) { |
| 394 | if (cur->filename) |
| 395 | channel(data, "%s:%d: \n", cur->filename, cur->line); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 396 | else if ((line != 0) && (domain == XML_FROM_PARSER)) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 397 | channel(data, "Entity: line %d: \n", cur->line); |
| 398 | xmlParserPrintFileContextInternal(cur, channel, data); |
| 399 | } |
| 400 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 401 | if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) && |
| 402 | (err->int1 < 100) && |
| 403 | (err->int1 < xmlStrlen((const xmlChar *)err->str1))) { |
| 404 | xmlChar buf[150]; |
| 405 | int i; |
| 406 | |
| 407 | channel(data, "%s\n", err->str1); |
| 408 | for (i=0;i < err->int1;i++) |
| 409 | buf[i] = ' '; |
| 410 | buf[i++] = '^'; |
| 411 | buf[i] = 0; |
| 412 | channel(data, "%s\n", buf); |
| 413 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 417 | * __xmlRaiseError: |
William M. Brack | d233e39 | 2004-05-16 03:12:08 +0000 | [diff] [blame] | 418 | * @schannel: the structured callback channel |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 419 | * @channel: the old callback channel |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 420 | * @data: the callback data |
| 421 | * @ctx: the parser context or NULL |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 422 | * @ctx: the parser context or NULL |
| 423 | * @domain: the domain for the error |
| 424 | * @code: the code for the error |
| 425 | * @level: the xmlErrorLevel for the error |
| 426 | * @file: the file source of the error (or NULL) |
| 427 | * @line: the line of the error or 0 if N/A |
| 428 | * @str1: extra string info |
| 429 | * @str2: extra string info |
| 430 | * @str3: extra string info |
| 431 | * @int1: extra int info |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 432 | * @col: column number of the error or 0 if N/A |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 433 | * @msg: the message to display/transmit |
| 434 | * @...: extra parameters for the message display |
| 435 | * |
William M. Brack | d233e39 | 2004-05-16 03:12:08 +0000 | [diff] [blame] | 436 | * Update the appropriate global or contextual error structure, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 437 | * then forward the error message down the parser or generic |
| 438 | * error callback handler |
| 439 | */ |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 440 | void XMLCDECL |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 441 | __xmlRaiseError(xmlStructuredErrorFunc schannel, |
| 442 | xmlGenericErrorFunc channel, void *data, void *ctx, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 443 | void *nod, int domain, int code, xmlErrorLevel level, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 444 | const char *file, int line, const char *str1, |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 445 | const char *str2, const char *str3, int int1, int col, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 446 | const char *msg, ...) |
| 447 | { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 448 | xmlParserCtxtPtr ctxt = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 449 | xmlNodePtr node = (xmlNodePtr) nod; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 450 | char *str = NULL; |
| 451 | xmlParserInputPtr input = NULL; |
| 452 | xmlErrorPtr to = &xmlLastError; |
William M. Brack | d040752 | 2004-10-02 03:54:00 +0000 | [diff] [blame] | 453 | xmlNodePtr baseptr = NULL; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 454 | |
Dmitry V. Levin | 111d705 | 2011-02-23 22:14:19 +0800 | [diff] [blame] | 455 | if (code == XML_ERR_OK) |
| 456 | return; |
Daniel Veillard | b5fa020 | 2003-12-08 17:41:29 +0000 | [diff] [blame] | 457 | if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING)) |
| 458 | return; |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 459 | if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) || |
| 460 | (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) || |
Daniel Veillard | 370ba3d | 2004-10-25 16:23:56 +0000 | [diff] [blame] | 461 | (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) { |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 462 | ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 463 | if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) && |
Dmitry V. Levin | 241d4a1 | 2011-02-23 22:30:59 +0800 | [diff] [blame] | 464 | (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 465 | (ctxt->sax->serror != NULL)) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 466 | schannel = ctxt->sax->serror; |
Dmitry V. Levin | 241d4a1 | 2011-02-23 22:30:59 +0800 | [diff] [blame] | 467 | data = ctxt->userData; |
| 468 | } |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 469 | } |
William M. Brack | cd3628b | 2004-07-25 21:07:29 +0000 | [diff] [blame] | 470 | /* |
| 471 | * Check if structured error handler set |
| 472 | */ |
| 473 | if (schannel == NULL) { |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 474 | schannel = xmlStructuredError; |
William M. Brack | cd3628b | 2004-07-25 21:07:29 +0000 | [diff] [blame] | 475 | /* |
| 476 | * if user has defined handler, change data ptr to user's choice |
| 477 | */ |
| 478 | if (schannel != NULL) |
Wang Lam | 1de382e | 2009-08-24 17:34:25 +0200 | [diff] [blame] | 479 | data = xmlStructuredErrorContext; |
William M. Brack | cd3628b | 2004-07-25 21:07:29 +0000 | [diff] [blame] | 480 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 481 | /* |
| 482 | * Formatting the message |
| 483 | */ |
| 484 | if (msg == NULL) { |
| 485 | str = (char *) xmlStrdup(BAD_CAST "No error message provided"); |
| 486 | } else { |
| 487 | XML_GET_VAR_STR(msg, str); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * specific processing if a parser context is provided |
| 492 | */ |
| 493 | if (ctxt != NULL) { |
| 494 | if (file == NULL) { |
| 495 | input = ctxt->input; |
| 496 | if ((input != NULL) && (input->filename == NULL) && |
| 497 | (ctxt->inputNr > 1)) { |
| 498 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 499 | } |
| 500 | if (input != NULL) { |
| 501 | file = input->filename; |
| 502 | line = input->line; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 503 | col = input->col; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | to = &ctxt->lastError; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 507 | } else if ((node != NULL) && (file == NULL)) { |
| 508 | int i; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 509 | |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 510 | if ((node->doc != NULL) && (node->doc->URL != NULL)) { |
William M. Brack | d040752 | 2004-10-02 03:54:00 +0000 | [diff] [blame] | 511 | baseptr = node; |
Daniel Veillard | 8ce01ce | 2005-08-25 20:14:38 +0000 | [diff] [blame] | 512 | /* file = (const char *) node->doc->URL; */ |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 513 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 514 | for (i = 0; |
| 515 | ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE)); |
| 516 | i++) |
| 517 | node = node->parent; |
William M. Brack | d040752 | 2004-10-02 03:54:00 +0000 | [diff] [blame] | 518 | if ((baseptr == NULL) && (node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 519 | (node->doc != NULL) && (node->doc->URL != NULL)) |
William M. Brack | d040752 | 2004-10-02 03:54:00 +0000 | [diff] [blame] | 520 | baseptr = node; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 521 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 522 | if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) |
Daniel Veillard | 3e35f8e | 2003-10-21 00:05:38 +0000 | [diff] [blame] | 523 | line = node->line; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* |
William M. Brack | d233e39 | 2004-05-16 03:12:08 +0000 | [diff] [blame] | 527 | * Save the information about the error |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 528 | */ |
| 529 | xmlResetError(to); |
| 530 | to->domain = domain; |
| 531 | to->code = code; |
| 532 | to->message = str; |
| 533 | to->level = level; |
| 534 | if (file != NULL) |
| 535 | to->file = (char *) xmlStrdup((const xmlChar *) file); |
William M. Brack | d040752 | 2004-10-02 03:54:00 +0000 | [diff] [blame] | 536 | else if (baseptr != NULL) { |
| 537 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 538 | /* |
| 539 | * We check if the error is within an XInclude section and, |
| 540 | * if so, attempt to print out the href of the XInclude instead |
| 541 | * of the usual "base" (doc->URL) for the node (bug 152623). |
| 542 | */ |
| 543 | xmlNodePtr prev = baseptr; |
| 544 | int inclcount = 0; |
| 545 | while (prev != NULL) { |
| 546 | if (prev->prev == NULL) |
| 547 | prev = prev->parent; |
| 548 | else { |
| 549 | prev = prev->prev; |
| 550 | if (prev->type == XML_XINCLUDE_START) { |
| 551 | if (--inclcount < 0) |
| 552 | break; |
| 553 | } else if (prev->type == XML_XINCLUDE_END) |
| 554 | inclcount++; |
| 555 | } |
| 556 | } |
| 557 | if (prev != NULL) { |
Daniel Veillard | 8ce01ce | 2005-08-25 20:14:38 +0000 | [diff] [blame] | 558 | if (prev->type == XML_XINCLUDE_START) { |
| 559 | prev->type = XML_ELEMENT_NODE; |
| 560 | to->file = (char *) xmlGetProp(prev, BAD_CAST "href"); |
| 561 | prev->type = XML_XINCLUDE_START; |
| 562 | } else { |
| 563 | to->file = (char *) xmlGetProp(prev, BAD_CAST "href"); |
| 564 | } |
William M. Brack | d040752 | 2004-10-02 03:54:00 +0000 | [diff] [blame] | 565 | } else |
| 566 | #endif |
| 567 | to->file = (char *) xmlStrdup(baseptr->doc->URL); |
Daniel Veillard | 8ce01ce | 2005-08-25 20:14:38 +0000 | [diff] [blame] | 568 | if ((to->file == NULL) && (node != NULL) && (node->doc != NULL)) { |
| 569 | to->file = (char *) xmlStrdup(node->doc->URL); |
| 570 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 571 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 572 | to->line = line; |
| 573 | if (str1 != NULL) |
| 574 | to->str1 = (char *) xmlStrdup((const xmlChar *) str1); |
| 575 | if (str2 != NULL) |
| 576 | to->str2 = (char *) xmlStrdup((const xmlChar *) str2); |
| 577 | if (str3 != NULL) |
| 578 | to->str3 = (char *) xmlStrdup((const xmlChar *) str3); |
| 579 | to->int1 = int1; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 580 | to->int2 = col; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 581 | to->node = node; |
Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 582 | to->ctxt = ctx; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 583 | |
Daniel Veillard | d34b0b8 | 2004-01-02 20:26:01 +0000 | [diff] [blame] | 584 | if (to != &xmlLastError) |
| 585 | xmlCopyError(to,&xmlLastError); |
| 586 | |
Dmitry V. Levin | c2a0fdc | 2011-02-23 22:44:05 +0800 | [diff] [blame] | 587 | if (schannel != NULL) { |
| 588 | schannel(data, to); |
| 589 | return; |
| 590 | } |
| 591 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 592 | /* |
William M. Brack | cd3628b | 2004-07-25 21:07:29 +0000 | [diff] [blame] | 593 | * Find the callback channel if channel param is NULL |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 594 | */ |
Wang Lam | 1de382e | 2009-08-24 17:34:25 +0200 | [diff] [blame] | 595 | if ((ctxt != NULL) && (channel == NULL) && |
| 596 | (xmlStructuredError == NULL) && (ctxt->sax != NULL)) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 597 | if (level == XML_ERR_WARNING) |
| 598 | channel = ctxt->sax->warning; |
| 599 | else |
| 600 | channel = ctxt->sax->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 601 | data = ctxt->userData; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 602 | } else if (channel == NULL) { |
Dmitry V. Levin | c2a0fdc | 2011-02-23 22:44:05 +0800 | [diff] [blame] | 603 | channel = xmlGenericError; |
| 604 | if (!data) |
| 605 | data = xmlGenericErrorContext; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 606 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 607 | if (channel == NULL) |
| 608 | return; |
| 609 | |
| 610 | if ((channel == xmlParserError) || |
| 611 | (channel == xmlParserWarning) || |
| 612 | (channel == xmlParserValidityError) || |
| 613 | (channel == xmlParserValidityWarning)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 614 | xmlReportError(to, ctxt, str, NULL, NULL); |
| 615 | else if ((channel == (xmlGenericErrorFunc) fprintf) || |
| 616 | (channel == xmlGenericErrorDefaultFunc)) |
| 617 | xmlReportError(to, ctxt, str, channel, data); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 618 | else |
| 619 | channel(data, "%s", str); |
| 620 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 621 | |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 622 | /** |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 623 | * __xmlSimpleError: |
| 624 | * @domain: where the error comes from |
| 625 | * @code: the error code |
| 626 | * @node: the context node |
| 627 | * @extra: extra informations |
| 628 | * |
| 629 | * Handle an out of memory condition |
| 630 | */ |
| 631 | void |
| 632 | __xmlSimpleError(int domain, int code, xmlNodePtr node, |
| 633 | const char *msg, const char *extra) |
| 634 | { |
| 635 | |
| 636 | if (code == XML_ERR_NO_MEMORY) { |
| 637 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 638 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 639 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 640 | NULL, NULL, 0, 0, |
| 641 | "Memory allocation failed : %s\n", extra); |
| 642 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 643 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 644 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 645 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 646 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 647 | __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain, |
Daniel Veillard | 18ec16e | 2003-10-07 23:16:40 +0000 | [diff] [blame] | 648 | code, XML_ERR_ERROR, NULL, 0, extra, |
| 649 | NULL, NULL, 0, 0, msg, extra); |
| 650 | } |
| 651 | } |
| 652 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 653 | * xmlParserError: |
| 654 | * @ctx: an XML parser context |
| 655 | * @msg: the message to display/transmit |
| 656 | * @...: extra parameters for the message display |
| 657 | * |
| 658 | * Display and format an error messages, gives file, line, position and |
| 659 | * extra parameters. |
| 660 | */ |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 661 | void XMLCDECL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 662 | xmlParserError(void *ctx, const char *msg, ...) |
| 663 | { |
| 664 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 665 | xmlParserInputPtr input = NULL; |
| 666 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 667 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 668 | |
| 669 | if (ctxt != NULL) { |
| 670 | input = ctxt->input; |
| 671 | if ((input != NULL) && (input->filename == NULL) && |
| 672 | (ctxt->inputNr > 1)) { |
| 673 | cur = input; |
| 674 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 675 | } |
| 676 | xmlParserPrintFileInfo(input); |
| 677 | } |
| 678 | |
| 679 | xmlGenericError(xmlGenericErrorContext, "error: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 680 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 681 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 682 | if (str != NULL) |
| 683 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 684 | |
| 685 | if (ctxt != NULL) { |
| 686 | xmlParserPrintFileContext(input); |
| 687 | if (cur != NULL) { |
| 688 | xmlParserPrintFileInfo(cur); |
| 689 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 690 | xmlParserPrintFileContext(cur); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * xmlParserWarning: |
| 697 | * @ctx: an XML parser context |
| 698 | * @msg: the message to display/transmit |
| 699 | * @...: extra parameters for the message display |
| 700 | * |
| 701 | * Display and format a warning messages, gives file, line, position and |
| 702 | * extra parameters. |
| 703 | */ |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 704 | void XMLCDECL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 705 | xmlParserWarning(void *ctx, const char *msg, ...) |
| 706 | { |
| 707 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 708 | xmlParserInputPtr input = NULL; |
| 709 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 710 | char * str; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 711 | |
| 712 | if (ctxt != NULL) { |
| 713 | input = ctxt->input; |
| 714 | if ((input != NULL) && (input->filename == NULL) && |
| 715 | (ctxt->inputNr > 1)) { |
| 716 | cur = input; |
| 717 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 718 | } |
| 719 | xmlParserPrintFileInfo(input); |
| 720 | } |
| 721 | |
| 722 | xmlGenericError(xmlGenericErrorContext, "warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 723 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 724 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 725 | if (str != NULL) |
| 726 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 727 | |
| 728 | if (ctxt != NULL) { |
| 729 | xmlParserPrintFileContext(input); |
| 730 | if (cur != NULL) { |
| 731 | xmlParserPrintFileInfo(cur); |
| 732 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 733 | xmlParserPrintFileContext(cur); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /************************************************************************ |
| 739 | * * |
| 740 | * Handling of validation errors * |
| 741 | * * |
| 742 | ************************************************************************/ |
| 743 | |
| 744 | /** |
| 745 | * xmlParserValidityError: |
| 746 | * @ctx: an XML parser context |
| 747 | * @msg: the message to display/transmit |
| 748 | * @...: extra parameters for the message display |
| 749 | * |
| 750 | * Display and format an validity error messages, gives file, |
| 751 | * line, position and extra parameters. |
| 752 | */ |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 753 | void XMLCDECL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 754 | xmlParserValidityError(void *ctx, const char *msg, ...) |
| 755 | { |
| 756 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 757 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 758 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 759 | int len = xmlStrlen((const xmlChar *) msg); |
| 760 | static int had_info = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 761 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 762 | if ((len > 1) && (msg[len - 2] != ':')) { |
| 763 | if (ctxt != NULL) { |
| 764 | input = ctxt->input; |
| 765 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 766 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 767 | |
| 768 | if (had_info == 0) { |
| 769 | xmlParserPrintFileInfo(input); |
| 770 | } |
| 771 | } |
| 772 | xmlGenericError(xmlGenericErrorContext, "validity error: "); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 773 | had_info = 0; |
| 774 | } else { |
| 775 | had_info = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 778 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 779 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 780 | if (str != NULL) |
| 781 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 782 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 783 | if ((ctxt != NULL) && (input != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 784 | xmlParserPrintFileContext(input); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * xmlParserValidityWarning: |
| 790 | * @ctx: an XML parser context |
| 791 | * @msg: the message to display/transmit |
| 792 | * @...: extra parameters for the message display |
| 793 | * |
| 794 | * Display and format a validity warning messages, gives file, line, |
| 795 | * position and extra parameters. |
| 796 | */ |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 797 | void XMLCDECL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 798 | xmlParserValidityWarning(void *ctx, const char *msg, ...) |
| 799 | { |
| 800 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 801 | xmlParserInputPtr input = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 802 | char * str; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 803 | int len = xmlStrlen((const xmlChar *) msg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 804 | |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 805 | if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 806 | input = ctxt->input; |
| 807 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 808 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 809 | |
| 810 | xmlParserPrintFileInfo(input); |
| 811 | } |
| 812 | |
| 813 | xmlGenericError(xmlGenericErrorContext, "validity warning: "); |
Daniel Veillard | 1c43dbf | 2001-06-05 17:12:52 +0000 | [diff] [blame] | 814 | XML_GET_VAR_STR(msg, str); |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 815 | xmlGenericError(xmlGenericErrorContext, "%s", str); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 816 | if (str != NULL) |
| 817 | xmlFree(str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 818 | |
| 819 | if (ctxt != NULL) { |
| 820 | xmlParserPrintFileContext(input); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 825 | /************************************************************************ |
| 826 | * * |
| 827 | * Extended Error Handling * |
| 828 | * * |
| 829 | ************************************************************************/ |
| 830 | |
| 831 | /** |
| 832 | * xmlGetLastError: |
| 833 | * |
| 834 | * Get the last global error registered. This is per thread if compiled |
| 835 | * with thread support. |
| 836 | * |
| 837 | * Returns NULL if no error occured or a pointer to the error |
| 838 | */ |
| 839 | xmlErrorPtr |
| 840 | xmlGetLastError(void) |
| 841 | { |
| 842 | if (xmlLastError.code == XML_ERR_OK) |
| 843 | return (NULL); |
| 844 | return (&xmlLastError); |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * xmlResetError: |
| 849 | * @err: pointer to the error. |
| 850 | * |
| 851 | * Cleanup the error. |
| 852 | */ |
| 853 | void |
| 854 | xmlResetError(xmlErrorPtr err) |
| 855 | { |
| 856 | if (err == NULL) |
| 857 | return; |
| 858 | if (err->code == XML_ERR_OK) |
| 859 | return; |
| 860 | if (err->message != NULL) |
| 861 | xmlFree(err->message); |
| 862 | if (err->file != NULL) |
| 863 | xmlFree(err->file); |
| 864 | if (err->str1 != NULL) |
| 865 | xmlFree(err->str1); |
| 866 | if (err->str2 != NULL) |
| 867 | xmlFree(err->str2); |
| 868 | if (err->str3 != NULL) |
| 869 | xmlFree(err->str3); |
| 870 | memset(err, 0, sizeof(xmlError)); |
| 871 | err->code = XML_ERR_OK; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * xmlResetLastError: |
| 876 | * |
| 877 | * Cleanup the last global error registered. For parsing error |
| 878 | * this does not change the well-formedness result. |
| 879 | */ |
| 880 | void |
| 881 | xmlResetLastError(void) |
| 882 | { |
| 883 | if (xmlLastError.code == XML_ERR_OK) |
| 884 | return; |
| 885 | xmlResetError(&xmlLastError); |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * xmlCtxtGetLastError: |
| 890 | * @ctx: an XML parser context |
| 891 | * |
| 892 | * Get the last parsing error registered. |
| 893 | * |
| 894 | * Returns NULL if no error occured or a pointer to the error |
| 895 | */ |
| 896 | xmlErrorPtr |
| 897 | xmlCtxtGetLastError(void *ctx) |
| 898 | { |
| 899 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 900 | |
| 901 | if (ctxt == NULL) |
| 902 | return (NULL); |
| 903 | if (ctxt->lastError.code == XML_ERR_OK) |
| 904 | return (NULL); |
| 905 | return (&ctxt->lastError); |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * xmlCtxtResetLastError: |
| 910 | * @ctx: an XML parser context |
| 911 | * |
| 912 | * Cleanup the last global error registered. For parsing error |
| 913 | * this does not change the well-formedness result. |
| 914 | */ |
| 915 | void |
| 916 | xmlCtxtResetLastError(void *ctx) |
| 917 | { |
| 918 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
| 919 | |
| 920 | if (ctxt == NULL) |
| 921 | return; |
Daniel Veillard | 26b0687 | 2010-03-15 15:59:07 +0100 | [diff] [blame] | 922 | ctxt->errNo = XML_ERR_OK; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 923 | if (ctxt->lastError.code == XML_ERR_OK) |
| 924 | return; |
| 925 | xmlResetError(&ctxt->lastError); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * xmlCopyError: |
| 930 | * @from: a source error |
| 931 | * @to: a target error |
| 932 | * |
| 933 | * Save the original error to the new place. |
| 934 | * |
| 935 | * Returns 0 in case of success and -1 in case of error. |
| 936 | */ |
| 937 | int |
| 938 | xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 939 | char *message, *file, *str1, *str2, *str3; |
| 940 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 941 | if ((from == NULL) || (to == NULL)) |
| 942 | return(-1); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 943 | |
| 944 | message = (char *) xmlStrdup((xmlChar *) from->message); |
| 945 | file = (char *) xmlStrdup ((xmlChar *) from->file); |
| 946 | str1 = (char *) xmlStrdup ((xmlChar *) from->str1); |
| 947 | str2 = (char *) xmlStrdup ((xmlChar *) from->str2); |
| 948 | str3 = (char *) xmlStrdup ((xmlChar *) from->str3); |
| 949 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 950 | if (to->message != NULL) |
| 951 | xmlFree(to->message); |
| 952 | if (to->file != NULL) |
| 953 | xmlFree(to->file); |
| 954 | if (to->str1 != NULL) |
| 955 | xmlFree(to->str1); |
| 956 | if (to->str2 != NULL) |
| 957 | xmlFree(to->str2); |
| 958 | if (to->str3 != NULL) |
| 959 | xmlFree(to->str3); |
| 960 | to->domain = from->domain; |
| 961 | to->code = from->code; |
| 962 | to->level = from->level; |
| 963 | to->line = from->line; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 964 | to->node = from->node; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 965 | to->int1 = from->int1; |
| 966 | to->int2 = from->int2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 967 | to->node = from->node; |
| 968 | to->ctxt = from->ctxt; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 969 | to->message = message; |
| 970 | to->file = file; |
| 971 | to->str1 = str1; |
| 972 | to->str2 = str2; |
| 973 | to->str3 = str3; |
| 974 | |
| 975 | return 0; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 978 | #define bottom_error |
| 979 | #include "elfgcchack.h" |