error.c 25.2 KB
Newer Older
1 2 3 4 5
/*
 * error.c: module displaying/handling XML parser errors
 *
 * See Copyright for the status of this software.
 *
6
 * Daniel Veillard <daniel@veillard.com>
7 8
 */

9
#define IN_LIBXML
Bjorn Reese's avatar
Bjorn Reese committed
10
#include "libxml.h"
11

12
#include <string.h>
13 14 15
#include <stdarg.h>
#include <libxml/parser.h>
#include <libxml/xmlerror.h>
Daniel Veillard's avatar
Daniel Veillard committed
16
#include <libxml/xmlmemory.h>
17
#include <libxml/globals.h>
18

19 20 21 22
void xmlGenericErrorDefaultFunc	(void *ctx ATTRIBUTE_UNUSED,
				 const char *msg,
				 ...);

23 24 25 26 27 28 29
#define XML_GET_VAR_STR(msg, str) {				\
    int       size;						\
    int       chars;						\
    char      *larger;						\
    va_list   ap;						\
								\
    str = (char *) xmlMalloc(150);				\
30
    if (str != NULL) {						\
31 32 33 34 35 36 37 38 39 40 41 42 43 44
								\
    size = 150;							\
								\
    while (1) {							\
	va_start(ap, msg);					\
  	chars = vsnprintf(str, size, msg, ap);			\
	va_end(ap);						\
	if ((chars > -1) && (chars < size))			\
	    break;						\
	if (chars > -1)						\
	    size += chars + 1;					\
	else							\
	    size += 100;					\
	if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
45
	    break;						\
46 47
	}							\
	str = larger;						\
48
    }}								\
49
}
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64
/************************************************************************
 * 									*
 * 			Handling of out of context errors		*
 * 									*
 ************************************************************************/

/**
 * xmlGenericErrorDefaultFunc:
 * @ctx:  an error context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 * 
 * Default handler for out of context error messages.
 */
65
void
66
xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
67 68 69 70 71 72 73 74 75 76
    va_list args;

    if (xmlGenericErrorContext == NULL)
	xmlGenericErrorContext = (void *) stderr;

    va_start(args, msg);
    vfprintf((FILE *)xmlGenericErrorContext, msg, args);
    va_end(args);
}

77 78 79 80 81
/**
 * initGenericErrorDefaultFunc:
 * @handler:  the handler
 * 
 * Set or reset (if NULL) the default handler for generic errors
82
 * to the builtin error function.
83
 */
84
void
85
initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
86
{
87 88 89
    if (handler == NULL)
        xmlGenericError = xmlGenericErrorDefaultFunc;
    else
90
        xmlGenericError = (*handler);
91
}
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

/**
 * xmlSetGenericErrorFunc:
 * @ctx:  the new error handling context
 * @handler:  the new handler function
 *
 * Function to reset the handler and the error context for out of
 * context error messages.
 * This simply means that @handler will be called for subsequent
 * error messages while not parsing nor validating. And @ctx will
 * be passed as first argument to @handler
 * One can simply force messages to be emitted to another FILE * than
 * stderr by setting @ctx to this file handle and @handler to NULL.
 */
void
xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
    xmlGenericErrorContext = ctx;
    if (handler != NULL)
	xmlGenericError = handler;
    else
	xmlGenericError = xmlGenericErrorDefaultFunc;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/**
 * xmlSetStructuredErrorFunc:
 * @ctx:  the new error handling context
 * @handler:  the new handler function
 *
 * Function to reset the handler and the error context for out of
 * context structured error messages.
 * This simply means that @handler will be called for subsequent
 * error messages while not parsing nor validating. And @ctx will
 * be passed as first argument to @handler
 */
void
xmlSetStructuredErrorFunc(void *ctx, xmlStructuredErrorFunc handler) {
    xmlGenericErrorContext = ctx;
    xmlStructuredError = handler;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/************************************************************************
 * 									*
 * 			Handling of parsing errors			*
 * 									*
 ************************************************************************/

/**
 * xmlParserPrintFileInfo:
 * @input:  an xmlParserInputPtr input
 * 
 * Displays the associated file and line informations for the current input
 */

void
xmlParserPrintFileInfo(xmlParserInputPtr input) {
    if (input != NULL) {
	if (input->filename)
	    xmlGenericError(xmlGenericErrorContext,
		    "%s:%d: ", input->filename,
		    input->line);
	else
	    xmlGenericError(xmlGenericErrorContext,
		    "Entity: line %d: ", input->line);
    }
}

/**
 * xmlParserPrintFileContext:
 * @input:  an xmlParserInputPtr input
 * 
 * Displays current context within the input content for error tracking
 */

165 166 167
static void
xmlParserPrintFileContextInternal(xmlParserInputPtr input , 
		xmlGenericErrorFunc channel, void *data ) {
168
    const xmlChar *cur, *base;
169
    unsigned int n, col;	/* GCC warns if signed, because compared with sizeof() */
William M. Brack's avatar
William M. Brack committed
170
    xmlChar  content[81]; /* space for 80 chars + line terminator */
171
    xmlChar *ctnt;
172

173
    if (input == NULL) return;
174 175
    cur = input->cur;
    base = input->base;
176
    /* skip backwards over any end-of-lines */
177
    while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) {
178
	cur--;
179 180
    }
    n = 0;
William M. Brack's avatar
William M. Brack committed
181
    /* search backwards for beginning-of-line (to max buff size) */
182 183
    while ((n++ < (sizeof(content)-1)) && (cur > base) && 
    	   (*(cur) != '\n') && (*(cur) != '\r'))
184
        cur--;
185
    if ((*(cur) == '\n') || (*(cur) == '\r')) cur++;
William M. Brack's avatar
William M. Brack committed
186 187 188
    /* calculate the error position in terms of the current position */
    col = input->cur - cur;
    /* search forward for end-of-line (to max buff size) */
189
    n = 0;
190
    ctnt = content;
William M. Brack's avatar
William M. Brack committed
191
    /* copy selected text to our buffer */
192 193
    while ((*cur != 0) && (*(cur) != '\n') && 
    	   (*(cur) != '\r') && (n < sizeof(content)-1)) {
194 195
		*ctnt++ = *cur++;
	n++;
196
    }
197
    *ctnt = 0;
William M. Brack's avatar
William M. Brack committed
198
    /* print out the selected text */
199
    channel(data ,"%s\n", content);
200
    /* create blank line with problem pointer */
201
    n = 0;
202
    ctnt = content;
William M. Brack's avatar
William M. Brack committed
203 204
    /* (leave buffer space for pointer + line terminator) */
    while ((n<col) && (n++ < sizeof(content)-2) && (*ctnt != 0)) {
205 206
	if (*(ctnt) != '\t')
	    *(ctnt) = ' ';
207
	ctnt++;
208
    }
William M. Brack's avatar
William M. Brack committed
209 210
    *ctnt++ = '^';
    *ctnt = 0;
211 212 213 214 215 216 217 218 219 220 221 222 223
    channel(data ,"%s\n", content);
}

/**
 * xmlParserPrintFileContext:
 * @input:  an xmlParserInputPtr input
 * 
 * Displays current context within the input content for error tracking
 */
void
xmlParserPrintFileContext(xmlParserInputPtr input) {
   xmlParserPrintFileContextInternal(input, xmlGenericError,
                                     xmlGenericErrorContext);
224 225 226
}

/**
227
 * xmlReportError:
228
 * @err: the error
229 230
 * @ctx: the parser context or NULL
 * @str: the formatted error message
231
 *
232 233
 * Report an erro with its context, replace the 4 old error/warning
 * routines.
234
 */
235
static void
236 237
xmlReportError(xmlErrorPtr err, xmlParserCtxtPtr ctxt, const char *str,
               xmlGenericErrorFunc channel, void *data)
238
{
239 240 241 242
    char *file = NULL;
    int line = 0;
    int code = -1;
    int domain;
243 244
    const xmlChar *name = NULL;
    xmlNodePtr node;
245 246 247 248
    xmlErrorLevel level;
    xmlParserInputPtr input = NULL;
    xmlParserInputPtr cur = NULL;

249 250
    if (err == NULL)
        return;
251

252 253 254 255
    if (channel == NULL) {
	channel = xmlGenericError;
	data = xmlGenericErrorContext;
    }
256 257 258 259 260
    file = err->file;
    line = err->line;
    code = err->code;
    domain = err->domain;
    level = err->level;
261
    node = err->node;
262

263 264 265
    if (code == XML_ERR_OK)
        return;

266 267 268
    if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
        name = node->name;

269 270 271
    /*
     * Maintain the compatibility with the legacy error handling
     */
272 273 274 275 276 277 278 279 280 281
    if (ctxt != NULL) {
        input = ctxt->input;
        if ((input != NULL) && (input->filename == NULL) &&
            (ctxt->inputNr > 1)) {
            cur = input;
            input = ctxt->inputTab[ctxt->inputNr - 2];
        }
        if (input != NULL) {
            if (input->filename)
                channel(data, "%s:%d: ", input->filename, input->line);
282
            else if ((line != 0) && (domain == XML_FROM_PARSER))
283 284 285 286 287
                channel(data, "Entity: line %d: ", input->line);
        }
    } else {
        if (file != NULL)
            channel(data, "%s:%d: ", file, line);
288
        else if ((line != 0) && (domain == XML_FROM_PARSER))
289
            channel(data, "Entity: line %d: ", line);
290
    }
291 292 293
    if (name != NULL) {
        channel(data, "element %s: ", name);
    }
294 295 296 297 298 299 300 301 302 303
    if (code == XML_ERR_OK)
        return;
    switch (domain) {
        case XML_FROM_PARSER:
            channel(data, "parser ");
            break;
        case XML_FROM_NAMESPACE:
            channel(data, "namespace ");
            break;
        case XML_FROM_DTD:
304
        case XML_FROM_VALID:
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
            channel(data, "validity ");
            break;
        case XML_FROM_HTML:
            channel(data, "HTML parser ");
            break;
        case XML_FROM_MEMORY:
            channel(data, "memory ");
            break;
        case XML_FROM_OUTPUT:
            channel(data, "output ");
            break;
        case XML_FROM_IO:
            channel(data, "I/O ");
            break;
        case XML_FROM_XINCLUDE:
            channel(data, "XInclude ");
            break;
        case XML_FROM_XPATH:
            channel(data, "XPath ");
            break;
        case XML_FROM_XPOINTER:
            channel(data, "parser ");
            break;
        case XML_FROM_REGEXP:
            channel(data, "regexp ");
            break;
331
        case XML_FROM_SCHEMASV:
332
            channel(data, "Schemas validity ");
333 334
            break;
        case XML_FROM_SCHEMASP:
335
            channel(data, "Schemas parser ");
336
            break;
337 338 339 340
        case XML_FROM_RELAXNGP:
            channel(data, "Relax-NG parser ");
            break;
        case XML_FROM_RELAXNGV:
341
            channel(data, "Relax-NG validity ");
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
            break;
        case XML_FROM_CATALOG:
            channel(data, "Catalog ");
            break;
        case XML_FROM_C14N:
            channel(data, "C14N ");
            break;
        case XML_FROM_XSLT:
            channel(data, "XSLT ");
            break;
        default:
            break;
    }
    if (code == XML_ERR_OK)
        return;
    switch (level) {
        case XML_ERR_NONE:
            channel(data, ": ");
360
            break;
361 362
        case XML_ERR_WARNING:
            channel(data, "warning : ");
363
            break;
364 365
        case XML_ERR_ERROR:
            channel(data, "error : ");
366
            break;
367 368
        case XML_ERR_FATAL:
            channel(data, "error : ");
369
            break;
370 371 372 373
    }
    if (code == XML_ERR_OK)
        return;
    if (str != NULL) {
374 375 376 377
        int len;
	len = xmlStrlen((const xmlChar *)str);
	if ((len > 0) && (str[len - 1] != '\n'))
	    channel(data, "%s\n", str);
Daniel Veillard's avatar
Daniel Veillard committed
378 379
	else
	    channel(data, "%s", str);
380
    } else {
381
        channel(data, "%s\n", "out of memory error");
Daniel Veillard's avatar
Daniel Veillard committed
382
    }
383 384 385 386 387 388 389 390
    if (code == XML_ERR_OK)
        return;

    if (ctxt != NULL) {
        xmlParserPrintFileContextInternal(input, channel, data);
        if (cur != NULL) {
            if (cur->filename)
                channel(data, "%s:%d: \n", cur->filename, cur->line);
391
            else if ((line != 0) && (domain == XML_FROM_PARSER))
392 393 394 395
                channel(data, "Entity: line %d: \n", cur->line);
            xmlParserPrintFileContextInternal(cur, channel, data);
        }
    }
396 397 398 399 400 401 402 403 404 405 406 407 408
    if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) &&
        (err->int1 < 100) &&
	(err->int1 < xmlStrlen((const xmlChar *)err->str1))) {
	xmlChar buf[150];
	int i;

	channel(data, "%s\n", err->str1);
	for (i=0;i < err->int1;i++)
	     buf[i] = ' ';
	buf[i++] = '^';
	buf[i] = 0;
	channel(data, "%s\n", buf);
    }
409 410 411
}

/**
412
 * __xmlRaiseError:
413
 * @schannel: the structured callback channel
414
 * @channel: the old callback channel
415 416
 * @data: the callback data
 * @ctx: the parser context or NULL
417 418 419 420 421 422 423 424 425 426 427 428 429 430
 * @ctx: the parser context or NULL
 * @domain: the domain for the error
 * @code: the code for the error
 * @level: the xmlErrorLevel for the error
 * @file: the file source of the error (or NULL)
 * @line: the line of the error or 0 if N/A
 * @str1: extra string info
 * @str2: extra string info
 * @str3: extra string info
 * @int1: extra int info
 * @int2: extra int info
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 *
431
 * Update the appropriate global or contextual error structure,
432 433 434 435
 * then forward the error message down the parser or generic
 * error callback handler
 */
void
436 437
__xmlRaiseError(xmlStructuredErrorFunc schannel,
              xmlGenericErrorFunc channel, void *data, void *ctx,
438
              void *nod, int domain, int code, xmlErrorLevel level,
439 440 441 442
              const char *file, int line, const char *str1,
              const char *str2, const char *str3, int int1, int int2,
	      const char *msg, ...)
{
443
    xmlParserCtxtPtr ctxt = NULL;
444
    xmlNodePtr node = (xmlNodePtr) nod;
445 446 447
    char *str = NULL;
    xmlParserInputPtr input = NULL;
    xmlErrorPtr to = &xmlLastError;
448
    xmlChar *base = NULL;
449

450 451
    if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING))
        return;
452 453 454 455
    if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) ||
        (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) ||
	(domain == XML_FROM_IO)) {
	ctxt = (xmlParserCtxtPtr) ctx;
456 457 458
	if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
	    (ctxt->sax->initialized == XML_SAX2_MAGIC))
	    schannel = ctxt->sax->serror;
459
    }
460 461 462 463
    /*
     * Check if structured error handler set
     */
    if (schannel == NULL) {
464
	schannel = xmlStructuredError;
465 466 467 468 469 470
	/*
	 * if user has defined handler, change data ptr to user's choice
	 */
	if (schannel != NULL)
	    data = xmlGenericErrorContext;
    }
471 472 473 474 475 476 477 478
    if ((domain == XML_FROM_VALID) &&
        ((channel == xmlParserValidityError) ||
	 (channel == xmlParserValidityWarning))) {
	ctxt = (xmlParserCtxtPtr) ctx;
	if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) &&
	    (ctxt->sax->initialized == XML_SAX2_MAGIC))
	    schannel = ctxt->sax->serror;
    }
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    if (code == XML_ERR_OK)
        return;
    /*
     * Formatting the message
     */
    if (msg == NULL) {
        str = (char *) xmlStrdup(BAD_CAST "No error message provided");
    } else {
        XML_GET_VAR_STR(msg, str);
    }

    /*
     * specific processing if a parser context is provided
     */
    if (ctxt != NULL) {
        if (file == NULL) {
            input = ctxt->input;
            if ((input != NULL) && (input->filename == NULL) &&
                (ctxt->inputNr > 1)) {
                input = ctxt->inputTab[ctxt->inputNr - 2];
            }
            if (input != NULL) {
                file = input->filename;
                line = input->line;
            }
        }
        to = &ctxt->lastError;
506 507
    } else if ((node != NULL) && (file == NULL)) {
	int i;
508 509 510

	if ((node->doc != NULL) && (node->doc->URL != NULL))
	    base = xmlStrdup(node->doc->URL);
511 512 513 514
	for (i = 0;
	     ((i < 10) && (node != NULL) && (node->type != XML_ELEMENT_NODE));
	     i++)
	     node = node->parent;
515 516 517 518
        if ((base == NULL) && (node != NULL) &&
	    (node->doc != NULL) && (node->doc->URL != NULL))
	    base = xmlStrdup(node->doc->URL);

519
	if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
520
	    line = node->line;
521 522 523
    }

    /*
524
     * Save the information about the error
525 526 527 528 529 530 531 532
     */
    xmlResetError(to);
    to->domain = domain;
    to->code = code;
    to->message = str;
    to->level = level;
    if (file != NULL)
        to->file = (char *) xmlStrdup((const xmlChar *) file);
533 534 535 536
    else if (base != NULL) {
        to->file = (char *) base;
	file = (char *) base;
    }
537 538 539 540 541 542 543 544 545
    to->line = line;
    if (str1 != NULL)
        to->str1 = (char *) xmlStrdup((const xmlChar *) str1);
    if (str2 != NULL)
        to->str2 = (char *) xmlStrdup((const xmlChar *) str2);
    if (str3 != NULL)
        to->str3 = (char *) xmlStrdup((const xmlChar *) str3);
    to->int1 = int1;
    to->int2 = int2;
546
    to->node = node;
547
    to->ctxt = ctx;
548

549 550 551
    if (to != &xmlLastError)
        xmlCopyError(to,&xmlLastError);

552
    /*
553
     * Find the callback channel if channel param is NULL
554
     */
555
    if ((ctxt != NULL) && (channel == NULL) && (xmlStructuredError == NULL) && (ctxt->sax != NULL)) {
556 557 558 559
        if (level == XML_ERR_WARNING)
	    channel = ctxt->sax->warning;
        else
	    channel = ctxt->sax->error;
560
	data = ctxt->userData;
561
    } else if (channel == NULL) {
562 563 564 565
        if (xmlStructuredError != NULL)
	    schannel = xmlStructuredError;
	else
	    channel = xmlGenericError;
566 567
	data = xmlGenericErrorContext;
    }
568 569 570 571
    if (schannel != NULL) {
        schannel(data, to);
	return;
    }
572 573 574 575 576 577 578
    if (channel == NULL)
        return;

    if ((channel == xmlParserError) ||
        (channel == xmlParserWarning) ||
	(channel == xmlParserValidityError) ||
	(channel == xmlParserValidityWarning))
579 580 581 582
	xmlReportError(to, ctxt, str, NULL, NULL);
    else if ((channel == (xmlGenericErrorFunc) fprintf) ||
             (channel == xmlGenericErrorDefaultFunc))
	xmlReportError(to, ctxt, str, channel, data);
583 584
    else
	channel(data, "%s", str);
Daniel Veillard's avatar
Daniel Veillard committed
585 586
}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
/**
 * __xmlSimpleError:
 * @domain: where the error comes from
 * @code: the error code
 * @node: the context node
 * @extra:  extra informations
 *
 * Handle an out of memory condition
 */
void
__xmlSimpleError(int domain, int code, xmlNodePtr node,
                 const char *msg, const char *extra)
{

    if (code == XML_ERR_NO_MEMORY) {
	if (extra)
603
	    __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
604 605 606 607
			    XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
			    NULL, NULL, 0, 0,
			    "Memory allocation failed : %s\n", extra);
	else
608
	    __xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
609 610 611
			    XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
			    NULL, NULL, 0, 0, "Memory allocation failed\n");
    } else {
612
	__xmlRaiseError(NULL, NULL, NULL, NULL, node, domain,
613 614 615 616
			code, XML_ERR_ERROR, NULL, 0, extra,
			NULL, NULL, 0, 0, msg, extra);
    }
}
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
/**
 * xmlParserError:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 * 
 * Display and format an error messages, gives file, line, position and
 * extra parameters.
 */
void
xmlParserError(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input = NULL;
    xmlParserInputPtr cur = NULL;
Daniel Veillard's avatar
Daniel Veillard committed
632
    char * str;
633 634 635 636 637 638 639 640 641 642 643 644

    if (ctxt != NULL) {
	input = ctxt->input;
	if ((input != NULL) && (input->filename == NULL) &&
	    (ctxt->inputNr > 1)) {
	    cur = input;
	    input = ctxt->inputTab[ctxt->inputNr - 2];
	}
	xmlParserPrintFileInfo(input);
    }

    xmlGenericError(xmlGenericErrorContext, "error: ");
645
    XML_GET_VAR_STR(msg, str);
646
    xmlGenericError(xmlGenericErrorContext, "%s", str);
647 648
    if (str != NULL)
	xmlFree(str);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

    if (ctxt != NULL) {
	xmlParserPrintFileContext(input);
	if (cur != NULL) {
	    xmlParserPrintFileInfo(cur);
	    xmlGenericError(xmlGenericErrorContext, "\n");
	    xmlParserPrintFileContext(cur);
	}
    }
}

/**
 * xmlParserWarning:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 * 
 * Display and format a warning messages, gives file, line, position and
 * extra parameters.
 */
void
xmlParserWarning(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input = NULL;
    xmlParserInputPtr cur = NULL;
Daniel Veillard's avatar
Daniel Veillard committed
675
    char * str;
676 677 678 679 680 681 682 683 684 685 686 687

    if (ctxt != NULL) {
	input = ctxt->input;
	if ((input != NULL) && (input->filename == NULL) &&
	    (ctxt->inputNr > 1)) {
	    cur = input;
	    input = ctxt->inputTab[ctxt->inputNr - 2];
	}
	xmlParserPrintFileInfo(input);
    }
        
    xmlGenericError(xmlGenericErrorContext, "warning: ");
688
    XML_GET_VAR_STR(msg, str);
689
    xmlGenericError(xmlGenericErrorContext, "%s", str);
690 691
    if (str != NULL)
	xmlFree(str);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722

    if (ctxt != NULL) {
	xmlParserPrintFileContext(input);
	if (cur != NULL) {
	    xmlParserPrintFileInfo(cur);
	    xmlGenericError(xmlGenericErrorContext, "\n");
	    xmlParserPrintFileContext(cur);
	}
    }
}

/************************************************************************
 * 									*
 * 			Handling of validation errors			*
 * 									*
 ************************************************************************/

/**
 * xmlParserValidityError:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 * 
 * Display and format an validity error messages, gives file,
 * line, position and extra parameters.
 */
void
xmlParserValidityError(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input = NULL;
723
    char * str;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    int len = xmlStrlen((const xmlChar *) msg);
    static int had_info = 0;

    if ((len > 1) && (msg[len - 2] != ':')) {
	if (ctxt != NULL) {
	    input = ctxt->input;
	    if ((input->filename == NULL) && (ctxt->inputNr > 1))
		input = ctxt->inputTab[ctxt->inputNr - 2];
		
	    if (had_info == 0) {
		xmlParserPrintFileInfo(input);
	    }
	}
	xmlGenericError(xmlGenericErrorContext, "validity error: ");
	had_info = 0;
    } else {
	had_info = 1;
741 742
    }

743
    XML_GET_VAR_STR(msg, str);
744
    xmlGenericError(xmlGenericErrorContext, "%s", str);
745 746
    if (str != NULL)
	xmlFree(str);
747

748
    if ((ctxt != NULL) && (input != NULL)) {
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	xmlParserPrintFileContext(input);
    }
}

/**
 * xmlParserValidityWarning:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 * 
 * Display and format a validity warning messages, gives file, line,
 * position and extra parameters.
 */
void
xmlParserValidityWarning(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input = NULL;
767
    char * str;
768
    int len = xmlStrlen((const xmlChar *) msg);
769

770
    if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
771 772 773 774 775 776 777 778
	input = ctxt->input;
	if ((input->filename == NULL) && (ctxt->inputNr > 1))
	    input = ctxt->inputTab[ctxt->inputNr - 2];

	xmlParserPrintFileInfo(input);
    }
        
    xmlGenericError(xmlGenericErrorContext, "validity warning: ");
779
    XML_GET_VAR_STR(msg, str);
780
    xmlGenericError(xmlGenericErrorContext, "%s", str);
781 782
    if (str != NULL)
	xmlFree(str);
783 784 785 786 787 788 789

    if (ctxt != NULL) {
	xmlParserPrintFileContext(input);
    }
}


790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
/************************************************************************
 *									*
 *			Extended Error Handling				*
 *									*
 ************************************************************************/

/**
 * xmlGetLastError:
 *
 * Get the last global error registered. This is per thread if compiled
 * with thread support.
 *
 * Returns NULL if no error occured or a pointer to the error
 */
xmlErrorPtr
xmlGetLastError(void)
{
    if (xmlLastError.code == XML_ERR_OK)
        return (NULL);
    return (&xmlLastError);
}

/**
 * xmlResetError:
 * @err: pointer to the error.
 *
 * Cleanup the error.
 */
void
xmlResetError(xmlErrorPtr err)
{
    if (err == NULL)
        return;
    if (err->code == XML_ERR_OK)
        return;
    if (err->message != NULL)
        xmlFree(err->message);
    if (err->file != NULL)
        xmlFree(err->file);
    if (err->str1 != NULL)
        xmlFree(err->str1);
    if (err->str2 != NULL)
        xmlFree(err->str2);
    if (err->str3 != NULL)
        xmlFree(err->str3);
    memset(err, 0, sizeof(xmlError));
    err->code = XML_ERR_OK;
}

/**
 * xmlResetLastError:
 *
 * Cleanup the last global error registered. For parsing error
 * this does not change the well-formedness result.
 */
void
xmlResetLastError(void)
{
    if (xmlLastError.code == XML_ERR_OK)
        return;
    xmlResetError(&xmlLastError);
}

/**
 * xmlCtxtGetLastError:
 * @ctx:  an XML parser context
 *
 * Get the last parsing error registered.
 *
 * Returns NULL if no error occured or a pointer to the error
 */
xmlErrorPtr
xmlCtxtGetLastError(void *ctx)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

    if (ctxt == NULL)
        return (NULL);
    if (ctxt->lastError.code == XML_ERR_OK)
        return (NULL);
    return (&ctxt->lastError);
}

/**
 * xmlCtxtResetLastError:
 * @ctx:  an XML parser context
 *
 * Cleanup the last global error registered. For parsing error
 * this does not change the well-formedness result.
 */
void
xmlCtxtResetLastError(void *ctx)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

    if (ctxt == NULL)
        return;
    if (ctxt->lastError.code == XML_ERR_OK)
        return;
    xmlResetError(&ctxt->lastError);
}

/**
 * xmlCopyError:
 * @from:  a source error
 * @to:  a target error
 *
 * Save the original error to the new place.
 *
 * Returns 0 in case of success and -1 in case of error.
 */
int
xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) {
    if ((from == NULL) || (to == NULL))
        return(-1);
    if (to->message != NULL)
        xmlFree(to->message);
    if (to->file != NULL)
        xmlFree(to->file);
    if (to->str1 != NULL)
        xmlFree(to->str1);
    if (to->str2 != NULL)
        xmlFree(to->str2);
    if (to->str3 != NULL)
        xmlFree(to->str3);
    to->domain = from->domain;
    to->code = from->code;
    to->level = from->level;
    to->line = from->line;
919
    to->node = from->node;
920 921
    to->int1 = from->int1;
    to->int2 = from->int2;
922 923
    to->node = from->node;
    to->ctxt = from->ctxt;
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    if (from->message != NULL)
        to->message = (char *) xmlStrdup((xmlChar *) from->message);
    else
        to->message = NULL;
    if (from->file != NULL)
        to->file = (char *) xmlStrdup((xmlChar *) from->file);
    else
        to->file = NULL;
    if (from->str1 != NULL)
        to->str1 = (char *) xmlStrdup((xmlChar *) from->str1);
    else
        to->str1 = NULL;
    if (from->str2 != NULL)
        to->str2 = (char *) xmlStrdup((xmlChar *) from->str2);
    else
        to->str2 = NULL;
    if (from->str3 != NULL)
        to->str3 = (char *) xmlStrdup((xmlChar *) from->str3);
    else
        to->str3 = NULL;
    return(0);
}