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 /* Use definition from readline 4.3. */
64 #define CTRL_CHAR(c) ((c) < control_character_threshold && (((c) & 0x80) == 0))
66 /* This file controls the IO interactions between gdb and curses.
67 When the TUI is enabled, gdb has two modes a curses and a standard
70 In curses mode, the gdb outputs are made in a curses command window.
71 For this, the gdb_stdout and gdb_stderr are redirected to the specific
72 ui_file implemented by TUI. The output is handled by tui_puts().
73 The input is also controlled by curses with tui_getc(). The readline
74 library uses this function to get its input. Several readline hooks
75 are installed to redirect readline output to the TUI (see also the
78 In normal mode, the gdb outputs are restored to their origin, that
79 is as if TUI is not used. Readline also uses its original getc()
82 Note: the current readline is not clean in its management of the output.
83 Even if we install a redisplay handler, it sometimes writes on a stdout
84 file. It is important to redirect every output produced by readline,
85 otherwise the curses window will be garbled. This is implemented with
86 a pipe that TUI reads and readline writes to. A gdb input handler
87 is created so that reading the pipe is handled automatically.
88 This will probably not work on non-Unix platforms. The best fix is
89 to make readline clean enougth so that is never write on stdout. */
91 /* TUI output files. */
92 static struct ui_file
*tui_stdout
;
93 static struct ui_file
*tui_stderr
;
94 static struct ui_out
*tui_out
;
96 /* GDB output files in non-curses mode. */
97 static struct ui_file
*tui_old_stdout
;
98 static struct ui_file
*tui_old_stderr
;
99 static struct ui_out
*tui_old_uiout
;
101 /* Readline previous hooks. */
102 static Function
*tui_old_rl_getc_function
;
103 static VFunction
*tui_old_rl_redisplay_function
;
104 static VFunction
*tui_old_rl_prep_terminal
;
105 static VFunction
*tui_old_rl_deprep_terminal
;
106 static int tui_old_readline_echoing_p
;
108 /* Readline output stream.
109 Should be removed when readline is clean. */
110 static FILE *tui_rl_outstream
;
111 static FILE *tui_old_rl_outstream
;
112 static int tui_readline_pipe
[2];
114 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
117 /* Print the string in the curses command window. */
119 tui_puts (const char *string
)
121 static int tui_skip_line
= -1;
125 w
= cmdWin
->generic
.handle
;
126 while ((c
= *string
++) != 0)
128 /* Catch annotation and discard them. We need two \032 and
129 discard until a \n is seen. */
134 else if (tui_skip_line
!= 1)
142 getyx (w
, cmdWin
->detail
.commandInfo
.curLine
,
143 cmdWin
->detail
.commandInfo
.curch
);
144 cmdWin
->detail
.commandInfo
.start_line
= cmdWin
->detail
.commandInfo
.curLine
;
146 /* We could defer the following. */
151 /* Readline callback.
152 Redisplay the command line with its prompt after readline has
153 changed the edited text. */
155 tui_redisplay_readline (void)
167 prompt
= get_prompt ();
171 w
= cmdWin
->generic
.handle
;
172 start_line
= cmdWin
->detail
.commandInfo
.start_line
;
173 wmove (w
, start_line
, 0);
176 for (in
= 0; prompt
&& prompt
[in
]; in
++)
178 waddch (w
, prompt
[in
]);
179 getyx (w
, line
, col
);
184 for (in
= 0; in
< rl_end
; in
++)
188 c
= (unsigned char) rl_line_buffer
[in
];
191 getyx (w
, c_line
, c_pos
);
194 if (CTRL_CHAR (c
) || c
== RUBOUT
)
197 waddch (w
, CTRL_CHAR (c
) ? UNCTRL (c
) : '?');
205 getyx (w
, cmdWin
->detail
.commandInfo
.start_line
,
206 cmdWin
->detail
.commandInfo
.curch
);
208 getyx (w
, line
, col
);
214 getyx (w
, cmdWin
->detail
.commandInfo
.start_line
,
215 cmdWin
->detail
.commandInfo
.curch
);
218 wmove (w
, c_line
, c_pos
);
219 cmdWin
->detail
.commandInfo
.curLine
= c_line
;
220 cmdWin
->detail
.commandInfo
.curch
= c_pos
;
222 cmdWin
->detail
.commandInfo
.start_line
-= height
- 1;
228 /* Readline callback to prepare the terminal. It is called once
229 each time we enter readline. There is nothing to do in curses mode. */
231 tui_prep_terminal (void)
235 /* Readline callback to restore the terminal. It is called once
236 each time we leave readline. There is nothing to do in curses mode. */
238 tui_deprep_terminal (void)
242 /* Read readline output pipe and feed the command window with it.
243 Should be removed when readline is clean. */
245 tui_readline_output (int code
, gdb_client_data data
)
250 size
= read (tui_readline_pipe
[0], buf
, sizeof (buf
) - 1);
251 if (size
> 0 && tui_active
)
258 /* Setup the IO for curses or non-curses mode.
259 - In non-curses mode, readline and gdb use the standard input and
260 standard output/error directly.
261 - In curses mode, the standard output/error is controlled by TUI
262 with the tui_stdout and tui_stderr. The output is redirected in
263 the curses command window. Several readline callbacks are installed
264 so that readline asks for its input to the curses command window
267 tui_setup_io (int mode
)
269 extern int readline_echoing_p
;
273 /* Redirect readline to TUI. */
274 tui_old_rl_redisplay_function
= rl_redisplay_function
;
275 tui_old_rl_deprep_terminal
= rl_deprep_term_function
;
276 tui_old_rl_prep_terminal
= rl_prep_term_function
;
277 tui_old_rl_getc_function
= rl_getc_function
;
278 tui_old_rl_outstream
= rl_outstream
;
279 tui_old_readline_echoing_p
= readline_echoing_p
;
280 rl_redisplay_function
= tui_redisplay_readline
;
281 rl_deprep_term_function
= tui_deprep_terminal
;
282 rl_prep_term_function
= tui_prep_terminal
;
283 rl_getc_function
= tui_getc
;
284 readline_echoing_p
= 0;
285 rl_outstream
= tui_rl_outstream
;
288 /* Keep track of previous gdb output. */
289 tui_old_stdout
= gdb_stdout
;
290 tui_old_stderr
= gdb_stderr
;
291 tui_old_uiout
= uiout
;
293 /* Reconfigure gdb output. */
294 gdb_stdout
= tui_stdout
;
295 gdb_stderr
= tui_stderr
;
296 gdb_stdlog
= gdb_stdout
; /* for moment */
297 gdb_stdtarg
= gdb_stderr
; /* for moment */
300 /* Save tty for SIGCONT. */
305 /* Restore gdb output. */
306 gdb_stdout
= tui_old_stdout
;
307 gdb_stderr
= tui_old_stderr
;
308 gdb_stdlog
= gdb_stdout
; /* for moment */
309 gdb_stdtarg
= gdb_stderr
; /* for moment */
310 uiout
= tui_old_uiout
;
312 /* Restore readline. */
313 rl_redisplay_function
= tui_old_rl_redisplay_function
;
314 rl_deprep_term_function
= tui_old_rl_deprep_terminal
;
315 rl_prep_term_function
= tui_old_rl_prep_terminal
;
316 rl_getc_function
= tui_old_rl_getc_function
;
317 rl_outstream
= tui_old_rl_outstream
;
318 readline_echoing_p
= tui_old_readline_echoing_p
;
320 /* Save tty for SIGCONT. */
326 /* Catch SIGCONT to restore the terminal and refresh the screen. */
328 tui_cont_sig (int sig
)
332 /* Restore the terminal setting because another process (shell)
333 might have changed it. */
336 /* Force a refresh of the screen. */
339 /* Update cursor position on the screen. */
340 wmove (cmdWin
->generic
.handle
,
341 cmdWin
->detail
.commandInfo
.start_line
,
342 cmdWin
->detail
.commandInfo
.curch
);
343 wrefresh (cmdWin
->generic
.handle
);
345 signal (sig
, tui_cont_sig
);
349 /* Initialize the IO for gdb in curses mode. */
354 signal (SIGCONT
, tui_cont_sig
);
357 /* Create tui output streams. */
358 tui_stdout
= tui_fileopen (stdout
);
359 tui_stderr
= tui_fileopen (stderr
);
360 tui_out
= tui_out_new (tui_stdout
);
362 /* Create the default UI. It is not created because we installed
364 uiout
= cli_out_new (gdb_stdout
);
366 /* Temporary solution for readline writing to stdout:
367 redirect readline output in a pipe, read that pipe and
368 output the content in the curses command window. */
369 if (pipe (tui_readline_pipe
) != 0)
371 fprintf_unfiltered (gdb_stderr
, "Cannot create pipe for readline");
374 tui_rl_outstream
= fdopen (tui_readline_pipe
[1], "w");
375 if (tui_rl_outstream
== 0)
377 fprintf_unfiltered (gdb_stderr
, "Cannot redirect readline output");
380 setlinebuf (tui_rl_outstream
);
383 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NONBLOCK
);
386 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NDELAY
);
390 add_file_handler (tui_readline_pipe
[0], tui_readline_output
, 0);
393 /* Get a character from the command window. This is called from the readline
401 w
= cmdWin
->generic
.handle
;
403 /* Flush readline output. */
404 tui_readline_output (GDB_READABLE
, 0);
407 ch
= _tuiHandleResizeDuringIO (ch
);
409 /* The \n must be echoed because it will not be printed by readline. */
412 /* When hitting return with an empty input, gdb executes the last
413 command. If we emit a newline, this fills up the command window
414 with empty lines with gdb prompt at beginning. Instead of that,
415 stay on the same line but provide a visual effect to show the
416 user we recognized the command. */
419 wmove (w
, cmdWin
->detail
.commandInfo
.curLine
, 0);
421 /* Clear the line. This will blink the gdb prompt since
422 it will be redrawn at the same line. */
429 wmove (w
, cmdWin
->detail
.commandInfo
.curLine
,
430 cmdWin
->detail
.commandInfo
.curch
);
435 if (m_isCommandChar (ch
))
436 { /* Handle prev/next/up/down here */
437 ch
= tuiDispatchCtrlChar (ch
);
440 if (ch
== '\n' || ch
== '\r' || ch
== '\f')
441 cmdWin
->detail
.commandInfo
.curch
= 0;
444 tuiIncrCommandCharCountBy (1);
446 if (ch
== KEY_BACKSPACE
)
453 /* Cleanup when a resize has occured.
454 Returns the character that must be processed. */
456 _tuiHandleResizeDuringIO (unsigned int originalCh
)
458 if (tuiWinResized ())
462 tuiSetWinResizedTo (FALSE
);