1 /* Routines for handling XML generic OS data provided by target.
3 Copyright (C) 2008-2017 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"
28 #if !defined(HAVE_LIBEXPAT)
31 osdata_parse (const char *xml
)
33 static int have_warned
;
38 warning (_("Can not parse XML OS data; XML support was disabled "
45 #else /* HAVE_LIBEXPAT */
47 /* Internal parsing data passed to all XML callbacks. */
48 struct osdata_parsing_data
50 struct osdata
*osdata
;
54 /* Handle the start of a <osdata> element. */
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
= (struct osdata_parsing_data
*) user_data
;
63 struct osdata
*osdata
;
66 gdb_xml_error (parser
, _("Seen more than on osdata element"));
68 type
= (char *) 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. */
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
= (struct osdata_parsing_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. */
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
= (struct osdata_parsing_data
*) user_data
;
96 = (const char *) xml_find_attribute (attributes
, "name")->value
;
98 data
->property_name
= xstrdup (name
);
101 /* Handle the end of a <column> element. */
104 osdata_end_column (struct gdb_xml_parser
*parser
,
105 const struct gdb_xml_element
*element
,
106 void *user_data
, const char *body_text
)
108 struct osdata_parsing_data
*data
= (struct osdata_parsing_data
*) user_data
;
109 struct osdata
*osdata
= data
->osdata
;
110 struct osdata_item
*item
= VEC_last (osdata_item_s
, osdata
->items
);
111 struct osdata_column
*col
= VEC_safe_push (osdata_column_s
,
112 item
->columns
, NULL
);
114 /* Transfer memory ownership. NAME was already strdup'ed. */
115 col
->name
= data
->property_name
;
116 col
->value
= xstrdup (body_text
);
117 data
->property_name
= NULL
;
120 /* Discard the constructed osdata (if an error occurs). */
123 clear_parsing_data (void *p
)
125 struct osdata_parsing_data
*data
= (struct osdata_parsing_data
*) p
;
127 osdata_free (data
->osdata
);
129 xfree (data
->property_name
);
130 data
->property_name
= NULL
;
133 /* The allowed elements and attributes for OS data object.
134 The root element is a <osdata>. */
136 const struct gdb_xml_attribute column_attributes
[] = {
137 { "name", GDB_XML_AF_NONE
, NULL
, NULL
},
138 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
141 const struct gdb_xml_element item_children
[] = {
142 { "column", column_attributes
, NULL
,
143 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
144 osdata_start_column
, osdata_end_column
},
145 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
148 const struct gdb_xml_attribute osdata_attributes
[] = {
149 { "type", GDB_XML_AF_NONE
, NULL
, NULL
},
150 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
153 const struct gdb_xml_element osdata_children
[] = {
154 { "item", NULL
, item_children
,
155 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
156 osdata_start_item
, NULL
},
157 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
160 const struct gdb_xml_element osdata_elements
[] = {
161 { "osdata", osdata_attributes
, osdata_children
,
162 GDB_XML_EF_NONE
, osdata_start_osdata
, NULL
},
163 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
167 osdata_parse (const char *xml
)
169 struct cleanup
*back_to
;
170 struct osdata_parsing_data data
= { NULL
};
172 back_to
= make_cleanup (clear_parsing_data
, &data
);
174 if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
175 osdata_elements
, xml
, &data
) == 0)
177 /* Parsed successfully, don't need to delete the result. */
178 discard_cleanups (back_to
);
182 do_cleanups (back_to
);
188 osdata_item_clear (struct osdata_item
*item
)
190 if (item
->columns
!= NULL
)
192 struct osdata_column
*col
;
196 VEC_iterate (osdata_column_s
, item
->columns
,
203 VEC_free (osdata_column_s
, item
->columns
);
204 item
->columns
= NULL
;
209 osdata_free (struct osdata
*osdata
)
214 if (osdata
->items
!= NULL
)
216 struct osdata_item
*item
;
220 VEC_iterate (osdata_item_s
, osdata
->items
,
223 osdata_item_clear (item
);
224 VEC_free (osdata_item_s
, osdata
->items
);
231 osdata_free_cleanup (void *arg
)
233 struct osdata
*osdata
= (struct osdata
*) arg
;
235 osdata_free (osdata
);
239 make_cleanup_osdata_free (struct osdata
*data
)
241 return make_cleanup (osdata_free_cleanup
, data
);
245 get_osdata (const char *type
)
247 struct osdata
*osdata
= NULL
;
248 char *xml
= target_get_osdata (type
);
252 struct cleanup
*old_chain
= make_cleanup (xfree
, xml
);
257 warning (_("Empty data returned by target. Wrong osdata type?"));
259 warning (_("Empty type list returned by target. No type data?"));
262 osdata
= osdata_parse (xml
);
264 do_cleanups (old_chain
);
268 error (_("Can not fetch data now."));
274 get_osdata_column (struct osdata_item
*item
, const char *name
)
276 struct osdata_column
*col
;
280 VEC_iterate (osdata_column_s
, item
->columns
,
283 if (strcmp (col
->name
, name
) == 0)
290 info_osdata (const char *type
)
292 struct ui_out
*uiout
= current_uiout
;
293 struct osdata
*osdata
= NULL
;
294 struct osdata_item
*last
= NULL
;
295 struct cleanup
*old_chain
;
298 int col_to_skip
= -1;
303 osdata
= get_osdata (type
);
304 old_chain
= make_cleanup_osdata_free (osdata
);
306 nrows
= VEC_length (osdata_item_s
, osdata
->items
);
308 if (*type
== '\0' && nrows
== 0)
309 error (_("Available types of OS data not reported."));
311 if (!VEC_empty (osdata_item_s
, osdata
->items
))
313 last
= VEC_last (osdata_item_s
, osdata
->items
);
315 ncols
= VEC_length (osdata_column_s
, last
->columns
);
317 /* As a special case, scan the listing of available data types
318 for a column named "Title", and only include it with MI
319 output; this column's normal use is for titles for interface
320 elements like menus, and it clutters up CLI output. */
321 if (*type
== '\0' && !uiout
->is_mi_like_p ())
323 struct osdata_column
*col
;
327 VEC_iterate (osdata_column_s
, last
->columns
, ix
, col
);
330 if (strcmp (col
->name
, "Title") == 0)
333 /* Be sure to reduce the total column count, otherwise
334 internal errors ensue. */
335 if (col_to_skip
>= 0)
340 ui_out_emit_table
table_emitter (uiout
, ncols
, nrows
, "OSDataTable");
342 /* With no columns/items, we just output an empty table, but we
343 still output the table. This matters for MI. */
346 do_cleanups (old_chain
);
350 if (last
&& last
->columns
)
352 struct osdata_column
*col
;
356 VEC_iterate (osdata_column_s
, last
->columns
,
362 if (ix
== col_to_skip
)
365 snprintf (col_name
, 32, "col%d", ix
);
366 uiout
->table_header (10, ui_left
,
367 col_name
, col
->name
);
371 uiout
->table_body ();
375 struct osdata_item
*item
;
379 VEC_iterate (osdata_item_s
, osdata
->items
,
384 struct osdata_column
*col
;
387 ui_out_emit_tuple
tuple_emitter (uiout
, "item");
390 VEC_iterate (osdata_column_s
, item
->columns
,
396 if (ix_cols
== col_to_skip
)
399 snprintf (col_name
, 32, "col%d", ix_cols
);
400 uiout
->field_string (col_name
, col
->value
);
408 do_cleanups (old_chain
);
412 info_osdata_command (char *arg
, int from_tty
)
418 _initialize_osdata (void)
420 add_info ("os", info_osdata_command
,
421 _("Show OS data ARG."));