blob: 4fbdd987ac4b83bc72090835c753bb84def0a14f [file] [log] [blame]
Daniel Veillard260a68f1998-08-13 03:39:55 +00001/*
Daniel Veillard97b58771998-10-20 06:14:16 +00002 * error.c: module displaying/handling XML parser errors
3 *
Daniel Veillard39a1f9a1999-01-17 19:11:59 +00004 * See Copyright for the status of this software.
5 *
Daniel Veillard97b58771998-10-20 06:14:16 +00006 * Daniel Veillard <Daniel.Veillard@w3.org>
Daniel Veillard260a68f1998-08-13 03:39:55 +00007 */
8
9#include <stdio.h>
10#include <stdarg.h>
11#include "parser.h"
12
Daniel Veillardb05deb71999-08-10 19:04:08 +000013static void
14xmlParserPrintFileInfo(xmlParserInputPtr input) {
15 if (input != NULL) {
16 if (input->filename)
17 fprintf(stderr, "%s:%d: ", input->filename,
18 input->line);
19 else
20 fprintf(stderr, "line %d: ", input->line);
21 }
22}
23
24static void
25xmlParserPrintFileContext(xmlParserInputPtr input) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000026 const CHAR *cur, *base;
Daniel Veillard260a68f1998-08-13 03:39:55 +000027 int n;
28
Daniel Veillardb05deb71999-08-10 19:04:08 +000029 cur = input->cur;
30 base = input->base;
Daniel Veillard1e346af1999-02-22 10:33:01 +000031 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000032 cur--;
Daniel Veillard260a68f1998-08-13 03:39:55 +000033 }
34 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000035 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
Daniel Veillard260a68f1998-08-13 03:39:55 +000036 cur--;
37 if ((*cur == '\n') || (*cur == '\r')) cur++;
38 base = cur;
39 n = 0;
40 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
41 fprintf(stderr, "%c", (unsigned char) *cur++);
42 n++;
43 }
44 fprintf(stderr, "\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +000045 cur = input->cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +000046 while ((*cur == '\n') || (*cur == '\r'))
47 cur--;
48 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000049 while ((cur != base) && (n++ < 80)) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000050 fprintf(stderr, " ");
51 base++;
52 }
53 fprintf(stderr,"^\n");
54}
55
Daniel Veillard97b58771998-10-20 06:14:16 +000056/**
Daniel Veillardb05deb71999-08-10 19:04:08 +000057 * xmlParserError:
58 * @ctx: an XML parser context
59 * @msg: the message to display/transmit
60 * @...: extra parameters for the message display
61 *
62 * Display and format an error messages, gives file, line, position and
63 * extra parameters.
64 */
65void
66xmlParserError(void *ctx, const char *msg, ...)
67{
68 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
69 xmlParserInputPtr input;
70 va_list args;
71
72 input = ctxt->input;
73 if ((input->filename == NULL) && (ctxt->inputNr > 1))
74 input = ctxt->inputTab[ctxt->inputNr - 2];
75
76 xmlParserPrintFileInfo(input);
77
78 fprintf(stderr, "error: ");
79 va_start(args, msg);
80 vfprintf(stderr, msg, args);
81 va_end(args);
82
83 xmlParserPrintFileContext(input);
84}
85
86/**
Daniel Veillard97b58771998-10-20 06:14:16 +000087 * xmlParserWarning:
Daniel Veillard011b63c1999-06-02 17:44:04 +000088 * @ctx: an XML parser context
Daniel Veillard97b58771998-10-20 06:14:16 +000089 * @msg: the message to display/transmit
90 * @...: extra parameters for the message display
91 *
92 * Display and format a warning messages, gives file, line, position and
93 * extra parameters.
Daniel Veillard260a68f1998-08-13 03:39:55 +000094 */
Daniel Veillard97b58771998-10-20 06:14:16 +000095void
Daniel Veillard27d88741999-05-29 11:51:49 +000096xmlParserWarning(void *ctx, const char *msg, ...)
Daniel Veillard97b58771998-10-20 06:14:16 +000097{
Daniel Veillard27d88741999-05-29 11:51:49 +000098 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillardb05deb71999-08-10 19:04:08 +000099 xmlParserInputPtr input;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000100 va_list args;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000101
Daniel Veillardb05deb71999-08-10 19:04:08 +0000102 input = ctxt->input;
103 if ((input->filename == NULL) && (ctxt->inputNr > 1))
104 input = ctxt->inputTab[ctxt->inputNr - 2];
105
106 xmlParserPrintFileInfo(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000107
108 fprintf(stderr, "warning: ");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000109 va_start(args, msg);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000110 vfprintf(stderr, msg, args);
Daniel Veillard64068b31999-03-24 20:42:16 +0000111 va_end(args);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000112
113 xmlParserPrintFileContext(input);
114}
115
116/**
117 * xmlParserValidityError:
118 * @ctx: an XML parser context
119 * @msg: the message to display/transmit
120 * @...: extra parameters for the message display
121 *
122 * Display and format an validity error messages, gives file,
123 * line, position and extra parameters.
124 */
125void
126xmlParserValidityError(void *ctx, const char *msg, ...)
127{
128 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
129 xmlParserInputPtr input;
130 va_list args;
131
132 input = ctxt->input;
133 if ((input->filename == NULL) && (ctxt->inputNr > 1))
134 input = ctxt->inputTab[ctxt->inputNr - 2];
135
136 xmlParserPrintFileInfo(input);
137
138 fprintf(stderr, "validity error: ");
139 va_start(args, msg);
140 vfprintf(stderr, msg, args);
141 va_end(args);
142
143 xmlParserPrintFileContext(input);
144}
145
146/**
147 * xmlParserValidityWarning:
148 * @ctx: an XML parser context
149 * @msg: the message to display/transmit
150 * @...: extra parameters for the message display
151 *
152 * Display and format a validity warning messages, gives file, line,
153 * position and extra parameters.
154 */
155void
156xmlParserValidityWarning(void *ctx, const char *msg, ...)
157{
158 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
159 xmlParserInputPtr input;
160 va_list args;
161
162 input = ctxt->input;
163 if ((input->filename == NULL) && (ctxt->inputNr > 1))
164 input = ctxt->inputTab[ctxt->inputNr - 2];
165
166 xmlParserPrintFileInfo(input);
167
168 fprintf(stderr, "validity warning: ");
169 va_start(args, msg);
170 vfprintf(stderr, msg, args);
171 va_end(args);
172
173 xmlParserPrintFileContext(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000174}
Daniel Veillard97b58771998-10-20 06:14:16 +0000175