]>
Commit | Line | Data |
---|---|---|
fd79ecee DJ |
1 | /* Helper routines for parsing XML using Expat. |
2 | ||
7b6bb8da JB |
3 | Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 |
4 | Free Software Foundation, Inc. | |
fd79ecee DJ |
5 | |
6 | This file is part of GDB. | |
7 | ||
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 | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
fd79ecee DJ |
11 | (at your option) any later version. |
12 | ||
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. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fd79ecee DJ |
20 | |
21 | ||
22 | #ifndef XML_SUPPORT_H | |
23 | #define XML_SUPPORT_H | |
24 | ||
e776119f DJ |
25 | #include "gdb_obstack.h" |
26 | #include "vec.h" | |
fd79ecee | 27 | |
e776119f DJ |
28 | struct gdb_xml_parser; |
29 | struct gdb_xml_element; | |
30 | struct gdb_xml_attribute; | |
fd79ecee | 31 | |
108546a0 DJ |
32 | /* Return an XML document which was compiled into GDB, from |
33 | the given FILENAME, or NULL if the file was not compiled in. */ | |
34 | ||
35 | const char *fetch_xml_builtin (const char *filename); | |
36 | ||
05a4558a DJ |
37 | /* A to_xfer_partial helper function which reads XML files which were |
38 | compiled into GDB. The target may call this function from its own | |
39 | to_xfer_partial handler, after converting object and annex to the | |
40 | appropriate filename. */ | |
41 | ||
42 | LONGEST xml_builtin_xfer_partial (const char *filename, | |
43 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
44 | ULONGEST offset, LONGEST len); | |
45 | ||
108546a0 DJ |
46 | /* The text of compiled-in XML documents, from xml-builtin.c |
47 | (generated). */ | |
de584861 | 48 | |
108546a0 DJ |
49 | extern const char *xml_builtin[][2]; |
50 | ||
de584861 PA |
51 | /* Return a malloc allocated string with special characters from TEXT |
52 | replaced by entity references. */ | |
53 | ||
54 | char *xml_escape_text (const char *text); | |
55 | ||
56 | /* Support for XInclude. */ | |
57 | ||
58 | /* Callback to fetch a new XML file, based on the provided HREF. */ | |
59 | ||
60 | typedef char *(*xml_fetch_another) (const char *href, void *baton); | |
61 | ||
62 | /* Return a new string which is the expansion of TEXT after processing | |
63 | <xi:include> tags. FETCHER will be called (with FETCHER_BATON) to | |
64 | retrieve any new files. DEPTH should be zero on the initial call. | |
65 | ||
66 | On failure, this function uses NAME in a warning and returns NULL. | |
67 | It may throw an exception, but does not for XML parsing | |
68 | problems. */ | |
69 | ||
70 | char *xml_process_xincludes (const char *name, const char *text, | |
71 | xml_fetch_another fetcher, void *fetcher_baton, | |
72 | int depth); | |
73 | ||
108546a0 DJ |
74 | /* Simplified XML parser infrastructure. */ |
75 | ||
e776119f | 76 | /* A name and value pair, used to record parsed attributes. */ |
fd79ecee | 77 | |
e776119f DJ |
78 | struct gdb_xml_value |
79 | { | |
80 | const char *name; | |
81 | void *value; | |
82 | }; | |
83 | typedef struct gdb_xml_value gdb_xml_value_s; | |
84 | DEF_VEC_O(gdb_xml_value_s); | |
fd79ecee | 85 | |
e776119f | 86 | /* The type of an attribute handler. |
fd79ecee | 87 | |
e776119f DJ |
88 | PARSER is the current XML parser, which should be used to issue any |
89 | debugging or error messages. The second argument is the | |
90 | corresponding attribute description, so that a single handler can | |
91 | be used for multiple attributes; the attribute name is available | |
92 | for error messages and the handler data is available for additional | |
93 | customization (see gdb_xml_parse_attr_enum). VALUE is the string | |
94 | value of the attribute. | |
95 | ||
96 | The returned value should be freeable with xfree, and will be freed | |
97 | after the start handler is called. Errors should be reported by | |
98 | calling gdb_xml_error. */ | |
99 | ||
100 | typedef void *(gdb_xml_attribute_handler) (struct gdb_xml_parser *parser, | |
101 | const struct gdb_xml_attribute *, | |
102 | const char *value); | |
103 | ||
104 | /* Flags for attributes. If no flags are specified, the attribute is | |
105 | required. */ | |
106 | ||
107 | enum gdb_xml_attribute_flag | |
108 | { | |
109 | GDB_XML_AF_NONE, | |
110 | GDB_XML_AF_OPTIONAL = 1 << 0, /* The attribute is optional. */ | |
111 | }; | |
112 | ||
113 | /* An expected attribute and the handler to call when it is | |
114 | encountered. Arrays of struct gdb_xml_attribute are terminated | |
115 | by an entry with NAME == NULL. */ | |
116 | ||
117 | struct gdb_xml_attribute | |
118 | { | |
119 | const char *name; | |
120 | int flags; | |
121 | gdb_xml_attribute_handler *handler; | |
122 | const void *handler_data; | |
123 | }; | |
124 | ||
125 | /* Flags for elements. If no flags are specified, the element is | |
126 | required exactly once. */ | |
127 | ||
128 | enum gdb_xml_element_flag | |
129 | { | |
130 | GDB_XML_EF_NONE, | |
131 | GDB_XML_EF_OPTIONAL = 1 << 0, /* The element is optional. */ | |
132 | GDB_XML_EF_REPEATABLE = 1 << 1, /* The element is repeatable. */ | |
133 | }; | |
134 | ||
135 | /* A handler called at the beginning of an element. | |
136 | ||
137 | PARSER is the current XML parser, which should be used to issue any | |
138 | debugging or error messages. ELEMENT is the current element. | |
139 | USER_DATA is the opaque pointer supplied when the parser was | |
140 | created. ATTRIBUTES is a vector of the values of any attributes | |
141 | attached to this element. | |
142 | ||
143 | The start handler will only be called if all the required | |
144 | attributes were present and parsed successfully, and elements of | |
145 | ATTRIBUTES are guaranteed to be in the same order used in | |
146 | ELEMENT->ATTRIBUTES (not the order from the XML file). Accordingly | |
147 | fixed offsets can be used to find any non-optional attributes as | |
148 | long as no optional attributes precede them. */ | |
149 | ||
150 | typedef void (gdb_xml_element_start_handler) | |
151 | (struct gdb_xml_parser *parser, const struct gdb_xml_element *element, | |
152 | void *user_data, VEC(gdb_xml_value_s) *attributes); | |
153 | ||
154 | /* A handler called at the end of an element. | |
155 | ||
156 | PARSER, ELEMENT, and USER_DATA are as for the start handler. BODY | |
157 | is any accumulated body text inside the element, with leading and | |
158 | trailing whitespace removed. It will never be NULL. */ | |
159 | ||
160 | typedef void (gdb_xml_element_end_handler) | |
161 | (struct gdb_xml_parser *, const struct gdb_xml_element *, | |
162 | void *user_data, const char *body_text); | |
163 | ||
164 | /* An expected element and the handlers to call when it is | |
165 | encountered. Arrays of struct gdb_xml_element are terminated | |
166 | by an entry with NAME == NULL. */ | |
167 | ||
168 | struct gdb_xml_element | |
169 | { | |
170 | const char *name; | |
171 | const struct gdb_xml_attribute *attributes; | |
172 | const struct gdb_xml_element *children; | |
173 | int flags; | |
174 | ||
175 | gdb_xml_element_start_handler *start_handler; | |
176 | gdb_xml_element_end_handler *end_handler; | |
177 | }; | |
178 | ||
179 | /* Initialize and return a parser. Register a cleanup to destroy the | |
180 | parser. */ | |
181 | ||
182 | struct gdb_xml_parser *gdb_xml_create_parser_and_cleanup | |
183 | (const char *name, const struct gdb_xml_element *elements, | |
184 | void *user_data); | |
185 | ||
108546a0 DJ |
186 | /* Associate DTD_NAME, which must be the name of a compiled-in DTD, |
187 | with PARSER. */ | |
188 | ||
189 | void gdb_xml_use_dtd (struct gdb_xml_parser *parser, const char *dtd_name); | |
190 | ||
e776119f DJ |
191 | /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which |
192 | should be NUL-terminated. | |
193 | ||
194 | The return value is 0 for success or -1 for error. It may throw, | |
195 | but only if something unexpected goes wrong during parsing; parse | |
196 | errors will be caught, warned about, and reported as failure. */ | |
197 | ||
198 | int gdb_xml_parse (struct gdb_xml_parser *parser, const char *buffer); | |
199 | ||
200 | /* Issue a debugging message from one of PARSER's handlers. */ | |
201 | ||
202 | void gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...) | |
a0b31db1 | 203 | ATTRIBUTE_PRINTF (2, 0); |
e776119f DJ |
204 | |
205 | /* Issue an error message from one of PARSER's handlers, and stop | |
206 | parsing. */ | |
207 | ||
208 | void gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...) | |
c25c4a8b | 209 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0); |
e776119f DJ |
210 | |
211 | /* Parse an integer attribute into a ULONGEST. */ | |
212 | ||
213 | extern gdb_xml_attribute_handler gdb_xml_parse_attr_ulongest; | |
214 | ||
215 | /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the | |
216 | value of handler_data when using gdb_xml_parse_attr_enum to parse a | |
217 | fixed list of possible strings. The list is terminated by an entry | |
218 | with NAME == NULL. */ | |
219 | ||
220 | struct gdb_xml_enum | |
221 | { | |
222 | const char *name; | |
223 | ULONGEST value; | |
224 | }; | |
225 | ||
123dc839 DJ |
226 | /* A handler_data for yes/no boolean values. */ |
227 | extern const struct gdb_xml_enum gdb_xml_enums_boolean[]; | |
228 | ||
e776119f DJ |
229 | extern gdb_xml_attribute_handler gdb_xml_parse_attr_enum; |
230 | ||
231 | /* Parse an integer string into a ULONGEST and return it, or call | |
232 | gdb_xml_error if it could not be parsed. */ | |
233 | ||
234 | ULONGEST gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, | |
235 | const char *value); | |
fd79ecee | 236 | |
07e059b5 VP |
237 | /* Simple printf to obstack function. Current implemented formatters: |
238 | %s - grow an xml escaped text in OBSTACK. */ | |
239 | ||
240 | extern void obstack_xml_printf (struct obstack *obstack, | |
241 | const char *format, ...) | |
242 | ATTRIBUTE_PRINTF_2; | |
243 | ||
a96d9b2e SDJ |
244 | /* Open FILENAME, read all its text into memory, close it, and return |
245 | the text. If something goes wrong, return NULL and warn. */ | |
246 | ||
247 | extern char *xml_fetch_content_from_file (const char *filename, | |
248 | void *baton); | |
249 | ||
fd79ecee | 250 | #endif |