mscms: Fix double free on error path in EnumColorProfilesA (scan-build).
[wine.git] / libs / xml2 / error.c
blob048dd152f0d415c1eb94c80ac7377c8df7284402
1 /*
2 * error.c: module displaying/handling XML parser errors
4 * See Copyright for the status of this software.
6 * Daniel Veillard <daniel@veillard.com>
7 */
9 #define IN_LIBXML
10 #include "libxml.h"
12 #include <string.h>
13 #include <stdarg.h>
14 #include <libxml/parser.h>
15 #include <libxml/xmlerror.h>
16 #include <libxml/xmlmemory.h>
17 #include <libxml/globals.h>
19 #include "private/error.h"
21 #define XML_MAX_ERRORS 100
23 #define XML_GET_VAR_STR(msg, str) { \
24 int size, prev_size = -1; \
25 int chars; \
26 char *larger; \
27 va_list ap; \
29 str = (char *) xmlMalloc(150); \
30 if (str != NULL) { \
32 size = 150; \
34 while (size < 64000) { \
35 va_start(ap, msg); \
36 chars = vsnprintf(str, size, msg, ap); \
37 va_end(ap); \
38 if ((chars > -1) && (chars < size)) { \
39 if (prev_size == chars) { \
40 break; \
41 } else { \
42 prev_size = chars; \
43 } \
44 } \
45 if (chars > -1) \
46 size += chars + 1; \
47 else \
48 size += 100; \
49 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
50 break; \
51 } \
52 str = larger; \
53 }} \
56 /************************************************************************
57 * *
58 * Handling of out of context errors *
59 * *
60 ************************************************************************/
62 /**
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.
70 void XMLCDECL
71 xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
72 va_list args;
74 if (xmlGenericErrorContext == NULL)
75 xmlGenericErrorContext = (void *) stderr;
77 va_start(args, msg);
78 vfprintf((FILE *)xmlGenericErrorContext, msg, args);
79 va_end(args);
82 /**
83 * initGenericErrorDefaultFunc:
84 * @handler: the handler
86 * DEPRECATED: Use xmlSetGenericErrorFunc.
88 * Set or reset (if NULL) the default handler for generic errors
89 * to the builtin error function.
91 void
92 initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
94 if (handler == NULL)
95 xmlGenericError = xmlGenericErrorDefaultFunc;
96 else
97 xmlGenericError = (*handler);
101 * xmlSetGenericErrorFunc:
102 * @ctx: the new error handling context
103 * @handler: the new handler function
105 * Function to reset the handler and the error context for out of
106 * context error messages.
107 * This simply means that @handler will be called for subsequent
108 * error messages while not parsing nor validating. And @ctx will
109 * be passed as first argument to @handler
110 * One can simply force messages to be emitted to another FILE * than
111 * stderr by setting @ctx to this file handle and @handler to NULL.
112 * For multi-threaded applications, this must be set separately for each thread.
114 void
115 xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
116 xmlGenericErrorContext = ctx;
117 if (handler != NULL)
118 xmlGenericError = handler;
119 else
120 xmlGenericError = xmlGenericErrorDefaultFunc;
124 * xmlSetStructuredErrorFunc:
125 * @ctx: the new error handling context
126 * @handler: the new handler function
128 * Function to reset the handler and the error context for out of
129 * context structured error messages.
130 * This simply means that @handler will be called for subsequent
131 * error messages while not parsing nor validating. And @ctx will
132 * be passed as first argument to @handler
133 * For multi-threaded applications, this must be set separately for each thread.
135 void
136 xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
137 xmlStructuredErrorContext = ctx;
138 xmlStructuredError = handler;
141 /************************************************************************
143 * Handling of parsing errors *
145 ************************************************************************/
148 * xmlParserPrintFileInfo:
149 * @input: an xmlParserInputPtr input
151 * Displays the associated file and line information for the current input
154 void
155 xmlParserPrintFileInfo(xmlParserInputPtr input) {
156 if (input != NULL) {
157 if (input->filename)
158 xmlGenericError(xmlGenericErrorContext,
159 "%s:%d: ", input->filename,
160 input->line);
161 else
162 xmlGenericError(xmlGenericErrorContext,
163 "Entity: line %d: ", input->line);
168 * xmlParserPrintFileContextInternal:
169 * @input: an xmlParserInputPtr input
171 * Displays current context within the input content for error tracking
174 static void
175 xmlParserPrintFileContextInternal(xmlParserInputPtr input ,
176 xmlGenericErrorFunc channel, void *data ) {
177 const xmlChar *cur, *base, *start;
178 unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
179 xmlChar content[81]; /* space for 80 chars + line terminator */
180 xmlChar *ctnt;
182 if ((input == NULL) || (input->cur == NULL))
183 return;
185 cur = input->cur;
186 base = input->base;
187 /* skip backwards over any end-of-lines */
188 while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
189 cur--;
191 n = 0;
192 /* search backwards for beginning-of-line (to max buff size) */
193 while ((n < sizeof(content) - 1) && (cur > base) &&
194 (*cur != '\n') && (*cur != '\r')) {
195 cur--;
196 n++;
198 if ((n > 0) && ((*cur == '\n') || (*cur == '\r'))) {
199 cur++;
200 } else {
201 /* skip over continuation bytes */
202 while ((cur < input->cur) && ((*cur & 0xC0) == 0x80))
203 cur++;
205 /* calculate the error position in terms of the current position */
206 col = input->cur - cur;
207 /* search forward for end-of-line (to max buff size) */
208 n = 0;
209 start = cur;
210 /* copy selected text to our buffer */
211 while ((*cur != 0) && (*(cur) != '\n') && (*(cur) != '\r')) {
212 int len = input->end - cur;
213 int c = xmlGetUTF8Char(cur, &len);
215 if ((c < 0) || (n + len > sizeof(content)-1))
216 break;
217 cur += len;
218 n += len;
220 memcpy(content, start, n);
221 content[n] = 0;
222 /* print out the selected text */
223 channel(data ,"%s\n", content);
224 /* create blank line with problem pointer */
225 n = 0;
226 ctnt = content;
227 /* (leave buffer space for pointer + line terminator) */
228 while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
229 if (*(ctnt) != '\t')
230 *(ctnt) = ' ';
231 ctnt++;
233 *ctnt++ = '^';
234 *ctnt = 0;
235 channel(data ,"%s\n", content);
239 * xmlParserPrintFileContext:
240 * @input: an xmlParserInputPtr input
242 * Displays current context within the input content for error tracking
244 void
245 xmlParserPrintFileContext(xmlParserInputPtr input) {
246 xmlParserPrintFileContextInternal(input, xmlGenericError,
247 xmlGenericErrorContext);
251 * xmlReportError:
252 * @err: the error
253 * @ctx: the parser context or NULL
254 * @str: the formatted error message
256 * Report an error with its context, replace the 4 old error/warning
257 * routines.
259 static void
260 xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str,
261 xmlGenericErrorFunc channel, void *data)
263 char *file = NULL;
264 int line = 0;
265 int code = -1;
266 int domain;
267 const xmlChar *name = NULL;
268 xmlNodePtr node;
269 xmlErrorLevel level;
270 xmlParserInputPtr input = NULL;
271 xmlParserInputPtr cur = NULL;
273 if (err == NULL)
274 return;
276 if (channel == NULL) {
277 channel = xmlGenericError;
278 data = xmlGenericErrorContext;
280 file = err->file;
281 line = err->line;
282 code = err->code;
283 domain = err->domain;
284 level = err->level;
285 node = err->node;
287 if (code == XML_ERR_OK)
288 return;
290 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
291 name = node->name;
294 * Maintain the compatibility with the legacy error handling
296 if (ctxt != NULL) {
297 input = ctxt->input;
298 if ((input != NULL) && (input->filename == NULL) &&
299 (ctxt->inputNr > 1)) {
300 cur = input;
301 input = ctxt->inputTab[ctxt->inputNr - 2];
303 if (input != NULL) {
304 if (input->filename)
305 channel(data, "%s:%d: ", input->filename, input->line);
306 else if ((line != 0) && (domain == XML_FROM_PARSER))
307 channel(data, "Entity: line %d: ", input->line);
309 } else {
310 if (file != NULL)
311 channel(data, "%s:%d: ", file, line);
312 else if ((line != 0) &&
313 ((domain == XML_FROM_PARSER) || (domain == XML_FROM_SCHEMASV)||
314 (domain == XML_FROM_SCHEMASP)||(domain == XML_FROM_DTD) ||
315 (domain == XML_FROM_RELAXNGP)||(domain == XML_FROM_RELAXNGV)))
316 channel(data, "Entity: line %d: ", line);
318 if (name != NULL) {
319 channel(data, "element %s: ", name);
321 switch (domain) {
322 case XML_FROM_PARSER:
323 channel(data, "parser ");
324 break;
325 case XML_FROM_NAMESPACE:
326 channel(data, "namespace ");
327 break;
328 case XML_FROM_DTD:
329 case XML_FROM_VALID:
330 channel(data, "validity ");
331 break;
332 case XML_FROM_HTML:
333 channel(data, "HTML parser ");
334 break;
335 case XML_FROM_MEMORY:
336 channel(data, "memory ");
337 break;
338 case XML_FROM_OUTPUT:
339 channel(data, "output ");
340 break;
341 case XML_FROM_IO:
342 channel(data, "I/O ");
343 break;
344 case XML_FROM_XINCLUDE:
345 channel(data, "XInclude ");
346 break;
347 case XML_FROM_XPATH:
348 channel(data, "XPath ");
349 break;
350 case XML_FROM_XPOINTER:
351 channel(data, "parser ");
352 break;
353 case XML_FROM_REGEXP:
354 channel(data, "regexp ");
355 break;
356 case XML_FROM_MODULE:
357 channel(data, "module ");
358 break;
359 case XML_FROM_SCHEMASV:
360 channel(data, "Schemas validity ");
361 break;
362 case XML_FROM_SCHEMASP:
363 channel(data, "Schemas parser ");
364 break;
365 case XML_FROM_RELAXNGP:
366 channel(data, "Relax-NG parser ");
367 break;
368 case XML_FROM_RELAXNGV:
369 channel(data, "Relax-NG validity ");
370 break;
371 case XML_FROM_CATALOG:
372 channel(data, "Catalog ");
373 break;
374 case XML_FROM_C14N:
375 channel(data, "C14N ");
376 break;
377 case XML_FROM_XSLT:
378 channel(data, "XSLT ");
379 break;
380 case XML_FROM_I18N:
381 channel(data, "encoding ");
382 break;
383 case XML_FROM_SCHEMATRONV:
384 channel(data, "schematron ");
385 break;
386 case XML_FROM_BUFFER:
387 channel(data, "internal buffer ");
388 break;
389 case XML_FROM_URI:
390 channel(data, "URI ");
391 break;
392 default:
393 break;
395 switch (level) {
396 case XML_ERR_NONE:
397 channel(data, ": ");
398 break;
399 case XML_ERR_WARNING:
400 channel(data, "warning : ");
401 break;
402 case XML_ERR_ERROR:
403 channel(data, "error : ");
404 break;
405 case XML_ERR_FATAL:
406 channel(data, "error : ");
407 break;
409 if (str != NULL) {
410 int len;
411 len = xmlStrlen((const xmlChar *)str);
412 if ((len > 0) && (str[len - 1] != '\n'))
413 channel(data, "%s\n", str);
414 else
415 channel(data, "%s", str);
416 } else {
417 channel(data, "%s\n", "out of memory error");
420 if (ctxt != NULL) {
421 xmlParserPrintFileContextInternal(input, channel, data);
422 if (cur != NULL) {
423 if (cur->filename)
424 channel(data, "%s:%d: \n", cur->filename, cur->line);
425 else if ((line != 0) && (domain == XML_FROM_PARSER))
426 channel(data, "Entity: line %d: \n", cur->line);
427 xmlParserPrintFileContextInternal(cur, channel, data);
430 if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
431 (err->int1 < 100) &&
432 (err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
433 xmlChar buf[150];
434 int i;
436 channel(data, "%s\n", err->str1);
437 for (i=0;i < err->int1;i++)
438 buf[i] = ' ';
439 buf[i++] = '^';
440 buf[i] = 0;
441 channel(data, "%s\n", buf);
446 * __xmlRaiseError:
447 * @schannel: the structured callback channel
448 * @channel: the old callback channel
449 * @data: the callback data
450 * @ctx: the parser context or NULL
451 * @ctx: the parser context or NULL
452 * @domain: the domain for the error
453 * @code: the code for the error
454 * @level: the xmlErrorLevel for the error
455 * @file: the file source of the error (or NULL)
456 * @line: the line of the error or 0 if N/A
457 * @str1: extra string info
458 * @str2: extra string info
459 * @str3: extra string info
460 * @int1: extra int info
461 * @col: column number of the error or 0 if N/A
462 * @msg: the message to display/transmit
463 * @...: extra parameters for the message display
465 * Update the appropriate global or contextual error structure,
466 * then forward the error message down the parser or generic
467 * error callback handler
469 void XMLCDECL
470 __xmlRaiseError(xmlStructuredErrorFunc schannel,
471 xmlGenericErrorFunc channel, void *data, void *ctx,
472 void *nod, int domain, int code, xmlErrorLevel level,
473 const char *file, int line, const char *str1,
474 const char *str2, const char *str3, int int1, int col,
475 const char *msg, ...)
477 xmlParserCtxtPtr ctxt = NULL;
478 xmlNodePtr node = (xmlNodePtr) nod;
479 char *str = NULL;
480 xmlParserInputPtr input = NULL;
481 xmlErrorPtr to = &xmlLastError;
482 xmlNodePtr baseptr = NULL;
484 if (code == XML_ERR_OK)
485 return;
486 if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
487 return;
488 if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
489 (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
490 (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) {
491 ctxt = (xmlParserCtxtPtr) ctx;
493 if (ctxt != NULL) {
494 if (level == XML_ERR_WARNING) {
495 if (ctxt->nbWarnings >= XML_MAX_ERRORS)
496 return;
497 ctxt->nbWarnings += 1;
498 } else {
499 if (ctxt->nbErrors >= XML_MAX_ERRORS)
500 return;
501 ctxt->nbErrors += 1;
504 if ((schannel == NULL) && (ctxt->sax != NULL) &&
505 (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
506 (ctxt->sax->serror != NULL)) {
507 schannel = ctxt->sax->serror;
508 data = ctxt->userData;
513 * Check if structured error handler set
515 if (schannel == NULL) {
516 schannel = xmlStructuredError;
518 * if user has defined handler, change data ptr to user's choice
520 if (schannel != NULL)
521 data = xmlStructuredErrorContext;
524 * Formatting the message
526 if (msg == NULL) {
527 str = (char *) xmlStrdup(BAD_CAST "No error message provided");
528 } else {
529 XML_GET_VAR_STR(msg, str);
533 * specific processing if a parser context is provided
535 if (ctxt != NULL) {
536 if (file == NULL) {
537 input = ctxt->input;
538 if ((input != NULL) && (input->filename == NULL) &&
539 (ctxt->inputNr > 1)) {
540 input = ctxt->inputTab[ctxt->inputNr - 2];
542 if (input != NULL) {
543 file = input->filename;
544 line = input->line;
545 col = input->col;
548 to = &ctxt->lastError;
549 } else if ((node != NULL) && (file == NULL)) {
550 int i;
552 if ((node->doc != NULL) && (node->doc->URL != NULL)) {
553 baseptr = node;
554 /* file = (const char *) node->doc->URL; */
556 for (i = 0;
557 ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
558 i++)
559 node = node->parent;
560 if ((baseptr == NULL) && (node != NULL) &&
561 (node->doc != NULL) && (node->doc->URL != NULL))
562 baseptr = node;
564 if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
565 line = node->line;
566 if ((line == 0) || (line == 65535))
567 line = xmlGetLineNo(node);
571 * Save the information about the error
573 xmlResetError(to);
574 to->domain = domain;
575 to->code = code;
576 to->message = str;
577 to->level = level;
578 if (file != NULL)
579 to->file = (char *) xmlStrdup((const xmlChar *) file);
580 else if (baseptr != NULL) {
581 #ifdef LIBXML_XINCLUDE_ENABLED
583 * We check if the error is within an XInclude section and,
584 * if so, attempt to print out the href of the XInclude instead
585 * of the usual "base" (doc->URL) for the node (bug 152623).
587 xmlNodePtr prev = baseptr;
588 char *href = NULL;
589 int inclcount = 0;
590 while (prev != NULL) {
591 if (prev->prev == NULL)
592 prev = prev->parent;
593 else {
594 prev = prev->prev;
595 if (prev->type == XML_XINCLUDE_START) {
596 if (inclcount > 0) {
597 --inclcount;
598 } else {
599 href = (char *) xmlGetProp(prev, BAD_CAST "href");
600 if (href != NULL)
601 break;
603 } else if (prev->type == XML_XINCLUDE_END)
604 inclcount++;
607 if (href != NULL)
608 to->file = href;
609 else
610 #endif
611 to->file = (char *) xmlStrdup(baseptr->doc->URL);
612 if ((to->file == NULL) && (node != NULL) && (node->doc != NULL)) {
613 to->file = (char *) xmlStrdup(node->doc->URL);
616 to->line = line;
617 if (str1 != NULL)
618 to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
619 if (str2 != NULL)
620 to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
621 if (str3 != NULL)
622 to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
623 to->int1 = int1;
624 to->int2 = col;
625 to->node = node;
626 to->ctxt = ctx;
628 if (to != &xmlLastError)
629 xmlCopyError(to,&xmlLastError);
631 if (schannel != NULL) {
632 schannel(data, to);
633 return;
637 * Find the callback channel if channel param is NULL
639 if ((ctxt != NULL) && (channel == NULL) &&
640 (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
641 if (level == XML_ERR_WARNING)
642 channel = ctxt->sax->warning;
643 else
644 channel = ctxt->sax->error;
645 data = ctxt->userData;
646 } else if (channel == NULL) {
647 channel = xmlGenericError;
648 if (ctxt != NULL) {
649 data = ctxt;
650 } else {
651 data = xmlGenericErrorContext;
654 if (channel == NULL)
655 return;
657 if ((channel == xmlParserError) ||
658 (channel == xmlParserWarning) ||
659 (channel == xmlParserValidityError) ||
660 (channel == xmlParserValidityWarning))
661 xmlReportError(to, ctxt, str, NULL, NULL);
662 else if (((void(*)(void)) channel == (void(*)(void)) fprintf) ||
663 (channel == xmlGenericErrorDefaultFunc))
664 xmlReportError(to, ctxt, str, channel, data);
665 else
666 channel(data, "%s", str);
670 * __xmlSimpleError:
671 * @domain: where the error comes from
672 * @code: the error code
673 * @node: the context node
674 * @extra: extra information
676 * Handle an out of memory condition
678 void
679 __xmlSimpleError(int domain, int code, xmlNodePtr node,
680 const char *msg, const char *extra)
683 if (code == XML_ERR_NO_MEMORY) {
684 if (extra)
685 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
686 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
687 NULL, NULL, 0, 0,
688 "Memory allocation failed : %s\n", extra);
689 else
690 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
691 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
692 NULL, NULL, 0, 0, "Memory allocation failed\n");
693 } else {
694 __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
695 code, XML_ERR_ERROR, NULL, 0, extra,
696 NULL, NULL, 0, 0, msg, extra);
700 * xmlParserError:
701 * @ctx: an XML parser context
702 * @msg: the message to display/transmit
703 * @...: extra parameters for the message display
705 * Display and format an error messages, gives file, line, position and
706 * extra parameters.
708 void XMLCDECL
709 xmlParserError(void *ctx, const char *msg, ...)
711 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
712 xmlParserInputPtr input = NULL;
713 xmlParserInputPtr cur = NULL;
714 char * str;
716 if (ctxt != NULL) {
717 input = ctxt->input;
718 if ((input != NULL) && (input->filename == NULL) &&
719 (ctxt->inputNr > 1)) {
720 cur = input;
721 input = ctxt->inputTab[ctxt->inputNr - 2];
723 xmlParserPrintFileInfo(input);
726 xmlGenericError(xmlGenericErrorContext, "error: ");
727 XML_GET_VAR_STR(msg, str);
728 xmlGenericError(xmlGenericErrorContext, "%s", str);
729 if (str != NULL)
730 xmlFree(str);
732 if (ctxt != NULL) {
733 xmlParserPrintFileContext(input);
734 if (cur != NULL) {
735 xmlParserPrintFileInfo(cur);
736 xmlGenericError(xmlGenericErrorContext, "\n");
737 xmlParserPrintFileContext(cur);
743 * xmlParserWarning:
744 * @ctx: an XML parser context
745 * @msg: the message to display/transmit
746 * @...: extra parameters for the message display
748 * Display and format a warning messages, gives file, line, position and
749 * extra parameters.
751 void XMLCDECL
752 xmlParserWarning(void *ctx, const char *msg, ...)
754 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
755 xmlParserInputPtr input = NULL;
756 xmlParserInputPtr cur = NULL;
757 char * str;
759 if (ctxt != NULL) {
760 input = ctxt->input;
761 if ((input != NULL) && (input->filename == NULL) &&
762 (ctxt->inputNr > 1)) {
763 cur = input;
764 input = ctxt->inputTab[ctxt->inputNr - 2];
766 xmlParserPrintFileInfo(input);
769 xmlGenericError(xmlGenericErrorContext, "warning: ");
770 XML_GET_VAR_STR(msg, str);
771 xmlGenericError(xmlGenericErrorContext, "%s", str);
772 if (str != NULL)
773 xmlFree(str);
775 if (ctxt != NULL) {
776 xmlParserPrintFileContext(input);
777 if (cur != NULL) {
778 xmlParserPrintFileInfo(cur);
779 xmlGenericError(xmlGenericErrorContext, "\n");
780 xmlParserPrintFileContext(cur);
785 /************************************************************************
787 * Handling of validation errors *
789 ************************************************************************/
792 * xmlParserValidityError:
793 * @ctx: an XML parser context
794 * @msg: the message to display/transmit
795 * @...: extra parameters for the message display
797 * Display and format an validity error messages, gives file,
798 * line, position and extra parameters.
800 void XMLCDECL
801 xmlParserValidityError(void *ctx, const char *msg, ...)
803 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
804 xmlParserInputPtr input = NULL;
805 char * str;
806 int len = xmlStrlen((const xmlChar *) msg);
807 static int had_info = 0;
809 if ((len > 1) && (msg[len - 2] != ':')) {
810 if (ctxt != NULL) {
811 input = ctxt->input;
812 if ((input->filename == NULL) && (ctxt->inputNr > 1))
813 input = ctxt->inputTab[ctxt->inputNr - 2];
815 if (had_info == 0) {
816 xmlParserPrintFileInfo(input);
819 xmlGenericError(xmlGenericErrorContext, "validity error: ");
820 had_info = 0;
821 } else {
822 had_info = 1;
825 XML_GET_VAR_STR(msg, str);
826 xmlGenericError(xmlGenericErrorContext, "%s", str);
827 if (str != NULL)
828 xmlFree(str);
830 if ((ctxt != NULL) && (input != NULL)) {
831 xmlParserPrintFileContext(input);
836 * xmlParserValidityWarning:
837 * @ctx: an XML parser context
838 * @msg: the message to display/transmit
839 * @...: extra parameters for the message display
841 * Display and format a validity warning messages, gives file, line,
842 * position and extra parameters.
844 void XMLCDECL
845 xmlParserValidityWarning(void *ctx, const char *msg, ...)
847 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
848 xmlParserInputPtr input = NULL;
849 char * str;
850 int len = xmlStrlen((const xmlChar *) msg);
852 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
853 input = ctxt->input;
854 if ((input->filename == NULL) && (ctxt->inputNr > 1))
855 input = ctxt->inputTab[ctxt->inputNr - 2];
857 xmlParserPrintFileInfo(input);
860 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
861 XML_GET_VAR_STR(msg, str);
862 xmlGenericError(xmlGenericErrorContext, "%s", str);
863 if (str != NULL)
864 xmlFree(str);
866 if (ctxt != NULL) {
867 xmlParserPrintFileContext(input);
872 /************************************************************************
874 * Extended Error Handling *
876 ************************************************************************/
879 * xmlGetLastError:
881 * Get the last global error registered. This is per thread if compiled
882 * with thread support.
884 * Returns NULL if no error occurred or a pointer to the error
886 xmlErrorPtr
887 xmlGetLastError(void)
889 if (xmlLastError.code == XML_ERR_OK)
890 return (NULL);
891 return (&xmlLastError);
895 * xmlResetError:
896 * @err: pointer to the error.
898 * Cleanup the error.
900 void
901 xmlResetError(xmlErrorPtr err)
903 if (err == NULL)
904 return;
905 if (err->code == XML_ERR_OK)
906 return;
907 if (err->message != NULL)
908 xmlFree(err->message);
909 if (err->file != NULL)
910 xmlFree(err->file);
911 if (err->str1 != NULL)
912 xmlFree(err->str1);
913 if (err->str2 != NULL)
914 xmlFree(err->str2);
915 if (err->str3 != NULL)
916 xmlFree(err->str3);
917 memset(err, 0, sizeof(xmlError));
918 err->code = XML_ERR_OK;
922 * xmlResetLastError:
924 * Cleanup the last global error registered. For parsing error
925 * this does not change the well-formedness result.
927 void
928 xmlResetLastError(void)
930 if (xmlLastError.code == XML_ERR_OK)
931 return;
932 xmlResetError(&xmlLastError);
936 * xmlCtxtGetLastError:
937 * @ctx: an XML parser context
939 * Get the last parsing error registered.
941 * Returns NULL if no error occurred or a pointer to the error
943 xmlErrorPtr
944 xmlCtxtGetLastError(void *ctx)
946 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
948 if (ctxt == NULL)
949 return (NULL);
950 if (ctxt->lastError.code == XML_ERR_OK)
951 return (NULL);
952 return (&ctxt->lastError);
956 * xmlCtxtResetLastError:
957 * @ctx: an XML parser context
959 * Cleanup the last global error registered. For parsing error
960 * this does not change the well-formedness result.
962 void
963 xmlCtxtResetLastError(void *ctx)
965 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
967 if (ctxt == NULL)
968 return;
969 ctxt->errNo = XML_ERR_OK;
970 if (ctxt->lastError.code == XML_ERR_OK)
971 return;
972 xmlResetError(&ctxt->lastError);
976 * xmlCopyError:
977 * @from: a source error
978 * @to: a target error
980 * Save the original error to the new place.
982 * Returns 0 in case of success and -1 in case of error.
985 xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
986 char *message, *file, *str1, *str2, *str3;
988 if ((from == NULL) || (to == NULL))
989 return(-1);
991 message = (char *) xmlStrdup((xmlChar *) from->message);
992 file = (char *) xmlStrdup ((xmlChar *) from->file);
993 str1 = (char *) xmlStrdup ((xmlChar *) from->str1);
994 str2 = (char *) xmlStrdup ((xmlChar *) from->str2);
995 str3 = (char *) xmlStrdup ((xmlChar *) from->str3);
997 if (to->message != NULL)
998 xmlFree(to->message);
999 if (to->file != NULL)
1000 xmlFree(to->file);
1001 if (to->str1 != NULL)
1002 xmlFree(to->str1);
1003 if (to->str2 != NULL)
1004 xmlFree(to->str2);
1005 if (to->str3 != NULL)
1006 xmlFree(to->str3);
1007 to->domain = from->domain;
1008 to->code = from->code;
1009 to->level = from->level;
1010 to->line = from->line;
1011 to->node = from->node;
1012 to->int1 = from->int1;
1013 to->int2 = from->int2;
1014 to->node = from->node;
1015 to->ctxt = from->ctxt;
1016 to->message = message;
1017 to->file = file;
1018 to->str1 = str1;
1019 to->str2 = str2;
1020 to->str3 = str3;
1022 return 0;