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)
67 key_is_command_char (int ch)
69 return ((ch == KEY_NPAGE) || (ch == KEY_PPAGE)
70 || (ch == KEY_LEFT) || (ch == KEY_RIGHT)
71 || (ch == KEY_UP) || (ch == KEY_DOWN)
72 || (ch == KEY_SF) || (ch == KEY_SR)
74 || key_is_start_sequence (ch));
77 /* Use definition from readline 4.3. */
79 #define CTRL_CHAR(c) \
80 ((c) < control_character_threshold && (((c) & 0x80) == 0))
82 /* This file controls the IO interactions between gdb and curses.
83 When the TUI is enabled, gdb has two modes a curses and a standard
86 In curses mode, the gdb outputs are made in a curses command
87 window. For this, the gdb_stdout and gdb_stderr are redirected to
88 the specific ui_file implemented by TUI. The output is handled by
89 tui_puts(). The input is also controlled by curses with
90 tui_getc(). The readline library uses this function to get its
91 input. Several readline hooks are installed to redirect readline
92 output to the TUI (see also the note below).
94 In normal mode, the gdb outputs are restored to their origin, that
95 is as if TUI is not used. Readline also uses its original getc()
98 Note SCz/2001-07-21: the current readline is not clean in its
99 management of the output. Even if we install a redisplay handler,
100 it sometimes writes on a stdout file. It is important to redirect
101 every output produced by readline, otherwise the curses window will
102 be garbled. This is implemented with a pipe that TUI reads and
103 readline writes to. A gdb input handler is created so that reading
104 the pipe is handled automatically. This will probably not work on
105 non-Unix platforms. The best fix is to make readline clean enougth
106 so that is never write on stdout.
108 Note SCz/2002-09-01: we now use more readline hooks and it seems
109 that with them we don't need the pipe anymore (verified by creating
110 the pipe and closing its end so that write causes a SIGPIPE). The
111 old pipe code is still there and can be conditionally removed by
112 #undef TUI_USE_PIPE_FOR_READLINE. */
114 /* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */
116 #define TUI_USE_PIPE_FOR_READLINE
118 /* #undef TUI_USE_PIPE_FOR_READLINE */
120 /* TUI output files. */
121 static struct ui_file *tui_stdout;
122 static struct ui_file *tui_stderr;
123 struct ui_out *tui_out;
125 /* GDB output files in non-curses mode. */
126 static struct ui_file *tui_old_stdout;
127 static struct ui_file *tui_old_stderr;
128 struct ui_out *tui_old_uiout;
130 /* Readline previous hooks. */
131 static rl_getc_func_t *tui_old_rl_getc_function;
132 static rl_voidfunc_t *tui_old_rl_redisplay_function;
133 static rl_vintfunc_t *tui_old_rl_prep_terminal;
134 static rl_voidfunc_t *tui_old_rl_deprep_terminal;
135 static int tui_old_rl_echoing_p;
137 /* Readline output stream.
138 Should be removed when readline is clean. */
139 static FILE *tui_rl_outstream;
140 static FILE *tui_old_rl_outstream;
141 #ifdef TUI_USE_PIPE_FOR_READLINE
142 static int tui_readline_pipe[2];
145 /* The last gdb prompt that was registered in readline.
146 This may be the main gdb prompt or a secondary prompt. */
147 static char *tui_rl_saved_prompt;
149 static int tui_handle_resize_during_io (int, int);
161 /* Print the string in the curses command window. */
163 tui_puts (const char *string)
165 static int tui_skip_line = -1;
169 w = TUI_CMD_WIN->generic.handle;
170 while ((c = *string++) != 0)
172 /* Catch annotation and discard them. We need two \032 and
173 discard until a \n is seen. */
178 else if (tui_skip_line != 1)
181 /* Expand TABs, since ncurses on MS-Windows doesn't. */
186 getyx (w, line, col);
191 } while ((col % 8) != 0);
199 getyx (w, TUI_CMD_WIN->detail.command_info.cur_line,
200 TUI_CMD_WIN->detail.command_info.curch);
201 TUI_CMD_WIN->detail.command_info.start_line
202 = TUI_CMD_WIN->detail.command_info.cur_line;
204 /* We could defer the following. */
209 /* Readline callback.
210 Redisplay the command line with its prompt after readline has
211 changed the edited text. */
213 tui_redisplay_readline (void)
225 /* Detect when we temporarily left SingleKey and now the readline
226 edit buffer is empty, automatically restore the SingleKey
227 mode. The restore must only be done if the command has finished.
228 The command could call prompt_for_continue and we must not
229 restore SingleKey so that the prompt and normal keymap are used. */
230 if (tui_current_key_mode == TUI_ONE_COMMAND_MODE && rl_end == 0
231 && immediate_quit == 0)
232 tui_set_key_mode (TUI_SINGLE_KEY_MODE);
234 if (tui_current_key_mode == TUI_SINGLE_KEY_MODE)
237 prompt = tui_rl_saved_prompt;
241 w = TUI_CMD_WIN->generic.handle;
242 start_line = TUI_CMD_WIN->detail.command_info.start_line;
243 wmove (w, start_line, 0);
246 for (in = 0; prompt && prompt[in]; in++)
248 waddch (w, prompt[in]);
249 getyx (w, line, col);
254 for (in = 0; in <= rl_end; in++)
260 getyx (w, c_line, c_pos);
266 c = (unsigned char) rl_line_buffer[in];
267 if (CTRL_CHAR (c) || c == RUBOUT)
270 waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
274 /* Expand TABs, since ncurses on MS-Windows doesn't. */
275 getyx (w, line, col);
280 } while ((col % 8) != 0);
288 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
289 TUI_CMD_WIN->detail.command_info.curch);
291 getyx (w, line, col);
297 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
298 TUI_CMD_WIN->detail.command_info.curch);
301 wmove (w, c_line, c_pos);
302 TUI_CMD_WIN->detail.command_info.cur_line = c_line;
303 TUI_CMD_WIN->detail.command_info.curch = c_pos;
305 TUI_CMD_WIN->detail.command_info.start_line -= height - 1;
311 /* Readline callback to prepare the terminal. It is called once each
312 time we enter readline. Terminal is already setup in curses
315 tui_prep_terminal (int notused1)
317 /* Save the prompt registered in readline to correctly display it.
318 (we can't use gdb_prompt() due to secondary prompts and can't use
319 rl_prompt because it points to an alloca buffer). */
320 xfree (tui_rl_saved_prompt);
321 tui_rl_saved_prompt = rl_prompt != NULL ? xstrdup (rl_prompt) : NULL;
324 /* Readline callback to restore the terminal. It is called once each
325 time we leave readline. There is nothing to do in curses mode. */
327 tui_deprep_terminal (void)
331 #ifdef TUI_USE_PIPE_FOR_READLINE
332 /* Read readline output pipe and feed the command window with it.
333 Should be removed when readline is clean. */
335 tui_readline_output (int error, gdb_client_data data)
340 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
341 if (size > 0 && tui_active)
349 /* TUI version of displayer.crlf. */
352 tui_mld_crlf (const struct match_list_displayer *displayer)
357 /* TUI version of displayer.putch. */
360 tui_mld_putch (const struct match_list_displayer *displayer, int ch)
365 /* TUI version of displayer.puts. */
368 tui_mld_puts (const struct match_list_displayer *displayer, const char *s)
373 /* TUI version of displayer.flush. */
376 tui_mld_flush (const struct match_list_displayer *displayer)
378 wrefresh (TUI_CMD_WIN->generic.handle);
381 /* TUI version of displayer.erase_entire_line. */
384 tui_mld_erase_entire_line (const struct match_list_displayer *displayer)
386 WINDOW *w = TUI_CMD_WIN->generic.handle;
388 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
390 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
393 /* TUI version of displayer.beep. */
396 tui_mld_beep (const struct match_list_displayer *displayer)
401 /* Helper function for tui_mld_read_key.
402 This temporarily replaces tui_getc for use during tab-completion
403 match list display. */
406 tui_mld_getc (FILE *fp)
408 WINDOW *w = TUI_CMD_WIN->generic.handle;
411 c = tui_handle_resize_during_io (c, 1);
416 /* TUI version of displayer.read_key. */
419 tui_mld_read_key (const struct match_list_displayer *displayer)
421 rl_getc_func_t *prev = rl_getc_function;
424 /* We can't use tui_getc as we need NEWLINE to not get emitted. */
425 rl_getc_function = tui_mld_getc;
427 rl_getc_function = prev;
431 /* TUI version of rl_completion_display_matches_hook.
432 See gdb_display_match_list for a description of the arguments. */
435 tui_rl_display_match_list (char **matches, int len, int max)
437 struct match_list_displayer displayer;
439 rl_get_screen_size (&displayer.height, &displayer.width);
440 displayer.crlf = tui_mld_crlf;
441 displayer.putch = tui_mld_putch;
442 displayer.puts = tui_mld_puts;
443 displayer.flush = tui_mld_flush;
444 displayer.erase_entire_line = tui_mld_erase_entire_line;
445 displayer.beep = tui_mld_beep;
446 displayer.read_key = tui_mld_read_key;
448 gdb_display_match_list (matches, len, max, &displayer);
451 /* Setup the IO for curses or non-curses mode.
452 - In non-curses mode, readline and gdb use the standard input and
453 standard output/error directly.
454 - In curses mode, the standard output/error is controlled by TUI
455 with the tui_stdout and tui_stderr. The output is redirected in
456 the curses command window. Several readline callbacks are installed
457 so that readline asks for its input to the curses command window
460 tui_setup_io (int mode)
462 extern int _rl_echoing_p;
466 /* Redirect readline to TUI. */
467 tui_old_rl_redisplay_function = rl_redisplay_function;
468 tui_old_rl_deprep_terminal = rl_deprep_term_function;
469 tui_old_rl_prep_terminal = rl_prep_term_function;
470 tui_old_rl_getc_function = rl_getc_function;
471 tui_old_rl_outstream = rl_outstream;
472 tui_old_rl_echoing_p = _rl_echoing_p;
473 rl_redisplay_function = tui_redisplay_readline;
474 rl_deprep_term_function = tui_deprep_terminal;
475 rl_prep_term_function = tui_prep_terminal;
476 rl_getc_function = tui_getc;
478 rl_outstream = tui_rl_outstream;
480 rl_completion_display_matches_hook = tui_rl_display_match_list;
481 rl_already_prompted = 0;
483 /* Keep track of previous gdb output. */
484 tui_old_stdout = gdb_stdout;
485 tui_old_stderr = gdb_stderr;
486 tui_old_uiout = current_uiout;
488 /* Reconfigure gdb output. */
489 gdb_stdout = tui_stdout;
490 gdb_stderr = tui_stderr;
491 gdb_stdlog = gdb_stdout; /* for moment */
492 gdb_stdtarg = gdb_stderr; /* for moment */
493 gdb_stdtargerr = gdb_stderr; /* for moment */
494 current_uiout = tui_out;
496 /* Save tty for SIGCONT. */
501 /* Restore gdb output. */
502 gdb_stdout = tui_old_stdout;
503 gdb_stderr = tui_old_stderr;
504 gdb_stdlog = gdb_stdout; /* for moment */
505 gdb_stdtarg = gdb_stderr; /* for moment */
506 gdb_stdtargerr = gdb_stderr; /* for moment */
507 current_uiout = tui_old_uiout;
509 /* Restore readline. */
510 rl_redisplay_function = tui_old_rl_redisplay_function;
511 rl_deprep_term_function = tui_old_rl_deprep_terminal;
512 rl_prep_term_function = tui_old_rl_prep_terminal;
513 rl_getc_function = tui_old_rl_getc_function;
514 rl_outstream = tui_old_rl_outstream;
515 rl_completion_display_matches_hook = 0;
516 _rl_echoing_p = tui_old_rl_echoing_p;
517 rl_already_prompted = 0;
519 /* Save tty for SIGCONT. */
525 /* Catch SIGCONT to restore the terminal and refresh the screen. */
527 tui_cont_sig (int sig)
531 /* Restore the terminal setting because another process (shell)
532 might have changed it. */
535 /* Force a refresh of the screen. */
536 tui_refresh_all_win ();
538 /* Update cursor position on the screen. */
539 wmove (TUI_CMD_WIN->generic.handle,
540 TUI_CMD_WIN->detail.command_info.start_line,
541 TUI_CMD_WIN->detail.command_info.curch);
542 wrefresh (TUI_CMD_WIN->generic.handle);
544 signal (sig, tui_cont_sig);
548 /* Initialize the IO for gdb in curses mode. */
550 tui_initialize_io (void)
553 signal (SIGCONT, tui_cont_sig);
556 /* Create tui output streams. */
557 tui_stdout = tui_fileopen (stdout);
558 tui_stderr = tui_fileopen (stderr);
559 tui_out = tui_out_new (tui_stdout);
561 /* Create the default UI. */
562 tui_old_uiout = cli_out_new (gdb_stdout);
564 #ifdef TUI_USE_PIPE_FOR_READLINE
565 /* Temporary solution for readline writing to stdout: redirect
566 readline output in a pipe, read that pipe and output the content
567 in the curses command window. */
568 if (gdb_pipe_cloexec (tui_readline_pipe) != 0)
569 error (_("Cannot create pipe for readline"));
571 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
572 if (tui_rl_outstream == 0)
573 error (_("Cannot redirect readline output"));
575 setvbuf (tui_rl_outstream, (char*) NULL, _IOLBF, 0);
578 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
581 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
584 add_file_handler (tui_readline_pipe[0], tui_readline_output, 0);
586 tui_rl_outstream = stdout;
590 /* Get a character from the command window. This is called from the
598 w = TUI_CMD_WIN->generic.handle;
600 #ifdef TUI_USE_PIPE_FOR_READLINE
601 /* Flush readline output. */
602 tui_readline_output (0, 0);
606 ch = tui_handle_resize_during_io (ch, 0);
608 /* The \n must be echoed because it will not be printed by
612 /* When hitting return with an empty input, gdb executes the last
613 command. If we emit a newline, this fills up the command window
614 with empty lines with gdb prompt at beginning. Instead of that,
615 stay on the same line but provide a visual effect to show the
616 user we recognized the command. */
619 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
621 /* Clear the line. This will blink the gdb prompt since
622 it will be redrawn at the same line. */
629 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line,
630 TUI_CMD_WIN->detail.command_info.curch);
635 if (key_is_command_char (ch))
636 { /* Handle prev/next/up/down here. */
637 ch = tui_dispatch_ctrl_char (ch);
640 if (ch == '\n' || ch == '\r' || ch == '\f')
641 TUI_CMD_WIN->detail.command_info.curch = 0;
642 if (ch == KEY_BACKSPACE)
645 if (async_command_editing_p && key_is_start_sequence (ch))
650 ch_pending = wgetch (w);
653 /* If we have pending input following a start sequence, call the stdin
654 event handler again because ncurses may have already read and stored
655 the input into its internal buffer, meaning that we won't get an stdin
656 event for it. If we don't compensate for this missed stdin event, key
657 sequences as Alt_F (^[f) will not behave promptly.
659 (We only compensates for the missed 2nd byte of a key sequence because
660 2-byte sequences are by far the most commonly used. ncurses may have
661 buffered a larger, 3+-byte key sequence though it remains to be seen
662 whether it is useful to compensate for all the bytes of such
664 if (ch_pending != ERR)
666 ungetch (ch_pending);
667 call_stdin_event_handler_again_p = 1;
674 /* Utility function to expand TABs in a STRING into spaces. STRING
675 will be displayed starting at column COL, and is assumed to include
676 no newlines. The returned expanded string is malloc'ed. */
679 tui_expand_tabs (const char *string, int col)
685 /* 1. How many additional characters do we need? */
686 for (n_adjust = 0, s = string; s; )
688 s = strpbrk (s, "\t");
691 col += (s - string) + n_adjust;
692 /* Adjustment for the next tab stop, minus one for the TAB
693 we replace with spaces. */
694 n_adjust += 8 - (col % 8) - 1;
699 /* Allocate the copy. */
700 ret = q = xmalloc (strlen (string) + n_adjust + 1);
702 /* 2. Copy the original string while replacing TABs with spaces. */
703 for (s = string; s; )
705 char *s1 = strpbrk (s, "\t");
710 strncpy (q, s, s1 - s);
717 } while ((col % 8) != 0);
728 /* Cleanup when a resize has occured.
729 Returns the character that must be processed. */
732 tui_handle_resize_during_io (int original_ch, int for_completion)
734 if (tui_win_resized ())
737 tui_refresh_all_win ();
738 tui_set_win_resized_to (FALSE);