* tuiIO.c (CTRL_CHAR): Redefine and use readline 4.3 definition.
[deliverable/binutils-gdb.git] / gdb / tui / tuiIO.c
1 /* TUI support I/O functions.
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
5
6 Contributed by Hewlett-Packard Company.
7
8 This file is part of GDB.
9
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.
14
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.
19
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. */
24
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>
31 first. */
32
33 #include "config.h"
34 #ifdef HAVE_NCURSES_H
35 #include <ncurses.h>
36 #else
37 #ifdef HAVE_CURSES_H
38 #include <curses.h>
39 #endif
40 #endif
41
42 #include <stdio.h>
43 #include "defs.h"
44 #include "terminal.h"
45 #include "target.h"
46 #include "event-loop.h"
47 #include "command.h"
48 #include "top.h"
49 #include "readline/readline.h"
50 #include "tui.h"
51 #include "tuiData.h"
52 #include "tuiIO.h"
53 #include "tuiCommand.h"
54 #include "tuiWin.h"
55 #include "tuiGeneralWin.h"
56 #include "tui-file.h"
57 #include "ui-out.h"
58 #include "cli-out.h"
59 #include <fcntl.h>
60 #include <signal.h>
61
62 /* Use definition from readline 4.3. */
63 #undef CTRL_CHAR
64 #define CTRL_CHAR(c) ((c) < control_character_threshold && (((c) & 0x80) == 0))
65
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
68 mode.
69
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
76 note below).
77
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()
80 function with stdin.
81
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. */
90
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;
95
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;
100
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;
107
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];
113
114 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
115
116
117 /* Print the string in the curses command window. */
118 void
119 tui_puts (const char *string)
120 {
121 static int tui_skip_line = -1;
122 char c;
123 WINDOW *w;
124
125 w = cmdWin->generic.handle;
126 while ((c = *string++) != 0)
127 {
128 /* Catch annotation and discard them. We need two \032 and
129 discard until a \n is seen. */
130 if (c == '\032')
131 {
132 tui_skip_line++;
133 }
134 else if (tui_skip_line != 1)
135 {
136 tui_skip_line = -1;
137 waddch (w, c);
138 }
139 else if (c == '\n')
140 tui_skip_line = -1;
141 }
142 getyx (w, cmdWin->detail.commandInfo.curLine,
143 cmdWin->detail.commandInfo.curch);
144 cmdWin->detail.commandInfo.start_line = cmdWin->detail.commandInfo.curLine;
145
146 /* We could defer the following. */
147 wrefresh (w);
148 fflush (stdout);
149 }
150
151 /* Readline callback.
152 Redisplay the command line with its prompt after readline has
153 changed the edited text. */
154 static void
155 tui_redisplay_readline (void)
156 {
157 int prev_col;
158 int height;
159 int col, line;
160 int c_pos;
161 int c_line;
162 int in;
163 WINDOW *w;
164 char *prompt;
165 int start_line;
166
167 prompt = get_prompt ();
168
169 c_pos = -1;
170 c_line = -1;
171 w = cmdWin->generic.handle;
172 start_line = cmdWin->detail.commandInfo.start_line;
173 wmove (w, start_line, 0);
174 prev_col = 0;
175 height = 1;
176 for (in = 0; prompt && prompt[in]; in++)
177 {
178 waddch (w, prompt[in]);
179 getyx (w, line, col);
180 if (col < prev_col)
181 height++;
182 prev_col = col;
183 }
184 for (in = 0; in < rl_end; in++)
185 {
186 unsigned char c;
187
188 c = (unsigned char) rl_line_buffer[in];
189 if (in == rl_point)
190 {
191 getyx (w, c_line, c_pos);
192 }
193
194 if (CTRL_CHAR (c) || c == RUBOUT)
195 {
196 waddch (w, '^');
197 waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
198 }
199 else
200 {
201 waddch (w, c);
202 }
203 if (c == '\n')
204 {
205 getyx (w, cmdWin->detail.commandInfo.start_line,
206 cmdWin->detail.commandInfo.curch);
207 }
208 getyx (w, line, col);
209 if (col < prev_col)
210 height++;
211 prev_col = col;
212 }
213 wclrtobot (w);
214 getyx (w, cmdWin->detail.commandInfo.start_line,
215 cmdWin->detail.commandInfo.curch);
216 if (c_line >= 0)
217 {
218 wmove (w, c_line, c_pos);
219 cmdWin->detail.commandInfo.curLine = c_line;
220 cmdWin->detail.commandInfo.curch = c_pos;
221 }
222 cmdWin->detail.commandInfo.start_line -= height - 1;
223
224 wrefresh (w);
225 fflush(stdout);
226 }
227
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. */
230 static void
231 tui_prep_terminal (void)
232 {
233 }
234
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. */
237 static void
238 tui_deprep_terminal (void)
239 {
240 }
241
242 /* Read readline output pipe and feed the command window with it.
243 Should be removed when readline is clean. */
244 static void
245 tui_readline_output (int code, gdb_client_data data)
246 {
247 int size;
248 char buf[256];
249
250 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
251 if (size > 0 && tui_active)
252 {
253 buf[size] = 0;
254 tui_puts (buf);
255 }
256 }
257
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
265 with wgetch(). */
266 void
267 tui_setup_io (int mode)
268 {
269 extern int readline_echoing_p;
270
271 if (mode)
272 {
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;
286 rl_prompt = 0;
287
288 /* Keep track of previous gdb output. */
289 tui_old_stdout = gdb_stdout;
290 tui_old_stderr = gdb_stderr;
291 tui_old_uiout = uiout;
292
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 */
298 uiout = tui_out;
299
300 /* Save tty for SIGCONT. */
301 savetty ();
302 }
303 else
304 {
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;
311
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;
319
320 /* Save tty for SIGCONT. */
321 savetty ();
322 }
323 }
324
325 #ifdef SIGCONT
326 /* Catch SIGCONT to restore the terminal and refresh the screen. */
327 static void
328 tui_cont_sig (int sig)
329 {
330 if (tui_active)
331 {
332 /* Restore the terminal setting because another process (shell)
333 might have changed it. */
334 resetty ();
335
336 /* Force a refresh of the screen. */
337 tuiRefreshAll ();
338
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);
344 }
345 signal (sig, tui_cont_sig);
346 }
347 #endif
348
349 /* Initialize the IO for gdb in curses mode. */
350 void
351 tui_initialize_io ()
352 {
353 #ifdef SIGCONT
354 signal (SIGCONT, tui_cont_sig);
355 #endif
356
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);
361
362 /* Create the default UI. It is not created because we installed
363 a init_ui_hook. */
364 uiout = cli_out_new (gdb_stdout);
365
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)
370 {
371 fprintf_unfiltered (gdb_stderr, "Cannot create pipe for readline");
372 exit (1);
373 }
374 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
375 if (tui_rl_outstream == 0)
376 {
377 fprintf_unfiltered (gdb_stderr, "Cannot redirect readline output");
378 exit (1);
379 }
380 setlinebuf (tui_rl_outstream);
381
382 #ifdef O_NONBLOCK
383 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
384 #else
385 #ifdef O_NDELAY
386 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
387 #endif
388 #endif
389
390 add_file_handler (tui_readline_pipe[0], tui_readline_output, 0);
391 }
392
393 /* Get a character from the command window. This is called from the readline
394 package. */
395 int
396 tui_getc (FILE *fp)
397 {
398 int ch;
399 WINDOW *w;
400
401 w = cmdWin->generic.handle;
402
403 /* Flush readline output. */
404 tui_readline_output (GDB_READABLE, 0);
405
406 ch = wgetch (w);
407 ch = _tuiHandleResizeDuringIO (ch);
408
409 /* The \n must be echoed because it will not be printed by readline. */
410 if (ch == '\n')
411 {
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. */
417 if (rl_end == 0)
418 {
419 wmove (w, cmdWin->detail.commandInfo.curLine, 0);
420
421 /* Clear the line. This will blink the gdb prompt since
422 it will be redrawn at the same line. */
423 wclrtoeol (w);
424 wrefresh (w);
425 napms (20);
426 }
427 else
428 {
429 wmove (w, cmdWin->detail.commandInfo.curLine,
430 cmdWin->detail.commandInfo.curch);
431 waddch (w, ch);
432 }
433 }
434
435 if (m_isCommandChar (ch))
436 { /* Handle prev/next/up/down here */
437 ch = tuiDispatchCtrlChar (ch);
438 }
439
440 if (ch == '\n' || ch == '\r' || ch == '\f')
441 cmdWin->detail.commandInfo.curch = 0;
442 #if 0
443 else
444 tuiIncrCommandCharCountBy (1);
445 #endif
446 if (ch == KEY_BACKSPACE)
447 return '\b';
448
449 return ch;
450 }
451
452
453 /* Cleanup when a resize has occured.
454 Returns the character that must be processed. */
455 static unsigned int
456 _tuiHandleResizeDuringIO (unsigned int originalCh)
457 {
458 if (tuiWinResized ())
459 {
460 tuiRefreshAll ();
461 dont_repeat ();
462 tuiSetWinResizedTo (FALSE);
463 return '\n';
464 }
465 else
466 return originalCh;
467 }
This page took 0.038344 seconds and 4 git commands to generate.