blob: 18fdbd0f7e74cbe9a887662e5c9c27d53618b9ee [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
Daniel Veillard3c558c31999-12-22 11:30:41 +00009#ifdef WIN32
10#include "win32config.h"
11#else
12#include "config.h"
13#endif
14
Daniel Veillard260a68f1998-08-13 03:39:55 +000015#include <stdio.h>
16#include <stdarg.h>
17#include "parser.h"
18
Daniel Veillardb96e6431999-08-29 21:02:19 +000019/**
20 * xmlParserPrintFileInfo:
21 * @input: an xmlParserInputPtr input
22 *
23 * Displays the associated file and line informations for the current input
24 */
25
26void
Daniel Veillardb05deb71999-08-10 19:04:08 +000027xmlParserPrintFileInfo(xmlParserInputPtr input) {
28 if (input != NULL) {
29 if (input->filename)
30 fprintf(stderr, "%s:%d: ", input->filename,
31 input->line);
32 else
Daniel Veillardb96e6431999-08-29 21:02:19 +000033 fprintf(stderr, "Entity: line %d: ", input->line);
Daniel Veillardb05deb71999-08-10 19:04:08 +000034 }
35}
36
Daniel Veillardb96e6431999-08-29 21:02:19 +000037/**
38 * xmlParserPrintFileContext:
39 * @input: an xmlParserInputPtr input
40 *
41 * Displays current context within the input content for error tracking
42 */
43
44void
Daniel Veillardb05deb71999-08-10 19:04:08 +000045xmlParserPrintFileContext(xmlParserInputPtr input) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000046 const xmlChar *cur, *base;
Daniel Veillard260a68f1998-08-13 03:39:55 +000047 int n;
48
Daniel Veillardb05deb71999-08-10 19:04:08 +000049 cur = input->cur;
50 base = input->base;
Daniel Veillard1e346af1999-02-22 10:33:01 +000051 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000052 cur--;
Daniel Veillard260a68f1998-08-13 03:39:55 +000053 }
54 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000055 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
Daniel Veillard260a68f1998-08-13 03:39:55 +000056 cur--;
57 if ((*cur == '\n') || (*cur == '\r')) cur++;
58 base = cur;
59 n = 0;
60 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
61 fprintf(stderr, "%c", (unsigned char) *cur++);
62 n++;
63 }
64 fprintf(stderr, "\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +000065 cur = input->cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +000066 while ((*cur == '\n') || (*cur == '\r'))
67 cur--;
68 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000069 while ((cur != base) && (n++ < 80)) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000070 fprintf(stderr, " ");
71 base++;
72 }
73 fprintf(stderr,"^\n");
74}
75
Daniel Veillard97b58771998-10-20 06:14:16 +000076/**
Daniel Veillardb05deb71999-08-10 19:04:08 +000077 * xmlParserError:
78 * @ctx: an XML parser context
79 * @msg: the message to display/transmit
80 * @...: extra parameters for the message display
81 *
82 * Display and format an error messages, gives file, line, position and
83 * extra parameters.
84 */
85void
86xmlParserError(void *ctx, const char *msg, ...)
87{
88 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
89 xmlParserInputPtr input;
Daniel Veillardb96e6431999-08-29 21:02:19 +000090 xmlParserInputPtr cur = NULL;
Daniel Veillardb05deb71999-08-10 19:04:08 +000091 va_list args;
92
93 input = ctxt->input;
Daniel Veillardb96e6431999-08-29 21:02:19 +000094 if ((input->filename == NULL) && (ctxt->inputNr > 1)) {
95 cur = input;
Daniel Veillardb05deb71999-08-10 19:04:08 +000096 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardb96e6431999-08-29 21:02:19 +000097 }
Daniel Veillardb05deb71999-08-10 19:04:08 +000098
99 xmlParserPrintFileInfo(input);
100
101 fprintf(stderr, "error: ");
102 va_start(args, msg);
103 vfprintf(stderr, msg, args);
104 va_end(args);
105
106 xmlParserPrintFileContext(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000107 if (cur != NULL) {
108 xmlParserPrintFileInfo(cur);
109 fprintf(stderr, "\n");
110 xmlParserPrintFileContext(cur);
111 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000112}
113
114/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000115 * xmlParserWarning:
Daniel Veillard011b63c1999-06-02 17:44:04 +0000116 * @ctx: an XML parser context
Daniel Veillard97b58771998-10-20 06:14:16 +0000117 * @msg: the message to display/transmit
118 * @...: extra parameters for the message display
119 *
120 * Display and format a warning messages, gives file, line, position and
121 * extra parameters.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000122 */
Daniel Veillard97b58771998-10-20 06:14:16 +0000123void
Daniel Veillard27d88741999-05-29 11:51:49 +0000124xmlParserWarning(void *ctx, const char *msg, ...)
Daniel Veillard97b58771998-10-20 06:14:16 +0000125{
Daniel Veillard27d88741999-05-29 11:51:49 +0000126 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000127 xmlParserInputPtr input;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000128 xmlParserInputPtr cur = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000129 va_list args;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000130
Daniel Veillardb05deb71999-08-10 19:04:08 +0000131 input = ctxt->input;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000132 if ((input->filename == NULL) && (ctxt->inputNr > 1)) {
133 cur = input;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000134 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardb96e6431999-08-29 21:02:19 +0000135 }
136
Daniel Veillardb05deb71999-08-10 19:04:08 +0000137
138 xmlParserPrintFileInfo(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000139
140 fprintf(stderr, "warning: ");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000141 va_start(args, msg);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000142 vfprintf(stderr, msg, args);
Daniel Veillard64068b31999-03-24 20:42:16 +0000143 va_end(args);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000144
145 xmlParserPrintFileContext(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000146 if (cur != NULL) {
147 xmlParserPrintFileInfo(cur);
148 fprintf(stderr, "\n");
149 xmlParserPrintFileContext(cur);
150 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000151}
152
153/**
154 * xmlParserValidityError:
155 * @ctx: an XML parser context
156 * @msg: the message to display/transmit
157 * @...: extra parameters for the message display
158 *
159 * Display and format an validity error messages, gives file,
160 * line, position and extra parameters.
161 */
162void
163xmlParserValidityError(void *ctx, const char *msg, ...)
164{
165 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
166 xmlParserInputPtr input;
167 va_list args;
168
169 input = ctxt->input;
170 if ((input->filename == NULL) && (ctxt->inputNr > 1))
171 input = ctxt->inputTab[ctxt->inputNr - 2];
172
173 xmlParserPrintFileInfo(input);
174
175 fprintf(stderr, "validity error: ");
176 va_start(args, msg);
177 vfprintf(stderr, msg, args);
178 va_end(args);
179
180 xmlParserPrintFileContext(input);
181}
182
183/**
184 * xmlParserValidityWarning:
185 * @ctx: an XML parser context
186 * @msg: the message to display/transmit
187 * @...: extra parameters for the message display
188 *
189 * Display and format a validity warning messages, gives file, line,
190 * position and extra parameters.
191 */
192void
193xmlParserValidityWarning(void *ctx, const char *msg, ...)
194{
195 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
196 xmlParserInputPtr input;
197 va_list args;
198
199 input = ctxt->input;
200 if ((input->filename == NULL) && (ctxt->inputNr > 1))
201 input = ctxt->inputTab[ctxt->inputNr - 2];
202
203 xmlParserPrintFileInfo(input);
204
205 fprintf(stderr, "validity warning: ");
206 va_start(args, msg);
207 vfprintf(stderr, msg, args);
208 va_end(args);
209
210 xmlParserPrintFileContext(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000211}
Daniel Veillard97b58771998-10-20 06:14:16 +0000212