1 /* TUI support I/O functions.
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
6 Contributed by Hewlett-Packard Company.
8 This file is part of GDB.
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 2 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 /* FIXME: cagney/2002-02-28: The GDB coding standard indicates that
26 "defs.h" should be included first. Unfortunatly some systems
27 (currently Debian GNU/Linux) include the <stdbool.h> via <curses.h>
28 and they clash with "bfd.h"'s definiton of true/false. The correct
29 fix is to remove true/false from "bfd.h", however, until that
30 happens, hack around it by including "config.h" and <curses.h>
46 #include "event-loop.h"
49 #include "readline/readline.h"
53 #include "tuiCommand.h"
55 #include "tuiGeneralWin.h"
62 /* This file controls the IO interactions between gdb and curses.
63 When the TUI is enabled, gdb has two modes a curses and a standard
66 In curses mode, the gdb outputs are made in a curses command window.
67 For this, the gdb_stdout and gdb_stderr are redirected to the specific
68 ui_file implemented by TUI. The output is handled by tui_puts().
69 The input is also controlled by curses with tui_getc(). The readline
70 library uses this function to get its input. Several readline hooks
71 are installed to redirect readline output to the TUI (see also the
74 In normal mode, the gdb outputs are restored to their origin, that
75 is as if TUI is not used. Readline also uses its original getc()
78 Note: the current readline is not clean in its management of the output.
79 Even if we install a redisplay handler, it sometimes writes on a stdout
80 file. It is important to redirect every output produced by readline,
81 otherwise the curses window will be garbled. This is implemented with
82 a pipe that TUI reads and readline writes to. A gdb input handler
83 is created so that reading the pipe is handled automatically.
84 This will probably not work on non-Unix platforms. The best fix is
85 to make readline clean enougth so that is never write on stdout. */
87 /* TUI output files. */
88 static struct ui_file
*tui_stdout
;
89 static struct ui_file
*tui_stderr
;
90 static struct ui_out
*tui_out
;
92 /* GDB output files in non-curses mode. */
93 static struct ui_file
*tui_old_stdout
;
94 static struct ui_file
*tui_old_stderr
;
95 static struct ui_out
*tui_old_uiout
;
97 /* Readline previous hooks. */
98 static Function
*tui_old_rl_getc_function
;
99 static VFunction
*tui_old_rl_redisplay_function
;
100 static VFunction
*tui_old_rl_prep_terminal
;
101 static VFunction
*tui_old_rl_deprep_terminal
;
102 static int tui_old_readline_echoing_p
;
104 /* Readline output stream.
105 Should be removed when readline is clean. */
106 static FILE *tui_rl_outstream
;
107 static FILE *tui_old_rl_outstream
;
108 static int tui_readline_pipe
[2];
110 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
113 /* Print the string in the curses command window. */
115 tui_puts (const char *string
)
117 static int tui_skip_line
= -1;
121 w
= cmdWin
->generic
.handle
;
122 while ((c
= *string
++) != 0)
124 /* Catch annotation and discard them. We need two \032 and
125 discard until a \n is seen. */
130 else if (tui_skip_line
!= 1)
138 getyx (w
, cmdWin
->detail
.commandInfo
.curLine
,
139 cmdWin
->detail
.commandInfo
.curch
);
140 cmdWin
->detail
.commandInfo
.start_line
= cmdWin
->detail
.commandInfo
.curLine
;
142 /* We could defer the following. */
147 /* Readline callback.
148 Redisplay the command line with its prompt after readline has
149 changed the edited text. */
151 tui_redisplay_readline (void)
163 prompt
= get_prompt ();
167 w
= cmdWin
->generic
.handle
;
168 start_line
= cmdWin
->detail
.commandInfo
.start_line
;
169 wmove (w
, start_line
, 0);
172 for (in
= 0; prompt
&& prompt
[in
]; in
++)
174 waddch (w
, prompt
[in
]);
175 getyx (w
, line
, col
);
180 for (in
= 0; in
< rl_end
; in
++)
184 c
= (unsigned char) rl_line_buffer
[in
];
187 getyx (w
, c_line
, c_pos
);
190 if (CTRL_CHAR (c
) || c
== RUBOUT
)
193 waddch (w
, CTRL_CHAR (c
) ? UNCTRL (c
) : '?');
201 getyx (w
, cmdWin
->detail
.commandInfo
.start_line
,
202 cmdWin
->detail
.commandInfo
.curch
);
204 getyx (w
, line
, col
);
210 getyx (w
, cmdWin
->detail
.commandInfo
.start_line
,
211 cmdWin
->detail
.commandInfo
.curch
);
214 wmove (w
, c_line
, c_pos
);
215 cmdWin
->detail
.commandInfo
.curLine
= c_line
;
216 cmdWin
->detail
.commandInfo
.curch
= c_pos
;
218 cmdWin
->detail
.commandInfo
.start_line
-= height
- 1;
224 /* Readline callback to prepare the terminal. It is called once
225 each time we enter readline. There is nothing to do in curses mode. */
227 tui_prep_terminal (void)
231 /* Readline callback to restore the terminal. It is called once
232 each time we leave readline. There is nothing to do in curses mode. */
234 tui_deprep_terminal (void)
238 /* Read readline output pipe and feed the command window with it.
239 Should be removed when readline is clean. */
241 tui_readline_output (int code
, gdb_client_data data
)
246 size
= read (tui_readline_pipe
[0], buf
, sizeof (buf
) - 1);
247 if (size
> 0 && tui_active
)
254 /* Setup the IO for curses or non-curses mode.
255 - In non-curses mode, readline and gdb use the standard input and
256 standard output/error directly.
257 - In curses mode, the standard output/error is controlled by TUI
258 with the tui_stdout and tui_stderr. The output is redirected in
259 the curses command window. Several readline callbacks are installed
260 so that readline asks for its input to the curses command window
263 tui_setup_io (int mode
)
265 extern int readline_echoing_p
;
269 /* Redirect readline to TUI. */
270 tui_old_rl_redisplay_function
= rl_redisplay_function
;
271 tui_old_rl_deprep_terminal
= rl_deprep_term_function
;
272 tui_old_rl_prep_terminal
= rl_prep_term_function
;
273 tui_old_rl_getc_function
= rl_getc_function
;
274 tui_old_rl_outstream
= rl_outstream
;
275 tui_old_readline_echoing_p
= readline_echoing_p
;
276 rl_redisplay_function
= tui_redisplay_readline
;
277 rl_deprep_term_function
= tui_deprep_terminal
;
278 rl_prep_term_function
= tui_prep_terminal
;
279 rl_getc_function
= tui_getc
;
280 readline_echoing_p
= 0;
281 rl_outstream
= tui_rl_outstream
;
284 /* Keep track of previous gdb output. */
285 tui_old_stdout
= gdb_stdout
;
286 tui_old_stderr
= gdb_stderr
;
287 tui_old_uiout
= uiout
;
289 /* Reconfigure gdb output. */
290 gdb_stdout
= tui_stdout
;
291 gdb_stderr
= tui_stderr
;
292 gdb_stdlog
= gdb_stdout
; /* for moment */
293 gdb_stdtarg
= gdb_stderr
; /* for moment */
296 /* Save tty for SIGCONT. */
301 /* Restore gdb output. */
302 gdb_stdout
= tui_old_stdout
;
303 gdb_stderr
= tui_old_stderr
;
304 gdb_stdlog
= gdb_stdout
; /* for moment */
305 gdb_stdtarg
= gdb_stderr
; /* for moment */
306 uiout
= tui_old_uiout
;
308 /* Restore readline. */
309 rl_redisplay_function
= tui_old_rl_redisplay_function
;
310 rl_deprep_term_function
= tui_old_rl_deprep_terminal
;
311 rl_prep_term_function
= tui_old_rl_prep_terminal
;
312 rl_getc_function
= tui_old_rl_getc_function
;
313 rl_outstream
= tui_old_rl_outstream
;
314 readline_echoing_p
= tui_old_readline_echoing_p
;
316 /* Save tty for SIGCONT. */
322 /* Catch SIGCONT to restore the terminal and refresh the screen. */
324 tui_cont_sig (int sig
)
328 /* Restore the terminal setting because another process (shell)
329 might have changed it. */
332 /* Force a refresh of the screen. */
335 /* Update cursor position on the screen. */
336 wmove (cmdWin
->generic
.handle
,
337 cmdWin
->detail
.commandInfo
.start_line
,
338 cmdWin
->detail
.commandInfo
.curch
);
339 wrefresh (cmdWin
->generic
.handle
);
341 signal (sig
, tui_cont_sig
);
345 /* Initialize the IO for gdb in curses mode. */
350 signal (SIGCONT
, tui_cont_sig
);
353 /* Create tui output streams. */
354 tui_stdout
= tui_fileopen (stdout
);
355 tui_stderr
= tui_fileopen (stderr
);
356 tui_out
= tui_out_new (tui_stdout
);
358 /* Create the default UI. It is not created because we installed
360 uiout
= cli_out_new (gdb_stdout
);
362 /* Temporary solution for readline writing to stdout:
363 redirect readline output in a pipe, read that pipe and
364 output the content in the curses command window. */
365 if (pipe (tui_readline_pipe
) != 0)
367 fprintf_unfiltered (gdb_stderr
, "Cannot create pipe for readline");
370 tui_rl_outstream
= fdopen (tui_readline_pipe
[1], "w");
371 if (tui_rl_outstream
== 0)
373 fprintf_unfiltered (gdb_stderr
, "Cannot redirect readline output");
376 setlinebuf (tui_rl_outstream
);
379 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NONBLOCK
);
382 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NDELAY
);
386 add_file_handler (tui_readline_pipe
[0], tui_readline_output
, 0);
389 /* Get a character from the command window. This is called from the readline
397 w
= cmdWin
->generic
.handle
;
399 /* Flush readline output. */
400 tui_readline_output (GDB_READABLE
, 0);
403 ch
= _tuiHandleResizeDuringIO (ch
);
405 /* The \n must be echoed because it will not be printed by readline. */
408 /* When hitting return with an empty input, gdb executes the last
409 command. If we emit a newline, this fills up the command window
410 with empty lines with gdb prompt at beginning. Instead of that,
411 stay on the same line but provide a visual effect to show the
412 user we recognized the command. */
415 wmove (w
, cmdWin
->detail
.commandInfo
.curLine
, 0);
417 /* Clear the line. This will blink the gdb prompt since
418 it will be redrawn at the same line. */
425 wmove (w
, cmdWin
->detail
.commandInfo
.curLine
,
426 cmdWin
->detail
.commandInfo
.curch
);
431 if (m_isCommandChar (ch
))
432 { /* Handle prev/next/up/down here */
433 ch
= tuiDispatchCtrlChar (ch
);
436 if (ch
== '\n' || ch
== '\r' || ch
== '\f')
437 cmdWin
->detail
.commandInfo
.curch
= 0;
440 tuiIncrCommandCharCountBy (1);
442 if (ch
== KEY_BACKSPACE
)
449 /* Cleanup when a resize has occured.
450 Returns the character that must be processed. */
452 _tuiHandleResizeDuringIO (unsigned int originalCh
)
454 if (tuiWinResized ())
458 tuiSetWinResizedTo (FALSE
);