Fix undefined behavior in TUI's TAB expansion
[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 /* Use definition from readline 4.3. */
67 #undef CTRL_CHAR
68 #define CTRL_CHAR(c) \
69 ((c) < control_character_threshold && (((c) & 0x80) == 0))
70
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
73 mode.
74
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).
82
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()
85 function with stdin.
86
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.
96
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. */
102
103 /* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */
104 #ifdef HAVE_PIPE
105 #define TUI_USE_PIPE_FOR_READLINE
106 #endif
107 /* #undef TUI_USE_PIPE_FOR_READLINE */
108
109 /* TUI output files. */
110 static struct ui_file *tui_stdout;
111 static struct ui_file *tui_stderr;
112 struct ui_out *tui_out;
113
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;
118
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;
126
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];
133 #endif
134
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;
138
139 static void
140 tui_putc (char c)
141 {
142 char buf[2];
143
144 buf[0] = c;
145 buf[1] = 0;
146 tui_puts (buf);
147 }
148
149 /* Print the string in the curses command window.
150 The output is buffered. It is up to the caller to refresh the screen
151 if necessary. */
152
153 void
154 tui_puts (const char *string)
155 {
156 static int tui_skip_line = -1;
157 char c;
158 WINDOW *w;
159
160 w = TUI_CMD_WIN->generic.handle;
161 while ((c = *string++) != 0)
162 {
163 /* Catch annotation and discard them. We need two \032 and
164 discard until a \n is seen. */
165 if (c == '\032')
166 {
167 tui_skip_line++;
168 }
169 else if (tui_skip_line != 1)
170 {
171 tui_skip_line = -1;
172 /* Expand TABs, since ncurses on MS-Windows doesn't. */
173 if (c == '\t')
174 {
175 int line, col;
176
177 getyx (w, line, col);
178 do
179 {
180 waddch (w, ' ');
181 col++;
182 } while ((col % 8) != 0);
183 }
184 else
185 waddch (w, c);
186 }
187 else if (c == '\n')
188 tui_skip_line = -1;
189 }
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;
194 }
195
196 /* Readline callback.
197 Redisplay the command line with its prompt after readline has
198 changed the edited text. */
199 void
200 tui_redisplay_readline (void)
201 {
202 int prev_col;
203 int height;
204 int col, line;
205 int c_pos;
206 int c_line;
207 int in;
208 WINDOW *w;
209 char *prompt;
210 int start_line;
211
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);
220
221 if (tui_current_key_mode == TUI_SINGLE_KEY_MODE)
222 prompt = "";
223 else
224 prompt = tui_rl_saved_prompt;
225
226 c_pos = -1;
227 c_line = -1;
228 w = TUI_CMD_WIN->generic.handle;
229 start_line = TUI_CMD_WIN->detail.command_info.start_line;
230 wmove (w, start_line, 0);
231 prev_col = 0;
232 height = 1;
233 for (in = 0; prompt && prompt[in]; in++)
234 {
235 waddch (w, prompt[in]);
236 getyx (w, line, col);
237 if (col <= prev_col)
238 height++;
239 prev_col = col;
240 }
241 for (in = 0; in <= rl_end; in++)
242 {
243 unsigned char c;
244
245 if (in == rl_point)
246 {
247 getyx (w, c_line, c_pos);
248 }
249
250 if (in == rl_end)
251 break;
252
253 c = (unsigned char) rl_line_buffer[in];
254 if (CTRL_CHAR (c) || c == RUBOUT)
255 {
256 waddch (w, '^');
257 waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
258 }
259 else if (c == '\t')
260 {
261 /* Expand TABs, since ncurses on MS-Windows doesn't. */
262 getyx (w, line, col);
263 do
264 {
265 waddch (w, ' ');
266 col++;
267 } while ((col % 8) != 0);
268 }
269 else
270 {
271 waddch (w, c);
272 }
273 if (c == '\n')
274 {
275 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
276 TUI_CMD_WIN->detail.command_info.curch);
277 }
278 getyx (w, line, col);
279 if (col < prev_col)
280 height++;
281 prev_col = col;
282 }
283 wclrtobot (w);
284 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
285 TUI_CMD_WIN->detail.command_info.curch);
286 if (c_line >= 0)
287 {
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;
291 }
292 TUI_CMD_WIN->detail.command_info.start_line -= height - 1;
293
294 wrefresh (w);
295 fflush(stdout);
296 }
297
298 /* Readline callback to prepare the terminal. It is called once each
299 time we enter readline. Terminal is already setup in curses
300 mode. */
301 static void
302 tui_prep_terminal (int notused1)
303 {
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;
309 }
310
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. */
313 static void
314 tui_deprep_terminal (void)
315 {
316 }
317
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. */
321 static void
322 tui_readline_output (int error, gdb_client_data data)
323 {
324 int size;
325 char buf[256];
326
327 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
328 if (size > 0 && tui_active)
329 {
330 buf[size] = 0;
331 tui_puts (buf);
332 }
333 }
334 #endif
335
336 /* TUI version of displayer.crlf. */
337
338 static void
339 tui_mld_crlf (const struct match_list_displayer *displayer)
340 {
341 tui_putc ('\n');
342 }
343
344 /* TUI version of displayer.putch. */
345
346 static void
347 tui_mld_putch (const struct match_list_displayer *displayer, int ch)
348 {
349 tui_putc (ch);
350 }
351
352 /* TUI version of displayer.puts. */
353
354 static void
355 tui_mld_puts (const struct match_list_displayer *displayer, const char *s)
356 {
357 tui_puts (s);
358 }
359
360 /* TUI version of displayer.flush. */
361
362 static void
363 tui_mld_flush (const struct match_list_displayer *displayer)
364 {
365 wrefresh (TUI_CMD_WIN->generic.handle);
366 }
367
368 /* TUI version of displayer.erase_entire_line. */
369
370 static void
371 tui_mld_erase_entire_line (const struct match_list_displayer *displayer)
372 {
373 WINDOW *w = TUI_CMD_WIN->generic.handle;
374
375 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
376 wclrtoeol (w);
377 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
378 }
379
380 /* TUI version of displayer.beep. */
381
382 static void
383 tui_mld_beep (const struct match_list_displayer *displayer)
384 {
385 beep ();
386 }
387
388 /* Helper function for tui_mld_read_key.
389 This temporarily replaces tui_getc for use during tab-completion
390 match list display. */
391
392 static int
393 tui_mld_getc (FILE *fp)
394 {
395 WINDOW *w = TUI_CMD_WIN->generic.handle;
396 int c = wgetch (w);
397
398 return c;
399 }
400
401 /* TUI version of displayer.read_key. */
402
403 static int
404 tui_mld_read_key (const struct match_list_displayer *displayer)
405 {
406 rl_getc_func_t *prev = rl_getc_function;
407 int c;
408
409 /* We can't use tui_getc as we need NEWLINE to not get emitted. */
410 rl_getc_function = tui_mld_getc;
411 c = rl_read_key ();
412 rl_getc_function = prev;
413 return c;
414 }
415
416 /* TUI version of rl_completion_display_matches_hook.
417 See gdb_display_match_list for a description of the arguments. */
418
419 static void
420 tui_rl_display_match_list (char **matches, int len, int max)
421 {
422 struct match_list_displayer displayer;
423
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;
432
433 gdb_display_match_list (matches, len, max, &displayer);
434 }
435
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
443 with wgetch(). */
444 void
445 tui_setup_io (int mode)
446 {
447 extern int _rl_echoing_p;
448
449 if (mode)
450 {
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;
463 _rl_echoing_p = 0;
464 rl_outstream = tui_rl_outstream;
465 rl_prompt = 0;
466 rl_completion_display_matches_hook = tui_rl_display_match_list;
467 rl_already_prompted = 0;
468
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;
473
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;
481
482 /* Save tty for SIGCONT. */
483 savetty ();
484 }
485 else
486 {
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;
494
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;
504
505 /* Save tty for SIGCONT. */
506 savetty ();
507 }
508 }
509
510 #ifdef SIGCONT
511 /* Catch SIGCONT to restore the terminal and refresh the screen. */
512 static void
513 tui_cont_sig (int sig)
514 {
515 if (tui_active)
516 {
517 /* Restore the terminal setting because another process (shell)
518 might have changed it. */
519 resetty ();
520
521 /* Force a refresh of the screen. */
522 tui_refresh_all_win ();
523
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);
529 }
530 signal (sig, tui_cont_sig);
531 }
532 #endif
533
534 /* Initialize the IO for gdb in curses mode. */
535 void
536 tui_initialize_io (void)
537 {
538 #ifdef SIGCONT
539 signal (SIGCONT, tui_cont_sig);
540 #endif
541
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);
546
547 /* Create the default UI. */
548 tui_old_uiout = cli_out_new (gdb_stdout);
549
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"));
556
557 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
558 if (tui_rl_outstream == 0)
559 error (_("Cannot redirect readline output"));
560
561 setvbuf (tui_rl_outstream, (char*) NULL, _IOLBF, 0);
562
563 #ifdef O_NONBLOCK
564 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
565 #else
566 #ifdef O_NDELAY
567 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
568 #endif
569 #endif
570 add_file_handler (tui_readline_pipe[0], tui_readline_output, 0);
571 #else
572 tui_rl_outstream = stdout;
573 #endif
574 }
575
576 /* Get a character from the command window. This is called from the
577 readline package. */
578 int
579 tui_getc (FILE *fp)
580 {
581 int ch;
582 WINDOW *w;
583
584 w = TUI_CMD_WIN->generic.handle;
585
586 #ifdef TUI_USE_PIPE_FOR_READLINE
587 /* Flush readline output. */
588 tui_readline_output (0, 0);
589 #endif
590
591 ch = wgetch (w);
592
593 /* The \n must be echoed because it will not be printed by
594 readline. */
595 if (ch == '\n')
596 {
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. */
602 if (rl_end == 0)
603 {
604 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
605
606 /* Clear the line. This will blink the gdb prompt since
607 it will be redrawn at the same line. */
608 wclrtoeol (w);
609 wrefresh (w);
610 napms (20);
611 }
612 else
613 {
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;
622 wmove (w, py, px);
623 waddch (w, ch);
624 }
625 }
626
627 /* Handle prev/next/up/down here. */
628 ch = tui_dispatch_ctrl_char (ch);
629
630 if (ch == '\n' || ch == '\r' || ch == '\f')
631 TUI_CMD_WIN->detail.command_info.curch = 0;
632 if (ch == KEY_BACKSPACE)
633 return '\b';
634
635 if (async_command_editing_p && key_is_start_sequence (ch))
636 {
637 int ch_pending;
638
639 nodelay (w, TRUE);
640 ch_pending = wgetch (w);
641 nodelay (w, FALSE);
642
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.
648
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
653 sequences.) */
654 if (ch_pending != ERR)
655 {
656 ungetch (ch_pending);
657 call_stdin_event_handler_again_p = 1;
658 }
659 }
660
661 return ch;
662 }
663
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. */
667
668 char *
669 tui_expand_tabs (const char *string, int col)
670 {
671 int n_adjust, ncol;
672 const char *s;
673 char *ret, *q;
674
675 /* 1. How many additional characters do we need? */
676 for (ncol = col, n_adjust = 0, s = string; s; )
677 {
678 s = strpbrk (s, "\t");
679 if (s)
680 {
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;
685 s++;
686 }
687 }
688
689 /* Allocate the copy. */
690 ret = q = xmalloc (strlen (string) + n_adjust + 1);
691
692 /* 2. Copy the original string while replacing TABs with spaces. */
693 for (ncol = col, s = string; s; )
694 {
695 const char *s1 = strpbrk (s, "\t");
696 if (s1)
697 {
698 if (s1 > s)
699 {
700 strncpy (q, s, s1 - s);
701 q += s1 - s;
702 ncol += s1 - s;
703 }
704 do {
705 *q++ = ' ';
706 ncol++;
707 } while ((ncol % 8) != 0);
708 s1++;
709 }
710 else
711 strcpy (q, s);
712 s = s1;
713 }
714
715 return ret;
716 }
This page took 0.082776 seconds and 5 git commands to generate.