Add max-completions parameter, and implement tab-completion limiting.
[deliverable/binutils-gdb.git] / gdb / tui / tui-io.c
1 /* TUI support I/O functions.
2
3 Copyright (C) 1998-2015 Free Software Foundation, Inc.
4
5 Contributed by Hewlett-Packard Company.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 #include "defs.h"
23 #include "target.h"
24 #include "event-loop.h"
25 #include "event-top.h"
26 #include "command.h"
27 #include "top.h"
28 #include "tui/tui.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"
35 #include "ui-out.h"
36 #include "cli-out.h"
37 #include <fcntl.h>
38 #include <signal.h>
39 #include "filestuff.h"
40 #include "completer.h"
41 #include "gdb_curses.h"
42
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
45 "gdb_curses.h". */
46 #include "readline/readline.h"
47
48 int
49 key_is_start_sequence (int ch)
50 {
51 return (ch == 27);
52 }
53
54 int
55 key_is_end_sequence (int ch)
56 {
57 return (ch == 126);
58 }
59
60 int
61 key_is_backspace (int ch)
62 {
63 return (ch == 8);
64 }
65
66 int
67 key_is_command_char (int ch)
68 {
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)
73 || (ch == (int)'\f')
74 || key_is_start_sequence (ch));
75 }
76
77 /* Use definition from readline 4.3. */
78 #undef CTRL_CHAR
79 #define CTRL_CHAR(c) \
80 ((c) < control_character_threshold && (((c) & 0x80) == 0))
81
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
84 mode.
85
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).
93
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()
96 function with stdin.
97
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.
107
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. */
113
114 /* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */
115 #ifdef HAVE_PIPE
116 #define TUI_USE_PIPE_FOR_READLINE
117 #endif
118 /* #undef TUI_USE_PIPE_FOR_READLINE */
119
120 /* TUI output files. */
121 static struct ui_file *tui_stdout;
122 static struct ui_file *tui_stderr;
123 struct ui_out *tui_out;
124
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;
129
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 rl_compdisp_func_t *tui_old_rl_display_matches_hook;
136 static int tui_old_rl_echoing_p;
137
138 /* Readline output stream.
139 Should be removed when readline is clean. */
140 static FILE *tui_rl_outstream;
141 static FILE *tui_old_rl_outstream;
142 #ifdef TUI_USE_PIPE_FOR_READLINE
143 static int tui_readline_pipe[2];
144 #endif
145
146 /* The last gdb prompt that was registered in readline.
147 This may be the main gdb prompt or a secondary prompt. */
148 static char *tui_rl_saved_prompt;
149
150 static int tui_handle_resize_during_io (int, int);
151
152 static void
153 tui_putc (char c)
154 {
155 char buf[2];
156
157 buf[0] = c;
158 buf[1] = 0;
159 tui_puts (buf);
160 }
161
162 /* Print the string in the curses command window. */
163 void
164 tui_puts (const char *string)
165 {
166 static int tui_skip_line = -1;
167 char c;
168 WINDOW *w;
169
170 w = TUI_CMD_WIN->generic.handle;
171 while ((c = *string++) != 0)
172 {
173 /* Catch annotation and discard them. We need two \032 and
174 discard until a \n is seen. */
175 if (c == '\032')
176 {
177 tui_skip_line++;
178 }
179 else if (tui_skip_line != 1)
180 {
181 tui_skip_line = -1;
182 /* Expand TABs, since ncurses on MS-Windows doesn't. */
183 if (c == '\t')
184 {
185 int line, col;
186
187 getyx (w, line, col);
188 do
189 {
190 waddch (w, ' ');
191 col++;
192 } while ((col % 8) != 0);
193 }
194 else
195 waddch (w, c);
196 }
197 else if (c == '\n')
198 tui_skip_line = -1;
199 }
200 getyx (w, TUI_CMD_WIN->detail.command_info.cur_line,
201 TUI_CMD_WIN->detail.command_info.curch);
202 TUI_CMD_WIN->detail.command_info.start_line
203 = TUI_CMD_WIN->detail.command_info.cur_line;
204
205 /* We could defer the following. */
206 wrefresh (w);
207 fflush (stdout);
208 }
209
210 /* Readline callback.
211 Redisplay the command line with its prompt after readline has
212 changed the edited text. */
213 void
214 tui_redisplay_readline (void)
215 {
216 int prev_col;
217 int height;
218 int col, line;
219 int c_pos;
220 int c_line;
221 int in;
222 WINDOW *w;
223 char *prompt;
224 int start_line;
225
226 /* Detect when we temporarily left SingleKey and now the readline
227 edit buffer is empty, automatically restore the SingleKey
228 mode. The restore must only be done if the command has finished.
229 The command could call prompt_for_continue and we must not
230 restore SingleKey so that the prompt and normal keymap are used. */
231 if (tui_current_key_mode == TUI_ONE_COMMAND_MODE && rl_end == 0
232 && immediate_quit == 0)
233 tui_set_key_mode (TUI_SINGLE_KEY_MODE);
234
235 if (tui_current_key_mode == TUI_SINGLE_KEY_MODE)
236 prompt = "";
237 else
238 prompt = tui_rl_saved_prompt;
239
240 c_pos = -1;
241 c_line = -1;
242 w = TUI_CMD_WIN->generic.handle;
243 start_line = TUI_CMD_WIN->detail.command_info.start_line;
244 wmove (w, start_line, 0);
245 prev_col = 0;
246 height = 1;
247 for (in = 0; prompt && prompt[in]; in++)
248 {
249 waddch (w, prompt[in]);
250 getyx (w, line, col);
251 if (col <= prev_col)
252 height++;
253 prev_col = col;
254 }
255 for (in = 0; in <= rl_end; in++)
256 {
257 unsigned char c;
258
259 if (in == rl_point)
260 {
261 getyx (w, c_line, c_pos);
262 }
263
264 if (in == rl_end)
265 break;
266
267 c = (unsigned char) rl_line_buffer[in];
268 if (CTRL_CHAR (c) || c == RUBOUT)
269 {
270 waddch (w, '^');
271 waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
272 }
273 else if (c == '\t')
274 {
275 /* Expand TABs, since ncurses on MS-Windows doesn't. */
276 getyx (w, line, col);
277 do
278 {
279 waddch (w, ' ');
280 col++;
281 } while ((col % 8) != 0);
282 }
283 else
284 {
285 waddch (w, c);
286 }
287 if (c == '\n')
288 {
289 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
290 TUI_CMD_WIN->detail.command_info.curch);
291 }
292 getyx (w, line, col);
293 if (col < prev_col)
294 height++;
295 prev_col = col;
296 }
297 wclrtobot (w);
298 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
299 TUI_CMD_WIN->detail.command_info.curch);
300 if (c_line >= 0)
301 {
302 wmove (w, c_line, c_pos);
303 TUI_CMD_WIN->detail.command_info.cur_line = c_line;
304 TUI_CMD_WIN->detail.command_info.curch = c_pos;
305 }
306 TUI_CMD_WIN->detail.command_info.start_line -= height - 1;
307
308 wrefresh (w);
309 fflush(stdout);
310 }
311
312 /* Readline callback to prepare the terminal. It is called once each
313 time we enter readline. Terminal is already setup in curses
314 mode. */
315 static void
316 tui_prep_terminal (int notused1)
317 {
318 /* Save the prompt registered in readline to correctly display it.
319 (we can't use gdb_prompt() due to secondary prompts and can't use
320 rl_prompt because it points to an alloca buffer). */
321 xfree (tui_rl_saved_prompt);
322 tui_rl_saved_prompt = rl_prompt != NULL ? xstrdup (rl_prompt) : NULL;
323 }
324
325 /* Readline callback to restore the terminal. It is called once each
326 time we leave readline. There is nothing to do in curses mode. */
327 static void
328 tui_deprep_terminal (void)
329 {
330 }
331
332 #ifdef TUI_USE_PIPE_FOR_READLINE
333 /* Read readline output pipe and feed the command window with it.
334 Should be removed when readline is clean. */
335 static void
336 tui_readline_output (int error, gdb_client_data data)
337 {
338 int size;
339 char buf[256];
340
341 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
342 if (size > 0 && tui_active)
343 {
344 buf[size] = 0;
345 tui_puts (buf);
346 }
347 }
348 #endif
349
350 /* TUI version of displayer.crlf. */
351
352 static void
353 tui_mld_crlf (const struct match_list_displayer *displayer)
354 {
355 tui_putc ('\n');
356 }
357
358 /* TUI version of displayer.putch. */
359
360 static void
361 tui_mld_putch (const struct match_list_displayer *displayer, int ch)
362 {
363 tui_putc (ch);
364 }
365
366 /* TUI version of displayer.puts. */
367
368 static void
369 tui_mld_puts (const struct match_list_displayer *displayer, const char *s)
370 {
371 tui_puts (s);
372 }
373
374 /* TUI version of displayer.flush. */
375
376 static void
377 tui_mld_flush (const struct match_list_displayer *displayer)
378 {
379 wrefresh (TUI_CMD_WIN->generic.handle);
380 }
381
382 /* TUI version of displayer.erase_entire_line. */
383
384 static void
385 tui_mld_erase_entire_line (const struct match_list_displayer *displayer)
386 {
387 WINDOW *w = TUI_CMD_WIN->generic.handle;
388
389 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
390 wclrtoeol (w);
391 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
392 }
393
394 /* TUI version of displayer.beep. */
395
396 static void
397 tui_mld_beep (const struct match_list_displayer *displayer)
398 {
399 beep ();
400 }
401
402 /* Helper function for tui_mld_read_key.
403 This temporarily replaces tui_getc for use during tab-completion
404 match list display. */
405
406 static int
407 tui_mld_getc (FILE *fp)
408 {
409 WINDOW *w = TUI_CMD_WIN->generic.handle;
410 int c = wgetch (w);
411
412 c = tui_handle_resize_during_io (c, 1);
413
414 return c;
415 }
416
417 /* TUI version of displayer.read_key. */
418
419 static int
420 tui_mld_read_key (const struct match_list_displayer *displayer)
421 {
422 rl_getc_func_t *prev = rl_getc_function;
423 int c;
424
425 /* We can't use tui_getc as we need NEWLINE to not get emitted. */
426 rl_getc_function = tui_mld_getc;
427 c = rl_read_key ();
428 rl_getc_function = prev;
429 return c;
430 }
431
432 /* TUI version of rl_completion_display_matches_hook.
433 See gdb_display_match_list for a description of the arguments. */
434
435 static void
436 tui_rl_display_match_list (char **matches, int len, int max)
437 {
438 struct match_list_displayer displayer;
439
440 rl_get_screen_size (&displayer.height, &displayer.width);
441 displayer.crlf = tui_mld_crlf;
442 displayer.putch = tui_mld_putch;
443 displayer.puts = tui_mld_puts;
444 displayer.flush = tui_mld_flush;
445 displayer.erase_entire_line = tui_mld_erase_entire_line;
446 displayer.beep = tui_mld_beep;
447 displayer.read_key = tui_mld_read_key;
448
449 gdb_display_match_list (matches, len, max, &displayer);
450 }
451
452 /* Setup the IO for curses or non-curses mode.
453 - In non-curses mode, readline and gdb use the standard input and
454 standard output/error directly.
455 - In curses mode, the standard output/error is controlled by TUI
456 with the tui_stdout and tui_stderr. The output is redirected in
457 the curses command window. Several readline callbacks are installed
458 so that readline asks for its input to the curses command window
459 with wgetch(). */
460 void
461 tui_setup_io (int mode)
462 {
463 extern int _rl_echoing_p;
464
465 if (mode)
466 {
467 /* Redirect readline to TUI. */
468 tui_old_rl_redisplay_function = rl_redisplay_function;
469 tui_old_rl_deprep_terminal = rl_deprep_term_function;
470 tui_old_rl_prep_terminal = rl_prep_term_function;
471 tui_old_rl_getc_function = rl_getc_function;
472 tui_old_rl_display_matches_hook = rl_completion_display_matches_hook;
473 tui_old_rl_outstream = rl_outstream;
474 tui_old_rl_echoing_p = _rl_echoing_p;
475 rl_redisplay_function = tui_redisplay_readline;
476 rl_deprep_term_function = tui_deprep_terminal;
477 rl_prep_term_function = tui_prep_terminal;
478 rl_getc_function = tui_getc;
479 _rl_echoing_p = 0;
480 rl_outstream = tui_rl_outstream;
481 rl_prompt = 0;
482 rl_completion_display_matches_hook = tui_rl_display_match_list;
483 rl_already_prompted = 0;
484
485 /* Keep track of previous gdb output. */
486 tui_old_stdout = gdb_stdout;
487 tui_old_stderr = gdb_stderr;
488 tui_old_uiout = current_uiout;
489
490 /* Reconfigure gdb output. */
491 gdb_stdout = tui_stdout;
492 gdb_stderr = tui_stderr;
493 gdb_stdlog = gdb_stdout; /* for moment */
494 gdb_stdtarg = gdb_stderr; /* for moment */
495 gdb_stdtargerr = gdb_stderr; /* for moment */
496 current_uiout = tui_out;
497
498 /* Save tty for SIGCONT. */
499 savetty ();
500 }
501 else
502 {
503 /* Restore gdb output. */
504 gdb_stdout = tui_old_stdout;
505 gdb_stderr = tui_old_stderr;
506 gdb_stdlog = gdb_stdout; /* for moment */
507 gdb_stdtarg = gdb_stderr; /* for moment */
508 gdb_stdtargerr = gdb_stderr; /* for moment */
509 current_uiout = tui_old_uiout;
510
511 /* Restore readline. */
512 rl_redisplay_function = tui_old_rl_redisplay_function;
513 rl_deprep_term_function = tui_old_rl_deprep_terminal;
514 rl_prep_term_function = tui_old_rl_prep_terminal;
515 rl_getc_function = tui_old_rl_getc_function;
516 rl_completion_display_matches_hook = tui_old_rl_display_matches_hook;
517 rl_outstream = tui_old_rl_outstream;
518 _rl_echoing_p = tui_old_rl_echoing_p;
519 rl_already_prompted = 0;
520
521 /* Save tty for SIGCONT. */
522 savetty ();
523 }
524 }
525
526 #ifdef SIGCONT
527 /* Catch SIGCONT to restore the terminal and refresh the screen. */
528 static void
529 tui_cont_sig (int sig)
530 {
531 if (tui_active)
532 {
533 /* Restore the terminal setting because another process (shell)
534 might have changed it. */
535 resetty ();
536
537 /* Force a refresh of the screen. */
538 tui_refresh_all_win ();
539
540 /* Update cursor position on the screen. */
541 wmove (TUI_CMD_WIN->generic.handle,
542 TUI_CMD_WIN->detail.command_info.start_line,
543 TUI_CMD_WIN->detail.command_info.curch);
544 wrefresh (TUI_CMD_WIN->generic.handle);
545 }
546 signal (sig, tui_cont_sig);
547 }
548 #endif
549
550 /* Initialize the IO for gdb in curses mode. */
551 void
552 tui_initialize_io (void)
553 {
554 #ifdef SIGCONT
555 signal (SIGCONT, tui_cont_sig);
556 #endif
557
558 /* Create tui output streams. */
559 tui_stdout = tui_fileopen (stdout);
560 tui_stderr = tui_fileopen (stderr);
561 tui_out = tui_out_new (tui_stdout);
562
563 /* Create the default UI. */
564 tui_old_uiout = cli_out_new (gdb_stdout);
565
566 #ifdef TUI_USE_PIPE_FOR_READLINE
567 /* Temporary solution for readline writing to stdout: redirect
568 readline output in a pipe, read that pipe and output the content
569 in the curses command window. */
570 if (gdb_pipe_cloexec (tui_readline_pipe) != 0)
571 error (_("Cannot create pipe for readline"));
572
573 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
574 if (tui_rl_outstream == 0)
575 error (_("Cannot redirect readline output"));
576
577 setvbuf (tui_rl_outstream, (char*) NULL, _IOLBF, 0);
578
579 #ifdef O_NONBLOCK
580 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
581 #else
582 #ifdef O_NDELAY
583 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
584 #endif
585 #endif
586 add_file_handler (tui_readline_pipe[0], tui_readline_output, 0);
587 #else
588 tui_rl_outstream = stdout;
589 #endif
590 }
591
592 /* Get a character from the command window. This is called from the
593 readline package. */
594 int
595 tui_getc (FILE *fp)
596 {
597 int ch;
598 WINDOW *w;
599
600 w = TUI_CMD_WIN->generic.handle;
601
602 #ifdef TUI_USE_PIPE_FOR_READLINE
603 /* Flush readline output. */
604 tui_readline_output (0, 0);
605 #endif
606
607 ch = wgetch (w);
608 ch = tui_handle_resize_during_io (ch, 0);
609
610 /* The \n must be echoed because it will not be printed by
611 readline. */
612 if (ch == '\n')
613 {
614 /* When hitting return with an empty input, gdb executes the last
615 command. If we emit a newline, this fills up the command window
616 with empty lines with gdb prompt at beginning. Instead of that,
617 stay on the same line but provide a visual effect to show the
618 user we recognized the command. */
619 if (rl_end == 0)
620 {
621 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
622
623 /* Clear the line. This will blink the gdb prompt since
624 it will be redrawn at the same line. */
625 wclrtoeol (w);
626 wrefresh (w);
627 napms (20);
628 }
629 else
630 {
631 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line,
632 TUI_CMD_WIN->detail.command_info.curch);
633 waddch (w, ch);
634 }
635 }
636
637 if (key_is_command_char (ch))
638 { /* Handle prev/next/up/down here. */
639 ch = tui_dispatch_ctrl_char (ch);
640 }
641
642 if (ch == '\n' || ch == '\r' || ch == '\f')
643 TUI_CMD_WIN->detail.command_info.curch = 0;
644 if (ch == KEY_BACKSPACE)
645 return '\b';
646
647 if (async_command_editing_p && key_is_start_sequence (ch))
648 {
649 int ch_pending;
650
651 nodelay (w, TRUE);
652 ch_pending = wgetch (w);
653 nodelay (w, FALSE);
654
655 /* If we have pending input following a start sequence, call the stdin
656 event handler again because ncurses may have already read and stored
657 the input into its internal buffer, meaning that we won't get an stdin
658 event for it. If we don't compensate for this missed stdin event, key
659 sequences as Alt_F (^[f) will not behave promptly.
660
661 (We only compensates for the missed 2nd byte of a key sequence because
662 2-byte sequences are by far the most commonly used. ncurses may have
663 buffered a larger, 3+-byte key sequence though it remains to be seen
664 whether it is useful to compensate for all the bytes of such
665 sequences.) */
666 if (ch_pending != ERR)
667 {
668 ungetch (ch_pending);
669 call_stdin_event_handler_again_p = 1;
670 }
671 }
672
673 return ch;
674 }
675
676 /* Utility function to expand TABs in a STRING into spaces. STRING
677 will be displayed starting at column COL, and is assumed to include
678 no newlines. The returned expanded string is malloc'ed. */
679
680 char *
681 tui_expand_tabs (const char *string, int col)
682 {
683 int n_adjust;
684 const char *s;
685 char *ret, *q;
686
687 /* 1. How many additional characters do we need? */
688 for (n_adjust = 0, s = string; s; )
689 {
690 s = strpbrk (s, "\t");
691 if (s)
692 {
693 col += (s - string) + n_adjust;
694 /* Adjustment for the next tab stop, minus one for the TAB
695 we replace with spaces. */
696 n_adjust += 8 - (col % 8) - 1;
697 s++;
698 }
699 }
700
701 /* Allocate the copy. */
702 ret = q = xmalloc (strlen (string) + n_adjust + 1);
703
704 /* 2. Copy the original string while replacing TABs with spaces. */
705 for (s = string; s; )
706 {
707 char *s1 = strpbrk (s, "\t");
708 if (s1)
709 {
710 if (s1 > s)
711 {
712 strncpy (q, s, s1 - s);
713 q += s1 - s;
714 col += s1 - s;
715 }
716 do {
717 *q++ = ' ';
718 col++;
719 } while ((col % 8) != 0);
720 s1++;
721 }
722 else
723 strcpy (q, s);
724 s = s1;
725 }
726
727 return ret;
728 }
729
730 /* Cleanup when a resize has occured.
731 Returns the character that must be processed. */
732
733 static int
734 tui_handle_resize_during_io (int original_ch, int for_completion)
735 {
736 if (tui_win_resized ())
737 {
738 tui_resize_all ();
739 tui_refresh_all_win ();
740 tui_set_win_resized_to (FALSE);
741 if (!for_completion)
742 {
743 dont_repeat ();
744 return '\n';
745 }
746 }
747
748 return original_ch;
749 }
This page took 0.064349 seconds and 4 git commands to generate.