gdb: Respect field width and alignment for 'fmt' fields in CLI output
[external/binutils.git] / gdb / cli-out.c
1 /* Output generating routines for GDB CLI.
2
3    Copyright (C) 1999-2018 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
29 /* These are the CLI output functions */
30
31 /* Mark beginning of a table */
32
33 void
34 cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
35 {
36   if (nr_rows == 0)
37     m_suppress_output = true;
38   else
39     /* Only the table suppresses the output and, fortunately, a table
40        is not a recursive data structure.  */
41     gdb_assert (!m_suppress_output);
42 }
43
44 /* Mark beginning of a table body */
45
46 void
47 cli_ui_out::do_table_body ()
48 {
49   if (m_suppress_output)
50     return;
51
52   /* first, close the table header line */
53   text ("\n");
54 }
55
56 /* Mark end of a table */
57
58 void
59 cli_ui_out::do_table_end ()
60 {
61   m_suppress_output = false;
62 }
63
64 /* Specify table header */
65
66 void
67 cli_ui_out::do_table_header (int width, ui_align alignment,
68                              const std::string &col_name,
69                              const std::string &col_hdr)
70 {
71   if (m_suppress_output)
72     return;
73
74   do_field_string (0, width, alignment, 0, col_hdr.c_str ());
75 }
76
77 /* Mark beginning of a list */
78
79 void
80 cli_ui_out::do_begin (ui_out_type type, const char *id)
81 {
82 }
83
84 /* Mark end of a list */
85
86 void
87 cli_ui_out::do_end (ui_out_type type)
88 {
89 }
90
91 /* output an int field */
92
93 void
94 cli_ui_out::do_field_int (int fldno, int width, ui_align alignment,
95                           const char *fldname, int value)
96 {
97   char buffer[20];      /* FIXME: how many chars long a %d can become? */
98
99   if (m_suppress_output)
100     return;
101
102   xsnprintf (buffer, sizeof (buffer), "%d", value);
103
104   do_field_string (fldno, width, alignment, fldname, buffer);
105 }
106
107 /* used to omit a field */
108
109 void
110 cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
111                            const char *fldname)
112 {
113   if (m_suppress_output)
114     return;
115
116   do_field_string (fldno, width, alignment, fldname, "");
117 }
118
119 /* other specific cli_field_* end up here so alignment and field
120    separators are both handled by cli_field_string */
121
122 void
123 cli_ui_out::do_field_string (int fldno, int width, ui_align align,
124                              const char *fldname, const char *string)
125 {
126   int before = 0;
127   int after = 0;
128
129   if (m_suppress_output)
130     return;
131
132   if ((align != ui_noalign) && string)
133     {
134       before = width - strlen (string);
135       if (before <= 0)
136         before = 0;
137       else
138         {
139           if (align == ui_right)
140             after = 0;
141           else if (align == ui_left)
142             {
143               after = before;
144               before = 0;
145             }
146           else
147             /* ui_center */
148             {
149               after = before / 2;
150               before -= after;
151             }
152         }
153     }
154
155   if (before)
156     spaces (before);
157
158   if (string)
159     fputs_filtered (string, m_streams.back ());
160
161   if (after)
162     spaces (after);
163
164   if (align != ui_noalign)
165     field_separator ();
166 }
167
168 /* Output field containing ARGS using printf formatting in FORMAT.  */
169
170 void
171 cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
172                           const char *fldname, const char *format,
173                           va_list args)
174 {
175   if (m_suppress_output)
176     return;
177
178   std::string str = string_vprintf (format, args);
179
180   do_field_string (fldno, width, align, fldname, str.c_str ());
181 }
182
183 void
184 cli_ui_out::do_spaces (int numspaces)
185 {
186   if (m_suppress_output)
187     return;
188
189   print_spaces_filtered (numspaces, m_streams.back ());
190 }
191
192 void
193 cli_ui_out::do_text (const char *string)
194 {
195   if (m_suppress_output)
196     return;
197
198   fputs_filtered (string, m_streams.back ());
199 }
200
201 void
202 cli_ui_out::do_message (const char *format, va_list args)
203 {
204   if (m_suppress_output)
205     return;
206
207   vfprintf_unfiltered (m_streams.back (), format, args);
208 }
209
210 void
211 cli_ui_out::do_wrap_hint (const char *identstring)
212 {
213   if (m_suppress_output)
214     return;
215
216   wrap_here (identstring);
217 }
218
219 void
220 cli_ui_out::do_flush ()
221 {
222   gdb_flush (m_streams.back ());
223 }
224
225 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
226    and make it therefore active.  OUTSTREAM as NULL will pop the last pushed
227    output stream; it is an internal error if it does not exist.  */
228
229 void
230 cli_ui_out::do_redirect (ui_file *outstream)
231 {
232   if (outstream != NULL)
233     m_streams.push_back (outstream);
234   else
235     m_streams.pop_back ();
236 }
237
238 /* local functions */
239
240 void
241 cli_ui_out::field_separator ()
242 {
243   fputc_filtered (' ', m_streams.back ());
244 }
245
246 /* Constructor for cli_ui_out.  */
247
248 cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags)
249 : ui_out (flags),
250   m_suppress_output (false)
251 {
252   gdb_assert (stream != NULL);
253
254   m_streams.push_back (stream);
255 }
256
257 cli_ui_out::~cli_ui_out ()
258 {
259 }
260
261 /* Initialize private members at startup.  */
262
263 cli_ui_out *
264 cli_out_new (struct ui_file *stream)
265 {
266   return new cli_ui_out (stream, ui_source_list);
267 }
268
269 ui_file *
270 cli_ui_out::set_stream (struct ui_file *stream)
271 {
272   ui_file *old;
273
274   old = m_streams.back ();
275   m_streams.back () = stream;
276
277   return old;
278 }
279
280 /* CLI interface to display tab-completion matches.  */
281
282 /* CLI version of displayer.crlf.  */
283
284 static void
285 cli_mld_crlf (const struct match_list_displayer *displayer)
286 {
287   rl_crlf ();
288 }
289
290 /* CLI version of displayer.putch.  */
291
292 static void
293 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
294 {
295   putc (ch, rl_outstream);
296 }
297
298 /* CLI version of displayer.puts.  */
299
300 static void
301 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
302 {
303   fputs (s, rl_outstream);
304 }
305
306 /* CLI version of displayer.flush.  */
307
308 static void
309 cli_mld_flush (const struct match_list_displayer *displayer)
310 {
311   fflush (rl_outstream);
312 }
313
314 EXTERN_C void _rl_erase_entire_line (void);
315
316 /* CLI version of displayer.erase_entire_line.  */
317
318 static void
319 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
320 {
321   _rl_erase_entire_line ();
322 }
323
324 /* CLI version of displayer.beep.  */
325
326 static void
327 cli_mld_beep (const struct match_list_displayer *displayer)
328 {
329   rl_ding ();
330 }
331
332 /* CLI version of displayer.read_key.  */
333
334 static int
335 cli_mld_read_key (const struct match_list_displayer *displayer)
336 {
337   return rl_read_key ();
338 }
339
340 /* CLI version of rl_completion_display_matches_hook.
341    See gdb_display_match_list for a description of the arguments.  */
342
343 void
344 cli_display_match_list (char **matches, int len, int max)
345 {
346   struct match_list_displayer displayer;
347
348   rl_get_screen_size (&displayer.height, &displayer.width);
349   displayer.crlf = cli_mld_crlf;
350   displayer.putch = cli_mld_putch;
351   displayer.puts = cli_mld_puts;
352   displayer.flush = cli_mld_flush;
353   displayer.erase_entire_line = cli_mld_erase_entire_line;
354   displayer.beep = cli_mld_beep;
355   displayer.read_key = cli_mld_read_key;
356
357   gdb_display_match_list (matches, len, max, &displayer);
358   rl_forced_update_display ();
359 }