2 * error.c: module displaying/handling XML parser errors
4 * See Copyright for the status of this software.
6 * Daniel Veillard <daniel@veillard.com>
14 #include <libxml/parser.h>
15 #include <libxml/xmlerror.h>
16 #include <libxml/xmlmemory.h>
17 #include <libxml/globals.h>
19 void XMLCDECL xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED,
21 ...) LIBXML_ATTR_FORMAT(2,3);
23 #define XML_GET_VAR_STR(msg, str) { \
24 int size, prev_size = -1; \
29 str = (char *) xmlMalloc(150); \
34 while (size < 64000) { \
36 chars = vsnprintf(str, size, msg, ap); \
38 if ((chars > -1) && (chars < size)) { \
39 if (prev_size == chars) { \
49 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
56 /************************************************************************
58 * Handling of out of context errors *
60 ************************************************************************/
63 * xmlGenericErrorDefaultFunc:
64 * @ctx: an error context
65 * @msg: the message to display/transmit
66 * @...: extra parameters for the message display
68 * Default handler for out of context error messages.
71 xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
74 if (xmlGenericErrorContext == NULL)
75 xmlGenericErrorContext = (void *) stderr;
78 vfprintf((FILE *)xmlGenericErrorContext, msg, args);
83 * initGenericErrorDefaultFunc:
84 * @handler: the handler
86 * Set or reset (if NULL) the default handler for generic errors
87 * to the builtin error function.
90 initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
93 xmlGenericError = xmlGenericErrorDefaultFunc;
95 xmlGenericError = (*handler);
99 * xmlSetGenericErrorFunc:
100 * @ctx: the new error handling context
101 * @handler: the new handler function
103 * Function to reset the handler and the error context for out of
104 * context error messages.
105 * This simply means that @handler will be called for subsequent
106 * error messages while not parsing nor validating. And @ctx will
107 * be passed as first argument to @handler
108 * One can simply force messages to be emitted to another FILE * than
109 * stderr by setting @ctx to this file handle and @handler to NULL.
110 * For multi-threaded applications, this must be set separately for each thread.
113 xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
114 xmlGenericErrorContext = ctx;
116 xmlGenericError = handler;
118 xmlGenericError = xmlGenericErrorDefaultFunc;
122 * xmlSetStructuredErrorFunc:
123 * @ctx: the new error handling context
124 * @handler: the new handler function
126 * Function to reset the handler and the error context for out of
127 * context structured error messages.
128 * This simply means that @handler will be called for subsequent
129 * error messages while not parsing nor validating. And @ctx will
130 * be passed as first argument to @handler
131 * For multi-threaded applications, this must be set separately for each thread.
134 xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
135 xmlStructuredErrorContext = ctx;
136 xmlStructuredError = handler;
139 /************************************************************************
141 * Handling of parsing errors *
143 ************************************************************************/
146 * xmlParserPrintFileInfo:
147 * @input: an xmlParserInputPtr input
149 * Displays the associated file and line informations for the current input
153 xmlParserPrintFileInfo(xmlParserInputPtr input) {
156 xmlGenericError(xmlGenericErrorContext,
157 "%s:%d: ", input->filename,
160 xmlGenericError(xmlGenericErrorContext,
161 "Entity: line %d: ", input->line);
166 * xmlParserPrintFileContext:
167 * @input: an xmlParserInputPtr input
169 * Displays current context within the input content for error tracking
173 xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
174 xmlGenericErrorFunc channel, void *data ) {
175 const xmlChar *cur, *base;
176 unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
177 xmlChar content[81]; /* space for 80 chars + line terminator */
180 if ((input == NULL) || (input->cur == NULL))
185 /* skip backwards over any end-of-lines */
186 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
190 /* search backwards for beginning-of-line (to max buff size) */
191 while ((n++ < (sizeof(content)-1)) && (cur > base) &&
192 (*(cur) != '\n') && (*(cur) != '\r'))
194 if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
195 /* calculate the error position in terms of the current position */
196 col = input->cur - cur;
197 /* search forward for end-of-line (to max buff size) */
200 /* copy selected text to our buffer */
201 while ((*cur != 0) && (*(cur) != '\n') &&
202 (*(cur) != '\r') && (n < sizeof(content)-1)) {
207 /* print out the selected text */
208 channel(data ,"%s\n", content);
209 /* create blank line with problem pointer */
212 /* (leave buffer space for pointer + line terminator) */
213 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
220 channel(data ,"%s\n", content);
224 * xmlParserPrintFileContext:
225 * @input: an xmlParserInputPtr input
227 * Displays current context within the input content for error tracking
230 xmlParserPrintFileContext(xmlParserInputPtr input) {
231 xmlParserPrintFileContextInternal(input, xmlGenericError,
232 xmlGenericErrorContext);
238 * @ctx: the parser context or NULL
239 * @str: the formatted error message
241 * Report an erro with its context, replace the 4 old error/warning
245 xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str,
246 xmlGenericErrorFunc channel, void *data)
252 const xmlChar *name = NULL;
255 xmlParserInputPtr input = NULL;
256 xmlParserInputPtr cur = NULL;
261 if (channel == NULL) {
262 channel = xmlGenericError;
263 data = xmlGenericErrorContext;
268 domain = err->domain;
272 if (code == XML_ERR_OK)
275 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
279 * Maintain the compatibility with the legacy error handling
283 if ((input != NULL) && (input->filename == NULL) &&
284 (ctxt->inputNr > 1)) {
286 input = ctxt->inputTab[ctxt->inputNr - 2];
290 channel(data, "%s:%d: ", input->filename, input->line);
291 else if ((line != 0) && (domain == XML_FROM_PARSER))
292 channel(data, "Entity: line %d: ", input->line);
296 channel(data, "%s:%d: ", file, line);
297 else if ((line != 0) &&
298 ((domain == XML_FROM_PARSER) || (domain == XML_FROM_SCHEMASV)||
299 (domain == XML_FROM_SCHEMASP)||(domain == XML_FROM_DTD) ||
300 (domain == XML_FROM_RELAXNGP)||(domain == XML_FROM_RELAXNGV)))
301 channel(data, "Entity: line %d: ", line);
304 channel(data, "element %s: ", name);
307 case XML_FROM_PARSER:
308 channel(data, "parser ");
310 case XML_FROM_NAMESPACE:
311 channel(data, "namespace ");
315 channel(data, "validity ");
318 channel(data, "HTML parser ");
320 case XML_FROM_MEMORY:
321 channel(data, "memory ");
323 case XML_FROM_OUTPUT:
324 channel(data, "output ");
327 channel(data, "I/O ");
329 case XML_FROM_XINCLUDE:
330 channel(data, "XInclude ");
333 channel(data, "XPath ");
335 case XML_FROM_XPOINTER:
336 channel(data, "parser ");
338 case XML_FROM_REGEXP:
339 channel(data, "regexp ");
341 case XML_FROM_MODULE:
342 channel(data, "module ");
344 case XML_FROM_SCHEMASV:
345 channel(data, "Schemas validity ");
347 case XML_FROM_SCHEMASP:
348 channel(data, "Schemas parser ");
350 case XML_FROM_RELAXNGP:
351 channel(data, "Relax-NG parser ");
353 case XML_FROM_RELAXNGV:
354 channel(data, "Relax-NG validity ");
356 case XML_FROM_CATALOG:
357 channel(data, "Catalog ");
360 channel(data, "C14N ");
363 channel(data, "XSLT ");
366 channel(data, "encoding ");
368 case XML_FROM_SCHEMATRONV:
369 channel(data, "schematron ");
371 case XML_FROM_BUFFER:
372 channel(data, "internal buffer ");
375 channel(data, "URI ");
384 case XML_ERR_WARNING:
385 channel(data, "warning : ");
388 channel(data, "error : ");
391 channel(data, "error : ");
396 len = xmlStrlen((const xmlChar *)str);
397 if ((len > 0) && (str[len - 1] != '\n'))
398 channel(data, "%s\n", str);
400 channel(data, "%s", str);
402 channel(data, "%s\n", "out of memory error");
406 xmlParserPrintFileContextInternal(input, channel, data);
409 channel(data, "%s:%d: \n", cur->filename, cur->line);
410 else if ((line != 0) && (domain == XML_FROM_PARSER))
411 channel(data, "Entity: line %d: \n", cur->line);
412 xmlParserPrintFileContextInternal(cur, channel, data);
415 if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
417 (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
421 channel(data, "%s\n", err->str1);
422 for (i=0;i < err->int1;i++)
426 channel(data, "%s\n", buf);
432 * @schannel: the structured callback channel
433 * @channel: the old callback channel
434 * @data: the callback data
435 * @ctx: the parser context or NULL
436 * @ctx: the parser context or NULL
437 * @domain: the domain for the error
438 * @code: the code for the error
439 * @level: the xmlErrorLevel for the error
440 * @file: the file source of the error (or NULL)
441 * @line: the line of the error or 0 if N/A
442 * @str1: extra string info
443 * @str2: extra string info
444 * @str3: extra string info
445 * @int1: extra int info
446 * @col: column number of the error or 0 if N/A
447 * @msg: the message to display/transmit
448 * @...: extra parameters for the message display
450 * Update the appropriate global or contextual error structure,
451 * then forward the error message down the parser or generic
452 * error callback handler
455 __xmlRaiseError(xmlStructuredErrorFunc schannel,
456 xmlGenericErrorFunc channel, void *data, void *ctx,
457 void *nod, int domain, int code, xmlErrorLevel level,
458 const char *file, int line, const char *str1,
459 const char *str2, const char *str3, int int1, int col,
460 const char *msg, ...)
462 xmlParserCtxtPtr ctxt = NULL;
463 xmlNodePtr node = (xmlNodePtr) nod;
465 xmlParserInputPtr input = NULL;
466 xmlErrorPtr to = &xmlLastError;
467 xmlNodePtr baseptr = NULL;
469 if (code == XML_ERR_OK)
471 if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
473 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
474 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
475 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
476 ctxt = (xmlParserCtxtPtr) ctx;
477 if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
478 (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
479 (ctxt->sax->serror != NULL)) {
480 schannel = ctxt->sax->serror;
481 data = ctxt->userData;
485 * Check if structured error handler set
487 if (schannel == NULL) {
488 schannel = xmlStructuredError;
490 * if user has defined handler, change data ptr to user's choice
492 if (schannel != NULL)
493 data = xmlStructuredErrorContext;
496 * Formatting the message
499 str = (char *) xmlStrdup(BAD_CAST "No error message provided");
501 XML_GET_VAR_STR(msg, str);
505 * specific processing if a parser context is provided
510 if ((input != NULL) && (input->filename == NULL) &&
511 (ctxt->inputNr > 1)) {
512 input = ctxt->inputTab[ctxt->inputNr - 2];
515 file = input->filename;
520 to = &ctxt->lastError;
521 } else if ((node != NULL) && (file == NULL)) {
524 if ((node->doc != NULL) && (node->doc->URL != NULL)) {
526 /* file = (const char *) node->doc->URL; */
529 ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
532 if ((baseptr == NULL) && (node != NULL) &&
533 (node->doc != NULL) && (node->doc->URL != NULL))
536 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
538 if ((line == 0) || (line == 65535))
539 line = xmlGetLineNo(node);
543 * Save the information about the error
551 to->file = (char *) xmlStrdup((const xmlChar *) file);
552 else if (baseptr != NULL) {
553 #ifdef LIBXML_XINCLUDE_ENABLED
555 * We check if the error is within an XInclude section and,
556 * if so, attempt to print out the href of the XInclude instead
557 * of the usual "base" (doc->URL) for the node (bug 152623).
559 xmlNodePtr prev = baseptr;
561 while (prev != NULL) {
562 if (prev->prev == NULL)
566 if (prev->type == XML_XINCLUDE_START) {
569 } else if (prev->type == XML_XINCLUDE_END)
574 if (prev->type == XML_XINCLUDE_START) {
575 prev->type = XML_ELEMENT_NODE;
576 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
577 prev->type = XML_XINCLUDE_START;
579 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
583 to->file = (char *) xmlStrdup(baseptr->doc->URL);
584 if ((to->file == NULL) && (node != NULL) && (node->doc != NULL)) {
585 to->file = (char *) xmlStrdup(node->doc->URL);
590 to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
592 to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
594 to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
600 if (to != &xmlLastError)
601 xmlCopyError(to,&xmlLastError);
603 if (schannel != NULL) {
609 * Find the callback channel if channel param is NULL
611 if ((ctxt != NULL) && (channel == NULL) &&
612 (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
613 if (level == XML_ERR_WARNING)
614 channel = ctxt->sax->warning;
616 channel = ctxt->sax->error;
617 data = ctxt->userData;
618 } else if (channel == NULL) {
619 channel = xmlGenericError;
623 data = xmlGenericErrorContext;
629 if ((channel == xmlParserError) ||
630 (channel == xmlParserWarning) ||
631 (channel == xmlParserValidityError) ||
632 (channel == xmlParserValidityWarning))
633 xmlReportError(to, ctxt, str, NULL, NULL);
634 else if ((channel == (xmlGenericErrorFunc) fprintf) ||
635 (channel == xmlGenericErrorDefaultFunc))
636 xmlReportError(to, ctxt, str, channel, data);
638 channel(data, "%s", str);
643 * @domain: where the error comes from
644 * @code: the error code
645 * @node: the context node
646 * @extra: extra informations
648 * Handle an out of memory condition
651 __xmlSimpleError(int domain, int code, xmlNodePtr node,
652 const char *msg, const char *extra)
655 if (code == XML_ERR_NO_MEMORY) {
657 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
658 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
660 "Memory allocation failed : %s\n", extra);
662 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
663 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
664 NULL, NULL, 0, 0, "Memory allocation failed\n");
666 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
667 code, XML_ERR_ERROR, NULL, 0, extra,
668 NULL, NULL, 0, 0, msg, extra);
673 * @ctx: an XML parser context
674 * @msg: the message to display/transmit
675 * @...: extra parameters for the message display
677 * Display and format an error messages, gives file, line, position and
681 xmlParserError(void *ctx, const char *msg, ...)
683 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
684 xmlParserInputPtr input = NULL;
685 xmlParserInputPtr cur = NULL;
690 if ((input != NULL) && (input->filename == NULL) &&
691 (ctxt->inputNr > 1)) {
693 input = ctxt->inputTab[ctxt->inputNr - 2];
695 xmlParserPrintFileInfo(input);
698 xmlGenericError(xmlGenericErrorContext, "error: ");
699 XML_GET_VAR_STR(msg, str);
700 xmlGenericError(xmlGenericErrorContext, "%s", str);
705 xmlParserPrintFileContext(input);
707 xmlParserPrintFileInfo(cur);
708 xmlGenericError(xmlGenericErrorContext, "\n");
709 xmlParserPrintFileContext(cur);
716 * @ctx: an XML parser context
717 * @msg: the message to display/transmit
718 * @...: extra parameters for the message display
720 * Display and format a warning messages, gives file, line, position and
724 xmlParserWarning(void *ctx, const char *msg, ...)
726 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
727 xmlParserInputPtr input = NULL;
728 xmlParserInputPtr cur = NULL;
733 if ((input != NULL) && (input->filename == NULL) &&
734 (ctxt->inputNr > 1)) {
736 input = ctxt->inputTab[ctxt->inputNr - 2];
738 xmlParserPrintFileInfo(input);
741 xmlGenericError(xmlGenericErrorContext, "warning: ");
742 XML_GET_VAR_STR(msg, str);
743 xmlGenericError(xmlGenericErrorContext, "%s", str);
748 xmlParserPrintFileContext(input);
750 xmlParserPrintFileInfo(cur);
751 xmlGenericError(xmlGenericErrorContext, "\n");
752 xmlParserPrintFileContext(cur);
757 /************************************************************************
759 * Handling of validation errors *
761 ************************************************************************/
764 * xmlParserValidityError:
765 * @ctx: an XML parser context
766 * @msg: the message to display/transmit
767 * @...: extra parameters for the message display
769 * Display and format an validity error messages, gives file,
770 * line, position and extra parameters.
773 xmlParserValidityError(void *ctx, const char *msg, ...)
775 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
776 xmlParserInputPtr input = NULL;
778 int len = xmlStrlen((const xmlChar *) msg);
779 static int had_info = 0;
781 if ((len > 1) && (msg[len - 2] != ':')) {
784 if ((input->filename == NULL) && (ctxt->inputNr > 1))
785 input = ctxt->inputTab[ctxt->inputNr - 2];
788 xmlParserPrintFileInfo(input);
791 xmlGenericError(xmlGenericErrorContext, "validity error: ");
797 XML_GET_VAR_STR(msg, str);
798 xmlGenericError(xmlGenericErrorContext, "%s", str);
802 if ((ctxt != NULL) && (input != NULL)) {
803 xmlParserPrintFileContext(input);
808 * xmlParserValidityWarning:
809 * @ctx: an XML parser context
810 * @msg: the message to display/transmit
811 * @...: extra parameters for the message display
813 * Display and format a validity warning messages, gives file, line,
814 * position and extra parameters.
817 xmlParserValidityWarning(void *ctx, const char *msg, ...)
819 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
820 xmlParserInputPtr input = NULL;
822 int len = xmlStrlen((const xmlChar *) msg);
824 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
826 if ((input->filename == NULL) && (ctxt->inputNr > 1))
827 input = ctxt->inputTab[ctxt->inputNr - 2];
829 xmlParserPrintFileInfo(input);
832 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
833 XML_GET_VAR_STR(msg, str);
834 xmlGenericError(xmlGenericErrorContext, "%s", str);
839 xmlParserPrintFileContext(input);
844 /************************************************************************
846 * Extended Error Handling *
848 ************************************************************************/
853 * Get the last global error registered. This is per thread if compiled
854 * with thread support.
856 * Returns NULL if no error occurred or a pointer to the error
859 xmlGetLastError(void)
861 if (xmlLastError.code == XML_ERR_OK)
863 return (&xmlLastError);
868 * @err: pointer to the error.
873 xmlResetError(xmlErrorPtr err)
877 if (err->code == XML_ERR_OK)
879 if (err->message != NULL)
880 xmlFree(err->message);
881 if (err->file != NULL)
883 if (err->str1 != NULL)
885 if (err->str2 != NULL)
887 if (err->str3 != NULL)
889 memset(err, 0, sizeof(xmlError));
890 err->code = XML_ERR_OK;
896 * Cleanup the last global error registered. For parsing error
897 * this does not change the well-formedness result.
900 xmlResetLastError(void)
902 if (xmlLastError.code == XML_ERR_OK)
904 xmlResetError(&xmlLastError);
908 * xmlCtxtGetLastError:
909 * @ctx: an XML parser context
911 * Get the last parsing error registered.
913 * Returns NULL if no error occurred or a pointer to the error
916 xmlCtxtGetLastError(void *ctx)
918 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
922 if (ctxt->lastError.code == XML_ERR_OK)
924 return (&ctxt->lastError);
928 * xmlCtxtResetLastError:
929 * @ctx: an XML parser context
931 * Cleanup the last global error registered. For parsing error
932 * this does not change the well-formedness result.
935 xmlCtxtResetLastError(void *ctx)
937 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
941 ctxt->errNo = XML_ERR_OK;
942 if (ctxt->lastError.code == XML_ERR_OK)
944 xmlResetError(&ctxt->lastError);
949 * @from: a source error
950 * @to: a target error
952 * Save the original error to the new place.
954 * Returns 0 in case of success and -1 in case of error.
957 xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
958 char *message, *file, *str1, *str2, *str3;
960 if ((from == NULL) || (to == NULL))
963 message = (char *) xmlStrdup((xmlChar *) from->message);
964 file = (char *) xmlStrdup ((xmlChar *) from->file);
965 str1 = (char *) xmlStrdup ((xmlChar *) from->str1);
966 str2 = (char *) xmlStrdup ((xmlChar *) from->str2);
967 str3 = (char *) xmlStrdup ((xmlChar *) from->str3);
969 if (to->message != NULL)
970 xmlFree(to->message);
971 if (to->file != NULL)
973 if (to->str1 != NULL)
975 if (to->str2 != NULL)
977 if (to->str3 != NULL)
979 to->domain = from->domain;
980 to->code = from->code;
981 to->level = from->level;
982 to->line = from->line;
983 to->node = from->node;
984 to->int1 = from->int1;
985 to->int2 = from->int2;
986 to->node = from->node;
987 to->ctxt = from->ctxt;
988 to->message = message;
998 #include "elfgcchack.h"