Annotation of XML/error.c, revision 1.28

1.1       daniel      1: /*
1.8       daniel      2:  * error.c: module displaying/handling XML parser errors
                      3:  *
1.9       daniel      4:  * See Copyright for the status of this software.
                      5:  *
1.8       daniel      6:  * Daniel Veillard <Daniel.Veillard@w3.org>
1.1       daniel      7:  */
                      8: 
1.21      daniel      9: #ifdef WIN32
                     10: #include "win32config.h"
                     11: #else
                     12: #include "config.h"
                     13: #endif
                     14: 
1.1       daniel     15: #include <stdio.h>
                     16: #include <stdarg.h>
1.23      daniel     17: #include <libxml/parser.h>
1.27      veillard   18: #include <libxml/xmlerror.h>
                     19: 
                     20: /************************************************************************
                     21:  *                                                                     *
                     22:  *                     Handling of out of context errors               *
                     23:  *                                                                     *
                     24:  ************************************************************************/
                     25: 
                     26: /**
                     27:  * xmlGenericErrorDefaultFunc:
                     28:  * @ctx:  an error context
                     29:  * @msg:  the message to display/transmit
                     30:  * @...:  extra parameters for the message display
                     31:  * 
                     32:  * Default handler for out of context error messages.
                     33:  */
                     34: void
                     35: xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...) {
                     36:     va_list args;
                     37: 
                     38:     if (xmlGenericErrorContext == NULL)
                     39:        xmlGenericErrorContext = (void *) stderr;
                     40: 
                     41:     va_start(args, msg);
                     42:     vfprintf((FILE *)xmlGenericErrorContext, msg, args);
                     43:     va_end(args);
                     44: }
                     45: 
                     46: xmlGenericErrorFunc xmlGenericError = xmlGenericErrorDefaultFunc;
                     47: void *xmlGenericErrorContext = NULL;
                     48: 
                     49: 
                     50: /**
                     51:  * xmlSetGenericErrorFunc:
                     52:  * @ctx:  the new error handling context
                     53:  * @handler:  the new handler function
                     54:  *
                     55:  * Function to reset the handler and the error context for out of
                     56:  * context error messages.
                     57:  * This simply means that @handler will be called for subsequent
                     58:  * error messages while not parsing nor validating. And @ctx will
                     59:  * be passed as first argument to @handler
                     60:  * One can simply force messages to be emitted to another FILE * than
                     61:  * stderr by setting @ctx to this file handle and @handler to NULL.
                     62:  */
                     63: void
                     64: xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
1.28    ! veillard   65:     xmlGenericErrorContext = ctx;
1.27      veillard   66:     if (handler != NULL)
                     67:        xmlGenericError = handler;
1.28    ! veillard   68:     else
        !            69:        xmlGenericError = xmlGenericErrorDefaultFunc;
1.27      veillard   70: }
                     71: 
                     72: /************************************************************************
                     73:  *                                                                     *
                     74:  *                     Handling of parsing errors                      *
                     75:  *                                                                     *
                     76:  ************************************************************************/
1.1       daniel     77: 
1.19      daniel     78: /**
                     79:  * xmlParserPrintFileInfo:
                     80:  * @input:  an xmlParserInputPtr input
                     81:  * 
                     82:  * Displays the associated file and line informations for the current input
                     83:  */
                     84: 
                     85: void
1.17      daniel     86: xmlParserPrintFileInfo(xmlParserInputPtr input) {
                     87:     if (input != NULL) {
                     88:        if (input->filename)
1.27      veillard   89:            xmlGenericError(xmlGenericErrorContext,
                     90:                    "%s:%d: ", input->filename,
1.17      daniel     91:                    input->line);
                     92:        else
1.27      veillard   93:            xmlGenericError(xmlGenericErrorContext,
                     94:                    "Entity: line %d: ", input->line);
1.17      daniel     95:     }
                     96: }
                     97: 
1.19      daniel     98: /**
                     99:  * xmlParserPrintFileContext:
                    100:  * @input:  an xmlParserInputPtr input
                    101:  * 
                    102:  * Displays current context within the input content for error tracking
                    103:  */
                    104: 
                    105: void
1.17      daniel    106: xmlParserPrintFileContext(xmlParserInputPtr input) {
1.20      daniel    107:     const xmlChar *cur, *base;
1.1       daniel    108:     int n;
                    109: 
1.22      daniel    110:     if (input == NULL) return;
1.16      daniel    111:     cur = input->cur;
                    112:     base = input->base;
1.11      daniel    113:     while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
1.7       daniel    114:        cur--;
                    115:     }
1.2       daniel    116:     n = 0;
1.12      daniel    117:     while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
1.2       daniel    118:         cur--;
1.7       daniel    119:     if ((*cur == '\n') || (*cur == '\r')) cur++;
1.1       daniel    120:     base = cur;
1.2       daniel    121:     n = 0;
1.1       daniel    122:     while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
1.27      veillard  123:         xmlGenericError(xmlGenericErrorContext,
                    124:                "%c", (unsigned char) *cur++);
1.1       daniel    125:        n++;
                    126:     }
1.27      veillard  127:     xmlGenericError(xmlGenericErrorContext, "\n");
1.16      daniel    128:     cur = input->cur;
1.7       daniel    129:     while ((*cur == '\n') || (*cur == '\r'))
                    130:        cur--;
1.5       daniel    131:     n = 0;
1.12      daniel    132:     while ((cur != base) && (n++ < 80)) {
1.27      veillard  133:         xmlGenericError(xmlGenericErrorContext, " ");
1.1       daniel    134:         base++;
                    135:     }
1.27      veillard  136:     xmlGenericError(xmlGenericErrorContext,"^\n");
1.1       daniel    137: }
1.6       daniel    138: 
1.8       daniel    139: /**
1.17      daniel    140:  * xmlParserError:
                    141:  * @ctx:  an XML parser context
                    142:  * @msg:  the message to display/transmit
                    143:  * @...:  extra parameters for the message display
                    144:  * 
                    145:  * Display and format an error messages, gives file, line, position and
                    146:  * extra parameters.
                    147:  */
                    148: void
                    149: xmlParserError(void *ctx, const char *msg, ...)
                    150: {
                    151:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1.26      veillard  152:     xmlParserInputPtr input = NULL;
1.18      daniel    153:     xmlParserInputPtr cur = NULL;
1.17      daniel    154:     va_list args;
                    155: 
1.26      veillard  156:     if (ctxt != NULL) {
                    157:        input = ctxt->input;
                    158:        if ((input != NULL) && (input->filename == NULL) &&
                    159:            (ctxt->inputNr > 1)) {
                    160:            cur = input;
                    161:            input = ctxt->inputTab[ctxt->inputNr - 2];
                    162:        }
                    163:        xmlParserPrintFileInfo(input);
1.18      daniel    164:     }
1.17      daniel    165: 
1.27      veillard  166:     xmlGenericError(xmlGenericErrorContext, "error: ");
1.17      daniel    167:     va_start(args, msg);
1.27      veillard  168:     vfprintf(xmlGenericErrorContext, msg, args);
1.17      daniel    169:     va_end(args);
                    170: 
1.26      veillard  171:     if (ctxt != NULL) {
                    172:        xmlParserPrintFileContext(input);
                    173:        if (cur != NULL) {
                    174:            xmlParserPrintFileInfo(cur);
1.27      veillard  175:            xmlGenericError(xmlGenericErrorContext, "\n");
1.26      veillard  176:            xmlParserPrintFileContext(cur);
                    177:        }
1.18      daniel    178:     }
1.17      daniel    179: }
                    180: 
                    181: /**
1.8       daniel    182:  * xmlParserWarning:
1.15      daniel    183:  * @ctx:  an XML parser context
1.8       daniel    184:  * @msg:  the message to display/transmit
                    185:  * @...:  extra parameters for the message display
                    186:  * 
                    187:  * Display and format a warning messages, gives file, line, position and
                    188:  * extra parameters.
1.6       daniel    189:  */
1.8       daniel    190: void
1.14      daniel    191: xmlParserWarning(void *ctx, const char *msg, ...)
1.8       daniel    192: {
1.14      daniel    193:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1.26      veillard  194:     xmlParserInputPtr input = NULL;
1.18      daniel    195:     xmlParserInputPtr cur = NULL;
1.6       daniel    196:     va_list args;
                    197: 
1.26      veillard  198:     if (ctxt != NULL) {
                    199:        input = ctxt->input;
                    200:        if ((input != NULL) && (input->filename == NULL) &&
                    201:            (ctxt->inputNr > 1)) {
                    202:            cur = input;
                    203:            input = ctxt->inputTab[ctxt->inputNr - 2];
                    204:        }
                    205:        xmlParserPrintFileInfo(input);
1.18      daniel    206:     }
                    207:         
1.27      veillard  208:     xmlGenericError(xmlGenericErrorContext, "warning: ");
1.16      daniel    209:     va_start(args, msg);
1.27      veillard  210:     vfprintf(xmlGenericErrorContext, msg, args);
1.13      daniel    211:     va_end(args);
1.17      daniel    212: 
1.27      veillard  213: 
1.26      veillard  214:     if (ctxt != NULL) {
                    215:        xmlParserPrintFileContext(input);
                    216:        if (cur != NULL) {
                    217:            xmlParserPrintFileInfo(cur);
1.27      veillard  218:            xmlGenericError(xmlGenericErrorContext, "\n");
1.26      veillard  219:            xmlParserPrintFileContext(cur);
                    220:        }
1.18      daniel    221:     }
1.17      daniel    222: }
                    223: 
1.27      veillard  224: /************************************************************************
                    225:  *                                                                     *
                    226:  *                     Handling of validation errors                   *
                    227:  *                                                                     *
                    228:  ************************************************************************/
                    229: 
1.17      daniel    230: /**
                    231:  * xmlParserValidityError:
                    232:  * @ctx:  an XML parser context
                    233:  * @msg:  the message to display/transmit
                    234:  * @...:  extra parameters for the message display
                    235:  * 
                    236:  * Display and format an validity error messages, gives file,
                    237:  * line, position and extra parameters.
                    238:  */
                    239: void
                    240: xmlParserValidityError(void *ctx, const char *msg, ...)
                    241: {
                    242:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1.26      veillard  243:     xmlParserInputPtr input = NULL;
1.17      daniel    244:     va_list args;
                    245: 
1.26      veillard  246:     if (ctxt != NULL) {
                    247:        input = ctxt->input;
                    248:        if ((input->filename == NULL) && (ctxt->inputNr > 1))
                    249:            input = ctxt->inputTab[ctxt->inputNr - 2];
                    250:            
                    251:        xmlParserPrintFileInfo(input);
                    252:     }
1.17      daniel    253: 
1.27      veillard  254:     xmlGenericError(xmlGenericErrorContext, "validity error: ");
1.17      daniel    255:     va_start(args, msg);
1.27      veillard  256:     vfprintf(xmlGenericErrorContext, msg, args);
1.17      daniel    257:     va_end(args);
                    258: 
1.26      veillard  259:     if (ctxt != NULL) {
                    260:        xmlParserPrintFileContext(input);
                    261:     }
1.17      daniel    262: }
                    263: 
                    264: /**
                    265:  * xmlParserValidityWarning:
                    266:  * @ctx:  an XML parser context
                    267:  * @msg:  the message to display/transmit
                    268:  * @...:  extra parameters for the message display
                    269:  * 
                    270:  * Display and format a validity warning messages, gives file, line,
                    271:  * position and extra parameters.
                    272:  */
                    273: void
                    274: xmlParserValidityWarning(void *ctx, const char *msg, ...)
                    275: {
                    276:     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1.26      veillard  277:     xmlParserInputPtr input = NULL;
1.17      daniel    278:     va_list args;
                    279: 
1.26      veillard  280:     if (ctxt != NULL) {
                    281:        input = ctxt->input;
                    282:        if ((input->filename == NULL) && (ctxt->inputNr > 1))
                    283:            input = ctxt->inputTab[ctxt->inputNr - 2];
1.17      daniel    284: 
1.26      veillard  285:        xmlParserPrintFileInfo(input);
                    286:     }
1.17      daniel    287:         
1.27      veillard  288:     xmlGenericError(xmlGenericErrorContext, "validity warning: ");
1.17      daniel    289:     va_start(args, msg);
1.27      veillard  290:     vfprintf(xmlGenericErrorContext, msg, args);
1.17      daniel    291:     va_end(args);
                    292: 
1.26      veillard  293:     if (ctxt != NULL) {
                    294:        xmlParserPrintFileContext(input);
                    295:     }
1.6       daniel    296: }
1.27      veillard  297: 
1.8       daniel    298: 

Webmaster