Use std::vector for cli_ui_out_data::streams
[external/binutils.git] / gdb / cli-out.c
1 /* Output generating routines for GDB CLI.
2
3    Copyright (C) 1999-2016 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 typedef struct cli_ui_out_data cli_out_data;
30
31 /* Prototypes for local functions */
32
33 static void cli_text (struct ui_out *uiout, const char *string);
34
35 static void field_separator (void);
36
37 static void out_field_fmt (struct ui_out *uiout, int fldno,
38                            const char *fldname,
39                            const char *format,...) ATTRIBUTE_PRINTF (4, 5);
40
41 /* The destructor.  */
42
43 static void
44 cli_uiout_dtor (struct ui_out *ui_out)
45 {
46   cli_out_data *data = (cli_out_data *) ui_out_data (ui_out);
47
48   delete data;
49 }
50
51 /* These are the CLI output functions */
52
53 /* Mark beginning of a table */
54
55 static void
56 cli_table_begin (struct ui_out *uiout, int nbrofcols,
57                  int nr_rows,
58                  const char *tblid)
59 {
60   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
61
62   if (nr_rows == 0)
63     data->suppress_output = 1;
64   else
65     /* Only the table suppresses the output and, fortunately, a table
66        is not a recursive data structure.  */
67     gdb_assert (data->suppress_output == 0);
68 }
69
70 /* Mark beginning of a table body */
71
72 static void
73 cli_table_body (struct ui_out *uiout)
74 {
75   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
76
77   if (data->suppress_output)
78     return;
79   /* first, close the table header line */
80   cli_text (uiout, "\n");
81 }
82
83 /* Mark end of a table */
84
85 static void
86 cli_table_end (struct ui_out *uiout)
87 {
88   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
89
90   data->suppress_output = 0;
91 }
92
93 /* Specify table header */
94
95 static void
96 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
97                   const char *col_name,
98                   const char *colhdr)
99 {
100   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
101
102   if (data->suppress_output)
103     return;
104
105   /* Always go through the function pointer (virtual function call).
106      We may have been extended.  */
107   uo_field_string (uiout, 0, width, alignment, 0, colhdr);
108 }
109
110 /* Mark beginning of a list */
111
112 static void
113 cli_begin (struct ui_out *uiout,
114            enum ui_out_type type,
115            int level,
116            const char *id)
117 {
118   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
119
120   if (data->suppress_output)
121     return;
122 }
123
124 /* Mark end of a list */
125
126 static void
127 cli_end (struct ui_out *uiout,
128          enum ui_out_type type,
129          int level)
130 {
131   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
132
133   if (data->suppress_output)
134     return;
135 }
136
137 /* output an int field */
138
139 static void
140 cli_field_int (struct ui_out *uiout, int fldno, int width,
141                enum ui_align alignment,
142                const char *fldname, int value)
143 {
144   char buffer[20];      /* FIXME: how many chars long a %d can become? */
145   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
146
147   if (data->suppress_output)
148     return;
149   xsnprintf (buffer, sizeof (buffer), "%d", value);
150
151   /* Always go through the function pointer (virtual function call).
152      We may have been extended.  */
153   uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
154 }
155
156 /* used to ommit a field */
157
158 static void
159 cli_field_skip (struct ui_out *uiout, int fldno, int width,
160                 enum ui_align alignment,
161                 const char *fldname)
162 {
163   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
164
165   if (data->suppress_output)
166     return;
167
168   /* Always go through the function pointer (virtual function call).
169      We may have been extended.  */
170   uo_field_string (uiout, fldno, width, alignment, fldname, "");
171 }
172
173 /* other specific cli_field_* end up here so alignment and field
174    separators are both handled by cli_field_string */
175
176 static void
177 cli_field_string (struct ui_out *uiout,
178                   int fldno,
179                   int width,
180                   enum ui_align align,
181                   const char *fldname,
182                   const char *string)
183 {
184   int before = 0;
185   int after = 0;
186   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
187
188   if (data->suppress_output)
189     return;
190
191   if ((align != ui_noalign) && string)
192     {
193       before = width - strlen (string);
194       if (before <= 0)
195         before = 0;
196       else
197         {
198           if (align == ui_right)
199             after = 0;
200           else if (align == ui_left)
201             {
202               after = before;
203               before = 0;
204             }
205           else
206             /* ui_center */
207             {
208               after = before / 2;
209               before -= after;
210             }
211         }
212     }
213
214   if (before)
215     ui_out_spaces (uiout, before);
216   if (string)
217     out_field_fmt (uiout, fldno, fldname, "%s", string);
218   if (after)
219     ui_out_spaces (uiout, after);
220
221   if (align != ui_noalign)
222     field_separator ();
223 }
224
225 /* This is the only field function that does not align.  */
226
227 static void ATTRIBUTE_PRINTF (6, 0)
228 cli_field_fmt (struct ui_out *uiout, int fldno,
229                int width, enum ui_align align,
230                const char *fldname,
231                const char *format,
232                va_list args)
233 {
234   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
235   struct ui_file *stream;
236
237   if (data->suppress_output)
238     return;
239
240   stream = data->streams.back ();
241   vfprintf_filtered (stream, format, args);
242
243   if (align != ui_noalign)
244     field_separator ();
245 }
246
247 static void
248 cli_spaces (struct ui_out *uiout, int numspaces)
249 {
250   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
251   struct ui_file *stream;
252
253   if (data->suppress_output)
254     return;
255
256   stream = data->streams.back ();
257   print_spaces_filtered (numspaces, stream);
258 }
259
260 static void
261 cli_text (struct ui_out *uiout, const char *string)
262 {
263   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
264   struct ui_file *stream;
265
266   if (data->suppress_output)
267     return;
268
269   stream = data->streams.back ();
270   fputs_filtered (string, stream);
271 }
272
273 static void ATTRIBUTE_PRINTF (2, 0)
274 cli_message (struct ui_out *uiout, const char *format, va_list args)
275 {
276   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
277
278   if (data->suppress_output)
279     return;
280
281   struct ui_file *stream = data->streams.back ();
282   vfprintf_unfiltered (stream, format, args);
283 }
284
285 static void
286 cli_wrap_hint (struct ui_out *uiout, const char *identstring)
287 {
288   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
289
290   if (data->suppress_output)
291     return;
292   wrap_here (identstring);
293 }
294
295 static void
296 cli_flush (struct ui_out *uiout)
297 {
298   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
299   struct ui_file *stream = data->streams.back ();
300
301   gdb_flush (stream);
302 }
303
304 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
305    and make it therefore active.  OUTSTREAM as NULL will pop the last pushed
306    output stream; it is an internal error if it does not exist.  */
307
308 static int
309 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
310 {
311   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
312
313   if (outstream != NULL)
314     data->streams.push_back (outstream);
315   else
316     data->streams.pop_back ();
317
318   return 0;
319 }
320
321 /* local functions */
322
323 /* Like cli_field_fmt, but takes a variable number of args
324    and makes a va_list and does not insert a separator.  */
325
326 /* VARARGS */
327 static void
328 out_field_fmt (struct ui_out *uiout, int fldno,
329                const char *fldname,
330                const char *format,...)
331 {
332   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
333   struct ui_file *stream = data->streams.back ();
334   va_list args;
335
336   va_start (args, format);
337   vfprintf_filtered (stream, format, args);
338
339   va_end (args);
340 }
341
342 /* Access to ui_out format private members.  */
343
344 static void
345 field_separator (void)
346 {
347   cli_out_data *data = (cli_out_data *) ui_out_data (current_uiout);
348   struct ui_file *stream = data->streams.back ();
349
350   fputc_filtered (' ', stream);
351 }
352
353 /* This is the CLI ui-out implementation functions vector */
354
355 const struct ui_out_impl cli_ui_out_impl =
356 {
357   cli_table_begin,
358   cli_table_body,
359   cli_table_end,
360   cli_table_header,
361   cli_begin,
362   cli_end,
363   cli_field_int,
364   cli_field_skip,
365   cli_field_string,
366   cli_field_fmt,
367   cli_spaces,
368   cli_text,
369   cli_message,
370   cli_wrap_hint,
371   cli_flush,
372   cli_redirect,
373   cli_uiout_dtor,
374   0, /* Does not need MI hacks (i.e. needs CLI hacks).  */
375 };
376
377 /* Constructor for a `cli_out_data' object.  */
378
379 void
380 cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
381 {
382   gdb_assert (stream != NULL);
383
384   self->streams.push_back (stream);
385
386   self->suppress_output = 0;
387 }
388
389 /* Initialize private members at startup.  */
390
391 struct ui_out *
392 cli_out_new (struct ui_file *stream)
393 {
394   int flags = ui_source_list;
395   cli_out_data *data = new cli_out_data ();
396
397   cli_out_data_ctor (data, stream);
398   return ui_out_new (&cli_ui_out_impl, data, flags);
399 }
400
401 struct ui_file *
402 cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
403 {
404   cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
405   struct ui_file *old;
406
407   old = data->streams.back ();
408   data->streams.back () = stream;
409
410   return old;
411 }
412
413 /* CLI interface to display tab-completion matches.  */
414
415 /* CLI version of displayer.crlf.  */
416
417 static void
418 cli_mld_crlf (const struct match_list_displayer *displayer)
419 {
420   rl_crlf ();
421 }
422
423 /* CLI version of displayer.putch.  */
424
425 static void
426 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
427 {
428   putc (ch, rl_outstream);
429 }
430
431 /* CLI version of displayer.puts.  */
432
433 static void
434 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
435 {
436   fputs (s, rl_outstream);
437 }
438
439 /* CLI version of displayer.flush.  */
440
441 static void
442 cli_mld_flush (const struct match_list_displayer *displayer)
443 {
444   fflush (rl_outstream);
445 }
446
447 EXTERN_C void _rl_erase_entire_line (void);
448
449 /* CLI version of displayer.erase_entire_line.  */
450
451 static void
452 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
453 {
454   _rl_erase_entire_line ();
455 }
456
457 /* CLI version of displayer.beep.  */
458
459 static void
460 cli_mld_beep (const struct match_list_displayer *displayer)
461 {
462   rl_ding ();
463 }
464
465 /* CLI version of displayer.read_key.  */
466
467 static int
468 cli_mld_read_key (const struct match_list_displayer *displayer)
469 {
470   return rl_read_key ();
471 }
472
473 /* CLI version of rl_completion_display_matches_hook.
474    See gdb_display_match_list for a description of the arguments.  */
475
476 void
477 cli_display_match_list (char **matches, int len, int max)
478 {
479   struct match_list_displayer displayer;
480
481   rl_get_screen_size (&displayer.height, &displayer.width);
482   displayer.crlf = cli_mld_crlf;
483   displayer.putch = cli_mld_putch;
484   displayer.puts = cli_mld_puts;
485   displayer.flush = cli_mld_flush;
486   displayer.erase_entire_line = cli_mld_erase_entire_line;
487   displayer.beep = cli_mld_beep;
488   displayer.read_key = cli_mld_read_key;
489
490   gdb_display_match_list (matches, len, max, &displayer);
491   rl_forced_update_display ();
492 }