GDB (API)
|
00001 /* Helper routines for parsing XML using Expat. 00002 00003 Copyright (C) 2006-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 00021 #ifndef XML_SUPPORT_H 00022 #define XML_SUPPORT_H 00023 00024 #include "gdb_obstack.h" 00025 #include "vec.h" 00026 #include "xml-utils.h" 00027 00028 struct gdb_xml_parser; 00029 struct gdb_xml_element; 00030 struct gdb_xml_attribute; 00031 00032 /* Return an XML document which was compiled into GDB, from 00033 the given FILENAME, or NULL if the file was not compiled in. */ 00034 00035 const char *fetch_xml_builtin (const char *filename); 00036 00037 /* A to_xfer_partial helper function which reads XML files which were 00038 compiled into GDB. The target may call this function from its own 00039 to_xfer_partial handler, after converting object and annex to the 00040 appropriate filename. */ 00041 00042 LONGEST xml_builtin_xfer_partial (const char *filename, 00043 gdb_byte *readbuf, const gdb_byte *writebuf, 00044 ULONGEST offset, LONGEST len); 00045 00046 /* The text of compiled-in XML documents, from xml-builtin.c 00047 (generated). */ 00048 00049 extern const char *xml_builtin[][2]; 00050 00051 /* Support for XInclude. */ 00052 00053 /* Callback to fetch a new XML file, based on the provided HREF. */ 00054 00055 typedef char *(*xml_fetch_another) (const char *href, void *baton); 00056 00057 /* Return a new string which is the expansion of TEXT after processing 00058 <xi:include> tags. FETCHER will be called (with FETCHER_BATON) to 00059 retrieve any new files. DEPTH should be zero on the initial call. 00060 00061 On failure, this function uses NAME in a warning and returns NULL. 00062 It may throw an exception, but does not for XML parsing 00063 problems. */ 00064 00065 char *xml_process_xincludes (const char *name, const char *text, 00066 xml_fetch_another fetcher, void *fetcher_baton, 00067 int depth); 00068 00069 /* Simplified XML parser infrastructure. */ 00070 00071 /* A name and value pair, used to record parsed attributes. */ 00072 00073 struct gdb_xml_value 00074 { 00075 const char *name; 00076 void *value; 00077 }; 00078 typedef struct gdb_xml_value gdb_xml_value_s; 00079 DEF_VEC_O(gdb_xml_value_s); 00080 00081 /* The type of an attribute handler. 00082 00083 PARSER is the current XML parser, which should be used to issue any 00084 debugging or error messages. The second argument is the 00085 corresponding attribute description, so that a single handler can 00086 be used for multiple attributes; the attribute name is available 00087 for error messages and the handler data is available for additional 00088 customization (see gdb_xml_parse_attr_enum). VALUE is the string 00089 value of the attribute. 00090 00091 The returned value should be freeable with xfree, and will be freed 00092 after the start handler is called. Errors should be reported by 00093 calling gdb_xml_error. */ 00094 00095 typedef void *(gdb_xml_attribute_handler) (struct gdb_xml_parser *parser, 00096 const struct gdb_xml_attribute *, 00097 const char *value); 00098 00099 /* Flags for attributes. If no flags are specified, the attribute is 00100 required. */ 00101 00102 enum gdb_xml_attribute_flag 00103 { 00104 GDB_XML_AF_NONE, 00105 GDB_XML_AF_OPTIONAL = 1 << 0, /* The attribute is optional. */ 00106 }; 00107 00108 /* An expected attribute and the handler to call when it is 00109 encountered. Arrays of struct gdb_xml_attribute are terminated 00110 by an entry with NAME == NULL. */ 00111 00112 struct gdb_xml_attribute 00113 { 00114 const char *name; 00115 int flags; 00116 gdb_xml_attribute_handler *handler; 00117 const void *handler_data; 00118 }; 00119 00120 /* Flags for elements. If no flags are specified, the element is 00121 required exactly once. */ 00122 00123 enum gdb_xml_element_flag 00124 { 00125 GDB_XML_EF_NONE, 00126 GDB_XML_EF_OPTIONAL = 1 << 0, /* The element is optional. */ 00127 GDB_XML_EF_REPEATABLE = 1 << 1, /* The element is repeatable. */ 00128 }; 00129 00130 /* A handler called at the beginning of an element. 00131 00132 PARSER is the current XML parser, which should be used to issue any 00133 debugging or error messages. ELEMENT is the current element. 00134 USER_DATA is the opaque pointer supplied when the parser was 00135 created. ATTRIBUTES is a vector of the values of any attributes 00136 attached to this element. 00137 00138 The start handler will only be called if all the required 00139 attributes were present and parsed successfully, and elements of 00140 ATTRIBUTES are guaranteed to be in the same order used in 00141 ELEMENT->ATTRIBUTES (not the order from the XML file). Accordingly 00142 fixed offsets can be used to find any non-optional attributes as 00143 long as no optional attributes precede them. */ 00144 00145 typedef void (gdb_xml_element_start_handler) 00146 (struct gdb_xml_parser *parser, const struct gdb_xml_element *element, 00147 void *user_data, VEC(gdb_xml_value_s) *attributes); 00148 00149 /* A handler called at the end of an element. 00150 00151 PARSER, ELEMENT, and USER_DATA are as for the start handler. BODY 00152 is any accumulated body text inside the element, with leading and 00153 trailing whitespace removed. It will never be NULL. */ 00154 00155 typedef void (gdb_xml_element_end_handler) 00156 (struct gdb_xml_parser *, const struct gdb_xml_element *, 00157 void *user_data, const char *body_text); 00158 00159 /* An expected element and the handlers to call when it is 00160 encountered. Arrays of struct gdb_xml_element are terminated 00161 by an entry with NAME == NULL. */ 00162 00163 struct gdb_xml_element 00164 { 00165 const char *name; 00166 const struct gdb_xml_attribute *attributes; 00167 const struct gdb_xml_element *children; 00168 int flags; 00169 00170 gdb_xml_element_start_handler *start_handler; 00171 gdb_xml_element_end_handler *end_handler; 00172 }; 00173 00174 /* Associate DTD_NAME, which must be the name of a compiled-in DTD, 00175 with PARSER. */ 00176 00177 void gdb_xml_use_dtd (struct gdb_xml_parser *parser, const char *dtd_name); 00178 00179 /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which 00180 should be NUL-terminated. 00181 00182 The return value is 0 for success or -1 for error. It may throw, 00183 but only if something unexpected goes wrong during parsing; parse 00184 errors will be caught, warned about, and reported as failure. */ 00185 00186 int gdb_xml_parse (struct gdb_xml_parser *parser, const char *buffer); 00187 00188 /* Parse a XML document. DOCUMENT is the data to parse, which should 00189 be NUL-terminated. If non-NULL, use the compiled-in DTD named 00190 DTD_NAME to drive the parsing. 00191 00192 The return value is 0 for success or -1 for error. It may throw, 00193 but only if something unexpected goes wrong during parsing; parse 00194 errors will be caught, warned about, and reported as failure. */ 00195 00196 int gdb_xml_parse_quick (const char *name, const char *dtd_name, 00197 const struct gdb_xml_element *elements, 00198 const char *document, void *user_data); 00199 00200 /* Issue a debugging message from one of PARSER's handlers. */ 00201 00202 void gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...) 00203 ATTRIBUTE_PRINTF (2, 0); 00204 00205 /* Issue an error message from one of PARSER's handlers, and stop 00206 parsing. */ 00207 00208 void gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...) 00209 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0); 00210 00211 /* Find the attribute named NAME in the set of parsed attributes 00212 ATTRIBUTES. Returns NULL if not found. */ 00213 00214 struct gdb_xml_value *xml_find_attribute (VEC(gdb_xml_value_s) *attributes, 00215 const char *name); 00216 00217 /* Parse an integer attribute into a ULONGEST. */ 00218 00219 extern gdb_xml_attribute_handler gdb_xml_parse_attr_ulongest; 00220 00221 /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the 00222 value of handler_data when using gdb_xml_parse_attr_enum to parse a 00223 fixed list of possible strings. The list is terminated by an entry 00224 with NAME == NULL. */ 00225 00226 struct gdb_xml_enum 00227 { 00228 const char *name; 00229 ULONGEST value; 00230 }; 00231 00232 /* A handler_data for yes/no boolean values. */ 00233 extern const struct gdb_xml_enum gdb_xml_enums_boolean[]; 00234 00235 extern gdb_xml_attribute_handler gdb_xml_parse_attr_enum; 00236 00237 /* Parse an integer string into a ULONGEST and return it, or call 00238 gdb_xml_error if it could not be parsed. */ 00239 00240 ULONGEST gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, 00241 const char *value); 00242 00243 /* Simple printf to obstack function. Current implemented formatters: 00244 %s - grow an xml escaped text in OBSTACK. */ 00245 00246 extern void obstack_xml_printf (struct obstack *obstack, 00247 const char *format, ...) 00248 ATTRIBUTE_PRINTF_2; 00249 00250 /* Open FILENAME, read all its text into memory, close it, and return 00251 the text. If something goes wrong, return NULL and warn. */ 00252 00253 extern char *xml_fetch_content_from_file (const char *filename, 00254 void *baton); 00255 00256 #endif