1 /* TUI support I/O functions.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Hewlett-Packard Company.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "event-loop.h"
29 #include "readline/readline.h"
33 #include "tuiCommand.h"
35 #include "tuiGeneralWin.h"
41 /* This file controls the IO interactions between gdb and curses.
42 When the TUI is enabled, gdb has two modes a curses and a standard
45 In curses mode, the gdb outputs are made in a curses command window.
46 For this, the gdb_stdout and gdb_stderr are redirected to the specific
47 ui_file implemented by TUI. The output is handled by tui_puts().
48 The input is also controlled by curses with tui_getc(). The readline
49 library uses this function to get its input. Several readline hooks
50 are installed to redirect readline output to the TUI (see also the
53 In normal mode, the gdb outputs are restored to their origin, that
54 is as if TUI is not used. Readline also uses its original getc()
57 Note: the current readline is not clean in its management of the output.
58 Even if we install a redisplay handler, it sometimes writes on a stdout
59 file. It is important to redirect every output produced by readline,
60 otherwise the curses window will be garbled. This is implemented with
61 a pipe that TUI reads and readline writes to. A gdb input handler
62 is created so that reading the pipe is handled automatically.
63 This will probably not work on non-Unix platforms. The best fix is
64 to make readline clean enougth so that is never write on stdout. */
66 /* TUI output files. */
67 static struct ui_file
*tui_stdout
;
68 static struct ui_file
*tui_stderr
;
69 static struct ui_out
*tui_out
;
71 /* GDB output files in non-curses mode. */
72 static struct ui_file
*tui_old_stdout
;
73 static struct ui_file
*tui_old_stderr
;
74 static struct ui_out
*tui_old_uiout
;
76 /* Readline previous hooks. */
77 static Function
*tui_old_rl_getc_function
;
78 static VFunction
*tui_old_rl_redisplay_function
;
79 static VFunction
*tui_old_rl_prep_terminal
;
80 static VFunction
*tui_old_rl_deprep_terminal
;
81 static int tui_old_readline_echoing_p
;
83 /* Readline output stream.
84 Should be removed when readline is clean. */
85 static FILE *tui_rl_outstream
;
86 static FILE *tui_old_rl_outstream
;
87 static int tui_readline_pipe
[2];
89 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
92 /* Print the string in the curses command window. */
94 tui_puts (const char *string
)
96 static int tui_skip_line
= -1;
100 w
= cmdWin
->generic
.handle
;
101 while ((c
= *string
++) != 0)
103 /* Catch annotation and discard them. We need two \032 and
104 discard until a \n is seen. */
109 else if (tui_skip_line
!= 1)
117 getyx (w
, cmdWin
->detail
.commandInfo
.curLine
,
118 cmdWin
->detail
.commandInfo
.curch
);
120 /* We could defer the following. */
125 /* Readline callback.
126 Redisplay the command line with its prompt after readline has
127 changed the edited text. */
129 tui_redisplay_readline (void)
141 prompt
= get_prompt ();
145 w
= cmdWin
->generic
.handle
;
146 start_line
= cmdWin
->detail
.commandInfo
.curLine
;
147 wmove (w
, start_line
, 0);
150 for (in
= 0; prompt
&& prompt
[in
]; in
++)
152 waddch (w
, prompt
[in
]);
153 getyx (w
, line
, col
);
158 for (in
= 0; in
< rl_end
; in
++)
162 c
= (unsigned char) rl_line_buffer
[in
];
165 getyx (w
, c_line
, c_pos
);
168 if (CTRL_CHAR (c
) || c
== RUBOUT
)
171 waddch (w
, CTRL_CHAR (c
) ? UNCTRL (c
) : '?');
179 getyx (w
, cmdWin
->detail
.commandInfo
.curLine
,
180 cmdWin
->detail
.commandInfo
.curch
);
182 getyx (w
, line
, col
);
188 getyx (w
, cmdWin
->detail
.commandInfo
.curLine
,
189 cmdWin
->detail
.commandInfo
.curch
);
191 wmove (w
, c_line
, c_pos
);
193 cmdWin
->detail
.commandInfo
.curLine
-= height
- 1;
199 /* Readline callback to prepare the terminal. It is called once
200 each time we enter readline. There is nothing to do in curses mode. */
202 tui_prep_terminal (void)
206 /* Readline callback to restore the terminal. It is called once
207 each time we leave readline. There is nothing to do in curses mode. */
209 tui_deprep_terminal (void)
213 /* Read readline output pipe and feed the command window with it.
214 Should be removed when readline is clean. */
216 tui_readline_output (int code
, gdb_client_data data
)
221 size
= read (tui_readline_pipe
[0], buf
, sizeof (buf
) - 1);
222 if (size
> 0 && tui_active
)
229 /* Setup the IO for curses or non-curses mode.
230 - In non-curses mode, readline and gdb use the standard input and
231 standard output/error directly.
232 - In curses mode, the standard output/error is controlled by TUI
233 with the tui_stdout and tui_stderr. The output is redirected in
234 the curses command window. Several readline callbacks are installed
235 so that readline asks for its input to the curses command window
238 tui_setup_io (int mode
)
240 extern int readline_echoing_p
;
244 /* Redirect readline to TUI. */
245 tui_old_rl_redisplay_function
= rl_redisplay_function
;
246 tui_old_rl_deprep_terminal
= rl_deprep_term_function
;
247 tui_old_rl_prep_terminal
= rl_prep_term_function
;
248 tui_old_rl_getc_function
= rl_getc_function
;
249 tui_old_rl_outstream
= rl_outstream
;
250 tui_old_readline_echoing_p
= readline_echoing_p
;
251 rl_redisplay_function
= tui_redisplay_readline
;
252 rl_deprep_term_function
= tui_deprep_terminal
;
253 rl_prep_term_function
= tui_prep_terminal
;
254 rl_getc_function
= tui_getc
;
255 readline_echoing_p
= 0;
256 rl_outstream
= tui_rl_outstream
;
259 /* Keep track of previous gdb output. */
260 tui_old_stdout
= gdb_stdout
;
261 tui_old_stderr
= gdb_stderr
;
262 tui_old_uiout
= uiout
;
264 /* Reconfigure gdb output. */
265 gdb_stdout
= tui_stdout
;
266 gdb_stderr
= tui_stderr
;
267 gdb_stdlog
= gdb_stdout
; /* for moment */
268 gdb_stdtarg
= gdb_stderr
; /* for moment */
273 /* Restore gdb output. */
274 gdb_stdout
= tui_old_stdout
;
275 gdb_stderr
= tui_old_stderr
;
276 gdb_stdlog
= gdb_stdout
; /* for moment */
277 gdb_stdtarg
= gdb_stderr
; /* for moment */
278 uiout
= tui_old_uiout
;
280 /* Restore readline. */
281 rl_redisplay_function
= tui_old_rl_redisplay_function
;
282 rl_deprep_term_function
= tui_old_rl_deprep_terminal
;
283 rl_prep_term_function
= tui_old_rl_prep_terminal
;
284 rl_getc_function
= tui_old_rl_getc_function
;
285 rl_outstream
= tui_old_rl_outstream
;
286 readline_echoing_p
= tui_old_readline_echoing_p
;
290 /* Initialize the IO for gdb in curses mode. */
294 /* Create tui output streams. */
295 tui_stdout
= tui_fileopen (stdout
);
296 tui_stderr
= tui_fileopen (stderr
);
297 tui_out
= tui_out_new (tui_stdout
);
299 /* Create the default UI. It is not created because we installed
301 uiout
= cli_out_new (gdb_stdout
);
303 /* Temporary solution for readline writing to stdout:
304 redirect readline output in a pipe, read that pipe and
305 output the content in the curses command window. */
306 if (pipe (tui_readline_pipe
) != 0)
308 fprintf_unfiltered (gdb_stderr
, "Cannot create pipe for readline");
311 tui_rl_outstream
= fdopen (tui_readline_pipe
[1], "w");
312 if (tui_rl_outstream
== 0)
314 fprintf_unfiltered (gdb_stderr
, "Cannot redirect readline output");
317 setlinebuf (tui_rl_outstream
);
320 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NONBLOCK
);
323 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NDELAY
);
327 add_file_handler (tui_readline_pipe
[0], tui_readline_output
, 0);
330 /* Get a character from the command window. This is called from the readline
338 w
= cmdWin
->generic
.handle
;
340 /* Flush readline output. */
341 tui_readline_output (GDB_READABLE
, 0);
344 ch
= _tuiHandleResizeDuringIO (ch
);
346 /* The \n must be echoed because it will not be printed by readline. */
349 /* When hitting return with an empty input, gdb executes the last
350 command. If we emit a newline, this fills up the command window
351 with empty lines with gdb prompt at beginning. Instead of that,
352 stay on the same line but provide a visual effect to show the
353 user we recognized the command. */
356 wmove (w
, cmdWin
->detail
.commandInfo
.curLine
, 0);
358 /* Clear the line. This will blink the gdb prompt since
359 it will be redrawn at the same line. */
366 wmove (w
, cmdWin
->detail
.commandInfo
.curLine
,
367 cmdWin
->detail
.commandInfo
.curch
);
372 if (m_isCommandChar (ch
))
373 { /* Handle prev/next/up/down here */
374 ch
= tuiDispatchCtrlChar (ch
);
377 if (ch
== '\n' || ch
== '\r' || ch
== '\f')
378 cmdWin
->detail
.commandInfo
.curch
= 0;
381 tuiIncrCommandCharCountBy (1);
383 if (ch
== KEY_BACKSPACE
)
390 /* Cleanup when a resize has occured.
391 Returns the character that must be processed. */
393 _tuiHandleResizeDuringIO (unsigned int originalCh
)
395 if (tuiWinResized ())
399 tuiSetWinResizedTo (FALSE
);