blob: b51aebeb456ef6f802326dde9031875c9fbbdd11 [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;
Daniel Veillard39c7d712000-09-10 16:14:55 +000090 xmlParserInputPtr input = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +000091 xmlParserInputPtr cur = NULL;
Daniel Veillardb05deb71999-08-10 19:04:08 +000092 va_list args;
93
Daniel Veillard39c7d712000-09-10 16:14:55 +000094 if (ctxt != NULL) {
95 input = ctxt->input;
96 if ((input != NULL) && (input->filename == NULL) &&
97 (ctxt->inputNr > 1)) {
98 cur = input;
99 input = ctxt->inputTab[ctxt->inputNr - 2];
100 }
101 xmlParserPrintFileInfo(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000102 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000103
104 fprintf(stderr, "error: ");
105 va_start(args, msg);
106 vfprintf(stderr, msg, args);
107 va_end(args);
108
Daniel Veillard39c7d712000-09-10 16:14:55 +0000109 if (ctxt != NULL) {
110 xmlParserPrintFileContext(input);
111 if (cur != NULL) {
112 xmlParserPrintFileInfo(cur);
113 fprintf(stderr, "\n");
114 xmlParserPrintFileContext(cur);
115 }
Daniel Veillardb96e6431999-08-29 21:02:19 +0000116 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000117}
118
119/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000120 * xmlParserWarning:
Daniel Veillard011b63c1999-06-02 17:44:04 +0000121 * @ctx: an XML parser context
Daniel Veillard97b58771998-10-20 06:14:16 +0000122 * @msg: the message to display/transmit
123 * @...: extra parameters for the message display
124 *
125 * Display and format a warning messages, gives file, line, position and
126 * extra parameters.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000127 */
Daniel Veillard97b58771998-10-20 06:14:16 +0000128void
Daniel Veillard27d88741999-05-29 11:51:49 +0000129xmlParserWarning(void *ctx, const char *msg, ...)
Daniel Veillard97b58771998-10-20 06:14:16 +0000130{
Daniel Veillard27d88741999-05-29 11:51:49 +0000131 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard39c7d712000-09-10 16:14:55 +0000132 xmlParserInputPtr input = NULL;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000133 xmlParserInputPtr cur = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000134 va_list args;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000135
Daniel Veillard39c7d712000-09-10 16:14:55 +0000136 if (ctxt != NULL) {
137 input = ctxt->input;
138 if ((input != NULL) && (input->filename == NULL) &&
139 (ctxt->inputNr > 1)) {
140 cur = input;
141 input = ctxt->inputTab[ctxt->inputNr - 2];
142 }
143 xmlParserPrintFileInfo(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000144 }
145
Daniel Veillard260a68f1998-08-13 03:39:55 +0000146 fprintf(stderr, "warning: ");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000147 va_start(args, msg);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000148 vfprintf(stderr, msg, args);
Daniel Veillard64068b31999-03-24 20:42:16 +0000149 va_end(args);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000150
Daniel Veillard39c7d712000-09-10 16:14:55 +0000151 if (ctxt != NULL) {
152 xmlParserPrintFileContext(input);
153 if (cur != NULL) {
154 xmlParserPrintFileInfo(cur);
155 fprintf(stderr, "\n");
156 xmlParserPrintFileContext(cur);
157 }
Daniel Veillardb96e6431999-08-29 21:02:19 +0000158 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000159}
160
161/**
162 * xmlParserValidityError:
163 * @ctx: an XML parser context
164 * @msg: the message to display/transmit
165 * @...: extra parameters for the message display
166 *
167 * Display and format an validity error messages, gives file,
168 * line, position and extra parameters.
169 */
170void
171xmlParserValidityError(void *ctx, const char *msg, ...)
172{
173 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard39c7d712000-09-10 16:14:55 +0000174 xmlParserInputPtr input = NULL;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000175 va_list args;
176
Daniel Veillard39c7d712000-09-10 16:14:55 +0000177 if (ctxt != NULL) {
178 input = ctxt->input;
179 if ((input->filename == NULL) && (ctxt->inputNr > 1))
180 input = ctxt->inputTab[ctxt->inputNr - 2];
181
182 xmlParserPrintFileInfo(input);
183 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000184
185 fprintf(stderr, "validity error: ");
186 va_start(args, msg);
187 vfprintf(stderr, msg, args);
188 va_end(args);
189
Daniel Veillard39c7d712000-09-10 16:14:55 +0000190 if (ctxt != NULL) {
191 xmlParserPrintFileContext(input);
192 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000193}
194
195/**
196 * xmlParserValidityWarning:
197 * @ctx: an XML parser context
198 * @msg: the message to display/transmit
199 * @...: extra parameters for the message display
200 *
201 * Display and format a validity warning messages, gives file, line,
202 * position and extra parameters.
203 */
204void
205xmlParserValidityWarning(void *ctx, const char *msg, ...)
206{
207 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard39c7d712000-09-10 16:14:55 +0000208 xmlParserInputPtr input = NULL;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000209 va_list args;
210
Daniel Veillard39c7d712000-09-10 16:14:55 +0000211 if (ctxt != NULL) {
212 input = ctxt->input;
213 if ((input->filename == NULL) && (ctxt->inputNr > 1))
214 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardb05deb71999-08-10 19:04:08 +0000215
Daniel Veillard39c7d712000-09-10 16:14:55 +0000216 xmlParserPrintFileInfo(input);
217 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000218
219 fprintf(stderr, "validity warning: ");
220 va_start(args, msg);
221 vfprintf(stderr, msg, args);
222 va_end(args);
223
Daniel Veillard39c7d712000-09-10 16:14:55 +0000224 if (ctxt != NULL) {
225 xmlParserPrintFileContext(input);
226 }
Daniel Veillard260a68f1998-08-13 03:39:55 +0000227}
Daniel Veillard97b58771998-10-20 06:14:16 +0000228