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