blob: cbcf5c9633535454af06cad22c96a4c4025ac6ed [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 Veillardffa3c742005-07-21 13:24:09 +000019void XMLCDECL xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED,
Daniel Veillard635ef722001-10-29 11:48:19 +000020 const char *msg,
21 ...);
22
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000023#define XML_GET_VAR_STR(msg, str) { \
Daniel Veillarddbf7bfe2005-10-28 08:25:51 +000024 int size, prev_size = -1; \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000025 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 \
Daniel Veillardfa750972008-04-03 07:31:25 +000034 while (size < 64000) { \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000035 va_start(ap, msg); \
Daniel Veillardf8e3db02012-09-11 13:26:36 +080036 chars = vsnprintf(str, size, msg, ap); \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000037 va_end(ap); \
Daniel Veillarddbf7bfe2005-10-28 08:25:51 +000038 if ((chars > -1) && (chars < size)) { \
39 if (prev_size == chars) { \
40 break; \
41 } else { \
42 prev_size = chars; \
43 } \
44 } \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000045 if (chars > -1) \
46 size += chars + 1; \
47 else \
48 size += 100; \
49 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000050 break; \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000051 } \
52 str = larger; \
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000053 }} \
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000054}
Bjorn Reese570ff082001-06-05 12:45:55 +000055
Owen Taylor3473f882001-02-23 17:55:21 +000056/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080057 * *
58 * Handling of out of context errors *
59 * *
Owen Taylor3473f882001-02-23 17:55:21 +000060 ************************************************************************/
61
62/**
63 * xmlGenericErrorDefaultFunc:
64 * @ctx: an error context
65 * @msg: the message to display/transmit
66 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +080067 *
Owen Taylor3473f882001-02-23 17:55:21 +000068 * Default handler for out of context error messages.
69 */
Daniel Veillardffa3c742005-07-21 13:24:09 +000070void XMLCDECL
Daniel Veillardc86a4fa2001-03-26 16:28:29 +000071xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
Owen Taylor3473f882001-02-23 17:55:21 +000072 va_list args;
73
74 if (xmlGenericErrorContext == NULL)
75 xmlGenericErrorContext = (void *) stderr;
76
77 va_start(args, msg);
78 vfprintf((FILE *)xmlGenericErrorContext, msg, args);
79 va_end(args);
80}
81
Daniel Veillard9d06d302002-01-22 18:15:52 +000082/**
83 * initGenericErrorDefaultFunc:
84 * @handler: the handler
Daniel Veillardf8e3db02012-09-11 13:26:36 +080085 *
Daniel Veillard9d06d302002-01-22 18:15:52 +000086 * Set or reset (if NULL) the default handler for generic errors
Daniel Veillard7424eb62003-01-24 14:14:52 +000087 * to the builtin error function.
Daniel Veillard9d06d302002-01-22 18:15:52 +000088 */
Daniel Veillardd0463562001-10-13 09:15:48 +000089void
Daniel Veillarddb5850a2002-01-18 11:49:26 +000090initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
Daniel Veillardd0463562001-10-13 09:15:48 +000091{
Daniel Veillarddb5850a2002-01-18 11:49:26 +000092 if (handler == NULL)
93 xmlGenericError = xmlGenericErrorDefaultFunc;
94 else
Daniel Veillardda0ff5d2004-04-20 09:45:26 +000095 xmlGenericError = (*handler);
Daniel Veillardd0463562001-10-13 09:15:48 +000096}
Owen Taylor3473f882001-02-23 17:55:21 +000097
98/**
99 * xmlSetGenericErrorFunc:
100 * @ctx: the new error handling context
101 * @handler: the new handler function
102 *
103 * Function to reset the handler and the error context for out of
104 * context error messages.
105 * This simply means that @handler will be called for subsequent
106 * error messages while not parsing nor validating. And @ctx will
107 * be passed as first argument to @handler
108 * One can simply force messages to be emitted to another FILE * than
109 * stderr by setting @ctx to this file handle and @handler to NULL.
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000110 * For multi-threaded applications, this must be set separately for each thread.
Owen Taylor3473f882001-02-23 17:55:21 +0000111 */
112void
113xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
114 xmlGenericErrorContext = ctx;
115 if (handler != NULL)
116 xmlGenericError = handler;
117 else
118 xmlGenericError = xmlGenericErrorDefaultFunc;
119}
120
Daniel Veillard659e71e2003-10-10 14:10:40 +0000121/**
122 * xmlSetStructuredErrorFunc:
123 * @ctx: the new error handling context
124 * @handler: the new handler function
125 *
126 * Function to reset the handler and the error context for out of
127 * context structured error messages.
128 * This simply means that @handler will be called for subsequent
129 * error messages while not parsing nor validating. And @ctx will
130 * be passed as first argument to @handler
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000131 * For multi-threaded applications, this must be set separately for each thread.
Daniel Veillard659e71e2003-10-10 14:10:40 +0000132 */
133void
134xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
Wang Lam1de382e2009-08-24 17:34:25 +0200135 xmlStructuredErrorContext = ctx;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000136 xmlStructuredError = handler;
137}
138
Owen Taylor3473f882001-02-23 17:55:21 +0000139/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800140 * *
141 * Handling of parsing errors *
142 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000143 ************************************************************************/
144
145/**
146 * xmlParserPrintFileInfo:
147 * @input: an xmlParserInputPtr input
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800148 *
Owen Taylor3473f882001-02-23 17:55:21 +0000149 * Displays the associated file and line informations for the current input
150 */
151
152void
153xmlParserPrintFileInfo(xmlParserInputPtr input) {
154 if (input != NULL) {
155 if (input->filename)
156 xmlGenericError(xmlGenericErrorContext,
157 "%s:%d: ", input->filename,
158 input->line);
159 else
160 xmlGenericError(xmlGenericErrorContext,
161 "Entity: line %d: ", input->line);
162 }
163}
164
165/**
166 * xmlParserPrintFileContext:
167 * @input: an xmlParserInputPtr input
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800168 *
Owen Taylor3473f882001-02-23 17:55:21 +0000169 * Displays current context within the input content for error tracking
170 */
171
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000172static void
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800173xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000174 xmlGenericErrorFunc channel, void *data ) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000175 const xmlChar *cur, *base;
William M. Brackc1939562003-08-05 15:52:22 +0000176 unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
William M. Brack3dd57f72003-05-13 02:06:18 +0000177 xmlChar content[81]; /* space for 80 chars + line terminator */
Daniel Veillard2be30642001-03-27 00:32:28 +0000178 xmlChar *ctnt;
Owen Taylor3473f882001-02-23 17:55:21 +0000179
Daniel Veillard561b7f82002-03-20 21:55:57 +0000180 if (input == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000181 cur = input->cur;
182 base = input->base;
Daniel Veillard2be30642001-03-27 00:32:28 +0000183 /* skip backwards over any end-of-lines */
William M. Brackc1939562003-08-05 15:52:22 +0000184 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000185 cur--;
Owen Taylor3473f882001-02-23 17:55:21 +0000186 }
187 n = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000188 /* search backwards for beginning-of-line (to max buff size) */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800189 while ((n++ < (sizeof(content)-1)) && (cur > base) &&
190 (*(cur) != '\n') && (*(cur) != '\r'))
Owen Taylor3473f882001-02-23 17:55:21 +0000191 cur--;
William M. Brackc1939562003-08-05 15:52:22 +0000192 if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
William M. Brack3dd57f72003-05-13 02:06:18 +0000193 /* calculate the error position in terms of the current position */
194 col = input->cur - cur;
195 /* search forward for end-of-line (to max buff size) */
Owen Taylor3473f882001-02-23 17:55:21 +0000196 n = 0;
Daniel Veillard2be30642001-03-27 00:32:28 +0000197 ctnt = content;
William M. Brack3dd57f72003-05-13 02:06:18 +0000198 /* copy selected text to our buffer */
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800199 while ((*cur != 0) && (*(cur) != '\n') &&
200 (*(cur) != '\r') && (n < sizeof(content)-1)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000201 *ctnt++ = *cur++;
202 n++;
Owen Taylor3473f882001-02-23 17:55:21 +0000203 }
Daniel Veillard2be30642001-03-27 00:32:28 +0000204 *ctnt = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000205 /* print out the selected text */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000206 channel(data ,"%s\n", content);
Daniel Veillard2be30642001-03-27 00:32:28 +0000207 /* create blank line with problem pointer */
Owen Taylor3473f882001-02-23 17:55:21 +0000208 n = 0;
Daniel Veillard7533cc82001-04-24 15:52:00 +0000209 ctnt = content;
William M. Brack3dd57f72003-05-13 02:06:18 +0000210 /* (leave buffer space for pointer + line terminator) */
211 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
William M. Brackc1939562003-08-05 15:52:22 +0000212 if (*(ctnt) != '\t')
213 *(ctnt) = ' ';
William M. Brack69848302003-09-22 00:24:51 +0000214 ctnt++;
Owen Taylor3473f882001-02-23 17:55:21 +0000215 }
William M. Brack3dd57f72003-05-13 02:06:18 +0000216 *ctnt++ = '^';
217 *ctnt = 0;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000218 channel(data ,"%s\n", content);
Owen Taylor3473f882001-02-23 17:55:21 +0000219}
220
Daniel Veillard561b7f82002-03-20 21:55:57 +0000221/**
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000222 * xmlParserPrintFileContext:
223 * @input: an xmlParserInputPtr input
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800224 *
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000225 * Displays current context within the input content for error tracking
Daniel Veillard561b7f82002-03-20 21:55:57 +0000226 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000227void
228xmlParserPrintFileContext(xmlParserInputPtr input) {
229 xmlParserPrintFileContextInternal(input, xmlGenericError,
230 xmlGenericErrorContext);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000231}
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000232
233/**
234 * xmlReportError:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000235 * @err: the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000236 * @ctx: the parser context or NULL
237 * @str: the formatted error message
238 *
239 * Report an erro with its context, replace the 4 old error/warning
240 * routines.
241 */
242static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000243xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str,
244 xmlGenericErrorFunc channel, void *data)
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000245{
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000246 char *file = NULL;
247 int line = 0;
248 int code = -1;
249 int domain;
Daniel Veillard4c004142003-10-07 11:33:24 +0000250 const xmlChar *name = NULL;
251 xmlNodePtr node;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000252 xmlErrorLevel level;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000253 xmlParserInputPtr input = NULL;
254 xmlParserInputPtr cur = NULL;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000255
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000256 if (err == NULL)
257 return;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000258
Daniel Veillard4c004142003-10-07 11:33:24 +0000259 if (channel == NULL) {
260 channel = xmlGenericError;
261 data = xmlGenericErrorContext;
262 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000263 file = err->file;
264 line = err->line;
265 code = err->code;
266 domain = err->domain;
267 level = err->level;
Daniel Veillard4c004142003-10-07 11:33:24 +0000268 node = err->node;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000269
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000270 if (code == XML_ERR_OK)
271 return;
272
Daniel Veillard4c004142003-10-07 11:33:24 +0000273 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
274 name = node->name;
275
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000276 /*
277 * Maintain the compatibility with the legacy error handling
278 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000279 if (ctxt != NULL) {
280 input = ctxt->input;
281 if ((input != NULL) && (input->filename == NULL) &&
282 (ctxt->inputNr > 1)) {
283 cur = input;
284 input = ctxt->inputTab[ctxt->inputNr - 2];
285 }
286 if (input != NULL) {
287 if (input->filename)
288 channel(data, "%s:%d: ", input->filename, input->line);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000289 else if ((line != 0) && (domain == XML_FROM_PARSER))
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000290 channel(data, "Entity: line %d: ", input->line);
291 }
292 } else {
293 if (file != NULL)
294 channel(data, "%s:%d: ", file, line);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800295 else if ((line != 0) &&
Daniel Veillard97fa5b32012-08-14 11:01:07 +0800296 ((domain == XML_FROM_PARSER) || (domain == XML_FROM_SCHEMASV)||
297 (domain == XML_FROM_SCHEMASP)||(domain == XML_FROM_DTD) ||
298 (domain == XML_FROM_RELAXNGP)||(domain == XML_FROM_RELAXNGV)))
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000299 channel(data, "Entity: line %d: ", line);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000300 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000301 if (name != NULL) {
302 channel(data, "element %s: ", name);
303 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000304 switch (domain) {
305 case XML_FROM_PARSER:
306 channel(data, "parser ");
307 break;
308 case XML_FROM_NAMESPACE:
309 channel(data, "namespace ");
310 break;
311 case XML_FROM_DTD:
Daniel Veillard72b9e292003-10-28 15:44:17 +0000312 case XML_FROM_VALID:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000313 channel(data, "validity ");
314 break;
315 case XML_FROM_HTML:
316 channel(data, "HTML parser ");
317 break;
318 case XML_FROM_MEMORY:
319 channel(data, "memory ");
320 break;
321 case XML_FROM_OUTPUT:
322 channel(data, "output ");
323 break;
324 case XML_FROM_IO:
325 channel(data, "I/O ");
326 break;
327 case XML_FROM_XINCLUDE:
328 channel(data, "XInclude ");
329 break;
330 case XML_FROM_XPATH:
331 channel(data, "XPath ");
332 break;
333 case XML_FROM_XPOINTER:
334 channel(data, "parser ");
335 break;
336 case XML_FROM_REGEXP:
337 channel(data, "regexp ");
338 break;
Daniel Veillardce1648b2005-01-04 15:10:22 +0000339 case XML_FROM_MODULE:
340 channel(data, "module ");
341 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000342 case XML_FROM_SCHEMASV:
Daniel Veillard87db3a82003-10-10 10:52:58 +0000343 channel(data, "Schemas validity ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000344 break;
345 case XML_FROM_SCHEMASP:
Daniel Veillard87db3a82003-10-10 10:52:58 +0000346 channel(data, "Schemas parser ");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000347 break;
Daniel Veillard4c004142003-10-07 11:33:24 +0000348 case XML_FROM_RELAXNGP:
349 channel(data, "Relax-NG parser ");
350 break;
351 case XML_FROM_RELAXNGV:
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000352 channel(data, "Relax-NG validity ");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000353 break;
354 case XML_FROM_CATALOG:
355 channel(data, "Catalog ");
356 break;
357 case XML_FROM_C14N:
358 channel(data, "C14N ");
359 break;
360 case XML_FROM_XSLT:
361 channel(data, "XSLT ");
362 break;
Daniel Veillard1fc3ed02005-08-24 12:46:09 +0000363 case XML_FROM_I18N:
364 channel(data, "encoding ");
365 break;
Daniel Veillard57560382012-07-24 11:44:23 +0800366 case XML_FROM_SCHEMATRONV:
367 channel(data, "schematron ");
368 break;
369 case XML_FROM_BUFFER:
370 channel(data, "internal buffer ");
371 break;
372 case XML_FROM_URI:
373 channel(data, "URI ");
374 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000375 default:
376 break;
377 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000378 switch (level) {
379 case XML_ERR_NONE:
380 channel(data, ": ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000381 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000382 case XML_ERR_WARNING:
383 channel(data, "warning : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000384 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000385 case XML_ERR_ERROR:
386 channel(data, "error : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000387 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000388 case XML_ERR_FATAL:
389 channel(data, "error : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000390 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000391 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000392 if (str != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +0000393 int len;
394 len = xmlStrlen((const xmlChar *)str);
395 if ((len > 0) && (str[len - 1] != '\n'))
Daniel Veillard828ce832003-10-08 19:19:10 +0000396 channel(data, "%s\n", str);
Daniel Veillarda8856222003-10-08 19:26:03 +0000397 else
398 channel(data, "%s", str);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000399 } else {
Daniel Veillard828ce832003-10-08 19:19:10 +0000400 channel(data, "%s\n", "out of memory error");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000401 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000402
403 if (ctxt != NULL) {
404 xmlParserPrintFileContextInternal(input, channel, data);
405 if (cur != NULL) {
406 if (cur->filename)
407 channel(data, "%s:%d: \n", cur->filename, cur->line);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000408 else if ((line != 0) && (domain == XML_FROM_PARSER))
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000409 channel(data, "Entity: line %d: \n", cur->line);
410 xmlParserPrintFileContextInternal(cur, channel, data);
411 }
412 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000413 if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
414 (err->int1 < 100) &&
415 (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
416 xmlChar buf[150];
417 int i;
418
419 channel(data, "%s\n", err->str1);
420 for (i=0;i < err->int1;i++)
421 buf[i] = ' ';
422 buf[i++] = '^';
423 buf[i] = 0;
424 channel(data, "%s\n", buf);
425 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000426}
427
428/**
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000429 * __xmlRaiseError:
William M. Brackd233e392004-05-16 03:12:08 +0000430 * @schannel: the structured callback channel
Daniel Veillard659e71e2003-10-10 14:10:40 +0000431 * @channel: the old callback channel
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000432 * @data: the callback data
433 * @ctx: the parser context or NULL
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000434 * @ctx: the parser context or NULL
435 * @domain: the domain for the error
436 * @code: the code for the error
437 * @level: the xmlErrorLevel for the error
438 * @file: the file source of the error (or NULL)
439 * @line: the line of the error or 0 if N/A
440 * @str1: extra string info
441 * @str2: extra string info
442 * @str3: extra string info
443 * @int1: extra int info
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800444 * @col: column number of the error or 0 if N/A
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000445 * @msg: the message to display/transmit
446 * @...: extra parameters for the message display
447 *
William M. Brackd233e392004-05-16 03:12:08 +0000448 * Update the appropriate global or contextual error structure,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000449 * then forward the error message down the parser or generic
450 * error callback handler
451 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000452void XMLCDECL
Daniel Veillard659e71e2003-10-10 14:10:40 +0000453__xmlRaiseError(xmlStructuredErrorFunc schannel,
454 xmlGenericErrorFunc channel, void *data, void *ctx,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000455 void *nod, int domain, int code, xmlErrorLevel level,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000456 const char *file, int line, const char *str1,
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000457 const char *str2, const char *str3, int int1, int col,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000458 const char *msg, ...)
459{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000460 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000461 xmlNodePtr node = (xmlNodePtr) nod;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000462 char *str = NULL;
463 xmlParserInputPtr input = NULL;
464 xmlErrorPtr to = &xmlLastError;
William M. Brackd0407522004-10-02 03:54:00 +0000465 xmlNodePtr baseptr = NULL;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000466
Dmitry V. Levin111d7052011-02-23 22:14:19 +0800467 if (code == XML_ERR_OK)
468 return;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000469 if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
470 return;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000471 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
472 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
Daniel Veillard370ba3d2004-10-25 16:23:56 +0000473 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000474 ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000475 if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
Dmitry V. Levin241d4a12011-02-23 22:30:59 +0800476 (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
477 (ctxt->sax->serror != NULL)) {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000478 schannel = ctxt->sax->serror;
Dmitry V. Levin241d4a12011-02-23 22:30:59 +0800479 data = ctxt->userData;
480 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000481 }
William M. Brackcd3628b2004-07-25 21:07:29 +0000482 /*
483 * Check if structured error handler set
484 */
485 if (schannel == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000486 schannel = xmlStructuredError;
William M. Brackcd3628b2004-07-25 21:07:29 +0000487 /*
488 * if user has defined handler, change data ptr to user's choice
489 */
490 if (schannel != NULL)
Wang Lam1de382e2009-08-24 17:34:25 +0200491 data = xmlStructuredErrorContext;
William M. Brackcd3628b2004-07-25 21:07:29 +0000492 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000493 /*
494 * Formatting the message
495 */
496 if (msg == NULL) {
497 str = (char *) xmlStrdup(BAD_CAST "No error message provided");
498 } else {
499 XML_GET_VAR_STR(msg, str);
500 }
501
502 /*
503 * specific processing if a parser context is provided
504 */
505 if (ctxt != NULL) {
506 if (file == NULL) {
507 input = ctxt->input;
508 if ((input != NULL) && (input->filename == NULL) &&
509 (ctxt->inputNr > 1)) {
510 input = ctxt->inputTab[ctxt->inputNr - 2];
511 }
512 if (input != NULL) {
513 file = input->filename;
514 line = input->line;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000515 col = input->col;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000516 }
517 }
518 to = &ctxt->lastError;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000519 } else if ((node != NULL) && (file == NULL)) {
520 int i;
Daniel Veillard4c004142003-10-07 11:33:24 +0000521
Daniel Veillarddbee0f12005-06-27 13:42:57 +0000522 if ((node->doc != NULL) && (node->doc->URL != NULL)) {
William M. Brackd0407522004-10-02 03:54:00 +0000523 baseptr = node;
Daniel Veillard8ce01ce2005-08-25 20:14:38 +0000524/* file = (const char *) node->doc->URL; */
Daniel Veillarddbee0f12005-06-27 13:42:57 +0000525 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000526 for (i = 0;
527 ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
528 i++)
529 node = node->parent;
William M. Brackd0407522004-10-02 03:54:00 +0000530 if ((baseptr == NULL) && (node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +0000531 (node->doc != NULL) && (node->doc->URL != NULL))
William M. Brackd0407522004-10-02 03:54:00 +0000532 baseptr = node;
Daniel Veillard4c004142003-10-07 11:33:24 +0000533
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000534 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +0000535 line = node->line;
Daniel Veillard968a03a2012-08-13 12:41:33 +0800536 if ((line == 0) || (line == 65535))
537 line = xmlGetLineNo(node);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000538 }
539
540 /*
William M. Brackd233e392004-05-16 03:12:08 +0000541 * Save the information about the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000542 */
543 xmlResetError(to);
544 to->domain = domain;
545 to->code = code;
546 to->message = str;
547 to->level = level;
548 if (file != NULL)
549 to->file = (char *) xmlStrdup((const xmlChar *) file);
William M. Brackd0407522004-10-02 03:54:00 +0000550 else if (baseptr != NULL) {
551#ifdef LIBXML_XINCLUDE_ENABLED
552 /*
553 * We check if the error is within an XInclude section and,
554 * if so, attempt to print out the href of the XInclude instead
555 * of the usual "base" (doc->URL) for the node (bug 152623).
556 */
557 xmlNodePtr prev = baseptr;
558 int inclcount = 0;
559 while (prev != NULL) {
560 if (prev->prev == NULL)
561 prev = prev->parent;
562 else {
563 prev = prev->prev;
564 if (prev->type == XML_XINCLUDE_START) {
565 if (--inclcount < 0)
566 break;
567 } else if (prev->type == XML_XINCLUDE_END)
568 inclcount++;
569 }
570 }
571 if (prev != NULL) {
Daniel Veillard8ce01ce2005-08-25 20:14:38 +0000572 if (prev->type == XML_XINCLUDE_START) {
573 prev->type = XML_ELEMENT_NODE;
574 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
575 prev->type = XML_XINCLUDE_START;
576 } else {
577 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
578 }
William M. Brackd0407522004-10-02 03:54:00 +0000579 } else
580#endif
581 to->file = (char *) xmlStrdup(baseptr->doc->URL);
Daniel Veillard8ce01ce2005-08-25 20:14:38 +0000582 if ((to->file == NULL) && (node != NULL) && (node->doc != NULL)) {
583 to->file = (char *) xmlStrdup(node->doc->URL);
584 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000585 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000586 to->line = line;
587 if (str1 != NULL)
588 to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
589 if (str2 != NULL)
590 to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
591 if (str3 != NULL)
592 to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
593 to->int1 = int1;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000594 to->int2 = col;
Daniel Veillard4c004142003-10-07 11:33:24 +0000595 to->node = node;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000596 to->ctxt = ctx;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000597
Daniel Veillardd34b0b82004-01-02 20:26:01 +0000598 if (to != &xmlLastError)
599 xmlCopyError(to,&xmlLastError);
600
Dmitry V. Levinc2a0fdc2011-02-23 22:44:05 +0800601 if (schannel != NULL) {
602 schannel(data, to);
603 return;
604 }
605
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000606 /*
William M. Brackcd3628b2004-07-25 21:07:29 +0000607 * Find the callback channel if channel param is NULL
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000608 */
Wang Lam1de382e2009-08-24 17:34:25 +0200609 if ((ctxt != NULL) && (channel == NULL) &&
610 (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000611 if (level == XML_ERR_WARNING)
612 channel = ctxt->sax->warning;
613 else
614 channel = ctxt->sax->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000615 data = ctxt->userData;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000616 } else if (channel == NULL) {
Dmitry V. Levinc2a0fdc2011-02-23 22:44:05 +0800617 channel = xmlGenericError;
Pietro Cerutti890faa52012-08-27 13:24:08 +0800618 if (ctxt != NULL) {
619 data = ctxt;
620 } else {
Dmitry V. Levinc2a0fdc2011-02-23 22:44:05 +0800621 data = xmlGenericErrorContext;
Pietro Cerutti890faa52012-08-27 13:24:08 +0800622 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000623 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000624 if (channel == NULL)
625 return;
626
627 if ((channel == xmlParserError) ||
628 (channel == xmlParserWarning) ||
629 (channel == xmlParserValidityError) ||
630 (channel == xmlParserValidityWarning))
Daniel Veillard4c004142003-10-07 11:33:24 +0000631 xmlReportError(to, ctxt, str, NULL, NULL);
632 else if ((channel == (xmlGenericErrorFunc) fprintf) ||
633 (channel == xmlGenericErrorDefaultFunc))
634 xmlReportError(to, ctxt, str, channel, data);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000635 else
636 channel(data, "%s", str);
637}
Daniel Veillard561b7f82002-03-20 21:55:57 +0000638
Daniel Veillarde356c282001-03-10 12:32:04 +0000639/**
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000640 * __xmlSimpleError:
641 * @domain: where the error comes from
642 * @code: the error code
643 * @node: the context node
644 * @extra: extra informations
645 *
646 * Handle an out of memory condition
647 */
648void
649__xmlSimpleError(int domain, int code, xmlNodePtr node,
650 const char *msg, const char *extra)
651{
652
653 if (code == XML_ERR_NO_MEMORY) {
654 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000655 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000656 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
657 NULL, NULL, 0, 0,
658 "Memory allocation failed : %s\n", extra);
659 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000660 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000661 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
662 NULL, NULL, 0, 0, "Memory allocation failed\n");
663 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000664 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000665 code, XML_ERR_ERROR, NULL, 0, extra,
666 NULL, NULL, 0, 0, msg, extra);
667 }
668}
669/**
Owen Taylor3473f882001-02-23 17:55:21 +0000670 * xmlParserError:
671 * @ctx: an XML parser context
672 * @msg: the message to display/transmit
673 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800674 *
Owen Taylor3473f882001-02-23 17:55:21 +0000675 * Display and format an error messages, gives file, line, position and
676 * extra parameters.
677 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000678void XMLCDECL
Owen Taylor3473f882001-02-23 17:55:21 +0000679xmlParserError(void *ctx, const char *msg, ...)
680{
681 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
682 xmlParserInputPtr input = NULL;
683 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000684 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000685
686 if (ctxt != NULL) {
687 input = ctxt->input;
688 if ((input != NULL) && (input->filename == NULL) &&
689 (ctxt->inputNr > 1)) {
690 cur = input;
691 input = ctxt->inputTab[ctxt->inputNr - 2];
692 }
693 xmlParserPrintFileInfo(input);
694 }
695
696 xmlGenericError(xmlGenericErrorContext, "error: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000697 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000698 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000699 if (str != NULL)
700 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000701
702 if (ctxt != NULL) {
703 xmlParserPrintFileContext(input);
704 if (cur != NULL) {
705 xmlParserPrintFileInfo(cur);
706 xmlGenericError(xmlGenericErrorContext, "\n");
707 xmlParserPrintFileContext(cur);
708 }
709 }
710}
711
712/**
713 * xmlParserWarning:
714 * @ctx: an XML parser context
715 * @msg: the message to display/transmit
716 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800717 *
Owen Taylor3473f882001-02-23 17:55:21 +0000718 * Display and format a warning messages, gives file, line, position and
719 * extra parameters.
720 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000721void XMLCDECL
Owen Taylor3473f882001-02-23 17:55:21 +0000722xmlParserWarning(void *ctx, const char *msg, ...)
723{
724 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
725 xmlParserInputPtr input = NULL;
726 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000727 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000728
729 if (ctxt != NULL) {
730 input = ctxt->input;
731 if ((input != NULL) && (input->filename == NULL) &&
732 (ctxt->inputNr > 1)) {
733 cur = input;
734 input = ctxt->inputTab[ctxt->inputNr - 2];
735 }
736 xmlParserPrintFileInfo(input);
737 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800738
Owen Taylor3473f882001-02-23 17:55:21 +0000739 xmlGenericError(xmlGenericErrorContext, "warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000740 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000741 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000742 if (str != NULL)
743 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000744
745 if (ctxt != NULL) {
746 xmlParserPrintFileContext(input);
747 if (cur != NULL) {
748 xmlParserPrintFileInfo(cur);
749 xmlGenericError(xmlGenericErrorContext, "\n");
750 xmlParserPrintFileContext(cur);
751 }
752 }
753}
754
755/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800756 * *
757 * Handling of validation errors *
758 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000759 ************************************************************************/
760
761/**
762 * xmlParserValidityError:
763 * @ctx: an XML parser context
764 * @msg: the message to display/transmit
765 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800766 *
Owen Taylor3473f882001-02-23 17:55:21 +0000767 * Display and format an validity error messages, gives file,
768 * line, position and extra parameters.
769 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000770void XMLCDECL
Owen Taylor3473f882001-02-23 17:55:21 +0000771xmlParserValidityError(void *ctx, const char *msg, ...)
772{
773 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
774 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000775 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000776 int len = xmlStrlen((const xmlChar *) msg);
777 static int had_info = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000778
Daniel Veillard76575762002-09-05 14:21:15 +0000779 if ((len > 1) && (msg[len - 2] != ':')) {
780 if (ctxt != NULL) {
781 input = ctxt->input;
782 if ((input->filename == NULL) && (ctxt->inputNr > 1))
783 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800784
Daniel Veillard76575762002-09-05 14:21:15 +0000785 if (had_info == 0) {
786 xmlParserPrintFileInfo(input);
787 }
788 }
789 xmlGenericError(xmlGenericErrorContext, "validity error: ");
Daniel Veillard76575762002-09-05 14:21:15 +0000790 had_info = 0;
791 } else {
792 had_info = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000793 }
794
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000795 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000796 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000797 if (str != NULL)
798 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000799
Daniel Veillard76575762002-09-05 14:21:15 +0000800 if ((ctxt != NULL) && (input != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000801 xmlParserPrintFileContext(input);
802 }
803}
804
805/**
806 * xmlParserValidityWarning:
807 * @ctx: an XML parser context
808 * @msg: the message to display/transmit
809 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800810 *
Owen Taylor3473f882001-02-23 17:55:21 +0000811 * Display and format a validity warning messages, gives file, line,
812 * position and extra parameters.
813 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000814void XMLCDECL
Owen Taylor3473f882001-02-23 17:55:21 +0000815xmlParserValidityWarning(void *ctx, const char *msg, ...)
816{
817 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
818 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000819 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000820 int len = xmlStrlen((const xmlChar *) msg);
Owen Taylor3473f882001-02-23 17:55:21 +0000821
Daniel Veillard76575762002-09-05 14:21:15 +0000822 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +0000823 input = ctxt->input;
824 if ((input->filename == NULL) && (ctxt->inputNr > 1))
825 input = ctxt->inputTab[ctxt->inputNr - 2];
826
827 xmlParserPrintFileInfo(input);
828 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800829
Owen Taylor3473f882001-02-23 17:55:21 +0000830 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000831 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000832 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000833 if (str != NULL)
834 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000835
836 if (ctxt != NULL) {
837 xmlParserPrintFileContext(input);
838 }
839}
840
841
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000842/************************************************************************
843 * *
844 * Extended Error Handling *
845 * *
846 ************************************************************************/
847
848/**
849 * xmlGetLastError:
850 *
851 * Get the last global error registered. This is per thread if compiled
852 * with thread support.
853 *
854 * Returns NULL if no error occured or a pointer to the error
855 */
856xmlErrorPtr
857xmlGetLastError(void)
858{
859 if (xmlLastError.code == XML_ERR_OK)
860 return (NULL);
861 return (&xmlLastError);
862}
863
864/**
865 * xmlResetError:
866 * @err: pointer to the error.
867 *
868 * Cleanup the error.
869 */
870void
871xmlResetError(xmlErrorPtr err)
872{
873 if (err == NULL)
874 return;
875 if (err->code == XML_ERR_OK)
876 return;
877 if (err->message != NULL)
878 xmlFree(err->message);
879 if (err->file != NULL)
880 xmlFree(err->file);
881 if (err->str1 != NULL)
882 xmlFree(err->str1);
883 if (err->str2 != NULL)
884 xmlFree(err->str2);
885 if (err->str3 != NULL)
886 xmlFree(err->str3);
887 memset(err, 0, sizeof(xmlError));
888 err->code = XML_ERR_OK;
889}
890
891/**
892 * xmlResetLastError:
893 *
894 * Cleanup the last global error registered. For parsing error
895 * this does not change the well-formedness result.
896 */
897void
898xmlResetLastError(void)
899{
900 if (xmlLastError.code == XML_ERR_OK)
901 return;
902 xmlResetError(&xmlLastError);
903}
904
905/**
906 * xmlCtxtGetLastError:
907 * @ctx: an XML parser context
908 *
909 * Get the last parsing error registered.
910 *
911 * Returns NULL if no error occured or a pointer to the error
912 */
913xmlErrorPtr
914xmlCtxtGetLastError(void *ctx)
915{
916 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
917
918 if (ctxt == NULL)
919 return (NULL);
920 if (ctxt->lastError.code == XML_ERR_OK)
921 return (NULL);
922 return (&ctxt->lastError);
923}
924
925/**
926 * xmlCtxtResetLastError:
927 * @ctx: an XML parser context
928 *
929 * Cleanup the last global error registered. For parsing error
930 * this does not change the well-formedness result.
931 */
932void
933xmlCtxtResetLastError(void *ctx)
934{
935 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
936
937 if (ctxt == NULL)
938 return;
Daniel Veillard26b06872010-03-15 15:59:07 +0100939 ctxt->errNo = XML_ERR_OK;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000940 if (ctxt->lastError.code == XML_ERR_OK)
941 return;
942 xmlResetError(&ctxt->lastError);
943}
944
945/**
946 * xmlCopyError:
947 * @from: a source error
948 * @to: a target error
949 *
950 * Save the original error to the new place.
951 *
952 * Returns 0 in case of success and -1 in case of error.
953 */
954int
955xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
William M. Bracka3215c72004-07-31 16:24:01 +0000956 char *message, *file, *str1, *str2, *str3;
957
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000958 if ((from == NULL) || (to == NULL))
959 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +0000960
961 message = (char *) xmlStrdup((xmlChar *) from->message);
962 file = (char *) xmlStrdup ((xmlChar *) from->file);
963 str1 = (char *) xmlStrdup ((xmlChar *) from->str1);
964 str2 = (char *) xmlStrdup ((xmlChar *) from->str2);
965 str3 = (char *) xmlStrdup ((xmlChar *) from->str3);
966
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000967 if (to->message != NULL)
968 xmlFree(to->message);
969 if (to->file != NULL)
970 xmlFree(to->file);
971 if (to->str1 != NULL)
972 xmlFree(to->str1);
973 if (to->str2 != NULL)
974 xmlFree(to->str2);
975 if (to->str3 != NULL)
976 xmlFree(to->str3);
977 to->domain = from->domain;
978 to->code = from->code;
979 to->level = from->level;
980 to->line = from->line;
Daniel Veillard4c004142003-10-07 11:33:24 +0000981 to->node = from->node;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000982 to->int1 = from->int1;
983 to->int2 = from->int2;
Daniel Veillard4c004142003-10-07 11:33:24 +0000984 to->node = from->node;
985 to->ctxt = from->ctxt;
William M. Bracka3215c72004-07-31 16:24:01 +0000986 to->message = message;
987 to->file = file;
988 to->str1 = str1;
989 to->str2 = str2;
990 to->str3 = str3;
991
992 return 0;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000993}
994
Daniel Veillard5d4644e2005-04-01 13:11:58 +0000995#define bottom_error
996#include "elfgcchack.h"