1 /* Helper routines for parsing XML using Expat.
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 /* The contents of this file are only useful if XML support is
33 #include "exceptions.h"
34 #include "xml-support.h"
36 #include "gdb_expat.h"
37 #include "gdb_string.h"
38 #include "safe-ctype.h"
40 /* A parsing level -- used to keep track of the current element
44 /* Elements we allow at this level. */
45 const struct gdb_xml_element
*elements
;
47 /* The element which we are within. */
48 const struct gdb_xml_element
*element
;
50 /* Mask of which elements we've seen at this level (used for
51 optional and repeatable checking). */
54 /* Body text accumulation. */
57 typedef struct scope_level scope_level_s
;
58 DEF_VEC_O(scope_level_s
);
60 /* The parser itself, and our additional state. */
63 XML_Parser expat_parser
; /* The underlying expat parser. */
65 const char *name
; /* Name of this parser. */
66 void *user_data
; /* The user's callback data, for handlers. */
68 VEC(scope_level_s
) *scopes
; /* Scoping stack. */
70 struct gdb_exception error
; /* A thrown error, if any. */
71 int last_line
; /* The line of the thrown error, or 0. */
74 /* Process some body text. We accumulate the text for later use; it's
75 wrong to do anything with it immediately, because a single block of
76 text might be broken up into multiple calls to this function. */
79 gdb_xml_body_text (void *data
, const XML_Char
*text
, int length
)
81 struct gdb_xml_parser
*parser
= data
;
82 struct scope_level
*scope
= VEC_last (scope_level_s
, parser
->scopes
);
84 if (parser
->error
.reason
< 0)
87 if (scope
->body
== NULL
)
89 scope
->body
= XZALLOC (struct obstack
);
90 obstack_init (scope
->body
);
93 obstack_grow (scope
->body
, text
, length
);
96 /* Issue a debugging message from one of PARSER's handlers. */
99 gdb_xml_debug (struct gdb_xml_parser
*parser
, const char *format
, ...)
101 int line
= XML_GetCurrentLineNumber (parser
->expat_parser
);
108 va_start (ap
, format
);
109 message
= xstrvprintf (format
, ap
);
111 fprintf_unfiltered (gdb_stderr
, "%s (line %d): %s\n",
112 parser
->name
, line
, message
);
114 fprintf_unfiltered (gdb_stderr
, "%s: %s\n",
115 parser
->name
, message
);
119 /* Issue an error message from one of PARSER's handlers, and stop
123 gdb_xml_error (struct gdb_xml_parser
*parser
, const char *format
, ...)
125 int line
= XML_GetCurrentLineNumber (parser
->expat_parser
);
128 parser
->last_line
= line
;
129 va_start (ap
, format
);
130 throw_verror (XML_PARSE_ERROR
, format
, ap
);
133 /* Clean up a vector of parsed attribute values. */
136 gdb_xml_values_cleanup (void *data
)
138 VEC(gdb_xml_value_s
) **values
= data
;
139 struct gdb_xml_value
*value
;
142 for (ix
= 0; VEC_iterate (gdb_xml_value_s
, *values
, ix
, value
); ix
++)
143 xfree (value
->value
);
144 VEC_free (gdb_xml_value_s
, *values
);
147 /* Handle the start of an element. DATA is our local XML parser, NAME
148 is the element, and ATTRS are the names and values of this
149 element's attributes. */
152 gdb_xml_start_element (void *data
, const XML_Char
*name
,
153 const XML_Char
**attrs
)
155 struct gdb_xml_parser
*parser
= data
;
156 struct scope_level
*scope
= VEC_last (scope_level_s
, parser
->scopes
);
157 struct scope_level new_scope
;
158 const struct gdb_xml_element
*element
;
159 const struct gdb_xml_attribute
*attribute
;
160 VEC(gdb_xml_value_s
) *attributes
= NULL
;
162 struct cleanup
*back_to
;
164 back_to
= make_cleanup (gdb_xml_values_cleanup
, &attributes
);
166 /* Push an error scope. If we return or throw an exception before
167 filling this in, it will tell us to ignore children of this
169 memset (&new_scope
, 0, sizeof (new_scope
));
170 VEC_safe_push (scope_level_s
, parser
->scopes
, &new_scope
);
172 gdb_xml_debug (parser
, _("Entering element <%s>"), name
);
174 /* Find this element in the list of the current scope's allowed
175 children. Record that we've seen it. */
178 for (element
= scope
->elements
; element
&& element
->name
;
179 element
++, seen
<<= 1)
180 if (strcmp (element
->name
, name
) == 0)
183 if (element
== NULL
|| element
->name
== NULL
)
185 gdb_xml_debug (parser
, _("Element <%s> unknown"), name
);
186 do_cleanups (back_to
);
190 if (!(element
->flags
& GDB_XML_EF_REPEATABLE
) && (seen
& scope
->seen
))
191 gdb_xml_error (parser
, _("Element <%s> only expected once"), name
);
195 for (attribute
= element
->attributes
;
196 attribute
!= NULL
&& attribute
->name
!= NULL
;
199 const char *val
= NULL
;
202 struct gdb_xml_value new_value
;
204 for (p
= attrs
; *p
!= NULL
; p
+= 2)
205 if (!strcmp (attribute
->name
, p
[0]))
211 if (*p
!= NULL
&& val
== NULL
)
213 gdb_xml_debug (parser
, _("Attribute \"%s\" missing a value"),
218 if (*p
== NULL
&& !(attribute
->flags
& GDB_XML_AF_OPTIONAL
))
220 gdb_xml_error (parser
, _("Required attribute \"%s\" of "
221 "<%s> not specified"),
222 attribute
->name
, element
->name
);
229 gdb_xml_debug (parser
, _("Parsing attribute %s=\"%s\""),
230 attribute
->name
, val
);
232 if (attribute
->handler
)
233 parsed_value
= attribute
->handler (parser
, attribute
, val
);
235 parsed_value
= xstrdup (val
);
237 new_value
.name
= attribute
->name
;
238 new_value
.value
= parsed_value
;
239 VEC_safe_push (gdb_xml_value_s
, attributes
, &new_value
);
242 /* Check for unrecognized attributes. */
247 for (p
= attrs
; *p
!= NULL
; p
+= 2)
249 for (attribute
= element
->attributes
;
250 attribute
!= NULL
&& attribute
->name
!= NULL
;
252 if (strcmp (attribute
->name
, *p
) == 0)
255 if (attribute
== NULL
|| attribute
->name
== NULL
)
256 gdb_xml_debug (parser
, _("Ignoring unknown attribute %s"), *p
);
260 /* Call the element handler if there is one. */
261 if (element
->start_handler
)
262 element
->start_handler (parser
, element
, parser
->user_data
, attributes
);
264 /* Fill in a new scope level. */
265 scope
= VEC_last (scope_level_s
, parser
->scopes
);
266 scope
->element
= element
;
267 scope
->elements
= element
->children
;
269 do_cleanups (back_to
);
272 /* Wrapper for gdb_xml_start_element, to prevent throwing exceptions
276 gdb_xml_start_element_wrapper (void *data
, const XML_Char
*name
,
277 const XML_Char
**attrs
)
279 struct gdb_xml_parser
*parser
= data
;
280 volatile struct gdb_exception ex
;
282 if (parser
->error
.reason
< 0)
285 TRY_CATCH (ex
, RETURN_MASK_ALL
)
287 gdb_xml_start_element (data
, name
, attrs
);
292 #ifdef HAVE_XML_STOPPARSER
293 XML_StopParser (parser
->expat_parser
, XML_FALSE
);
298 /* Handle the end of an element. DATA is our local XML parser, and
299 NAME is the current element. */
302 gdb_xml_end_element (void *data
, const XML_Char
*name
)
304 struct gdb_xml_parser
*parser
= data
;
305 struct scope_level
*scope
= VEC_last (scope_level_s
, parser
->scopes
);
306 const struct gdb_xml_element
*element
;
310 gdb_xml_debug (parser
, _("Leaving element <%s>"), name
);
312 for (element
= scope
->elements
, seen
= 1;
313 element
!= NULL
&& element
->name
!= NULL
;
314 element
++, seen
<<= 1)
315 if ((scope
->seen
& seen
) == 0
316 && (element
->flags
& GDB_XML_EF_OPTIONAL
) == 0)
317 gdb_xml_error (parser
, _("Required element <%s> is missing"),
320 /* Call the element processor. */
321 if (scope
->body
== NULL
)
327 length
= obstack_object_size (scope
->body
);
328 obstack_1grow (scope
->body
, '\0');
329 body
= obstack_finish (scope
->body
);
331 /* Strip leading and trailing whitespace. */
332 while (length
> 0 && ISSPACE (body
[length
-1]))
333 body
[--length
] = '\0';
334 while (*body
&& ISSPACE (*body
))
338 if (scope
->element
!= NULL
&& scope
->element
->end_handler
)
339 scope
->element
->end_handler (parser
, scope
->element
, parser
->user_data
,
342 /* Pop the scope level. */
345 obstack_free (scope
->body
, NULL
);
348 VEC_pop (scope_level_s
, parser
->scopes
);
351 /* Wrapper for gdb_xml_end_element, to prevent throwing exceptions
355 gdb_xml_end_element_wrapper (void *data
, const XML_Char
*name
)
357 struct gdb_xml_parser
*parser
= data
;
358 volatile struct gdb_exception ex
;
360 if (parser
->error
.reason
< 0)
363 TRY_CATCH (ex
, RETURN_MASK_ALL
)
365 gdb_xml_end_element (data
, name
);
370 #ifdef HAVE_XML_STOPPARSER
371 XML_StopParser (parser
->expat_parser
, XML_FALSE
);
376 /* Free a parser and all its associated state. */
379 gdb_xml_cleanup (void *arg
)
381 struct gdb_xml_parser
*parser
= arg
;
382 struct scope_level
*scope
;
385 XML_ParserFree (parser
->expat_parser
);
387 /* Clean up the scopes. */
388 for (ix
= 0; VEC_iterate (scope_level_s
, parser
->scopes
, ix
, scope
); ix
++)
391 obstack_free (scope
->body
, NULL
);
394 VEC_free (scope_level_s
, parser
->scopes
);
399 /* Initialize and return a parser. Register a cleanup to destroy the
402 struct gdb_xml_parser
*
403 gdb_xml_create_parser_and_cleanup (const char *name
,
404 const struct gdb_xml_element
*elements
,
407 struct gdb_xml_parser
*parser
;
408 struct scope_level start_scope
;
410 /* Initialize the parser. */
411 parser
= XZALLOC (struct gdb_xml_parser
);
412 parser
->expat_parser
= XML_ParserCreateNS (NULL
, '!');
413 if (parser
->expat_parser
== NULL
)
421 parser
->user_data
= user_data
;
422 XML_SetUserData (parser
->expat_parser
, parser
);
424 /* Set the callbacks. */
425 XML_SetElementHandler (parser
->expat_parser
, gdb_xml_start_element_wrapper
,
426 gdb_xml_end_element_wrapper
);
427 XML_SetCharacterDataHandler (parser
->expat_parser
, gdb_xml_body_text
);
429 /* Initialize the outer scope. */
430 memset (&start_scope
, 0, sizeof (start_scope
));
431 start_scope
.elements
= elements
;
432 VEC_safe_push (scope_level_s
, parser
->scopes
, &start_scope
);
434 make_cleanup (gdb_xml_cleanup
, parser
);
439 /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which
440 should be NUL-terminated.
442 The return value is 0 for success or -1 for error. It may throw,
443 but only if something unexpected goes wrong during parsing; parse
444 errors will be caught, warned about, and reported as failure. */
447 gdb_xml_parse (struct gdb_xml_parser
*parser
, const char *buffer
)
449 enum XML_Status status
;
450 const char *error_string
;
452 status
= XML_Parse (parser
->expat_parser
, buffer
, strlen (buffer
), 1);
454 if (status
== XML_STATUS_OK
&& parser
->error
.reason
== 0)
457 if (parser
->error
.reason
== RETURN_ERROR
458 && parser
->error
.error
== XML_PARSE_ERROR
)
460 gdb_assert (parser
->error
.message
!= NULL
);
461 error_string
= parser
->error
.message
;
463 else if (status
== XML_STATUS_ERROR
)
465 enum XML_Error err
= XML_GetErrorCode (parser
->expat_parser
);
466 error_string
= XML_ErrorString (err
);
470 gdb_assert (parser
->error
.reason
< 0);
471 throw_exception (parser
->error
);
474 if (parser
->last_line
!= 0)
475 warning (_("while parsing %s (at line %d): %s"), parser
->name
,
476 parser
->last_line
, error_string
);
478 warning (_("while parsing %s: %s"), parser
->name
, error_string
);
483 /* Parse a field VALSTR that we expect to contain an integer value.
484 The integer is returned in *VALP. The string is parsed with an
485 equivalent to strtoul.
487 Returns 0 for success, -1 for error. */
490 xml_parse_unsigned_integer (const char *valstr
, ULONGEST
*valp
)
498 result
= strtoulst (valstr
, &endptr
, 0);
506 /* Parse an integer string into a ULONGEST and return it, or call
507 gdb_xml_error if it could not be parsed. */
510 gdb_xml_parse_ulongest (struct gdb_xml_parser
*parser
, const char *value
)
514 if (xml_parse_unsigned_integer (value
, &result
) != 0)
515 gdb_xml_error (parser
, _("Can't convert \"%s\" to an integer"), value
);
520 /* Parse an integer attribute into a ULONGEST. */
523 gdb_xml_parse_attr_ulongest (struct gdb_xml_parser
*parser
,
524 const struct gdb_xml_attribute
*attribute
,
530 if (xml_parse_unsigned_integer (value
, &result
) != 0)
531 gdb_xml_error (parser
, _("Can't convert %s=\"%s\" to an integer"),
532 attribute
->name
, value
);
534 ret
= xmalloc (sizeof (result
));
535 memcpy (ret
, &result
, sizeof (result
));
539 /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the
540 value of handler_data when using gdb_xml_parse_attr_enum to parse a
541 fixed list of possible strings. The list is terminated by an entry
542 with NAME == NULL. */
545 gdb_xml_parse_attr_enum (struct gdb_xml_parser
*parser
,
546 const struct gdb_xml_attribute
*attribute
,
549 const struct gdb_xml_enum
*enums
= attribute
->handler_data
;
552 for (enums
= attribute
->handler_data
; enums
->name
!= NULL
; enums
++)
553 if (strcmp (enums
->name
, value
) == 0)
556 if (enums
->name
== NULL
)
557 gdb_xml_error (parser
, _("Unknown attribute value %s=\"%s\""),
558 attribute
->name
, value
);
560 ret
= xmalloc (sizeof (enums
->value
));
561 memcpy (ret
, &enums
->value
, sizeof (enums
->value
));
565 #endif /* HAVE_LIBEXPAT */
568 show_debug_xml (struct ui_file
*file
, int from_tty
,
569 struct cmd_list_element
*c
, const char *value
)
571 fprintf_filtered (file
, _("XML debugging is %s.\n"), value
);
574 void _initialize_xml_support (void);
577 _initialize_xml_support (void)
579 add_setshow_boolean_cmd ("xml", class_maintenance
, &debug_xml
,
580 _("Set XML parser debugging."),
581 _("Show XML parser debugging."),
582 _("When set, debugging messages for XML parsers "
584 NULL
, show_debug_xml
,
585 &setdebuglist
, &showdebuglist
);