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); \
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 xmlGenericErrorContext = 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) && (domain == XML_FROM_PARSER))
296 channel(data, "Entity: line %d: ", line);
299 channel(data, "element %s: ", name);
302 case XML_FROM_PARSER:
303 channel(data, "parser ");
305 case XML_FROM_NAMESPACE:
306 channel(data, "namespace ");
310 channel(data, "validity ");
313 channel(data, "HTML parser ");
315 case XML_FROM_MEMORY:
316 channel(data, "memory ");
318 case XML_FROM_OUTPUT:
319 channel(data, "output ");
322 channel(data, "I/O ");
324 case XML_FROM_XINCLUDE:
325 channel(data, "XInclude ");
328 channel(data, "XPath ");
330 case XML_FROM_XPOINTER:
331 channel(data, "parser ");
333 case XML_FROM_REGEXP:
334 channel(data, "regexp ");
336 case XML_FROM_MODULE:
337 channel(data, "module ");
339 case XML_FROM_SCHEMASV:
340 channel(data, "Schemas validity ");
342 case XML_FROM_SCHEMASP:
343 channel(data, "Schemas parser ");
345 case XML_FROM_RELAXNGP:
346 channel(data, "Relax-NG parser ");
348 case XML_FROM_RELAXNGV:
349 channel(data, "Relax-NG validity ");
351 case XML_FROM_CATALOG:
352 channel(data, "Catalog ");
355 channel(data, "C14N ");
358 channel(data, "XSLT ");
361 channel(data, "encoding ");
370 case XML_ERR_WARNING:
371 channel(data, "warning : ");
374 channel(data, "error : ");
377 channel(data, "error : ");
382 len = xmlStrlen((const xmlChar *)str);
383 if ((len > 0) && (str[len - 1] != '\n'))
384 channel(data, "%s\n", str);
386 channel(data, "%s", str);
388 channel(data, "%s\n", "out of memory error");
392 xmlParserPrintFileContextInternal(input, channel, data);
395 channel(data, "%s:%d: \n", cur->filename, cur->line);
396 else if ((line != 0) && (domain == XML_FROM_PARSER))
397 channel(data, "Entity: line %d: \n", cur->line);
398 xmlParserPrintFileContextInternal(cur, channel, data);
401 if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
403 (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
407 channel(data, "%s\n", err->str1);
408 for (i=0;i < err->int1;i++)
412 channel(data, "%s\n", buf);
418 * @schannel: the structured callback channel
419 * @channel: the old callback channel
420 * @data: the callback data
421 * @ctx: the parser context or NULL
422 * @ctx: the parser context or NULL
423 * @domain: the domain for the error
424 * @code: the code for the error
425 * @level: the xmlErrorLevel for the error
426 * @file: the file source of the error (or NULL)
427 * @line: the line of the error or 0 if N/A
428 * @str1: extra string info
429 * @str2: extra string info
430 * @str3: extra string info
431 * @int1: extra int info
432 * @col: column number of the error or 0 if N/A
433 * @msg: the message to display/transmit
434 * @...: extra parameters for the message display
436 * Update the appropriate global or contextual error structure,
437 * then forward the error message down the parser or generic
438 * error callback handler
441 __xmlRaiseError(xmlStructuredErrorFunc schannel,
442 xmlGenericErrorFunc channel, void *data, void *ctx,
443 void *nod, int domain, int code, xmlErrorLevel level,
444 const char *file, int line, const char *str1,
445 const char *str2, const char *str3, int int1, int col,
446 const char *msg, ...)
448 xmlParserCtxtPtr ctxt = NULL;
449 xmlNodePtr node = (xmlNodePtr) nod;
451 xmlParserInputPtr input = NULL;
452 xmlErrorPtr to = &xmlLastError;
453 xmlNodePtr baseptr = NULL;
455 if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
457 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
458 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
459 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
460 ctxt = (xmlParserCtxtPtr) ctx;
461 if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
462 (ctxt->sax->initialized == XML_SAX2_MAGIC))
463 schannel = ctxt->sax->serror;
466 * Check if structured error handler set
468 if (schannel == NULL) {
469 schannel = xmlStructuredError;
471 * if user has defined handler, change data ptr to user's choice
473 if (schannel != NULL)
474 data = xmlGenericErrorContext;
476 if ((domain == XML_FROM_VALID) &&
477 ((channel == xmlParserValidityError) ||
478 (channel == xmlParserValidityWarning))) {
479 ctxt = (xmlParserCtxtPtr) ctx;
480 if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
481 (ctxt->sax->initialized == XML_SAX2_MAGIC))
482 schannel = ctxt->sax->serror;
484 if (code == XML_ERR_OK)
487 * Formatting the message
490 str = (char *) xmlStrdup(BAD_CAST "No error message provided");
492 XML_GET_VAR_STR(msg, str);
496 * specific processing if a parser context is provided
501 if ((input != NULL) && (input->filename == NULL) &&
502 (ctxt->inputNr > 1)) {
503 input = ctxt->inputTab[ctxt->inputNr - 2];
506 file = input->filename;
511 to = &ctxt->lastError;
512 } else if ((node != NULL) && (file == NULL)) {
515 if ((node->doc != NULL) && (node->doc->URL != NULL)) {
517 /* file = (const char *) node->doc->URL; */
520 ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
523 if ((baseptr == NULL) && (node != NULL) &&
524 (node->doc != NULL) && (node->doc->URL != NULL))
527 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
532 * Save the information about the error
540 to->file = (char *) xmlStrdup((const xmlChar *) file);
541 else if (baseptr != NULL) {
542 #ifdef LIBXML_XINCLUDE_ENABLED
544 * We check if the error is within an XInclude section and,
545 * if so, attempt to print out the href of the XInclude instead
546 * of the usual "base" (doc->URL) for the node (bug 152623).
548 xmlNodePtr prev = baseptr;
550 while (prev != NULL) {
551 if (prev->prev == NULL)
555 if (prev->type == XML_XINCLUDE_START) {
558 } else if (prev->type == XML_XINCLUDE_END)
563 if (prev->type == XML_XINCLUDE_START) {
564 prev->type = XML_ELEMENT_NODE;
565 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
566 prev->type = XML_XINCLUDE_START;
568 to->file = (char *) xmlGetProp(prev, BAD_CAST "href");
572 to->file = (char *) xmlStrdup(baseptr->doc->URL);
573 if ((to->file == NULL) && (node != NULL) && (node->doc != NULL)) {
574 to->file = (char *) xmlStrdup(node->doc->URL);
580 to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
582 to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
584 to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
590 if (to != &xmlLastError)
591 xmlCopyError(to,&xmlLastError);
594 * Find the callback channel if channel param is NULL
596 if ((ctxt != NULL) && (channel == NULL) && (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
597 if (level == XML_ERR_WARNING)
598 channel = ctxt->sax->warning;
600 channel = ctxt->sax->error;
601 data = ctxt->userData;
602 } else if (channel == NULL) {
603 if (xmlStructuredError != NULL)
604 schannel = xmlStructuredError;
606 channel = xmlGenericError;
608 data = xmlGenericErrorContext;
611 if (schannel != NULL) {
618 if ((channel == xmlParserError) ||
619 (channel == xmlParserWarning) ||
620 (channel == xmlParserValidityError) ||
621 (channel == xmlParserValidityWarning))
622 xmlReportError(to, ctxt, str, NULL, NULL);
623 else if ((channel == (xmlGenericErrorFunc) fprintf) ||
624 (channel == xmlGenericErrorDefaultFunc))
625 xmlReportError(to, ctxt, str, channel, data);
627 channel(data, "%s", str);
632 * @domain: where the error comes from
633 * @code: the error code
634 * @node: the context node
635 * @extra: extra informations
637 * Handle an out of memory condition
640 __xmlSimpleError(int domain, int code, xmlNodePtr node,
641 const char *msg, const char *extra)
644 if (code == XML_ERR_NO_MEMORY) {
646 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
647 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
649 "Memory allocation failed : %s\n", extra);
651 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
652 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
653 NULL, NULL, 0, 0, "Memory allocation failed\n");
655 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
656 code, XML_ERR_ERROR, NULL, 0, extra,
657 NULL, NULL, 0, 0, msg, extra);
662 * @ctx: an XML parser context
663 * @msg: the message to display/transmit
664 * @...: extra parameters for the message display
666 * Display and format an error messages, gives file, line, position and
670 xmlParserError(void *ctx, const char *msg, ...)
672 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
673 xmlParserInputPtr input = NULL;
674 xmlParserInputPtr cur = NULL;
679 if ((input != NULL) && (input->filename == NULL) &&
680 (ctxt->inputNr > 1)) {
682 input = ctxt->inputTab[ctxt->inputNr - 2];
684 xmlParserPrintFileInfo(input);
687 xmlGenericError(xmlGenericErrorContext, "error: ");
688 XML_GET_VAR_STR(msg, str);
689 xmlGenericError(xmlGenericErrorContext, "%s", str);
694 xmlParserPrintFileContext(input);
696 xmlParserPrintFileInfo(cur);
697 xmlGenericError(xmlGenericErrorContext, "\n");
698 xmlParserPrintFileContext(cur);
705 * @ctx: an XML parser context
706 * @msg: the message to display/transmit
707 * @...: extra parameters for the message display
709 * Display and format a warning messages, gives file, line, position and
713 xmlParserWarning(void *ctx, const char *msg, ...)
715 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
716 xmlParserInputPtr input = NULL;
717 xmlParserInputPtr cur = NULL;
722 if ((input != NULL) && (input->filename == NULL) &&
723 (ctxt->inputNr > 1)) {
725 input = ctxt->inputTab[ctxt->inputNr - 2];
727 xmlParserPrintFileInfo(input);
730 xmlGenericError(xmlGenericErrorContext, "warning: ");
731 XML_GET_VAR_STR(msg, str);
732 xmlGenericError(xmlGenericErrorContext, "%s", str);
737 xmlParserPrintFileContext(input);
739 xmlParserPrintFileInfo(cur);
740 xmlGenericError(xmlGenericErrorContext, "\n");
741 xmlParserPrintFileContext(cur);
746 /************************************************************************
748 * Handling of validation errors *
750 ************************************************************************/
753 * xmlParserValidityError:
754 * @ctx: an XML parser context
755 * @msg: the message to display/transmit
756 * @...: extra parameters for the message display
758 * Display and format an validity error messages, gives file,
759 * line, position and extra parameters.
762 xmlParserValidityError(void *ctx, const char *msg, ...)
764 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
765 xmlParserInputPtr input = NULL;
767 int len = xmlStrlen((const xmlChar *) msg);
768 static int had_info = 0;
770 if ((len > 1) && (msg[len - 2] != ':')) {
773 if ((input->filename == NULL) && (ctxt->inputNr > 1))
774 input = ctxt->inputTab[ctxt->inputNr - 2];
777 xmlParserPrintFileInfo(input);
780 xmlGenericError(xmlGenericErrorContext, "validity error: ");
786 XML_GET_VAR_STR(msg, str);
787 xmlGenericError(xmlGenericErrorContext, "%s", str);
791 if ((ctxt != NULL) && (input != NULL)) {
792 xmlParserPrintFileContext(input);
797 * xmlParserValidityWarning:
798 * @ctx: an XML parser context
799 * @msg: the message to display/transmit
800 * @...: extra parameters for the message display
802 * Display and format a validity warning messages, gives file, line,
803 * position and extra parameters.
806 xmlParserValidityWarning(void *ctx, const char *msg, ...)
808 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
809 xmlParserInputPtr input = NULL;
811 int len = xmlStrlen((const xmlChar *) msg);
813 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
815 if ((input->filename == NULL) && (ctxt->inputNr > 1))
816 input = ctxt->inputTab[ctxt->inputNr - 2];
818 xmlParserPrintFileInfo(input);
821 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
822 XML_GET_VAR_STR(msg, str);
823 xmlGenericError(xmlGenericErrorContext, "%s", str);
828 xmlParserPrintFileContext(input);
833 /************************************************************************
835 * Extended Error Handling *
837 ************************************************************************/
842 * Get the last global error registered. This is per thread if compiled
843 * with thread support.
845 * Returns NULL if no error occured or a pointer to the error
848 xmlGetLastError(void)
850 if (xmlLastError.code == XML_ERR_OK)
852 return (&xmlLastError);
857 * @err: pointer to the error.
862 xmlResetError(xmlErrorPtr err)
866 if (err->code == XML_ERR_OK)
868 if (err->message != NULL)
869 xmlFree(err->message);
870 if (err->file != NULL)
872 if (err->str1 != NULL)
874 if (err->str2 != NULL)
876 if (err->str3 != NULL)
878 memset(err, 0, sizeof(xmlError));
879 err->code = XML_ERR_OK;
885 * Cleanup the last global error registered. For parsing error
886 * this does not change the well-formedness result.
889 xmlResetLastError(void)
891 if (xmlLastError.code == XML_ERR_OK)
893 xmlResetError(&xmlLastError);
897 * xmlCtxtGetLastError:
898 * @ctx: an XML parser context
900 * Get the last parsing error registered.
902 * Returns NULL if no error occured or a pointer to the error
905 xmlCtxtGetLastError(void *ctx)
907 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
911 if (ctxt->lastError.code == XML_ERR_OK)
913 return (&ctxt->lastError);
917 * xmlCtxtResetLastError:
918 * @ctx: an XML parser context
920 * Cleanup the last global error registered. For parsing error
921 * this does not change the well-formedness result.
924 xmlCtxtResetLastError(void *ctx)
926 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
930 if (ctxt->lastError.code == XML_ERR_OK)
932 xmlResetError(&ctxt->lastError);
937 * @from: a source error
938 * @to: a target error
940 * Save the original error to the new place.
942 * Returns 0 in case of success and -1 in case of error.
945 xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
946 char *message, *file, *str1, *str2, *str3;
948 if ((from == NULL) || (to == NULL))
951 message = (char *) xmlStrdup((xmlChar *) from->message);
952 file = (char *) xmlStrdup ((xmlChar *) from->file);
953 str1 = (char *) xmlStrdup ((xmlChar *) from->str1);
954 str2 = (char *) xmlStrdup ((xmlChar *) from->str2);
955 str3 = (char *) xmlStrdup ((xmlChar *) from->str3);
957 if (to->message != NULL)
958 xmlFree(to->message);
959 if (to->file != NULL)
961 if (to->str1 != NULL)
963 if (to->str2 != NULL)
965 if (to->str3 != NULL)
967 to->domain = from->domain;
968 to->code = from->code;
969 to->level = from->level;
970 to->line = from->line;
971 to->node = from->node;
972 to->int1 = from->int1;
973 to->int2 = from->int2;
974 to->node = from->node;
975 to->ctxt = from->ctxt;
976 to->message = message;
986 #include "elfgcchack.h"