blob: 4de1418e1474ee928ceb51b05bcbf5a26a4dbc53 [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
Nick Wellnhofer0f568c02022-08-26 01:22:33 +020019#include "private/error.h"
Daniel Veillard635ef722001-10-29 11:48:19 +000020
Nick Wellnhofer59b33662022-12-27 14:15:51 +010021#define XML_MAX_ERRORS 100
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 */
Nick Wellnhoferdd3569e2022-12-08 02:43:17 +010070void
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 *
Nick Wellnhofera9669672022-09-09 01:44:00 +020086 * DEPRECATED: Use xmlSetGenericErrorFunc.
87 *
Daniel Veillard9d06d302002-01-22 18:15:52 +000088 * Set or reset (if NULL) the default handler for generic errors
Daniel Veillard7424eb62003-01-24 14:14:52 +000089 * to the builtin error function.
Daniel Veillard9d06d302002-01-22 18:15:52 +000090 */
Daniel Veillardd0463562001-10-13 09:15:48 +000091void
Daniel Veillarddb5850a2002-01-18 11:49:26 +000092initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
Daniel Veillardd0463562001-10-13 09:15:48 +000093{
Daniel Veillarddb5850a2002-01-18 11:49:26 +000094 if (handler == NULL)
95 xmlGenericError = xmlGenericErrorDefaultFunc;
96 else
Daniel Veillardda0ff5d2004-04-20 09:45:26 +000097 xmlGenericError = (*handler);
Daniel Veillardd0463562001-10-13 09:15:48 +000098}
Owen Taylor3473f882001-02-23 17:55:21 +000099
100/**
101 * xmlSetGenericErrorFunc:
102 * @ctx: the new error handling context
103 * @handler: the new handler function
104 *
105 * Function to reset the handler and the error context for out of
106 * context error messages.
107 * This simply means that @handler will be called for subsequent
108 * error messages while not parsing nor validating. And @ctx will
109 * be passed as first argument to @handler
110 * One can simply force messages to be emitted to another FILE * than
111 * stderr by setting @ctx to this file handle and @handler to NULL.
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000112 * For multi-threaded applications, this must be set separately for each thread.
Owen Taylor3473f882001-02-23 17:55:21 +0000113 */
114void
115xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
116 xmlGenericErrorContext = ctx;
117 if (handler != NULL)
118 xmlGenericError = handler;
119 else
120 xmlGenericError = xmlGenericErrorDefaultFunc;
121}
122
Daniel Veillard659e71e2003-10-10 14:10:40 +0000123/**
124 * xmlSetStructuredErrorFunc:
125 * @ctx: the new error handling context
126 * @handler: the new handler function
127 *
128 * Function to reset the handler and the error context for out of
129 * context structured error messages.
130 * This simply means that @handler will be called for subsequent
131 * error messages while not parsing nor validating. And @ctx will
132 * be passed as first argument to @handler
Daniel Veillardda3b29a2004-08-14 11:15:13 +0000133 * For multi-threaded applications, this must be set separately for each thread.
Daniel Veillard659e71e2003-10-10 14:10:40 +0000134 */
135void
136xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
Wang Lam1de382e2009-08-24 17:34:25 +0200137 xmlStructuredErrorContext = ctx;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000138 xmlStructuredError = handler;
139}
140
Owen Taylor3473f882001-02-23 17:55:21 +0000141/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800142 * *
143 * Handling of parsing errors *
144 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000145 ************************************************************************/
146
147/**
148 * xmlParserPrintFileInfo:
149 * @input: an xmlParserInputPtr input
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800150 *
Nick Wellnhofer20c60882020-03-08 17:19:42 +0100151 * Displays the associated file and line information for the current input
Owen Taylor3473f882001-02-23 17:55:21 +0000152 */
153
154void
155xmlParserPrintFileInfo(xmlParserInputPtr input) {
156 if (input != NULL) {
157 if (input->filename)
158 xmlGenericError(xmlGenericErrorContext,
159 "%s:%d: ", input->filename,
160 input->line);
161 else
162 xmlGenericError(xmlGenericErrorContext,
163 "Entity: line %d: ", input->line);
164 }
165}
166
167/**
Nick Wellnhofer76c6da42022-12-04 23:01:00 +0100168 * xmlParserPrintFileContextInternal:
Owen Taylor3473f882001-02-23 17:55:21 +0000169 * @input: an xmlParserInputPtr input
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800170 *
Owen Taylor3473f882001-02-23 17:55:21 +0000171 * Displays current context within the input content for error tracking
172 */
173
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000174static void
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800175xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000176 xmlGenericErrorFunc channel, void *data ) {
Nick Wellnhofer76c6da42022-12-04 23:01:00 +0100177 const xmlChar *cur, *base, *start;
William M. Brackc1939562003-08-05 15:52:22 +0000178 unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
William M. Brack3dd57f72003-05-13 02:06:18 +0000179 xmlChar content[81]; /* space for 80 chars + line terminator */
Daniel Veillard2be30642001-03-27 00:32:28 +0000180 xmlChar *ctnt;
Owen Taylor3473f882001-02-23 17:55:21 +0000181
Pavel Raiskupc4184ba2015-12-01 13:24:44 +0100182 if ((input == NULL) || (input->cur == NULL))
183 return;
Daniel Veillardce0b0d02015-11-20 15:01:22 +0800184
Owen Taylor3473f882001-02-23 17:55:21 +0000185 cur = input->cur;
186 base = input->base;
Daniel Veillard2be30642001-03-27 00:32:28 +0000187 /* skip backwards over any end-of-lines */
William M. Brackc1939562003-08-05 15:52:22 +0000188 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000189 cur--;
Owen Taylor3473f882001-02-23 17:55:21 +0000190 }
191 n = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000192 /* search backwards for beginning-of-line (to max buff size) */
Nick Wellnhoferd9a8dab2023-01-22 12:00:59 +0100193 while ((n < sizeof(content) - 1) && (cur > base) &&
194 (*cur != '\n') && (*cur != '\r')) {
Owen Taylor3473f882001-02-23 17:55:21 +0000195 cur--;
Nick Wellnhoferd9a8dab2023-01-22 12:00:59 +0100196 n++;
197 }
198 if ((n > 0) && ((*cur == '\n') || (*cur == '\r'))) {
Nick Wellnhofer76c6da42022-12-04 23:01:00 +0100199 cur++;
200 } else {
201 /* skip over continuation bytes */
202 while ((cur < input->cur) && ((*cur & 0xC0) == 0x80))
203 cur++;
204 }
William M. Brack3dd57f72003-05-13 02:06:18 +0000205 /* calculate the error position in terms of the current position */
206 col = input->cur - cur;
207 /* search forward for end-of-line (to max buff size) */
Owen Taylor3473f882001-02-23 17:55:21 +0000208 n = 0;
Nick Wellnhofer76c6da42022-12-04 23:01:00 +0100209 start = cur;
William M. Brack3dd57f72003-05-13 02:06:18 +0000210 /* copy selected text to our buffer */
Nick Wellnhofer76c6da42022-12-04 23:01:00 +0100211 while ((*cur != 0) && (*(cur) != '\n') && (*(cur) != '\r')) {
212 int len = input->end - cur;
213 int c = xmlGetUTF8Char(cur, &len);
214
215 if ((c < 0) || (n + len > sizeof(content)-1))
216 break;
217 cur += len;
218 n += len;
Owen Taylor3473f882001-02-23 17:55:21 +0000219 }
Nick Wellnhofer76c6da42022-12-04 23:01:00 +0100220 memcpy(content, start, n);
221 content[n] = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000222 /* print out the selected text */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000223 channel(data ,"%s\n", content);
Daniel Veillard2be30642001-03-27 00:32:28 +0000224 /* create blank line with problem pointer */
Owen Taylor3473f882001-02-23 17:55:21 +0000225 n = 0;
Daniel Veillard7533cc82001-04-24 15:52:00 +0000226 ctnt = content;
William M. Brack3dd57f72003-05-13 02:06:18 +0000227 /* (leave buffer space for pointer + line terminator) */
228 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
William M. Brackc1939562003-08-05 15:52:22 +0000229 if (*(ctnt) != '\t')
230 *(ctnt) = ' ';
William M. Brack69848302003-09-22 00:24:51 +0000231 ctnt++;
Owen Taylor3473f882001-02-23 17:55:21 +0000232 }
William M. Brack3dd57f72003-05-13 02:06:18 +0000233 *ctnt++ = '^';
234 *ctnt = 0;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000235 channel(data ,"%s\n", content);
Owen Taylor3473f882001-02-23 17:55:21 +0000236}
237
Daniel Veillard561b7f82002-03-20 21:55:57 +0000238/**
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000239 * xmlParserPrintFileContext:
240 * @input: an xmlParserInputPtr input
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800241 *
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000242 * Displays current context within the input content for error tracking
Daniel Veillard561b7f82002-03-20 21:55:57 +0000243 */
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000244void
245xmlParserPrintFileContext(xmlParserInputPtr input) {
246 xmlParserPrintFileContextInternal(input, xmlGenericError,
247 xmlGenericErrorContext);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000248}
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000249
250/**
251 * xmlReportError:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000252 * @err: the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000253 * @ctx: the parser context or NULL
254 * @str: the formatted error message
255 *
Jared Yanovich2a350ee2019-09-30 17:04:54 +0200256 * Report an error with its context, replace the 4 old error/warning
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000257 * routines.
258 */
259static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000260xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str,
261 xmlGenericErrorFunc channel, void *data)
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000262{
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000263 char *file = NULL;
264 int line = 0;
265 int code = -1;
266 int domain;
Daniel Veillard4c004142003-10-07 11:33:24 +0000267 const xmlChar *name = NULL;
268 xmlNodePtr node;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000269 xmlErrorLevel level;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000270 xmlParserInputPtr input = NULL;
271 xmlParserInputPtr cur = NULL;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000272
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000273 if (err == NULL)
274 return;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000275
Daniel Veillard4c004142003-10-07 11:33:24 +0000276 if (channel == NULL) {
277 channel = xmlGenericError;
278 data = xmlGenericErrorContext;
279 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000280 file = err->file;
281 line = err->line;
282 code = err->code;
283 domain = err->domain;
284 level = err->level;
Daniel Veillard4c004142003-10-07 11:33:24 +0000285 node = err->node;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000286
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000287 if (code == XML_ERR_OK)
288 return;
289
Daniel Veillard4c004142003-10-07 11:33:24 +0000290 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
291 name = node->name;
292
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000293 /*
294 * Maintain the compatibility with the legacy error handling
295 */
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000296 if (ctxt != NULL) {
297 input = ctxt->input;
298 if ((input != NULL) && (input->filename == NULL) &&
299 (ctxt->inputNr > 1)) {
300 cur = input;
301 input = ctxt->inputTab[ctxt->inputNr - 2];
302 }
303 if (input != NULL) {
304 if (input->filename)
305 channel(data, "%s:%d: ", input->filename, input->line);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000306 else if ((line != 0) && (domain == XML_FROM_PARSER))
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000307 channel(data, "Entity: line %d: ", input->line);
308 }
309 } else {
310 if (file != NULL)
311 channel(data, "%s:%d: ", file, line);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800312 else if ((line != 0) &&
Daniel Veillard97fa5b32012-08-14 11:01:07 +0800313 ((domain == XML_FROM_PARSER) || (domain == XML_FROM_SCHEMASV)||
314 (domain == XML_FROM_SCHEMASP)||(domain == XML_FROM_DTD) ||
315 (domain == XML_FROM_RELAXNGP)||(domain == XML_FROM_RELAXNGV)))
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000316 channel(data, "Entity: line %d: ", line);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000317 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000318 if (name != NULL) {
319 channel(data, "element %s: ", name);
320 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000321 switch (domain) {
322 case XML_FROM_PARSER:
323 channel(data, "parser ");
324 break;
325 case XML_FROM_NAMESPACE:
326 channel(data, "namespace ");
327 break;
328 case XML_FROM_DTD:
Daniel Veillard72b9e292003-10-28 15:44:17 +0000329 case XML_FROM_VALID:
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000330 channel(data, "validity ");
331 break;
332 case XML_FROM_HTML:
333 channel(data, "HTML parser ");
334 break;
335 case XML_FROM_MEMORY:
336 channel(data, "memory ");
337 break;
338 case XML_FROM_OUTPUT:
339 channel(data, "output ");
340 break;
341 case XML_FROM_IO:
342 channel(data, "I/O ");
343 break;
344 case XML_FROM_XINCLUDE:
345 channel(data, "XInclude ");
346 break;
347 case XML_FROM_XPATH:
348 channel(data, "XPath ");
349 break;
350 case XML_FROM_XPOINTER:
351 channel(data, "parser ");
352 break;
353 case XML_FROM_REGEXP:
354 channel(data, "regexp ");
355 break;
Daniel Veillardce1648b2005-01-04 15:10:22 +0000356 case XML_FROM_MODULE:
357 channel(data, "module ");
358 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000359 case XML_FROM_SCHEMASV:
Daniel Veillard87db3a82003-10-10 10:52:58 +0000360 channel(data, "Schemas validity ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000361 break;
362 case XML_FROM_SCHEMASP:
Daniel Veillard87db3a82003-10-10 10:52:58 +0000363 channel(data, "Schemas parser ");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000364 break;
Daniel Veillard4c004142003-10-07 11:33:24 +0000365 case XML_FROM_RELAXNGP:
366 channel(data, "Relax-NG parser ");
367 break;
368 case XML_FROM_RELAXNGV:
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000369 channel(data, "Relax-NG validity ");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000370 break;
371 case XML_FROM_CATALOG:
372 channel(data, "Catalog ");
373 break;
374 case XML_FROM_C14N:
375 channel(data, "C14N ");
376 break;
377 case XML_FROM_XSLT:
378 channel(data, "XSLT ");
379 break;
Daniel Veillard1fc3ed02005-08-24 12:46:09 +0000380 case XML_FROM_I18N:
381 channel(data, "encoding ");
382 break;
Daniel Veillard57560382012-07-24 11:44:23 +0800383 case XML_FROM_SCHEMATRONV:
384 channel(data, "schematron ");
385 break;
386 case XML_FROM_BUFFER:
387 channel(data, "internal buffer ");
388 break;
389 case XML_FROM_URI:
390 channel(data, "URI ");
391 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000392 default:
393 break;
394 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000395 switch (level) {
396 case XML_ERR_NONE:
397 channel(data, ": ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000398 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000399 case XML_ERR_WARNING:
400 channel(data, "warning : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000401 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000402 case XML_ERR_ERROR:
403 channel(data, "error : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000404 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000405 case XML_ERR_FATAL:
406 channel(data, "error : ");
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000407 break;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000408 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000409 if (str != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +0000410 int len;
411 len = xmlStrlen((const xmlChar *)str);
412 if ((len > 0) && (str[len - 1] != '\n'))
Daniel Veillard828ce832003-10-08 19:19:10 +0000413 channel(data, "%s\n", str);
Daniel Veillarda8856222003-10-08 19:26:03 +0000414 else
415 channel(data, "%s", str);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000416 } else {
Daniel Veillard828ce832003-10-08 19:19:10 +0000417 channel(data, "%s\n", "out of memory error");
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000418 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000419
420 if (ctxt != NULL) {
421 xmlParserPrintFileContextInternal(input, channel, data);
422 if (cur != NULL) {
423 if (cur->filename)
424 channel(data, "%s:%d: \n", cur->filename, cur->line);
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000425 else if ((line != 0) && (domain == XML_FROM_PARSER))
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000426 channel(data, "Entity: line %d: \n", cur->line);
427 xmlParserPrintFileContextInternal(cur, channel, data);
428 }
429 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000430 if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
431 (err->int1 < 100) &&
432 (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
433 xmlChar buf[150];
434 int i;
435
436 channel(data, "%s\n", err->str1);
437 for (i=0;i < err->int1;i++)
438 buf[i] = ' ';
439 buf[i++] = '^';
440 buf[i] = 0;
441 channel(data, "%s\n", buf);
442 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000443}
444
445/**
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000446 * __xmlRaiseError:
William M. Brackd233e392004-05-16 03:12:08 +0000447 * @schannel: the structured callback channel
Daniel Veillard659e71e2003-10-10 14:10:40 +0000448 * @channel: the old callback channel
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000449 * @data: the callback data
450 * @ctx: the parser context or NULL
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000451 * @ctx: the parser context or NULL
452 * @domain: the domain for the error
453 * @code: the code for the error
454 * @level: the xmlErrorLevel for the error
455 * @file: the file source of the error (or NULL)
456 * @line: the line of the error or 0 if N/A
457 * @str1: extra string info
458 * @str2: extra string info
459 * @str3: extra string info
460 * @int1: extra int info
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800461 * @col: column number of the error or 0 if N/A
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000462 * @msg: the message to display/transmit
463 * @...: extra parameters for the message display
464 *
William M. Brackd233e392004-05-16 03:12:08 +0000465 * Update the appropriate global or contextual error structure,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000466 * then forward the error message down the parser or generic
467 * error callback handler
468 */
Nick Wellnhoferdd3569e2022-12-08 02:43:17 +0100469void
Daniel Veillard659e71e2003-10-10 14:10:40 +0000470__xmlRaiseError(xmlStructuredErrorFunc schannel,
471 xmlGenericErrorFunc channel, void *data, void *ctx,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000472 void *nod, int domain, int code, xmlErrorLevel level,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000473 const char *file, int line, const char *str1,
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000474 const char *str2, const char *str3, int int1, int col,
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000475 const char *msg, ...)
476{
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000477 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000478 xmlNodePtr node = (xmlNodePtr) nod;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000479 char *str = NULL;
480 xmlParserInputPtr input = NULL;
481 xmlErrorPtr to = &xmlLastError;
William M. Brackd0407522004-10-02 03:54:00 +0000482 xmlNodePtr baseptr = NULL;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000483
Dmitry V. Levin111d7052011-02-23 22:14:19 +0800484 if (code == XML_ERR_OK)
485 return;
Daniel Veillardb5fa0202003-12-08 17:41:29 +0000486 if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
487 return;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000488 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
489 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
Daniel Veillard370ba3d2004-10-25 16:23:56 +0000490 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000491 ctxt = (xmlParserCtxtPtr) ctx;
Nick Wellnhofer59b33662022-12-27 14:15:51 +0100492
493 if (ctxt != NULL) {
494 if (level == XML_ERR_WARNING) {
495 if (ctxt->nbWarnings >= XML_MAX_ERRORS)
496 return;
497 ctxt->nbWarnings += 1;
498 } else {
499 if (ctxt->nbErrors >= XML_MAX_ERRORS)
500 return;
501 ctxt->nbErrors += 1;
502 }
503
504 if ((schannel == NULL) && (ctxt->sax != NULL) &&
505 (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
506 (ctxt->sax->serror != NULL)) {
507 schannel = ctxt->sax->serror;
508 data = ctxt->userData;
509 }
510 }
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000511 }
William M. Brackcd3628b2004-07-25 21:07:29 +0000512 /*
513 * Check if structured error handler set
514 */
515 if (schannel == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000516 schannel = xmlStructuredError;
William M. Brackcd3628b2004-07-25 21:07:29 +0000517 /*
518 * if user has defined handler, change data ptr to user's choice
519 */
520 if (schannel != NULL)
Wang Lam1de382e2009-08-24 17:34:25 +0200521 data = xmlStructuredErrorContext;
William M. Brackcd3628b2004-07-25 21:07:29 +0000522 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000523 /*
524 * Formatting the message
525 */
526 if (msg == NULL) {
527 str = (char *) xmlStrdup(BAD_CAST "No error message provided");
528 } else {
529 XML_GET_VAR_STR(msg, str);
530 }
531
532 /*
533 * specific processing if a parser context is provided
534 */
535 if (ctxt != NULL) {
536 if (file == NULL) {
537 input = ctxt->input;
538 if ((input != NULL) && (input->filename == NULL) &&
539 (ctxt->inputNr > 1)) {
540 input = ctxt->inputTab[ctxt->inputNr - 2];
541 }
542 if (input != NULL) {
543 file = input->filename;
544 line = input->line;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000545 col = input->col;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000546 }
547 }
548 to = &ctxt->lastError;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000549 } else if ((node != NULL) && (file == NULL)) {
550 int i;
Daniel Veillard4c004142003-10-07 11:33:24 +0000551
Daniel Veillarddbee0f12005-06-27 13:42:57 +0000552 if ((node->doc != NULL) && (node->doc->URL != NULL)) {
William M. Brackd0407522004-10-02 03:54:00 +0000553 baseptr = node;
Daniel Veillard8ce01ce2005-08-25 20:14:38 +0000554/* file = (const char *) node->doc->URL; */
Daniel Veillarddbee0f12005-06-27 13:42:57 +0000555 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000556 for (i = 0;
557 ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
558 i++)
559 node = node->parent;
William M. Brackd0407522004-10-02 03:54:00 +0000560 if ((baseptr == NULL) && (node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +0000561 (node->doc != NULL) && (node->doc->URL != NULL))
William M. Brackd0407522004-10-02 03:54:00 +0000562 baseptr = node;
Daniel Veillard4c004142003-10-07 11:33:24 +0000563
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000564 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
Daniel Veillard3e35f8e2003-10-21 00:05:38 +0000565 line = node->line;
Daniel Veillard968a03a2012-08-13 12:41:33 +0800566 if ((line == 0) || (line == 65535))
567 line = xmlGetLineNo(node);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000568 }
569
570 /*
William M. Brackd233e392004-05-16 03:12:08 +0000571 * Save the information about the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000572 */
573 xmlResetError(to);
574 to->domain = domain;
575 to->code = code;
576 to->message = str;
577 to->level = level;
578 if (file != NULL)
579 to->file = (char *) xmlStrdup((const xmlChar *) file);
William M. Brackd0407522004-10-02 03:54:00 +0000580 else if (baseptr != NULL) {
581#ifdef LIBXML_XINCLUDE_ENABLED
582 /*
583 * We check if the error is within an XInclude section and,
584 * if so, attempt to print out the href of the XInclude instead
585 * of the usual "base" (doc->URL) for the node (bug 152623).
586 */
587 xmlNodePtr prev = baseptr;
Nick Wellnhofer2c747122020-08-17 00:54:12 +0200588 char *href = NULL;
William M. Brackd0407522004-10-02 03:54:00 +0000589 int inclcount = 0;
590 while (prev != NULL) {
591 if (prev->prev == NULL)
592 prev = prev->parent;
593 else {
594 prev = prev->prev;
595 if (prev->type == XML_XINCLUDE_START) {
Nick Wellnhofer2c747122020-08-17 00:54:12 +0200596 if (inclcount > 0) {
597 --inclcount;
598 } else {
599 href = (char *) xmlGetProp(prev, BAD_CAST "href");
600 if (href != NULL)
601 break;
602 }
William M. Brackd0407522004-10-02 03:54:00 +0000603 } else if (prev->type == XML_XINCLUDE_END)
604 inclcount++;
605 }
606 }
Nick Wellnhofer2c747122020-08-17 00:54:12 +0200607 if (href != NULL)
608 to->file = href;
609 else
William M. Brackd0407522004-10-02 03:54:00 +0000610#endif
611 to->file = (char *) xmlStrdup(baseptr->doc->URL);
Daniel Veillard8ce01ce2005-08-25 20:14:38 +0000612 if ((to->file == NULL) && (node != NULL) && (node->doc != NULL)) {
613 to->file = (char *) xmlStrdup(node->doc->URL);
614 }
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000615 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000616 to->line = line;
617 if (str1 != NULL)
618 to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
619 if (str2 != NULL)
620 to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
621 if (str3 != NULL)
622 to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
623 to->int1 = int1;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000624 to->int2 = col;
Daniel Veillard4c004142003-10-07 11:33:24 +0000625 to->node = node;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000626 to->ctxt = ctx;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000627
Daniel Veillardd34b0b82004-01-02 20:26:01 +0000628 if (to != &xmlLastError)
629 xmlCopyError(to,&xmlLastError);
630
Dmitry V. Levinc2a0fdc2011-02-23 22:44:05 +0800631 if (schannel != NULL) {
632 schannel(data, to);
633 return;
634 }
635
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000636 /*
William M. Brackcd3628b2004-07-25 21:07:29 +0000637 * Find the callback channel if channel param is NULL
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000638 */
Wang Lam1de382e2009-08-24 17:34:25 +0200639 if ((ctxt != NULL) && (channel == NULL) &&
640 (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000641 if (level == XML_ERR_WARNING)
642 channel = ctxt->sax->warning;
643 else
644 channel = ctxt->sax->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000645 data = ctxt->userData;
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000646 } else if (channel == NULL) {
Dmitry V. Levinc2a0fdc2011-02-23 22:44:05 +0800647 channel = xmlGenericError;
Pietro Cerutti890faa52012-08-27 13:24:08 +0800648 if (ctxt != NULL) {
649 data = ctxt;
650 } else {
Dmitry V. Levinc2a0fdc2011-02-23 22:44:05 +0800651 data = xmlGenericErrorContext;
Pietro Cerutti890faa52012-08-27 13:24:08 +0800652 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000653 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000654 if (channel == NULL)
655 return;
656
657 if ((channel == xmlParserError) ||
658 (channel == xmlParserWarning) ||
659 (channel == xmlParserValidityError) ||
660 (channel == xmlParserValidityWarning))
Daniel Veillard4c004142003-10-07 11:33:24 +0000661 xmlReportError(to, ctxt, str, NULL, NULL);
Nick Wellnhofer19f09502019-01-01 16:38:42 +0100662 else if (((void(*)(void)) channel == (void(*)(void)) fprintf) ||
Daniel Veillard4c004142003-10-07 11:33:24 +0000663 (channel == xmlGenericErrorDefaultFunc))
664 xmlReportError(to, ctxt, str, channel, data);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000665 else
666 channel(data, "%s", str);
667}
Daniel Veillard561b7f82002-03-20 21:55:57 +0000668
Daniel Veillarde356c282001-03-10 12:32:04 +0000669/**
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000670 * __xmlSimpleError:
671 * @domain: where the error comes from
672 * @code: the error code
673 * @node: the context node
Nick Wellnhofer20c60882020-03-08 17:19:42 +0100674 * @extra: extra information
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000675 *
676 * Handle an out of memory condition
677 */
678void
679__xmlSimpleError(int domain, int code, xmlNodePtr node,
680 const char *msg, const char *extra)
681{
682
683 if (code == XML_ERR_NO_MEMORY) {
684 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000685 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000686 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
687 NULL, NULL, 0, 0,
688 "Memory allocation failed : %s\n", extra);
689 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000690 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000691 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
692 NULL, NULL, 0, 0, "Memory allocation failed\n");
693 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000694 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000695 code, XML_ERR_ERROR, NULL, 0, extra,
696 NULL, NULL, 0, 0, msg, extra);
697 }
698}
699/**
Owen Taylor3473f882001-02-23 17:55:21 +0000700 * xmlParserError:
701 * @ctx: an XML parser context
702 * @msg: the message to display/transmit
703 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800704 *
Owen Taylor3473f882001-02-23 17:55:21 +0000705 * Display and format an error messages, gives file, line, position and
706 * extra parameters.
707 */
Nick Wellnhoferdd3569e2022-12-08 02:43:17 +0100708void
Owen Taylor3473f882001-02-23 17:55:21 +0000709xmlParserError(void *ctx, const char *msg, ...)
710{
711 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
712 xmlParserInputPtr input = NULL;
713 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000714 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000715
716 if (ctxt != NULL) {
717 input = ctxt->input;
718 if ((input != NULL) && (input->filename == NULL) &&
719 (ctxt->inputNr > 1)) {
720 cur = input;
721 input = ctxt->inputTab[ctxt->inputNr - 2];
722 }
723 xmlParserPrintFileInfo(input);
724 }
725
726 xmlGenericError(xmlGenericErrorContext, "error: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000727 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000728 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000729 if (str != NULL)
730 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000731
732 if (ctxt != NULL) {
733 xmlParserPrintFileContext(input);
734 if (cur != NULL) {
735 xmlParserPrintFileInfo(cur);
736 xmlGenericError(xmlGenericErrorContext, "\n");
737 xmlParserPrintFileContext(cur);
738 }
739 }
740}
741
742/**
743 * xmlParserWarning:
744 * @ctx: an XML parser context
745 * @msg: the message to display/transmit
746 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800747 *
Owen Taylor3473f882001-02-23 17:55:21 +0000748 * Display and format a warning messages, gives file, line, position and
749 * extra parameters.
750 */
Nick Wellnhoferdd3569e2022-12-08 02:43:17 +0100751void
Owen Taylor3473f882001-02-23 17:55:21 +0000752xmlParserWarning(void *ctx, const char *msg, ...)
753{
754 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
755 xmlParserInputPtr input = NULL;
756 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000757 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000758
759 if (ctxt != NULL) {
760 input = ctxt->input;
761 if ((input != NULL) && (input->filename == NULL) &&
762 (ctxt->inputNr > 1)) {
763 cur = input;
764 input = ctxt->inputTab[ctxt->inputNr - 2];
765 }
766 xmlParserPrintFileInfo(input);
767 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800768
Owen Taylor3473f882001-02-23 17:55:21 +0000769 xmlGenericError(xmlGenericErrorContext, "warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000770 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000771 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000772 if (str != NULL)
773 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000774
775 if (ctxt != NULL) {
776 xmlParserPrintFileContext(input);
777 if (cur != NULL) {
778 xmlParserPrintFileInfo(cur);
779 xmlGenericError(xmlGenericErrorContext, "\n");
780 xmlParserPrintFileContext(cur);
781 }
782 }
783}
784
785/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800786 * *
787 * Handling of validation errors *
788 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000789 ************************************************************************/
790
791/**
792 * xmlParserValidityError:
793 * @ctx: an XML parser context
794 * @msg: the message to display/transmit
795 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800796 *
Owen Taylor3473f882001-02-23 17:55:21 +0000797 * Display and format an validity error messages, gives file,
798 * line, position and extra parameters.
799 */
Nick Wellnhoferdd3569e2022-12-08 02:43:17 +0100800void
Owen Taylor3473f882001-02-23 17:55:21 +0000801xmlParserValidityError(void *ctx, const char *msg, ...)
802{
803 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
804 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000805 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000806 int len = xmlStrlen((const xmlChar *) msg);
807 static int had_info = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000808
Daniel Veillard76575762002-09-05 14:21:15 +0000809 if ((len > 1) && (msg[len - 2] != ':')) {
810 if (ctxt != NULL) {
811 input = ctxt->input;
812 if ((input->filename == NULL) && (ctxt->inputNr > 1))
813 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800814
Daniel Veillard76575762002-09-05 14:21:15 +0000815 if (had_info == 0) {
816 xmlParserPrintFileInfo(input);
817 }
818 }
819 xmlGenericError(xmlGenericErrorContext, "validity error: ");
Daniel Veillard76575762002-09-05 14:21:15 +0000820 had_info = 0;
821 } else {
822 had_info = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000823 }
824
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000825 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000826 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000827 if (str != NULL)
828 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000829
Daniel Veillard76575762002-09-05 14:21:15 +0000830 if ((ctxt != NULL) && (input != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000831 xmlParserPrintFileContext(input);
832 }
833}
834
835/**
836 * xmlParserValidityWarning:
837 * @ctx: an XML parser context
838 * @msg: the message to display/transmit
839 * @...: extra parameters for the message display
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800840 *
Owen Taylor3473f882001-02-23 17:55:21 +0000841 * Display and format a validity warning messages, gives file, line,
842 * position and extra parameters.
843 */
Nick Wellnhoferdd3569e2022-12-08 02:43:17 +0100844void
Owen Taylor3473f882001-02-23 17:55:21 +0000845xmlParserValidityWarning(void *ctx, const char *msg, ...)
846{
847 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
848 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000849 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000850 int len = xmlStrlen((const xmlChar *) msg);
Owen Taylor3473f882001-02-23 17:55:21 +0000851
Daniel Veillard76575762002-09-05 14:21:15 +0000852 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +0000853 input = ctxt->input;
854 if ((input->filename == NULL) && (ctxt->inputNr > 1))
855 input = ctxt->inputTab[ctxt->inputNr - 2];
856
857 xmlParserPrintFileInfo(input);
858 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800859
Owen Taylor3473f882001-02-23 17:55:21 +0000860 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000861 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000862 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000863 if (str != NULL)
864 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000865
866 if (ctxt != NULL) {
867 xmlParserPrintFileContext(input);
868 }
869}
870
871
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000872/************************************************************************
873 * *
874 * Extended Error Handling *
875 * *
876 ************************************************************************/
877
878/**
879 * xmlGetLastError:
880 *
881 * Get the last global error registered. This is per thread if compiled
882 * with thread support.
883 *
Nick Wellnhofer8bbe4502017-06-17 16:15:09 +0200884 * Returns NULL if no error occurred or a pointer to the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000885 */
886xmlErrorPtr
887xmlGetLastError(void)
888{
889 if (xmlLastError.code == XML_ERR_OK)
890 return (NULL);
891 return (&xmlLastError);
892}
893
894/**
895 * xmlResetError:
896 * @err: pointer to the error.
897 *
898 * Cleanup the error.
899 */
900void
901xmlResetError(xmlErrorPtr err)
902{
903 if (err == NULL)
904 return;
905 if (err->code == XML_ERR_OK)
906 return;
907 if (err->message != NULL)
908 xmlFree(err->message);
909 if (err->file != NULL)
910 xmlFree(err->file);
911 if (err->str1 != NULL)
912 xmlFree(err->str1);
913 if (err->str2 != NULL)
914 xmlFree(err->str2);
915 if (err->str3 != NULL)
916 xmlFree(err->str3);
917 memset(err, 0, sizeof(xmlError));
918 err->code = XML_ERR_OK;
919}
920
921/**
922 * xmlResetLastError:
923 *
924 * Cleanup the last global error registered. For parsing error
925 * this does not change the well-formedness result.
926 */
927void
928xmlResetLastError(void)
929{
930 if (xmlLastError.code == XML_ERR_OK)
931 return;
932 xmlResetError(&xmlLastError);
933}
934
935/**
936 * xmlCtxtGetLastError:
937 * @ctx: an XML parser context
938 *
939 * Get the last parsing error registered.
940 *
Nick Wellnhofer8bbe4502017-06-17 16:15:09 +0200941 * Returns NULL if no error occurred or a pointer to the error
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000942 */
943xmlErrorPtr
944xmlCtxtGetLastError(void *ctx)
945{
946 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
947
948 if (ctxt == NULL)
949 return (NULL);
950 if (ctxt->lastError.code == XML_ERR_OK)
951 return (NULL);
952 return (&ctxt->lastError);
953}
954
955/**
956 * xmlCtxtResetLastError:
957 * @ctx: an XML parser context
958 *
959 * Cleanup the last global error registered. For parsing error
960 * this does not change the well-formedness result.
961 */
962void
963xmlCtxtResetLastError(void *ctx)
964{
965 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
966
967 if (ctxt == NULL)
968 return;
Daniel Veillard26b06872010-03-15 15:59:07 +0100969 ctxt->errNo = XML_ERR_OK;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000970 if (ctxt->lastError.code == XML_ERR_OK)
971 return;
972 xmlResetError(&ctxt->lastError);
973}
974
975/**
976 * xmlCopyError:
977 * @from: a source error
978 * @to: a target error
979 *
980 * Save the original error to the new place.
981 *
982 * Returns 0 in case of success and -1 in case of error.
983 */
984int
985xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
William M. Bracka3215c72004-07-31 16:24:01 +0000986 char *message, *file, *str1, *str2, *str3;
987
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000988 if ((from == NULL) || (to == NULL))
989 return(-1);
William M. Bracka3215c72004-07-31 16:24:01 +0000990
991 message = (char *) xmlStrdup((xmlChar *) from->message);
992 file = (char *) xmlStrdup ((xmlChar *) from->file);
993 str1 = (char *) xmlStrdup ((xmlChar *) from->str1);
994 str2 = (char *) xmlStrdup ((xmlChar *) from->str2);
995 str3 = (char *) xmlStrdup ((xmlChar *) from->str3);
996
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000997 if (to->message != NULL)
998 xmlFree(to->message);
999 if (to->file != NULL)
1000 xmlFree(to->file);
1001 if (to->str1 != NULL)
1002 xmlFree(to->str1);
1003 if (to->str2 != NULL)
1004 xmlFree(to->str2);
1005 if (to->str3 != NULL)
1006 xmlFree(to->str3);
1007 to->domain = from->domain;
1008 to->code = from->code;
1009 to->level = from->level;
1010 to->line = from->line;
Daniel Veillard4c004142003-10-07 11:33:24 +00001011 to->node = from->node;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001012 to->int1 = from->int1;
1013 to->int2 = from->int2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001014 to->node = from->node;
1015 to->ctxt = from->ctxt;
William M. Bracka3215c72004-07-31 16:24:01 +00001016 to->message = message;
1017 to->file = file;
1018 to->str1 = str1;
1019 to->str2 = str2;
1020 to->str3 = str3;
1021
1022 return 0;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001023}
1024