1 /* Routines for handling XML generic OS data provided by target.
3 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "xml-support.h"
25 #include "gdb_string.h"
29 #if !defined(HAVE_LIBEXPAT)
32 osdata_parse (const char *xml)
34 static int have_warned;
39 warning (_("Can not parse XML OS data; XML support was disabled "
46 #else /* HAVE_LIBEXPAT */
48 #include "xml-support.h"
50 /* Internal parsing data passed to all XML callbacks. */
51 struct osdata_parsing_data
53 struct osdata *osdata;
57 /* Handle the start of a <osdata> element. */
60 osdata_start_osdata (struct gdb_xml_parser *parser,
61 const struct gdb_xml_element *element,
62 void *user_data, VEC(gdb_xml_value_s) *attributes)
64 struct osdata_parsing_data *data = user_data;
66 struct osdata *osdata;
69 gdb_xml_error (parser, _("Seen more than on osdata element"));
71 type = VEC_index (gdb_xml_value_s, attributes, 0)->value;
72 osdata = XZALLOC (struct osdata);
73 osdata->type = xstrdup (type);
74 data->osdata = osdata;
77 /* Handle the start of a <item> element. */
80 osdata_start_item (struct gdb_xml_parser *parser,
81 const struct gdb_xml_element *element,
82 void *user_data, VEC(gdb_xml_value_s) *attributes)
84 struct osdata_parsing_data *data = user_data;
85 struct osdata_item item = { NULL };
87 VEC_safe_push (osdata_item_s, data->osdata->items, &item);
90 /* Handle the start of a <column> element. */
93 osdata_start_column (struct gdb_xml_parser *parser,
94 const struct gdb_xml_element *element,
95 void *user_data, VEC(gdb_xml_value_s) *attributes)
97 struct osdata_parsing_data *data = user_data;
98 const char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
100 data->property_name = xstrdup (name);
103 /* Handle the end of a <column> element. */
106 osdata_end_column (struct gdb_xml_parser *parser,
107 const struct gdb_xml_element *element,
108 void *user_data, const char *body_text)
110 struct osdata_parsing_data *data = user_data;
111 struct osdata *osdata = data->osdata;
112 struct osdata_item *item = VEC_last (osdata_item_s, osdata->items);
113 struct osdata_column *col = VEC_safe_push (osdata_column_s,
114 item->columns, NULL);
116 /* Transfer memory ownership. NAME was already strdup'ed. */
117 col->name = data->property_name;
118 col->value = xstrdup (body_text);
119 data->property_name = NULL;
122 /* Discard the constructed osdata (if an error occurs). */
125 clear_parsing_data (void *p)
127 struct osdata_parsing_data *data = p;
129 osdata_free (data->osdata);
131 xfree (data->property_name);
132 data->property_name = NULL;
135 /* The allowed elements and attributes for OS data object.
136 The root element is a <osdata>. */
138 const struct gdb_xml_attribute column_attributes[] = {
139 { "name", GDB_XML_AF_NONE, NULL, NULL },
140 { NULL, GDB_XML_AF_NONE, NULL, NULL }
143 const struct gdb_xml_element item_children[] = {
144 { "column", column_attributes, NULL,
145 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
146 osdata_start_column, osdata_end_column },
147 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
150 const struct gdb_xml_attribute osdata_attributes[] = {
151 { "type", GDB_XML_AF_NONE, NULL, NULL },
152 { NULL, GDB_XML_AF_NONE, NULL, NULL }
155 const struct gdb_xml_element osdata_children[] = {
156 { "item", NULL, item_children,
157 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
158 osdata_start_item, NULL },
159 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
162 const struct gdb_xml_element osdata_elements[] = {
163 { "osdata", osdata_attributes, osdata_children,
164 GDB_XML_EF_NONE, osdata_start_osdata, NULL },
165 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
169 osdata_parse (const char *xml)
171 struct cleanup *back_to;
172 struct osdata_parsing_data data = { NULL };
174 back_to = make_cleanup (clear_parsing_data, &data);
176 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
177 osdata_elements, xml, &data) == 0)
179 /* Parsed successfully, don't need to delete the result. */
180 discard_cleanups (back_to);
184 do_cleanups (back_to);
190 osdata_item_clear (struct osdata_item *item)
192 if (item->columns != NULL)
194 struct osdata_column *col;
198 VEC_iterate (osdata_column_s, item->columns,
205 VEC_free (osdata_column_s, item->columns);
206 item->columns = NULL;
211 osdata_free (struct osdata *osdata)
216 if (osdata->items != NULL)
218 struct osdata_item *item;
222 VEC_iterate (osdata_item_s, osdata->items,
225 osdata_item_clear (item);
226 VEC_free (osdata_item_s, osdata->items);
233 osdata_free_cleanup (void *arg)
235 struct osdata *osdata = arg;
237 osdata_free (osdata);
241 make_cleanup_osdata_free (struct osdata *data)
243 return make_cleanup (osdata_free_cleanup, data);
247 get_osdata (const char *type)
249 struct osdata *osdata = NULL;
250 char *xml = target_get_osdata (type);
254 struct cleanup *old_chain = make_cleanup (xfree, xml);
259 warning (_("Empty data returned by target. Wrong osdata type?"));
261 warning (_("Empty type list returned by target. No type data?"));
264 osdata = osdata_parse (xml);
266 do_cleanups (old_chain);
270 error (_("Can not fetch data now.\n"));
276 get_osdata_column (struct osdata_item *item, const char *name)
278 struct osdata_column *col;
282 VEC_iterate (osdata_column_s, item->columns,
285 if (strcmp (col->name, name) == 0)
292 info_osdata_command (char *type, int from_tty)
294 struct osdata *osdata = NULL;
295 struct osdata_item *last;
296 struct cleanup *old_chain;
300 osdata = get_osdata (type);
301 old_chain = make_cleanup_osdata_free (osdata);
303 nprocs = VEC_length (osdata_item_s, osdata->items);
305 if (!type && nprocs == 0)
306 error (_("Available types of OS data not reported."));
308 last = VEC_last (osdata_item_s, osdata->items);
309 if (last && last->columns)
310 ncols = VEC_length (osdata_column_s, last->columns);
314 make_cleanup_ui_out_table_begin_end (uiout, ncols, nprocs,
317 if (last && last->columns)
319 struct osdata_column *col;
323 VEC_iterate (osdata_column_s, last->columns,
326 ui_out_table_header (uiout, 10, ui_left,
327 col->name, col->name);
330 ui_out_table_body (uiout);
334 struct osdata_item *item;
338 VEC_iterate (osdata_item_s, osdata->items,
342 struct cleanup *old_chain;
343 struct ui_stream *stb;
345 struct osdata_column *col;
347 stb = ui_out_stream_new (uiout);
348 old_chain = make_cleanup_ui_out_stream_delete (stb);
349 make_cleanup_ui_out_tuple_begin_end (uiout, "item");
352 VEC_iterate (osdata_column_s, item->columns,
355 ui_out_field_string (uiout, col->name, col->value);
357 do_cleanups (old_chain);
359 ui_out_text (uiout, "\n");
363 do_cleanups (old_chain);
366 extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */
369 _initialize_osdata (void)
371 add_info ("os", info_osdata_command,
372 _("Show OS data ARG."));