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