1 /* TUI support I/O functions.
3 Copyright (C) 1998-2015 Free Software Foundation, Inc.
5 Contributed by Hewlett-Packard Company.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "event-loop.h"
25 #include "event-top.h"
29 #include "tui/tui-data.h"
30 #include "tui/tui-io.h"
31 #include "tui/tui-command.h"
32 #include "tui/tui-win.h"
33 #include "tui/tui-wingeneral.h"
34 #include "tui/tui-file.h"
39 #include "filestuff.h"
40 #include "completer.h"
41 #include "gdb_curses.h"
43 /* This redefines CTRL if it is not already defined, so it must come
44 after terminal state releated include files like <term.h> and
46 #include "readline/readline.h"
49 key_is_start_sequence (int ch
)
55 key_is_end_sequence (int ch
)
61 key_is_backspace (int ch
)
66 /* Use definition from readline 4.3. */
68 #define CTRL_CHAR(c) \
69 ((c) < control_character_threshold && (((c) & 0x80) == 0))
71 /* This file controls the IO interactions between gdb and curses.
72 When the TUI is enabled, gdb has two modes a curses and a standard
75 In curses mode, the gdb outputs are made in a curses command
76 window. For this, the gdb_stdout and gdb_stderr are redirected to
77 the specific ui_file implemented by TUI. The output is handled by
78 tui_puts(). The input is also controlled by curses with
79 tui_getc(). The readline library uses this function to get its
80 input. Several readline hooks are installed to redirect readline
81 output to the TUI (see also the note below).
83 In normal mode, the gdb outputs are restored to their origin, that
84 is as if TUI is not used. Readline also uses its original getc()
87 Note SCz/2001-07-21: the current readline is not clean in its
88 management of the output. Even if we install a redisplay handler,
89 it sometimes writes on a stdout file. It is important to redirect
90 every output produced by readline, otherwise the curses window will
91 be garbled. This is implemented with a pipe that TUI reads and
92 readline writes to. A gdb input handler is created so that reading
93 the pipe is handled automatically. This will probably not work on
94 non-Unix platforms. The best fix is to make readline clean enougth
95 so that is never write on stdout.
97 Note SCz/2002-09-01: we now use more readline hooks and it seems
98 that with them we don't need the pipe anymore (verified by creating
99 the pipe and closing its end so that write causes a SIGPIPE). The
100 old pipe code is still there and can be conditionally removed by
101 #undef TUI_USE_PIPE_FOR_READLINE. */
103 /* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */
105 #define TUI_USE_PIPE_FOR_READLINE
107 /* #undef TUI_USE_PIPE_FOR_READLINE */
109 /* TUI output files. */
110 static struct ui_file
*tui_stdout
;
111 static struct ui_file
*tui_stderr
;
112 struct ui_out
*tui_out
;
114 /* GDB output files in non-curses mode. */
115 static struct ui_file
*tui_old_stdout
;
116 static struct ui_file
*tui_old_stderr
;
117 struct ui_out
*tui_old_uiout
;
119 /* Readline previous hooks. */
120 static rl_getc_func_t
*tui_old_rl_getc_function
;
121 static rl_voidfunc_t
*tui_old_rl_redisplay_function
;
122 static rl_vintfunc_t
*tui_old_rl_prep_terminal
;
123 static rl_voidfunc_t
*tui_old_rl_deprep_terminal
;
124 static rl_compdisp_func_t
*tui_old_rl_display_matches_hook
;
125 static int tui_old_rl_echoing_p
;
127 /* Readline output stream.
128 Should be removed when readline is clean. */
129 static FILE *tui_rl_outstream
;
130 static FILE *tui_old_rl_outstream
;
131 #ifdef TUI_USE_PIPE_FOR_READLINE
132 static int tui_readline_pipe
[2];
135 /* The last gdb prompt that was registered in readline.
136 This may be the main gdb prompt or a secondary prompt. */
137 static char *tui_rl_saved_prompt
;
149 /* Print the string in the curses command window.
150 The output is buffered. It is up to the caller to refresh the screen
154 tui_puts (const char *string
)
156 static int tui_skip_line
= -1;
160 w
= TUI_CMD_WIN
->generic
.handle
;
161 while ((c
= *string
++) != 0)
163 /* Catch annotation and discard them. We need two \032 and
164 discard until a \n is seen. */
169 else if (tui_skip_line
!= 1)
172 /* Expand TABs, since ncurses on MS-Windows doesn't. */
177 getyx (w
, line
, col
);
182 } while ((col
% 8) != 0);
190 getyx (w
, TUI_CMD_WIN
->detail
.command_info
.cur_line
,
191 TUI_CMD_WIN
->detail
.command_info
.curch
);
192 TUI_CMD_WIN
->detail
.command_info
.start_line
193 = TUI_CMD_WIN
->detail
.command_info
.cur_line
;
196 /* Readline callback.
197 Redisplay the command line with its prompt after readline has
198 changed the edited text. */
200 tui_redisplay_readline (void)
212 /* Detect when we temporarily left SingleKey and now the readline
213 edit buffer is empty, automatically restore the SingleKey
214 mode. The restore must only be done if the command has finished.
215 The command could call prompt_for_continue and we must not
216 restore SingleKey so that the prompt and normal keymap are used. */
217 if (tui_current_key_mode
== TUI_ONE_COMMAND_MODE
&& rl_end
== 0
218 && immediate_quit
== 0)
219 tui_set_key_mode (TUI_SINGLE_KEY_MODE
);
221 if (tui_current_key_mode
== TUI_SINGLE_KEY_MODE
)
224 prompt
= tui_rl_saved_prompt
;
228 w
= TUI_CMD_WIN
->generic
.handle
;
229 start_line
= TUI_CMD_WIN
->detail
.command_info
.start_line
;
230 wmove (w
, start_line
, 0);
233 for (in
= 0; prompt
&& prompt
[in
]; in
++)
235 waddch (w
, prompt
[in
]);
236 getyx (w
, line
, col
);
241 for (in
= 0; in
<= rl_end
; in
++)
247 getyx (w
, c_line
, c_pos
);
253 c
= (unsigned char) rl_line_buffer
[in
];
254 if (CTRL_CHAR (c
) || c
== RUBOUT
)
257 waddch (w
, CTRL_CHAR (c
) ? UNCTRL (c
) : '?');
261 /* Expand TABs, since ncurses on MS-Windows doesn't. */
262 getyx (w
, line
, col
);
267 } while ((col
% 8) != 0);
275 getyx (w
, TUI_CMD_WIN
->detail
.command_info
.start_line
,
276 TUI_CMD_WIN
->detail
.command_info
.curch
);
278 getyx (w
, line
, col
);
284 getyx (w
, TUI_CMD_WIN
->detail
.command_info
.start_line
,
285 TUI_CMD_WIN
->detail
.command_info
.curch
);
288 wmove (w
, c_line
, c_pos
);
289 TUI_CMD_WIN
->detail
.command_info
.cur_line
= c_line
;
290 TUI_CMD_WIN
->detail
.command_info
.curch
= c_pos
;
292 TUI_CMD_WIN
->detail
.command_info
.start_line
-= height
- 1;
298 /* Readline callback to prepare the terminal. It is called once each
299 time we enter readline. Terminal is already setup in curses
302 tui_prep_terminal (int notused1
)
304 /* Save the prompt registered in readline to correctly display it.
305 (we can't use gdb_prompt() due to secondary prompts and can't use
306 rl_prompt because it points to an alloca buffer). */
307 xfree (tui_rl_saved_prompt
);
308 tui_rl_saved_prompt
= rl_prompt
!= NULL
? xstrdup (rl_prompt
) : NULL
;
311 /* Readline callback to restore the terminal. It is called once each
312 time we leave readline. There is nothing to do in curses mode. */
314 tui_deprep_terminal (void)
318 #ifdef TUI_USE_PIPE_FOR_READLINE
319 /* Read readline output pipe and feed the command window with it.
320 Should be removed when readline is clean. */
322 tui_readline_output (int error
, gdb_client_data data
)
327 size
= read (tui_readline_pipe
[0], buf
, sizeof (buf
) - 1);
328 if (size
> 0 && tui_active
)
336 /* TUI version of displayer.crlf. */
339 tui_mld_crlf (const struct match_list_displayer
*displayer
)
344 /* TUI version of displayer.putch. */
347 tui_mld_putch (const struct match_list_displayer
*displayer
, int ch
)
352 /* TUI version of displayer.puts. */
355 tui_mld_puts (const struct match_list_displayer
*displayer
, const char *s
)
360 /* TUI version of displayer.flush. */
363 tui_mld_flush (const struct match_list_displayer
*displayer
)
365 wrefresh (TUI_CMD_WIN
->generic
.handle
);
368 /* TUI version of displayer.erase_entire_line. */
371 tui_mld_erase_entire_line (const struct match_list_displayer
*displayer
)
373 WINDOW
*w
= TUI_CMD_WIN
->generic
.handle
;
375 wmove (w
, TUI_CMD_WIN
->detail
.command_info
.cur_line
, 0);
377 wmove (w
, TUI_CMD_WIN
->detail
.command_info
.cur_line
, 0);
380 /* TUI version of displayer.beep. */
383 tui_mld_beep (const struct match_list_displayer
*displayer
)
388 /* Helper function for tui_mld_read_key.
389 This temporarily replaces tui_getc for use during tab-completion
390 match list display. */
393 tui_mld_getc (FILE *fp
)
395 WINDOW
*w
= TUI_CMD_WIN
->generic
.handle
;
401 /* TUI version of displayer.read_key. */
404 tui_mld_read_key (const struct match_list_displayer
*displayer
)
406 rl_getc_func_t
*prev
= rl_getc_function
;
409 /* We can't use tui_getc as we need NEWLINE to not get emitted. */
410 rl_getc_function
= tui_mld_getc
;
412 rl_getc_function
= prev
;
416 /* TUI version of rl_completion_display_matches_hook.
417 See gdb_display_match_list for a description of the arguments. */
420 tui_rl_display_match_list (char **matches
, int len
, int max
)
422 struct match_list_displayer displayer
;
424 rl_get_screen_size (&displayer
.height
, &displayer
.width
);
425 displayer
.crlf
= tui_mld_crlf
;
426 displayer
.putch
= tui_mld_putch
;
427 displayer
.puts
= tui_mld_puts
;
428 displayer
.flush
= tui_mld_flush
;
429 displayer
.erase_entire_line
= tui_mld_erase_entire_line
;
430 displayer
.beep
= tui_mld_beep
;
431 displayer
.read_key
= tui_mld_read_key
;
433 gdb_display_match_list (matches
, len
, max
, &displayer
);
436 /* Setup the IO for curses or non-curses mode.
437 - In non-curses mode, readline and gdb use the standard input and
438 standard output/error directly.
439 - In curses mode, the standard output/error is controlled by TUI
440 with the tui_stdout and tui_stderr. The output is redirected in
441 the curses command window. Several readline callbacks are installed
442 so that readline asks for its input to the curses command window
445 tui_setup_io (int mode
)
447 extern int _rl_echoing_p
;
451 /* Redirect readline to TUI. */
452 tui_old_rl_redisplay_function
= rl_redisplay_function
;
453 tui_old_rl_deprep_terminal
= rl_deprep_term_function
;
454 tui_old_rl_prep_terminal
= rl_prep_term_function
;
455 tui_old_rl_getc_function
= rl_getc_function
;
456 tui_old_rl_display_matches_hook
= rl_completion_display_matches_hook
;
457 tui_old_rl_outstream
= rl_outstream
;
458 tui_old_rl_echoing_p
= _rl_echoing_p
;
459 rl_redisplay_function
= tui_redisplay_readline
;
460 rl_deprep_term_function
= tui_deprep_terminal
;
461 rl_prep_term_function
= tui_prep_terminal
;
462 rl_getc_function
= tui_getc
;
464 rl_outstream
= tui_rl_outstream
;
466 rl_completion_display_matches_hook
= tui_rl_display_match_list
;
467 rl_already_prompted
= 0;
469 /* Keep track of previous gdb output. */
470 tui_old_stdout
= gdb_stdout
;
471 tui_old_stderr
= gdb_stderr
;
472 tui_old_uiout
= current_uiout
;
474 /* Reconfigure gdb output. */
475 gdb_stdout
= tui_stdout
;
476 gdb_stderr
= tui_stderr
;
477 gdb_stdlog
= gdb_stdout
; /* for moment */
478 gdb_stdtarg
= gdb_stderr
; /* for moment */
479 gdb_stdtargerr
= gdb_stderr
; /* for moment */
480 current_uiout
= tui_out
;
482 /* Save tty for SIGCONT. */
487 /* Restore gdb output. */
488 gdb_stdout
= tui_old_stdout
;
489 gdb_stderr
= tui_old_stderr
;
490 gdb_stdlog
= gdb_stdout
; /* for moment */
491 gdb_stdtarg
= gdb_stderr
; /* for moment */
492 gdb_stdtargerr
= gdb_stderr
; /* for moment */
493 current_uiout
= tui_old_uiout
;
495 /* Restore readline. */
496 rl_redisplay_function
= tui_old_rl_redisplay_function
;
497 rl_deprep_term_function
= tui_old_rl_deprep_terminal
;
498 rl_prep_term_function
= tui_old_rl_prep_terminal
;
499 rl_getc_function
= tui_old_rl_getc_function
;
500 rl_completion_display_matches_hook
= tui_old_rl_display_matches_hook
;
501 rl_outstream
= tui_old_rl_outstream
;
502 _rl_echoing_p
= tui_old_rl_echoing_p
;
503 rl_already_prompted
= 0;
505 /* Save tty for SIGCONT. */
511 /* Catch SIGCONT to restore the terminal and refresh the screen. */
513 tui_cont_sig (int sig
)
517 /* Restore the terminal setting because another process (shell)
518 might have changed it. */
521 /* Force a refresh of the screen. */
522 tui_refresh_all_win ();
524 /* Update cursor position on the screen. */
525 wmove (TUI_CMD_WIN
->generic
.handle
,
526 TUI_CMD_WIN
->detail
.command_info
.start_line
,
527 TUI_CMD_WIN
->detail
.command_info
.curch
);
528 wrefresh (TUI_CMD_WIN
->generic
.handle
);
530 signal (sig
, tui_cont_sig
);
534 /* Initialize the IO for gdb in curses mode. */
536 tui_initialize_io (void)
539 signal (SIGCONT
, tui_cont_sig
);
542 /* Create tui output streams. */
543 tui_stdout
= tui_fileopen (stdout
);
544 tui_stderr
= tui_fileopen (stderr
);
545 tui_out
= tui_out_new (tui_stdout
);
547 /* Create the default UI. */
548 tui_old_uiout
= cli_out_new (gdb_stdout
);
550 #ifdef TUI_USE_PIPE_FOR_READLINE
551 /* Temporary solution for readline writing to stdout: redirect
552 readline output in a pipe, read that pipe and output the content
553 in the curses command window. */
554 if (gdb_pipe_cloexec (tui_readline_pipe
) != 0)
555 error (_("Cannot create pipe for readline"));
557 tui_rl_outstream
= fdopen (tui_readline_pipe
[1], "w");
558 if (tui_rl_outstream
== 0)
559 error (_("Cannot redirect readline output"));
561 setvbuf (tui_rl_outstream
, (char*) NULL
, _IOLBF
, 0);
564 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NONBLOCK
);
567 (void) fcntl (tui_readline_pipe
[0], F_SETFL
, O_NDELAY
);
570 add_file_handler (tui_readline_pipe
[0], tui_readline_output
, 0);
572 tui_rl_outstream
= stdout
;
576 /* Get a character from the command window. This is called from the
584 w
= TUI_CMD_WIN
->generic
.handle
;
586 #ifdef TUI_USE_PIPE_FOR_READLINE
587 /* Flush readline output. */
588 tui_readline_output (0, 0);
593 /* The \n must be echoed because it will not be printed by
597 /* When hitting return with an empty input, gdb executes the last
598 command. If we emit a newline, this fills up the command window
599 with empty lines with gdb prompt at beginning. Instead of that,
600 stay on the same line but provide a visual effect to show the
601 user we recognized the command. */
604 wmove (w
, TUI_CMD_WIN
->detail
.command_info
.cur_line
, 0);
606 /* Clear the line. This will blink the gdb prompt since
607 it will be redrawn at the same line. */
614 /* Move cursor to the end of the command line before emitting the
615 newline. We need to do so because when ncurses outputs a newline
616 it truncates any text that appears past the end of the cursor. */
617 int px
= TUI_CMD_WIN
->detail
.command_info
.curch
;
618 int py
= TUI_CMD_WIN
->detail
.command_info
.cur_line
;
619 px
+= rl_end
- rl_point
;
620 py
+= px
/ TUI_CMD_WIN
->generic
.width
;
621 px
%= TUI_CMD_WIN
->generic
.width
;
627 /* Handle prev/next/up/down here. */
628 ch
= tui_dispatch_ctrl_char (ch
);
630 if (ch
== '\n' || ch
== '\r' || ch
== '\f')
631 TUI_CMD_WIN
->detail
.command_info
.curch
= 0;
632 if (ch
== KEY_BACKSPACE
)
635 if (async_command_editing_p
&& key_is_start_sequence (ch
))
640 ch_pending
= wgetch (w
);
643 /* If we have pending input following a start sequence, call the stdin
644 event handler again because ncurses may have already read and stored
645 the input into its internal buffer, meaning that we won't get an stdin
646 event for it. If we don't compensate for this missed stdin event, key
647 sequences as Alt_F (^[f) will not behave promptly.
649 (We only compensates for the missed 2nd byte of a key sequence because
650 2-byte sequences are by far the most commonly used. ncurses may have
651 buffered a larger, 3+-byte key sequence though it remains to be seen
652 whether it is useful to compensate for all the bytes of such
654 if (ch_pending
!= ERR
)
656 ungetch (ch_pending
);
657 call_stdin_event_handler_again_p
= 1;
664 /* Utility function to expand TABs in a STRING into spaces. STRING
665 will be displayed starting at column COL, and is assumed to include
666 no newlines. The returned expanded string is malloc'ed. */
669 tui_expand_tabs (const char *string
, int col
)
675 /* 1. How many additional characters do we need? */
676 for (ncol
= col
, n_adjust
= 0, s
= string
; s
; )
678 s
= strpbrk (s
, "\t");
681 ncol
+= (s
- string
) + n_adjust
;
682 /* Adjustment for the next tab stop, minus one for the TAB
683 we replace with spaces. */
684 n_adjust
+= 8 - (ncol
% 8) - 1;
689 /* Allocate the copy. */
690 ret
= q
= xmalloc (strlen (string
) + n_adjust
+ 1);
692 /* 2. Copy the original string while replacing TABs with spaces. */
693 for (ncol
= col
, s
= string
; s
; )
695 const char *s1
= strpbrk (s
, "\t");
700 strncpy (q
, s
, s1
- s
);
707 } while ((ncol
% 8) != 0);