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