Commit | Line | Data |
---|---|---|
fd79ecee DJ |
1 | /* Helper routines for parsing XML using Expat. |
2 | ||
3 | Copyright (C) 2006 | |
4 | Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. */ | |
22 | ||
23 | #include "defs.h" | |
e776119f DJ |
24 | #include "gdbcmd.h" |
25 | ||
26 | /* Debugging flag. */ | |
27 | static int debug_xml; | |
fd79ecee DJ |
28 | |
29 | /* The contents of this file are only useful if XML support is | |
30 | available. */ | |
31 | #ifdef HAVE_LIBEXPAT | |
32 | ||
33 | #include "exceptions.h" | |
34 | #include "xml-support.h" | |
35 | ||
dbc981de | 36 | #include "gdb_expat.h" |
fd79ecee | 37 | #include "gdb_string.h" |
e776119f DJ |
38 | #include "safe-ctype.h" |
39 | ||
40 | /* A parsing level -- used to keep track of the current element | |
41 | nesting. */ | |
42 | struct scope_level | |
43 | { | |
44 | /* Elements we allow at this level. */ | |
45 | const struct gdb_xml_element *elements; | |
46 | ||
47 | /* The element which we are within. */ | |
48 | const struct gdb_xml_element *element; | |
49 | ||
50 | /* Mask of which elements we've seen at this level (used for | |
51 | optional and repeatable checking). */ | |
52 | unsigned int seen; | |
53 | ||
54 | /* Body text accumulation. */ | |
55 | struct obstack *body; | |
56 | }; | |
57 | typedef struct scope_level scope_level_s; | |
58 | DEF_VEC_O(scope_level_s); | |
59 | ||
60 | /* The parser itself, and our additional state. */ | |
61 | struct gdb_xml_parser | |
62 | { | |
63 | XML_Parser expat_parser; /* The underlying expat parser. */ | |
64 | ||
65 | const char *name; /* Name of this parser. */ | |
66 | void *user_data; /* The user's callback data, for handlers. */ | |
67 | ||
68 | VEC(scope_level_s) *scopes; /* Scoping stack. */ | |
69 | ||
70 | struct gdb_exception error; /* A thrown error, if any. */ | |
71 | int last_line; /* The line of the thrown error, or 0. */ | |
72 | }; | |
73 | ||
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. */ | |
77 | ||
78 | static void | |
79 | gdb_xml_body_text (void *data, const XML_Char *text, int length) | |
80 | { | |
81 | struct gdb_xml_parser *parser = data; | |
82 | struct scope_level *scope = VEC_last (scope_level_s, parser->scopes); | |
83 | ||
ca4ca11e DJ |
84 | if (parser->error.reason < 0) |
85 | return; | |
86 | ||
e776119f DJ |
87 | if (scope->body == NULL) |
88 | { | |
89 | scope->body = XZALLOC (struct obstack); | |
90 | obstack_init (scope->body); | |
91 | } | |
92 | ||
93 | obstack_grow (scope->body, text, length); | |
94 | } | |
fd79ecee | 95 | |
e776119f | 96 | /* Issue a debugging message from one of PARSER's handlers. */ |
fd79ecee | 97 | |
e776119f DJ |
98 | void |
99 | gdb_xml_debug (struct gdb_xml_parser *parser, const char *format, ...) | |
100 | { | |
101 | int line = XML_GetCurrentLineNumber (parser->expat_parser); | |
102 | va_list ap; | |
103 | char *message; | |
104 | ||
105 | if (!debug_xml) | |
106 | return; | |
107 | ||
108 | va_start (ap, format); | |
109 | message = xstrvprintf (format, ap); | |
110 | if (line) | |
111 | fprintf_unfiltered (gdb_stderr, "%s (line %d): %s\n", | |
112 | parser->name, line, message); | |
113 | else | |
114 | fprintf_unfiltered (gdb_stderr, "%s: %s\n", | |
115 | parser->name, message); | |
116 | xfree (message); | |
117 | } | |
118 | ||
119 | /* Issue an error message from one of PARSER's handlers, and stop | |
120 | parsing. */ | |
121 | ||
122 | void | |
123 | gdb_xml_error (struct gdb_xml_parser *parser, const char *format, ...) | |
124 | { | |
125 | int line = XML_GetCurrentLineNumber (parser->expat_parser); | |
126 | va_list ap; | |
127 | ||
128 | parser->last_line = line; | |
129 | va_start (ap, format); | |
130 | throw_verror (XML_PARSE_ERROR, format, ap); | |
131 | } | |
132 | ||
133 | /* Clean up a vector of parsed attribute values. */ | |
134 | ||
135 | static void | |
136 | gdb_xml_values_cleanup (void *data) | |
137 | { | |
138 | VEC(gdb_xml_value_s) **values = data; | |
139 | struct gdb_xml_value *value; | |
140 | int ix; | |
141 | ||
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); | |
145 | } | |
146 | ||
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. */ | |
150 | ||
151 | static void | |
152 | gdb_xml_start_element (void *data, const XML_Char *name, | |
153 | const XML_Char **attrs) | |
fd79ecee | 154 | { |
e776119f DJ |
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; | |
161 | unsigned int seen; | |
162 | struct cleanup *back_to; | |
163 | ||
164 | back_to = make_cleanup (gdb_xml_values_cleanup, &attributes); | |
165 | ||
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 | |
168 | element. */ | |
169 | memset (&new_scope, 0, sizeof (new_scope)); | |
170 | VEC_safe_push (scope_level_s, parser->scopes, &new_scope); | |
171 | ||
172 | gdb_xml_debug (parser, _("Entering element <%s>"), name); | |
173 | ||
174 | /* Find this element in the list of the current scope's allowed | |
175 | children. Record that we've seen it. */ | |
176 | ||
177 | seen = 1; | |
178 | for (element = scope->elements; element && element->name; | |
179 | element++, seen <<= 1) | |
180 | if (strcmp (element->name, name) == 0) | |
181 | break; | |
182 | ||
183 | if (element == NULL || element->name == NULL) | |
184 | { | |
185 | gdb_xml_debug (parser, _("Element <%s> unknown"), name); | |
186 | do_cleanups (back_to); | |
187 | return; | |
188 | } | |
189 | ||
190 | if (!(element->flags & GDB_XML_EF_REPEATABLE) && (seen & scope->seen)) | |
191 | gdb_xml_error (parser, _("Element <%s> only expected once"), name); | |
192 | ||
193 | scope->seen |= seen; | |
194 | ||
195 | for (attribute = element->attributes; | |
196 | attribute != NULL && attribute->name != NULL; | |
197 | attribute++) | |
198 | { | |
199 | const char *val = NULL; | |
200 | const XML_Char **p; | |
201 | void *parsed_value; | |
202 | struct gdb_xml_value new_value; | |
203 | ||
204 | for (p = attrs; *p != NULL; p += 2) | |
205 | if (!strcmp (attribute->name, p[0])) | |
206 | { | |
207 | val = p[1]; | |
208 | break; | |
209 | } | |
210 | ||
211 | if (*p != NULL && val == NULL) | |
212 | { | |
213 | gdb_xml_debug (parser, _("Attribute \"%s\" missing a value"), | |
214 | attribute->name); | |
215 | continue; | |
216 | } | |
217 | ||
218 | if (*p == NULL && !(attribute->flags & GDB_XML_AF_OPTIONAL)) | |
219 | { | |
220 | gdb_xml_error (parser, _("Required attribute \"%s\" of " | |
221 | "<%s> not specified"), | |
222 | attribute->name, element->name); | |
223 | continue; | |
224 | } | |
225 | ||
226 | if (*p == NULL) | |
227 | continue; | |
228 | ||
229 | gdb_xml_debug (parser, _("Parsing attribute %s=\"%s\""), | |
230 | attribute->name, val); | |
231 | ||
232 | if (attribute->handler) | |
233 | parsed_value = attribute->handler (parser, attribute, val); | |
234 | else | |
235 | parsed_value = xstrdup (val); | |
236 | ||
237 | new_value.name = attribute->name; | |
238 | new_value.value = parsed_value; | |
239 | VEC_safe_push (gdb_xml_value_s, attributes, &new_value); | |
240 | } | |
241 | ||
242 | /* Check for unrecognized attributes. */ | |
243 | if (debug_xml) | |
fd79ecee | 244 | { |
e776119f DJ |
245 | const XML_Char **p; |
246 | ||
247 | for (p = attrs; *p != NULL; p += 2) | |
248 | { | |
249 | for (attribute = element->attributes; | |
250 | attribute != NULL && attribute->name != NULL; | |
251 | attribute++) | |
252 | if (strcmp (attribute->name, *p) == 0) | |
253 | break; | |
254 | ||
255 | if (attribute == NULL || attribute->name == NULL) | |
256 | gdb_xml_debug (parser, _("Ignoring unknown attribute %s"), *p); | |
257 | } | |
258 | } | |
259 | ||
260 | /* Call the element handler if there is one. */ | |
261 | if (element->start_handler) | |
262 | element->start_handler (parser, element, parser->user_data, attributes); | |
263 | ||
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; | |
268 | ||
269 | do_cleanups (back_to); | |
270 | } | |
271 | ||
272 | /* Wrapper for gdb_xml_start_element, to prevent throwing exceptions | |
273 | through expat. */ | |
274 | ||
275 | static void | |
276 | gdb_xml_start_element_wrapper (void *data, const XML_Char *name, | |
277 | const XML_Char **attrs) | |
278 | { | |
279 | struct gdb_xml_parser *parser = data; | |
280 | volatile struct gdb_exception ex; | |
281 | ||
282 | if (parser->error.reason < 0) | |
283 | return; | |
fd79ecee | 284 | |
e776119f DJ |
285 | TRY_CATCH (ex, RETURN_MASK_ALL) |
286 | { | |
287 | gdb_xml_start_element (data, name, attrs); | |
288 | } | |
289 | if (ex.reason < 0) | |
290 | { | |
291 | parser->error = ex; | |
ca4ca11e | 292 | #ifdef HAVE_XML_STOPPARSER |
e776119f | 293 | XML_StopParser (parser->expat_parser, XML_FALSE); |
ca4ca11e | 294 | #endif |
fd79ecee | 295 | } |
e776119f DJ |
296 | } |
297 | ||
298 | /* Handle the end of an element. DATA is our local XML parser, and | |
299 | NAME is the current element. */ | |
300 | ||
301 | static void | |
302 | gdb_xml_end_element (void *data, const XML_Char *name) | |
303 | { | |
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; | |
307 | unsigned int seen; | |
308 | char *body; | |
309 | ||
310 | gdb_xml_debug (parser, _("Leaving element <%s>"), name); | |
311 | ||
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) | |
d097fa3e | 317 | gdb_xml_error (parser, _("Required element <%s> is missing"), |
e776119f DJ |
318 | element->name); |
319 | ||
320 | /* Call the element processor. */ | |
321 | if (scope->body == NULL) | |
322 | body = ""; | |
323 | else | |
324 | { | |
325 | int length; | |
326 | ||
327 | length = obstack_object_size (scope->body); | |
328 | obstack_1grow (scope->body, '\0'); | |
329 | body = obstack_finish (scope->body); | |
330 | ||
331 | /* Strip leading and trailing whitespace. */ | |
332 | while (length > 0 && ISSPACE (body[length-1])) | |
333 | body[--length] = '\0'; | |
334 | while (*body && ISSPACE (*body)) | |
335 | body++; | |
336 | } | |
337 | ||
338 | if (scope->element != NULL && scope->element->end_handler) | |
339 | scope->element->end_handler (parser, scope->element, parser->user_data, | |
340 | body); | |
341 | ||
342 | /* Pop the scope level. */ | |
343 | if (scope->body) | |
344 | { | |
345 | obstack_free (scope->body, NULL); | |
346 | xfree (scope->body); | |
347 | } | |
348 | VEC_pop (scope_level_s, parser->scopes); | |
349 | } | |
350 | ||
351 | /* Wrapper for gdb_xml_end_element, to prevent throwing exceptions | |
352 | through expat. */ | |
353 | ||
354 | static void | |
355 | gdb_xml_end_element_wrapper (void *data, const XML_Char *name) | |
356 | { | |
357 | struct gdb_xml_parser *parser = data; | |
358 | volatile struct gdb_exception ex; | |
359 | ||
360 | if (parser->error.reason < 0) | |
361 | return; | |
362 | ||
363 | TRY_CATCH (ex, RETURN_MASK_ALL) | |
364 | { | |
365 | gdb_xml_end_element (data, name); | |
366 | } | |
367 | if (ex.reason < 0) | |
368 | { | |
369 | parser->error = ex; | |
ca4ca11e | 370 | #ifdef HAVE_XML_STOPPARSER |
e776119f | 371 | XML_StopParser (parser->expat_parser, XML_FALSE); |
ca4ca11e | 372 | #endif |
e776119f DJ |
373 | } |
374 | } | |
375 | ||
376 | /* Free a parser and all its associated state. */ | |
377 | ||
378 | static void | |
379 | gdb_xml_cleanup (void *arg) | |
380 | { | |
381 | struct gdb_xml_parser *parser = arg; | |
382 | struct scope_level *scope; | |
383 | int ix; | |
384 | ||
385 | XML_ParserFree (parser->expat_parser); | |
386 | ||
387 | /* Clean up the scopes. */ | |
388 | for (ix = 0; VEC_iterate (scope_level_s, parser->scopes, ix, scope); ix++) | |
389 | if (scope->body) | |
390 | { | |
391 | obstack_free (scope->body, NULL); | |
392 | xfree (scope->body); | |
393 | } | |
394 | VEC_free (scope_level_s, parser->scopes); | |
395 | ||
396 | xfree (parser); | |
397 | } | |
398 | ||
399 | /* Initialize and return a parser. Register a cleanup to destroy the | |
400 | parser. */ | |
401 | ||
402 | struct gdb_xml_parser * | |
403 | gdb_xml_create_parser_and_cleanup (const char *name, | |
404 | const struct gdb_xml_element *elements, | |
405 | void *user_data) | |
406 | { | |
407 | struct gdb_xml_parser *parser; | |
408 | struct scope_level start_scope; | |
409 | ||
410 | /* Initialize the parser. */ | |
411 | parser = XZALLOC (struct gdb_xml_parser); | |
412 | parser->expat_parser = XML_ParserCreateNS (NULL, '!'); | |
413 | if (parser->expat_parser == NULL) | |
414 | { | |
415 | xfree (parser); | |
416 | nomem (0); | |
417 | } | |
418 | ||
419 | parser->name = name; | |
420 | ||
421 | parser->user_data = user_data; | |
422 | XML_SetUserData (parser->expat_parser, parser); | |
423 | ||
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); | |
428 | ||
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); | |
433 | ||
434 | make_cleanup (gdb_xml_cleanup, parser); | |
435 | ||
436 | return parser; | |
437 | } | |
438 | ||
439 | /* Invoke PARSER on BUFFER. BUFFER is the data to parse, which | |
440 | should be NUL-terminated. | |
441 | ||
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. */ | |
445 | ||
446 | int | |
447 | gdb_xml_parse (struct gdb_xml_parser *parser, const char *buffer) | |
448 | { | |
449 | enum XML_Status status; | |
450 | const char *error_string; | |
451 | ||
452 | status = XML_Parse (parser->expat_parser, buffer, strlen (buffer), 1); | |
453 | ||
454 | if (status == XML_STATUS_OK && parser->error.reason == 0) | |
455 | return 0; | |
456 | ||
457 | if (parser->error.reason == RETURN_ERROR | |
458 | && parser->error.error == XML_PARSE_ERROR) | |
459 | { | |
460 | gdb_assert (parser->error.message != NULL); | |
461 | error_string = parser->error.message; | |
462 | } | |
463 | else if (status == XML_STATUS_ERROR) | |
464 | { | |
465 | enum XML_Error err = XML_GetErrorCode (parser->expat_parser); | |
466 | error_string = XML_ErrorString (err); | |
467 | } | |
468 | else | |
469 | { | |
470 | gdb_assert (parser->error.reason < 0); | |
471 | throw_exception (parser->error); | |
472 | } | |
473 | ||
474 | if (parser->last_line != 0) | |
475 | warning (_("while parsing %s (at line %d): %s"), parser->name, | |
476 | parser->last_line, error_string); | |
477 | else | |
478 | warning (_("while parsing %s: %s"), parser->name, error_string); | |
479 | ||
480 | return -1; | |
fd79ecee DJ |
481 | } |
482 | ||
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. | |
486 | ||
487 | Returns 0 for success, -1 for error. */ | |
488 | ||
489 | static int | |
490 | xml_parse_unsigned_integer (const char *valstr, ULONGEST *valp) | |
491 | { | |
492 | const char *endptr; | |
493 | ULONGEST result; | |
494 | ||
495 | if (*valstr == '\0') | |
496 | return -1; | |
497 | ||
498 | result = strtoulst (valstr, &endptr, 0); | |
499 | if (*endptr != '\0') | |
500 | return -1; | |
501 | ||
502 | *valp = result; | |
503 | return 0; | |
504 | } | |
505 | ||
e776119f DJ |
506 | /* Parse an integer string into a ULONGEST and return it, or call |
507 | gdb_xml_error if it could not be parsed. */ | |
fd79ecee DJ |
508 | |
509 | ULONGEST | |
e776119f | 510 | gdb_xml_parse_ulongest (struct gdb_xml_parser *parser, const char *value) |
fd79ecee DJ |
511 | { |
512 | ULONGEST result; | |
fd79ecee DJ |
513 | |
514 | if (xml_parse_unsigned_integer (value, &result) != 0) | |
e776119f DJ |
515 | gdb_xml_error (parser, _("Can't convert \"%s\" to an integer"), value); |
516 | ||
fd79ecee DJ |
517 | return result; |
518 | } | |
519 | ||
e776119f DJ |
520 | /* Parse an integer attribute into a ULONGEST. */ |
521 | ||
522 | void * | |
523 | gdb_xml_parse_attr_ulongest (struct gdb_xml_parser *parser, | |
524 | const struct gdb_xml_attribute *attribute, | |
525 | const char *value) | |
526 | { | |
527 | ULONGEST result; | |
528 | void *ret; | |
fd79ecee | 529 | |
e776119f DJ |
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); | |
fd79ecee | 533 | |
e776119f DJ |
534 | ret = xmalloc (sizeof (result)); |
535 | memcpy (ret, &result, sizeof (result)); | |
536 | return ret; | |
537 | } | |
538 | ||
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. */ | |
543 | ||
544 | void * | |
545 | gdb_xml_parse_attr_enum (struct gdb_xml_parser *parser, | |
546 | const struct gdb_xml_attribute *attribute, | |
547 | const char *value) | |
fd79ecee | 548 | { |
e776119f DJ |
549 | const struct gdb_xml_enum *enums = attribute->handler_data; |
550 | void *ret; | |
fd79ecee | 551 | |
e776119f DJ |
552 | for (enums = attribute->handler_data; enums->name != NULL; enums++) |
553 | if (strcmp (enums->name, value) == 0) | |
554 | break; | |
555 | ||
556 | if (enums->name == NULL) | |
557 | gdb_xml_error (parser, _("Unknown attribute value %s=\"%s\""), | |
558 | attribute->name, value); | |
559 | ||
560 | ret = xmalloc (sizeof (enums->value)); | |
561 | memcpy (ret, &enums->value, sizeof (enums->value)); | |
562 | return ret; | |
fd79ecee DJ |
563 | } |
564 | ||
e776119f | 565 | #endif /* HAVE_LIBEXPAT */ |
fd79ecee DJ |
566 | |
567 | static void | |
e776119f DJ |
568 | show_debug_xml (struct ui_file *file, int from_tty, |
569 | struct cmd_list_element *c, const char *value) | |
fd79ecee | 570 | { |
e776119f | 571 | fprintf_filtered (file, _("XML debugging is %s.\n"), value); |
fd79ecee DJ |
572 | } |
573 | ||
e776119f | 574 | void _initialize_xml_support (void); |
fd79ecee DJ |
575 | |
576 | void | |
e776119f | 577 | _initialize_xml_support (void) |
fd79ecee | 578 | { |
e776119f DJ |
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 " | |
583 | "are displayed."), | |
584 | NULL, show_debug_xml, | |
585 | &setdebuglist, &showdebuglist); | |
fd79ecee | 586 | } |