blob: 4a124db42c5af71ff10b9e69cf105308e5860479 [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>
Daniel Veillard361d8452000-04-03 19:48:13 +000017#include <libxml/parser.h>
Daniel Veillard260a68f1998-08-13 03:39:55 +000018
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 Veillard686d6b62000-01-03 11:08:02 +000049 if (input == NULL) return;
Daniel Veillardb05deb71999-08-10 19:04:08 +000050 cur = input->cur;
51 base = input->base;
Daniel Veillard1e346af1999-02-22 10:33:01 +000052 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000053 cur--;
Daniel Veillard260a68f1998-08-13 03:39:55 +000054 }
55 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000056 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
Daniel Veillard260a68f1998-08-13 03:39:55 +000057 cur--;
58 if ((*cur == '\n') || (*cur == '\r')) cur++;
59 base = cur;
60 n = 0;
61 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
62 fprintf(stderr, "%c", (unsigned char) *cur++);
63 n++;
64 }
65 fprintf(stderr, "\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +000066 cur = input->cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +000067 while ((*cur == '\n') || (*cur == '\r'))
68 cur--;
69 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000070 while ((cur != base) && (n++ < 80)) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000071 fprintf(stderr, " ");
72 base++;
73 }
74 fprintf(stderr,"^\n");
75}
76
Daniel Veillard97b58771998-10-20 06:14:16 +000077/**
Daniel Veillardb05deb71999-08-10 19:04:08 +000078 * xmlParserError:
79 * @ctx: an XML parser context
80 * @msg: the message to display/transmit
81 * @...: extra parameters for the message display
82 *
83 * Display and format an error messages, gives file, line, position and
84 * extra parameters.
85 */
86void
87xmlParserError(void *ctx, const char *msg, ...)
88{
89 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
90 xmlParserInputPtr input;
Daniel Veillardb96e6431999-08-29 21:02:19 +000091 xmlParserInputPtr cur = NULL;
Daniel Veillardb05deb71999-08-10 19:04:08 +000092 va_list args;
93
94 input = ctxt->input;
Daniel Veillard686d6b62000-01-03 11:08:02 +000095 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardb96e6431999-08-29 21:02:19 +000096 cur = input;
Daniel Veillardb05deb71999-08-10 19:04:08 +000097 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardb96e6431999-08-29 21:02:19 +000098 }
Daniel Veillardb05deb71999-08-10 19:04:08 +000099
100 xmlParserPrintFileInfo(input);
101
102 fprintf(stderr, "error: ");
103 va_start(args, msg);
104 vfprintf(stderr, msg, args);
105 va_end(args);
106
107 xmlParserPrintFileContext(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000108 if (cur != NULL) {
109 xmlParserPrintFileInfo(cur);
110 fprintf(stderr, "\n");
111 xmlParserPrintFileContext(cur);
112 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000113}
114
115/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000116 * xmlParserWarning:
Daniel Veillard011b63c1999-06-02 17:44:04 +0000117 * @ctx: an XML parser context
Daniel Veillard97b58771998-10-20 06:14:16 +0000118 * @msg: the message to display/transmit
119 * @...: extra parameters for the message display
120 *
121 * Display and format a warning messages, gives file, line, position and
122 * extra parameters.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000123 */
Daniel Veillard97b58771998-10-20 06:14:16 +0000124void
Daniel Veillard27d88741999-05-29 11:51:49 +0000125xmlParserWarning(void *ctx, const char *msg, ...)
Daniel Veillard97b58771998-10-20 06:14:16 +0000126{
Daniel Veillard27d88741999-05-29 11:51:49 +0000127 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000128 xmlParserInputPtr input;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000129 xmlParserInputPtr cur = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000130 va_list args;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000131
Daniel Veillardb05deb71999-08-10 19:04:08 +0000132 input = ctxt->input;
Daniel Veillard686d6b62000-01-03 11:08:02 +0000133 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000134 cur = input;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000135 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardb96e6431999-08-29 21:02:19 +0000136 }
137
Daniel Veillardb05deb71999-08-10 19:04:08 +0000138
139 xmlParserPrintFileInfo(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000140
141 fprintf(stderr, "warning: ");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000142 va_start(args, msg);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000143 vfprintf(stderr, msg, args);
Daniel Veillard64068b31999-03-24 20:42:16 +0000144 va_end(args);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000145
146 xmlParserPrintFileContext(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000147 if (cur != NULL) {
148 xmlParserPrintFileInfo(cur);
149 fprintf(stderr, "\n");
150 xmlParserPrintFileContext(cur);
151 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000152}
153
154/**
155 * xmlParserValidityError:
156 * @ctx: an XML parser context
157 * @msg: the message to display/transmit
158 * @...: extra parameters for the message display
159 *
160 * Display and format an validity error messages, gives file,
161 * line, position and extra parameters.
162 */
163void
164xmlParserValidityError(void *ctx, const char *msg, ...)
165{
166 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
167 xmlParserInputPtr input;
168 va_list args;
169
170 input = ctxt->input;
171 if ((input->filename == NULL) && (ctxt->inputNr > 1))
172 input = ctxt->inputTab[ctxt->inputNr - 2];
173
174 xmlParserPrintFileInfo(input);
175
176 fprintf(stderr, "validity error: ");
177 va_start(args, msg);
178 vfprintf(stderr, msg, args);
179 va_end(args);
180
181 xmlParserPrintFileContext(input);
182}
183
184/**
185 * xmlParserValidityWarning:
186 * @ctx: an XML parser context
187 * @msg: the message to display/transmit
188 * @...: extra parameters for the message display
189 *
190 * Display and format a validity warning messages, gives file, line,
191 * position and extra parameters.
192 */
193void
194xmlParserValidityWarning(void *ctx, const char *msg, ...)
195{
196 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
197 xmlParserInputPtr input;
198 va_list args;
199
200 input = ctxt->input;
201 if ((input->filename == NULL) && (ctxt->inputNr > 1))
202 input = ctxt->inputTab[ctxt->inputNr - 2];
203
204 xmlParserPrintFileInfo(input);
205
206 fprintf(stderr, "validity warning: ");
207 va_start(args, msg);
208 vfprintf(stderr, msg, args);
209 va_end(args);
210
211 xmlParserPrintFileContext(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000212}
Daniel Veillard97b58771998-10-20 06:14:16 +0000213