+/* Simplified XML parser infrastructure. */
+
+/* A parsing level -- used to keep track of the current element
+ nesting. */
+struct scope_level
+{
+ /* Elements we allow at this level. */
+ const struct gdb_xml_element *elements;
+
+ /* The element which we are within. */
+ const struct gdb_xml_element *element;
+
+ /* Mask of which elements we've seen at this level (used for
+ optional and repeatable checking). */
+ unsigned int seen;
+
+ /* Body text accumulation. */
+ struct obstack *body;
+};
+typedef struct scope_level scope_level_s;
+DEF_VEC_O(scope_level_s);
+
+/* The parser itself, and our additional state. */
+struct gdb_xml_parser
+{
+ XML_Parser expat_parser; /* The underlying expat parser. */
+
+ const char *name; /* Name of this parser. */
+ void *user_data; /* The user's callback data, for handlers. */
+
+ VEC(scope_level_s) *scopes; /* Scoping stack. */
+
+ struct gdb_exception error; /* A thrown error, if any. */
+ int last_line; /* The line of the thrown error, or 0. */
+
+ const char *dtd_name; /* The name of the expected / default DTD,
+ if specified. */
+ int is_xinclude; /* Are we the special <xi:include> parser? */
+};
+
+/* Process some body text. We accumulate the text for later use; it's
+ wrong to do anything with it immediately, because a single block of
+ text might be broken up into multiple calls to this function. */
+
+static void
+gdb_xml_body_text (void *data, const XML_Char *text, int length)
+{
+ struct gdb_xml_parser *parser = data;
+ struct scope_level *scope = VEC_last (scope_level_s, parser->scopes);
+
+ if (parser->error.reason < 0)
+ return;
+
+ if (scope->body == NULL)
+ {
+ scope->body = XZALLOC (struct obstack);
+ obstack_init (scope->body);
+ }
+
+ obstack_grow (scope->body, text, length);
+}
+
+/* Issue a debugging message from one of PARSER's handlers. */
+
+void
+gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...)
+{
+ int line = XML_GetCurrentLineNumber (parser->expat_parser);
+ va_list ap;
+ char *message;
+
+ if (!debug_xml)
+ return;
+
+ va_start (ap, format);
+ message = xstrvprintf (format, ap);
+ if (line)
+ fprintf_unfiltered (gdb_stderr, "%s (line %d): %s\n",
+ parser->name, line, message);
+ else
+ fprintf_unfiltered (gdb_stderr, "%s: %s\n",
+ parser->name, message);
+ xfree (message);
+}
+
+/* Issue an error message from one of PARSER's handlers, and stop
+ parsing. */
+
+void
+gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...)
+{
+ int line = XML_GetCurrentLineNumber (parser->expat_parser);
+ va_list ap;
+
+ parser->last_line = line;
+ va_start (ap, format);
+ throw_verror (XML_PARSE_ERROR, format, ap);
+}
+
+/* Find the attribute named NAME in the set of parsed attributes
+ ATTRIBUTES. Returns NULL if not found. */
+
+struct gdb_xml_value *
+xml_find_attribute (VEC(gdb_xml_value_s) *attributes, const char *name)
+{
+ struct gdb_xml_value *value;
+ int ix;
+
+ for (ix = 0; VEC_iterate (gdb_xml_value_s, attributes, ix, value); ix++)
+ if (strcmp (value->name, name) == 0)
+ return value;
+
+ return NULL;
+}
+
+/* Clean up a vector of parsed attribute values. */
+
+static void
+gdb_xml_values_cleanup (void *data)
+{
+ VEC(gdb_xml_value_s) **values = data;
+ struct gdb_xml_value *value;
+ int ix;
+
+ for (ix = 0; VEC_iterate (gdb_xml_value_s, *values, ix, value); ix++)
+ xfree (value->value);
+ VEC_free (gdb_xml_value_s, *values);
+}
+
+/* Handle the start of an element. DATA is our local XML parser, NAME
+ is the element, and ATTRS are the names and values of this
+ element's attributes. */
+
+static void
+gdb_xml_start_element (void *data, const XML_Char *name,
+ const XML_Char **attrs)
+{
+ struct gdb_xml_parser *parser = data;
+ struct scope_level *scope;
+ struct scope_level new_scope;
+ const struct gdb_xml_element *element;
+ const struct gdb_xml_attribute *attribute;
+ VEC(gdb_xml_value_s) *attributes = NULL;
+ unsigned int seen;
+ struct cleanup *back_to;
+
+ /* Push an error scope. If we return or throw an exception before
+ filling this in, it will tell us to ignore children of this
+ element. */
+ VEC_reserve (scope_level_s, parser->scopes, 1);
+ scope = VEC_last (scope_level_s, parser->scopes);
+ memset (&new_scope, 0, sizeof (new_scope));
+ VEC_quick_push (scope_level_s, parser->scopes, &new_scope);
+
+ gdb_xml_debug (parser, _("Entering element <%s>"), name);
+
+ /* Find this element in the list of the current scope's allowed
+ children. Record that we've seen it. */
+
+ seen = 1;
+ for (element = scope->elements; element && element->name;
+ element++, seen <<= 1)
+ if (strcmp (element->name, name) == 0)
+ break;
+
+ if (element == NULL || element->name == NULL)
+ {
+ /* If we're working on XInclude, <xi:include> can be the child
+ of absolutely anything. Copy the previous scope's element
+ list into the new scope even if there was no match. */
+ if (parser->is_xinclude)
+ {
+ struct scope_level *unknown_scope;
+
+ XML_DefaultCurrent (parser->expat_parser);
+
+ unknown_scope = VEC_last (scope_level_s, parser->scopes);
+ unknown_scope->elements = scope->elements;
+ return;
+ }
+
+ gdb_xml_debug (parser, _("Element <%s> unknown"), name);
+ return;
+ }
+
+ if (!(element->flags & GDB_XML_EF_REPEATABLE) && (seen & scope->seen))
+ gdb_xml_error (parser, _("Element <%s> only expected once"), name);
+
+ scope->seen |= seen;
+
+ back_to = make_cleanup (gdb_xml_values_cleanup, &attributes);
+
+ for (attribute = element->attributes;
+ attribute != NULL && attribute->name != NULL;
+ attribute++)
+ {
+ const char *val = NULL;
+ const XML_Char **p;
+ void *parsed_value;
+ struct gdb_xml_value new_value;
+
+ for (p = attrs; *p != NULL; p += 2)
+ if (!strcmp (attribute->name, p[0]))
+ {
+ val = p[1];
+ break;
+ }
+
+ if (*p != NULL && val == NULL)
+ {
+ gdb_xml_debug (parser, _("Attribute \"%s\" missing a value"),
+ attribute->name);
+ continue;
+ }
+
+ if (*p == NULL && !(attribute->flags & GDB_XML_AF_OPTIONAL))
+ {
+ gdb_xml_error (parser, _("Required attribute \"%s\" of "
+ "<%s> not specified"),
+ attribute->name, element->name);
+ continue;
+ }
+
+ if (*p == NULL)
+ continue;
+
+ gdb_xml_debug (parser, _("Parsing attribute %s=\"%s\""),
+ attribute->name, val);
+
+ if (attribute->handler)
+ parsed_value = attribute->handler (parser, attribute, val);
+ else
+ parsed_value = xstrdup (val);
+
+ new_value.name = attribute->name;
+ new_value.value = parsed_value;
+ VEC_safe_push (gdb_xml_value_s, attributes, &new_value);
+ }
+
+ /* Check for unrecognized attributes. */
+ if (debug_xml)
+ {
+ const XML_Char **p;
+
+ for (p = attrs; *p != NULL; p += 2)
+ {
+ for (attribute = element->attributes;
+ attribute != NULL && attribute->name != NULL;
+ attribute++)
+ if (strcmp (attribute->name, *p) == 0)
+ break;
+
+ if (attribute == NULL || attribute->name == NULL)
+ gdb_xml_debug (parser, _("Ignoring unknown attribute %s"), *p);
+ }
+ }
+
+ /* Call the element handler if there is one. */
+ if (element->start_handler)
+ element->start_handler (parser, element, parser->user_data, attributes);
+
+ /* Fill in a new scope level. */
+ scope = VEC_last (scope_level_s, parser->scopes);
+ scope->element = element;
+ scope->elements = element->children;
+
+ do_cleanups (back_to);
+}
+
+/* Wrapper for gdb_xml_start_element, to prevent throwing exceptions
+ through expat. */
+
+static void
+gdb_xml_start_element_wrapper (void *data, const XML_Char *name,
+ const XML_Char **attrs)
+{
+ struct gdb_xml_parser *parser = data;
+ volatile struct gdb_exception ex;
+
+ if (parser->error.reason < 0)
+ return;
+
+ TRY_CATCH (ex, RETURN_MASK_ALL)
+ {
+ gdb_xml_start_element (data, name, attrs);
+ }
+ if (ex.reason < 0)
+ {
+ parser->error = ex;
+#ifdef HAVE_XML_STOPPARSER
+ XML_StopParser (parser->expat_parser, XML_FALSE);
+#endif
+ }
+}
+
+/* Handle the end of an element. DATA is our local XML parser, and
+ NAME is the current element. */
+
+static void
+gdb_xml_end_element (void *data, const XML_Char *name)
+{
+ struct gdb_xml_parser *parser = data;
+ struct scope_level *scope = VEC_last (scope_level_s, parser->scopes);
+ const struct gdb_xml_element *element;
+ unsigned int seen;
+
+ gdb_xml_debug (parser, _("Leaving element <%s>"), name);
+
+ for (element = scope->elements, seen = 1;
+ element != NULL && element->name != NULL;
+ element++, seen <<= 1)
+ if ((scope->seen & seen) == 0
+ && (element->flags & GDB_XML_EF_OPTIONAL) == 0)
+ gdb_xml_error (parser, _("Required element <%s> is missing"),
+ element->name);
+
+ /* Call the element processor. */
+ if (scope->element != NULL && scope->element->end_handler)
+ {
+ char *body;
+
+ if (scope->body == NULL)
+ body = "";
+ else
+ {
+ int length;