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