* tui-hooks.c: New file, gdb hooks for tui.
[binutils-gdb.git] / gdb / tui / tuiIO.c
1 /* TUI support I/O functions.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Hewlett-Packard Company.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include <stdio.h>
23 #include "defs.h"
24 #include "terminal.h"
25 #include "target.h"
26 #include "event-loop.h"
27 #include "command.h"
28 #include "top.h"
29 #include "readline/readline.h"
30 #include "tui.h"
31 #include "tuiData.h"
32 #include "tuiIO.h"
33 #include "tuiCommand.h"
34 #include "tuiWin.h"
35 #include "tuiGeneralWin.h"
36 #include "tui-file.h"
37 #include "ui-out.h"
38 #include "cli-out.h"
39 #include <fcntl.h>
40 #include <signal.h>
41
42 /* This file controls the IO interactions between gdb and curses.
43 When the TUI is enabled, gdb has two modes a curses and a standard
44 mode.
45
46 In curses mode, the gdb outputs are made in a curses command window.
47 For this, the gdb_stdout and gdb_stderr are redirected to the specific
48 ui_file implemented by TUI. The output is handled by tui_puts().
49 The input is also controlled by curses with tui_getc(). The readline
50 library uses this function to get its input. Several readline hooks
51 are installed to redirect readline output to the TUI (see also the
52 note below).
53
54 In normal mode, the gdb outputs are restored to their origin, that
55 is as if TUI is not used. Readline also uses its original getc()
56 function with stdin.
57
58 Note: the current readline is not clean in its management of the output.
59 Even if we install a redisplay handler, it sometimes writes on a stdout
60 file. It is important to redirect every output produced by readline,
61 otherwise the curses window will be garbled. This is implemented with
62 a pipe that TUI reads and readline writes to. A gdb input handler
63 is created so that reading the pipe is handled automatically.
64 This will probably not work on non-Unix platforms. The best fix is
65 to make readline clean enougth so that is never write on stdout. */
66
67 /* TUI output files. */
68 static struct ui_file *tui_stdout;
69 static struct ui_file *tui_stderr;
70 static struct ui_out *tui_out;
71
72 /* GDB output files in non-curses mode. */
73 static struct ui_file *tui_old_stdout;
74 static struct ui_file *tui_old_stderr;
75 static struct ui_out *tui_old_uiout;
76
77 /* Readline previous hooks. */
78 static Function *tui_old_rl_getc_function;
79 static VFunction *tui_old_rl_redisplay_function;
80 static VFunction *tui_old_rl_prep_terminal;
81 static VFunction *tui_old_rl_deprep_terminal;
82 static int tui_old_readline_echoing_p;
83
84 /* Readline output stream.
85 Should be removed when readline is clean. */
86 static FILE *tui_rl_outstream;
87 static FILE *tui_old_rl_outstream;
88 static int tui_readline_pipe[2];
89
90 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
91
92
93 /* Print the string in the curses command window. */
94 void
95 tui_puts (const char *string)
96 {
97 static int tui_skip_line = -1;
98 char c;
99 WINDOW *w;
100
101 w = cmdWin->generic.handle;
102 while ((c = *string++) != 0)
103 {
104 /* Catch annotation and discard them. We need two \032 and
105 discard until a \n is seen. */
106 if (c == '\032')
107 {
108 tui_skip_line++;
109 }
110 else if (tui_skip_line != 1)
111 {
112 tui_skip_line = -1;
113 waddch (w, c);
114 }
115 else if (c == '\n')
116 tui_skip_line = -1;
117 }
118 getyx (w, cmdWin->detail.commandInfo.curLine,
119 cmdWin->detail.commandInfo.curch);
120 cmdWin->detail.commandInfo.start_line = cmdWin->detail.commandInfo.curLine;
121
122 /* We could defer the following. */
123 wrefresh (w);
124 fflush (stdout);
125 }
126
127 /* Readline callback.
128 Redisplay the command line with its prompt after readline has
129 changed the edited text. */
130 static void
131 tui_redisplay_readline (void)
132 {
133 int prev_col;
134 int height;
135 int col, line;
136 int c_pos;
137 int c_line;
138 int in;
139 WINDOW *w;
140 char *prompt;
141 int start_line;
142
143 prompt = get_prompt ();
144
145 c_pos = -1;
146 c_line = -1;
147 w = cmdWin->generic.handle;
148 start_line = cmdWin->detail.commandInfo.start_line;
149 wmove (w, start_line, 0);
150 prev_col = 0;
151 height = 1;
152 for (in = 0; prompt && prompt[in]; in++)
153 {
154 waddch (w, prompt[in]);
155 getyx (w, line, col);
156 if (col < prev_col)
157 height++;
158 prev_col = col;
159 }
160 for (in = 0; in < rl_end; in++)
161 {
162 unsigned char c;
163
164 c = (unsigned char) rl_line_buffer[in];
165 if (in == rl_point)
166 {
167 getyx (w, c_line, c_pos);
168 }
169
170 if (CTRL_CHAR (c) || c == RUBOUT)
171 {
172 waddch (w, '^');
173 waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
174 }
175 else
176 {
177 waddch (w, c);
178 }
179 if (c == '\n')
180 {
181 getyx (w, cmdWin->detail.commandInfo.start_line,
182 cmdWin->detail.commandInfo.curch);
183 }
184 getyx (w, line, col);
185 if (col < prev_col)
186 height++;
187 prev_col = col;
188 }
189 wclrtobot (w);
190 getyx (w, cmdWin->detail.commandInfo.start_line,
191 cmdWin->detail.commandInfo.curch);
192 if (c_line >= 0)
193 {
194 wmove (w, c_line, c_pos);
195 cmdWin->detail.commandInfo.curLine = c_line;
196 cmdWin->detail.commandInfo.curch = c_pos;
197 }
198 cmdWin->detail.commandInfo.start_line -= height - 1;
199
200 wrefresh (w);
201 fflush(stdout);
202 }
203
204 /* Readline callback to prepare the terminal. It is called once
205 each time we enter readline. There is nothing to do in curses mode. */
206 static void
207 tui_prep_terminal (void)
208 {
209 }
210
211 /* Readline callback to restore the terminal. It is called once
212 each time we leave readline. There is nothing to do in curses mode. */
213 static void
214 tui_deprep_terminal (void)
215 {
216 }
217
218 /* Read readline output pipe and feed the command window with it.
219 Should be removed when readline is clean. */
220 static void
221 tui_readline_output (int code, gdb_client_data data)
222 {
223 int size;
224 char buf[256];
225
226 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
227 if (size > 0 && tui_active)
228 {
229 buf[size] = 0;
230 tui_puts (buf);
231 }
232 }
233
234 /* Setup the IO for curses or non-curses mode.
235 - In non-curses mode, readline and gdb use the standard input and
236 standard output/error directly.
237 - In curses mode, the standard output/error is controlled by TUI
238 with the tui_stdout and tui_stderr. The output is redirected in
239 the curses command window. Several readline callbacks are installed
240 so that readline asks for its input to the curses command window
241 with wgetch(). */
242 void
243 tui_setup_io (int mode)
244 {
245 extern int readline_echoing_p;
246
247 if (mode)
248 {
249 /* Redirect readline to TUI. */
250 tui_old_rl_redisplay_function = rl_redisplay_function;
251 tui_old_rl_deprep_terminal = rl_deprep_term_function;
252 tui_old_rl_prep_terminal = rl_prep_term_function;
253 tui_old_rl_getc_function = rl_getc_function;
254 tui_old_rl_outstream = rl_outstream;
255 tui_old_readline_echoing_p = readline_echoing_p;
256 rl_redisplay_function = tui_redisplay_readline;
257 rl_deprep_term_function = tui_deprep_terminal;
258 rl_prep_term_function = tui_prep_terminal;
259 rl_getc_function = tui_getc;
260 readline_echoing_p = 0;
261 rl_outstream = tui_rl_outstream;
262 rl_prompt = 0;
263
264 /* Keep track of previous gdb output. */
265 tui_old_stdout = gdb_stdout;
266 tui_old_stderr = gdb_stderr;
267 tui_old_uiout = uiout;
268
269 /* Reconfigure gdb output. */
270 gdb_stdout = tui_stdout;
271 gdb_stderr = tui_stderr;
272 gdb_stdlog = gdb_stdout; /* for moment */
273 gdb_stdtarg = gdb_stderr; /* for moment */
274 uiout = tui_out;
275
276 /* Save tty for SIGCONT. */
277 savetty ();
278 }
279 else
280 {
281 /* Restore gdb output. */
282 gdb_stdout = tui_old_stdout;
283 gdb_stderr = tui_old_stderr;
284 gdb_stdlog = gdb_stdout; /* for moment */
285 gdb_stdtarg = gdb_stderr; /* for moment */
286 uiout = tui_old_uiout;
287
288 /* Restore readline. */
289 rl_redisplay_function = tui_old_rl_redisplay_function;
290 rl_deprep_term_function = tui_old_rl_deprep_terminal;
291 rl_prep_term_function = tui_old_rl_prep_terminal;
292 rl_getc_function = tui_old_rl_getc_function;
293 rl_outstream = tui_old_rl_outstream;
294 readline_echoing_p = tui_old_readline_echoing_p;
295
296 /* Save tty for SIGCONT. */
297 savetty ();
298 }
299 }
300
301 #ifdef SIGCONT
302 /* Catch SIGCONT to restore the terminal and refresh the screen. */
303 static void
304 tui_cont_sig (int sig)
305 {
306 if (tui_active)
307 {
308 /* Restore the terminal setting because another process (shell)
309 might have changed it. */
310 resetty ();
311
312 /* Force a refresh of the screen. */
313 tuiRefreshAll ();
314
315 /* Update cursor position on the screen. */
316 wmove (cmdWin->generic.handle,
317 cmdWin->detail.commandInfo.start_line,
318 cmdWin->detail.commandInfo.curch);
319 wrefresh (cmdWin->generic.handle);
320 }
321 signal (sig, tui_cont_sig);
322 }
323 #endif
324
325 /* Initialize the IO for gdb in curses mode. */
326 void
327 tui_initialize_io ()
328 {
329 #ifdef SIGCONT
330 signal (SIGCONT, tui_cont_sig);
331 #endif
332
333 /* Create tui output streams. */
334 tui_stdout = tui_fileopen (stdout);
335 tui_stderr = tui_fileopen (stderr);
336 tui_out = tui_out_new (tui_stdout);
337
338 /* Create the default UI. It is not created because we installed
339 a init_ui_hook. */
340 uiout = cli_out_new (gdb_stdout);
341
342 /* Temporary solution for readline writing to stdout:
343 redirect readline output in a pipe, read that pipe and
344 output the content in the curses command window. */
345 if (pipe (tui_readline_pipe) != 0)
346 {
347 fprintf_unfiltered (gdb_stderr, "Cannot create pipe for readline");
348 exit (1);
349 }
350 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
351 if (tui_rl_outstream == 0)
352 {
353 fprintf_unfiltered (gdb_stderr, "Cannot redirect readline output");
354 exit (1);
355 }
356 setlinebuf (tui_rl_outstream);
357
358 #ifdef O_NONBLOCK
359 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
360 #else
361 #ifdef O_NDELAY
362 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
363 #endif
364 #endif
365
366 add_file_handler (tui_readline_pipe[0], tui_readline_output, 0);
367 }
368
369 /* Get a character from the command window. This is called from the readline
370 package. */
371 int
372 tui_getc (FILE *fp)
373 {
374 int ch;
375 WINDOW *w;
376
377 w = cmdWin->generic.handle;
378
379 /* Flush readline output. */
380 tui_readline_output (GDB_READABLE, 0);
381
382 ch = wgetch (w);
383 ch = _tuiHandleResizeDuringIO (ch);
384
385 /* The \n must be echoed because it will not be printed by readline. */
386 if (ch == '\n')
387 {
388 /* When hitting return with an empty input, gdb executes the last
389 command. If we emit a newline, this fills up the command window
390 with empty lines with gdb prompt at beginning. Instead of that,
391 stay on the same line but provide a visual effect to show the
392 user we recognized the command. */
393 if (rl_end == 0)
394 {
395 wmove (w, cmdWin->detail.commandInfo.curLine, 0);
396
397 /* Clear the line. This will blink the gdb prompt since
398 it will be redrawn at the same line. */
399 wclrtoeol (w);
400 wrefresh (w);
401 napms (20);
402 }
403 else
404 {
405 wmove (w, cmdWin->detail.commandInfo.curLine,
406 cmdWin->detail.commandInfo.curch);
407 waddch (w, ch);
408 }
409 }
410
411 if (m_isCommandChar (ch))
412 { /* Handle prev/next/up/down here */
413 ch = tuiDispatchCtrlChar (ch);
414 }
415
416 if (ch == '\n' || ch == '\r' || ch == '\f')
417 cmdWin->detail.commandInfo.curch = 0;
418 #if 0
419 else
420 tuiIncrCommandCharCountBy (1);
421 #endif
422 if (ch == KEY_BACKSPACE)
423 return '\b';
424
425 return ch;
426 }
427
428
429 /* Cleanup when a resize has occured.
430 Returns the character that must be processed. */
431 static unsigned int
432 _tuiHandleResizeDuringIO (unsigned int originalCh)
433 {
434 if (tuiWinResized ())
435 {
436 tuiRefreshAll ();
437 dont_repeat ();
438 tuiSetWinResizedTo (FALSE);
439 return '\n';
440 }
441 else
442 return originalCh;
443 }