1 /* Helper routines for parsing XML using Expat.
3 Copyright (C) 2006-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "xml-support.h"
23 #include "filestuff.h"
24 #include "safe-ctype.h"
31 /* The contents of this file are only useful if XML support is
35 #include "gdb_expat.h"
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
41 /* Simplified XML parser infrastructure. */
43 /* A parsing level -- used to keep track of the current element
47 explicit scope_level (const gdb_xml_element *elements_ = NULL)
48 : elements (elements_),
53 /* Elements we allow at this level. */
54 const struct gdb_xml_element *elements;
56 /* The element which we are within. */
57 const struct gdb_xml_element *element;
59 /* Mask of which elements we've seen at this level (used for
60 optional and repeatable checking). */
63 /* Body text accumulation. */
67 /* The parser itself, and our additional state. */
70 gdb_xml_parser (const char *name,
71 const gdb_xml_element *elements,
75 /* Associate DTD_NAME, which must be the name of a compiled-in DTD,
77 void use_dtd (const char *dtd_name);
79 /* Return the name of the expected / default DTD, if specified. */
80 const char *dtd_name ()
81 { return m_dtd_name; }
83 /* Invoke the parser on BUFFER. BUFFER is the data to parse, which
84 should be NUL-terminated.
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);
91 /* Issue a debugging message. */
92 void vdebug (const char *format, va_list ap)
93 ATTRIBUTE_PRINTF (2, 0);
95 /* Issue an error message, and stop parsing. */
96 void verror (const char *format, va_list ap)
97 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0);
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);
103 /* Return the name of this parser. */
107 /* Return the user's callback data, for handlers. */
109 { return m_user_data; };
111 /* Are we the special <xi:include> parser? */
112 void set_is_xinclude (bool is_xinclude)
113 { m_is_xinclude = is_xinclude; }
115 /* A thrown error, if any. */
116 void set_error (gdb_exception error)
119 #ifdef HAVE_XML_STOPPARSER
120 XML_StopParser (m_expat_parser, XML_FALSE);
124 /* Return the underlying expat parser. */
125 XML_Parser expat_parser ()
126 { return m_expat_parser; }
129 /* The underlying expat parser. */
130 XML_Parser m_expat_parser;
132 /* Name of this parser. */
135 /* The user's callback data, for handlers. */
139 std::vector<scope_level> m_scopes;
141 /* A thrown error, if any. */
142 struct gdb_exception m_error;
144 /* The line of the thrown error, or 0. */
147 /* The name of the expected / default DTD, if specified. */
148 const char *m_dtd_name;
150 /* Are we the special <xi:include> parser? */
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. */
159 gdb_xml_parser::body_text (const XML_Char *text, int length)
161 if (m_error.reason < 0)
164 scope_level &scope = m_scopes.back ();
165 scope.body.append (text, length);
169 gdb_xml_body_text (void *data, const XML_Char *text, int length)
171 struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data;
173 parser->body_text (text, length);
176 /* Issue a debugging message from one of PARSER's handlers. */
179 gdb_xml_parser::vdebug (const char *format, va_list ap)
181 int line = XML_GetCurrentLineNumber (m_expat_parser);
184 message = xstrvprintf (format, ap);
186 fprintf_unfiltered (gdb_stderr, "%s (line %d): %s\n",
187 m_name, line, message);
189 fprintf_unfiltered (gdb_stderr, "%s: %s\n",
195 gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...)
201 va_start (ap, format);
202 parser->vdebug (format, ap);
206 /* Issue an error message from one of PARSER's handlers, and stop
210 gdb_xml_parser::verror (const char *format, va_list ap)
212 int line = XML_GetCurrentLineNumber (m_expat_parser);
215 throw_verror (XML_PARSE_ERROR, format, ap);
219 gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...)
222 va_start (ap, format);
223 parser->verror (format, ap);
227 /* Find the attribute named NAME in the set of parsed attributes
228 ATTRIBUTES. Returns NULL if not found. */
230 struct gdb_xml_value *
231 xml_find_attribute (std::vector<gdb_xml_value> &attributes,
234 for (gdb_xml_value &value : attributes)
235 if (strcmp (value.name, name) == 0)
241 /* Handle the start of an element. NAME is the element, and ATTRS are
242 the names and values of this element's attributes. */
245 gdb_xml_parser::start_element (const XML_Char *name,
246 const XML_Char **attrs)
248 if (m_error.reason < 0)
251 const struct gdb_xml_element *element;
252 const struct gdb_xml_attribute *attribute;
255 /* Push an error scope. If we return or throw an exception before
256 filling this in, it will tell us to ignore children of this
257 element. Note we don't take a reference to the element yet
258 because further below we'll process the element which may recurse
259 back here and push more elements to the vector. When the
260 recursion unrolls all such elements will have been popped back
261 already, but if one of those pushes reallocates the vector,
262 previous element references will be invalidated. */
263 m_scopes.emplace_back ();
265 /* Get a reference to the current scope. */
266 scope_level &scope = m_scopes[m_scopes.size () - 2];
268 gdb_xml_debug (this, _("Entering element <%s>"), name);
270 /* Find this element in the list of the current scope's allowed
271 children. Record that we've seen it. */
274 for (element = scope.elements; element && element->name;
275 element++, seen <<= 1)
276 if (strcmp (element->name, name) == 0)
279 if (element == NULL || element->name == NULL)
281 /* If we're working on XInclude, <xi:include> can be the child
282 of absolutely anything. Copy the previous scope's element
283 list into the new scope even if there was no match. */
286 XML_DefaultCurrent (m_expat_parser);
288 scope_level &unknown_scope = m_scopes.back ();
289 unknown_scope.elements = scope.elements;
293 gdb_xml_debug (this, _("Element <%s> unknown"), name);
297 if (!(element->flags & GDB_XML_EF_REPEATABLE) && (seen & scope.seen))
298 gdb_xml_error (this, _("Element <%s> only expected once"), name);
302 std::vector<gdb_xml_value> attributes;
304 for (attribute = element->attributes;
305 attribute != NULL && attribute->name != NULL;
308 const char *val = NULL;
312 for (p = attrs; *p != NULL; p += 2)
313 if (!strcmp (attribute->name, p[0]))
319 if (*p != NULL && val == NULL)
321 gdb_xml_debug (this, _("Attribute \"%s\" missing a value"),
326 if (*p == NULL && !(attribute->flags & GDB_XML_AF_OPTIONAL))
328 gdb_xml_error (this, _("Required attribute \"%s\" of "
329 "<%s> not specified"),
330 attribute->name, element->name);
337 gdb_xml_debug (this, _("Parsing attribute %s=\"%s\""),
338 attribute->name, val);
340 if (attribute->handler)
341 parsed_value = attribute->handler (this, attribute, val);
343 parsed_value = xstrdup (val);
345 attributes.emplace_back (attribute->name, parsed_value);
348 /* Check for unrecognized attributes. */
353 for (p = attrs; *p != NULL; p += 2)
355 for (attribute = element->attributes;
356 attribute != NULL && attribute->name != NULL;
358 if (strcmp (attribute->name, *p) == 0)
361 if (attribute == NULL || attribute->name == NULL)
362 gdb_xml_debug (this, _("Ignoring unknown attribute %s"), *p);
366 /* Call the element handler if there is one. */
367 if (element->start_handler)
368 element->start_handler (this, element, m_user_data, attributes);
370 /* Fill in a new scope level. Note that we must delay getting a
371 back reference till here because above we might have recursed,
372 which may have reallocated the vector which invalidates
373 iterators/pointers/references. */
374 scope_level &new_scope = m_scopes.back ();
375 new_scope.element = element;
376 new_scope.elements = element->children;
379 /* Wrapper for gdb_xml_start_element, to prevent throwing exceptions
383 gdb_xml_start_element_wrapper (void *data, const XML_Char *name,
384 const XML_Char **attrs)
386 struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data;
390 parser->start_element (name, attrs);
392 CATCH (ex, RETURN_MASK_ALL)
394 parser->set_error (ex);
399 /* Handle the end of an element. NAME is the current element. */
402 gdb_xml_parser::end_element (const XML_Char *name)
404 if (m_error.reason < 0)
407 struct scope_level *scope = &m_scopes.back ();
408 const struct gdb_xml_element *element;
411 gdb_xml_debug (this, _("Leaving element <%s>"), name);
413 for (element = scope->elements, seen = 1;
414 element != NULL && element->name != NULL;
415 element++, seen <<= 1)
416 if ((scope->seen & seen) == 0
417 && (element->flags & GDB_XML_EF_OPTIONAL) == 0)
418 gdb_xml_error (this, _("Required element <%s> is missing"),
421 /* Call the element processor. */
422 if (scope->element != NULL && scope->element->end_handler)
426 if (scope->body.empty ())
432 length = scope->body.size ();
433 body = scope->body.c_str ();
435 /* Strip leading and trailing whitespace. */
436 while (length > 0 && ISSPACE (body[length - 1]))
438 scope->body.erase (length);
439 while (*body && ISSPACE (*body))
443 scope->element->end_handler (this, scope->element,
446 else if (scope->element == NULL)
447 XML_DefaultCurrent (m_expat_parser);
449 /* Pop the scope level. */
450 m_scopes.pop_back ();
453 /* Wrapper for gdb_xml_end_element, to prevent throwing exceptions
457 gdb_xml_end_element_wrapper (void *data, const XML_Char *name)
459 struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data;
463 parser->end_element (name);
465 CATCH (ex, RETURN_MASK_ALL)
467 parser->set_error (ex);
472 /* Free a parser and all its associated state. */
474 gdb_xml_parser::~gdb_xml_parser ()
476 XML_ParserFree (m_expat_parser);
479 /* Initialize a parser. */
481 gdb_xml_parser::gdb_xml_parser (const char *name,
482 const gdb_xml_element *elements,
485 m_user_data (user_data),
486 m_error (exception_none),
489 m_is_xinclude (false)
491 m_expat_parser = XML_ParserCreateNS (NULL, '!');
492 if (m_expat_parser == NULL)
495 XML_SetUserData (m_expat_parser, this);
497 /* Set the callbacks. */
498 XML_SetElementHandler (m_expat_parser, gdb_xml_start_element_wrapper,
499 gdb_xml_end_element_wrapper);
500 XML_SetCharacterDataHandler (m_expat_parser, gdb_xml_body_text);
502 /* Initialize the outer scope. */
503 m_scopes.emplace_back (elements);
506 /* External entity handler. The only external entities we support
507 are those compiled into GDB (we do not fetch entities from the
511 gdb_xml_fetch_external_entity (XML_Parser expat_parser,
512 const XML_Char *context,
513 const XML_Char *base,
514 const XML_Char *systemId,
515 const XML_Char *publicId)
517 XML_Parser entity_parser;
519 enum XML_Status status;
521 if (systemId == NULL)
523 gdb_xml_parser *parser
524 = (gdb_xml_parser *) XML_GetUserData (expat_parser);
526 text = fetch_xml_builtin (parser->dtd_name ());
528 internal_error (__FILE__, __LINE__,
529 _("could not locate built-in DTD %s"),
530 parser->dtd_name ());
534 text = fetch_xml_builtin (systemId);
536 return XML_STATUS_ERROR;
539 entity_parser = XML_ExternalEntityParserCreate (expat_parser,
542 /* Don't use our handlers for the contents of the DTD. Just let expat
544 XML_SetElementHandler (entity_parser, NULL, NULL);
545 XML_SetDoctypeDeclHandler (entity_parser, NULL, NULL);
546 XML_SetXmlDeclHandler (entity_parser, NULL);
547 XML_SetDefaultHandler (entity_parser, NULL);
548 XML_SetUserData (entity_parser, NULL);
550 status = XML_Parse (entity_parser, text, strlen (text), 1);
552 XML_ParserFree (entity_parser);
557 gdb_xml_parser::use_dtd (const char *dtd_name)
561 m_dtd_name = dtd_name;
563 XML_SetParamEntityParsing (m_expat_parser,
564 XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
565 XML_SetExternalEntityRefHandler (m_expat_parser,
566 gdb_xml_fetch_external_entity);
568 /* Even if no DTD is provided, use the built-in DTD anyway. */
569 err = XML_UseForeignDTD (m_expat_parser, XML_TRUE);
570 if (err != XML_ERROR_NONE)
571 internal_error (__FILE__, __LINE__,
572 _("XML_UseForeignDTD failed: %s"),
573 XML_ErrorString (err));
576 /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which
577 should be NUL-terminated.
579 The return value is 0 for success or -1 for error. It may throw,
580 but only if something unexpected goes wrong during parsing; parse
581 errors will be caught, warned about, and reported as failure. */
584 gdb_xml_parser::parse (const char *buffer)
586 enum XML_Status status;
587 const char *error_string;
589 gdb_xml_debug (this, _("Starting:\n%s"), buffer);
591 status = XML_Parse (m_expat_parser, buffer, strlen (buffer), 1);
593 if (status == XML_STATUS_OK && m_error.reason == 0)
596 if (m_error.reason == RETURN_ERROR
597 && m_error.error == XML_PARSE_ERROR)
599 gdb_assert (m_error.message != NULL);
600 error_string = m_error.message;
602 else if (status == XML_STATUS_ERROR)
604 enum XML_Error err = XML_GetErrorCode (m_expat_parser);
606 error_string = XML_ErrorString (err);
610 gdb_assert (m_error.reason < 0);
611 throw_exception (m_error);
614 if (m_last_line != 0)
615 warning (_("while parsing %s (at line %d): %s"), m_name,
616 m_last_line, error_string);
618 warning (_("while parsing %s: %s"), m_name, error_string);
624 gdb_xml_parse_quick (const char *name, const char *dtd_name,
625 const struct gdb_xml_element *elements,
626 const char *document, void *user_data)
628 gdb_xml_parser parser (name, elements, user_data);
629 if (dtd_name != NULL)
630 parser.use_dtd (dtd_name);
631 return parser.parse (document);
634 /* Parse a field VALSTR that we expect to contain an integer value.
635 The integer is returned in *VALP. The string is parsed with an
636 equivalent to strtoul.
638 Returns 0 for success, -1 for error. */
641 xml_parse_unsigned_integer (const char *valstr, ULONGEST *valp)
649 result = strtoulst (valstr, &endptr, 0);
657 /* Parse an integer string into a ULONGEST and return it, or call
658 gdb_xml_error if it could not be parsed. */
661 gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, const char *value)
665 if (xml_parse_unsigned_integer (value, &result) != 0)
666 gdb_xml_error (parser, _("Can't convert \"%s\" to an integer"), value);
671 /* Parse an integer attribute into a ULONGEST. */
674 gdb_xml_parse_attr_ulongest (struct gdb_xml_parser *parser,
675 const struct gdb_xml_attribute *attribute,
681 if (xml_parse_unsigned_integer (value, &result) != 0)
682 gdb_xml_error (parser, _("Can't convert %s=\"%s\" to an integer"),
683 attribute->name, value);
685 ret = XNEW (ULONGEST);
686 memcpy (ret, &result, sizeof (result));
690 /* A handler_data for yes/no boolean values. */
692 const struct gdb_xml_enum gdb_xml_enums_boolean[] = {
698 /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the
699 value of handler_data when using gdb_xml_parse_attr_enum to parse a
700 fixed list of possible strings. The list is terminated by an entry
701 with NAME == NULL. */
704 gdb_xml_parse_attr_enum (struct gdb_xml_parser *parser,
705 const struct gdb_xml_attribute *attribute,
708 const struct gdb_xml_enum *enums
709 = (const struct gdb_xml_enum *) attribute->handler_data;
712 for (enums = (const struct gdb_xml_enum *) attribute->handler_data;
713 enums->name != NULL; enums++)
714 if (strcasecmp (enums->name, value) == 0)
717 if (enums->name == NULL)
718 gdb_xml_error (parser, _("Unknown attribute value %s=\"%s\""),
719 attribute->name, value);
721 ret = xmalloc (sizeof (enums->value));
722 memcpy (ret, &enums->value, sizeof (enums->value));
727 /* XInclude processing. This is done as a separate step from actually
728 parsing the document, so that we can produce a single combined XML
729 document - e.g. to hand to a front end or to simplify comparing two
730 documents. We make extensive use of XML_DefaultCurrent, to pass
731 input text directly into the output without reformatting or
734 We output the DOCTYPE declaration for the first document unchanged,
735 if present, and discard DOCTYPEs from included documents. Only the
736 one we pass through here is used when we feed the result back to
737 expat. The XInclude standard explicitly does not discuss
738 validation of the result; we choose to apply the same DTD applied
739 to the outermost document.
741 We can not simply include the external DTD subset in the document
742 as an internal subset, because <!IGNORE> and <!INCLUDE> are valid
743 only in external subsets. But if we do not pass the DTD into the
744 output at all, default values will not be filled in.
746 We don't pass through any <?xml> declaration because we generate
747 UTF-8, not whatever the input encoding was. */
749 struct xinclude_parsing_data
751 xinclude_parsing_data (std::string &output_,
752 xml_fetch_another fetcher_, void *fetcher_baton_,
756 include_depth (include_depth_),
758 fetcher_baton (fetcher_baton_)
761 /* Where the output goes. */
764 /* A count indicating whether we are in an element whose
765 children should not be copied to the output, and if so,
766 how deep we are nested. This is used for anything inside
767 an xi:include, and for the DTD. */
770 /* The number of <xi:include> elements currently being processed,
774 /* A function to call to obtain additional features, and its
776 xml_fetch_another fetcher;
781 xinclude_start_include (struct gdb_xml_parser *parser,
782 const struct gdb_xml_element *element,
784 std::vector<gdb_xml_value> &attributes)
786 struct xinclude_parsing_data *data
787 = (struct xinclude_parsing_data *) user_data;
788 char *href = (char *) xml_find_attribute (attributes, "href")->value.get ();
790 gdb_xml_debug (parser, _("Processing XInclude of \"%s\""), href);
792 if (data->include_depth > MAX_XINCLUDE_DEPTH)
793 gdb_xml_error (parser, _("Maximum XInclude depth (%d) exceeded"),
796 gdb::optional<gdb::char_vector> text
797 = data->fetcher (href, data->fetcher_baton);
799 gdb_xml_error (parser, _("Could not load XML document \"%s\""), href);
801 if (!xml_process_xincludes (data->output, parser->name (),
802 text->data (), data->fetcher,
804 data->include_depth + 1))
805 gdb_xml_error (parser, _("Parsing \"%s\" failed"), href);
811 xinclude_end_include (struct gdb_xml_parser *parser,
812 const struct gdb_xml_element *element,
813 void *user_data, const char *body_text)
815 struct xinclude_parsing_data *data
816 = (struct xinclude_parsing_data *) user_data;
822 xml_xinclude_default (void *data_, const XML_Char *s, int len)
824 struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_;
825 xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data ();
827 /* If we are inside of e.g. xi:include or the DTD, don't save this
829 if (data->skip_depth)
832 /* Otherwise just add it to the end of the document we're building
834 data->output.append (s, len);
838 xml_xinclude_start_doctype (void *data_, const XML_Char *doctypeName,
839 const XML_Char *sysid, const XML_Char *pubid,
840 int has_internal_subset)
842 struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_;
843 xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data ();
845 /* Don't print out the doctype, or the contents of the DTD internal
851 xml_xinclude_end_doctype (void *data_)
853 struct gdb_xml_parser *parser = (struct gdb_xml_parser *) data_;
854 xinclude_parsing_data *data = (xinclude_parsing_data *) parser->user_data ();
860 xml_xinclude_xml_decl (void *data_, const XML_Char *version,
861 const XML_Char *encoding, int standalone)
863 /* Do nothing - this function prevents the default handler from
864 being called, thus suppressing the XML declaration from the
868 const struct gdb_xml_attribute xinclude_attributes[] = {
869 { "href", GDB_XML_AF_NONE, NULL, NULL },
870 { NULL, GDB_XML_AF_NONE, NULL, NULL }
873 const struct gdb_xml_element xinclude_elements[] = {
874 { "http://www.w3.org/2001/XInclude!include", xinclude_attributes, NULL,
875 GDB_XML_EF_OPTIONAL | GDB_XML_EF_REPEATABLE,
876 xinclude_start_include, xinclude_end_include },
877 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
880 /* The main entry point for <xi:include> processing. */
883 xml_process_xincludes (std::string &result,
884 const char *name, const char *text,
885 xml_fetch_another fetcher, void *fetcher_baton,
888 xinclude_parsing_data data (result, fetcher, fetcher_baton, depth);
890 gdb_xml_parser parser (name, xinclude_elements, &data);
891 parser.set_is_xinclude (true);
893 XML_SetCharacterDataHandler (parser.expat_parser (), NULL);
894 XML_SetDefaultHandler (parser.expat_parser (), xml_xinclude_default);
896 /* Always discard the XML version declarations; the only important
897 thing this provides is encoding, and our result will have been
898 converted to UTF-8. */
899 XML_SetXmlDeclHandler (parser.expat_parser (), xml_xinclude_xml_decl);
902 /* Discard the doctype for included documents. */
903 XML_SetDoctypeDeclHandler (parser.expat_parser (),
904 xml_xinclude_start_doctype,
905 xml_xinclude_end_doctype);
907 parser.use_dtd ("xinclude.dtd");
909 if (parser.parse (text) == 0)
912 gdb_xml_debug (&parser, _("XInclude processing succeeded."));
918 #endif /* HAVE_LIBEXPAT */
921 /* Return an XML document which was compiled into GDB, from
922 the given FILENAME, or NULL if the file was not compiled in. */
925 fetch_xml_builtin (const char *filename)
929 for (p = xml_builtin; (*p)[0]; p++)
930 if (strcmp ((*p)[0], filename) == 0)
936 /* A to_xfer_partial helper function which reads XML files which were
937 compiled into GDB. The target may call this function from its own
938 to_xfer_partial handler, after converting object and annex to the
939 appropriate filename. */
942 xml_builtin_xfer_partial (const char *filename,
943 gdb_byte *readbuf, const gdb_byte *writebuf,
944 ULONGEST offset, LONGEST len)
949 gdb_assert (readbuf != NULL && writebuf == NULL);
950 gdb_assert (filename != NULL);
952 buf = fetch_xml_builtin (filename);
956 len_avail = strlen (buf);
957 if (offset >= len_avail)
960 if (len > len_avail - offset)
961 len = len_avail - offset;
962 memcpy (readbuf, buf + offset, len);
968 show_debug_xml (struct ui_file *file, int from_tty,
969 struct cmd_list_element *c, const char *value)
971 fprintf_filtered (file, _("XML debugging is %s.\n"), value);
974 gdb::optional<gdb::char_vector>
975 xml_fetch_content_from_file (const char *filename, void *baton)
977 const char *dirname = (const char *) baton;
980 if (dirname && *dirname)
982 char *fullname = concat (dirname, "/", filename, (char *) NULL);
984 if (fullname == NULL)
986 file = gdb_fopen_cloexec (fullname, FOPEN_RT);
990 file = gdb_fopen_cloexec (filename, FOPEN_RT);
995 /* Read in the whole file. */
999 if (fseek (file.get (), 0, SEEK_END) == -1)
1000 perror_with_name (_("seek to end of file"));
1001 len = ftell (file.get ());
1002 rewind (file.get ());
1004 gdb::char_vector text (len + 1);
1006 if (fread (text.data (), 1, len, file.get ()) != len
1007 || ferror (file.get ()))
1009 warning (_("Read error from \"%s\""), filename);
1013 text.back () = '\0';
1018 _initialize_xml_support (void)
1020 add_setshow_boolean_cmd ("xml", class_maintenance, &debug_xml,
1021 _("Set XML parser debugging."),
1022 _("Show XML parser debugging."),
1023 _("When set, debugging messages for XML parsers "
1025 NULL, show_debug_xml,
1026 &setdebuglist, &showdebuglist);