Automatic date update in version.in
[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"
82083d6d 27#include "readline/readline.h"
8b93c638 28
0a8fce9a 29typedef struct cli_ui_out_data cli_out_data;
8b93c638 30
8b93c638
JM
31/* Prototypes for local functions */
32
02a45ac0 33static void cli_text (struct ui_out *uiout, const char *string);
8b93c638
JM
34
35static void field_separator (void);
36
e2e11a41
AC
37static void out_field_fmt (struct ui_out *uiout, int fldno,
38 const char *fldname,
a0b31db1 39 const char *format,...) ATTRIBUTE_PRINTF (4, 5);
8b93c638 40
17b2616c
PA
41/* The destructor. */
42
43static void
44cli_uiout_dtor (struct ui_out *ui_out)
45{
9a3c8263 46 cli_out_data *data = (cli_out_data *) ui_out_data (ui_out);
17b2616c 47
5486f164 48 delete data;
17b2616c
PA
49}
50
02a45ac0
PA
51/* These are the CLI output functions */
52
8b93c638
JM
53/* Mark beginning of a table */
54
02a45ac0 55static void
e2e11a41 56cli_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 57 int nr_rows,
e2e11a41 58 const char *tblid)
8b93c638 59{
9a3c8263 60 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 61
698384cd
AC
62 if (nr_rows == 0)
63 data->suppress_output = 1;
64 else
ce2826aa 65 /* Only the table suppresses the output and, fortunately, a table
30fdc99f 66 is not a recursive data structure. */
698384cd 67 gdb_assert (data->suppress_output == 0);
8b93c638
JM
68}
69
70/* Mark beginning of a table body */
71
02a45ac0 72static void
fba45db2 73cli_table_body (struct ui_out *uiout)
8b93c638 74{
9a3c8263 75 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 76
698384cd
AC
77 if (data->suppress_output)
78 return;
8b93c638
JM
79 /* first, close the table header line */
80 cli_text (uiout, "\n");
81}
82
83/* Mark end of a table */
84
02a45ac0 85static void
fba45db2 86cli_table_end (struct ui_out *uiout)
8b93c638 87{
9a3c8263 88 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 89
698384cd 90 data->suppress_output = 0;
8b93c638
JM
91}
92
93/* Specify table header */
94
02a45ac0 95static void
fba45db2 96cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
c5209615 97 const std::string &col_name, const std::string &col_hdr)
8b93c638 98{
9a3c8263 99 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 100
698384cd
AC
101 if (data->suppress_output)
102 return;
0a8fce9a
PA
103
104 /* Always go through the function pointer (virtual function call).
105 We may have been extended. */
c5209615 106 uo_field_string (uiout, 0, width, alignment, 0, col_hdr.c_str ());
8b93c638
JM
107}
108
109/* Mark beginning of a list */
110
02a45ac0 111static void
631ec795
AC
112cli_begin (struct ui_out *uiout,
113 enum ui_out_type type,
631ec795 114 const char *id)
8b93c638 115{
9a3c8263 116 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 117
698384cd
AC
118 if (data->suppress_output)
119 return;
8b93c638
JM
120}
121
122/* Mark end of a list */
123
02a45ac0 124static void
631ec795 125cli_end (struct ui_out *uiout,
33b2fac6 126 enum ui_out_type type)
8b93c638 127{
9a3c8263 128 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 129
698384cd
AC
130 if (data->suppress_output)
131 return;
8b93c638
JM
132}
133
134/* output an int field */
135
02a45ac0 136static void
fba45db2 137cli_field_int (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
138 enum ui_align alignment,
139 const char *fldname, int value)
8b93c638 140{
c5504eaf 141 char buffer[20]; /* FIXME: how many chars long a %d can become? */
9a3c8263 142 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 143
698384cd
AC
144 if (data->suppress_output)
145 return;
08850b56 146 xsnprintf (buffer, sizeof (buffer), "%d", value);
0a8fce9a
PA
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);
8b93c638
JM
151}
152
153/* used to ommit a field */
154
02a45ac0 155static void
fba45db2 156cli_field_skip (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
157 enum ui_align alignment,
158 const char *fldname)
8b93c638 159{
9a3c8263 160 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 161
698384cd
AC
162 if (data->suppress_output)
163 return;
0a8fce9a
PA
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, "");
8b93c638
JM
168}
169
170/* other specific cli_field_* end up here so alignment and field
171 separators are both handled by cli_field_string */
172
02a45ac0 173static void
8b93c638
JM
174cli_field_string (struct ui_out *uiout,
175 int fldno,
176 int width,
55555bbc 177 enum ui_align align,
e2e11a41 178 const char *fldname,
8b93c638
JM
179 const char *string)
180{
181 int before = 0;
182 int after = 0;
9a3c8263 183 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 184
698384cd
AC
185 if (data->suppress_output)
186 return;
187
8b93c638
JM
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
30fdc99f 222/* This is the only field function that does not align. */
8b93c638 223
a0b31db1 224static void ATTRIBUTE_PRINTF (6, 0)
8b93c638
JM
225cli_field_fmt (struct ui_out *uiout, int fldno,
226 int width, enum ui_align align,
e2e11a41
AC
227 const char *fldname,
228 const char *format,
229 va_list args)
8b93c638 230{
9a3c8263 231 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
14dba4b4 232 struct ui_file *stream;
c5504eaf 233
698384cd
AC
234 if (data->suppress_output)
235 return;
236
b9b118c3 237 stream = data->streams.back ();
14dba4b4 238 vfprintf_filtered (stream, format, args);
8b93c638
JM
239
240 if (align != ui_noalign)
241 field_separator ();
242}
243
02a45ac0 244static void
fba45db2 245cli_spaces (struct ui_out *uiout, int numspaces)
8b93c638 246{
9a3c8263 247 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
14dba4b4 248 struct ui_file *stream;
c5504eaf 249
698384cd
AC
250 if (data->suppress_output)
251 return;
14dba4b4 252
b9b118c3 253 stream = data->streams.back ();
14dba4b4 254 print_spaces_filtered (numspaces, stream);
8b93c638
JM
255}
256
02a45ac0 257static void
e2e11a41 258cli_text (struct ui_out *uiout, const char *string)
8b93c638 259{
9a3c8263 260 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
14dba4b4 261 struct ui_file *stream;
c5504eaf 262
698384cd
AC
263 if (data->suppress_output)
264 return;
14dba4b4 265
b9b118c3 266 stream = data->streams.back ();
14dba4b4 267 fputs_filtered (string, stream);
8b93c638
JM
268}
269
7fb048a2
SM
270static void ATTRIBUTE_PRINTF (2, 0)
271cli_message (struct ui_out *uiout, const char *format, va_list args)
8b93c638 272{
9a3c8263 273 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 274
698384cd
AC
275 if (data->suppress_output)
276 return;
14dba4b4 277
b9b118c3 278 struct ui_file *stream = data->streams.back ();
7fb048a2 279 vfprintf_unfiltered (stream, format, args);
8b93c638
JM
280}
281
02a45ac0 282static void
d2c0eef4 283cli_wrap_hint (struct ui_out *uiout, const char *identstring)
8b93c638 284{
9a3c8263 285 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 286
698384cd
AC
287 if (data->suppress_output)
288 return;
8b93c638
JM
289 wrap_here (identstring);
290}
291
02a45ac0 292static void
fba45db2 293cli_flush (struct ui_out *uiout)
8b93c638 294{
9a3c8263 295 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
b9b118c3 296 struct ui_file *stream = data->streams.back ();
c5504eaf 297
14dba4b4 298 gdb_flush (stream);
8b93c638
JM
299}
300
14dba4b4
JK
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
02a45ac0 305static int
0fac0b41
DJ
306cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
307{
9a3c8263 308 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
c5504eaf 309
0fac0b41 310 if (outstream != NULL)
b9b118c3 311 data->streams.push_back (outstream);
14dba4b4 312 else
b9b118c3 313 data->streams.pop_back ();
0fac0b41
DJ
314
315 return 0;
316}
317
8b93c638
JM
318/* local functions */
319
320/* Like cli_field_fmt, but takes a variable number of args
30fdc99f 321 and makes a va_list and does not insert a separator. */
8b93c638
JM
322
323/* VARARGS */
324static void
e2e11a41
AC
325out_field_fmt (struct ui_out *uiout, int fldno,
326 const char *fldname,
327 const char *format,...)
8b93c638 328{
9a3c8263 329 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
b9b118c3 330 struct ui_file *stream = data->streams.back ();
8b93c638
JM
331 va_list args;
332
333 va_start (args, format);
14dba4b4 334 vfprintf_filtered (stream, format, args);
8b93c638
JM
335
336 va_end (args);
337}
338
30fdc99f 339/* Access to ui_out format private members. */
8b93c638
JM
340
341static void
fba45db2 342field_separator (void)
8b93c638 343{
9a3c8263 344 cli_out_data *data = (cli_out_data *) ui_out_data (current_uiout);
b9b118c3 345 struct ui_file *stream = data->streams.back ();
c5504eaf 346
14dba4b4 347 fputc_filtered (' ', stream);
8b93c638
JM
348}
349
02a45ac0
PA
350/* This is the CLI ui-out implementation functions vector */
351
89de4da4 352const struct ui_out_impl cli_ui_out_impl =
02a45ac0
PA
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,
17b2616c 370 cli_uiout_dtor,
02a45ac0
PA
371 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
372};
373
0a8fce9a
PA
374/* Constructor for a `cli_out_data' object. */
375
376void
377cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
378{
14dba4b4
JK
379 gdb_assert (stream != NULL);
380
b9b118c3 381 self->streams.push_back (stream);
14dba4b4 382
0a8fce9a
PA
383 self->suppress_output = 0;
384}
385
386/* Initialize private members at startup. */
8b93c638
JM
387
388struct ui_out *
389cli_out_new (struct ui_file *stream)
390{
bef721e2 391 ui_out_flags flags = ui_source_list;
5486f164 392 cli_out_data *data = new cli_out_data ();
c5504eaf 393
0a8fce9a 394 cli_out_data_ctor (data, stream);
8b93c638
JM
395 return ui_out_new (&cli_ui_out_impl, data, flags);
396}
397
4389a95a
AC
398struct ui_file *
399cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
400{
9a3c8263 401 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
14dba4b4 402 struct ui_file *old;
b9b118c3
SM
403
404 old = data->streams.back ();
405 data->streams.back () = stream;
c5504eaf 406
4389a95a
AC
407 return old;
408}
b9b118c3 409
82083d6d
DE
410/* CLI interface to display tab-completion matches. */
411
412/* CLI version of displayer.crlf. */
413
414static void
415cli_mld_crlf (const struct match_list_displayer *displayer)
416{
417 rl_crlf ();
418}
419
420/* CLI version of displayer.putch. */
421
422static void
423cli_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
430static void
431cli_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
438static void
439cli_mld_flush (const struct match_list_displayer *displayer)
440{
441 fflush (rl_outstream);
442}
443
56000a98
PA
444EXTERN_C void _rl_erase_entire_line (void);
445
82083d6d
DE
446/* CLI version of displayer.erase_entire_line. */
447
448static void
449cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
450{
82083d6d
DE
451 _rl_erase_entire_line ();
452}
453
454/* CLI version of displayer.beep. */
455
456static void
457cli_mld_beep (const struct match_list_displayer *displayer)
458{
459 rl_ding ();
460}
461
462/* CLI version of displayer.read_key. */
463
464static int
465cli_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
473void
474cli_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}
This page took 1.047092 seconds and 4 git commands to generate.