Commit | Line | Data |
---|---|---|
07e059b5 VP |
1 | /* Routines for handling XML generic OS data provided by target. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2008-2014 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" | |
07e059b5 VP |
25 | #include "ui-out.h" |
26 | #include "gdbcmd.h" | |
27 | ||
28 | #if !defined(HAVE_LIBEXPAT) | |
29 | ||
30 | struct osdata * | |
31 | osdata_parse (const char *xml) | |
32 | { | |
33 | static int have_warned; | |
34 | ||
35 | if (!have_warned) | |
36 | { | |
37 | have_warned = 1; | |
38 | warning (_("Can not parse XML OS data; XML support was disabled " | |
39 | "at compile time")); | |
40 | } | |
41 | ||
42 | return NULL; | |
43 | } | |
44 | ||
45 | #else /* HAVE_LIBEXPAT */ | |
46 | ||
07e059b5 VP |
47 | /* Internal parsing data passed to all XML callbacks. */ |
48 | struct osdata_parsing_data | |
49 | { | |
50 | struct osdata *osdata; | |
51 | char *property_name; | |
52 | }; | |
53 | ||
07e059b5 VP |
54 | /* Handle the start of a <osdata> element. */ |
55 | ||
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) | |
60 | { | |
61 | struct osdata_parsing_data *data = user_data; | |
62 | char *type; | |
63 | struct osdata *osdata; | |
64 | ||
65 | if (data->osdata) | |
66 | gdb_xml_error (parser, _("Seen more than on osdata element")); | |
67 | ||
3d2c1d41 | 68 | type = xml_find_attribute (attributes, "type")->value; |
41bf6aca | 69 | osdata = XCNEW (struct osdata); |
07e059b5 VP |
70 | osdata->type = xstrdup (type); |
71 | data->osdata = osdata; | |
72 | } | |
73 | ||
74 | /* Handle the start of a <item> element. */ | |
75 | ||
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) | |
80 | { | |
81 | struct osdata_parsing_data *data = user_data; | |
82 | struct osdata_item item = { NULL }; | |
5cc80db3 | 83 | |
07e059b5 VP |
84 | VEC_safe_push (osdata_item_s, data->osdata->items, &item); |
85 | } | |
86 | ||
87 | /* Handle the start of a <column> element. */ | |
88 | ||
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) | |
93 | { | |
94 | struct osdata_parsing_data *data = user_data; | |
3d2c1d41 | 95 | const char *name = xml_find_attribute (attributes, "name")->value; |
5cc80db3 | 96 | |
07e059b5 VP |
97 | data->property_name = xstrdup (name); |
98 | } | |
99 | ||
100 | /* Handle the end of a <column> element. */ | |
101 | ||
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) | |
106 | { | |
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); | |
112 | ||
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; | |
117 | } | |
118 | ||
119 | /* Discard the constructed osdata (if an error occurs). */ | |
120 | ||
121 | static void | |
122 | clear_parsing_data (void *p) | |
123 | { | |
124 | struct osdata_parsing_data *data = p; | |
5cc80db3 | 125 | |
07e059b5 VP |
126 | osdata_free (data->osdata); |
127 | data->osdata = NULL; | |
128 | xfree (data->property_name); | |
129 | data->property_name = NULL; | |
130 | } | |
131 | ||
132 | /* The allowed elements and attributes for OS data object. | |
133 | The root element is a <osdata>. */ | |
134 | ||
135 | const struct gdb_xml_attribute column_attributes[] = { | |
136 | { "name", GDB_XML_AF_NONE, NULL, NULL }, | |
137 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
138 | }; | |
139 | ||
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 } | |
145 | }; | |
146 | ||
147 | const struct gdb_xml_attribute osdata_attributes[] = { | |
148 | { "type", GDB_XML_AF_NONE, NULL, NULL }, | |
149 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
150 | }; | |
151 | ||
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 } | |
157 | }; | |
158 | ||
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 } | |
163 | }; | |
164 | ||
165 | struct osdata * | |
166 | osdata_parse (const char *xml) | |
167 | { | |
efc0eabd | 168 | struct cleanup *back_to; |
07e059b5 VP |
169 | struct osdata_parsing_data data = { NULL }; |
170 | ||
efc0eabd | 171 | back_to = make_cleanup (clear_parsing_data, &data); |
07e059b5 | 172 | |
efc0eabd PA |
173 | if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd", |
174 | osdata_elements, xml, &data) == 0) | |
175 | { | |
176 | /* Parsed successfully, don't need to delete the result. */ | |
177 | discard_cleanups (back_to); | |
178 | return data.osdata; | |
179 | } | |
07e059b5 VP |
180 | |
181 | do_cleanups (back_to); | |
efc0eabd | 182 | return NULL; |
07e059b5 VP |
183 | } |
184 | #endif | |
185 | ||
e0665bc8 PA |
186 | static void |
187 | osdata_item_clear (struct osdata_item *item) | |
188 | { | |
189 | if (item->columns != NULL) | |
190 | { | |
191 | struct osdata_column *col; | |
192 | int ix; | |
5cc80db3 | 193 | |
e0665bc8 PA |
194 | for (ix = 0; |
195 | VEC_iterate (osdata_column_s, item->columns, | |
196 | ix, col); | |
197 | ix++) | |
198 | { | |
199 | xfree (col->name); | |
200 | xfree (col->value); | |
201 | } | |
202 | VEC_free (osdata_column_s, item->columns); | |
203 | item->columns = NULL; | |
204 | } | |
205 | } | |
206 | ||
07e059b5 VP |
207 | void |
208 | osdata_free (struct osdata *osdata) | |
209 | { | |
210 | if (osdata == NULL) | |
211 | return; | |
212 | ||
213 | if (osdata->items != NULL) | |
214 | { | |
215 | struct osdata_item *item; | |
216 | int ix; | |
5cc80db3 | 217 | |
07e059b5 VP |
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); | |
224 | } | |
225 | ||
226 | xfree (osdata); | |
227 | } | |
228 | ||
e0665bc8 PA |
229 | static void |
230 | osdata_free_cleanup (void *arg) | |
231 | { | |
232 | struct osdata *osdata = arg; | |
5cc80db3 | 233 | |
e0665bc8 PA |
234 | osdata_free (osdata); |
235 | } | |
236 | ||
237 | struct cleanup * | |
238 | make_cleanup_osdata_free (struct osdata *data) | |
239 | { | |
240 | return make_cleanup (osdata_free_cleanup, data); | |
241 | } | |
242 | ||
243 | struct osdata * | |
244 | get_osdata (const char *type) | |
07e059b5 | 245 | { |
e0665bc8 | 246 | struct osdata *osdata = NULL; |
07e059b5 | 247 | char *xml = target_get_osdata (type); |
5cc80db3 | 248 | |
07e059b5 VP |
249 | if (xml) |
250 | { | |
e0665bc8 PA |
251 | struct cleanup *old_chain = make_cleanup (xfree, xml); |
252 | ||
07e059b5 | 253 | if (xml[0] == '\0') |
a61408f8 SS |
254 | { |
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?")); | |
259 | } | |
e0665bc8 PA |
260 | else |
261 | osdata = osdata_parse (xml); | |
262 | ||
263 | do_cleanups (old_chain); | |
07e059b5 | 264 | } |
e0665bc8 | 265 | |
07e059b5 | 266 | if (!osdata) |
b37520b6 | 267 | error (_("Can not fetch data now.")); |
07e059b5 VP |
268 | |
269 | return osdata; | |
270 | } | |
271 | ||
272 | const char * | |
273 | get_osdata_column (struct osdata_item *item, const char *name) | |
274 | { | |
275 | struct osdata_column *col; | |
276 | int ix_cols; | |
277 | ||
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; | |
284 | ||
285 | return NULL; | |
286 | } | |
287 | ||
f3e0e960 | 288 | void |
07e059b5 VP |
289 | info_osdata_command (char *type, int from_tty) |
290 | { | |
79a45e25 | 291 | struct ui_out *uiout = current_uiout; |
e0665bc8 | 292 | struct osdata *osdata = NULL; |
8443c207 | 293 | struct osdata_item *last = NULL; |
e0665bc8 | 294 | struct cleanup *old_chain; |
8443c207 KY |
295 | int ncols = 0; |
296 | int nrows; | |
71caed83 | 297 | int col_to_skip = -1; |
07e059b5 | 298 | |
07e059b5 | 299 | osdata = get_osdata (type); |
e0665bc8 | 300 | old_chain = make_cleanup_osdata_free (osdata); |
07e059b5 | 301 | |
8443c207 | 302 | nrows = VEC_length (osdata_item_s, osdata->items); |
07e059b5 | 303 | |
8443c207 | 304 | if (!type && nrows == 0) |
a61408f8 | 305 | error (_("Available types of OS data not reported.")); |
8443c207 KY |
306 | |
307 | if (!VEC_empty (osdata_item_s, osdata->items)) | |
308 | { | |
309 | last = VEC_last (osdata_item_s, osdata->items); | |
310 | if (last->columns) | |
311 | ncols = VEC_length (osdata_column_s, last->columns); | |
71caed83 SS |
312 | |
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)) | |
318 | { | |
319 | struct osdata_column *col; | |
320 | int ix; | |
321 | ||
322 | for (ix = 0; | |
323 | VEC_iterate (osdata_column_s, last->columns, ix, col); | |
324 | ix++) | |
325 | { | |
326 | if (strcmp (col->name, "Title") == 0) | |
327 | col_to_skip = ix; | |
328 | } | |
329 | /* Be sure to reduce the total column count, otherwise | |
330 | internal errors ensue. */ | |
331 | if (col_to_skip >= 0) | |
332 | --ncols; | |
333 | } | |
8443c207 | 334 | } |
a61408f8 | 335 | |
8443c207 | 336 | make_cleanup_ui_out_table_begin_end (uiout, ncols, nrows, |
e0665bc8 | 337 | "OSDataTable"); |
07e059b5 | 338 | |
8443c207 KY |
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) | |
342 | { | |
343 | do_cleanups (old_chain); | |
344 | return; | |
345 | } | |
346 | ||
07e059b5 VP |
347 | if (last && last->columns) |
348 | { | |
349 | struct osdata_column *col; | |
350 | int ix; | |
5cc80db3 | 351 | |
07e059b5 VP |
352 | for (ix = 0; |
353 | VEC_iterate (osdata_column_s, last->columns, | |
354 | ix, col); | |
355 | ix++) | |
8443c207 KY |
356 | { |
357 | char col_name[32]; | |
71caed83 SS |
358 | |
359 | if (ix == col_to_skip) | |
360 | continue; | |
361 | ||
8443c207 KY |
362 | snprintf (col_name, 32, "col%d", ix); |
363 | ui_out_table_header (uiout, 10, ui_left, | |
364 | col_name, col->name); | |
365 | } | |
07e059b5 VP |
366 | } |
367 | ||
368 | ui_out_table_body (uiout); | |
369 | ||
8443c207 | 370 | if (nrows != 0) |
07e059b5 VP |
371 | { |
372 | struct osdata_item *item; | |
373 | int ix_items; | |
5cc80db3 | 374 | |
07e059b5 VP |
375 | for (ix_items = 0; |
376 | VEC_iterate (osdata_item_s, osdata->items, | |
377 | ix_items, item); | |
378 | ix_items++) | |
379 | { | |
e0665bc8 | 380 | struct cleanup *old_chain; |
07e059b5 VP |
381 | int ix_cols; |
382 | struct osdata_column *col; | |
383 | ||
f99d8bf4 | 384 | old_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "item"); |
07e059b5 VP |
385 | |
386 | for (ix_cols = 0; | |
387 | VEC_iterate (osdata_column_s, item->columns, | |
388 | ix_cols, col); | |
389 | ix_cols++) | |
8443c207 KY |
390 | { |
391 | char col_name[32]; | |
71caed83 SS |
392 | |
393 | if (ix_cols == col_to_skip) | |
394 | continue; | |
395 | ||
8443c207 KY |
396 | snprintf (col_name, 32, "col%d", ix_cols); |
397 | ui_out_field_string (uiout, col_name, col->value); | |
398 | } | |
399 | ||
07e059b5 VP |
400 | do_cleanups (old_chain); |
401 | ||
402 | ui_out_text (uiout, "\n"); | |
403 | } | |
404 | } | |
405 | ||
e0665bc8 | 406 | do_cleanups (old_chain); |
07e059b5 VP |
407 | } |
408 | ||
409 | extern initialize_file_ftype _initialize_osdata; /* -Wmissing-prototypes */ | |
410 | ||
411 | void | |
412 | _initialize_osdata (void) | |
413 | { | |
414 | add_info ("os", info_osdata_command, | |
415 | _("Show OS data ARG.")); | |
07e059b5 | 416 | } |