s390-vregs.exp: Avoid compile errors with older GCCs and on 31-bit targets
[binutils-gdb.git] / gdb / osdata.c
blobd33026c56fab37bb16ddb20aba2cae03c6cebd3e
1 /* Routines for handling XML generic OS data provided by target.
3 Copyright (C) 2008-2015 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/>. */
20 #include "defs.h"
21 #include "target.h"
22 #include "vec.h"
23 #include "xml-support.h"
24 #include "osdata.h"
25 #include "ui-out.h"
26 #include "gdbcmd.h"
28 #if !defined(HAVE_LIBEXPAT)
30 struct osdata *
31 osdata_parse (const char *xml)
33 static int have_warned;
35 if (!have_warned)
37 have_warned = 1;
38 warning (_("Can not parse XML OS data; XML support was disabled "
39 "at compile time"));
42 return NULL;
45 #else /* HAVE_LIBEXPAT */
47 /* Internal parsing data passed to all XML callbacks. */
48 struct osdata_parsing_data
50 struct osdata *osdata;
51 char *property_name;
54 /* Handle the start of a <osdata> element. */
56 static void
57 osdata_start_osdata (struct gdb_xml_parser *parser,
58 const struct gdb_xml_element *element,
59 void *user_data, VEC(gdb_xml_value_s) *attributes)
61 struct osdata_parsing_data *data = user_data;
62 char *type;
63 struct osdata *osdata;
65 if (data->osdata)
66 gdb_xml_error (parser, _("Seen more than on osdata element"));
68 type = xml_find_attribute (attributes, "type")->value;
69 osdata = XCNEW (struct osdata);
70 osdata->type = xstrdup (type);
71 data->osdata = osdata;
74 /* Handle the start of a <item> element. */
76 static void
77 osdata_start_item (struct gdb_xml_parser *parser,
78 const struct gdb_xml_element *element,
79 void *user_data, VEC(gdb_xml_value_s) *attributes)
81 struct osdata_parsing_data *data = user_data;
82 struct osdata_item item = { NULL };
84 VEC_safe_push (osdata_item_s, data->osdata->items, &item);
87 /* Handle the start of a <column> element. */
89 static void
90 osdata_start_column (struct gdb_xml_parser *parser,
91 const struct gdb_xml_element *element,
92 void *user_data, VEC(gdb_xml_value_s) *attributes)
94 struct osdata_parsing_data *data = user_data;
95 const char *name = xml_find_attribute (attributes, "name")->value;
97 data->property_name = xstrdup (name);
100 /* Handle the end of a <column> element. */
102 static void
103 osdata_end_column (struct gdb_xml_parser *parser,
104 const struct gdb_xml_element *element,
105 void *user_data, const char *body_text)
107 struct osdata_parsing_data *data = user_data;
108 struct osdata *osdata = data->osdata;
109 struct osdata_item *item = VEC_last (osdata_item_s, osdata->items);
110 struct osdata_column *col = VEC_safe_push (osdata_column_s,
111 item->columns, NULL);
113 /* Transfer memory ownership. NAME was already strdup'ed. */
114 col->name = data->property_name;
115 col->value = xstrdup (body_text);
116 data->property_name = NULL;
119 /* Discard the constructed osdata (if an error occurs). */
121 static void
122 clear_parsing_data (void *p)
124 struct osdata_parsing_data *data = p;
126 osdata_free (data->osdata);
127 data->osdata = NULL;
128 xfree (data->property_name);
129 data->property_name = NULL;
132 /* The allowed elements and attributes for OS data object.
133 The root element is a <osdata>. */
135 const struct gdb_xml_attribute column_attributes[] = {
136 { "name", GDB_XML_AF_NONE, NULL, NULL },
137 { NULL, GDB_XML_AF_NONE, NULL, NULL }
140 const struct gdb_xml_element item_children[] = {
141 { "column", column_attributes, NULL,
142 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
143 osdata_start_column, osdata_end_column },
144 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
147 const struct gdb_xml_attribute osdata_attributes[] = {
148 { "type", GDB_XML_AF_NONE, NULL, NULL },
149 { NULL, GDB_XML_AF_NONE, NULL, NULL }
152 const struct gdb_xml_element osdata_children[] = {
153 { "item", NULL, item_children,
154 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
155 osdata_start_item, NULL },
156 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
159 const struct gdb_xml_element osdata_elements[] = {
160 { "osdata", osdata_attributes, osdata_children,
161 GDB_XML_EF_NONE, osdata_start_osdata, NULL },
162 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
165 struct osdata *
166 osdata_parse (const char *xml)
168 struct cleanup *back_to;
169 struct osdata_parsing_data data = { NULL };
171 back_to = make_cleanup (clear_parsing_data, &data);
173 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
174 osdata_elements, xml, &data) == 0)
176 /* Parsed successfully, don't need to delete the result. */
177 discard_cleanups (back_to);
178 return data.osdata;
181 do_cleanups (back_to);
182 return NULL;
184 #endif
186 static void
187 osdata_item_clear (struct osdata_item *item)
189 if (item->columns != NULL)
191 struct osdata_column *col;
192 int ix;
194 for (ix = 0;
195 VEC_iterate (osdata_column_s, item->columns,
196 ix, col);
197 ix++)
199 xfree (col->name);
200 xfree (col->value);
202 VEC_free (osdata_column_s, item->columns);
203 item->columns = NULL;
207 void
208 osdata_free (struct osdata *osdata)
210 if (osdata == NULL)
211 return;
213 if (osdata->items != NULL)
215 struct osdata_item *item;
216 int ix;
218 for (ix = 0;
219 VEC_iterate (osdata_item_s, osdata->items,
220 ix, item);
221 ix++)
222 osdata_item_clear (item);
223 VEC_free (osdata_item_s, osdata->items);
226 xfree (osdata);
229 static void
230 osdata_free_cleanup (void *arg)
232 struct osdata *osdata = arg;
234 osdata_free (osdata);
237 struct cleanup *
238 make_cleanup_osdata_free (struct osdata *data)
240 return make_cleanup (osdata_free_cleanup, data);
243 struct osdata *
244 get_osdata (const char *type)
246 struct osdata *osdata = NULL;
247 char *xml = target_get_osdata (type);
249 if (xml)
251 struct cleanup *old_chain = make_cleanup (xfree, xml);
253 if (xml[0] == '\0')
255 if (type)
256 warning (_("Empty data returned by target. Wrong osdata type?"));
257 else
258 warning (_("Empty type list returned by target. No type data?"));
260 else
261 osdata = osdata_parse (xml);
263 do_cleanups (old_chain);
266 if (!osdata)
267 error (_("Can not fetch data now."));
269 return osdata;
272 const char *
273 get_osdata_column (struct osdata_item *item, const char *name)
275 struct osdata_column *col;
276 int ix_cols;
278 for (ix_cols = 0;
279 VEC_iterate (osdata_column_s, item->columns,
280 ix_cols, col);
281 ix_cols++)
282 if (strcmp (col->name, name) == 0)
283 return col->value;
285 return NULL;
288 void
289 info_osdata_command (char *type, int from_tty)
291 struct ui_out *uiout = current_uiout;
292 struct osdata *osdata = NULL;
293 struct osdata_item *last = NULL;
294 struct cleanup *old_chain;
295 int ncols = 0;
296 int nrows;
297 int col_to_skip = -1;
299 osdata = get_osdata (type);
300 old_chain = make_cleanup_osdata_free (osdata);
302 nrows = VEC_length (osdata_item_s, osdata->items);
304 if (!type && nrows == 0)
305 error (_("Available types of OS data not reported."));
307 if (!VEC_empty (osdata_item_s, osdata->items))
309 last = VEC_last (osdata_item_s, osdata->items);
310 if (last->columns)
311 ncols = VEC_length (osdata_column_s, last->columns);
313 /* As a special case, scan the listing of available data types
314 for a column named "Title", and only include it with MI
315 output; this column's normal use is for titles for interface
316 elements like menus, and it clutters up CLI output. */
317 if (!type && !ui_out_is_mi_like_p (uiout))
319 struct osdata_column *col;
320 int ix;
322 for (ix = 0;
323 VEC_iterate (osdata_column_s, last->columns, ix, col);
324 ix++)
326 if (strcmp (col->name, "Title") == 0)
327 col_to_skip = ix;
329 /* Be sure to reduce the total column count, otherwise
330 internal errors ensue. */
331 if (col_to_skip >= 0)
332 --ncols;
336 make_cleanup_ui_out_table_begin_end (uiout, ncols, nrows,
337 "OSDataTable");
339 /* With no columns/items, we just output an empty table, but we
340 still output the table. This matters for MI. */
341 if (ncols == 0)
343 do_cleanups (old_chain);
344 return;
347 if (last && last->columns)
349 struct osdata_column *col;
350 int ix;
352 for (ix = 0;
353 VEC_iterate (osdata_column_s, last->columns,
354 ix, col);
355 ix++)
357 char col_name[32];
359 if (ix == col_to_skip)
360 continue;
362 snprintf (col_name, 32, "col%d", ix);
363 ui_out_table_header (uiout, 10, ui_left,
364 col_name, col->name);
368 ui_out_table_body (uiout);
370 if (nrows != 0)
372 struct osdata_item *item;
373 int ix_items;
375 for (ix_items = 0;
376 VEC_iterate (osdata_item_s, osdata->items,
377 ix_items, item);
378 ix_items++)
380 struct cleanup *old_chain;
381 int ix_cols;
382 struct osdata_column *col;
384 old_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "item");
386 for (ix_cols = 0;
387 VEC_iterate (osdata_column_s, item->columns,
388 ix_cols, col);
389 ix_cols++)
391 char col_name[32];
393 if (ix_cols == col_to_skip)
394 continue;
396 snprintf (col_name, 32, "col%d", ix_cols);
397 ui_out_field_string (uiout, col_name, col->value);
400 do_cleanups (old_chain);
402 ui_out_text (uiout, "\n");
406 do_cleanups (old_chain);
409 extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */
411 void
412 _initialize_osdata (void)
414 add_info ("os", info_osdata_command,
415 _("Show OS data ARG."));