Commit | Line | Data |
---|---|---|
07e059b5 VP |
1 | /* Routines for handling XML generic OS data provided by target. |
2 | ||
28e7fd62 | 3 | Copyright (C) 2008-2013 Free Software Foundation, Inc. |
07e059b5 VP |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "target.h" | |
22 | #include "vec.h" | |
23 | #include "xml-support.h" | |
24 | #include "osdata.h" | |
25 | #include "gdb_string.h" | |
26 | #include "ui-out.h" | |
27 | #include "gdbcmd.h" | |
28 | ||
29 | #if !defined(HAVE_LIBEXPAT) | |
30 | ||
31 | struct osdata * | |
32 | osdata_parse (const char *xml) | |
33 | { | |
34 | static int have_warned; | |
35 | ||
36 | if (!have_warned) | |
37 | { | |
38 | have_warned = 1; | |
39 | warning (_("Can not parse XML OS data; XML support was disabled " | |
40 | "at compile time")); | |
41 | } | |
42 | ||
43 | return NULL; | |
44 | } | |
45 | ||
46 | #else /* HAVE_LIBEXPAT */ | |
47 | ||
48 | #include "xml-support.h" | |
49 | ||
50 | /* Internal parsing data passed to all XML callbacks. */ | |
51 | struct osdata_parsing_data | |
52 | { | |
53 | struct osdata *osdata; | |
54 | char *property_name; | |
55 | }; | |
56 | ||
07e059b5 VP |
57 | /* Handle the start of a <osdata> element. */ |
58 | ||
59 | static void | |
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) | |
63 | { | |
64 | struct osdata_parsing_data *data = user_data; | |
65 | char *type; | |
66 | struct osdata *osdata; | |
67 | ||
68 | if (data->osdata) | |
69 | gdb_xml_error (parser, _("Seen more than on osdata element")); | |
70 | ||
3d2c1d41 | 71 | type = xml_find_attribute (attributes, "type")->value; |
07e059b5 VP |
72 | osdata = XZALLOC (struct osdata); |
73 | osdata->type = xstrdup (type); | |
74 | data->osdata = osdata; | |
75 | } | |
76 | ||
77 | /* Handle the start of a <item> element. */ | |
78 | ||
79 | static void | |
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) | |
83 | { | |
84 | struct osdata_parsing_data *data = user_data; | |
85 | struct osdata_item item = { NULL }; | |
5cc80db3 | 86 | |
07e059b5 VP |
87 | VEC_safe_push (osdata_item_s, data->osdata->items, &item); |
88 | } | |
89 | ||
90 | /* Handle the start of a <column> element. */ | |
91 | ||
92 | static void | |
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) | |
96 | { | |
97 | struct osdata_parsing_data *data = user_data; | |
3d2c1d41 | 98 | const char *name = xml_find_attribute (attributes, "name")->value; |
5cc80db3 | 99 | |
07e059b5 VP |
100 | data->property_name = xstrdup (name); |
101 | } | |
102 | ||
103 | /* Handle the end of a <column> element. */ | |
104 | ||
105 | static void | |
106 | osdata_end_column (struct gdb_xml_parser *parser, | |
107 | const struct gdb_xml_element *element, | |
108 | void *user_data, const char *body_text) | |
109 | { | |
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); | |
115 | ||
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; | |
120 | } | |
121 | ||
122 | /* Discard the constructed osdata (if an error occurs). */ | |
123 | ||
124 | static void | |
125 | clear_parsing_data (void *p) | |
126 | { | |
127 | struct osdata_parsing_data *data = p; | |
5cc80db3 | 128 | |
07e059b5 VP |
129 | osdata_free (data->osdata); |
130 | data->osdata = NULL; | |
131 | xfree (data->property_name); | |
132 | data->property_name = NULL; | |
133 | } | |
134 | ||
135 | /* The allowed elements and attributes for OS data object. | |
136 | The root element is a <osdata>. */ | |
137 | ||
138 | const struct gdb_xml_attribute column_attributes[] = { | |
139 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
140 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
141 | }; | |
142 | ||
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 } | |
148 | }; | |
149 | ||
150 | const struct gdb_xml_attribute osdata_attributes[] = { | |
151 | { "type", GDB_XML_AF_NONE, NULL, NULL }, | |
152 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
153 | }; | |
154 | ||
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 } | |
160 | }; | |
161 | ||
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 } | |
166 | }; | |
167 | ||
168 | struct osdata * | |
169 | osdata_parse (const char *xml) | |
170 | { | |
efc0eabd | 171 | struct cleanup *back_to; |
07e059b5 VP |
172 | struct osdata_parsing_data data = { NULL }; |
173 | ||
efc0eabd | 174 | back_to = make_cleanup (clear_parsing_data, &data); |
07e059b5 | 175 | |
efc0eabd PA |
176 | if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd", |
177 | osdata_elements, xml, &data) == 0) | |
178 | { | |
179 | /* Parsed successfully, don't need to delete the result. */ | |
180 | discard_cleanups (back_to); | |
181 | return data.osdata; | |
182 | } | |
07e059b5 VP |
183 | |
184 | do_cleanups (back_to); | |
efc0eabd | 185 | return NULL; |
07e059b5 VP |
186 | } |
187 | #endif | |
188 | ||
e0665bc8 PA |
189 | static void |
190 | osdata_item_clear (struct osdata_item *item) | |
191 | { | |
192 | if (item->columns != NULL) | |
193 | { | |
194 | struct osdata_column *col; | |
195 | int ix; | |
5cc80db3 | 196 | |
e0665bc8 PA |
197 | for (ix = 0; |
198 | VEC_iterate (osdata_column_s, item->columns, | |
199 | ix, col); | |
200 | ix++) | |
201 | { | |
202 | xfree (col->name); | |
203 | xfree (col->value); | |
204 | } | |
205 | VEC_free (osdata_column_s, item->columns); | |
206 | item->columns = NULL; | |
207 | } | |
208 | } | |
209 | ||
07e059b5 VP |
210 | void |
211 | osdata_free (struct osdata *osdata) | |
212 | { | |
213 | if (osdata == NULL) | |
214 | return; | |
215 | ||
216 | if (osdata->items != NULL) | |
217 | { | |
218 | struct osdata_item *item; | |
219 | int ix; | |
5cc80db3 | 220 | |
07e059b5 VP |
221 | for (ix = 0; |
222 | VEC_iterate (osdata_item_s, osdata->items, | |
223 | ix, item); | |
224 | ix++) | |
225 | osdata_item_clear (item); | |
226 | VEC_free (osdata_item_s, osdata->items); | |
227 | } | |
228 | ||
229 | xfree (osdata); | |
230 | } | |
231 | ||
e0665bc8 PA |
232 | static void |
233 | osdata_free_cleanup (void *arg) | |
234 | { | |
235 | struct osdata *osdata = arg; | |
5cc80db3 | 236 | |
e0665bc8 PA |
237 | osdata_free (osdata); |
238 | } | |
239 | ||
240 | struct cleanup * | |
241 | make_cleanup_osdata_free (struct osdata *data) | |
242 | { | |
243 | return make_cleanup (osdata_free_cleanup, data); | |
244 | } | |
245 | ||
246 | struct osdata * | |
247 | get_osdata (const char *type) | |
07e059b5 | 248 | { |
e0665bc8 | 249 | struct osdata *osdata = NULL; |
07e059b5 | 250 | char *xml = target_get_osdata (type); |
5cc80db3 | 251 | |
07e059b5 VP |
252 | if (xml) |
253 | { | |
e0665bc8 PA |
254 | struct cleanup *old_chain = make_cleanup (xfree, xml); |
255 | ||
07e059b5 | 256 | if (xml[0] == '\0') |
a61408f8 SS |
257 | { |
258 | if (type) | |
259 | warning (_("Empty data returned by target. Wrong osdata type?")); | |
260 | else | |
261 | warning (_("Empty type list returned by target. No type data?")); | |
262 | } | |
e0665bc8 PA |
263 | else |
264 | osdata = osdata_parse (xml); | |
265 | ||
266 | do_cleanups (old_chain); | |
07e059b5 | 267 | } |
e0665bc8 | 268 | |
07e059b5 | 269 | if (!osdata) |
b37520b6 | 270 | error (_("Can not fetch data now.")); |
07e059b5 VP |
271 | |
272 | return osdata; | |
273 | } | |
274 | ||
275 | const char * | |
276 | get_osdata_column (struct osdata_item *item, const char *name) | |
277 | { | |
278 | struct osdata_column *col; | |
279 | int ix_cols; | |
280 | ||
281 | for (ix_cols = 0; | |
282 | VEC_iterate (osdata_column_s, item->columns, | |
283 | ix_cols, col); | |
284 | ix_cols++) | |
285 | if (strcmp (col->name, name) == 0) | |
286 | return col->value; | |
287 | ||
288 | return NULL; | |
289 | } | |
290 | ||
f3e0e960 | 291 | void |
07e059b5 VP |
292 | info_osdata_command (char *type, int from_tty) |
293 | { | |
79a45e25 | 294 | struct ui_out *uiout = current_uiout; |
e0665bc8 | 295 | struct osdata *osdata = NULL; |
8443c207 | 296 | struct osdata_item *last = NULL; |
e0665bc8 | 297 | struct cleanup *old_chain; |
8443c207 KY |
298 | int ncols = 0; |
299 | int nrows; | |
71caed83 | 300 | int col_to_skip = -1; |
07e059b5 | 301 | |
07e059b5 | 302 | osdata = get_osdata (type); |
e0665bc8 | 303 | old_chain = make_cleanup_osdata_free (osdata); |
07e059b5 | 304 | |
8443c207 | 305 | nrows = VEC_length (osdata_item_s, osdata->items); |
07e059b5 | 306 | |
8443c207 | 307 | if (!type && nrows == 0) |
a61408f8 | 308 | error (_("Available types of OS data not reported.")); |
8443c207 KY |
309 | |
310 | if (!VEC_empty (osdata_item_s, osdata->items)) | |
311 | { | |
312 | last = VEC_last (osdata_item_s, osdata->items); | |
313 | if (last->columns) | |
314 | ncols = VEC_length (osdata_column_s, last->columns); | |
71caed83 SS |
315 | |
316 | /* As a special case, scan the listing of available data types | |
317 | for a column named "Title", and only include it with MI | |
318 | output; this column's normal use is for titles for interface | |
319 | elements like menus, and it clutters up CLI output. */ | |
320 | if (!type && !ui_out_is_mi_like_p (uiout)) | |
321 | { | |
322 | struct osdata_column *col; | |
323 | int ix; | |
324 | ||
325 | for (ix = 0; | |
326 | VEC_iterate (osdata_column_s, last->columns, ix, col); | |
327 | ix++) | |
328 | { | |
329 | if (strcmp (col->name, "Title") == 0) | |
330 | col_to_skip = ix; | |
331 | } | |
332 | /* Be sure to reduce the total column count, otherwise | |
333 | internal errors ensue. */ | |
334 | if (col_to_skip >= 0) | |
335 | --ncols; | |
336 | } | |
8443c207 | 337 | } |
a61408f8 | 338 | |
8443c207 | 339 | make_cleanup_ui_out_table_begin_end (uiout, ncols, nrows, |
e0665bc8 | 340 | "OSDataTable"); |
07e059b5 | 341 | |
8443c207 KY |
342 | /* With no columns/items, we just output an empty table, but we |
343 | still output the table. This matters for MI. */ | |
344 | if (ncols == 0) | |
345 | { | |
346 | do_cleanups (old_chain); | |
347 | return; | |
348 | } | |
349 | ||
07e059b5 VP |
350 | if (last && last->columns) |
351 | { | |
352 | struct osdata_column *col; | |
353 | int ix; | |
5cc80db3 | 354 | |
07e059b5 VP |
355 | for (ix = 0; |
356 | VEC_iterate (osdata_column_s, last->columns, | |
357 | ix, col); | |
358 | ix++) | |
8443c207 KY |
359 | { |
360 | char col_name[32]; | |
71caed83 SS |
361 | |
362 | if (ix == col_to_skip) | |
363 | continue; | |
364 | ||
8443c207 KY |
365 | snprintf (col_name, 32, "col%d", ix); |
366 | ui_out_table_header (uiout, 10, ui_left, | |
367 | col_name, col->name); | |
368 | } | |
07e059b5 VP |
369 | } |
370 | ||
371 | ui_out_table_body (uiout); | |
372 | ||
8443c207 | 373 | if (nrows != 0) |
07e059b5 VP |
374 | { |
375 | struct osdata_item *item; | |
376 | int ix_items; | |
5cc80db3 | 377 | |
07e059b5 VP |
378 | for (ix_items = 0; |
379 | VEC_iterate (osdata_item_s, osdata->items, | |
380 | ix_items, item); | |
381 | ix_items++) | |
382 | { | |
e0665bc8 | 383 | struct cleanup *old_chain; |
07e059b5 VP |
384 | int ix_cols; |
385 | struct osdata_column *col; | |
386 | ||
f99d8bf4 | 387 | old_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "item"); |
07e059b5 VP |
388 | |
389 | for (ix_cols = 0; | |
390 | VEC_iterate (osdata_column_s, item->columns, | |
391 | ix_cols, col); | |
392 | ix_cols++) | |
8443c207 KY |
393 | { |
394 | char col_name[32]; | |
71caed83 SS |
395 | |
396 | if (ix_cols == col_to_skip) | |
397 | continue; | |
398 | ||
8443c207 KY |
399 | snprintf (col_name, 32, "col%d", ix_cols); |
400 | ui_out_field_string (uiout, col_name, col->value); | |
401 | } | |
402 | ||
07e059b5 VP |
403 | do_cleanups (old_chain); |
404 | ||
405 | ui_out_text (uiout, "\n"); | |
406 | } | |
407 | } | |
408 | ||
e0665bc8 | 409 | do_cleanups (old_chain); |
07e059b5 VP |
410 | } |
411 | ||
412 | extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */ | |
413 | ||
414 | void | |
415 | _initialize_osdata (void) | |
416 | { | |
417 | add_info ("os", info_osdata_command, | |
418 | _("Show OS data ARG.")); | |
07e059b5 | 419 | } |