blob: 5b5c2891ed11c625b35445db1eb3b6aac24c62d4 [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
Owen Taylor3473f882001-02-23 17:55:21 +000012#include <stdarg.h>
13#include <libxml/parser.h>
14#include <libxml/xmlerror.h>
Daniel Veillarde356c282001-03-10 12:32:04 +000015#include <libxml/xmlmemory.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000016#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000017
Daniel Veillard635ef722001-10-29 11:48:19 +000018void xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED,
19 const char *msg,
20 ...);
21
Daniel Veillard1c43dbf2001-06-05 17:12:52 +000022#define XML_GET_VAR_STR(msg, str) { \
23 int size; \
24 int chars; \
25 char *larger; \
26 va_list ap; \
27 \
28 str = (char *) xmlMalloc(150); \
29 if (str == NULL) \
30 return; \
31 \
32 size = 150; \
33 \
34 while (1) { \
35 va_start(ap, msg); \
36 chars = vsnprintf(str, size, msg, ap); \
37 va_end(ap); \
38 if ((chars > -1) && (chars < size)) \
39 break; \
40 if (chars > -1) \
41 size += chars + 1; \
42 else \
43 size += 100; \
44 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
45 xmlFree(str); \
46 return; \
47 } \
48 str = larger; \
49 } \
50}
Bjorn Reese570ff082001-06-05 12:45:55 +000051
Owen Taylor3473f882001-02-23 17:55:21 +000052/************************************************************************
53 * *
54 * Handling of out of context errors *
55 * *
56 ************************************************************************/
57
58/**
59 * xmlGenericErrorDefaultFunc:
60 * @ctx: an error context
61 * @msg: the message to display/transmit
62 * @...: extra parameters for the message display
63 *
64 * Default handler for out of context error messages.
65 */
Daniel Veillard635ef722001-10-29 11:48:19 +000066void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +000067xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
Owen Taylor3473f882001-02-23 17:55:21 +000068 va_list args;
69
70 if (xmlGenericErrorContext == NULL)
71 xmlGenericErrorContext = (void *) stderr;
72
73 va_start(args, msg);
74 vfprintf((FILE *)xmlGenericErrorContext, msg, args);
75 va_end(args);
76}
77
Daniel Veillard9d06d302002-01-22 18:15:52 +000078/**
79 * initGenericErrorDefaultFunc:
80 * @handler: the handler
81 *
82 * Set or reset (if NULL) the default handler for generic errors
Daniel Veillard7424eb62003-01-24 14:14:52 +000083 * to the builtin error function.
Daniel Veillard9d06d302002-01-22 18:15:52 +000084 */
Daniel Veillardd0463562001-10-13 09:15:48 +000085void
Daniel Veillarddb5850a2002-01-18 11:49:26 +000086initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
Daniel Veillardd0463562001-10-13 09:15:48 +000087{
Daniel Veillarddb5850a2002-01-18 11:49:26 +000088 if (handler == NULL)
89 xmlGenericError = xmlGenericErrorDefaultFunc;
90 else
91 (*handler) = xmlGenericErrorDefaultFunc;
Daniel Veillardd0463562001-10-13 09:15:48 +000092}
Owen Taylor3473f882001-02-23 17:55:21 +000093
94/**
95 * xmlSetGenericErrorFunc:
96 * @ctx: the new error handling context
97 * @handler: the new handler function
98 *
99 * Function to reset the handler and the error context for out of
100 * context error messages.
101 * This simply means that @handler will be called for subsequent
102 * error messages while not parsing nor validating. And @ctx will
103 * be passed as first argument to @handler
104 * One can simply force messages to be emitted to another FILE * than
105 * stderr by setting @ctx to this file handle and @handler to NULL.
106 */
107void
108xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
109 xmlGenericErrorContext = ctx;
110 if (handler != NULL)
111 xmlGenericError = handler;
112 else
113 xmlGenericError = xmlGenericErrorDefaultFunc;
114}
115
116/************************************************************************
117 * *
118 * Handling of parsing errors *
119 * *
120 ************************************************************************/
121
122/**
123 * xmlParserPrintFileInfo:
124 * @input: an xmlParserInputPtr input
125 *
126 * Displays the associated file and line informations for the current input
127 */
128
129void
130xmlParserPrintFileInfo(xmlParserInputPtr input) {
131 if (input != NULL) {
132 if (input->filename)
133 xmlGenericError(xmlGenericErrorContext,
134 "%s:%d: ", input->filename,
135 input->line);
136 else
137 xmlGenericError(xmlGenericErrorContext,
138 "Entity: line %d: ", input->line);
139 }
140}
141
142/**
143 * xmlParserPrintFileContext:
144 * @input: an xmlParserInputPtr input
145 *
146 * Displays current context within the input content for error tracking
147 */
148
149void
Daniel Veillard561b7f82002-03-20 21:55:57 +0000150xmlParserPrintFileContext(xmlParserInputPtr input) {
151 const xmlChar *cur, *base;
William M. Brackc1939562003-08-05 15:52:22 +0000152 unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
William M. Brack3dd57f72003-05-13 02:06:18 +0000153 xmlChar content[81]; /* space for 80 chars + line terminator */
Daniel Veillard2be30642001-03-27 00:32:28 +0000154 xmlChar *ctnt;
Owen Taylor3473f882001-02-23 17:55:21 +0000155
Daniel Veillard561b7f82002-03-20 21:55:57 +0000156 if (input == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000157 cur = input->cur;
158 base = input->base;
Daniel Veillard2be30642001-03-27 00:32:28 +0000159 /* skip backwards over any end-of-lines */
William M. Brackc1939562003-08-05 15:52:22 +0000160 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000161 cur--;
Owen Taylor3473f882001-02-23 17:55:21 +0000162 }
163 n = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000164 /* search backwards for beginning-of-line (to max buff size) */
William M. Brackc1939562003-08-05 15:52:22 +0000165 while ((n++ < (sizeof(content)-1)) && (cur > base) &&
166 (*(cur) != '\n') && (*(cur) != '\r'))
Owen Taylor3473f882001-02-23 17:55:21 +0000167 cur--;
William M. Brackc1939562003-08-05 15:52:22 +0000168 if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
William M. Brack3dd57f72003-05-13 02:06:18 +0000169 /* calculate the error position in terms of the current position */
170 col = input->cur - cur;
171 /* search forward for end-of-line (to max buff size) */
Owen Taylor3473f882001-02-23 17:55:21 +0000172 n = 0;
Daniel Veillard2be30642001-03-27 00:32:28 +0000173 ctnt = content;
William M. Brack3dd57f72003-05-13 02:06:18 +0000174 /* copy selected text to our buffer */
William M. Brackc1939562003-08-05 15:52:22 +0000175 while ((*cur != 0) && (*(cur) != '\n') &&
176 (*(cur) != '\r') && (n < sizeof(content)-1)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000177 *ctnt++ = *cur++;
178 n++;
Owen Taylor3473f882001-02-23 17:55:21 +0000179 }
Daniel Veillard2be30642001-03-27 00:32:28 +0000180 *ctnt = 0;
William M. Brack3dd57f72003-05-13 02:06:18 +0000181 /* print out the selected text */
Daniel Veillard561b7f82002-03-20 21:55:57 +0000182 xmlGenericError(xmlGenericErrorContext,"%s\n", content);
Daniel Veillard2be30642001-03-27 00:32:28 +0000183 /* create blank line with problem pointer */
Owen Taylor3473f882001-02-23 17:55:21 +0000184 n = 0;
Daniel Veillard7533cc82001-04-24 15:52:00 +0000185 ctnt = content;
William M. Brack3dd57f72003-05-13 02:06:18 +0000186 /* (leave buffer space for pointer + line terminator) */
187 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
William M. Brackc1939562003-08-05 15:52:22 +0000188 if (*(ctnt) != '\t')
189 *(ctnt) = ' ';
William M. Brack3dd57f72003-05-13 02:06:18 +0000190 *ctnt++;
Owen Taylor3473f882001-02-23 17:55:21 +0000191 }
William M. Brack3dd57f72003-05-13 02:06:18 +0000192 *ctnt++ = '^';
193 *ctnt = 0;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000194 xmlGenericError(xmlGenericErrorContext,"%s\n", content);
Owen Taylor3473f882001-02-23 17:55:21 +0000195}
196
Daniel Veillard561b7f82002-03-20 21:55:57 +0000197#if 0
198/**
199 * xmlGetVarStr:
200 * @msg: the message format
201 * @args: a va_list argument list
202 *
203 * SGS contribution
204 * Get an arbitrary-sized string for an error argument
205 * The caller must free() the returned string
206 */
207static char *
208xmlGetVarStr(const char * msg, va_list args) {
209 int size;
210 int length;
211 int chars, left;
212 char *str, *larger;
213 va_list ap;
214
215 str = (char *) xmlMalloc(150);
216 if (str == NULL)
217 return(NULL);
218
219 size = 150;
220 length = 0;
221
222 while (1) {
223 left = size - length;
224 /* Try to print in the allocated space. */
225 va_start(msg, ap);
226 chars = vsnprintf(str + length, left, msg, ap);
227 va_end(ap);
228 /* If that worked, we're done. */
229 if ((chars > -1) && (chars < left ))
230 break;
231 /* Else try again with more space. */
232 if (chars > -1) /* glibc 2.1 */
233 size += chars + 1; /* precisely what is needed */
234 else /* glibc 2.0 */
235 size += 100;
236 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
237 xmlFree(str);
238 return(NULL);
239 }
240 str = larger;
241 }
242 return(str);
243}
244#endif
245
Daniel Veillarde356c282001-03-10 12:32:04 +0000246/**
Owen Taylor3473f882001-02-23 17:55:21 +0000247 * xmlParserError:
248 * @ctx: an XML parser context
249 * @msg: the message to display/transmit
250 * @...: extra parameters for the message display
251 *
252 * Display and format an error messages, gives file, line, position and
253 * extra parameters.
254 */
255void
256xmlParserError(void *ctx, const char *msg, ...)
257{
258 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
259 xmlParserInputPtr input = NULL;
260 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000261 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000262
263 if (ctxt != NULL) {
264 input = ctxt->input;
265 if ((input != NULL) && (input->filename == NULL) &&
266 (ctxt->inputNr > 1)) {
267 cur = input;
268 input = ctxt->inputTab[ctxt->inputNr - 2];
269 }
270 xmlParserPrintFileInfo(input);
271 }
272
273 xmlGenericError(xmlGenericErrorContext, "error: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000274 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000275 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000276 if (str != NULL)
277 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000278
279 if (ctxt != NULL) {
280 xmlParserPrintFileContext(input);
281 if (cur != NULL) {
282 xmlParserPrintFileInfo(cur);
283 xmlGenericError(xmlGenericErrorContext, "\n");
284 xmlParserPrintFileContext(cur);
285 }
286 }
287}
288
289/**
290 * xmlParserWarning:
291 * @ctx: an XML parser context
292 * @msg: the message to display/transmit
293 * @...: extra parameters for the message display
294 *
295 * Display and format a warning messages, gives file, line, position and
296 * extra parameters.
297 */
298void
299xmlParserWarning(void *ctx, const char *msg, ...)
300{
301 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
302 xmlParserInputPtr input = NULL;
303 xmlParserInputPtr cur = NULL;
Daniel Veillarde356c282001-03-10 12:32:04 +0000304 char * str;
Owen Taylor3473f882001-02-23 17:55:21 +0000305
306 if (ctxt != NULL) {
307 input = ctxt->input;
308 if ((input != NULL) && (input->filename == NULL) &&
309 (ctxt->inputNr > 1)) {
310 cur = input;
311 input = ctxt->inputTab[ctxt->inputNr - 2];
312 }
313 xmlParserPrintFileInfo(input);
314 }
315
316 xmlGenericError(xmlGenericErrorContext, "warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000317 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000318 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000319 if (str != NULL)
320 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000321
322 if (ctxt != NULL) {
323 xmlParserPrintFileContext(input);
324 if (cur != NULL) {
325 xmlParserPrintFileInfo(cur);
326 xmlGenericError(xmlGenericErrorContext, "\n");
327 xmlParserPrintFileContext(cur);
328 }
329 }
330}
331
332/************************************************************************
333 * *
334 * Handling of validation errors *
335 * *
336 ************************************************************************/
337
338/**
339 * xmlParserValidityError:
340 * @ctx: an XML parser context
341 * @msg: the message to display/transmit
342 * @...: extra parameters for the message display
343 *
344 * Display and format an validity error messages, gives file,
345 * line, position and extra parameters.
346 */
347void
348xmlParserValidityError(void *ctx, const char *msg, ...)
349{
350 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
351 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000352 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000353 int len = xmlStrlen((const xmlChar *) msg);
354 static int had_info = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000355
Daniel Veillard76575762002-09-05 14:21:15 +0000356 if ((len > 1) && (msg[len - 2] != ':')) {
357 if (ctxt != NULL) {
358 input = ctxt->input;
359 if ((input->filename == NULL) && (ctxt->inputNr > 1))
360 input = ctxt->inputTab[ctxt->inputNr - 2];
361
362 if (had_info == 0) {
363 xmlParserPrintFileInfo(input);
364 }
365 }
366 xmlGenericError(xmlGenericErrorContext, "validity error: ");
Daniel Veillard76575762002-09-05 14:21:15 +0000367 had_info = 0;
368 } else {
369 had_info = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000370 }
371
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000372 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000373 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000374 if (str != NULL)
375 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000376
Daniel Veillard76575762002-09-05 14:21:15 +0000377 if ((ctxt != NULL) && (input != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000378 xmlParserPrintFileContext(input);
379 }
380}
381
382/**
383 * xmlParserValidityWarning:
384 * @ctx: an XML parser context
385 * @msg: the message to display/transmit
386 * @...: extra parameters for the message display
387 *
388 * Display and format a validity warning messages, gives file, line,
389 * position and extra parameters.
390 */
391void
392xmlParserValidityWarning(void *ctx, const char *msg, ...)
393{
394 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
395 xmlParserInputPtr input = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000396 char * str;
Daniel Veillard76575762002-09-05 14:21:15 +0000397 int len = xmlStrlen((const xmlChar *) msg);
Owen Taylor3473f882001-02-23 17:55:21 +0000398
Daniel Veillard76575762002-09-05 14:21:15 +0000399 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
Owen Taylor3473f882001-02-23 17:55:21 +0000400 input = ctxt->input;
401 if ((input->filename == NULL) && (ctxt->inputNr > 1))
402 input = ctxt->inputTab[ctxt->inputNr - 2];
403
404 xmlParserPrintFileInfo(input);
405 }
406
407 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
Daniel Veillard1c43dbf2001-06-05 17:12:52 +0000408 XML_GET_VAR_STR(msg, str);
Daniel Veillard635ef722001-10-29 11:48:19 +0000409 xmlGenericError(xmlGenericErrorContext, "%s", str);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000410 if (str != NULL)
411 xmlFree(str);
Owen Taylor3473f882001-02-23 17:55:21 +0000412
413 if (ctxt != NULL) {
414 xmlParserPrintFileContext(input);
415 }
416}
417
418