Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2 | * error.c: module displaying/handling XML parser errors |
| 3 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 6 | * Daniel Veillard <Daniel.Veillard@w3.org> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 9 | #ifdef WIN32 |
| 10 | #include "win32config.h" |
| 11 | #else |
| 12 | #include "config.h" |
| 13 | #endif |
| 14 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdarg.h> |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 17 | #include <libxml/parser.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 18 | #include <libxml/xmlerror.h> |
| 19 | |
| 20 | /************************************************************************ |
| 21 | * * |
| 22 | * Handling of out of context errors * |
| 23 | * * |
| 24 | ************************************************************************/ |
| 25 | |
| 26 | /** |
| 27 | * xmlGenericErrorDefaultFunc: |
| 28 | * @ctx: an error context |
| 29 | * @msg: the message to display/transmit |
| 30 | * @...: extra parameters for the message display |
| 31 | * |
| 32 | * Default handler for out of context error messages. |
| 33 | */ |
| 34 | void |
| 35 | xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...) { |
| 36 | va_list args; |
| 37 | |
| 38 | if (xmlGenericErrorContext == NULL) |
| 39 | xmlGenericErrorContext = (void *) stderr; |
| 40 | |
| 41 | va_start(args, msg); |
| 42 | vfprintf((FILE *)xmlGenericErrorContext, msg, args); |
| 43 | va_end(args); |
| 44 | } |
| 45 | |
| 46 | xmlGenericErrorFunc xmlGenericError = xmlGenericErrorDefaultFunc; |
| 47 | void *xmlGenericErrorContext = NULL; |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * xmlSetGenericErrorFunc: |
| 52 | * @ctx: the new error handling context |
| 53 | * @handler: the new handler function |
| 54 | * |
| 55 | * Function to reset the handler and the error context for out of |
| 56 | * context error messages. |
| 57 | * This simply means that @handler will be called for subsequent |
| 58 | * error messages while not parsing nor validating. And @ctx will |
| 59 | * be passed as first argument to @handler |
| 60 | * One can simply force messages to be emitted to another FILE * than |
| 61 | * stderr by setting @ctx to this file handle and @handler to NULL. |
| 62 | */ |
| 63 | void |
| 64 | xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) { |
| 65 | if (ctx != NULL) |
| 66 | xmlGenericErrorContext = ctx; |
| 67 | if (handler != NULL) |
| 68 | xmlGenericError = handler; |
| 69 | } |
| 70 | |
| 71 | /************************************************************************ |
| 72 | * * |
| 73 | * Handling of parsing errors * |
| 74 | * * |
| 75 | ************************************************************************/ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 76 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 77 | /** |
| 78 | * xmlParserPrintFileInfo: |
| 79 | * @input: an xmlParserInputPtr input |
| 80 | * |
| 81 | * Displays the associated file and line informations for the current input |
| 82 | */ |
| 83 | |
| 84 | void |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 85 | xmlParserPrintFileInfo(xmlParserInputPtr input) { |
| 86 | if (input != NULL) { |
| 87 | if (input->filename) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 88 | xmlGenericError(xmlGenericErrorContext, |
| 89 | "%s:%d: ", input->filename, |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 90 | input->line); |
| 91 | else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 92 | xmlGenericError(xmlGenericErrorContext, |
| 93 | "Entity: line %d: ", input->line); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 97 | /** |
| 98 | * xmlParserPrintFileContext: |
| 99 | * @input: an xmlParserInputPtr input |
| 100 | * |
| 101 | * Displays current context within the input content for error tracking |
| 102 | */ |
| 103 | |
| 104 | void |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 105 | xmlParserPrintFileContext(xmlParserInputPtr input) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 106 | const xmlChar *cur, *base; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 107 | int n; |
| 108 | |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 109 | if (input == NULL) return; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 110 | cur = input->cur; |
| 111 | base = input->base; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 112 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 113 | cur--; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 114 | } |
| 115 | n = 0; |
Daniel Veillard | bc50b59 | 1999-03-01 12:28:53 +0000 | [diff] [blame] | 116 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 117 | cur--; |
| 118 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
| 119 | base = cur; |
| 120 | n = 0; |
| 121 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 122 | xmlGenericError(xmlGenericErrorContext, |
| 123 | "%c", (unsigned char) *cur++); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 124 | n++; |
| 125 | } |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 126 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 127 | cur = input->cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 128 | while ((*cur == '\n') || (*cur == '\r')) |
| 129 | cur--; |
| 130 | n = 0; |
Daniel Veillard | bc50b59 | 1999-03-01 12:28:53 +0000 | [diff] [blame] | 131 | while ((cur != base) && (n++ < 80)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 132 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 133 | base++; |
| 134 | } |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 135 | xmlGenericError(xmlGenericErrorContext,"^\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 138 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 139 | * xmlParserError: |
| 140 | * @ctx: an XML parser context |
| 141 | * @msg: the message to display/transmit |
| 142 | * @...: extra parameters for the message display |
| 143 | * |
| 144 | * Display and format an error messages, gives file, line, position and |
| 145 | * extra parameters. |
| 146 | */ |
| 147 | void |
| 148 | xmlParserError(void *ctx, const char *msg, ...) |
| 149 | { |
| 150 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 151 | xmlParserInputPtr input = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 152 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 153 | va_list args; |
| 154 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 155 | if (ctxt != NULL) { |
| 156 | input = ctxt->input; |
| 157 | if ((input != NULL) && (input->filename == NULL) && |
| 158 | (ctxt->inputNr > 1)) { |
| 159 | cur = input; |
| 160 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 161 | } |
| 162 | xmlParserPrintFileInfo(input); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 163 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 164 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 165 | xmlGenericError(xmlGenericErrorContext, "error: "); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 166 | va_start(args, msg); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 167 | vfprintf(xmlGenericErrorContext, msg, args); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 168 | va_end(args); |
| 169 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 170 | if (ctxt != NULL) { |
| 171 | xmlParserPrintFileContext(input); |
| 172 | if (cur != NULL) { |
| 173 | xmlParserPrintFileInfo(cur); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 174 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 175 | xmlParserPrintFileContext(cur); |
| 176 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 177 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 181 | * xmlParserWarning: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 182 | * @ctx: an XML parser context |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 183 | * @msg: the message to display/transmit |
| 184 | * @...: extra parameters for the message display |
| 185 | * |
| 186 | * Display and format a warning messages, gives file, line, position and |
| 187 | * extra parameters. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 188 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 189 | void |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 190 | xmlParserWarning(void *ctx, const char *msg, ...) |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 191 | { |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 192 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 193 | xmlParserInputPtr input = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 194 | xmlParserInputPtr cur = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 195 | va_list args; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 196 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 197 | if (ctxt != NULL) { |
| 198 | input = ctxt->input; |
| 199 | if ((input != NULL) && (input->filename == NULL) && |
| 200 | (ctxt->inputNr > 1)) { |
| 201 | cur = input; |
| 202 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 203 | } |
| 204 | xmlParserPrintFileInfo(input); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 207 | xmlGenericError(xmlGenericErrorContext, "warning: "); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 208 | va_start(args, msg); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 209 | vfprintf(xmlGenericErrorContext, msg, args); |
Daniel Veillard | 64068b3 | 1999-03-24 20:42:16 +0000 | [diff] [blame] | 210 | va_end(args); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 211 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 212 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 213 | if (ctxt != NULL) { |
| 214 | xmlParserPrintFileContext(input); |
| 215 | if (cur != NULL) { |
| 216 | xmlParserPrintFileInfo(cur); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 217 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 218 | xmlParserPrintFileContext(cur); |
| 219 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 220 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 223 | /************************************************************************ |
| 224 | * * |
| 225 | * Handling of validation errors * |
| 226 | * * |
| 227 | ************************************************************************/ |
| 228 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 229 | /** |
| 230 | * xmlParserValidityError: |
| 231 | * @ctx: an XML parser context |
| 232 | * @msg: the message to display/transmit |
| 233 | * @...: extra parameters for the message display |
| 234 | * |
| 235 | * Display and format an validity error messages, gives file, |
| 236 | * line, position and extra parameters. |
| 237 | */ |
| 238 | void |
| 239 | xmlParserValidityError(void *ctx, const char *msg, ...) |
| 240 | { |
| 241 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 242 | xmlParserInputPtr input = NULL; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 243 | va_list args; |
| 244 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 245 | if (ctxt != NULL) { |
| 246 | input = ctxt->input; |
| 247 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 248 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
| 249 | |
| 250 | xmlParserPrintFileInfo(input); |
| 251 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 252 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 253 | xmlGenericError(xmlGenericErrorContext, "validity error: "); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 254 | va_start(args, msg); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 255 | vfprintf(xmlGenericErrorContext, msg, args); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 256 | va_end(args); |
| 257 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 258 | if (ctxt != NULL) { |
| 259 | xmlParserPrintFileContext(input); |
| 260 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * xmlParserValidityWarning: |
| 265 | * @ctx: an XML parser context |
| 266 | * @msg: the message to display/transmit |
| 267 | * @...: extra parameters for the message display |
| 268 | * |
| 269 | * Display and format a validity warning messages, gives file, line, |
| 270 | * position and extra parameters. |
| 271 | */ |
| 272 | void |
| 273 | xmlParserValidityWarning(void *ctx, const char *msg, ...) |
| 274 | { |
| 275 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 276 | xmlParserInputPtr input = NULL; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 277 | va_list args; |
| 278 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 279 | if (ctxt != NULL) { |
| 280 | input = ctxt->input; |
| 281 | if ((input->filename == NULL) && (ctxt->inputNr > 1)) |
| 282 | input = ctxt->inputTab[ctxt->inputNr - 2]; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 283 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 284 | xmlParserPrintFileInfo(input); |
| 285 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 286 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 287 | xmlGenericError(xmlGenericErrorContext, "validity warning: "); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 288 | va_start(args, msg); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 289 | vfprintf(xmlGenericErrorContext, msg, args); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 290 | va_end(args); |
| 291 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 292 | if (ctxt != NULL) { |
| 293 | xmlParserPrintFileContext(input); |
| 294 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 295 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 296 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 297 | |