1/* Routines for handling XML generic OS data provided by target.
2
3   Copyright (C) 2008-2020 Free Software Foundation, Inc.
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 "xml-support.h"
23#include "osdata.h"
24#include "ui-out.h"
25#include "gdbcmd.h"
26
27#if !defined(HAVE_LIBEXPAT)
28
29std::unique_ptr<osdata>
30osdata_parse (const char *xml)
31{
32  static int have_warned;
33
34  if (!have_warned)
35    {
36      have_warned = 1;
37      warning (_("Can not parse XML OS data; XML support was disabled "
38                "at compile time"));
39    }
40
41  return NULL;
42}
43
44#else /* HAVE_LIBEXPAT */
45
46/* Internal parsing data passed to all XML callbacks.  */
47struct osdata_parsing_data
48{
49  std::unique_ptr<struct osdata> osdata;
50  std::string property_name;
51};
52
53/* Handle the start of a <osdata> element.  */
54
55static void
56osdata_start_osdata (struct gdb_xml_parser *parser,
57                        const struct gdb_xml_element *element,
58                        void *user_data,
59			std::vector<gdb_xml_value> &attributes)
60{
61  struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
62
63  if (data->osdata != NULL)
64    gdb_xml_error (parser, _("Seen more than on osdata element"));
65
66  char *type = (char *) xml_find_attribute (attributes, "type")->value.get ();
67  data->osdata.reset (new struct osdata (std::string (type)));
68}
69
70/* Handle the start of a <item> element.  */
71
72static void
73osdata_start_item (struct gdb_xml_parser *parser,
74                  const struct gdb_xml_element *element,
75                  void *user_data,
76		  std::vector<gdb_xml_value> &attributes)
77{
78  struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
79  data->osdata->items.emplace_back ();
80}
81
82/* Handle the start of a <column> element.  */
83
84static void
85osdata_start_column (struct gdb_xml_parser *parser,
86                    const struct gdb_xml_element *element,
87                    void *user_data,
88		    std::vector<gdb_xml_value> &attributes)
89{
90  struct osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
91  const char *name
92    = (const char *) xml_find_attribute (attributes, "name")->value.get ();
93
94  data->property_name.assign (name);
95}
96
97/* Handle the end of a <column> element.  */
98
99static void
100osdata_end_column (struct gdb_xml_parser *parser,
101                  const struct gdb_xml_element *element,
102                  void *user_data, const char *body_text)
103{
104  osdata_parsing_data *data = (struct osdata_parsing_data *) user_data;
105  struct osdata *osdata = data->osdata.get ();
106  osdata_item &item = osdata->items.back ();
107
108  item.columns.emplace_back (std::move (data->property_name),
109			     std::string (body_text));
110}
111
112/* The allowed elements and attributes for OS data object.
113   The root element is a <osdata>.  */
114
115const struct gdb_xml_attribute column_attributes[] = {
116  { "name", GDB_XML_AF_NONE, NULL, NULL },
117  { NULL, GDB_XML_AF_NONE, NULL, NULL }
118};
119
120const struct gdb_xml_element item_children[] = {
121  { "column", column_attributes, NULL,
122    GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
123    osdata_start_column, osdata_end_column },
124  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
125};
126
127const struct gdb_xml_attribute osdata_attributes[] = {
128  { "type", GDB_XML_AF_NONE, NULL, NULL },
129  { NULL, GDB_XML_AF_NONE, NULL, NULL }
130};
131
132const struct gdb_xml_element osdata_children[] = {
133  { "item", NULL, item_children,
134    GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
135    osdata_start_item, NULL },
136  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
137};
138
139const struct gdb_xml_element osdata_elements[] = {
140  { "osdata", osdata_attributes, osdata_children,
141    GDB_XML_EF_NONE, osdata_start_osdata, NULL },
142  { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
143};
144
145std::unique_ptr<osdata>
146osdata_parse (const char *xml)
147{
148  osdata_parsing_data data;
149
150  if (gdb_xml_parse_quick (_("osdata"), "osdata.dtd",
151			   osdata_elements, xml, &data) == 0)
152    {
153      /* Parsed successfully, don't need to delete the result.  */
154      return std::move (data.osdata);
155    }
156
157  return NULL;
158}
159#endif
160
161std::unique_ptr<osdata>
162get_osdata (const char *type)
163{
164  std::unique_ptr<osdata> osdata;
165  gdb::optional<gdb::char_vector> xml = target_get_osdata (type);
166
167  if (xml)
168    {
169      if ((*xml)[0] == '\0')
170	{
171	  if (type)
172	    warning (_("Empty data returned by target.  Wrong osdata type?"));
173	  else
174	    warning (_("Empty type list returned by target.  No type data?"));
175	}
176      else
177	osdata = osdata_parse (xml->data ());
178    }
179
180  if (osdata == NULL)
181    error (_("Can not fetch data now."));
182
183  return osdata;
184}
185
186const std::string *
187get_osdata_column (const osdata_item &item, const char *name)
188{
189  for (const osdata_column &col : item.columns)
190    if (col.name == name)
191      return &col.value;
192
193  return NULL;
194}
195
196void
197info_osdata (const char *type)
198{
199  struct ui_out *uiout = current_uiout;
200  struct osdata_item *last = NULL;
201  int ncols = 0;
202  int col_to_skip = -1;
203
204  if (type == NULL)
205    type = "";
206
207  std::unique_ptr<osdata> osdata = get_osdata (type);
208
209  int nrows = osdata->items.size ();
210
211  if (*type == '\0' && nrows == 0)
212    error (_("Available types of OS data not reported."));
213
214  if (!osdata->items.empty ())
215    {
216      last = &osdata->items.back ();
217      ncols = last->columns.size ();
218
219      /* As a special case, scan the listing of available data types
220	 for a column named "Title", and only include it with MI
221	 output; this column's normal use is for titles for interface
222	 elements like menus, and it clutters up CLI output.  */
223      if (*type == '\0' && !uiout->is_mi_like_p ())
224	{
225	  for (int ix = 0; ix < last->columns.size (); ix++)
226	    {
227	      if (last->columns[ix].name == "Title")
228		col_to_skip = ix;
229	    }
230	  /* Be sure to reduce the total column count, otherwise
231	     internal errors ensue.  */
232	  if (col_to_skip >= 0)
233	    --ncols;
234	}
235    }
236
237  ui_out_emit_table table_emitter (uiout, ncols, nrows, "OSDataTable");
238
239  /* With no columns/items, we just output an empty table, but we
240     still output the table.  This matters for MI.  */
241  if (ncols == 0)
242    return;
243
244  if (last != NULL && !last->columns.empty ())
245    {
246      for (int ix = 0; ix < last->columns.size (); ix++)
247	{
248	  char col_name[32];
249
250	  if (ix == col_to_skip)
251	    continue;
252
253	  snprintf (col_name, 32, "col%d", ix);
254	  uiout->table_header (10, ui_left,
255			       col_name, last->columns[ix].name.c_str ());
256        }
257    }
258
259  uiout->table_body ();
260
261  if (nrows != 0)
262    {
263      for (const osdata_item &item : osdata->items)
264       {
265	 {
266	   ui_out_emit_tuple tuple_emitter (uiout, "item");
267
268	   for (int ix_cols = 0; ix_cols < item.columns.size (); ix_cols++)
269	     {
270	       char col_name[32];
271
272	       if (ix_cols == col_to_skip)
273		 continue;
274
275	       snprintf (col_name, 32, "col%d", ix_cols);
276	       uiout->field_string (col_name,
277				    item.columns[ix_cols].value.c_str ());
278	     }
279	 }
280
281         uiout->text ("\n");
282       }
283    }
284}
285
286static void
287info_osdata_command (const char *arg, int from_tty)
288{
289  info_osdata (arg);
290}
291
292void _initialize_osdata ();
293void
294_initialize_osdata ()
295{
296  add_info ("os", info_osdata_command,
297           _("Show OS data ARG."));
298}
299