Commit | Line | Data |
---|---|---|
fd79ecee DJ |
1 | /* Helper routines for parsing XML using Expat. |
2 | ||
61baf725 | 3 | Copyright (C) 2006-2017 Free Software Foundation, Inc. |
fd79ecee DJ |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
fd79ecee DJ |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fd79ecee DJ |
19 | |
20 | #include "defs.h" | |
e776119f | 21 | #include "gdbcmd.h" |
05a4558a | 22 | #include "xml-support.h" |
614c279d | 23 | #include "filestuff.h" |
05a4558a | 24 | #include "safe-ctype.h" |
a7fc9b61 | 25 | #include <vector> |
bd8a901f | 26 | #include <string> |
e776119f DJ |
27 | |
28 | /* Debugging flag. */ | |
29 | static int debug_xml; | |
fd79ecee DJ |
30 | |
31 | /* The contents of this file are only useful if XML support is | |
32 | available. */ | |
33 | #ifdef HAVE_LIBEXPAT | |
34 | ||
dbc981de | 35 | #include "gdb_expat.h" |
e776119f | 36 | |
108546a0 DJ |
37 | /* The maximum depth of <xi:include> nesting. No need to be miserly, |
38 | we just want to avoid running out of stack on loops. */ | |
39 | #define MAX_XINCLUDE_DEPTH 30 | |
40 | ||
123dc839 DJ |
41 | /* Simplified XML parser infrastructure. */ |
42 | ||
e776119f DJ |
43 | /* A parsing level -- used to keep track of the current element |
44 | nesting. */ | |
45 | struct scope_level | |
46 | { | |
a7fc9b61 PA |
47 | explicit scope_level (const gdb_xml_element *elements_ = NULL) |
48 | : elements (elements_), | |
49 | element (NULL), | |
bd8a901f | 50 | seen (0) |
a7fc9b61 PA |
51 | {} |
52 | ||
e776119f DJ |
53 | /* Elements we allow at this level. */ |
54 | const struct gdb_xml_element *elements; | |
55 | ||
56 | /* The element which we are within. */ | |
57 | const struct gdb_xml_element *element; | |
58 | ||
59 | /* Mask of which elements we've seen at this level (used for | |
60 | optional and repeatable checking). */ | |
61 | unsigned int seen; | |
62 | ||
bd8a901f PA |
63 | /* Body text accumulation. */ |
64 | std::string body; | |
e776119f | 65 | }; |
e776119f DJ |
66 | |
67 | /* The parser itself, and our additional state. */ | |
68 | struct gdb_xml_parser | |
69 | { | |
010151c9 PA |
70 | gdb_xml_parser (const char *name, |
71 | const gdb_xml_element *elements, | |
72 | void *user_data); | |
73 | ~gdb_xml_parser(); | |
74 | ||
4895cde2 PA |
75 | /* Associate DTD_NAME, which must be the name of a compiled-in DTD, |
76 | with the parser. */ | |
77 | void use_dtd (const char *dtd_name); | |
78 | ||
79 | /* Return the name of the expected / default DTD, if specified. */ | |
80 | const char *dtd_name () | |
81 | { return m_dtd_name; } | |
82 | ||
83 | /* Invoke the parser on BUFFER. BUFFER is the data to parse, which | |
84 | should be NUL-terminated. | |
85 | ||
86 | The return value is 0 for success or -1 for error. It may throw, | |
87 | but only if something unexpected goes wrong during parsing; parse | |
88 | errors will be caught, warned about, and reported as failure. */ | |
89 | int parse (const char *buffer); | |
90 | ||
91 | /* Issue a debugging message. */ | |
92 | void vdebug (const char *format, va_list ap) | |
93 | ATTRIBUTE_PRINTF (2, 0); | |
94 | ||
95 | /* Issue an error message, and stop parsing. */ | |
96 | void verror (const char *format, va_list ap) | |
97 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0); | |
98 | ||
99 | void body_text (const XML_Char *text, int length); | |
100 | void start_element (const XML_Char *name, const XML_Char **attrs); | |
101 | void end_element (const XML_Char *name); | |
102 | ||
103 | /* Return the name of this parser. */ | |
104 | const char *name () | |
105 | { return m_name; } | |
106 | ||
107 | /* Return the user's callback data, for handlers. */ | |
108 | void *user_data () | |
109 | { return m_user_data; }; | |
110 | ||
111 | /* Are we the special <xi:include> parser? */ | |
112 | void set_is_xinclude (bool is_xinclude) | |
113 | { m_is_xinclude = is_xinclude; } | |
114 | ||
115 | /* A thrown error, if any. */ | |
116 | void set_error (gdb_exception error) | |
117 | { | |
118 | m_error = error; | |
119 | #ifdef HAVE_XML_STOPPARSER | |
120 | XML_StopParser (m_expat_parser, XML_FALSE); | |
121 | #endif | |
122 | } | |
123 | ||
124 | /* Return the underlying expat parser. */ | |
125 | XML_Parser expat_parser () | |
126 | { return m_expat_parser; } | |
127 | ||
128 | private: | |
129 | /* The underlying expat parser. */ | |
130 | XML_Parser m_expat_parser; | |
131 | ||
132 | /* Name of this parser. */ | |
133 | const char *m_name; | |
e776119f | 134 | |
4895cde2 PA |
135 | /* The user's callback data, for handlers. */ |
136 | void *m_user_data; | |
e776119f | 137 | |
a7fc9b61 | 138 | /* Scoping stack. */ |
4895cde2 | 139 | std::vector<scope_level> m_scopes; |
e776119f | 140 | |
4895cde2 PA |
141 | /* A thrown error, if any. */ |
142 | struct gdb_exception m_error; | |
108546a0 | 143 | |
4895cde2 PA |
144 | /* The line of the thrown error, or 0. */ |
145 | int m_last_line; | |
146 | ||
147 | /* The name of the expected / default DTD, if specified. */ | |
148 | const char *m_dtd_name; | |
149 | ||
150 | /* Are we the special <xi:include> parser? */ | |
151 | bool m_is_xinclude; | |
e776119f DJ |
152 | }; |
153 | ||
154 | /* Process some body text. We accumulate the text for later use; it's | |
155 | wrong to do anything with it immediately, because a single block of | |
156 | text might be broken up into multiple calls to this function. */ | |
157 | ||
4895cde2 PA |
158 | void |
159 | gdb_xml_parser::body_text (const XML_Char *text, int length) | |
160 | { | |
161 | if (m_error.reason < 0) | |
162 | return; | |
163 | ||
164 | scope_level &scope = m_scopes.back (); | |
165 | scope.body.append (text, length); | |
166 | } | |
167 | ||
e776119f DJ |
168 | static void |
169 | gdb_xml_body_text (void *data, const XML_Char *text, int length) | |
170 | { | |
19ba03f4 | 171 | struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data; |
e776119f | 172 | |
4895cde2 | 173 | parser->body_text (text, length); |
e776119f | 174 | } |
fd79ecee | 175 | |
e776119f | 176 | /* Issue a debugging message from one of PARSER's handlers. */ |
fd79ecee | 177 | |
e776119f | 178 | void |
4895cde2 | 179 | gdb_xml_parser::vdebug (const char *format, va_list ap) |
e776119f | 180 | { |
4895cde2 | 181 | int line = XML_GetCurrentLineNumber (m_expat_parser); |
e776119f DJ |
182 | char *message; |
183 | ||
e776119f DJ |
184 | message = xstrvprintf (format, ap); |
185 | if (line) | |
186 | fprintf_unfiltered (gdb_stderr, "%s (line %d): %s\n", | |
4895cde2 | 187 | m_name, line, message); |
e776119f DJ |
188 | else |
189 | fprintf_unfiltered (gdb_stderr, "%s: %s\n", | |
4895cde2 | 190 | m_name, message); |
e776119f DJ |
191 | xfree (message); |
192 | } | |
193 | ||
4895cde2 PA |
194 | void |
195 | gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...) | |
196 | { | |
197 | if (!debug_xml) | |
198 | return; | |
199 | ||
200 | va_list ap; | |
201 | va_start (ap, format); | |
202 | parser->vdebug (format, ap); | |
203 | va_end (ap); | |
204 | } | |
205 | ||
e776119f DJ |
206 | /* Issue an error message from one of PARSER's handlers, and stop |
207 | parsing. */ | |
208 | ||
4895cde2 PA |
209 | void |
210 | gdb_xml_parser::verror (const char *format, va_list ap) | |
211 | { | |
212 | int line = XML_GetCurrentLineNumber (m_expat_parser); | |
213 | ||
214 | m_last_line = line; | |
215 | throw_verror (XML_PARSE_ERROR, format, ap); | |
216 | } | |
217 | ||
e776119f DJ |
218 | void |
219 | gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...) | |
220 | { | |
e776119f | 221 | va_list ap; |
e776119f | 222 | va_start (ap, format); |
4895cde2 PA |
223 | parser->verror (format, ap); |
224 | va_end (ap); | |
e776119f DJ |
225 | } |
226 | ||
3d2c1d41 PA |
227 | /* Find the attribute named NAME in the set of parsed attributes |
228 | ATTRIBUTES. Returns NULL if not found. */ | |
229 | ||
230 | struct gdb_xml_value * | |
231 | xml_find_attribute (VEC(gdb_xml_value_s) *attributes, const char *name) | |
232 | { | |
233 | struct gdb_xml_value *value; | |
234 | int ix; | |
235 | ||
236 | for (ix = 0; VEC_iterate (gdb_xml_value_s, attributes, ix, value); ix++) | |
237 | if (strcmp (value->name, name) == 0) | |
238 | return value; | |
239 | ||
240 | return NULL; | |
241 | } | |
242 | ||
e776119f DJ |
243 | /* Clean up a vector of parsed attribute values. */ |
244 | ||
245 | static void | |
246 | gdb_xml_values_cleanup (void *data) | |
247 | { | |
19ba03f4 | 248 | VEC(gdb_xml_value_s) **values = (VEC(gdb_xml_value_s) **) data; |
e776119f DJ |
249 | struct gdb_xml_value *value; |
250 | int ix; | |
251 | ||
252 | for (ix = 0; VEC_iterate (gdb_xml_value_s, *values, ix, value); ix++) | |
253 | xfree (value->value); | |
254 | VEC_free (gdb_xml_value_s, *values); | |
255 | } | |
256 | ||
4895cde2 PA |
257 | /* Handle the start of an element. NAME is the element, and ATTRS are |
258 | the names and values of this element's attributes. */ | |
e776119f | 259 | |
4895cde2 PA |
260 | void |
261 | gdb_xml_parser::start_element (const XML_Char *name, | |
262 | const XML_Char **attrs) | |
fd79ecee | 263 | { |
4895cde2 PA |
264 | if (m_error.reason < 0) |
265 | return; | |
266 | ||
e776119f DJ |
267 | const struct gdb_xml_element *element; |
268 | const struct gdb_xml_attribute *attribute; | |
269 | VEC(gdb_xml_value_s) *attributes = NULL; | |
270 | unsigned int seen; | |
271 | struct cleanup *back_to; | |
272 | ||
e776119f DJ |
273 | /* Push an error scope. If we return or throw an exception before |
274 | filling this in, it will tell us to ignore children of this | |
a7fc9b61 PA |
275 | element. Note we don't take a reference to the element yet |
276 | because further below we'll process the element which may recurse | |
277 | back here and push more elements to the vector. When the | |
278 | recursion unrolls all such elements will have been popped back | |
279 | already, but if one of those pushes reallocates the vector, | |
280 | previous element references will be invalidated. */ | |
4895cde2 | 281 | m_scopes.emplace_back (); |
a7fc9b61 PA |
282 | |
283 | /* Get a reference to the current scope. */ | |
4895cde2 | 284 | scope_level &scope = m_scopes[m_scopes.size () - 2]; |
e776119f | 285 | |
4895cde2 | 286 | gdb_xml_debug (this, _("Entering element <%s>"), name); |
e776119f DJ |
287 | |
288 | /* Find this element in the list of the current scope's allowed | |
289 | children. Record that we've seen it. */ | |
290 | ||
291 | seen = 1; | |
a7fc9b61 | 292 | for (element = scope.elements; element && element->name; |
e776119f DJ |
293 | element++, seen <<= 1) |
294 | if (strcmp (element->name, name) == 0) | |
295 | break; | |
296 | ||
297 | if (element == NULL || element->name == NULL) | |
298 | { | |
108546a0 DJ |
299 | /* If we're working on XInclude, <xi:include> can be the child |
300 | of absolutely anything. Copy the previous scope's element | |
301 | list into the new scope even if there was no match. */ | |
4895cde2 | 302 | if (m_is_xinclude) |
108546a0 | 303 | { |
4895cde2 | 304 | XML_DefaultCurrent (m_expat_parser); |
108546a0 | 305 | |
4895cde2 | 306 | scope_level &unknown_scope = m_scopes.back (); |
a7fc9b61 | 307 | unknown_scope.elements = scope.elements; |
108546a0 DJ |
308 | return; |
309 | } | |
310 | ||
4895cde2 | 311 | gdb_xml_debug (this, _("Element <%s> unknown"), name); |
e776119f DJ |
312 | return; |
313 | } | |
314 | ||
a7fc9b61 | 315 | if (!(element->flags & GDB_XML_EF_REPEATABLE) && (seen & scope.seen)) |
4895cde2 | 316 | gdb_xml_error (this, _("Element <%s> only expected once"), name); |
e776119f | 317 | |
a7fc9b61 | 318 | scope.seen |= seen; |
e776119f | 319 | |
108546a0 DJ |
320 | back_to = make_cleanup (gdb_xml_values_cleanup, &attributes); |
321 | ||
e776119f DJ |
322 | for (attribute = element->attributes; |
323 | attribute != NULL && attribute->name != NULL; | |
324 | attribute++) | |
325 | { | |
326 | const char *val = NULL; | |
327 | const XML_Char **p; | |
328 | void *parsed_value; | |
329 | struct gdb_xml_value new_value; | |
330 | ||
331 | for (p = attrs; *p != NULL; p += 2) | |
332 | if (!strcmp (attribute->name, p[0])) | |
333 | { | |
334 | val = p[1]; | |
335 | break; | |
336 | } | |
337 | ||
338 | if (*p != NULL && val == NULL) | |
339 | { | |
4895cde2 | 340 | gdb_xml_debug (this, _("Attribute \"%s\" missing a value"), |
e776119f DJ |
341 | attribute->name); |
342 | continue; | |
343 | } | |
344 | ||
345 | if (*p == NULL && !(attribute->flags & GDB_XML_AF_OPTIONAL)) | |
346 | { | |
4895cde2 | 347 | gdb_xml_error (this, _("Required attribute \"%s\" of " |
e776119f DJ |
348 | "<%s> not specified"), |
349 | attribute->name, element->name); | |
350 | continue; | |
351 | } | |
352 | ||
353 | if (*p == NULL) | |
354 | continue; | |
355 | ||
4895cde2 | 356 | gdb_xml_debug (this, _("Parsing attribute %s=\"%s\""), |
e776119f DJ |
357 | attribute->name, val); |
358 | ||
359 | if (attribute->handler) | |
4895cde2 | 360 | parsed_value = attribute->handler (this, attribute, val); |
e776119f DJ |
361 | else |
362 | parsed_value = xstrdup (val); | |
363 | ||
364 | new_value.name = attribute->name; | |
365 | new_value.value = parsed_value; | |
366 | VEC_safe_push (gdb_xml_value_s, attributes, &new_value); | |
367 | } | |
368 | ||
369 | /* Check for unrecognized attributes. */ | |
370 | if (debug_xml) | |
fd79ecee | 371 | { |
e776119f DJ |
372 | const XML_Char **p; |
373 | ||
374 | for (p = attrs; *p != NULL; p += 2) | |
375 | { | |
376 | for (attribute = element->attributes; | |
377 | attribute != NULL && attribute->name != NULL; | |
378 | attribute++) | |
379 | if (strcmp (attribute->name, *p) == 0) | |
380 | break; | |
381 | ||
382 | if (attribute == NULL || attribute->name == NULL) | |
4895cde2 | 383 | gdb_xml_debug (this, _("Ignoring unknown attribute %s"), *p); |
e776119f DJ |
384 | } |
385 | } | |
386 | ||
387 | /* Call the element handler if there is one. */ | |
388 | if (element->start_handler) | |
4895cde2 | 389 | element->start_handler (this, element, m_user_data, attributes); |
e776119f | 390 | |
a7fc9b61 PA |
391 | /* Fill in a new scope level. Note that we must delay getting a |
392 | back reference till here because above we might have recursed, | |
393 | which may have reallocated the vector which invalidates | |
394 | iterators/pointers/references. */ | |
4895cde2 | 395 | scope_level &new_scope = m_scopes.back (); |
a7fc9b61 PA |
396 | new_scope.element = element; |
397 | new_scope.elements = element->children; | |
e776119f DJ |
398 | |
399 | do_cleanups (back_to); | |
400 | } | |
401 | ||
402 | /* Wrapper for gdb_xml_start_element, to prevent throwing exceptions | |
403 | through expat. */ | |
404 | ||
405 | static void | |
406 | gdb_xml_start_element_wrapper (void *data, const XML_Char *name, | |
407 | const XML_Char **attrs) | |
408 | { | |
19ba03f4 | 409 | struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data; |
e776119f | 410 | |
492d29ea | 411 | TRY |
e776119f | 412 | { |
4895cde2 | 413 | parser->start_element (name, attrs); |
e776119f | 414 | } |
492d29ea | 415 | CATCH (ex, RETURN_MASK_ALL) |
e776119f | 416 | { |
4895cde2 | 417 | parser->set_error (ex); |
fd79ecee | 418 | } |
492d29ea | 419 | END_CATCH |
e776119f DJ |
420 | } |
421 | ||
4895cde2 | 422 | /* Handle the end of an element. NAME is the current element. */ |
e776119f | 423 | |
4895cde2 PA |
424 | void |
425 | gdb_xml_parser::end_element (const XML_Char *name) | |
e776119f | 426 | { |
4895cde2 PA |
427 | if (m_error.reason < 0) |
428 | return; | |
429 | ||
430 | struct scope_level *scope = &m_scopes.back (); | |
e776119f DJ |
431 | const struct gdb_xml_element *element; |
432 | unsigned int seen; | |
e776119f | 433 | |
4895cde2 | 434 | gdb_xml_debug (this, _("Leaving element <%s>"), name); |
e776119f DJ |
435 | |
436 | for (element = scope->elements, seen = 1; | |
437 | element != NULL && element->name != NULL; | |
438 | element++, seen <<= 1) | |
439 | if ((scope->seen & seen) == 0 | |
440 | && (element->flags & GDB_XML_EF_OPTIONAL) == 0) | |
4895cde2 | 441 | gdb_xml_error (this, _("Required element <%s> is missing"), |
e776119f DJ |
442 | element->name); |
443 | ||
581e13c1 | 444 | /* Call the element processor. */ |
108546a0 | 445 | if (scope->element != NULL && scope->element->end_handler) |
e776119f | 446 | { |
bd8a901f | 447 | const char *body; |
e776119f | 448 | |
bd8a901f PA |
449 | if (scope->body.empty ()) |
450 | body = ""; | |
108546a0 DJ |
451 | else |
452 | { | |
453 | int length; | |
e776119f | 454 | |
bd8a901f PA |
455 | length = scope->body.size (); |
456 | body = scope->body.c_str (); | |
e776119f | 457 | |
108546a0 | 458 | /* Strip leading and trailing whitespace. */ |
bd8a901f PA |
459 | while (length > 0 && ISSPACE (body[length - 1])) |
460 | length--; | |
461 | scope->body.erase (length); | |
108546a0 DJ |
462 | while (*body && ISSPACE (*body)) |
463 | body++; | |
464 | } | |
465 | ||
4895cde2 PA |
466 | scope->element->end_handler (this, scope->element, |
467 | m_user_data, body); | |
108546a0 DJ |
468 | } |
469 | else if (scope->element == NULL) | |
4895cde2 | 470 | XML_DefaultCurrent (m_expat_parser); |
e776119f DJ |
471 | |
472 | /* Pop the scope level. */ | |
4895cde2 | 473 | m_scopes.pop_back (); |
e776119f DJ |
474 | } |
475 | ||
476 | /* Wrapper for gdb_xml_end_element, to prevent throwing exceptions | |
477 | through expat. */ | |
478 | ||
479 | static void | |
480 | gdb_xml_end_element_wrapper (void *data, const XML_Char *name) | |
481 | { | |
19ba03f4 | 482 | struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data; |
e776119f | 483 | |
492d29ea | 484 | TRY |
e776119f | 485 | { |
4895cde2 | 486 | parser->end_element (name); |
e776119f | 487 | } |
492d29ea | 488 | CATCH (ex, RETURN_MASK_ALL) |
e776119f | 489 | { |
4895cde2 | 490 | parser->set_error (ex); |
e776119f | 491 | } |
492d29ea | 492 | END_CATCH |
e776119f DJ |
493 | } |
494 | ||
495 | /* Free a parser and all its associated state. */ | |
496 | ||
010151c9 | 497 | gdb_xml_parser::~gdb_xml_parser () |
e776119f | 498 | { |
4895cde2 | 499 | XML_ParserFree (m_expat_parser); |
e776119f DJ |
500 | } |
501 | ||
010151c9 PA |
502 | /* Initialize a parser. */ |
503 | ||
4895cde2 | 504 | gdb_xml_parser::gdb_xml_parser (const char *name, |
010151c9 | 505 | const gdb_xml_element *elements, |
4895cde2 PA |
506 | void *user_data) |
507 | : m_name (name), | |
508 | m_user_data (user_data), | |
509 | m_error (exception_none), | |
510 | m_last_line (0), | |
511 | m_dtd_name (NULL), | |
512 | m_is_xinclude (false) | |
513 | { | |
514 | m_expat_parser = XML_ParserCreateNS (NULL, '!'); | |
515 | if (m_expat_parser == NULL) | |
010151c9 | 516 | malloc_failure (0); |
e776119f | 517 | |
4895cde2 | 518 | XML_SetUserData (m_expat_parser, this); |
e776119f DJ |
519 | |
520 | /* Set the callbacks. */ | |
4895cde2 | 521 | XML_SetElementHandler (m_expat_parser, gdb_xml_start_element_wrapper, |
e776119f | 522 | gdb_xml_end_element_wrapper); |
4895cde2 | 523 | XML_SetCharacterDataHandler (m_expat_parser, gdb_xml_body_text); |
e776119f DJ |
524 | |
525 | /* Initialize the outer scope. */ | |
4895cde2 | 526 | m_scopes.emplace_back (elements); |
efc0eabd PA |
527 | } |
528 | ||
108546a0 DJ |
529 | /* External entity handler. The only external entities we support |
530 | are those compiled into GDB (we do not fetch entities from the | |
531 | target). */ | |
532 | ||
533 | static int XMLCALL | |
534 | gdb_xml_fetch_external_entity (XML_Parser expat_parser, | |
535 | const XML_Char *context, | |
536 | const XML_Char *base, | |
537 | const XML_Char *systemId, | |
538 | const XML_Char *publicId) | |
539 | { | |
108546a0 DJ |
540 | XML_Parser entity_parser; |
541 | const char *text; | |
542 | enum XML_Status status; | |
543 | ||
544 | if (systemId == NULL) | |
545 | { | |
4895cde2 PA |
546 | gdb_xml_parser *parser |
547 | = (gdb_xml_parser *) XML_GetUserData (expat_parser); | |
548 | ||
549 | text = fetch_xml_builtin (parser->dtd_name ()); | |
108546a0 | 550 | if (text == NULL) |
9b20d036 MS |
551 | internal_error (__FILE__, __LINE__, |
552 | _("could not locate built-in DTD %s"), | |
4895cde2 | 553 | parser->dtd_name ()); |
108546a0 DJ |
554 | } |
555 | else | |
556 | { | |
557 | text = fetch_xml_builtin (systemId); | |
558 | if (text == NULL) | |
559 | return XML_STATUS_ERROR; | |
560 | } | |
561 | ||
4895cde2 PA |
562 | entity_parser = XML_ExternalEntityParserCreate (expat_parser, |
563 | context, NULL); | |
108546a0 DJ |
564 | |
565 | /* Don't use our handlers for the contents of the DTD. Just let expat | |
566 | process it. */ | |
567 | XML_SetElementHandler (entity_parser, NULL, NULL); | |
568 | XML_SetDoctypeDeclHandler (entity_parser, NULL, NULL); | |
569 | XML_SetXmlDeclHandler (entity_parser, NULL); | |
570 | XML_SetDefaultHandler (entity_parser, NULL); | |
571 | XML_SetUserData (entity_parser, NULL); | |
572 | ||
573 | status = XML_Parse (entity_parser, text, strlen (text), 1); | |
574 | ||
575 | XML_ParserFree (entity_parser); | |
576 | return status; | |
577 | } | |
578 | ||
108546a0 | 579 | void |
4895cde2 | 580 | gdb_xml_parser::use_dtd (const char *dtd_name) |
108546a0 DJ |
581 | { |
582 | enum XML_Error err; | |
583 | ||
4895cde2 | 584 | m_dtd_name = dtd_name; |
108546a0 | 585 | |
4895cde2 | 586 | XML_SetParamEntityParsing (m_expat_parser, |
108546a0 | 587 | XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); |
4895cde2 | 588 | XML_SetExternalEntityRefHandler (m_expat_parser, |
108546a0 DJ |
589 | gdb_xml_fetch_external_entity); |
590 | ||
591 | /* Even if no DTD is provided, use the built-in DTD anyway. */ | |
4895cde2 | 592 | err = XML_UseForeignDTD (m_expat_parser, XML_TRUE); |
108546a0 DJ |
593 | if (err != XML_ERROR_NONE) |
594 | internal_error (__FILE__, __LINE__, | |
9b20d036 MS |
595 | _("XML_UseForeignDTD failed: %s"), |
596 | XML_ErrorString (err)); | |
108546a0 DJ |
597 | } |
598 | ||
e776119f DJ |
599 | /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which |
600 | should be NUL-terminated. | |
601 | ||
602 | The return value is 0 for success or -1 for error. It may throw, | |
603 | but only if something unexpected goes wrong during parsing; parse | |
604 | errors will be caught, warned about, and reported as failure. */ | |
605 | ||
606 | int | |
4895cde2 | 607 | gdb_xml_parser::parse (const char *buffer) |
e776119f DJ |
608 | { |
609 | enum XML_Status status; | |
610 | const char *error_string; | |
611 | ||
4895cde2 | 612 | gdb_xml_debug (this, _("Starting:\n%s"), buffer); |
de584861 | 613 | |
4895cde2 | 614 | status = XML_Parse (m_expat_parser, buffer, strlen (buffer), 1); |
e776119f | 615 | |
4895cde2 | 616 | if (status == XML_STATUS_OK && m_error.reason == 0) |
e776119f DJ |
617 | return 0; |
618 | ||
4895cde2 PA |
619 | if (m_error.reason == RETURN_ERROR |
620 | && m_error.error == XML_PARSE_ERROR) | |
e776119f | 621 | { |
4895cde2 PA |
622 | gdb_assert (m_error.message != NULL); |
623 | error_string = m_error.message; | |
e776119f DJ |
624 | } |
625 | else if (status == XML_STATUS_ERROR) | |
626 | { | |
4895cde2 | 627 | enum XML_Error err = XML_GetErrorCode (m_expat_parser); |
a109c7c1 | 628 | |
e776119f DJ |
629 | error_string = XML_ErrorString (err); |
630 | } | |
631 | else | |
632 | { | |
4895cde2 PA |
633 | gdb_assert (m_error.reason < 0); |
634 | throw_exception (m_error); | |
e776119f DJ |
635 | } |
636 | ||
4895cde2 PA |
637 | if (m_last_line != 0) |
638 | warning (_("while parsing %s (at line %d): %s"), m_name, | |
639 | m_last_line, error_string); | |
e776119f | 640 | else |
4895cde2 | 641 | warning (_("while parsing %s: %s"), m_name, error_string); |
e776119f DJ |
642 | |
643 | return -1; | |
fd79ecee DJ |
644 | } |
645 | ||
efc0eabd PA |
646 | int |
647 | gdb_xml_parse_quick (const char *name, const char *dtd_name, | |
648 | const struct gdb_xml_element *elements, | |
649 | const char *document, void *user_data) | |
650 | { | |
010151c9 | 651 | gdb_xml_parser parser (name, elements, user_data); |
efc0eabd | 652 | if (dtd_name != NULL) |
4895cde2 PA |
653 | parser.use_dtd (dtd_name); |
654 | return parser.parse (document); | |
efc0eabd PA |
655 | } |
656 | ||
fd79ecee DJ |
657 | /* Parse a field VALSTR that we expect to contain an integer value. |
658 | The integer is returned in *VALP. The string is parsed with an | |
659 | equivalent to strtoul. | |
660 | ||
661 | Returns 0 for success, -1 for error. */ | |
662 | ||
663 | static int | |
664 | xml_parse_unsigned_integer (const char *valstr, ULONGEST *valp) | |
665 | { | |
666 | const char *endptr; | |
667 | ULONGEST result; | |
668 | ||
669 | if (*valstr == '\0') | |
670 | return -1; | |
671 | ||
672 | result = strtoulst (valstr, &endptr, 0); | |
673 | if (*endptr != '\0') | |
674 | return -1; | |
675 | ||
676 | *valp = result; | |
677 | return 0; | |
678 | } | |
679 | ||
e776119f DJ |
680 | /* Parse an integer string into a ULONGEST and return it, or call |
681 | gdb_xml_error if it could not be parsed. */ | |
fd79ecee DJ |
682 | |
683 | ULONGEST | |
e776119f | 684 | gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, const char *value) |
fd79ecee DJ |
685 | { |
686 | ULONGEST result; | |
fd79ecee DJ |
687 | |
688 | if (xml_parse_unsigned_integer (value, &result) != 0) | |
e776119f DJ |
689 | gdb_xml_error (parser, _("Can't convert \"%s\" to an integer"), value); |
690 | ||
fd79ecee DJ |
691 | return result; |
692 | } | |
693 | ||
e776119f DJ |
694 | /* Parse an integer attribute into a ULONGEST. */ |
695 | ||
696 | void * | |
697 | gdb_xml_parse_attr_ulongest (struct gdb_xml_parser *parser, | |
698 | const struct gdb_xml_attribute *attribute, | |
699 | const char *value) | |
700 | { | |
701 | ULONGEST result; | |
702 | void *ret; | |
fd79ecee | 703 | |
e776119f DJ |
704 | if (xml_parse_unsigned_integer (value, &result) != 0) |
705 | gdb_xml_error (parser, _("Can't convert %s=\"%s\" to an integer"), | |
706 | attribute->name, value); | |
fd79ecee | 707 | |
8d749320 | 708 | ret = XNEW (ULONGEST); |
e776119f DJ |
709 | memcpy (ret, &result, sizeof (result)); |
710 | return ret; | |
711 | } | |
712 | ||
123dc839 DJ |
713 | /* A handler_data for yes/no boolean values. */ |
714 | ||
715 | const struct gdb_xml_enum gdb_xml_enums_boolean[] = { | |
716 | { "yes", 1 }, | |
717 | { "no", 0 }, | |
718 | { NULL, 0 } | |
719 | }; | |
720 | ||
e776119f DJ |
721 | /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the |
722 | value of handler_data when using gdb_xml_parse_attr_enum to parse a | |
723 | fixed list of possible strings. The list is terminated by an entry | |
724 | with NAME == NULL. */ | |
725 | ||
726 | void * | |
727 | gdb_xml_parse_attr_enum (struct gdb_xml_parser *parser, | |
728 | const struct gdb_xml_attribute *attribute, | |
729 | const char *value) | |
fd79ecee | 730 | { |
19ba03f4 SM |
731 | const struct gdb_xml_enum *enums |
732 | = (const struct gdb_xml_enum *) attribute->handler_data; | |
e776119f | 733 | void *ret; |
fd79ecee | 734 | |
19ba03f4 SM |
735 | for (enums = (const struct gdb_xml_enum *) attribute->handler_data; |
736 | enums->name != NULL; enums++) | |
123dc839 | 737 | if (strcasecmp (enums->name, value) == 0) |
e776119f DJ |
738 | break; |
739 | ||
740 | if (enums->name == NULL) | |
741 | gdb_xml_error (parser, _("Unknown attribute value %s=\"%s\""), | |
742 | attribute->name, value); | |
743 | ||
744 | ret = xmalloc (sizeof (enums->value)); | |
745 | memcpy (ret, &enums->value, sizeof (enums->value)); | |
746 | return ret; | |
fd79ecee | 747 | } |
108546a0 DJ |
748 | \f |
749 | ||
750 | /* XInclude processing. This is done as a separate step from actually | |
751 | parsing the document, so that we can produce a single combined XML | |
752 | document - e.g. to hand to a front end or to simplify comparing two | |
753 | documents. We make extensive use of XML_DefaultCurrent, to pass | |
754 | input text directly into the output without reformatting or | |
755 | requoting it. | |
756 | ||
757 | We output the DOCTYPE declaration for the first document unchanged, | |
758 | if present, and discard DOCTYPEs from included documents. Only the | |
759 | one we pass through here is used when we feed the result back to | |
760 | expat. The XInclude standard explicitly does not discuss | |
761 | validation of the result; we choose to apply the same DTD applied | |
762 | to the outermost document. | |
763 | ||
764 | We can not simply include the external DTD subset in the document | |
765 | as an internal subset, because <!IGNORE> and <!INCLUDE> are valid | |
766 | only in external subsets. But if we do not pass the DTD into the | |
767 | output at all, default values will not be filled in. | |
768 | ||
769 | We don't pass through any <?xml> declaration because we generate | |
770 | UTF-8, not whatever the input encoding was. */ | |
771 | ||
772 | struct xinclude_parsing_data | |
773 | { | |
bd8a901f PA |
774 | xinclude_parsing_data (std::string &output_, |
775 | xml_fetch_another fetcher_, void *fetcher_baton_, | |
010151c9 | 776 | int include_depth_) |
bd8a901f PA |
777 | : output (output_), |
778 | skip_depth (0), | |
010151c9 PA |
779 | include_depth (include_depth_), |
780 | fetcher (fetcher_), | |
781 | fetcher_baton (fetcher_baton_) | |
bd8a901f | 782 | {} |
010151c9 | 783 | |
bd8a901f PA |
784 | /* Where the output goes. */ |
785 | std::string &output; | |
108546a0 DJ |
786 | |
787 | /* A count indicating whether we are in an element whose | |
788 | children should not be copied to the output, and if so, | |
789 | how deep we are nested. This is used for anything inside | |
790 | an xi:include, and for the DTD. */ | |
791 | int skip_depth; | |
792 | ||
793 | /* The number of <xi:include> elements currently being processed, | |
794 | to detect loops. */ | |
795 | int include_depth; | |
796 | ||
797 | /* A function to call to obtain additional features, and its | |
798 | baton. */ | |
799 | xml_fetch_another fetcher; | |
800 | void *fetcher_baton; | |
801 | }; | |
802 | ||
803 | static void | |
804 | xinclude_start_include (struct gdb_xml_parser *parser, | |
805 | const struct gdb_xml_element *element, | |
806 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
807 | { | |
19ba03f4 SM |
808 | struct xinclude_parsing_data *data |
809 | = (struct xinclude_parsing_data *) user_data; | |
810 | char *href = (char *) xml_find_attribute (attributes, "href")->value; | |
108546a0 DJ |
811 | |
812 | gdb_xml_debug (parser, _("Processing XInclude of \"%s\""), href); | |
813 | ||
814 | if (data->include_depth > MAX_XINCLUDE_DEPTH) | |
815 | gdb_xml_error (parser, _("Maximum XInclude depth (%d) exceeded"), | |
816 | MAX_XINCLUDE_DEPTH); | |
817 | ||
b7b030ad TT |
818 | gdb::unique_xmalloc_ptr<char> text = data->fetcher (href, |
819 | data->fetcher_baton); | |
108546a0 DJ |
820 | if (text == NULL) |
821 | gdb_xml_error (parser, _("Could not load XML document \"%s\""), href); | |
108546a0 | 822 | |
4895cde2 | 823 | if (!xml_process_xincludes (data->output, parser->name (), |
b7b030ad | 824 | text.get (), data->fetcher, |
bd8a901f PA |
825 | data->fetcher_baton, |
826 | data->include_depth + 1)) | |
108546a0 DJ |
827 | gdb_xml_error (parser, _("Parsing \"%s\" failed"), href); |
828 | ||
108546a0 DJ |
829 | data->skip_depth++; |
830 | } | |
831 | ||
832 | static void | |
833 | xinclude_end_include (struct gdb_xml_parser *parser, | |
834 | const struct gdb_xml_element *element, | |
835 | void *user_data, const char *body_text) | |
836 | { | |
19ba03f4 SM |
837 | struct xinclude_parsing_data *data |
838 | = (struct xinclude_parsing_data *) user_data; | |
108546a0 DJ |
839 | |
840 | data->skip_depth--; | |
841 | } | |
842 | ||
843 | static void XMLCALL | |
844 | xml_xinclude_default (void *data_, const XML_Char *s, int len) | |
845 | { | |
19ba03f4 | 846 | struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_; |
4895cde2 | 847 | xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data (); |
108546a0 DJ |
848 | |
849 | /* If we are inside of e.g. xi:include or the DTD, don't save this | |
850 | string. */ | |
851 | if (data->skip_depth) | |
852 | return; | |
853 | ||
854 | /* Otherwise just add it to the end of the document we're building | |
855 | up. */ | |
bd8a901f | 856 | data->output.append (s, len); |
108546a0 DJ |
857 | } |
858 | ||
859 | static void XMLCALL | |
860 | xml_xinclude_start_doctype (void *data_, const XML_Char *doctypeName, | |
861 | const XML_Char *sysid, const XML_Char *pubid, | |
862 | int has_internal_subset) | |
863 | { | |
19ba03f4 | 864 | struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_; |
4895cde2 | 865 | xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data (); |
108546a0 DJ |
866 | |
867 | /* Don't print out the doctype, or the contents of the DTD internal | |
868 | subset, if any. */ | |
869 | data->skip_depth++; | |
870 | } | |
871 | ||
872 | static void XMLCALL | |
873 | xml_xinclude_end_doctype (void *data_) | |
874 | { | |
19ba03f4 | 875 | struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_; |
4895cde2 | 876 | xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data (); |
108546a0 DJ |
877 | |
878 | data->skip_depth--; | |
879 | } | |
880 | ||
881 | static void XMLCALL | |
882 | xml_xinclude_xml_decl (void *data_, const XML_Char *version, | |
883 | const XML_Char *encoding, int standalone) | |
884 | { | |
885 | /* Do nothing - this function prevents the default handler from | |
886 | being called, thus suppressing the XML declaration from the | |
887 | output. */ | |
888 | } | |
889 | ||
108546a0 DJ |
890 | const struct gdb_xml_attribute xinclude_attributes[] = { |
891 | { "href", GDB_XML_AF_NONE, NULL, NULL }, | |
892 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
893 | }; | |
894 | ||
895 | const struct gdb_xml_element xinclude_elements[] = { | |
896 | { "http://www.w3.org/2001/XInclude!include", xinclude_attributes, NULL, | |
897 | GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE, | |
898 | xinclude_start_include, xinclude_end_include }, | |
899 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
900 | }; | |
901 | ||
902 | /* The main entry point for <xi:include> processing. */ | |
903 | ||
bd8a901f PA |
904 | bool |
905 | xml_process_xincludes (std::string &result, | |
906 | const char *name, const char *text, | |
108546a0 DJ |
907 | xml_fetch_another fetcher, void *fetcher_baton, |
908 | int depth) | |
909 | { | |
bd8a901f | 910 | xinclude_parsing_data data (result, fetcher, fetcher_baton, depth); |
108546a0 | 911 | |
010151c9 | 912 | gdb_xml_parser parser (name, xinclude_elements, &data); |
4895cde2 | 913 | parser.set_is_xinclude (true); |
108546a0 | 914 | |
4895cde2 PA |
915 | XML_SetCharacterDataHandler (parser.expat_parser (), NULL); |
916 | XML_SetDefaultHandler (parser.expat_parser (), xml_xinclude_default); | |
108546a0 DJ |
917 | |
918 | /* Always discard the XML version declarations; the only important | |
919 | thing this provides is encoding, and our result will have been | |
920 | converted to UTF-8. */ | |
4895cde2 | 921 | XML_SetXmlDeclHandler (parser.expat_parser (), xml_xinclude_xml_decl); |
108546a0 DJ |
922 | |
923 | if (depth > 0) | |
924 | /* Discard the doctype for included documents. */ | |
4895cde2 | 925 | XML_SetDoctypeDeclHandler (parser.expat_parser (), |
108546a0 DJ |
926 | xml_xinclude_start_doctype, |
927 | xml_xinclude_end_doctype); | |
928 | ||
4895cde2 | 929 | parser.use_dtd ("xinclude.dtd"); |
108546a0 | 930 | |
4895cde2 | 931 | if (parser.parse (text) == 0) |
108546a0 | 932 | { |
108546a0 | 933 | if (depth == 0) |
010151c9 | 934 | gdb_xml_debug (&parser, _("XInclude processing succeeded.")); |
bd8a901f | 935 | return true; |
108546a0 | 936 | } |
108546a0 | 937 | |
bd8a901f | 938 | return false; |
108546a0 | 939 | } |
05a4558a | 940 | #endif /* HAVE_LIBEXPAT */ |
108546a0 DJ |
941 | \f |
942 | ||
943 | /* Return an XML document which was compiled into GDB, from | |
944 | the given FILENAME, or NULL if the file was not compiled in. */ | |
945 | ||
946 | const char * | |
947 | fetch_xml_builtin (const char *filename) | |
948 | { | |
949 | const char *(*p)[2]; | |
950 | ||
951 | for (p = xml_builtin; (*p)[0]; p++) | |
952 | if (strcmp ((*p)[0], filename) == 0) | |
953 | return (*p)[1]; | |
954 | ||
955 | return NULL; | |
956 | } | |
fd79ecee | 957 | |
05a4558a DJ |
958 | /* A to_xfer_partial helper function which reads XML files which were |
959 | compiled into GDB. The target may call this function from its own | |
960 | to_xfer_partial handler, after converting object and annex to the | |
961 | appropriate filename. */ | |
962 | ||
963 | LONGEST | |
964 | xml_builtin_xfer_partial (const char *filename, | |
965 | gdb_byte *readbuf, const gdb_byte *writebuf, | |
966 | ULONGEST offset, LONGEST len) | |
967 | { | |
968 | const char *buf; | |
969 | LONGEST len_avail; | |
970 | ||
971 | gdb_assert (readbuf != NULL && writebuf == NULL); | |
972 | gdb_assert (filename != NULL); | |
973 | ||
974 | buf = fetch_xml_builtin (filename); | |
975 | if (buf == NULL) | |
976 | return -1; | |
977 | ||
978 | len_avail = strlen (buf); | |
979 | if (offset >= len_avail) | |
980 | return 0; | |
981 | ||
982 | if (len > len_avail - offset) | |
983 | len = len_avail - offset; | |
984 | memcpy (readbuf, buf + offset, len); | |
985 | return len; | |
986 | } | |
987 | \f | |
fd79ecee DJ |
988 | |
989 | static void | |
e776119f DJ |
990 | show_debug_xml (struct ui_file *file, int from_tty, |
991 | struct cmd_list_element *c, const char *value) | |
fd79ecee | 992 | { |
e776119f | 993 | fprintf_filtered (file, _("XML debugging is %s.\n"), value); |
fd79ecee DJ |
994 | } |
995 | ||
b7b030ad | 996 | gdb::unique_xmalloc_ptr<char> |
a96d9b2e SDJ |
997 | xml_fetch_content_from_file (const char *filename, void *baton) |
998 | { | |
19ba03f4 | 999 | const char *dirname = (const char *) baton; |
d419f42d | 1000 | gdb_file_up file; |
a96d9b2e SDJ |
1001 | |
1002 | if (dirname && *dirname) | |
1003 | { | |
1004 | char *fullname = concat (dirname, "/", filename, (char *) NULL); | |
a109c7c1 | 1005 | |
a96d9b2e | 1006 | if (fullname == NULL) |
d26e3629 | 1007 | malloc_failure (0); |
614c279d | 1008 | file = gdb_fopen_cloexec (fullname, FOPEN_RT); |
a96d9b2e SDJ |
1009 | xfree (fullname); |
1010 | } | |
1011 | else | |
614c279d | 1012 | file = gdb_fopen_cloexec (filename, FOPEN_RT); |
a96d9b2e SDJ |
1013 | |
1014 | if (file == NULL) | |
1015 | return NULL; | |
1016 | ||
2edf834e | 1017 | /* Read in the whole file. */ |
a96d9b2e | 1018 | |
2edf834e | 1019 | size_t len; |
a96d9b2e | 1020 | |
2edf834e PA |
1021 | if (fseek (file.get (), 0, SEEK_END) == -1) |
1022 | perror_with_name (_("seek to end of file")); | |
1023 | len = ftell (file.get ()); | |
1024 | rewind (file.get ()); | |
a96d9b2e | 1025 | |
2edf834e | 1026 | gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len + 1)); |
a96d9b2e | 1027 | |
63929e84 PA |
1028 | if (fread (text.get (), 1, len, file.get ()) != len |
1029 | || ferror (file.get ())) | |
2edf834e PA |
1030 | { |
1031 | warning (_("Read error from \"%s\""), filename); | |
1032 | return NULL; | |
a96d9b2e SDJ |
1033 | } |
1034 | ||
2edf834e | 1035 | text.get ()[len] = '\0'; |
a96d9b2e SDJ |
1036 | return text; |
1037 | } | |
1038 | ||
fd79ecee | 1039 | void |
e776119f | 1040 | _initialize_xml_support (void) |
fd79ecee | 1041 | { |
e776119f DJ |
1042 | add_setshow_boolean_cmd ("xml", class_maintenance, &debug_xml, |
1043 | _("Set XML parser debugging."), | |
1044 | _("Show XML parser debugging."), | |
1045 | _("When set, debugging messages for XML parsers " | |
1046 | "are displayed."), | |
1047 | NULL, show_debug_xml, | |
1048 | &setdebuglist, &showdebuglist); | |
fd79ecee | 1049 | } |