blob: b77839e885912b67139d79cb7cbd2b6f15aa94fd [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 Veillardb96e6431999-08-29 21:02:19 +000013/**
14 * xmlParserPrintFileInfo:
15 * @input: an xmlParserInputPtr input
16 *
17 * Displays the associated file and line informations for the current input
18 */
19
20void
Daniel Veillardb05deb71999-08-10 19:04:08 +000021xmlParserPrintFileInfo(xmlParserInputPtr input) {
22 if (input != NULL) {
23 if (input->filename)
24 fprintf(stderr, "%s:%d: ", input->filename,
25 input->line);
26 else
Daniel Veillardb96e6431999-08-29 21:02:19 +000027 fprintf(stderr, "Entity: line %d: ", input->line);
Daniel Veillardb05deb71999-08-10 19:04:08 +000028 }
29}
30
Daniel Veillardb96e6431999-08-29 21:02:19 +000031/**
32 * xmlParserPrintFileContext:
33 * @input: an xmlParserInputPtr input
34 *
35 * Displays current context within the input content for error tracking
36 */
37
38void
Daniel Veillardb05deb71999-08-10 19:04:08 +000039xmlParserPrintFileContext(xmlParserInputPtr input) {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000040 const xmlChar *cur, *base;
Daniel Veillard260a68f1998-08-13 03:39:55 +000041 int n;
42
Daniel Veillardb05deb71999-08-10 19:04:08 +000043 cur = input->cur;
44 base = input->base;
Daniel Veillard1e346af1999-02-22 10:33:01 +000045 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000046 cur--;
Daniel Veillard260a68f1998-08-13 03:39:55 +000047 }
48 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000049 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
Daniel Veillard260a68f1998-08-13 03:39:55 +000050 cur--;
51 if ((*cur == '\n') || (*cur == '\r')) cur++;
52 base = cur;
53 n = 0;
54 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
55 fprintf(stderr, "%c", (unsigned char) *cur++);
56 n++;
57 }
58 fprintf(stderr, "\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +000059 cur = input->cur;
Daniel Veillard260a68f1998-08-13 03:39:55 +000060 while ((*cur == '\n') || (*cur == '\r'))
61 cur--;
62 n = 0;
Daniel Veillardbc50b591999-03-01 12:28:53 +000063 while ((cur != base) && (n++ < 80)) {
Daniel Veillard260a68f1998-08-13 03:39:55 +000064 fprintf(stderr, " ");
65 base++;
66 }
67 fprintf(stderr,"^\n");
68}
69
Daniel Veillard97b58771998-10-20 06:14:16 +000070/**
Daniel Veillardb05deb71999-08-10 19:04:08 +000071 * xmlParserError:
72 * @ctx: an XML parser context
73 * @msg: the message to display/transmit
74 * @...: extra parameters for the message display
75 *
76 * Display and format an error messages, gives file, line, position and
77 * extra parameters.
78 */
79void
80xmlParserError(void *ctx, const char *msg, ...)
81{
82 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
83 xmlParserInputPtr input;
Daniel Veillardb96e6431999-08-29 21:02:19 +000084 xmlParserInputPtr cur = NULL;
Daniel Veillardb05deb71999-08-10 19:04:08 +000085 va_list args;
86
87 input = ctxt->input;
Daniel Veillardb96e6431999-08-29 21:02:19 +000088 if ((input->filename == NULL) && (ctxt->inputNr > 1)) {
89 cur = input;
Daniel Veillardb05deb71999-08-10 19:04:08 +000090 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardb96e6431999-08-29 21:02:19 +000091 }
Daniel Veillardb05deb71999-08-10 19:04:08 +000092
93 xmlParserPrintFileInfo(input);
94
95 fprintf(stderr, "error: ");
96 va_start(args, msg);
97 vfprintf(stderr, msg, args);
98 va_end(args);
99
100 xmlParserPrintFileContext(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000101 if (cur != NULL) {
102 xmlParserPrintFileInfo(cur);
103 fprintf(stderr, "\n");
104 xmlParserPrintFileContext(cur);
105 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000106}
107
108/**
Daniel Veillard97b58771998-10-20 06:14:16 +0000109 * xmlParserWarning:
Daniel Veillard011b63c1999-06-02 17:44:04 +0000110 * @ctx: an XML parser context
Daniel Veillard97b58771998-10-20 06:14:16 +0000111 * @msg: the message to display/transmit
112 * @...: extra parameters for the message display
113 *
114 * Display and format a warning messages, gives file, line, position and
115 * extra parameters.
Daniel Veillard260a68f1998-08-13 03:39:55 +0000116 */
Daniel Veillard97b58771998-10-20 06:14:16 +0000117void
Daniel Veillard27d88741999-05-29 11:51:49 +0000118xmlParserWarning(void *ctx, const char *msg, ...)
Daniel Veillard97b58771998-10-20 06:14:16 +0000119{
Daniel Veillard27d88741999-05-29 11:51:49 +0000120 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000121 xmlParserInputPtr input;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000122 xmlParserInputPtr cur = NULL;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000123 va_list args;
Daniel Veillard260a68f1998-08-13 03:39:55 +0000124
Daniel Veillardb05deb71999-08-10 19:04:08 +0000125 input = ctxt->input;
Daniel Veillardb96e6431999-08-29 21:02:19 +0000126 if ((input->filename == NULL) && (ctxt->inputNr > 1)) {
127 cur = input;
Daniel Veillardb05deb71999-08-10 19:04:08 +0000128 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardb96e6431999-08-29 21:02:19 +0000129 }
130
Daniel Veillardb05deb71999-08-10 19:04:08 +0000131
132 xmlParserPrintFileInfo(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000133
134 fprintf(stderr, "warning: ");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000135 va_start(args, msg);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000136 vfprintf(stderr, msg, args);
Daniel Veillard64068b31999-03-24 20:42:16 +0000137 va_end(args);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000138
139 xmlParserPrintFileContext(input);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000140 if (cur != NULL) {
141 xmlParserPrintFileInfo(cur);
142 fprintf(stderr, "\n");
143 xmlParserPrintFileContext(cur);
144 }
Daniel Veillardb05deb71999-08-10 19:04:08 +0000145}
146
147/**
148 * xmlParserValidityError:
149 * @ctx: an XML parser context
150 * @msg: the message to display/transmit
151 * @...: extra parameters for the message display
152 *
153 * Display and format an validity error messages, gives file,
154 * line, position and extra parameters.
155 */
156void
157xmlParserValidityError(void *ctx, const char *msg, ...)
158{
159 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
160 xmlParserInputPtr input;
161 va_list args;
162
163 input = ctxt->input;
164 if ((input->filename == NULL) && (ctxt->inputNr > 1))
165 input = ctxt->inputTab[ctxt->inputNr - 2];
166
167 xmlParserPrintFileInfo(input);
168
169 fprintf(stderr, "validity error: ");
170 va_start(args, msg);
171 vfprintf(stderr, msg, args);
172 va_end(args);
173
174 xmlParserPrintFileContext(input);
175}
176
177/**
178 * xmlParserValidityWarning:
179 * @ctx: an XML parser context
180 * @msg: the message to display/transmit
181 * @...: extra parameters for the message display
182 *
183 * Display and format a validity warning messages, gives file, line,
184 * position and extra parameters.
185 */
186void
187xmlParserValidityWarning(void *ctx, const char *msg, ...)
188{
189 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
190 xmlParserInputPtr input;
191 va_list args;
192
193 input = ctxt->input;
194 if ((input->filename == NULL) && (ctxt->inputNr > 1))
195 input = ctxt->inputTab[ctxt->inputNr - 2];
196
197 xmlParserPrintFileInfo(input);
198
199 fprintf(stderr, "validity warning: ");
200 va_start(args, msg);
201 vfprintf(stderr, msg, args);
202 va_end(args);
203
204 xmlParserPrintFileContext(input);
Daniel Veillard260a68f1998-08-13 03:39:55 +0000205}
Daniel Veillard97b58771998-10-20 06:14:16 +0000206