+#include "tuiGeneralWin.h"
+#include "tui-file.h"
+#include "ui-out.h"
+#include "cli-out.h"
+#include <fcntl.h>
+#include <signal.h>
+
+/* Use definition from readline 4.3. */
+#undef CTRL_CHAR
+#define CTRL_CHAR(c) ((c) < control_character_threshold && (((c) & 0x80) == 0))
+
+/* This file controls the IO interactions between gdb and curses.
+ When the TUI is enabled, gdb has two modes a curses and a standard
+ mode.
+
+ In curses mode, the gdb outputs are made in a curses command window.
+ For this, the gdb_stdout and gdb_stderr are redirected to the specific
+ ui_file implemented by TUI. The output is handled by tui_puts().
+ The input is also controlled by curses with tui_getc(). The readline
+ library uses this function to get its input. Several readline hooks
+ are installed to redirect readline output to the TUI (see also the
+ note below).
+
+ In normal mode, the gdb outputs are restored to their origin, that
+ is as if TUI is not used. Readline also uses its original getc()
+ function with stdin.
+
+ Note SCz/2001-07-21: the current readline is not clean in its management of
+ the output. Even if we install a redisplay handler, it sometimes writes on
+ a stdout file. It is important to redirect every output produced by
+ readline, otherwise the curses window will be garbled. This is implemented
+ with a pipe that TUI reads and readline writes to. A gdb input handler
+ is created so that reading the pipe is handled automatically.
+ This will probably not work on non-Unix platforms. The best fix is
+ to make readline clean enougth so that is never write on stdout.
+
+ Note SCz/2002-09-01: we now use more readline hooks and it seems that
+ with them we don't need the pipe anymore (verified by creating the pipe
+ and closing its end so that write causes a SIGPIPE). The old pipe code
+ is still there and can be conditionally removed by
+ #undef TUI_USE_PIPE_FOR_READLINE. */
+
+/* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */
+#define TUI_USE_PIPE_FOR_READLINE
+/*#undef TUI_USE_PIPE_FOR_READLINE*/
+
+/* TUI output files. */
+static struct ui_file *tui_stdout;
+static struct ui_file *tui_stderr;
+struct ui_out *tui_out;
+
+/* GDB output files in non-curses mode. */
+static struct ui_file *tui_old_stdout;
+static struct ui_file *tui_old_stderr;
+struct ui_out *tui_old_uiout;
+
+/* Readline previous hooks. */
+static Function *tui_old_rl_getc_function;
+static VFunction *tui_old_rl_redisplay_function;
+static VFunction *tui_old_rl_prep_terminal;
+static VFunction *tui_old_rl_deprep_terminal;
+static int tui_old_readline_echoing_p;
+
+/* Readline output stream.
+ Should be removed when readline is clean. */
+static FILE *tui_rl_outstream;
+static FILE *tui_old_rl_outstream;
+#ifdef TUI_USE_PIPE_FOR_READLINE
+static int tui_readline_pipe[2];