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