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