*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / tui / tuiIO.c
CommitLineData
f377b406 1/* TUI support I/O functions.
f33c6cbf
AC
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
5
f377b406
SC
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. */
c906108c 24
f33c6cbf
AC
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
4e8f7a8b
DJ
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
c906108c
SS
42#include <stdio.h>
43#include "defs.h"
44#include "terminal.h"
a198b876
SC
45#include "target.h"
46#include "event-loop.h"
47#include "command.h"
48#include "top.h"
49#include "readline/readline.h"
c906108c
SS
50#include "tui.h"
51#include "tuiData.h"
52#include "tuiIO.h"
53#include "tuiCommand.h"
54#include "tuiWin.h"
a198b876
SC
55#include "tuiGeneralWin.h"
56#include "tui-file.h"
57#include "ui-out.h"
58#include "cli-out.h"
59#include <fcntl.h>
9d876a16 60#include <signal.h>
a198b876 61
ec6f8892
SC
62/* Use definition from readline 4.3. */
63#undef CTRL_CHAR
64#define CTRL_CHAR(c) ((c) < control_character_threshold && (((c) & 0x80) == 0))
65
a198b876
SC
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. */
92static struct ui_file *tui_stdout;
93static struct ui_file *tui_stderr;
94static struct ui_out *tui_out;
95
96/* GDB output files in non-curses mode. */
97static struct ui_file *tui_old_stdout;
98static struct ui_file *tui_old_stderr;
99static struct ui_out *tui_old_uiout;
100
101/* Readline previous hooks. */
102static Function *tui_old_rl_getc_function;
103static VFunction *tui_old_rl_redisplay_function;
104static VFunction *tui_old_rl_prep_terminal;
105static VFunction *tui_old_rl_deprep_terminal;
106static int tui_old_readline_echoing_p;
107
108/* Readline output stream.
109 Should be removed when readline is clean. */
110static FILE *tui_rl_outstream;
111static FILE *tui_old_rl_outstream;
112static int tui_readline_pipe[2];
c906108c 113
a14ed312 114static unsigned int _tuiHandleResizeDuringIO (unsigned int);
c906108c
SS
115
116
a198b876 117/* Print the string in the curses command window. */
c906108c 118void
a198b876 119tui_puts (const char *string)
c906108c 120{
a198b876
SC
121 static int tui_skip_line = -1;
122 char c;
123 WINDOW *w;
c906108c 124
a198b876
SC
125 w = cmdWin->generic.handle;
126 while ((c = *string++) != 0)
c906108c 127 {
a198b876
SC
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);
d75e970c 144 cmdWin->detail.commandInfo.start_line = cmdWin->detail.commandInfo.curLine;
a198b876
SC
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. */
154static void
155tui_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;
d75e970c 172 start_line = cmdWin->detail.commandInfo.start_line;
a198b876
SC
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 }
c906108c
SS
199 else
200 {
a198b876 201 waddch (w, c);
c906108c 202 }
a198b876
SC
203 if (c == '\n')
204 {
d75e970c 205 getyx (w, cmdWin->detail.commandInfo.start_line,
a198b876
SC
206 cmdWin->detail.commandInfo.curch);
207 }
208 getyx (w, line, col);
209 if (col < prev_col)
210 height++;
211 prev_col = col;
c906108c 212 }
a198b876 213 wclrtobot (w);
d75e970c 214 getyx (w, cmdWin->detail.commandInfo.start_line,
a198b876
SC
215 cmdWin->detail.commandInfo.curch);
216 if (c_line >= 0)
d75e970c
SC
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;
a198b876 223
a198b876
SC
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. */
230static void
231tui_prep_terminal (void)
c906108c 232{
a198b876 233}
c906108c 234
a198b876
SC
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. */
237static void
238tui_deprep_terminal (void)
239{
240}
c906108c 241
a198b876
SC
242/* Read readline output pipe and feed the command window with it.
243 Should be removed when readline is clean. */
244static void
245tui_readline_output (int code, gdb_client_data data)
246{
247 int size;
248 char buf[256];
c906108c 249
a198b876
SC
250 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
251 if (size > 0 && tui_active)
c906108c 252 {
a198b876
SC
253 buf[size] = 0;
254 tui_puts (buf);
c906108c 255 }
a198b876
SC
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(). */
266void
267tui_setup_io (int mode)
268{
269 extern int readline_echoing_p;
270
271 if (mode)
c906108c 272 {
a198b876
SC
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;
9d876a16
SC
299
300 /* Save tty for SIGCONT. */
301 savetty ();
c906108c 302 }
a198b876 303 else
c906108c 304 {
a198b876
SC
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;
9d876a16
SC
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. */
327static void
328tui_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 ();
d75e970c
SC
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);
c906108c 344 }
9d876a16 345 signal (sig, tui_cont_sig);
a198b876 346}
9d876a16 347#endif
c906108c 348
a198b876
SC
349/* Initialize the IO for gdb in curses mode. */
350void
351tui_initialize_io ()
352{
9d876a16
SC
353#ifdef SIGCONT
354 signal (SIGCONT, tui_cont_sig);
355#endif
356
a198b876
SC
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)
c906108c 370 {
a198b876
SC
371 fprintf_unfiltered (gdb_stderr, "Cannot create pipe for readline");
372 exit (1);
c906108c 373 }
a198b876
SC
374 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
375 if (tui_rl_outstream == 0)
c906108c 376 {
a198b876
SC
377 fprintf_unfiltered (gdb_stderr, "Cannot redirect readline output");
378 exit (1);
c906108c 379 }
a198b876 380 setlinebuf (tui_rl_outstream);
c906108c 381
a198b876
SC
382#ifdef O_NONBLOCK
383 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
c906108c 384#else
a198b876
SC
385#ifdef O_NDELAY
386 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
c906108c 387#endif
a198b876
SC
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. */
395int
396tui_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);
c906108c
SS
407 ch = _tuiHandleResizeDuringIO (ch);
408
a198b876
SC
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
c906108c
SS
435 if (m_isCommandChar (ch))
436 { /* Handle prev/next/up/down here */
c906108c 437 ch = tuiDispatchCtrlChar (ch);
c906108c 438 }
a198b876 439
c906108c
SS
440 if (ch == '\n' || ch == '\r' || ch == '\f')
441 cmdWin->detail.commandInfo.curch = 0;
a198b876 442#if 0
c906108c
SS
443 else
444 tuiIncrCommandCharCountBy (1);
a198b876
SC
445#endif
446 if (ch == KEY_BACKSPACE)
447 return '\b';
448
c906108c 449 return ch;
a198b876 450}
c906108c 451
c906108c 452
a198b876
SC
453/* Cleanup when a resize has occured.
454 Returns the character that must be processed. */
c906108c 455static unsigned int
eca6576c 456_tuiHandleResizeDuringIO (unsigned int originalCh)
c906108c
SS
457{
458 if (tuiWinResized ())
459 {
e8b915dc 460 tuiRefreshAll ();
c906108c
SS
461 dont_repeat ();
462 tuiSetWinResizedTo (FALSE);
c906108c
SS
463 return '\n';
464 }
465 else
466 return originalCh;
a198b876 467}
This page took 0.32284 seconds and 4 git commands to generate.