]>
Commit | Line | Data |
---|---|---|
fd79ecee DJ |
1 | /* Helper routines for parsing XML using Expat. |
2 | ||
3 | Copyright (C) 2006 | |
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 2 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, write to the Free Software | |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
21 | Boston, MA 02110-1301, USA. */ | |
22 | ||
23 | ||
24 | #ifndef XML_SUPPORT_H | |
25 | #define XML_SUPPORT_H | |
26 | ||
e776119f DJ |
27 | #include "gdb_obstack.h" |
28 | #include "vec.h" | |
fd79ecee | 29 | |
e776119f DJ |
30 | struct gdb_xml_parser; |
31 | struct gdb_xml_element; | |
32 | struct gdb_xml_attribute; | |
fd79ecee | 33 | |
e776119f | 34 | /* A name and value pair, used to record parsed attributes. */ |
fd79ecee | 35 | |
e776119f DJ |
36 | struct gdb_xml_value |
37 | { | |
38 | const char *name; | |
39 | void *value; | |
40 | }; | |
41 | typedef struct gdb_xml_value gdb_xml_value_s; | |
42 | DEF_VEC_O(gdb_xml_value_s); | |
fd79ecee | 43 | |
e776119f | 44 | /* The type of an attribute handler. |
fd79ecee | 45 | |
e776119f DJ |
46 | PARSER is the current XML parser, which should be used to issue any |
47 | debugging or error messages. The second argument is the | |
48 | corresponding attribute description, so that a single handler can | |
49 | be used for multiple attributes; the attribute name is available | |
50 | for error messages and the handler data is available for additional | |
51 | customization (see gdb_xml_parse_attr_enum). VALUE is the string | |
52 | value of the attribute. | |
53 | ||
54 | The returned value should be freeable with xfree, and will be freed | |
55 | after the start handler is called. Errors should be reported by | |
56 | calling gdb_xml_error. */ | |
57 | ||
58 | typedef void *(gdb_xml_attribute_handler) (struct gdb_xml_parser *parser, | |
59 | const struct gdb_xml_attribute *, | |
60 | const char *value); | |
61 | ||
62 | /* Flags for attributes. If no flags are specified, the attribute is | |
63 | required. */ | |
64 | ||
65 | enum gdb_xml_attribute_flag | |
66 | { | |
67 | GDB_XML_AF_NONE, | |
68 | GDB_XML_AF_OPTIONAL = 1 << 0, /* The attribute is optional. */ | |
69 | }; | |
70 | ||
71 | /* An expected attribute and the handler to call when it is | |
72 | encountered. Arrays of struct gdb_xml_attribute are terminated | |
73 | by an entry with NAME == NULL. */ | |
74 | ||
75 | struct gdb_xml_attribute | |
76 | { | |
77 | const char *name; | |
78 | int flags; | |
79 | gdb_xml_attribute_handler *handler; | |
80 | const void *handler_data; | |
81 | }; | |
82 | ||
83 | /* Flags for elements. If no flags are specified, the element is | |
84 | required exactly once. */ | |
85 | ||
86 | enum gdb_xml_element_flag | |
87 | { | |
88 | GDB_XML_EF_NONE, | |
89 | GDB_XML_EF_OPTIONAL = 1 << 0, /* The element is optional. */ | |
90 | GDB_XML_EF_REPEATABLE = 1 << 1, /* The element is repeatable. */ | |
91 | }; | |
92 | ||
93 | /* A handler called at the beginning of an element. | |
94 | ||
95 | PARSER is the current XML parser, which should be used to issue any | |
96 | debugging or error messages. ELEMENT is the current element. | |
97 | USER_DATA is the opaque pointer supplied when the parser was | |
98 | created. ATTRIBUTES is a vector of the values of any attributes | |
99 | attached to this element. | |
100 | ||
101 | The start handler will only be called if all the required | |
102 | attributes were present and parsed successfully, and elements of | |
103 | ATTRIBUTES are guaranteed to be in the same order used in | |
104 | ELEMENT->ATTRIBUTES (not the order from the XML file). Accordingly | |
105 | fixed offsets can be used to find any non-optional attributes as | |
106 | long as no optional attributes precede them. */ | |
107 | ||
108 | typedef void (gdb_xml_element_start_handler) | |
109 | (struct gdb_xml_parser *parser, const struct gdb_xml_element *element, | |
110 | void *user_data, VEC(gdb_xml_value_s) *attributes); | |
111 | ||
112 | /* A handler called at the end of an element. | |
113 | ||
114 | PARSER, ELEMENT, and USER_DATA are as for the start handler. BODY | |
115 | is any accumulated body text inside the element, with leading and | |
116 | trailing whitespace removed. It will never be NULL. */ | |
117 | ||
118 | typedef void (gdb_xml_element_end_handler) | |
119 | (struct gdb_xml_parser *, const struct gdb_xml_element *, | |
120 | void *user_data, const char *body_text); | |
121 | ||
122 | /* An expected element and the handlers to call when it is | |
123 | encountered. Arrays of struct gdb_xml_element are terminated | |
124 | by an entry with NAME == NULL. */ | |
125 | ||
126 | struct gdb_xml_element | |
127 | { | |
128 | const char *name; | |
129 | const struct gdb_xml_attribute *attributes; | |
130 | const struct gdb_xml_element *children; | |
131 | int flags; | |
132 | ||
133 | gdb_xml_element_start_handler *start_handler; | |
134 | gdb_xml_element_end_handler *end_handler; | |
135 | }; | |
136 | ||
137 | /* Initialize and return a parser. Register a cleanup to destroy the | |
138 | parser. */ | |
139 | ||
140 | struct gdb_xml_parser *gdb_xml_create_parser_and_cleanup | |
141 | (const char *name, const struct gdb_xml_element *elements, | |
142 | void *user_data); | |
143 | ||
144 | /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which | |
145 | should be NUL-terminated. | |
146 | ||
147 | The return value is 0 for success or -1 for error. It may throw, | |
148 | but only if something unexpected goes wrong during parsing; parse | |
149 | errors will be caught, warned about, and reported as failure. */ | |
150 | ||
151 | int gdb_xml_parse (struct gdb_xml_parser *parser, const char *buffer); | |
152 | ||
153 | /* Issue a debugging message from one of PARSER's handlers. */ | |
154 | ||
155 | void gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...) | |
156 | ATTR_FORMAT (printf, 2, 0); | |
157 | ||
158 | /* Issue an error message from one of PARSER's handlers, and stop | |
159 | parsing. */ | |
160 | ||
161 | void gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...) | |
162 | ATTR_NORETURN ATTR_FORMAT (printf, 2, 0); | |
163 | ||
164 | /* Parse an integer attribute into a ULONGEST. */ | |
165 | ||
166 | extern gdb_xml_attribute_handler gdb_xml_parse_attr_ulongest; | |
167 | ||
168 | /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the | |
169 | value of handler_data when using gdb_xml_parse_attr_enum to parse a | |
170 | fixed list of possible strings. The list is terminated by an entry | |
171 | with NAME == NULL. */ | |
172 | ||
173 | struct gdb_xml_enum | |
174 | { | |
175 | const char *name; | |
176 | ULONGEST value; | |
177 | }; | |
178 | ||
179 | extern gdb_xml_attribute_handler gdb_xml_parse_attr_enum; | |
180 | ||
181 | /* Parse an integer string into a ULONGEST and return it, or call | |
182 | gdb_xml_error if it could not be parsed. */ | |
183 | ||
184 | ULONGEST gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, | |
185 | const char *value); | |
fd79ecee DJ |
186 | |
187 | #endif |