* Makefile.in (gdb_expat_h): New.
[external/binutils.git] / gdb / xml-support.c
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"
24
25 /* The contents of this file are only useful if XML support is
26    available.  */
27 #ifdef HAVE_LIBEXPAT
28
29 #include "exceptions.h"
30 #include "xml-support.h"
31
32 #include "gdb_expat.h"
33 #include "gdb_string.h"
34
35 /* Returns the value of attribute ATTR from expat attribute list
36    ATTRLIST.  If not found, throws an exception.  */
37
38 const XML_Char *
39 xml_get_required_attribute (const XML_Char **attrs,
40                             const XML_Char *attr)
41 {
42   const XML_Char **p;
43   for (p = attrs; *p; p += 2)
44     {
45       const char *name = p[0];
46       const char *val = p[1];
47
48       if (strcmp (name, attr) == 0)
49         return val;
50     }
51   throw_error (XML_PARSE_ERROR, _("Can't find attribute %s"), attr);
52 }
53
54 /* Parse a field VALSTR that we expect to contain an integer value.
55    The integer is returned in *VALP.  The string is parsed with an
56    equivalent to strtoul.
57
58    Returns 0 for success, -1 for error.  */
59
60 static int
61 xml_parse_unsigned_integer (const char *valstr, ULONGEST *valp)
62 {
63   const char *endptr;
64   ULONGEST result;
65
66   if (*valstr == '\0')
67     return -1;
68
69   result = strtoulst (valstr, &endptr, 0);
70   if (*endptr != '\0')
71     return -1;
72
73   *valp = result;
74   return 0;
75 }
76
77 /* Gets the value of an integer attribute named ATTR, if it's present.
78    If the attribute is not found, or can't be parsed as integer,
79    throws an exception.  */
80
81 ULONGEST
82 xml_get_integer_attribute (const XML_Char **attrs,
83                            const XML_Char *attr)
84 {
85   ULONGEST result;
86   const XML_Char *value = xml_get_required_attribute (attrs, attr);
87
88   if (xml_parse_unsigned_integer (value, &result) != 0)
89     {
90       throw_error (XML_PARSE_ERROR,
91                    _("Can't convert value of attribute %s, %s, to integer"),
92                    attr, value);
93     }
94   return result;
95 }
96
97 /* Obtains a value of attribute with enumerated type. In XML, enumerated
98    attributes have string as a value, and in C, they are represented as
99    values of enumerated type. This function maps the attribute onto
100    an integer value that can be immediately converted into enumerated
101    type.
102
103    First, obtains the string value of ATTR in ATTRS.
104    Then, finds the index of that value in XML_NAMES, which is a zero-terminated
105    array of strings. If found, returns the element of VALUES with that index.
106    Otherwise throws.  */
107
108 int
109 xml_get_enum_value (const XML_Char **attrs,
110                     const XML_Char *attr,
111                     const XML_Char **xml_names,
112                     int *values)
113 {
114   const XML_Char *value = xml_get_required_attribute (attrs, attr);
115
116   int i;
117   for (i = 0; xml_names[i]; ++i)
118     {
119       if (strcmp (xml_names[i], value) == 0)
120         return values[i];
121     }
122   throw_error (XML_PARSE_ERROR,
123                _("Invalid enumerated value in XML: %s"), value);
124 }
125
126 /* Cleanup wrapper for XML_ParserFree, with the correct type
127    for make_cleanup.  */
128
129 static void
130 free_xml_parser (void *parser)
131 {
132   XML_ParserFree (parser);
133 }
134
135 /* Register a cleanup to release PARSER.  Only the parser itself
136    is freed; another cleanup may be necessary to discard any
137    associated user data.  */
138
139 void
140 make_cleanup_free_xml_parser (XML_Parser parser)
141 {
142   make_cleanup (free_xml_parser, parser);
143 }
144
145 #endif /* HAVE_LIBEXPAT */