1 /* Helper routines for parsing XML using Expat.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "exceptions.h"
24 #include "xml-support.h"
26 #include "gdb_string.h"
27 #include "safe-ctype.h"
32 /* The contents of this file are only useful if XML support is
36 #include "gdb_expat.h"
38 /* The maximum depth of <xi:include> nesting. No need to be miserly,
39 we just want to avoid running out of stack on loops. */
40 #define MAX_XINCLUDE_DEPTH 30
42 /* Simplified XML parser infrastructure. */
44 /* A parsing level -- used to keep track of the current element
48 /* Elements we allow at this level. */
49 const struct gdb_xml_element
*elements
;
51 /* The element which we are within. */
52 const struct gdb_xml_element
*element
;
54 /* Mask of which elements we've seen at this level (used for
55 optional and repeatable checking). */
58 /* Body text accumulation. */
61 typedef struct scope_level scope_level_s
;
62 DEF_VEC_O(scope_level_s
);
64 /* The parser itself, and our additional state. */
67 XML_Parser expat_parser
; /* The underlying expat parser. */
69 const char *name
; /* Name of this parser. */
70 void *user_data
; /* The user's callback data, for handlers. */
72 VEC(scope_level_s
) *scopes
; /* Scoping stack. */
74 struct gdb_exception error
; /* A thrown error, if any. */
75 int last_line
; /* The line of the thrown error, or 0. */
77 const char *dtd_name
; /* The name of the expected / default DTD,
79 int is_xinclude
; /* Are we the special <xi:include> parser? */
82 /* Process some body text. We accumulate the text for later use; it's
83 wrong to do anything with it immediately, because a single block of
84 text might be broken up into multiple calls to this function. */
87 gdb_xml_body_text (void *data
, const XML_Char
*text
, int length
)
89 struct gdb_xml_parser
*parser
= data
;
90 struct scope_level
*scope
= VEC_last (scope_level_s
, parser
->scopes
);
92 if (parser
->error
.reason
< 0)
95 if (scope
->body
== NULL
)
97 scope
->body
= XZALLOC (struct obstack
);
98 obstack_init (scope
->body
);
101 obstack_grow (scope
->body
, text
, length
);
104 /* Issue a debugging message from one of PARSER's handlers. */
107 gdb_xml_debug (struct gdb_xml_parser
*parser
, const char *format
, ...)
109 int line
= XML_GetCurrentLineNumber (parser
->expat_parser
);
116 va_start (ap
, format
);
117 message
= xstrvprintf (format
, ap
);
119 fprintf_unfiltered (gdb_stderr
, "%s (line %d): %s\n",
120 parser
->name
, line
, message
);
122 fprintf_unfiltered (gdb_stderr
, "%s: %s\n",
123 parser
->name
, message
);
127 /* Issue an error message from one of PARSER's handlers, and stop
131 gdb_xml_error (struct gdb_xml_parser
*parser
, const char *format
, ...)
133 int line
= XML_GetCurrentLineNumber (parser
->expat_parser
);
136 parser
->last_line
= line
;
137 va_start (ap
, format
);
138 throw_verror (XML_PARSE_ERROR
, format
, ap
);
141 /* Clean up a vector of parsed attribute values. */
144 gdb_xml_values_cleanup (void *data
)
146 VEC(gdb_xml_value_s
) **values
= data
;
147 struct gdb_xml_value
*value
;
150 for (ix
= 0; VEC_iterate (gdb_xml_value_s
, *values
, ix
, value
); ix
++)
151 xfree (value
->value
);
152 VEC_free (gdb_xml_value_s
, *values
);
155 /* Handle the start of an element. DATA is our local XML parser, NAME
156 is the element, and ATTRS are the names and values of this
157 element's attributes. */
160 gdb_xml_start_element (void *data
, const XML_Char
*name
,
161 const XML_Char
**attrs
)
163 struct gdb_xml_parser
*parser
= data
;
164 struct scope_level
*scope
;
165 struct scope_level new_scope
;
166 const struct gdb_xml_element
*element
;
167 const struct gdb_xml_attribute
*attribute
;
168 VEC(gdb_xml_value_s
) *attributes
= NULL
;
170 struct cleanup
*back_to
;
172 /* Push an error scope. If we return or throw an exception before
173 filling this in, it will tell us to ignore children of this
175 VEC_reserve (scope_level_s
, parser
->scopes
, 1);
176 scope
= VEC_last (scope_level_s
, parser
->scopes
);
177 memset (&new_scope
, 0, sizeof (new_scope
));
178 VEC_quick_push (scope_level_s
, parser
->scopes
, &new_scope
);
180 gdb_xml_debug (parser
, _("Entering element <%s>"), name
);
182 /* Find this element in the list of the current scope's allowed
183 children. Record that we've seen it. */
186 for (element
= scope
->elements
; element
&& element
->name
;
187 element
++, seen
<<= 1)
188 if (strcmp (element
->name
, name
) == 0)
191 if (element
== NULL
|| element
->name
== NULL
)
193 /* If we're working on XInclude, <xi:include> can be the child
194 of absolutely anything. Copy the previous scope's element
195 list into the new scope even if there was no match. */
196 if (parser
->is_xinclude
)
198 struct scope_level
*unknown_scope
;
200 XML_DefaultCurrent (parser
->expat_parser
);
202 unknown_scope
= VEC_last (scope_level_s
, parser
->scopes
);
203 unknown_scope
->elements
= scope
->elements
;
207 gdb_xml_debug (parser
, _("Element <%s> unknown"), name
);
211 if (!(element
->flags
& GDB_XML_EF_REPEATABLE
) && (seen
& scope
->seen
))
212 gdb_xml_error (parser
, _("Element <%s> only expected once"), name
);
216 back_to
= make_cleanup (gdb_xml_values_cleanup
, &attributes
);
218 for (attribute
= element
->attributes
;
219 attribute
!= NULL
&& attribute
->name
!= NULL
;
222 const char *val
= NULL
;
225 struct gdb_xml_value new_value
;
227 for (p
= attrs
; *p
!= NULL
; p
+= 2)
228 if (!strcmp (attribute
->name
, p
[0]))
234 if (*p
!= NULL
&& val
== NULL
)
236 gdb_xml_debug (parser
, _("Attribute \"%s\" missing a value"),
241 if (*p
== NULL
&& !(attribute
->flags
& GDB_XML_AF_OPTIONAL
))
243 gdb_xml_error (parser
, _("Required attribute \"%s\" of "
244 "<%s> not specified"),
245 attribute
->name
, element
->name
);
252 gdb_xml_debug (parser
, _("Parsing attribute %s=\"%s\""),
253 attribute
->name
, val
);
255 if (attribute
->handler
)
256 parsed_value
= attribute
->handler (parser
, attribute
, val
);
258 parsed_value
= xstrdup (val
);
260 new_value
.name
= attribute
->name
;
261 new_value
.value
= parsed_value
;
262 VEC_safe_push (gdb_xml_value_s
, attributes
, &new_value
);
265 /* Check for unrecognized attributes. */
270 for (p
= attrs
; *p
!= NULL
; p
+= 2)
272 for (attribute
= element
->attributes
;
273 attribute
!= NULL
&& attribute
->name
!= NULL
;
275 if (strcmp (attribute
->name
, *p
) == 0)
278 if (attribute
== NULL
|| attribute
->name
== NULL
)
279 gdb_xml_debug (parser
, _("Ignoring unknown attribute %s"), *p
);
283 /* Call the element handler if there is one. */
284 if (element
->start_handler
)
285 element
->start_handler (parser
, element
, parser
->user_data
, attributes
);
287 /* Fill in a new scope level. */
288 scope
= VEC_last (scope_level_s
, parser
->scopes
);
289 scope
->element
= element
;
290 scope
->elements
= element
->children
;
292 do_cleanups (back_to
);
295 /* Wrapper for gdb_xml_start_element, to prevent throwing exceptions
299 gdb_xml_start_element_wrapper (void *data
, const XML_Char
*name
,
300 const XML_Char
**attrs
)
302 struct gdb_xml_parser
*parser
= data
;
303 volatile struct gdb_exception ex
;
305 if (parser
->error
.reason
< 0)
308 TRY_CATCH (ex
, RETURN_MASK_ALL
)
310 gdb_xml_start_element (data
, name
, attrs
);
315 #ifdef HAVE_XML_STOPPARSER
316 XML_StopParser (parser
->expat_parser
, XML_FALSE
);
321 /* Handle the end of an element. DATA is our local XML parser, and
322 NAME is the current element. */
325 gdb_xml_end_element (void *data
, const XML_Char
*name
)
327 struct gdb_xml_parser
*parser
= data
;
328 struct scope_level
*scope
= VEC_last (scope_level_s
, parser
->scopes
);
329 const struct gdb_xml_element
*element
;
332 gdb_xml_debug (parser
, _("Leaving element <%s>"), name
);
334 for (element
= scope
->elements
, seen
= 1;
335 element
!= NULL
&& element
->name
!= NULL
;
336 element
++, seen
<<= 1)
337 if ((scope
->seen
& seen
) == 0
338 && (element
->flags
& GDB_XML_EF_OPTIONAL
) == 0)
339 gdb_xml_error (parser
, _("Required element <%s> is missing"),
342 /* Call the element processor. */
343 if (scope
->element
!= NULL
&& scope
->element
->end_handler
)
347 if (scope
->body
== NULL
)
353 length
= obstack_object_size (scope
->body
);
354 obstack_1grow (scope
->body
, '\0');
355 body
= obstack_finish (scope
->body
);
357 /* Strip leading and trailing whitespace. */
358 while (length
> 0 && ISSPACE (body
[length
-1]))
359 body
[--length
] = '\0';
360 while (*body
&& ISSPACE (*body
))
364 scope
->element
->end_handler (parser
, scope
->element
, parser
->user_data
,
367 else if (scope
->element
== NULL
)
368 XML_DefaultCurrent (parser
->expat_parser
);
370 /* Pop the scope level. */
373 obstack_free (scope
->body
, NULL
);
376 VEC_pop (scope_level_s
, parser
->scopes
);
379 /* Wrapper for gdb_xml_end_element, to prevent throwing exceptions
383 gdb_xml_end_element_wrapper (void *data
, const XML_Char
*name
)
385 struct gdb_xml_parser
*parser
= data
;
386 volatile struct gdb_exception ex
;
388 if (parser
->error
.reason
< 0)
391 TRY_CATCH (ex
, RETURN_MASK_ALL
)
393 gdb_xml_end_element (data
, name
);
398 #ifdef HAVE_XML_STOPPARSER
399 XML_StopParser (parser
->expat_parser
, XML_FALSE
);
404 /* Free a parser and all its associated state. */
407 gdb_xml_cleanup (void *arg
)
409 struct gdb_xml_parser
*parser
= arg
;
410 struct scope_level
*scope
;
413 XML_ParserFree (parser
->expat_parser
);
415 /* Clean up the scopes. */
416 for (ix
= 0; VEC_iterate (scope_level_s
, parser
->scopes
, ix
, scope
); ix
++)
419 obstack_free (scope
->body
, NULL
);
422 VEC_free (scope_level_s
, parser
->scopes
);
427 /* Initialize and return a parser. Register a cleanup to destroy the
430 struct gdb_xml_parser
*
431 gdb_xml_create_parser_and_cleanup (const char *name
,
432 const struct gdb_xml_element
*elements
,
435 struct gdb_xml_parser
*parser
;
436 struct scope_level start_scope
;
438 /* Initialize the parser. */
439 parser
= XZALLOC (struct gdb_xml_parser
);
440 parser
->expat_parser
= XML_ParserCreateNS (NULL
, '!');
441 if (parser
->expat_parser
== NULL
)
449 parser
->user_data
= user_data
;
450 XML_SetUserData (parser
->expat_parser
, parser
);
452 /* Set the callbacks. */
453 XML_SetElementHandler (parser
->expat_parser
, gdb_xml_start_element_wrapper
,
454 gdb_xml_end_element_wrapper
);
455 XML_SetCharacterDataHandler (parser
->expat_parser
, gdb_xml_body_text
);
457 /* Initialize the outer scope. */
458 memset (&start_scope
, 0, sizeof (start_scope
));
459 start_scope
.elements
= elements
;
460 VEC_safe_push (scope_level_s
, parser
->scopes
, &start_scope
);
462 make_cleanup (gdb_xml_cleanup
, parser
);
467 /* External entity handler. The only external entities we support
468 are those compiled into GDB (we do not fetch entities from the
472 gdb_xml_fetch_external_entity (XML_Parser expat_parser
,
473 const XML_Char
*context
,
474 const XML_Char
*base
,
475 const XML_Char
*systemId
,
476 const XML_Char
*publicId
)
478 struct gdb_xml_parser
*parser
= XML_GetUserData (expat_parser
);
479 XML_Parser entity_parser
;
481 enum XML_Status status
;
483 if (systemId
== NULL
)
485 text
= fetch_xml_builtin (parser
->dtd_name
);
487 internal_error (__FILE__
, __LINE__
, "could not locate built-in DTD %s",
492 text
= fetch_xml_builtin (systemId
);
494 return XML_STATUS_ERROR
;
497 entity_parser
= XML_ExternalEntityParserCreate (expat_parser
, context
, NULL
);
499 /* Don't use our handlers for the contents of the DTD. Just let expat
501 XML_SetElementHandler (entity_parser
, NULL
, NULL
);
502 XML_SetDoctypeDeclHandler (entity_parser
, NULL
, NULL
);
503 XML_SetXmlDeclHandler (entity_parser
, NULL
);
504 XML_SetDefaultHandler (entity_parser
, NULL
);
505 XML_SetUserData (entity_parser
, NULL
);
507 status
= XML_Parse (entity_parser
, text
, strlen (text
), 1);
509 XML_ParserFree (entity_parser
);
513 /* Associate DTD_NAME, which must be the name of a compiled-in DTD,
517 gdb_xml_use_dtd (struct gdb_xml_parser
*parser
, const char *dtd_name
)
521 parser
->dtd_name
= dtd_name
;
523 XML_SetParamEntityParsing (parser
->expat_parser
,
524 XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE
);
525 XML_SetExternalEntityRefHandler (parser
->expat_parser
,
526 gdb_xml_fetch_external_entity
);
528 /* Even if no DTD is provided, use the built-in DTD anyway. */
529 err
= XML_UseForeignDTD (parser
->expat_parser
, XML_TRUE
);
530 if (err
!= XML_ERROR_NONE
)
531 internal_error (__FILE__
, __LINE__
,
532 "XML_UseForeignDTD failed: %s", XML_ErrorString (err
));
535 /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which
536 should be NUL-terminated.
538 The return value is 0 for success or -1 for error. It may throw,
539 but only if something unexpected goes wrong during parsing; parse
540 errors will be caught, warned about, and reported as failure. */
543 gdb_xml_parse (struct gdb_xml_parser
*parser
, const char *buffer
)
545 enum XML_Status status
;
546 const char *error_string
;
548 gdb_xml_debug (parser
, _("Starting:\n%s"), buffer
);
550 status
= XML_Parse (parser
->expat_parser
, buffer
, strlen (buffer
), 1);
552 if (status
== XML_STATUS_OK
&& parser
->error
.reason
== 0)
555 if (parser
->error
.reason
== RETURN_ERROR
556 && parser
->error
.error
== XML_PARSE_ERROR
)
558 gdb_assert (parser
->error
.message
!= NULL
);
559 error_string
= parser
->error
.message
;
561 else if (status
== XML_STATUS_ERROR
)
563 enum XML_Error err
= XML_GetErrorCode (parser
->expat_parser
);
565 error_string
= XML_ErrorString (err
);
569 gdb_assert (parser
->error
.reason
< 0);
570 throw_exception (parser
->error
);
573 if (parser
->last_line
!= 0)
574 warning (_("while parsing %s (at line %d): %s"), parser
->name
,
575 parser
->last_line
, error_string
);
577 warning (_("while parsing %s: %s"), parser
->name
, error_string
);
582 /* Parse a field VALSTR that we expect to contain an integer value.
583 The integer is returned in *VALP. The string is parsed with an
584 equivalent to strtoul.
586 Returns 0 for success, -1 for error. */
589 xml_parse_unsigned_integer (const char *valstr
, ULONGEST
*valp
)
597 result
= strtoulst (valstr
, &endptr
, 0);
605 /* Parse an integer string into a ULONGEST and return it, or call
606 gdb_xml_error if it could not be parsed. */
609 gdb_xml_parse_ulongest (struct gdb_xml_parser
*parser
, const char *value
)
613 if (xml_parse_unsigned_integer (value
, &result
) != 0)
614 gdb_xml_error (parser
, _("Can't convert \"%s\" to an integer"), value
);
619 /* Parse an integer attribute into a ULONGEST. */
622 gdb_xml_parse_attr_ulongest (struct gdb_xml_parser
*parser
,
623 const struct gdb_xml_attribute
*attribute
,
629 if (xml_parse_unsigned_integer (value
, &result
) != 0)
630 gdb_xml_error (parser
, _("Can't convert %s=\"%s\" to an integer"),
631 attribute
->name
, value
);
633 ret
= xmalloc (sizeof (result
));
634 memcpy (ret
, &result
, sizeof (result
));
638 /* A handler_data for yes/no boolean values. */
640 const struct gdb_xml_enum gdb_xml_enums_boolean
[] = {
646 /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the
647 value of handler_data when using gdb_xml_parse_attr_enum to parse a
648 fixed list of possible strings. The list is terminated by an entry
649 with NAME == NULL. */
652 gdb_xml_parse_attr_enum (struct gdb_xml_parser
*parser
,
653 const struct gdb_xml_attribute
*attribute
,
656 const struct gdb_xml_enum
*enums
= attribute
->handler_data
;
659 for (enums
= attribute
->handler_data
; enums
->name
!= NULL
; enums
++)
660 if (strcasecmp (enums
->name
, value
) == 0)
663 if (enums
->name
== NULL
)
664 gdb_xml_error (parser
, _("Unknown attribute value %s=\"%s\""),
665 attribute
->name
, value
);
667 ret
= xmalloc (sizeof (enums
->value
));
668 memcpy (ret
, &enums
->value
, sizeof (enums
->value
));
673 /* XInclude processing. This is done as a separate step from actually
674 parsing the document, so that we can produce a single combined XML
675 document - e.g. to hand to a front end or to simplify comparing two
676 documents. We make extensive use of XML_DefaultCurrent, to pass
677 input text directly into the output without reformatting or
680 We output the DOCTYPE declaration for the first document unchanged,
681 if present, and discard DOCTYPEs from included documents. Only the
682 one we pass through here is used when we feed the result back to
683 expat. The XInclude standard explicitly does not discuss
684 validation of the result; we choose to apply the same DTD applied
685 to the outermost document.
687 We can not simply include the external DTD subset in the document
688 as an internal subset, because <!IGNORE> and <!INCLUDE> are valid
689 only in external subsets. But if we do not pass the DTD into the
690 output at all, default values will not be filled in.
692 We don't pass through any <?xml> declaration because we generate
693 UTF-8, not whatever the input encoding was. */
695 struct xinclude_parsing_data
697 /* The obstack to build the output in. */
698 struct obstack obstack
;
700 /* A count indicating whether we are in an element whose
701 children should not be copied to the output, and if so,
702 how deep we are nested. This is used for anything inside
703 an xi:include, and for the DTD. */
706 /* The number of <xi:include> elements currently being processed,
710 /* A function to call to obtain additional features, and its
712 xml_fetch_another fetcher
;
717 xinclude_start_include (struct gdb_xml_parser
*parser
,
718 const struct gdb_xml_element
*element
,
719 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
721 struct xinclude_parsing_data
*data
= user_data
;
722 char *href
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
723 struct cleanup
*back_to
;
726 gdb_xml_debug (parser
, _("Processing XInclude of \"%s\""), href
);
728 if (data
->include_depth
> MAX_XINCLUDE_DEPTH
)
729 gdb_xml_error (parser
, _("Maximum XInclude depth (%d) exceeded"),
732 text
= data
->fetcher (href
, data
->fetcher_baton
);
734 gdb_xml_error (parser
, _("Could not load XML document \"%s\""), href
);
735 back_to
= make_cleanup (xfree
, text
);
737 output
= xml_process_xincludes (parser
->name
, text
, data
->fetcher
,
739 data
->include_depth
+ 1);
741 gdb_xml_error (parser
, _("Parsing \"%s\" failed"), href
);
743 obstack_grow (&data
->obstack
, output
, strlen (output
));
746 do_cleanups (back_to
);
752 xinclude_end_include (struct gdb_xml_parser
*parser
,
753 const struct gdb_xml_element
*element
,
754 void *user_data
, const char *body_text
)
756 struct xinclude_parsing_data
*data
= user_data
;
762 xml_xinclude_default (void *data_
, const XML_Char
*s
, int len
)
764 struct gdb_xml_parser
*parser
= data_
;
765 struct xinclude_parsing_data
*data
= parser
->user_data
;
767 /* If we are inside of e.g. xi:include or the DTD, don't save this
769 if (data
->skip_depth
)
772 /* Otherwise just add it to the end of the document we're building
774 obstack_grow (&data
->obstack
, s
, len
);
778 xml_xinclude_start_doctype (void *data_
, const XML_Char
*doctypeName
,
779 const XML_Char
*sysid
, const XML_Char
*pubid
,
780 int has_internal_subset
)
782 struct gdb_xml_parser
*parser
= data_
;
783 struct xinclude_parsing_data
*data
= parser
->user_data
;
785 /* Don't print out the doctype, or the contents of the DTD internal
791 xml_xinclude_end_doctype (void *data_
)
793 struct gdb_xml_parser
*parser
= data_
;
794 struct xinclude_parsing_data
*data
= parser
->user_data
;
800 xml_xinclude_xml_decl (void *data_
, const XML_Char
*version
,
801 const XML_Char
*encoding
, int standalone
)
803 /* Do nothing - this function prevents the default handler from
804 being called, thus suppressing the XML declaration from the
809 xml_xinclude_cleanup (void *data_
)
811 struct xinclude_parsing_data
*data
= data_
;
813 obstack_free (&data
->obstack
, NULL
);
817 const struct gdb_xml_attribute xinclude_attributes
[] = {
818 { "href", GDB_XML_AF_NONE
, NULL
, NULL
},
819 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
822 const struct gdb_xml_element xinclude_elements
[] = {
823 { "http://www.w3.org/2001/XInclude!include", xinclude_attributes
, NULL
,
824 GDB_XML_EF_OPTIONAL
| GDB_XML_EF_REPEATABLE
,
825 xinclude_start_include
, xinclude_end_include
},
826 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
829 /* The main entry point for <xi:include> processing. */
832 xml_process_xincludes (const char *name
, const char *text
,
833 xml_fetch_another fetcher
, void *fetcher_baton
,
836 struct gdb_xml_parser
*parser
;
837 struct xinclude_parsing_data
*data
;
838 struct cleanup
*back_to
;
841 data
= XZALLOC (struct xinclude_parsing_data
);
842 obstack_init (&data
->obstack
);
843 back_to
= make_cleanup (xml_xinclude_cleanup
, data
);
845 parser
= gdb_xml_create_parser_and_cleanup (name
, xinclude_elements
, data
);
846 parser
->is_xinclude
= 1;
848 data
->include_depth
= depth
;
849 data
->fetcher
= fetcher
;
850 data
->fetcher_baton
= fetcher_baton
;
852 XML_SetCharacterDataHandler (parser
->expat_parser
, NULL
);
853 XML_SetDefaultHandler (parser
->expat_parser
, xml_xinclude_default
);
855 /* Always discard the XML version declarations; the only important
856 thing this provides is encoding, and our result will have been
857 converted to UTF-8. */
858 XML_SetXmlDeclHandler (parser
->expat_parser
, xml_xinclude_xml_decl
);
861 /* Discard the doctype for included documents. */
862 XML_SetDoctypeDeclHandler (parser
->expat_parser
,
863 xml_xinclude_start_doctype
,
864 xml_xinclude_end_doctype
);
866 gdb_xml_use_dtd (parser
, "xinclude.dtd");
868 if (gdb_xml_parse (parser
, text
) == 0)
870 obstack_1grow (&data
->obstack
, '\0');
871 result
= xstrdup (obstack_finish (&data
->obstack
));
874 gdb_xml_debug (parser
, _("XInclude processing succeeded."));
879 do_cleanups (back_to
);
882 #endif /* HAVE_LIBEXPAT */
885 /* Return an XML document which was compiled into GDB, from
886 the given FILENAME, or NULL if the file was not compiled in. */
889 fetch_xml_builtin (const char *filename
)
893 for (p
= xml_builtin
; (*p
)[0]; p
++)
894 if (strcmp ((*p
)[0], filename
) == 0)
900 /* A to_xfer_partial helper function which reads XML files which were
901 compiled into GDB. The target may call this function from its own
902 to_xfer_partial handler, after converting object and annex to the
903 appropriate filename. */
906 xml_builtin_xfer_partial (const char *filename
,
907 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
908 ULONGEST offset
, LONGEST len
)
913 gdb_assert (readbuf
!= NULL
&& writebuf
== NULL
);
914 gdb_assert (filename
!= NULL
);
916 buf
= fetch_xml_builtin (filename
);
920 len_avail
= strlen (buf
);
921 if (offset
>= len_avail
)
924 if (len
> len_avail
- offset
)
925 len
= len_avail
- offset
;
926 memcpy (readbuf
, buf
+ offset
, len
);
932 show_debug_xml (struct ui_file
*file
, int from_tty
,
933 struct cmd_list_element
*c
, const char *value
)
935 fprintf_filtered (file
, _("XML debugging is %s.\n"), value
);
938 /* Return a malloc allocated string with special characters from TEXT
939 replaced by entity references. */
942 xml_escape_text (const char *text
)
947 /* Compute the length of the result. */
948 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
966 /* Expand the result. */
967 result
= xmalloc (i
+ special
+ 1);
968 for (i
= 0, special
= 0; text
[i
] != '\0'; i
++)
972 strcpy (result
+ i
+ special
, "'");
976 strcpy (result
+ i
+ special
, """);
980 strcpy (result
+ i
+ special
, "&");
984 strcpy (result
+ i
+ special
, "<");
988 strcpy (result
+ i
+ special
, ">");
992 result
[i
+ special
] = text
[i
];
995 result
[i
+ special
] = '\0';
1001 obstack_xml_printf (struct obstack
*obstack
, const char *format
, ...)
1008 va_start (ap
, format
);
1011 for (f
= format
; *f
; f
++)
1020 char *a
= va_arg (ap
, char *);
1022 obstack_grow (obstack
, prev
, f
- prev
- 1);
1023 p
= xml_escape_text (a
);
1024 obstack_grow_str (obstack
, p
);
1036 obstack_grow_str (obstack
, prev
);
1041 xml_fetch_content_from_file (const char *filename
, void *baton
)
1043 const char *dirname
= baton
;
1045 struct cleanup
*back_to
;
1049 if (dirname
&& *dirname
)
1051 char *fullname
= concat (dirname
, "/", filename
, (char *) NULL
);
1053 if (fullname
== NULL
)
1055 file
= fopen (fullname
, FOPEN_RT
);
1059 file
= fopen (filename
, FOPEN_RT
);
1064 back_to
= make_cleanup_fclose (file
);
1066 /* Read in the whole file, one chunk at a time. */
1069 text
= xmalloc (len
);
1070 make_cleanup (free_current_contents
, &text
);
1075 /* Continue reading where the last read left off. Leave at least
1076 one byte so that we can NUL-terminate the result. */
1077 bytes_read
= fread (text
+ offset
, 1, len
- offset
- 1, file
);
1080 warning (_("Read error from \"%s\""), filename
);
1081 do_cleanups (back_to
);
1085 offset
+= bytes_read
;
1091 text
= xrealloc (text
, len
);
1095 discard_cleanups (back_to
);
1097 text
[offset
] = '\0';
1101 void _initialize_xml_support (void);
1104 _initialize_xml_support (void)
1106 add_setshow_boolean_cmd ("xml", class_maintenance
, &debug_xml
,
1107 _("Set XML parser debugging."),
1108 _("Show XML parser debugging."),
1109 _("When set, debugging messages for XML parsers "
1111 NULL
, show_debug_xml
,
1112 &setdebuglist
, &showdebuglist
);