missing project/build files
[client-tools.git] / src / external / 3rd / library / libxml / error.c
blobd6266af05165473810b5d546fa195a7d730d43ff
1 /*
2 * error.c: module displaying/handling XML parser errors
4 * See Copyright for the status of this software.
6 * Daniel Veillard <daniel@veillard.com>
7 */
9 #define IN_LIBXML
10 #include "libxml.h"
12 #include <stdarg.h>
13 #include <libxml/parser.h>
14 #include <libxml/xmlerror.h>
15 #include <libxml/xmlmemory.h>
16 #include <libxml/globals.h>
18 void xmlGenericErrorDefaultFunc (void *ctx ATTRIBUTE_UNUSED,
19 const char *msg,
20 ...);
22 #define XML_GET_VAR_STR(msg, str) { \
23 int size; \
24 int chars; \
25 char *larger; \
26 va_list ap; \
28 str = (char *) xmlMalloc(150); \
29 if (str == NULL) \
30 return; \
32 size = 150; \
34 while (1) { \
35 va_start(ap, msg); \
36 chars = vsnprintf(str, size, msg, ap); \
37 va_end(ap); \
38 if ((chars > -1) && (chars < size)) \
39 break; \
40 if (chars > -1) \
41 size += chars + 1; \
42 else \
43 size += 100; \
44 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
45 xmlFree(str); \
46 return; \
47 } \
48 str = larger; \
49 } \
52 /************************************************************************
53 * *
54 * Handling of out of context errors *
55 * *
56 ************************************************************************/
58 /**
59 * xmlGenericErrorDefaultFunc:
60 * @ctx: an error context
61 * @msg: the message to display/transmit
62 * @...: extra parameters for the message display
64 * Default handler for out of context error messages.
66 void
67 xmlGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
68 va_list args;
70 if (xmlGenericErrorContext == NULL)
71 xmlGenericErrorContext = (void *) stderr;
73 va_start(args, msg);
74 vfprintf((FILE *)xmlGenericErrorContext, msg, args);
75 va_end(args);
78 /**
79 * initGenericErrorDefaultFunc:
80 * @handler: the handler
82 * Set or reset (if NULL) the default handler for generic errors
84 void
85 initGenericErrorDefaultFunc(xmlGenericErrorFunc * handler)
87 if (handler == NULL)
88 xmlGenericError = xmlGenericErrorDefaultFunc;
89 else
90 (*handler) = xmlGenericErrorDefaultFunc;
93 /**
94 * xmlSetGenericErrorFunc:
95 * @ctx: the new error handling context
96 * @handler: the new handler function
98 * Function to reset the handler and the error context for out of
99 * context error messages.
100 * This simply means that @handler will be called for subsequent
101 * error messages while not parsing nor validating. And @ctx will
102 * be passed as first argument to @handler
103 * One can simply force messages to be emitted to another FILE * than
104 * stderr by setting @ctx to this file handle and @handler to NULL.
106 void
107 xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
108 xmlGenericErrorContext = ctx;
109 if (handler != NULL)
110 xmlGenericError = handler;
111 else
112 xmlGenericError = xmlGenericErrorDefaultFunc;
115 /************************************************************************
117 * Handling of parsing errors *
119 ************************************************************************/
122 * xmlParserPrintFileInfo:
123 * @input: an xmlParserInputPtr input
125 * Displays the associated file and line informations for the current input
128 void
129 xmlParserPrintFileInfo(xmlParserInputPtr input) {
130 if (input != NULL) {
131 if (input->filename)
132 xmlGenericError(xmlGenericErrorContext,
133 "%s:%d: ", input->filename,
134 input->line);
135 else
136 xmlGenericError(xmlGenericErrorContext,
137 "Entity: line %d: ", input->line);
142 * xmlParserPrintFileContext:
143 * @input: an xmlParserInputPtr input
145 * Displays current context within the input content for error tracking
148 void
149 xmlParserPrintFileContext(xmlParserInputPtr input) {
150 const xmlChar *cur, *base;
151 int n;
152 xmlChar content[81];
153 xmlChar *ctnt;
155 if (input == NULL) return;
156 cur = input->cur;
157 base = input->base;
158 /* skip backwards over any end-of-lines */
159 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
160 cur--;
162 n = 0;
163 /* search backwards for beginning-of-line maximum 80 characters */
164 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
165 cur--;
166 if ((*cur == '\n') || (*cur == '\r')) cur++;
167 /* search forward for end-of-line maximum 80 characters */
168 n = 0;
169 ctnt = content;
170 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
171 *ctnt++ = *cur++;
172 n++;
174 *ctnt = 0;
175 xmlGenericError(xmlGenericErrorContext,"%s\n", content);
176 /* create blank line with problem pointer */
177 cur = input->cur;
178 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
179 cur--;
181 n = 0;
182 ctnt = content;
183 while ((n++ < 79) && (cur > base) && (*cur != '\n') && (*cur != '\r')) {
184 *ctnt++ = ' ';
185 cur--;
187 if (ctnt > content) {
188 *(--ctnt) = '^';
189 *(++ctnt) = 0;
190 } else {
191 *ctnt = '^';
192 *(++ctnt) = 0;
194 xmlGenericError(xmlGenericErrorContext,"%s\n", content);
197 #if 0
199 * xmlGetVarStr:
200 * @msg: the message format
201 * @args: a va_list argument list
203 * SGS contribution
204 * Get an arbitrary-sized string for an error argument
205 * The caller must free() the returned string
207 static char *
208 xmlGetVarStr(const char * msg, va_list args) {
209 int size;
210 int length;
211 int chars, left;
212 char *str, *larger;
213 va_list ap;
215 str = (char *) xmlMalloc(150);
216 if (str == NULL)
217 return(NULL);
219 size = 150;
220 length = 0;
222 while (1) {
223 left = size - length;
224 /* Try to print in the allocated space. */
225 va_start(msg, ap);
226 chars = vsnprintf(str + length, left, msg, ap);
227 va_end(ap);
228 /* If that worked, we're done. */
229 if ((chars > -1) && (chars < left ))
230 break;
231 /* Else try again with more space. */
232 if (chars > -1) /* glibc 2.1 */
233 size += chars + 1; /* precisely what is needed */
234 else /* glibc 2.0 */
235 size += 100;
236 if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
237 xmlFree(str);
238 return(NULL);
240 str = larger;
242 return(str);
244 #endif
247 * xmlParserError:
248 * @ctx: an XML parser context
249 * @msg: the message to display/transmit
250 * @...: extra parameters for the message display
252 * Display and format an error messages, gives file, line, position and
253 * extra parameters.
255 void
256 xmlParserError(void *ctx, const char *msg, ...)
258 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
259 xmlParserInputPtr input = NULL;
260 xmlParserInputPtr cur = NULL;
261 char * str;
263 if (ctxt != NULL) {
264 input = ctxt->input;
265 if ((input != NULL) && (input->filename == NULL) &&
266 (ctxt->inputNr > 1)) {
267 cur = input;
268 input = ctxt->inputTab[ctxt->inputNr - 2];
270 xmlParserPrintFileInfo(input);
273 xmlGenericError(xmlGenericErrorContext, "error: ");
274 XML_GET_VAR_STR(msg, str);
275 xmlGenericError(xmlGenericErrorContext, "%s", str);
276 if (str != NULL)
277 xmlFree(str);
279 if (ctxt != NULL) {
280 xmlParserPrintFileContext(input);
281 if (cur != NULL) {
282 xmlParserPrintFileInfo(cur);
283 xmlGenericError(xmlGenericErrorContext, "\n");
284 xmlParserPrintFileContext(cur);
290 * xmlParserWarning:
291 * @ctx: an XML parser context
292 * @msg: the message to display/transmit
293 * @...: extra parameters for the message display
295 * Display and format a warning messages, gives file, line, position and
296 * extra parameters.
298 void
299 xmlParserWarning(void *ctx, const char *msg, ...)
301 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
302 xmlParserInputPtr input = NULL;
303 xmlParserInputPtr cur = NULL;
304 char * str;
306 if (ctxt != NULL) {
307 input = ctxt->input;
308 if ((input != NULL) && (input->filename == NULL) &&
309 (ctxt->inputNr > 1)) {
310 cur = input;
311 input = ctxt->inputTab[ctxt->inputNr - 2];
313 xmlParserPrintFileInfo(input);
316 xmlGenericError(xmlGenericErrorContext, "warning: ");
317 XML_GET_VAR_STR(msg, str);
318 xmlGenericError(xmlGenericErrorContext, "%s", str);
319 if (str != NULL)
320 xmlFree(str);
322 if (ctxt != NULL) {
323 xmlParserPrintFileContext(input);
324 if (cur != NULL) {
325 xmlParserPrintFileInfo(cur);
326 xmlGenericError(xmlGenericErrorContext, "\n");
327 xmlParserPrintFileContext(cur);
332 /************************************************************************
334 * Handling of validation errors *
336 ************************************************************************/
339 * xmlParserValidityError:
340 * @ctx: an XML parser context
341 * @msg: the message to display/transmit
342 * @...: extra parameters for the message display
344 * Display and format an validity error messages, gives file,
345 * line, position and extra parameters.
347 void
348 xmlParserValidityError(void *ctx, const char *msg, ...)
350 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
351 xmlParserInputPtr input = NULL;
352 char * str;
353 int len = xmlStrlen((const xmlChar *) msg);
354 static int had_info = 0;
355 int need_context = 0;
357 if ((len > 1) && (msg[len - 2] != ':')) {
358 if (ctxt != NULL) {
359 input = ctxt->input;
360 if ((input->filename == NULL) && (ctxt->inputNr > 1))
361 input = ctxt->inputTab[ctxt->inputNr - 2];
363 if (had_info == 0) {
364 xmlParserPrintFileInfo(input);
367 xmlGenericError(xmlGenericErrorContext, "validity error: ");
368 need_context = 1;
369 had_info = 0;
370 } else {
371 had_info = 1;
374 XML_GET_VAR_STR(msg, str);
375 xmlGenericError(xmlGenericErrorContext, "%s", str);
376 if (str != NULL)
377 xmlFree(str);
379 if ((ctxt != NULL) && (input != NULL)) {
380 xmlParserPrintFileContext(input);
385 * xmlParserValidityWarning:
386 * @ctx: an XML parser context
387 * @msg: the message to display/transmit
388 * @...: extra parameters for the message display
390 * Display and format a validity warning messages, gives file, line,
391 * position and extra parameters.
393 void
394 xmlParserValidityWarning(void *ctx, const char *msg, ...)
396 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
397 xmlParserInputPtr input = NULL;
398 char * str;
399 int len = xmlStrlen((const xmlChar *) msg);
401 if ((ctxt != NULL) && (len != 0) && (msg[len - 1] != ':')) {
402 input = ctxt->input;
403 if ((input->filename == NULL) && (ctxt->inputNr > 1))
404 input = ctxt->inputTab[ctxt->inputNr - 2];
406 xmlParserPrintFileInfo(input);
409 xmlGenericError(xmlGenericErrorContext, "validity warning: ");
410 XML_GET_VAR_STR(msg, str);
411 xmlGenericError(xmlGenericErrorContext, "%s", str);
412 if (str != NULL)
413 xmlFree(str);
415 if (ctxt != NULL) {
416 xmlParserPrintFileContext(input);