Commit | Line | Data |
---|---|---|
8b93c638 | 1 | /* Output generating routines for GDB CLI. |
349c5d5f | 2 | |
e2882c85 | 3 | Copyright (C) 1999-2018 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 | |
02a45ac0 PA |
29 | /* These are the CLI output functions */ |
30 | ||
8b93c638 JM |
31 | /* Mark beginning of a table */ |
32 | ||
112e8700 SM |
33 | void |
34 | cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid) | |
8b93c638 | 35 | { |
698384cd | 36 | if (nr_rows == 0) |
112e8700 | 37 | m_suppress_output = true; |
698384cd | 38 | else |
ce2826aa | 39 | /* Only the table suppresses the output and, fortunately, a table |
30fdc99f | 40 | is not a recursive data structure. */ |
112e8700 | 41 | gdb_assert (!m_suppress_output); |
8b93c638 JM |
42 | } |
43 | ||
44 | /* Mark beginning of a table body */ | |
45 | ||
112e8700 SM |
46 | void |
47 | cli_ui_out::do_table_body () | |
8b93c638 | 48 | { |
112e8700 | 49 | if (m_suppress_output) |
698384cd | 50 | return; |
112e8700 | 51 | |
8b93c638 | 52 | /* first, close the table header line */ |
112e8700 | 53 | text ("\n"); |
8b93c638 JM |
54 | } |
55 | ||
56 | /* Mark end of a table */ | |
57 | ||
112e8700 SM |
58 | void |
59 | cli_ui_out::do_table_end () | |
8b93c638 | 60 | { |
112e8700 | 61 | m_suppress_output = false; |
8b93c638 JM |
62 | } |
63 | ||
64 | /* Specify table header */ | |
65 | ||
112e8700 SM |
66 | void |
67 | cli_ui_out::do_table_header (int width, ui_align alignment, | |
68 | const std::string &col_name, | |
69 | const std::string &col_hdr) | |
8b93c638 | 70 | { |
112e8700 | 71 | if (m_suppress_output) |
698384cd | 72 | return; |
0a8fce9a | 73 | |
112e8700 | 74 | do_field_string (0, width, alignment, 0, col_hdr.c_str ()); |
8b93c638 JM |
75 | } |
76 | ||
77 | /* Mark beginning of a list */ | |
78 | ||
112e8700 SM |
79 | void |
80 | cli_ui_out::do_begin (ui_out_type type, const char *id) | |
8b93c638 JM |
81 | { |
82 | } | |
83 | ||
84 | /* Mark end of a list */ | |
85 | ||
112e8700 SM |
86 | void |
87 | cli_ui_out::do_end (ui_out_type type) | |
8b93c638 JM |
88 | { |
89 | } | |
90 | ||
91 | /* output an int field */ | |
92 | ||
112e8700 SM |
93 | void |
94 | cli_ui_out::do_field_int (int fldno, int width, ui_align alignment, | |
95 | const char *fldname, int value) | |
8b93c638 | 96 | { |
112e8700 | 97 | if (m_suppress_output) |
698384cd | 98 | return; |
112e8700 | 99 | |
d63095c4 | 100 | std::string str = string_printf ("%d", value); |
0a8fce9a | 101 | |
d63095c4 | 102 | do_field_string (fldno, width, alignment, fldname, str.c_str ()); |
8b93c638 JM |
103 | } |
104 | ||
112e8700 | 105 | /* used to omit a field */ |
8b93c638 | 106 | |
112e8700 SM |
107 | void |
108 | cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment, | |
109 | const char *fldname) | |
8b93c638 | 110 | { |
112e8700 | 111 | if (m_suppress_output) |
698384cd | 112 | return; |
0a8fce9a | 113 | |
112e8700 | 114 | do_field_string (fldno, width, alignment, fldname, ""); |
8b93c638 JM |
115 | } |
116 | ||
117 | /* other specific cli_field_* end up here so alignment and field | |
118 | separators are both handled by cli_field_string */ | |
119 | ||
112e8700 SM |
120 | void |
121 | cli_ui_out::do_field_string (int fldno, int width, ui_align align, | |
122 | const char *fldname, const char *string) | |
8b93c638 JM |
123 | { |
124 | int before = 0; | |
125 | int after = 0; | |
c5504eaf | 126 | |
112e8700 | 127 | if (m_suppress_output) |
698384cd AC |
128 | return; |
129 | ||
8b93c638 JM |
130 | if ((align != ui_noalign) && string) |
131 | { | |
132 | before = width - strlen (string); | |
133 | if (before <= 0) | |
134 | before = 0; | |
135 | else | |
136 | { | |
137 | if (align == ui_right) | |
138 | after = 0; | |
139 | else if (align == ui_left) | |
140 | { | |
141 | after = before; | |
142 | before = 0; | |
143 | } | |
144 | else | |
145 | /* ui_center */ | |
146 | { | |
147 | after = before / 2; | |
148 | before -= after; | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | if (before) | |
112e8700 SM |
154 | spaces (before); |
155 | ||
8b93c638 | 156 | if (string) |
05b1d8d6 | 157 | fputs_filtered (string, m_streams.back ()); |
112e8700 | 158 | |
8b93c638 | 159 | if (after) |
112e8700 | 160 | spaces (after); |
8b93c638 JM |
161 | |
162 | if (align != ui_noalign) | |
163 | field_separator (); | |
164 | } | |
165 | ||
1871a62d | 166 | /* Output field containing ARGS using printf formatting in FORMAT. */ |
8b93c638 | 167 | |
112e8700 SM |
168 | void |
169 | cli_ui_out::do_field_fmt (int fldno, int width, ui_align align, | |
170 | const char *fldname, const char *format, | |
171 | va_list args) | |
8b93c638 | 172 | { |
112e8700 | 173 | if (m_suppress_output) |
698384cd AC |
174 | return; |
175 | ||
1871a62d | 176 | std::string str = string_vprintf (format, args); |
8b93c638 | 177 | |
1871a62d | 178 | do_field_string (fldno, width, align, fldname, str.c_str ()); |
8b93c638 JM |
179 | } |
180 | ||
112e8700 SM |
181 | void |
182 | cli_ui_out::do_spaces (int numspaces) | |
8b93c638 | 183 | { |
112e8700 | 184 | if (m_suppress_output) |
698384cd | 185 | return; |
14dba4b4 | 186 | |
112e8700 | 187 | print_spaces_filtered (numspaces, m_streams.back ()); |
8b93c638 JM |
188 | } |
189 | ||
112e8700 SM |
190 | void |
191 | cli_ui_out::do_text (const char *string) | |
8b93c638 | 192 | { |
112e8700 | 193 | if (m_suppress_output) |
698384cd | 194 | return; |
14dba4b4 | 195 | |
112e8700 | 196 | fputs_filtered (string, m_streams.back ()); |
8b93c638 JM |
197 | } |
198 | ||
112e8700 SM |
199 | void |
200 | cli_ui_out::do_message (const char *format, va_list args) | |
8b93c638 | 201 | { |
112e8700 | 202 | if (m_suppress_output) |
698384cd | 203 | return; |
14dba4b4 | 204 | |
112e8700 | 205 | vfprintf_unfiltered (m_streams.back (), format, args); |
8b93c638 JM |
206 | } |
207 | ||
112e8700 SM |
208 | void |
209 | cli_ui_out::do_wrap_hint (const char *identstring) | |
8b93c638 | 210 | { |
112e8700 | 211 | if (m_suppress_output) |
698384cd | 212 | return; |
112e8700 | 213 | |
8b93c638 JM |
214 | wrap_here (identstring); |
215 | } | |
216 | ||
112e8700 SM |
217 | void |
218 | cli_ui_out::do_flush () | |
8b93c638 | 219 | { |
112e8700 | 220 | gdb_flush (m_streams.back ()); |
8b93c638 JM |
221 | } |
222 | ||
14dba4b4 JK |
223 | /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams |
224 | and make it therefore active. OUTSTREAM as NULL will pop the last pushed | |
225 | output stream; it is an internal error if it does not exist. */ | |
226 | ||
7becfd03 | 227 | void |
112e8700 | 228 | cli_ui_out::do_redirect (ui_file *outstream) |
0fac0b41 | 229 | { |
0fac0b41 | 230 | if (outstream != NULL) |
112e8700 | 231 | m_streams.push_back (outstream); |
14dba4b4 | 232 | else |
112e8700 | 233 | m_streams.pop_back (); |
0fac0b41 DJ |
234 | } |
235 | ||
8b93c638 JM |
236 | /* local functions */ |
237 | ||
112e8700 SM |
238 | void |
239 | cli_ui_out::field_separator () | |
8b93c638 | 240 | { |
112e8700 | 241 | fputc_filtered (' ', m_streams.back ()); |
8b93c638 JM |
242 | } |
243 | ||
112e8700 | 244 | /* Constructor for cli_ui_out. */ |
02a45ac0 | 245 | |
112e8700 SM |
246 | cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags) |
247 | : ui_out (flags), | |
248 | m_suppress_output (false) | |
0a8fce9a | 249 | { |
14dba4b4 JK |
250 | gdb_assert (stream != NULL); |
251 | ||
112e8700 SM |
252 | m_streams.push_back (stream); |
253 | } | |
14dba4b4 | 254 | |
112e8700 SM |
255 | cli_ui_out::~cli_ui_out () |
256 | { | |
0a8fce9a PA |
257 | } |
258 | ||
259 | /* Initialize private members at startup. */ | |
8b93c638 | 260 | |
112e8700 | 261 | cli_ui_out * |
8b93c638 JM |
262 | cli_out_new (struct ui_file *stream) |
263 | { | |
112e8700 | 264 | return new cli_ui_out (stream, ui_source_list); |
8b93c638 JM |
265 | } |
266 | ||
112e8700 SM |
267 | ui_file * |
268 | cli_ui_out::set_stream (struct ui_file *stream) | |
4389a95a | 269 | { |
112e8700 | 270 | ui_file *old; |
b9b118c3 | 271 | |
112e8700 SM |
272 | old = m_streams.back (); |
273 | m_streams.back () = stream; | |
c5504eaf | 274 | |
4389a95a AC |
275 | return old; |
276 | } | |
b9b118c3 | 277 | |
82083d6d DE |
278 | /* CLI interface to display tab-completion matches. */ |
279 | ||
280 | /* CLI version of displayer.crlf. */ | |
281 | ||
282 | static void | |
283 | cli_mld_crlf (const struct match_list_displayer *displayer) | |
284 | { | |
285 | rl_crlf (); | |
286 | } | |
287 | ||
288 | /* CLI version of displayer.putch. */ | |
289 | ||
290 | static void | |
291 | cli_mld_putch (const struct match_list_displayer *displayer, int ch) | |
292 | { | |
293 | putc (ch, rl_outstream); | |
294 | } | |
295 | ||
296 | /* CLI version of displayer.puts. */ | |
297 | ||
298 | static void | |
299 | cli_mld_puts (const struct match_list_displayer *displayer, const char *s) | |
300 | { | |
301 | fputs (s, rl_outstream); | |
302 | } | |
303 | ||
304 | /* CLI version of displayer.flush. */ | |
305 | ||
306 | static void | |
307 | cli_mld_flush (const struct match_list_displayer *displayer) | |
308 | { | |
309 | fflush (rl_outstream); | |
310 | } | |
311 | ||
56000a98 PA |
312 | EXTERN_C void _rl_erase_entire_line (void); |
313 | ||
82083d6d DE |
314 | /* CLI version of displayer.erase_entire_line. */ |
315 | ||
316 | static void | |
317 | cli_mld_erase_entire_line (const struct match_list_displayer *displayer) | |
318 | { | |
82083d6d DE |
319 | _rl_erase_entire_line (); |
320 | } | |
321 | ||
322 | /* CLI version of displayer.beep. */ | |
323 | ||
324 | static void | |
325 | cli_mld_beep (const struct match_list_displayer *displayer) | |
326 | { | |
327 | rl_ding (); | |
328 | } | |
329 | ||
330 | /* CLI version of displayer.read_key. */ | |
331 | ||
332 | static int | |
333 | cli_mld_read_key (const struct match_list_displayer *displayer) | |
334 | { | |
335 | return rl_read_key (); | |
336 | } | |
337 | ||
338 | /* CLI version of rl_completion_display_matches_hook. | |
339 | See gdb_display_match_list for a description of the arguments. */ | |
340 | ||
341 | void | |
342 | cli_display_match_list (char **matches, int len, int max) | |
343 | { | |
344 | struct match_list_displayer displayer; | |
345 | ||
346 | rl_get_screen_size (&displayer.height, &displayer.width); | |
347 | displayer.crlf = cli_mld_crlf; | |
348 | displayer.putch = cli_mld_putch; | |
349 | displayer.puts = cli_mld_puts; | |
350 | displayer.flush = cli_mld_flush; | |
351 | displayer.erase_entire_line = cli_mld_erase_entire_line; | |
352 | displayer.beep = cli_mld_beep; | |
353 | displayer.read_key = cli_mld_read_key; | |
354 | ||
355 | gdb_display_match_list (matches, len, max, &displayer); | |
356 | rl_forced_update_display (); | |
357 | } |