2 * error.c: module displaying/handling XML parser errors
4 * See Copyright for the status of this software.
6 * Daniel Veillard <daniel@veillard.com>
13 #include <libxml/parser.h>
14 #include <libxml/xmlerror.h>
15 #include <libxml/xmlmemory.h>
16 #include <libxml/globals.h>
18 void xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED,
22 #define XML_GET_VAR_STR(msg, str) { \
28 str = (char *) xmlMalloc(150); \
36 chars = vsnprintf(str, size, msg, ap); \
38 if ((chars > -1) && (chars < size)) \
44 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
52 /************************************************************************
54 * Handling of out of context errors *
56 ************************************************************************/
59 * xmlGenericErrorDefaultFunc:
60 * @ctx: an error context
61 * @msg: the message to display/transmit
62 * @...: extra parameters for the message display
64 * Default handler for out of context error messages.
67 xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
70 if (xmlGenericErrorContext == NULL)
71 xmlGenericErrorContext = (void *) stderr;
74 vfprintf((FILE *)xmlGenericErrorContext, msg, args);
79 * initGenericErrorDefaultFunc:
80 * @handler: the handler
82 * Set or reset (if NULL) the default handler for generic errors
83 * to the builtin error function.
86 initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
89 xmlGenericError = xmlGenericErrorDefaultFunc;
91 (*handler) = xmlGenericErrorDefaultFunc;
95 * xmlSetGenericErrorFunc:
96 * @ctx: the new error handling context
97 * @handler: the new handler function
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.
108 xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
109 xmlGenericErrorContext = ctx;
111 xmlGenericError = handler;
113 xmlGenericError = xmlGenericErrorDefaultFunc;
116 /************************************************************************
118 * Handling of parsing errors *
120 ************************************************************************/
123 * xmlParserPrintFileInfo:
124 * @input: an xmlParserInputPtr input
126 * Displays the associated file and line informations for the current input
130 xmlParserPrintFileInfo(xmlParserInputPtr input) {
133 xmlGenericError(xmlGenericErrorContext,
134 "%s:%d: ", input->filename,
137 xmlGenericError(xmlGenericErrorContext,
138 "Entity: line %d: ", input->line);
143 * xmlParserPrintFileContext:
144 * @input: an xmlParserInputPtr input
146 * Displays current context within the input content for error tracking
150 xmlParserPrintFileContext(xmlParserInputPtr input) {
151 const xmlChar *cur, *base;
156 if (input == NULL) return;
159 /* skip backwards over any end-of-lines */
160 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
164 /* search backwards for beginning-of-line maximum 80 characters */
165 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
167 if ((*cur == '\n') || (*cur == '\r')) cur++;
168 /* search forward for end-of-line maximum 80 characters */
171 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
176 xmlGenericError(xmlGenericErrorContext,"%s\n", content);
177 /* create blank line with problem pointer */
179 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
184 while ((n++ < 79) && (cur > base) && (*cur != '\n') && (*cur != '\r')) {
188 if (ctnt > content) {
195 xmlGenericError(xmlGenericErrorContext,"%s\n", content);
201 * @msg: the message format
202 * @args: a va_list argument list
205 * Get an arbitrary-sized string for an error argument
206 * The caller must free() the returned string
209 xmlGetVarStr(const char * msg, va_list args) {
216 str = (char *) xmlMalloc(150);
224 left = size - length;
225 /* Try to print in the allocated space. */
227 chars = vsnprintf(str + length, left, msg, ap);
229 /* If that worked, we're done. */
230 if ((chars > -1) && (chars < left ))
232 /* Else try again with more space. */
233 if (chars > -1) /* glibc 2.1 */
234 size += chars + 1; /* precisely what is needed */
237 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
249 * @ctx: an XML parser context
250 * @msg: the message to display/transmit
251 * @...: extra parameters for the message display
253 * Display and format an error messages, gives file, line, position and
257 xmlParserError(void *ctx, const char *msg, ...)
259 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
260 xmlParserInputPtr input = NULL;
261 xmlParserInputPtr cur = NULL;
266 if ((input != NULL) && (input->filename == NULL) &&
267 (ctxt->inputNr > 1)) {
269 input = ctxt->inputTab[ctxt->inputNr - 2];
271 xmlParserPrintFileInfo(input);
274 xmlGenericError(xmlGenericErrorContext, "error: ");
275 XML_GET_VAR_STR(msg, str);
276 xmlGenericError(xmlGenericErrorContext, "%s", str);
281 xmlParserPrintFileContext(input);
283 xmlParserPrintFileInfo(cur);
284 xmlGenericError(xmlGenericErrorContext, "\n");
285 xmlParserPrintFileContext(cur);
292 * @ctx: an XML parser context
293 * @msg: the message to display/transmit
294 * @...: extra parameters for the message display
296 * Display and format a warning messages, gives file, line, position and
300 xmlParserWarning(void *ctx, const char *msg, ...)
302 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
303 xmlParserInputPtr input = NULL;
304 xmlParserInputPtr cur = NULL;
309 if ((input != NULL) && (input->filename == NULL) &&
310 (ctxt->inputNr > 1)) {
312 input = ctxt->inputTab[ctxt->inputNr - 2];
314 xmlParserPrintFileInfo(input);
317 xmlGenericError(xmlGenericErrorContext, "warning: ");
318 XML_GET_VAR_STR(msg, str);
319 xmlGenericError(xmlGenericErrorContext, "%s", str);
324 xmlParserPrintFileContext(input);
326 xmlParserPrintFileInfo(cur);
327 xmlGenericError(xmlGenericErrorContext, "\n");
328 xmlParserPrintFileContext(cur);
333 /************************************************************************
335 * Handling of validation errors *
337 ************************************************************************/
340 * xmlParserValidityError:
341 * @ctx: an XML parser context
342 * @msg: the message to display/transmit
343 * @...: extra parameters for the message display
345 * Display and format an validity error messages, gives file,
346 * line, position and extra parameters.
349 xmlParserValidityError(void *ctx, const char *msg, ...)
351 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
352 xmlParserInputPtr input = NULL;
354 int len = xmlStrlen((const xmlChar *) msg);
355 static int had_info = 0;
356 int need_context = 0;
358 if ((len > 1) && (msg[len - 2] != ':')) {
361 if ((input->filename == NULL) && (ctxt->inputNr > 1))
362 input = ctxt->inputTab[ctxt->inputNr - 2];
365 xmlParserPrintFileInfo(input);
368 xmlGenericError(xmlGenericErrorContext, "validity error: ");
375 XML_GET_VAR_STR(msg, str);
376 xmlGenericError(xmlGenericErrorContext, "%s", str);
380 if ((ctxt != NULL) && (input != NULL)) {
381 xmlParserPrintFileContext(input);
386 * xmlParserValidityWarning:
387 * @ctx: an XML parser context
388 * @msg: the message to display/transmit
389 * @...: extra parameters for the message display
391 * Display and format a validity warning messages, gives file, line,
392 * position and extra parameters.
395 xmlParserValidityWarning(void *ctx, const char *msg, ...)
397 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
398 xmlParserInputPtr input = NULL;
400 int len = xmlStrlen((const xmlChar *) msg);
402 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
404 if ((input->filename == NULL) && (ctxt->inputNr > 1))
405 input = ctxt->inputTab[ctxt->inputNr - 2];
407 xmlParserPrintFileInfo(input);
410 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
411 XML_GET_VAR_STR(msg, str);
412 xmlGenericError(xmlGenericErrorContext, "%s", str);
417 xmlParserPrintFileContext(input);