Commit | Line | Data |
---|---|---|
f377b406 SC |
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. */ | |
c906108c SS |
21 | |
22 | #include <stdio.h> | |
23 | #include "defs.h" | |
24 | #include "terminal.h" | |
a198b876 SC |
25 | #include "target.h" |
26 | #include "event-loop.h" | |
27 | #include "command.h" | |
28 | #include "top.h" | |
29 | #include "readline/readline.h" | |
c906108c SS |
30 | #include "tui.h" |
31 | #include "tuiData.h" | |
32 | #include "tuiIO.h" | |
33 | #include "tuiCommand.h" | |
34 | #include "tuiWin.h" | |
a198b876 SC |
35 | #include "tuiGeneralWin.h" |
36 | #include "tui-file.h" | |
37 | #include "ui-out.h" | |
38 | #include "cli-out.h" | |
39 | #include <fcntl.h> | |
9d876a16 | 40 | #include <signal.h> |
a198b876 SC |
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]; | |
c906108c | 89 | |
a14ed312 | 90 | static unsigned int _tuiHandleResizeDuringIO (unsigned int); |
c906108c SS |
91 | |
92 | ||
a198b876 | 93 | /* Print the string in the curses command window. */ |
c906108c | 94 | void |
a198b876 | 95 | tui_puts (const char *string) |
c906108c | 96 | { |
a198b876 SC |
97 | static int tui_skip_line = -1; |
98 | char c; | |
99 | WINDOW *w; | |
c906108c | 100 | |
a198b876 SC |
101 | w = cmdWin->generic.handle; |
102 | while ((c = *string++) != 0) | |
c906108c | 103 | { |
a198b876 SC |
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); | |
d75e970c | 120 | cmdWin->detail.commandInfo.start_line = cmdWin->detail.commandInfo.curLine; |
a198b876 SC |
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; | |
d75e970c | 148 | start_line = cmdWin->detail.commandInfo.start_line; |
a198b876 SC |
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 | } | |
c906108c SS |
175 | else |
176 | { | |
a198b876 | 177 | waddch (w, c); |
c906108c | 178 | } |
a198b876 SC |
179 | if (c == '\n') |
180 | { | |
d75e970c | 181 | getyx (w, cmdWin->detail.commandInfo.start_line, |
a198b876 SC |
182 | cmdWin->detail.commandInfo.curch); |
183 | } | |
184 | getyx (w, line, col); | |
185 | if (col < prev_col) | |
186 | height++; | |
187 | prev_col = col; | |
c906108c | 188 | } |
a198b876 | 189 | wclrtobot (w); |
d75e970c | 190 | getyx (w, cmdWin->detail.commandInfo.start_line, |
a198b876 SC |
191 | cmdWin->detail.commandInfo.curch); |
192 | if (c_line >= 0) | |
d75e970c SC |
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; | |
a198b876 | 199 | |
a198b876 SC |
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) | |
c906108c | 208 | { |
a198b876 | 209 | } |
c906108c | 210 | |
a198b876 SC |
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 | } | |
c906108c | 217 | |
a198b876 SC |
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]; | |
c906108c | 225 | |
a198b876 SC |
226 | size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1); |
227 | if (size > 0 && tui_active) | |
c906108c | 228 | { |
a198b876 SC |
229 | buf[size] = 0; |
230 | tui_puts (buf); | |
c906108c | 231 | } |
a198b876 SC |
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) | |
c906108c | 248 | { |
a198b876 SC |
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; | |
9d876a16 SC |
275 | |
276 | /* Save tty for SIGCONT. */ | |
277 | savetty (); | |
c906108c | 278 | } |
a198b876 | 279 | else |
c906108c | 280 | { |
a198b876 SC |
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; | |
9d876a16 SC |
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 (); | |
d75e970c SC |
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); | |
c906108c | 320 | } |
9d876a16 | 321 | signal (sig, tui_cont_sig); |
a198b876 | 322 | } |
9d876a16 | 323 | #endif |
c906108c | 324 | |
a198b876 SC |
325 | /* Initialize the IO for gdb in curses mode. */ |
326 | void | |
327 | tui_initialize_io () | |
328 | { | |
9d876a16 SC |
329 | #ifdef SIGCONT |
330 | signal (SIGCONT, tui_cont_sig); | |
331 | #endif | |
332 | ||
a198b876 SC |
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) | |
c906108c | 346 | { |
a198b876 SC |
347 | fprintf_unfiltered (gdb_stderr, "Cannot create pipe for readline"); |
348 | exit (1); | |
c906108c | 349 | } |
a198b876 SC |
350 | tui_rl_outstream = fdopen (tui_readline_pipe[1], "w"); |
351 | if (tui_rl_outstream == 0) | |
c906108c | 352 | { |
a198b876 SC |
353 | fprintf_unfiltered (gdb_stderr, "Cannot redirect readline output"); |
354 | exit (1); | |
c906108c | 355 | } |
a198b876 | 356 | setlinebuf (tui_rl_outstream); |
c906108c | 357 | |
a198b876 SC |
358 | #ifdef O_NONBLOCK |
359 | (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK); | |
c906108c | 360 | #else |
a198b876 SC |
361 | #ifdef O_NDELAY |
362 | (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY); | |
c906108c | 363 | #endif |
a198b876 SC |
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); | |
c906108c SS |
383 | ch = _tuiHandleResizeDuringIO (ch); |
384 | ||
a198b876 SC |
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 | ||
c906108c SS |
411 | if (m_isCommandChar (ch)) |
412 | { /* Handle prev/next/up/down here */ | |
c906108c | 413 | ch = tuiDispatchCtrlChar (ch); |
c906108c | 414 | } |
a198b876 | 415 | |
c906108c SS |
416 | if (ch == '\n' || ch == '\r' || ch == '\f') |
417 | cmdWin->detail.commandInfo.curch = 0; | |
a198b876 | 418 | #if 0 |
c906108c SS |
419 | else |
420 | tuiIncrCommandCharCountBy (1); | |
a198b876 SC |
421 | #endif |
422 | if (ch == KEY_BACKSPACE) | |
423 | return '\b'; | |
424 | ||
c906108c | 425 | return ch; |
a198b876 | 426 | } |
c906108c | 427 | |
c906108c | 428 | |
a198b876 SC |
429 | /* Cleanup when a resize has occured. |
430 | Returns the character that must be processed. */ | |
c906108c | 431 | static unsigned int |
eca6576c | 432 | _tuiHandleResizeDuringIO (unsigned int originalCh) |
c906108c SS |
433 | { |
434 | if (tuiWinResized ()) | |
435 | { | |
e8b915dc | 436 | tuiRefreshAll (); |
c906108c SS |
437 | dont_repeat (); |
438 | tuiSetWinResizedTo (FALSE); | |
c906108c SS |
439 | return '\n'; |
440 | } | |
441 | else | |
442 | return originalCh; | |
a198b876 | 443 | } |