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