* tuiIO.h: Remove old declarations and add the new ones.
[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
41 /* This file controls the IO interactions between gdb and curses.
42 When the TUI is enabled, gdb has two modes a curses and a standard
43 mode.
44
45 In curses mode, the gdb outputs are made in a curses command window.
46 For this, the gdb_stdout and gdb_stderr are redirected to the specific
47 ui_file implemented by TUI. The output is handled by tui_puts().
48 The input is also controlled by curses with tui_getc(). The readline
49 library uses this function to get its input. Several readline hooks
50 are installed to redirect readline output to the TUI (see also the
51 note below).
52
53 In normal mode, the gdb outputs are restored to their origin, that
54 is as if TUI is not used. Readline also uses its original getc()
55 function with stdin.
56
57 Note: the current readline is not clean in its management of the output.
58 Even if we install a redisplay handler, it sometimes writes on a stdout
59 file. It is important to redirect every output produced by readline,
60 otherwise the curses window will be garbled. This is implemented with
61 a pipe that TUI reads and readline writes to. A gdb input handler
62 is created so that reading the pipe is handled automatically.
63 This will probably not work on non-Unix platforms. The best fix is
64 to make readline clean enougth so that is never write on stdout. */
65
66 /* TUI output files. */
67 static struct ui_file *tui_stdout;
68 static struct ui_file *tui_stderr;
69 static struct ui_out *tui_out;
70
71 /* GDB output files in non-curses mode. */
72 static struct ui_file *tui_old_stdout;
73 static struct ui_file *tui_old_stderr;
74 static struct ui_out *tui_old_uiout;
75
76 /* Readline previous hooks. */
77 static Function *tui_old_rl_getc_function;
78 static VFunction *tui_old_rl_redisplay_function;
79 static VFunction *tui_old_rl_prep_terminal;
80 static VFunction *tui_old_rl_deprep_terminal;
81 static int tui_old_readline_echoing_p;
82
83 /* Readline output stream.
84 Should be removed when readline is clean. */
85 static FILE *tui_rl_outstream;
86 static FILE *tui_old_rl_outstream;
87 static int tui_readline_pipe[2];
88
89 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
90
91
92 /* Print the string in the curses command window. */
93 void
94 tui_puts (const char *string)
95 {
96 static int tui_skip_line = -1;
97 char c;
98 WINDOW *w;
99
100 w = cmdWin->generic.handle;
101 while ((c = *string++) != 0)
102 {
103 /* Catch annotation and discard them. We need two \032 and
104 discard until a \n is seen. */
105 if (c == '\032')
106 {
107 tui_skip_line++;
108 }
109 else if (tui_skip_line != 1)
110 {
111 tui_skip_line = -1;
112 waddch (w, c);
113 }
114 else if (c == '\n')
115 tui_skip_line = -1;
116 }
117 getyx (w, cmdWin->detail.commandInfo.curLine,
118 cmdWin->detail.commandInfo.curch);
119
120 /* We could defer the following. */
121 wrefresh (w);
122 fflush (stdout);
123 }
124
125 /* Readline callback.
126 Redisplay the command line with its prompt after readline has
127 changed the edited text. */
128 static void
129 tui_redisplay_readline (void)
130 {
131 int prev_col;
132 int height;
133 int col, line;
134 int c_pos;
135 int c_line;
136 int in;
137 WINDOW *w;
138 char *prompt;
139 int start_line;
140
141 prompt = get_prompt ();
142
143 c_pos = -1;
144 c_line = -1;
145 w = cmdWin->generic.handle;
146 start_line = cmdWin->detail.commandInfo.curLine;
147 wmove (w, start_line, 0);
148 prev_col = 0;
149 height = 1;
150 for (in = 0; prompt && prompt[in]; in++)
151 {
152 waddch (w, prompt[in]);
153 getyx (w, line, col);
154 if (col < prev_col)
155 height++;
156 prev_col = col;
157 }
158 for (in = 0; in < rl_end; in++)
159 {
160 unsigned char c;
161
162 c = (unsigned char) rl_line_buffer[in];
163 if (in == rl_point)
164 {
165 getyx (w, c_line, c_pos);
166 }
167
168 if (CTRL_CHAR (c) || c == RUBOUT)
169 {
170 waddch (w, '^');
171 waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
172 }
173 else
174 {
175 waddch (w, c);
176 }
177 if (c == '\n')
178 {
179 getyx (w, cmdWin->detail.commandInfo.curLine,
180 cmdWin->detail.commandInfo.curch);
181 }
182 getyx (w, line, col);
183 if (col < prev_col)
184 height++;
185 prev_col = col;
186 }
187 wclrtobot (w);
188 getyx (w, cmdWin->detail.commandInfo.curLine,
189 cmdWin->detail.commandInfo.curch);
190 if (c_line >= 0)
191 wmove (w, c_line, c_pos);
192
193 cmdWin->detail.commandInfo.curLine -= height - 1;
194
195 wrefresh (w);
196 fflush(stdout);
197 }
198
199 /* Readline callback to prepare the terminal. It is called once
200 each time we enter readline. There is nothing to do in curses mode. */
201 static void
202 tui_prep_terminal (void)
203 {
204 }
205
206 /* Readline callback to restore the terminal. It is called once
207 each time we leave readline. There is nothing to do in curses mode. */
208 static void
209 tui_deprep_terminal (void)
210 {
211 }
212
213 /* Read readline output pipe and feed the command window with it.
214 Should be removed when readline is clean. */
215 static void
216 tui_readline_output (int code, gdb_client_data data)
217 {
218 int size;
219 char buf[256];
220
221 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
222 if (size > 0 && tui_active)
223 {
224 buf[size] = 0;
225 tui_puts (buf);
226 }
227 }
228
229 /* Setup the IO for curses or non-curses mode.
230 - In non-curses mode, readline and gdb use the standard input and
231 standard output/error directly.
232 - In curses mode, the standard output/error is controlled by TUI
233 with the tui_stdout and tui_stderr. The output is redirected in
234 the curses command window. Several readline callbacks are installed
235 so that readline asks for its input to the curses command window
236 with wgetch(). */
237 void
238 tui_setup_io (int mode)
239 {
240 extern int readline_echoing_p;
241
242 if (mode)
243 {
244 /* Redirect readline to TUI. */
245 tui_old_rl_redisplay_function = rl_redisplay_function;
246 tui_old_rl_deprep_terminal = rl_deprep_term_function;
247 tui_old_rl_prep_terminal = rl_prep_term_function;
248 tui_old_rl_getc_function = rl_getc_function;
249 tui_old_rl_outstream = rl_outstream;
250 tui_old_readline_echoing_p = readline_echoing_p;
251 rl_redisplay_function = tui_redisplay_readline;
252 rl_deprep_term_function = tui_deprep_terminal;
253 rl_prep_term_function = tui_prep_terminal;
254 rl_getc_function = tui_getc;
255 readline_echoing_p = 0;
256 rl_outstream = tui_rl_outstream;
257 rl_prompt = 0;
258
259 /* Keep track of previous gdb output. */
260 tui_old_stdout = gdb_stdout;
261 tui_old_stderr = gdb_stderr;
262 tui_old_uiout = uiout;
263
264 /* Reconfigure gdb output. */
265 gdb_stdout = tui_stdout;
266 gdb_stderr = tui_stderr;
267 gdb_stdlog = gdb_stdout; /* for moment */
268 gdb_stdtarg = gdb_stderr; /* for moment */
269 uiout = tui_out;
270 }
271 else
272 {
273 /* Restore gdb output. */
274 gdb_stdout = tui_old_stdout;
275 gdb_stderr = tui_old_stderr;
276 gdb_stdlog = gdb_stdout; /* for moment */
277 gdb_stdtarg = gdb_stderr; /* for moment */
278 uiout = tui_old_uiout;
279
280 /* Restore readline. */
281 rl_redisplay_function = tui_old_rl_redisplay_function;
282 rl_deprep_term_function = tui_old_rl_deprep_terminal;
283 rl_prep_term_function = tui_old_rl_prep_terminal;
284 rl_getc_function = tui_old_rl_getc_function;
285 rl_outstream = tui_old_rl_outstream;
286 readline_echoing_p = tui_old_readline_echoing_p;
287 }
288 }
289
290 /* Initialize the IO for gdb in curses mode. */
291 void
292 tui_initialize_io ()
293 {
294 /* Create tui output streams. */
295 tui_stdout = tui_fileopen (stdout);
296 tui_stderr = tui_fileopen (stderr);
297 tui_out = tui_out_new (tui_stdout);
298
299 /* Create the default UI. It is not created because we installed
300 a init_ui_hook. */
301 uiout = cli_out_new (gdb_stdout);
302
303 /* Temporary solution for readline writing to stdout:
304 redirect readline output in a pipe, read that pipe and
305 output the content in the curses command window. */
306 if (pipe (tui_readline_pipe) != 0)
307 {
308 fprintf_unfiltered (gdb_stderr, "Cannot create pipe for readline");
309 exit (1);
310 }
311 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
312 if (tui_rl_outstream == 0)
313 {
314 fprintf_unfiltered (gdb_stderr, "Cannot redirect readline output");
315 exit (1);
316 }
317 setlinebuf (tui_rl_outstream);
318
319 #ifdef O_NONBLOCK
320 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
321 #else
322 #ifdef O_NDELAY
323 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
324 #endif
325 #endif
326
327 add_file_handler (tui_readline_pipe[0], tui_readline_output, 0);
328 }
329
330 /* Get a character from the command window. This is called from the readline
331 package. */
332 int
333 tui_getc (FILE *fp)
334 {
335 int ch;
336 WINDOW *w;
337
338 w = cmdWin->generic.handle;
339
340 /* Flush readline output. */
341 tui_readline_output (GDB_READABLE, 0);
342
343 ch = wgetch (w);
344 ch = _tuiHandleResizeDuringIO (ch);
345
346 /* The \n must be echoed because it will not be printed by readline. */
347 if (ch == '\n')
348 {
349 /* When hitting return with an empty input, gdb executes the last
350 command. If we emit a newline, this fills up the command window
351 with empty lines with gdb prompt at beginning. Instead of that,
352 stay on the same line but provide a visual effect to show the
353 user we recognized the command. */
354 if (rl_end == 0)
355 {
356 wmove (w, cmdWin->detail.commandInfo.curLine, 0);
357
358 /* Clear the line. This will blink the gdb prompt since
359 it will be redrawn at the same line. */
360 wclrtoeol (w);
361 wrefresh (w);
362 napms (20);
363 }
364 else
365 {
366 wmove (w, cmdWin->detail.commandInfo.curLine,
367 cmdWin->detail.commandInfo.curch);
368 waddch (w, ch);
369 }
370 }
371
372 if (m_isCommandChar (ch))
373 { /* Handle prev/next/up/down here */
374 ch = tuiDispatchCtrlChar (ch);
375 }
376
377 if (ch == '\n' || ch == '\r' || ch == '\f')
378 cmdWin->detail.commandInfo.curch = 0;
379 #if 0
380 else
381 tuiIncrCommandCharCountBy (1);
382 #endif
383 if (ch == KEY_BACKSPACE)
384 return '\b';
385
386 return ch;
387 }
388
389
390 /* Cleanup when a resize has occured.
391 Returns the character that must be processed. */
392 static unsigned int
393 _tuiHandleResizeDuringIO (unsigned int originalCh)
394 {
395 if (tuiWinResized ())
396 {
397 tuiRefreshAll ();
398 dont_repeat ();
399 tuiSetWinResizedTo (FALSE);
400 return '\n';
401 }
402 else
403 return originalCh;
404 }