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 (scope
->body
== NULL
)
86 scope
->body
= XZALLOC (struct obstack
);
87 obstack_init (scope
->body
);
90 obstack_grow (scope
->body
, text
, length
);
93 /* Issue a debugging message from one of PARSER's handlers. */
96 gdb_xml_debug (struct gdb_xml_parser
*parser
, const char *format
, ...)
98 int line
= XML_GetCurrentLineNumber (parser
->expat_parser
);
105 va_start (ap
, format
);
106 message
= xstrvprintf (format
, ap
);
108 fprintf_unfiltered (gdb_stderr
, "%s (line %d): %s\n",
109 parser
->name
, line
, message
);
111 fprintf_unfiltered (gdb_stderr
, "%s: %s\n",
112 parser
->name
, message
);
116 /* Issue an error message from one of PARSER's handlers, and stop
120 gdb_xml_error (struct gdb_xml_parser
*parser
, const char *format
, ...)
122 int line
= XML_GetCurrentLineNumber (parser
->expat_parser
);
125 parser
->last_line
= line
;
126 va_start (ap
, format
);
127 throw_verror (XML_PARSE_ERROR
, format
, ap
);
130 /* Clean up a vector of parsed attribute values. */
133 gdb_xml_values_cleanup (void *data
)
135 VEC(gdb_xml_value_s
) **values
= data
;
136 struct gdb_xml_value
*value
;
139 for (ix
= 0; VEC_iterate (gdb_xml_value_s
, *values
, ix
, value
); ix
++)
140 xfree (value
->value
);
141 VEC_free (gdb_xml_value_s
, *values
);
144 /* Handle the start of an element. DATA is our local XML parser, NAME
145 is the element, and ATTRS are the names and values of this
146 element's attributes. */
149 gdb_xml_start_element (void *data
, const XML_Char
*name
,
150 const XML_Char
**attrs
)
152 struct gdb_xml_parser
*parser
= data
;
153 struct scope_level
*scope
= VEC_last (scope_level_s
, parser
->scopes
);
154 struct scope_level new_scope
;
155 const struct gdb_xml_element
*element
;
156 const struct gdb_xml_attribute
*attribute
;
157 VEC(gdb_xml_value_s
) *attributes
= NULL
;
159 struct cleanup
*back_to
;
161 back_to
= make_cleanup (gdb_xml_values_cleanup
, &attributes
);
163 /* Push an error scope. If we return or throw an exception before
164 filling this in, it will tell us to ignore children of this
166 memset (&new_scope
, 0, sizeof (new_scope
));
167 VEC_safe_push (scope_level_s
, parser
->scopes
, &new_scope
);
169 gdb_xml_debug (parser
, _("Entering element <%s>"), name
);
171 /* Find this element in the list of the current scope's allowed
172 children. Record that we've seen it. */
175 for (element
= scope
->elements
; element
&& element
->name
;
176 element
++, seen
<<= 1)
177 if (strcmp (element
->name
, name
) == 0)
180 if (element
== NULL
|| element
->name
== NULL
)
182 gdb_xml_debug (parser
, _("Element <%s> unknown"), name
);
183 do_cleanups (back_to
);
187 if (!(element
->flags
& GDB_XML_EF_REPEATABLE
) && (seen
& scope
->seen
))
188 gdb_xml_error (parser
, _("Element <%s> only expected once"), name
);
192 for (attribute
= element
->attributes
;
193 attribute
!= NULL
&& attribute
->name
!= NULL
;
196 const char *val
= NULL
;
199 struct gdb_xml_value new_value
;
201 for (p
= attrs
; *p
!= NULL
; p
+= 2)
202 if (!strcmp (attribute
->name
, p
[0]))
208 if (*p
!= NULL
&& val
== NULL
)
210 gdb_xml_debug (parser
, _("Attribute \"%s\" missing a value"),
215 if (*p
== NULL
&& !(attribute
->flags
& GDB_XML_AF_OPTIONAL
))
217 gdb_xml_error (parser
, _("Required attribute \"%s\" of "
218 "<%s> not specified"),
219 attribute
->name
, element
->name
);
226 gdb_xml_debug (parser
, _("Parsing attribute %s=\"%s\""),
227 attribute
->name
, val
);
229 if (attribute
->handler
)
230 parsed_value
= attribute
->handler (parser
, attribute
, val
);
232 parsed_value
= xstrdup (val
);
234 new_value
.name
= attribute
->name
;
235 new_value
.value
= parsed_value
;
236 VEC_safe_push (gdb_xml_value_s
, attributes
, &new_value
);
239 /* Check for unrecognized attributes. */
244 for (p
= attrs
; *p
!= NULL
; p
+= 2)
246 for (attribute
= element
->attributes
;
247 attribute
!= NULL
&& attribute
->name
!= NULL
;
249 if (strcmp (attribute
->name
, *p
) == 0)
252 if (attribute
== NULL
|| attribute
->name
== NULL
)
253 gdb_xml_debug (parser
, _("Ignoring unknown attribute %s"), *p
);
257 /* Call the element handler if there is one. */
258 if (element
->start_handler
)
259 element
->start_handler (parser
, element
, parser
->user_data
, attributes
);
261 /* Fill in a new scope level. */
262 scope
= VEC_last (scope_level_s
, parser
->scopes
);
263 scope
->element
= element
;
264 scope
->elements
= element
->children
;
266 do_cleanups (back_to
);
269 /* Wrapper for gdb_xml_start_element, to prevent throwing exceptions
273 gdb_xml_start_element_wrapper (void *data
, const XML_Char
*name
,
274 const XML_Char
**attrs
)
276 struct gdb_xml_parser
*parser
= data
;
277 volatile struct gdb_exception ex
;
279 if (parser
->error
.reason
< 0)
282 TRY_CATCH (ex
, RETURN_MASK_ALL
)
284 gdb_xml_start_element (data
, name
, attrs
);
289 XML_StopParser (parser
->expat_parser
, XML_FALSE
);
293 /* Handle the end of an element. DATA is our local XML parser, and
294 NAME is the current element. */
297 gdb_xml_end_element (void *data
, const XML_Char
*name
)
299 struct gdb_xml_parser
*parser
= data
;
300 struct scope_level
*scope
= VEC_last (scope_level_s
, parser
->scopes
);
301 const struct gdb_xml_element
*element
;
305 gdb_xml_debug (parser
, _("Leaving element <%s>"), name
);
307 for (element
= scope
->elements
, seen
= 1;
308 element
!= NULL
&& element
->name
!= NULL
;
309 element
++, seen
<<= 1)
310 if ((scope
->seen
& seen
) == 0
311 && (element
->flags
& GDB_XML_EF_OPTIONAL
) == 0)
312 gdb_xml_error (parser
, _("Required element \<%s\> is missing"),
315 /* Call the element processor. */
316 if (scope
->body
== NULL
)
322 length
= obstack_object_size (scope
->body
);
323 obstack_1grow (scope
->body
, '\0');
324 body
= obstack_finish (scope
->body
);
326 /* Strip leading and trailing whitespace. */
327 while (length
> 0 && ISSPACE (body
[length
-1]))
328 body
[--length
] = '\0';
329 while (*body
&& ISSPACE (*body
))
333 if (scope
->element
!= NULL
&& scope
->element
->end_handler
)
334 scope
->element
->end_handler (parser
, scope
->element
, parser
->user_data
,
337 /* Pop the scope level. */
340 obstack_free (scope
->body
, NULL
);
343 VEC_pop (scope_level_s
, parser
->scopes
);
346 /* Wrapper for gdb_xml_end_element, to prevent throwing exceptions
350 gdb_xml_end_element_wrapper (void *data
, const XML_Char
*name
)
352 struct gdb_xml_parser
*parser
= data
;
353 volatile struct gdb_exception ex
;
355 if (parser
->error
.reason
< 0)
358 TRY_CATCH (ex
, RETURN_MASK_ALL
)
360 gdb_xml_end_element (data
, name
);
365 XML_StopParser (parser
->expat_parser
, XML_FALSE
);
369 /* Free a parser and all its associated state. */
372 gdb_xml_cleanup (void *arg
)
374 struct gdb_xml_parser
*parser
= arg
;
375 struct scope_level
*scope
;
378 XML_ParserFree (parser
->expat_parser
);
380 /* Clean up the scopes. */
381 for (ix
= 0; VEC_iterate (scope_level_s
, parser
->scopes
, ix
, scope
); ix
++)
384 obstack_free (scope
->body
, NULL
);
387 VEC_free (scope_level_s
, parser
->scopes
);
392 /* Initialize and return a parser. Register a cleanup to destroy the
395 struct gdb_xml_parser
*
396 gdb_xml_create_parser_and_cleanup (const char *name
,
397 const struct gdb_xml_element
*elements
,
400 struct gdb_xml_parser
*parser
;
401 struct scope_level start_scope
;
403 /* Initialize the parser. */
404 parser
= XZALLOC (struct gdb_xml_parser
);
405 parser
->expat_parser
= XML_ParserCreateNS (NULL
, '!');
406 if (parser
->expat_parser
== NULL
)
414 parser
->user_data
= user_data
;
415 XML_SetUserData (parser
->expat_parser
, parser
);
417 /* Set the callbacks. */
418 XML_SetElementHandler (parser
->expat_parser
, gdb_xml_start_element_wrapper
,
419 gdb_xml_end_element_wrapper
);
420 XML_SetCharacterDataHandler (parser
->expat_parser
, gdb_xml_body_text
);
422 /* Initialize the outer scope. */
423 memset (&start_scope
, 0, sizeof (start_scope
));
424 start_scope
.elements
= elements
;
425 VEC_safe_push (scope_level_s
, parser
->scopes
, &start_scope
);
427 make_cleanup (gdb_xml_cleanup
, parser
);
432 /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which
433 should be NUL-terminated.
435 The return value is 0 for success or -1 for error. It may throw,
436 but only if something unexpected goes wrong during parsing; parse
437 errors will be caught, warned about, and reported as failure. */
440 gdb_xml_parse (struct gdb_xml_parser
*parser
, const char *buffer
)
442 enum XML_Status status
;
443 const char *error_string
;
445 status
= XML_Parse (parser
->expat_parser
, buffer
, strlen (buffer
), 1);
447 if (status
== XML_STATUS_OK
&& parser
->error
.reason
== 0)
450 if (parser
->error
.reason
== RETURN_ERROR
451 && parser
->error
.error
== XML_PARSE_ERROR
)
453 gdb_assert (parser
->error
.message
!= NULL
);
454 error_string
= parser
->error
.message
;
456 else if (status
== XML_STATUS_ERROR
)
458 enum XML_Error err
= XML_GetErrorCode (parser
->expat_parser
);
459 error_string
= XML_ErrorString (err
);
463 gdb_assert (parser
->error
.reason
< 0);
464 throw_exception (parser
->error
);
467 if (parser
->last_line
!= 0)
468 warning (_("while parsing %s (at line %d): %s"), parser
->name
,
469 parser
->last_line
, error_string
);
471 warning (_("while parsing %s: %s"), parser
->name
, error_string
);
476 /* Parse a field VALSTR that we expect to contain an integer value.
477 The integer is returned in *VALP. The string is parsed with an
478 equivalent to strtoul.
480 Returns 0 for success, -1 for error. */
483 xml_parse_unsigned_integer (const char *valstr
, ULONGEST
*valp
)
491 result
= strtoulst (valstr
, &endptr
, 0);
499 /* Parse an integer string into a ULONGEST and return it, or call
500 gdb_xml_error if it could not be parsed. */
503 gdb_xml_parse_ulongest (struct gdb_xml_parser
*parser
, const char *value
)
507 if (xml_parse_unsigned_integer (value
, &result
) != 0)
508 gdb_xml_error (parser
, _("Can't convert \"%s\" to an integer"), value
);
513 /* Parse an integer attribute into a ULONGEST. */
516 gdb_xml_parse_attr_ulongest (struct gdb_xml_parser
*parser
,
517 const struct gdb_xml_attribute
*attribute
,
523 if (xml_parse_unsigned_integer (value
, &result
) != 0)
524 gdb_xml_error (parser
, _("Can't convert %s=\"%s\" to an integer"),
525 attribute
->name
, value
);
527 ret
= xmalloc (sizeof (result
));
528 memcpy (ret
, &result
, sizeof (result
));
532 /* Map NAME to VALUE. A struct gdb_xml_enum * should be saved as the
533 value of handler_data when using gdb_xml_parse_attr_enum to parse a
534 fixed list of possible strings. The list is terminated by an entry
535 with NAME == NULL. */
538 gdb_xml_parse_attr_enum (struct gdb_xml_parser
*parser
,
539 const struct gdb_xml_attribute
*attribute
,
542 const struct gdb_xml_enum
*enums
= attribute
->handler_data
;
545 for (enums
= attribute
->handler_data
; enums
->name
!= NULL
; enums
++)
546 if (strcmp (enums
->name
, value
) == 0)
549 if (enums
->name
== NULL
)
550 gdb_xml_error (parser
, _("Unknown attribute value %s=\"%s\""),
551 attribute
->name
, value
);
553 ret
= xmalloc (sizeof (enums
->value
));
554 memcpy (ret
, &enums
->value
, sizeof (enums
->value
));
558 #endif /* HAVE_LIBEXPAT */
561 show_debug_xml (struct ui_file
*file
, int from_tty
,
562 struct cmd_list_element
*c
, const char *value
)
564 fprintf_filtered (file
, _("XML debugging is %s.\n"), value
);
567 void _initialize_xml_support (void);
570 _initialize_xml_support (void)
572 add_setshow_boolean_cmd ("xml", class_maintenance
, &debug_xml
,
573 _("Set XML parser debugging."),
574 _("Show XML parser debugging."),
575 _("When set, debugging messages for XML parsers "
577 NULL
, show_debug_xml
,
578 &setdebuglist
, &showdebuglist
);