1 /* MI Command Set - output generating routines.
2 
3    Copyright (C) 2000-2024 Free Software Foundation, Inc.
4 
5    Contributed by Cygnus Solutions (a Red Hat company).
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 #include "mi-out.h"
23 
24 #include <vector>
25 
26 #include "interps.h"
27 #include "ui-out.h"
28 #include "utils.h"
29 #include "gdbsupport/gdb-checked-static-cast.h"
30 
31 /* Mark beginning of a table.  */
32 
33 void
do_table_begin(int nr_cols,int nr_rows,const char * tblid)34 mi_ui_out::do_table_begin (int nr_cols, int nr_rows,
35                                  const char *tblid)
36 {
37   open (tblid, ui_out_type_tuple);
38   do_field_signed (-1, -1, ui_left, "nr_rows", nr_rows);
39   do_field_signed (-1, -1, ui_left, "nr_cols", nr_cols);
40   open ("hdr", ui_out_type_list);
41 }
42 
43 /* Mark beginning of a table body.  */
44 
45 void
do_table_body()46 mi_ui_out::do_table_body ()
47 {
48   /* close the table header line if there were any headers */
49   close (ui_out_type_list);
50   open ("body", ui_out_type_list);
51 }
52 
53 /* Mark end of a table.  */
54 
55 void
do_table_end()56 mi_ui_out::do_table_end ()
57 {
58   close (ui_out_type_list); /* body */
59   close (ui_out_type_tuple);
60 }
61 
62 /* Specify table header.  */
63 
64 void
do_table_header(int width,ui_align alignment,const std::string & col_name,const std::string & col_hdr)65 mi_ui_out::do_table_header (int width, ui_align alignment,
66                                   const std::string &col_name,
67                                   const std::string &col_hdr)
68 {
69   open (NULL, ui_out_type_tuple);
70   do_field_signed (0, 0, ui_center, "width", width);
71   do_field_signed (0, 0, ui_center, "alignment", alignment);
72   do_field_string (0, 0, ui_center, "col_name", col_name.c_str (),
73                        ui_file_style ());
74   do_field_string (0, width, alignment, "colhdr", col_hdr.c_str (),
75                        ui_file_style ());
76   close (ui_out_type_tuple);
77 }
78 
79 /* Mark beginning of a list.  */
80 
81 void
do_begin(ui_out_type type,const char * id)82 mi_ui_out::do_begin (ui_out_type type, const char *id)
83 {
84   open (id, type);
85 }
86 
87 /* Mark end of a list.  */
88 
89 void
do_end(ui_out_type type)90 mi_ui_out::do_end (ui_out_type type)
91 {
92   close (type);
93 }
94 
95 /* Output an int field.  */
96 
97 void
do_field_signed(int fldno,int width,ui_align alignment,const char * fldname,LONGEST value)98 mi_ui_out::do_field_signed (int fldno, int width, ui_align alignment,
99                                   const char *fldname, LONGEST value)
100 {
101   do_field_string (fldno, width, alignment, fldname, plongest (value),
102                        ui_file_style ());
103 }
104 
105 /* Output an unsigned field.  */
106 
107 void
do_field_unsigned(int fldno,int width,ui_align alignment,const char * fldname,ULONGEST value)108 mi_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
109                                     const char *fldname, ULONGEST value)
110 {
111   do_field_string (fldno, width, alignment, fldname, pulongest (value),
112                        ui_file_style ());
113 }
114 
115 /* Used to omit a field.  */
116 
117 void
do_field_skip(int fldno,int width,ui_align alignment,const char * fldname)118 mi_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
119                                 const char *fldname)
120 {
121 }
122 
123 /* Other specific mi_field_* end up here so alignment and field
124    separators are both handled by mi_field_string. */
125 
126 void
do_field_string(int fldno,int width,ui_align align,const char * fldname,const char * string,const ui_file_style & style)127 mi_ui_out::do_field_string (int fldno, int width, ui_align align,
128                                   const char *fldname, const char *string,
129                                   const ui_file_style &style)
130 {
131   ui_file *stream = m_streams.back ();
132   field_separator ();
133 
134   if (fldname)
135     gdb_printf (stream, "%s=", fldname);
136   gdb_printf (stream, "\"");
137   if (string)
138     stream->putstr (string, '"');
139   gdb_printf (stream, "\"");
140 }
141 
142 void
do_field_fmt(int fldno,int width,ui_align align,const char * fldname,const ui_file_style & style,const char * format,va_list args)143 mi_ui_out::do_field_fmt (int fldno, int width, ui_align align,
144                                const char *fldname, const ui_file_style &style,
145                                const char *format, va_list args)
146 {
147   ui_file *stream = m_streams.back ();
148   field_separator ();
149 
150   if (fldname)
151     gdb_printf (stream, "%s=\"", fldname);
152   else
153     gdb_puts ("\"", stream);
154   gdb_vprintf (stream, format, args);
155   gdb_puts ("\"", stream);
156 }
157 
158 void
do_spaces(int numspaces)159 mi_ui_out::do_spaces (int numspaces)
160 {
161 }
162 
163 void
do_text(const char * string)164 mi_ui_out::do_text (const char *string)
165 {
166 }
167 
168 void
do_message(const ui_file_style & style,const char * format,va_list args)169 mi_ui_out::do_message (const ui_file_style &style,
170                            const char *format, va_list args)
171 {
172 }
173 
174 void
do_wrap_hint(int indent)175 mi_ui_out::do_wrap_hint (int indent)
176 {
177   m_streams.back ()->wrap_here (indent);
178 }
179 
180 void
do_flush()181 mi_ui_out::do_flush ()
182 {
183 
184   gdb_flush (m_streams.back ());
185 }
186 
187 void
do_redirect(ui_file * outstream)188 mi_ui_out::do_redirect (ui_file *outstream)
189 {
190   if (outstream != NULL)
191     m_streams.push_back (outstream);
192   else
193     m_streams.pop_back ();
194 }
195 
196 void
field_separator()197 mi_ui_out::field_separator ()
198 {
199   if (m_suppress_field_separator)
200     m_suppress_field_separator = false;
201   else
202     gdb_putc (',', m_streams.back ());
203 }
204 
205 void
open(const char * name,ui_out_type type)206 mi_ui_out::open (const char *name, ui_out_type type)
207 {
208   ui_file *stream = m_streams.back ();
209 
210   field_separator ();
211   m_suppress_field_separator = true;
212 
213   if (name)
214     gdb_printf (stream, "%s=", name);
215 
216   switch (type)
217     {
218     case ui_out_type_tuple:
219       gdb_putc ('{', stream);
220       break;
221 
222     case ui_out_type_list:
223       gdb_putc ('[', stream);
224       break;
225 
226     default:
227       internal_error (_("bad switch"));
228     }
229 }
230 
231 void
close(ui_out_type type)232 mi_ui_out::close (ui_out_type type)
233 {
234   ui_file *stream = m_streams.back ();
235 
236   switch (type)
237     {
238     case ui_out_type_tuple:
239       gdb_putc ('}', stream);
240       break;
241 
242     case ui_out_type_list:
243       gdb_putc (']', stream);
244       break;
245 
246     default:
247       internal_error (_("bad switch"));
248     }
249 
250   m_suppress_field_separator = false;
251 }
252 
253 string_file *
main_stream()254 mi_ui_out::main_stream ()
255 {
256   gdb_assert (m_streams.size () == 1);
257 
258   return (string_file *) m_streams.back ();
259 }
260 
261 /* Initialize a progress update to be displayed with
262    mi_ui_out::do_progress_notify.  */
263 
264 void
do_progress_start()265 mi_ui_out::do_progress_start ()
266 {
267   m_progress_info.emplace_back ();
268 }
269 
270 /* Indicate that a task described by MSG is in progress.  */
271 
272 void
do_progress_notify(const std::string & msg,const char * unit,double cur,double total)273 mi_ui_out::do_progress_notify (const std::string &msg, const char *unit,
274                                      double cur, double total)
275 {
276   mi_progress_info &info (m_progress_info.back ());
277 
278   if (info.state == progress_update::START)
279     {
280       gdb_printf ("%s...\n", msg.c_str ());
281       info.state = progress_update::WORKING;
282     }
283 }
284 
285 /* Remove the most recent progress update from the progress_info stack.  */
286 
287 void
do_progress_end()288 mi_ui_out::do_progress_end ()
289 {
290   m_progress_info.pop_back ();
291 }
292 
293 /* Clear the buffer.  */
294 
295 void
rewind()296 mi_ui_out::rewind ()
297 {
298   main_stream ()->clear ();
299 }
300 
301 /* Dump the buffer onto the specified stream.  */
302 
303 void
put(ui_file * where)304 mi_ui_out::put (ui_file *where)
305 {
306   string_file *mi_stream = main_stream ();
307 
308   where->write (mi_stream->data (), mi_stream->size ());
309   mi_stream->clear ();
310 }
311 
312 /* Return the current MI version.  */
313 
314 int
version()315 mi_ui_out::version ()
316 {
317   return m_mi_version;
318 }
319 
320 /* Constructor for an `mi_out_data' object.  */
321 
mi_ui_out(int mi_version)322 mi_ui_out::mi_ui_out (int mi_version)
323 : ui_out (make_flags (mi_version)),
324   m_suppress_field_separator (false),
325   m_suppress_output (false),
326   m_mi_version (mi_version)
327 {
328   string_file *stream = new string_file ();
329   m_streams.push_back (stream);
330 }
331 
~mi_ui_out()332 mi_ui_out::~mi_ui_out ()
333 {
334 }
335 
336 /* See mi/mi-out.h.  */
337 
338 std::unique_ptr<mi_ui_out>
mi_out_new(const char * mi_version)339 mi_out_new (const char *mi_version)
340 {
341   if (streq (mi_version, INTERP_MI4) ||  streq (mi_version, INTERP_MI))
342     return std::make_unique<mi_ui_out> (4);
343 
344   if (streq (mi_version, INTERP_MI3))
345     return std::make_unique<mi_ui_out> (3);
346 
347   if (streq (mi_version, INTERP_MI2))
348     return std::make_unique<mi_ui_out> (2);
349 
350   return nullptr;
351 }
352 
353 /* Helper function to return the given UIOUT as an mi_ui_out.  It is an error
354    to call this function with an ui_out that is not an MI.  */
355 
356 static mi_ui_out *
as_mi_ui_out(ui_out * uiout)357 as_mi_ui_out (ui_out *uiout)
358 {
359   return gdb::checked_static_cast<mi_ui_out *> (uiout);
360 }
361 
362 void
mi_out_put(ui_out * uiout,struct ui_file * stream)363 mi_out_put (ui_out *uiout, struct ui_file *stream)
364 {
365   return as_mi_ui_out (uiout)->put (stream);
366 }
367 
368 void
mi_out_rewind(ui_out * uiout)369 mi_out_rewind (ui_out *uiout)
370 {
371   return as_mi_ui_out (uiout)->rewind ();
372 }
373