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