1 /* Helper routines for parsing XML using Expat.
3 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
26 #include "gdb_obstack.h"
29 struct gdb_xml_parser
;
30 struct gdb_xml_element
;
31 struct gdb_xml_attribute
;
33 /* Support for XInclude. */
35 /* Callback to fetch a new XML file, based on the provided HREF. */
37 typedef char *(*xml_fetch_another
) (const char *href
, void *baton
);
39 /* Return a new string which is the expansion of TEXT after processing
40 <xi:include> tags. FETCHER will be called (with FETCHER_BATON) to
41 retrieve any new files. DEPTH should be zero on the initial call.
43 On failure, this function uses NAME in a warning and returns NULL.
44 It may throw an exception, but does not for XML parsing
47 char *xml_process_xincludes (const char *name
, const char *text
,
48 xml_fetch_another fetcher
, void *fetcher_baton
,
51 /* Return an XML document which was compiled into GDB, from
52 the given FILENAME, or NULL if the file was not compiled in. */
54 const char *fetch_xml_builtin (const char *filename
);
56 /* A to_xfer_partial helper function which reads XML files which were
57 compiled into GDB. The target may call this function from its own
58 to_xfer_partial handler, after converting object and annex to the
59 appropriate filename. */
61 LONGEST
xml_builtin_xfer_partial (const char *filename
,
62 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
63 ULONGEST offset
, LONGEST len
);
65 /* The text of compiled-in XML documents, from xml-builtin.c
67 extern const char *xml_builtin
[][2];
69 /* Simplified XML parser infrastructure. */
71 /* A name and value pair, used to record parsed attributes. */
78 typedef struct gdb_xml_value gdb_xml_value_s
;
79 DEF_VEC_O(gdb_xml_value_s
);
81 /* The type of an attribute handler.
83 PARSER is the current XML parser, which should be used to issue any
84 debugging or error messages. The second argument is the
85 corresponding attribute description, so that a single handler can
86 be used for multiple attributes; the attribute name is available
87 for error messages and the handler data is available for additional
88 customization (see gdb_xml_parse_attr_enum). VALUE is the string
89 value of the attribute.
91 The returned value should be freeable with xfree, and will be freed
92 after the start handler is called. Errors should be reported by
93 calling gdb_xml_error. */
95 typedef void *(gdb_xml_attribute_handler
) (struct gdb_xml_parser
*parser
,
96 const struct gdb_xml_attribute
*,
99 /* Flags for attributes. If no flags are specified, the attribute is
102 enum gdb_xml_attribute_flag
105 GDB_XML_AF_OPTIONAL
= 1 << 0, /* The attribute is optional. */
108 /* An expected attribute and the handler to call when it is
109 encountered. Arrays of struct gdb_xml_attribute are terminated
110 by an entry with NAME == NULL. */
112 struct gdb_xml_attribute
116 gdb_xml_attribute_handler
*handler
;
117 const void *handler_data
;
120 /* Flags for elements. If no flags are specified, the element is
121 required exactly once. */
123 enum gdb_xml_element_flag
126 GDB_XML_EF_OPTIONAL
= 1 << 0, /* The element is optional. */
127 GDB_XML_EF_REPEATABLE
= 1 << 1, /* The element is repeatable. */
130 /* A handler called at the beginning of an element.
132 PARSER is the current XML parser, which should be used to issue any
133 debugging or error messages. ELEMENT is the current element.
134 USER_DATA is the opaque pointer supplied when the parser was
135 created. ATTRIBUTES is a vector of the values of any attributes
136 attached to this element.
138 The start handler will only be called if all the required
139 attributes were present and parsed successfully, and elements of
140 ATTRIBUTES are guaranteed to be in the same order used in
141 ELEMENT->ATTRIBUTES (not the order from the XML file). Accordingly
142 fixed offsets can be used to find any non-optional attributes as
143 long as no optional attributes precede them. */
145 typedef void (gdb_xml_element_start_handler
)
146 (struct gdb_xml_parser
*parser
, const struct gdb_xml_element
*element
,
147 void *user_data
, VEC(gdb_xml_value_s
) *attributes
);
149 /* A handler called at the end of an element.
151 PARSER, ELEMENT, and USER_DATA are as for the start handler. BODY
152 is any accumulated body text inside the element, with leading and
153 trailing whitespace removed. It will never be NULL. */
155 typedef void (gdb_xml_element_end_handler
)
156 (struct gdb_xml_parser
*, const struct gdb_xml_element
*,
157 void *user_data
, const char *body_text
);
159 /* An expected element and the handlers to call when it is
160 encountered. Arrays of struct gdb_xml_element are terminated
161 by an entry with NAME == NULL. */
163 struct gdb_xml_element
166 const struct gdb_xml_attribute
*attributes
;
167 const struct gdb_xml_element
*children
;
170 gdb_xml_element_start_handler
*start_handler
;
171 gdb_xml_element_end_handler
*end_handler
;
174 /* Initialize and return a parser. Register a cleanup to destroy the
177 struct gdb_xml_parser
*gdb_xml_create_parser_and_cleanup
178 (const char *name
, const struct gdb_xml_element
*elements
,
181 /* Associate DTD_NAME, which must be the name of a compiled-in DTD,
184 void gdb_xml_use_dtd (struct gdb_xml_parser
*parser
, const char *dtd_name
);
186 /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which
187 should be NUL-terminated.
189 The return value is 0 for success or -1 for error. It may throw,
190 but only if something unexpected goes wrong during parsing; parse
191 errors will be caught, warned about, and reported as failure. */
193 int gdb_xml_parse (struct gdb_xml_parser
*parser
, const char *buffer
);
195 /* Issue a debugging message from one of PARSER's handlers. */
197 void gdb_xml_debug (struct gdb_xml_parser
*parser
, const char *format
, ...)
198 ATTR_FORMAT (printf
, 2, 0);
200 /* Issue an error message from one of PARSER's handlers, and stop
203 void gdb_xml_error (struct gdb_xml_parser
*parser
, const char *format
, ...)
204 ATTR_NORETURN
ATTR_FORMAT (printf
, 2, 0);
206 /* Parse an integer attribute into a ULONGEST. */
208 extern gdb_xml_attribute_handler gdb_xml_parse_attr_ulongest
;
210 /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the
211 value of handler_data when using gdb_xml_parse_attr_enum to parse a
212 fixed list of possible strings. The list is terminated by an entry
213 with NAME == NULL. */
221 /* A handler_data for yes/no boolean values. */
222 extern const struct gdb_xml_enum gdb_xml_enums_boolean
[];
224 extern gdb_xml_attribute_handler gdb_xml_parse_attr_enum
;
226 /* Parse an integer string into a ULONGEST and return it, or call
227 gdb_xml_error if it could not be parsed. */
229 ULONGEST
gdb_xml_parse_ulongest (struct gdb_xml_parser
*parser
,