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,
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) return;
183 /* skip backwards over any end-of-lines */
184 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
188 /* search backwards for beginning-of-line (to max buff size) */
189 while ((n++ < (sizeof(content)-1)) && (cur > base) &&
190 (*(cur) != '\n') && (*(cur) != '\r'))
192 if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
193 /* calculate the error position in terms of the current position */
194 col = input->cur - cur;
195 /* search forward for end-of-line (to max buff size) */
198 /* copy selected text to our buffer */
199 while ((*cur != 0) && (*(cur) != '\n') &&
200 (*(cur) != '\r') && (n < sizeof(content)-1)) {
205 /* print out the selected text */
206 channel(data ,"%s\n", content);
207 /* create blank line with problem pointer */
210 /* (leave buffer space for pointer + line terminator) */
211 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
218 channel(data ,"%s\n", content);
222 * xmlParserPrintFileContext:
223 * @input: an xmlParserInputPtr input
225 * Displays current context within the input content for error tracking
228 xmlParserPrintFileContext(xmlParserInputPtr input) {
229 xmlParserPrintFileContextInternal(input, xmlGenericError,
230 xmlGenericErrorContext);
236 * @ctx: the parser context or NULL
237 * @str: the formatted error message
239 * Report an erro with its context, replace the 4 old error/warning
243 xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str,
244 xmlGenericErrorFunc channel, void *data)
250 const xmlChar *name = NULL;
253 xmlParserInputPtr input = NULL;
254 xmlParserInputPtr cur = NULL;
259 if (channel == NULL) {
260 channel = xmlGenericError;
261 data = xmlGenericErrorContext;
266 domain = err->domain;
270 if (code == XML_ERR_OK)
273 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
277 * Maintain the compatibility with the legacy error handling
281 if ((input != NULL) && (input->filename == NULL) &&
282 (ctxt->inputNr > 1)) {
284 input = ctxt->inputTab[ctxt->inputNr - 2];
288 channel(data, "%s:%d: ", input->filename, input->line);
289 else if ((line != 0) && (domain == XML_FROM_PARSER))
290 channel(data, "Entity: line %d: ", input->line);
294 channel(data, "%s:%d: ", file, line);
295 else if ((line != 0) &&
296 ((domain == XML_FROM_PARSER) || (domain == XML_FROM_SCHEMASV)||
297 (domain == XML_FROM_SCHEMASP)||(domain == XML_FROM_DTD) ||
298 (domain == XML_FROM_RELAXNGP)||(domain == XML_FROM_RELAXNGV)))
299 channel(data, "Entity: line %d: ", line);
302 channel(data, "element %s: ", name);
305 case XML_FROM_PARSER:
306 channel(data, "parser ");
308 case XML_FROM_NAMESPACE:
309 channel(data, "namespace ");
313 channel(data, "validity ");
316 channel(data, "HTML parser ");
318 case XML_FROM_MEMORY:
319 channel(data, "memory ");
321 case XML_FROM_OUTPUT:
322 channel(data, "output ");
325 channel(data, "I/O ");
327 case XML_FROM_XINCLUDE:
328 channel(data, "XInclude ");
331 channel(data, "XPath ");
333 case XML_FROM_XPOINTER:
334 channel(data, "parser ");
336 case XML_FROM_REGEXP:
337 channel(data, "regexp ");
339 case XML_FROM_MODULE:
340 channel(data, "module ");
342 case XML_FROM_SCHEMASV:
343 channel(data, "Schemas validity ");
345 case XML_FROM_SCHEMASP:
346 channel(data, "Schemas parser ");
348 case XML_FROM_RELAXNGP:
349 channel(data, "Relax-NG parser ");
351 case XML_FROM_RELAXNGV:
352 channel(data, "Relax-NG validity ");
354 case XML_FROM_CATALOG:
355 channel(data, "Catalog ");
358 channel(data, "C14N ");
361 channel(data, "XSLT ");
364 channel(data, "encoding ");
366 case XML_FROM_SCHEMATRONV:
367 channel(data, "schematron ");
369 case XML_FROM_BUFFER:
370 channel(data, "internal buffer ");
373 channel(data, "URI ");
382 case XML_ERR_WARNING:
383 channel(data, "warning : ");
386 channel(data, "error : ");
389 channel(data, "error : ");
394 len = xmlStrlen((const xmlChar *)str);
395 if ((len > 0) && (str[len - 1] != '\n'))
396 channel(data, "%s\n", str);
398 channel(data, "%s", str);
400 channel(data, "%s\n", "out of memory error");
404 xmlParserPrintFileContextInternal(input, channel, data);
407 channel(data, "%s:%d: \n", cur->filename, cur->line);
408 else if ((line != 0) && (domain == XML_FROM_PARSER))
409 channel(data, "Entity: line %d: \n", cur->line);
410 xmlParserPrintFileContextInternal(cur, channel, data);
413 if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
415 (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
419 channel(data, "%s\n", err->str1);
420 for (i=0;i < err->int1;i++)
424 channel(data, "%s\n", buf);
430 * @schannel: the structured callback channel
431 * @channel: the old callback channel
432 * @data: the callback data
433 * @ctx: the parser context or NULL
434 * @ctx: the parser context or NULL
435 * @domain: the domain for the error
436 * @code: the code for the error
437 * @level: the xmlErrorLevel for the error
438 * @file: the file source of the error (or NULL)
439 * @line: the line of the error or 0 if N/A
440 * @str1: extra string info
441 * @str2: extra string info
442 * @str3: extra string info
443 * @int1: extra int info
444 * @col: column number of the error or 0 if N/A
445 * @msg: the message to display/transmit
446 * @...: extra parameters for the message display
448 * Update the appropriate global or contextual error structure,
449 * then forward the error message down the parser or generic
450 * error callback handler
453 __xmlRaiseError(xmlStructuredErrorFunc schannel,
454 xmlGenericErrorFunc channel, void *data, void *ctx,
455 void *nod, int domain, int code, xmlErrorLevel level,
456 const char *file, int line, const char *str1,
457 const char *str2, const char *str3, int int1, int col,
458 const char *msg, ...)
460 xmlParserCtxtPtr ctxt = NULL;
461 xmlNodePtr node = (xmlNodePtr) nod;
463 xmlParserInputPtr input = NULL;
464 xmlErrorPtr to = &xmlLastError;
465 xmlNodePtr baseptr = NULL;
467 if (code == XML_ERR_OK)
469 if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
471 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
472 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
473 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
474 ctxt = (xmlParserCtxtPtr) ctx;
475 if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
476 (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
477 (ctxt->sax->serror != NULL)) {
478 schannel = ctxt->sax->serror;
479 data = ctxt->userData;
483 * Check if structured error handler set
485 if (schannel == NULL) {
486 schannel = xmlStructuredError;
488 * if user has defined handler, change data ptr to user's choice
490 if (schannel != NULL)
491 data = xmlStructuredErrorContext;
494 * Formatting the message
497 str = (char *) xmlStrdup(BAD_CAST "No error message provided");
499 XML_GET_VAR_STR(msg, str);
503 * specific processing if a parser context is provided
508 if ((input != NULL) && (input->filename == NULL) &&
509 (ctxt->inputNr > 1)) {
510 input = ctxt->inputTab[ctxt->inputNr - 2];
513 file = input->filename;
518 to = &ctxt->lastError;
519 } else if ((node != NULL) && (file == NULL)) {
522 if ((node->doc != NULL) && (node->doc->URL != NULL)) {
524 /* file = (const char *) node->doc->URL; */
527 ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
530 if ((baseptr == NULL) && (node != NULL) &&
531 (node->doc != NULL) && (node->doc->URL != NULL))
534 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
536 if ((line == 0) || (line == 65535))
537 line = xmlGetLineNo(node);
541 * Save the information about the error
549 to->file = (char *) xmlStrdup((const xmlChar *) file);
550 else if (baseptr != NULL) {
551 #ifdef LIBXML_XINCLUDE_ENABLED
553 * We check if the error is within an XInclude section and,
554 * if so, attempt to print out the href of the XInclude instead
555 * of the usual "base" (doc->URL) for the node (bug 152623).
557 xmlNodePtr prev = baseptr;
559 while (prev != NULL) {
560 if (prev->prev == NULL)
564 if (prev->type == XML_XINCLUDE_START) {
567 } else if (prev->type == XML_XINCLUDE_END)
572 if (prev->type == XML_XINCLUDE_START) {
573 prev->type = XML_ELEMENT_NODE;
574 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
575 prev->type = XML_XINCLUDE_START;
577 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
581 to->file = (char *) xmlStrdup(baseptr->doc->URL);
582 if ((to->file == NULL) && (node != NULL) && (node->doc != NULL)) {
583 to->file = (char *) xmlStrdup(node->doc->URL);
588 to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
590 to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
592 to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
598 if (to != &xmlLastError)
599 xmlCopyError(to,&xmlLastError);
601 if (schannel != NULL) {
607 * Find the callback channel if channel param is NULL
609 if ((ctxt != NULL) && (channel == NULL) &&
610 (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
611 if (level == XML_ERR_WARNING)
612 channel = ctxt->sax->warning;
614 channel = ctxt->sax->error;
615 data = ctxt->userData;
616 } else if (channel == NULL) {
617 channel = xmlGenericError;
621 data = xmlGenericErrorContext;
627 if ((channel == xmlParserError) ||
628 (channel == xmlParserWarning) ||
629 (channel == xmlParserValidityError) ||
630 (channel == xmlParserValidityWarning))
631 xmlReportError(to, ctxt, str, NULL, NULL);
632 else if ((channel == (xmlGenericErrorFunc) fprintf) ||
633 (channel == xmlGenericErrorDefaultFunc))
634 xmlReportError(to, ctxt, str, channel, data);
636 channel(data, "%s", str);
641 * @domain: where the error comes from
642 * @code: the error code
643 * @node: the context node
644 * @extra: extra informations
646 * Handle an out of memory condition
649 __xmlSimpleError(int domain, int code, xmlNodePtr node,
650 const char *msg, const char *extra)
653 if (code == XML_ERR_NO_MEMORY) {
655 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
656 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
658 "Memory allocation failed : %s\n", extra);
660 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
661 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
662 NULL, NULL, 0, 0, "Memory allocation failed\n");
664 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
665 code, XML_ERR_ERROR, NULL, 0, extra,
666 NULL, NULL, 0, 0, msg, extra);
671 * @ctx: an XML parser context
672 * @msg: the message to display/transmit
673 * @...: extra parameters for the message display
675 * Display and format an error messages, gives file, line, position and
679 xmlParserError(void *ctx, const char *msg, ...)
681 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
682 xmlParserInputPtr input = NULL;
683 xmlParserInputPtr cur = NULL;
688 if ((input != NULL) && (input->filename == NULL) &&
689 (ctxt->inputNr > 1)) {
691 input = ctxt->inputTab[ctxt->inputNr - 2];
693 xmlParserPrintFileInfo(input);
696 xmlGenericError(xmlGenericErrorContext, "error: ");
697 XML_GET_VAR_STR(msg, str);
698 xmlGenericError(xmlGenericErrorContext, "%s", str);
703 xmlParserPrintFileContext(input);
705 xmlParserPrintFileInfo(cur);
706 xmlGenericError(xmlGenericErrorContext, "\n");
707 xmlParserPrintFileContext(cur);
714 * @ctx: an XML parser context
715 * @msg: the message to display/transmit
716 * @...: extra parameters for the message display
718 * Display and format a warning messages, gives file, line, position and
722 xmlParserWarning(void *ctx, const char *msg, ...)
724 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
725 xmlParserInputPtr input = NULL;
726 xmlParserInputPtr cur = NULL;
731 if ((input != NULL) && (input->filename == NULL) &&
732 (ctxt->inputNr > 1)) {
734 input = ctxt->inputTab[ctxt->inputNr - 2];
736 xmlParserPrintFileInfo(input);
739 xmlGenericError(xmlGenericErrorContext, "warning: ");
740 XML_GET_VAR_STR(msg, str);
741 xmlGenericError(xmlGenericErrorContext, "%s", str);
746 xmlParserPrintFileContext(input);
748 xmlParserPrintFileInfo(cur);
749 xmlGenericError(xmlGenericErrorContext, "\n");
750 xmlParserPrintFileContext(cur);
755 /************************************************************************
757 * Handling of validation errors *
759 ************************************************************************/
762 * xmlParserValidityError:
763 * @ctx: an XML parser context
764 * @msg: the message to display/transmit
765 * @...: extra parameters for the message display
767 * Display and format an validity error messages, gives file,
768 * line, position and extra parameters.
771 xmlParserValidityError(void *ctx, const char *msg, ...)
773 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
774 xmlParserInputPtr input = NULL;
776 int len = xmlStrlen((const xmlChar *) msg);
777 static int had_info = 0;
779 if ((len > 1) && (msg[len - 2] != ':')) {
782 if ((input->filename == NULL) && (ctxt->inputNr > 1))
783 input = ctxt->inputTab[ctxt->inputNr - 2];
786 xmlParserPrintFileInfo(input);
789 xmlGenericError(xmlGenericErrorContext, "validity error: ");
795 XML_GET_VAR_STR(msg, str);
796 xmlGenericError(xmlGenericErrorContext, "%s", str);
800 if ((ctxt != NULL) && (input != NULL)) {
801 xmlParserPrintFileContext(input);
806 * xmlParserValidityWarning:
807 * @ctx: an XML parser context
808 * @msg: the message to display/transmit
809 * @...: extra parameters for the message display
811 * Display and format a validity warning messages, gives file, line,
812 * position and extra parameters.
815 xmlParserValidityWarning(void *ctx, const char *msg, ...)
817 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
818 xmlParserInputPtr input = NULL;
820 int len = xmlStrlen((const xmlChar *) msg);
822 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
824 if ((input->filename == NULL) && (ctxt->inputNr > 1))
825 input = ctxt->inputTab[ctxt->inputNr - 2];
827 xmlParserPrintFileInfo(input);
830 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
831 XML_GET_VAR_STR(msg, str);
832 xmlGenericError(xmlGenericErrorContext, "%s", str);
837 xmlParserPrintFileContext(input);
842 /************************************************************************
844 * Extended Error Handling *
846 ************************************************************************/
851 * Get the last global error registered. This is per thread if compiled
852 * with thread support.
854 * Returns NULL if no error occured or a pointer to the error
857 xmlGetLastError(void)
859 if (xmlLastError.code == XML_ERR_OK)
861 return (&xmlLastError);
866 * @err: pointer to the error.
871 xmlResetError(xmlErrorPtr err)
875 if (err->code == XML_ERR_OK)
877 if (err->message != NULL)
878 xmlFree(err->message);
879 if (err->file != NULL)
881 if (err->str1 != NULL)
883 if (err->str2 != NULL)
885 if (err->str3 != NULL)
887 memset(err, 0, sizeof(xmlError));
888 err->code = XML_ERR_OK;
894 * Cleanup the last global error registered. For parsing error
895 * this does not change the well-formedness result.
898 xmlResetLastError(void)
900 if (xmlLastError.code == XML_ERR_OK)
902 xmlResetError(&xmlLastError);
906 * xmlCtxtGetLastError:
907 * @ctx: an XML parser context
909 * Get the last parsing error registered.
911 * Returns NULL if no error occured or a pointer to the error
914 xmlCtxtGetLastError(void *ctx)
916 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
920 if (ctxt->lastError.code == XML_ERR_OK)
922 return (&ctxt->lastError);
926 * xmlCtxtResetLastError:
927 * @ctx: an XML parser context
929 * Cleanup the last global error registered. For parsing error
930 * this does not change the well-formedness result.
933 xmlCtxtResetLastError(void *ctx)
935 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
939 ctxt->errNo = XML_ERR_OK;
940 if (ctxt->lastError.code == XML_ERR_OK)
942 xmlResetError(&ctxt->lastError);
947 * @from: a source error
948 * @to: a target error
950 * Save the original error to the new place.
952 * Returns 0 in case of success and -1 in case of error.
955 xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
956 char *message, *file, *str1, *str2, *str3;
958 if ((from == NULL) || (to == NULL))
961 message = (char *) xmlStrdup((xmlChar *) from->message);
962 file = (char *) xmlStrdup ((xmlChar *) from->file);
963 str1 = (char *) xmlStrdup ((xmlChar *) from->str1);
964 str2 = (char *) xmlStrdup ((xmlChar *) from->str2);
965 str3 = (char *) xmlStrdup ((xmlChar *) from->str3);
967 if (to->message != NULL)
968 xmlFree(to->message);
969 if (to->file != NULL)
971 if (to->str1 != NULL)
973 if (to->str2 != NULL)
975 if (to->str3 != NULL)
977 to->domain = from->domain;
978 to->code = from->code;
979 to->level = from->level;
980 to->line = from->line;
981 to->node = from->node;
982 to->int1 = from->int1;
983 to->int2 = from->int2;
984 to->node = from->node;
985 to->ctxt = from->ctxt;
986 to->message = message;
996 #include "elfgcchack.h"