blob: b0d0f147fe752aff08dae5f4f45cdf2551fc1b55 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * error.c: module displaying/handling XML parser errors
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * Daniel Veillard <daniel@veillard.com>
Owen Taylor3473f882001-02-23 17:55:21 +00007 */
8
Daniel Veillard34ce8be2002-03-18 19:37:11 +00009#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000010#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000011
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000012#include <string.h>
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <stdarg.h>
14#include <libxml/parser.h>
15#include <libxml/xmlerror.h>
Daniel Veillarde356c282001-03-10 12:32:04 +000016#include <libxml/xmlmemory.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000017#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000018
Daniel Veillard635ef722001-10-29 11:48:19 +000019void xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED,
20 const char *msg,
21 ...);
22
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000023#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 Veillard2b8c4a12003-10-02 22:28:19 +000030 if (str != NULL) { \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000031 \
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 Veillard2b8c4a12003-10-02 22:28:19 +000045 break; \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000046 } \
47 str = larger; \
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000048 }} \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000049}
Bjorn Reese570ff082001-06-05 12:45:55 +000050
Owen Taylor3473f882001-02-23 17:55:21 +000051/************************************************************************
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 Veillard635ef722001-10-29 11:48:19 +000065void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +000066xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
Owen Taylor3473f882001-02-23 17:55:21 +000067 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 Veillard9d06d302002-01-22 18:15:52 +000077/**
78 * initGenericErrorDefaultFunc:
79 * @handler: the handler
80 *
81 * Set or reset (if NULL) the default handler for generic errors
Daniel Veillard7424eb62003-01-24 14:14:52 +000082 * to the builtin error function.
Daniel Veillard9d06d302002-01-22 18:15:52 +000083 */
Daniel Veillardd0463562001-10-13 09:15:48 +000084void
Daniel Veillarddb5850a2002-01-18 11:49:26 +000085initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
Daniel Veillardd0463562001-10-13 09:15:48 +000086{
Daniel Veillarddb5850a2002-01-18 11:49:26 +000087 if (handler == NULL)
88 xmlGenericError = xmlGenericErrorDefaultFunc;
89 else
Daniel Veillardda0ff5d2004-04-20 09:45:26 +000090 xmlGenericError = (*handler);
Daniel Veillardd0463562001-10-13 09:15:48 +000091}
Owen Taylor3473f882001-02-23 17:55:21 +000092
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 Veillardda3b29a2004-08-14 11:15:13 +0000105 * For multi-threaded applications, this must be set separately for each thread.
Owen Taylor3473f882001-02-23 17:55:21 +0000106 */
107void
108xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
109 xmlGenericErrorContext = ctx;
110 if (handler != NULL)
111 xmlGenericError = handler;
112 else
113 xmlGenericError = xmlGenericErrorDefaultFunc;
114}
115
Daniel Veillard659e71e2003-10-10 14:10:40 +0000116/**
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 Veillardda3b29a2004-08-14 11:15:13 +0000126 * For multi-threaded applications, this must be set separately for each thread.
Daniel Veillard659e71e2003-10-10 14:10:40 +0000127 */
128void
129xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
130 xmlGenericErrorContext = ctx;
131 xmlStructuredError = handler;
132}
133
Owen Taylor3473f882001-02-23 17:55:21 +0000134/************************************************************************
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
147void
148xmlParserPrintFileInfo(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 Veillard2b8c4a12003-10-02 22:28:19 +0000167static void
168xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
169 xmlGenericErrorFunc channel, void *data ) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000170 const xmlChar *cur, *base;
William M. Brackc1939562003-08-05 15:52:22 +0000171 unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
William M. Brack3dd57f72003-05-13 02:06:18 +0000172 xmlChar content[81]; /* space for 80 chars + line terminator */
Daniel Veillard2be30642001-03-27 00:32:28 +0000173 xmlChar *ctnt;
Owen Taylor3473f882001-02-23 17:55:21 +0000174
Daniel Veillard561b7f82002-03-20 21:55:57 +0000175 if (input == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000176 cur = input->cur;
177 base = input->base;
Daniel Veillard2be30642001-03-27 00:32:28 +0000178 /* skip backwards over any end-of-lines */
William M. Brackc1939562003-08-05 15:52:22 +0000179 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000180 cur--;
Owen Taylor3473f882001-02-23 17:55:21 +0000181 }
182 n = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000183 /* search backwards for beginning-of-line (to max buff size) */
William M. Brackc1939562003-08-05 15:52:22 +0000184 while ((n++ < (sizeof(content)-1)) && (cur > base) &&
185 (*(cur) != '\n') && (*(cur) != '\r'))
Owen Taylor3473f882001-02-23 17:55:21 +0000186 cur--;
William M. Brackc1939562003-08-05 15:52:22 +0000187 if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
William M. Brack3dd57f72003-05-13 02:06:18 +0000188 /* 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 Taylor3473f882001-02-23 17:55:21 +0000191 n = 0;
Daniel Veillard2be30642001-03-27 00:32:28 +0000192 ctnt = content;
William M. Brack3dd57f72003-05-13 02:06:18 +0000193 /* copy selected text to our buffer */
William M. Brackc1939562003-08-05 15:52:22 +0000194 while ((*cur != 0) && (*(cur) != '\n') &&
195 (*(cur) != '\r') && (n < sizeof(content)-1)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000196 *ctnt++ = *cur++;
197 n++;
Owen Taylor3473f882001-02-23 17:55:21 +0000198 }
Daniel Veillard2be30642001-03-27 00:32:28 +0000199 *ctnt = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000200 /* print out the selected text */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000201 channel(data ,"%s\n", content);
Daniel Veillard2be30642001-03-27 00:32:28 +0000202 /* create blank line with problem pointer */
Owen Taylor3473f882001-02-23 17:55:21 +0000203 n = 0;
Daniel Veillard7533cc82001-04-24 15:52:00 +0000204 ctnt = content;
William M. Brack3dd57f72003-05-13 02:06:18 +0000205 /* (leave buffer space for pointer + line terminator) */
206 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
William M. Brackc1939562003-08-05 15:52:22 +0000207 if (*(ctnt) != '\t')
208 *(ctnt) = ' ';
William M. Brack69848302003-09-22 00:24:51 +0000209 ctnt++;
Owen Taylor3473f882001-02-23 17:55:21 +0000210 }
William M. Brack3dd57f72003-05-13 02:06:18 +0000211 *ctnt++ = '^';
212 *ctnt = 0;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000213 channel(data ,"%s\n", content);
Owen Taylor3473f882001-02-23 17:55:21 +0000214}
215
Daniel Veillard561b7f82002-03-20 21:55:57 +0000216/**
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000217 * xmlParserPrintFileContext:
218 * @input: an xmlParserInputPtr input
219 *
220 * Displays current context within the input content for error tracking
Daniel Veillard561b7f82002-03-20 21:55:57 +0000221 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000222void
223xmlParserPrintFileContext(xmlParserInputPtr input) {
224 xmlParserPrintFileContextInternal(input, xmlGenericError,
225 xmlGenericErrorContext);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000226}
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000227
228/**
229 * xmlReportError:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000230 * @err: the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000231 * @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 */
237static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000238xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str,
239 xmlGenericErrorFunc channel, void *data)
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000240{
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000241 char *file = NULL;
242 int line = 0;
243 int code = -1;
244 int domain;
Daniel Veillard4c004142003-10-07 11:33:24 +0000245 const xmlChar *name = NULL;
246 xmlNodePtr node;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000247 xmlErrorLevel level;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000248 xmlParserInputPtr input = NULL;
249 xmlParserInputPtr cur = NULL;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000250
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000251 if (err == NULL)
252 return;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000253
Daniel Veillard4c004142003-10-07 11:33:24 +0000254 if (channel == NULL) {
255 channel = xmlGenericError;
256 data = xmlGenericErrorContext;
257 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000258 file = err->file;
259 line = err->line;
260 code = err->code;
261 domain = err->domain;
262 level = err->level;
Daniel Veillard4c004142003-10-07 11:33:24 +0000263 node = err->node;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000264
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000265 if (code == XML_ERR_OK)
266 return;
267
Daniel Veillard4c004142003-10-07 11:33:24 +0000268 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
269 name = node->name;
270
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000271 /*
272 * Maintain the compatibility with the legacy error handling
273 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000274 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 Veillardd96f6d32003-10-07 21:25:12 +0000284 else if ((line != 0) && (domain == XML_FROM_PARSER))
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000285 channel(data, "Entity: line %d: ", input->line);
286 }
287 } else {
288 if (file != NULL)
289 channel(data, "%s:%d: ", file, line);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000290 else if ((line != 0) && (domain == XML_FROM_PARSER))
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000291 channel(data, "Entity: line %d: ", line);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000292 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000293 if (name != NULL) {
294 channel(data, "element %s: ", name);
295 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000296 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 Veillard72b9e292003-10-28 15:44:17 +0000306 case XML_FROM_VALID:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000307 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 Veillardce1648b2005-01-04 15:10:22 +0000333 case XML_FROM_MODULE:
334 channel(data, "module ");
335 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000336 case XML_FROM_SCHEMASV:
Daniel Veillard87db3a82003-10-10 10:52:58 +0000337 channel(data, "Schemas validity ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000338 break;
339 case XML_FROM_SCHEMASP:
Daniel Veillard87db3a82003-10-10 10:52:58 +0000340 channel(data, "Schemas parser ");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000341 break;
Daniel Veillard4c004142003-10-07 11:33:24 +0000342 case XML_FROM_RELAXNGP:
343 channel(data, "Relax-NG parser ");
344 break;
345 case XML_FROM_RELAXNGV:
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000346 channel(data, "Relax-NG validity ");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000347 break;
348 case XML_FROM_CATALOG:
349 channel(data, "Catalog ");
350 break;
351 case XML_FROM_C14N:
352 channel(data, "C14N ");
353 break;
354 case XML_FROM_XSLT:
355 channel(data, "XSLT ");
356 break;
357 default:
358 break;
359 }
360 if (code == XML_ERR_OK)
361 return;
362 switch (level) {
363 case XML_ERR_NONE:
364 channel(data, ": ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000365 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000366 case XML_ERR_WARNING:
367 channel(data, "warning : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000368 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000369 case XML_ERR_ERROR:
370 channel(data, "error : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000371 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000372 case XML_ERR_FATAL:
373 channel(data, "error : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000374 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000375 }
376 if (code == XML_ERR_OK)
377 return;
378 if (str != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +0000379 int len;
380 len = xmlStrlen((const xmlChar *)str);
381 if ((len > 0) && (str[len - 1] != '\n'))
Daniel Veillard828ce832003-10-08 19:19:10 +0000382 channel(data, "%s\n", str);
Daniel Veillarda8856222003-10-08 19:26:03 +0000383 else
384 channel(data, "%s", str);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000385 } else {
Daniel Veillard828ce832003-10-08 19:19:10 +0000386 channel(data, "%s\n", "out of memory error");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000387 }
388 if (code == XML_ERR_OK)
389 return;
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 Veillardd96f6d32003-10-07 21:25:12 +0000396 else if ((line != 0) && (domain == XML_FROM_PARSER))
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000397 channel(data, "Entity: line %d: \n", cur->line);
398 xmlParserPrintFileContextInternal(cur, channel, data);
399 }
400 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000401 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 Veillard2b8c4a12003-10-02 22:28:19 +0000414}
415
416/**
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000417 * __xmlRaiseError:
William M. Brackd233e392004-05-16 03:12:08 +0000418 * @schannel: the structured callback channel
Daniel Veillard659e71e2003-10-10 14:10:40 +0000419 * @channel: the old callback channel
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000420 * @data: the callback data
421 * @ctx: the parser context or NULL
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000422 * @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 Sanin8fdc32a2005-01-05 15:37:55 +0000432 * @col: column number of the error or 0 if N/A
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000433 * @msg: the message to display/transmit
434 * @...: extra parameters for the message display
435 *
William M. Brackd233e392004-05-16 03:12:08 +0000436 * Update the appropriate global or contextual error structure,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000437 * then forward the error message down the parser or generic
438 * error callback handler
439 */
440void
Daniel Veillard659e71e2003-10-10 14:10:40 +0000441__xmlRaiseError(xmlStructuredErrorFunc schannel,
442 xmlGenericErrorFunc channel, void *data, void *ctx,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000443 void *nod, int domain, int code, xmlErrorLevel level,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000444 const char *file, int line, const char *str1,
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000445 const char *str2, const char *str3, int int1, int col,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000446 const char *msg, ...)
447{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000448 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000449 xmlNodePtr node = (xmlNodePtr) nod;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000450 char *str = NULL;
451 xmlParserInputPtr input = NULL;
452 xmlErrorPtr to = &xmlLastError;
William M. Brackd0407522004-10-02 03:54:00 +0000453 xmlNodePtr baseptr = NULL;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000454
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000455 if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
456 return;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000457 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
458 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
Daniel Veillard370ba3d2004-10-25 16:23:56 +0000459 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000460 ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000461 if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
462 (ctxt->sax->initialized == XML_SAX2_MAGIC))
463 schannel = ctxt->sax->serror;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000464 }
William M. Brackcd3628b2004-07-25 21:07:29 +0000465 /*
466 * Check if structured error handler set
467 */
468 if (schannel == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000469 schannel = xmlStructuredError;
William M. Brackcd3628b2004-07-25 21:07:29 +0000470 /*
471 * if user has defined handler, change data ptr to user's choice
472 */
473 if (schannel != NULL)
474 data = xmlGenericErrorContext;
475 }
Daniel Veillard72b9e292003-10-28 15:44:17 +0000476 if ((domain == XML_FROM_VALID) &&
477 ((channel == xmlParserValidityError) ||
478 (channel == xmlParserValidityWarning))) {
479 ctxt = (xmlParserCtxtPtr) ctx;
480 if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
481 (ctxt->sax->initialized == XML_SAX2_MAGIC))
482 schannel = ctxt->sax->serror;
483 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000484 if (code == XML_ERR_OK)
485 return;
486 /*
487 * Formatting the message
488 */
489 if (msg == NULL) {
490 str = (char *) xmlStrdup(BAD_CAST "No error message provided");
491 } else {
492 XML_GET_VAR_STR(msg, str);
493 }
494
495 /*
496 * specific processing if a parser context is provided
497 */
498 if (ctxt != NULL) {
499 if (file == NULL) {
500 input = ctxt->input;
501 if ((input != NULL) && (input->filename == NULL) &&
502 (ctxt->inputNr > 1)) {
503 input = ctxt->inputTab[ctxt->inputNr - 2];
504 }
505 if (input != NULL) {
506 file = input->filename;
507 line = input->line;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000508 col = input->col;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000509 }
510 }
511 to = &ctxt->lastError;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000512 } else if ((node != NULL) && (file == NULL)) {
513 int i;
Daniel Veillard4c004142003-10-07 11:33:24 +0000514
Daniel Veillarddbee0f12005-06-27 13:42:57 +0000515 if ((node->doc != NULL) && (node->doc->URL != NULL)) {
William M. Brackd0407522004-10-02 03:54:00 +0000516 baseptr = node;
Daniel Veillard39e5c892005-07-03 22:48:50 +0000517 file = (const char *) node->doc->URL;
Daniel Veillarddbee0f12005-06-27 13:42:57 +0000518 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000519 for (i = 0;
520 ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
521 i++)
522 node = node->parent;
William M. Brackd0407522004-10-02 03:54:00 +0000523 if ((baseptr == NULL) && (node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +0000524 (node->doc != NULL) && (node->doc->URL != NULL))
William M. Brackd0407522004-10-02 03:54:00 +0000525 baseptr = node;
Daniel Veillard4c004142003-10-07 11:33:24 +0000526
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000527 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +0000528 line = node->line;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000529 }
530
531 /*
William M. Brackd233e392004-05-16 03:12:08 +0000532 * Save the information about the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000533 */
534 xmlResetError(to);
535 to->domain = domain;
536 to->code = code;
537 to->message = str;
538 to->level = level;
539 if (file != NULL)
540 to->file = (char *) xmlStrdup((const xmlChar *) file);
William M. Brackd0407522004-10-02 03:54:00 +0000541 else if (baseptr != NULL) {
542#ifdef LIBXML_XINCLUDE_ENABLED
543 /*
544 * We check if the error is within an XInclude section and,
545 * if so, attempt to print out the href of the XInclude instead
546 * of the usual "base" (doc->URL) for the node (bug 152623).
547 */
548 xmlNodePtr prev = baseptr;
549 int inclcount = 0;
550 while (prev != NULL) {
551 if (prev->prev == NULL)
552 prev = prev->parent;
553 else {
554 prev = prev->prev;
555 if (prev->type == XML_XINCLUDE_START) {
556 if (--inclcount < 0)
557 break;
558 } else if (prev->type == XML_XINCLUDE_END)
559 inclcount++;
560 }
561 }
562 if (prev != NULL) {
563 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
564 } else
565#endif
566 to->file = (char *) xmlStrdup(baseptr->doc->URL);
567 file = to->file;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000568 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000569 to->line = line;
570 if (str1 != NULL)
571 to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
572 if (str2 != NULL)
573 to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
574 if (str3 != NULL)
575 to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
576 to->int1 = int1;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000577 to->int2 = col;
Daniel Veillard4c004142003-10-07 11:33:24 +0000578 to->node = node;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000579 to->ctxt = ctx;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000580
Daniel Veillardd34b0b82004-01-02 20:26:01 +0000581 if (to != &xmlLastError)
582 xmlCopyError(to,&xmlLastError);
583
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000584 /*
William M. Brackcd3628b2004-07-25 21:07:29 +0000585 * Find the callback channel if channel param is NULL
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000586 */
William M. Brack9f797ab2004-07-28 07:40:12 +0000587 if ((ctxt != NULL) && (channel == NULL) && (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000588 if (level == XML_ERR_WARNING)
589 channel = ctxt->sax->warning;
590 else
591 channel = ctxt->sax->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000592 data = ctxt->userData;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000593 } else if (channel == NULL) {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000594 if (xmlStructuredError != NULL)
595 schannel = xmlStructuredError;
596 else
597 channel = xmlGenericError;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +0000598 if (!data) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000599 data = xmlGenericErrorContext;
600 }
Daniel Veillardda0aa4c2005-07-13 23:07:49 +0000601 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000602 if (schannel != NULL) {
603 schannel(data, to);
604 return;
605 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000606 if (channel == NULL)
607 return;
608
609 if ((channel == xmlParserError) ||
610 (channel == xmlParserWarning) ||
611 (channel == xmlParserValidityError) ||
612 (channel == xmlParserValidityWarning))
Daniel Veillard4c004142003-10-07 11:33:24 +0000613 xmlReportError(to, ctxt, str, NULL, NULL);
614 else if ((channel == (xmlGenericErrorFunc) fprintf) ||
615 (channel == xmlGenericErrorDefaultFunc))
616 xmlReportError(to, ctxt, str, channel, data);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000617 else
618 channel(data, "%s", str);
619}
Daniel Veillard561b7f82002-03-20 21:55:57 +0000620
Daniel Veillarde356c282001-03-10 12:32:04 +0000621/**
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000622 * __xmlSimpleError:
623 * @domain: where the error comes from
624 * @code: the error code
625 * @node: the context node
626 * @extra: extra informations
627 *
628 * Handle an out of memory condition
629 */
630void
631__xmlSimpleError(int domain, int code, xmlNodePtr node,
632 const char *msg, const char *extra)
633{
634
635 if (code == XML_ERR_NO_MEMORY) {
636 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000637 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000638 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
639 NULL, NULL, 0, 0,
640 "Memory allocation failed : %s\n", extra);
641 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000642 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000643 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
644 NULL, NULL, 0, 0, "Memory allocation failed\n");
645 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000646 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000647 code, XML_ERR_ERROR, NULL, 0, extra,
648 NULL, NULL, 0, 0, msg, extra);
649 }
650}
651/**
Owen Taylor3473f882001-02-23 17:55:21 +0000652 * xmlParserError:
653 * @ctx: an XML parser context
654 * @msg: the message to display/transmit
655 * @...: extra parameters for the message display
656 *
657 * Display and format an error messages, gives file, line, position and
658 * extra parameters.
659 */
660void
661xmlParserError(void *ctx, const char *msg, ...)
662{
663 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
664 xmlParserInputPtr input = NULL;
665 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000666 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000667
668 if (ctxt != NULL) {
669 input = ctxt->input;
670 if ((input != NULL) && (input->filename == NULL) &&
671 (ctxt->inputNr > 1)) {
672 cur = input;
673 input = ctxt->inputTab[ctxt->inputNr - 2];
674 }
675 xmlParserPrintFileInfo(input);
676 }
677
678 xmlGenericError(xmlGenericErrorContext, "error: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000679 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000680 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000681 if (str != NULL)
682 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000683
684 if (ctxt != NULL) {
685 xmlParserPrintFileContext(input);
686 if (cur != NULL) {
687 xmlParserPrintFileInfo(cur);
688 xmlGenericError(xmlGenericErrorContext, "\n");
689 xmlParserPrintFileContext(cur);
690 }
691 }
692}
693
694/**
695 * xmlParserWarning:
696 * @ctx: an XML parser context
697 * @msg: the message to display/transmit
698 * @...: extra parameters for the message display
699 *
700 * Display and format a warning messages, gives file, line, position and
701 * extra parameters.
702 */
703void
704xmlParserWarning(void *ctx, const char *msg, ...)
705{
706 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
707 xmlParserInputPtr input = NULL;
708 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000709 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000710
711 if (ctxt != NULL) {
712 input = ctxt->input;
713 if ((input != NULL) && (input->filename == NULL) &&
714 (ctxt->inputNr > 1)) {
715 cur = input;
716 input = ctxt->inputTab[ctxt->inputNr - 2];
717 }
718 xmlParserPrintFileInfo(input);
719 }
720
721 xmlGenericError(xmlGenericErrorContext, "warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000722 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000723 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000724 if (str != NULL)
725 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000726
727 if (ctxt != NULL) {
728 xmlParserPrintFileContext(input);
729 if (cur != NULL) {
730 xmlParserPrintFileInfo(cur);
731 xmlGenericError(xmlGenericErrorContext, "\n");
732 xmlParserPrintFileContext(cur);
733 }
734 }
735}
736
737/************************************************************************
738 * *
739 * Handling of validation errors *
740 * *
741 ************************************************************************/
742
743/**
744 * xmlParserValidityError:
745 * @ctx: an XML parser context
746 * @msg: the message to display/transmit
747 * @...: extra parameters for the message display
748 *
749 * Display and format an validity error messages, gives file,
750 * line, position and extra parameters.
751 */
752void
753xmlParserValidityError(void *ctx, const char *msg, ...)
754{
755 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
756 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000757 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000758 int len = xmlStrlen((const xmlChar *) msg);
759 static int had_info = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000760
Daniel Veillard76575762002-09-05 14:21:15 +0000761 if ((len > 1) && (msg[len - 2] != ':')) {
762 if (ctxt != NULL) {
763 input = ctxt->input;
764 if ((input->filename == NULL) && (ctxt->inputNr > 1))
765 input = ctxt->inputTab[ctxt->inputNr - 2];
766
767 if (had_info == 0) {
768 xmlParserPrintFileInfo(input);
769 }
770 }
771 xmlGenericError(xmlGenericErrorContext, "validity error: ");
Daniel Veillard76575762002-09-05 14:21:15 +0000772 had_info = 0;
773 } else {
774 had_info = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000775 }
776
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000777 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000778 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000779 if (str != NULL)
780 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000781
Daniel Veillard76575762002-09-05 14:21:15 +0000782 if ((ctxt != NULL) && (input != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000783 xmlParserPrintFileContext(input);
784 }
785}
786
787/**
788 * xmlParserValidityWarning:
789 * @ctx: an XML parser context
790 * @msg: the message to display/transmit
791 * @...: extra parameters for the message display
792 *
793 * Display and format a validity warning messages, gives file, line,
794 * position and extra parameters.
795 */
796void
797xmlParserValidityWarning(void *ctx, const char *msg, ...)
798{
799 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
800 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000801 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000802 int len = xmlStrlen((const xmlChar *) msg);
Owen Taylor3473f882001-02-23 17:55:21 +0000803
Daniel Veillard76575762002-09-05 14:21:15 +0000804 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +0000805 input = ctxt->input;
806 if ((input->filename == NULL) && (ctxt->inputNr > 1))
807 input = ctxt->inputTab[ctxt->inputNr - 2];
808
809 xmlParserPrintFileInfo(input);
810 }
811
812 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000813 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000814 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000815 if (str != NULL)
816 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000817
818 if (ctxt != NULL) {
819 xmlParserPrintFileContext(input);
820 }
821}
822
823
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000824/************************************************************************
825 * *
826 * Extended Error Handling *
827 * *
828 ************************************************************************/
829
830/**
831 * xmlGetLastError:
832 *
833 * Get the last global error registered. This is per thread if compiled
834 * with thread support.
835 *
836 * Returns NULL if no error occured or a pointer to the error
837 */
838xmlErrorPtr
839xmlGetLastError(void)
840{
841 if (xmlLastError.code == XML_ERR_OK)
842 return (NULL);
843 return (&xmlLastError);
844}
845
846/**
847 * xmlResetError:
848 * @err: pointer to the error.
849 *
850 * Cleanup the error.
851 */
852void
853xmlResetError(xmlErrorPtr err)
854{
855 if (err == NULL)
856 return;
857 if (err->code == XML_ERR_OK)
858 return;
859 if (err->message != NULL)
860 xmlFree(err->message);
861 if (err->file != NULL)
862 xmlFree(err->file);
863 if (err->str1 != NULL)
864 xmlFree(err->str1);
865 if (err->str2 != NULL)
866 xmlFree(err->str2);
867 if (err->str3 != NULL)
868 xmlFree(err->str3);
869 memset(err, 0, sizeof(xmlError));
870 err->code = XML_ERR_OK;
871}
872
873/**
874 * xmlResetLastError:
875 *
876 * Cleanup the last global error registered. For parsing error
877 * this does not change the well-formedness result.
878 */
879void
880xmlResetLastError(void)
881{
882 if (xmlLastError.code == XML_ERR_OK)
883 return;
884 xmlResetError(&xmlLastError);
885}
886
887/**
888 * xmlCtxtGetLastError:
889 * @ctx: an XML parser context
890 *
891 * Get the last parsing error registered.
892 *
893 * Returns NULL if no error occured or a pointer to the error
894 */
895xmlErrorPtr
896xmlCtxtGetLastError(void *ctx)
897{
898 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
899
900 if (ctxt == NULL)
901 return (NULL);
902 if (ctxt->lastError.code == XML_ERR_OK)
903 return (NULL);
904 return (&ctxt->lastError);
905}
906
907/**
908 * xmlCtxtResetLastError:
909 * @ctx: an XML parser context
910 *
911 * Cleanup the last global error registered. For parsing error
912 * this does not change the well-formedness result.
913 */
914void
915xmlCtxtResetLastError(void *ctx)
916{
917 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
918
919 if (ctxt == NULL)
920 return;
921 if (ctxt->lastError.code == XML_ERR_OK)
922 return;
923 xmlResetError(&ctxt->lastError);
924}
925
926/**
927 * xmlCopyError:
928 * @from: a source error
929 * @to: a target error
930 *
931 * Save the original error to the new place.
932 *
933 * Returns 0 in case of success and -1 in case of error.
934 */
935int
936xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
William M. Bracka3215c72004-07-31 16:24:01 +0000937 char *message, *file, *str1, *str2, *str3;
938
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000939 if ((from == NULL) || (to == NULL))
940 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +0000941
942 message = (char *) xmlStrdup((xmlChar *) from->message);
943 file = (char *) xmlStrdup ((xmlChar *) from->file);
944 str1 = (char *) xmlStrdup ((xmlChar *) from->str1);
945 str2 = (char *) xmlStrdup ((xmlChar *) from->str2);
946 str3 = (char *) xmlStrdup ((xmlChar *) from->str3);
947
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000948 if (to->message != NULL)
949 xmlFree(to->message);
950 if (to->file != NULL)
951 xmlFree(to->file);
952 if (to->str1 != NULL)
953 xmlFree(to->str1);
954 if (to->str2 != NULL)
955 xmlFree(to->str2);
956 if (to->str3 != NULL)
957 xmlFree(to->str3);
958 to->domain = from->domain;
959 to->code = from->code;
960 to->level = from->level;
961 to->line = from->line;
Daniel Veillard4c004142003-10-07 11:33:24 +0000962 to->node = from->node;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000963 to->int1 = from->int1;
964 to->int2 = from->int2;
Daniel Veillard4c004142003-10-07 11:33:24 +0000965 to->node = from->node;
966 to->ctxt = from->ctxt;
William M. Bracka3215c72004-07-31 16:24:01 +0000967 to->message = message;
968 to->file = file;
969 to->str1 = str1;
970 to->str2 = str2;
971 to->str3 = str3;
972
973 return 0;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000974}
975
Daniel Veillard5d4644e2005-04-01 13:11:58 +0000976#define bottom_error
977#include "elfgcchack.h"