Introduce field_unsigned
[external/binutils.git] / gdb / cli-out.c
1 /* Output generating routines for GDB CLI.
2
3    Copyright (C) 1999-2019 Free Software Foundation, Inc.
4
5    Contributed by Cygnus Solutions.
6    Written by Fernando Nasser for Cygnus.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "cli-out.h"
26 #include "completer.h"
27 #include "readline/readline.h"
28 #include "cli/cli-style.h"
29
30 /* These are the CLI output functions */
31
32 /* Mark beginning of a table */
33
34 void
35 cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
36 {
37   if (nr_rows == 0)
38     m_suppress_output = true;
39   else
40     /* Only the table suppresses the output and, fortunately, a table
41        is not a recursive data structure.  */
42     gdb_assert (!m_suppress_output);
43 }
44
45 /* Mark beginning of a table body */
46
47 void
48 cli_ui_out::do_table_body ()
49 {
50   if (m_suppress_output)
51     return;
52
53   /* first, close the table header line */
54   text ("\n");
55 }
56
57 /* Mark end of a table */
58
59 void
60 cli_ui_out::do_table_end ()
61 {
62   m_suppress_output = false;
63 }
64
65 /* Specify table header */
66
67 void
68 cli_ui_out::do_table_header (int width, ui_align alignment,
69                              const std::string &col_name,
70                              const std::string &col_hdr)
71 {
72   if (m_suppress_output)
73     return;
74
75   do_field_string (0, width, alignment, 0, col_hdr.c_str (),
76                    ui_out_style_kind::DEFAULT);
77 }
78
79 /* Mark beginning of a list */
80
81 void
82 cli_ui_out::do_begin (ui_out_type type, const char *id)
83 {
84 }
85
86 /* Mark end of a list */
87
88 void
89 cli_ui_out::do_end (ui_out_type type)
90 {
91 }
92
93 /* output an int field */
94
95 void
96 cli_ui_out::do_field_int (int fldno, int width, ui_align alignment,
97                           const char *fldname, int value)
98 {
99   if (m_suppress_output)
100     return;
101
102   std::string str = string_printf ("%d", value);
103
104   do_field_string (fldno, width, alignment, fldname, str.c_str (),
105                    ui_out_style_kind::DEFAULT);
106 }
107
108 /* output an unsigned field */
109
110 void
111 cli_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
112                                const char *fldname, ULONGEST value)
113 {
114   if (m_suppress_output)
115     return;
116
117   do_field_string (fldno, width, alignment, fldname, pulongest (value),
118                    ui_out_style_kind::DEFAULT);
119 }
120
121 /* used to omit a field */
122
123 void
124 cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
125                            const char *fldname)
126 {
127   if (m_suppress_output)
128     return;
129
130   do_field_string (fldno, width, alignment, fldname, "",
131                    ui_out_style_kind::DEFAULT);
132 }
133
134 /* other specific cli_field_* end up here so alignment and field
135    separators are both handled by cli_field_string */
136
137 void
138 cli_ui_out::do_field_string (int fldno, int width, ui_align align,
139                              const char *fldname, const char *string,
140                              ui_out_style_kind style)
141 {
142   int before = 0;
143   int after = 0;
144
145   if (m_suppress_output)
146     return;
147
148   if ((align != ui_noalign) && string)
149     {
150       before = width - strlen (string);
151       if (before <= 0)
152         before = 0;
153       else
154         {
155           if (align == ui_right)
156             after = 0;
157           else if (align == ui_left)
158             {
159               after = before;
160               before = 0;
161             }
162           else
163             /* ui_center */
164             {
165               after = before / 2;
166               before -= after;
167             }
168         }
169     }
170
171   if (before)
172     spaces (before);
173
174   if (string)
175     {
176       ui_file_style fstyle;
177       switch (style)
178         {
179         case ui_out_style_kind::DEFAULT:
180           /* Nothing.  */
181           break;
182         case ui_out_style_kind::FILE:
183           /* Nothing.  */
184           fstyle = file_name_style.style ();
185           break;
186         case ui_out_style_kind::FUNCTION:
187           fstyle = function_name_style.style ();
188           break;
189         case ui_out_style_kind::VARIABLE:
190           fstyle = variable_name_style.style ();
191           break;
192         case ui_out_style_kind::ADDRESS:
193           fstyle = address_style.style ();
194           break;
195         default:
196           gdb_assert_not_reached ("missing case");
197         }
198       fputs_styled (string, fstyle, m_streams.back ());
199     }
200
201   if (after)
202     spaces (after);
203
204   if (align != ui_noalign)
205     field_separator ();
206 }
207
208 /* Output field containing ARGS using printf formatting in FORMAT.  */
209
210 void
211 cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
212                           const char *fldname, const char *format,
213                           va_list args)
214 {
215   if (m_suppress_output)
216     return;
217
218   std::string str = string_vprintf (format, args);
219
220   do_field_string (fldno, width, align, fldname, str.c_str (),
221                    ui_out_style_kind::DEFAULT);
222 }
223
224 void
225 cli_ui_out::do_spaces (int numspaces)
226 {
227   if (m_suppress_output)
228     return;
229
230   print_spaces_filtered (numspaces, m_streams.back ());
231 }
232
233 void
234 cli_ui_out::do_text (const char *string)
235 {
236   if (m_suppress_output)
237     return;
238
239   fputs_filtered (string, m_streams.back ());
240 }
241
242 void
243 cli_ui_out::do_message (const char *format, va_list args)
244 {
245   if (m_suppress_output)
246     return;
247
248   vfprintf_unfiltered (m_streams.back (), format, args);
249 }
250
251 void
252 cli_ui_out::do_wrap_hint (const char *identstring)
253 {
254   if (m_suppress_output)
255     return;
256
257   wrap_here (identstring);
258 }
259
260 void
261 cli_ui_out::do_flush ()
262 {
263   gdb_flush (m_streams.back ());
264 }
265
266 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
267    and make it therefore active.  OUTSTREAM as NULL will pop the last pushed
268    output stream; it is an internal error if it does not exist.  */
269
270 void
271 cli_ui_out::do_redirect (ui_file *outstream)
272 {
273   if (outstream != NULL)
274     m_streams.push_back (outstream);
275   else
276     m_streams.pop_back ();
277 }
278
279 /* local functions */
280
281 void
282 cli_ui_out::field_separator ()
283 {
284   fputc_filtered (' ', m_streams.back ());
285 }
286
287 /* Constructor for cli_ui_out.  */
288
289 cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags)
290 : ui_out (flags),
291   m_suppress_output (false)
292 {
293   gdb_assert (stream != NULL);
294
295   m_streams.push_back (stream);
296 }
297
298 cli_ui_out::~cli_ui_out ()
299 {
300 }
301
302 /* Initialize private members at startup.  */
303
304 cli_ui_out *
305 cli_out_new (struct ui_file *stream)
306 {
307   return new cli_ui_out (stream, ui_source_list);
308 }
309
310 ui_file *
311 cli_ui_out::set_stream (struct ui_file *stream)
312 {
313   ui_file *old;
314
315   old = m_streams.back ();
316   m_streams.back () = stream;
317
318   return old;
319 }
320
321 /* CLI interface to display tab-completion matches.  */
322
323 /* CLI version of displayer.crlf.  */
324
325 static void
326 cli_mld_crlf (const struct match_list_displayer *displayer)
327 {
328   rl_crlf ();
329 }
330
331 /* CLI version of displayer.putch.  */
332
333 static void
334 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
335 {
336   putc (ch, rl_outstream);
337 }
338
339 /* CLI version of displayer.puts.  */
340
341 static void
342 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
343 {
344   fputs (s, rl_outstream);
345 }
346
347 /* CLI version of displayer.flush.  */
348
349 static void
350 cli_mld_flush (const struct match_list_displayer *displayer)
351 {
352   fflush (rl_outstream);
353 }
354
355 EXTERN_C void _rl_erase_entire_line (void);
356
357 /* CLI version of displayer.erase_entire_line.  */
358
359 static void
360 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
361 {
362   _rl_erase_entire_line ();
363 }
364
365 /* CLI version of displayer.beep.  */
366
367 static void
368 cli_mld_beep (const struct match_list_displayer *displayer)
369 {
370   rl_ding ();
371 }
372
373 /* CLI version of displayer.read_key.  */
374
375 static int
376 cli_mld_read_key (const struct match_list_displayer *displayer)
377 {
378   return rl_read_key ();
379 }
380
381 /* CLI version of rl_completion_display_matches_hook.
382    See gdb_display_match_list for a description of the arguments.  */
383
384 void
385 cli_display_match_list (char **matches, int len, int max)
386 {
387   struct match_list_displayer displayer;
388
389   rl_get_screen_size (&displayer.height, &displayer.width);
390   displayer.crlf = cli_mld_crlf;
391   displayer.putch = cli_mld_putch;
392   displayer.puts = cli_mld_puts;
393   displayer.flush = cli_mld_flush;
394   displayer.erase_entire_line = cli_mld_erase_entire_line;
395   displayer.beep = cli_mld_beep;
396   displayer.read_key = cli_mld_read_key;
397
398   gdb_display_match_list (matches, len, max, &displayer);
399   rl_forced_update_display ();
400 }