Commit | Line | Data |
---|---|---|
f377b406 | 1 | /* TUI support I/O functions. |
f33c6cbf | 2 | |
32d0add0 | 3 | Copyright (C) 1998-2015 Free Software Foundation, Inc. |
f33c6cbf | 4 | |
f377b406 SC |
5 | Contributed by Hewlett-Packard Company. |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
f377b406 SC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c | 21 | |
c906108c | 22 | #include "defs.h" |
a198b876 SC |
23 | #include "target.h" |
24 | #include "event-loop.h" | |
e09d2eba | 25 | #include "event-top.h" |
a198b876 SC |
26 | #include "command.h" |
27 | #include "top.h" | |
d7b2e967 AC |
28 | #include "tui/tui.h" |
29 | #include "tui/tui-data.h" | |
30 | #include "tui/tui-io.h" | |
31 | #include "tui/tui-command.h" | |
32 | #include "tui/tui-win.h" | |
33 | #include "tui/tui-wingeneral.h" | |
34 | #include "tui/tui-file.h" | |
a198b876 SC |
35 | #include "ui-out.h" |
36 | #include "cli-out.h" | |
37 | #include <fcntl.h> | |
9d876a16 | 38 | #include <signal.h> |
614c279d | 39 | #include "filestuff.h" |
82083d6d | 40 | #include "completer.h" |
6a83354a | 41 | #include "gdb_curses.h" |
a198b876 | 42 | |
4a1bcc8c MK |
43 | /* This redefines CTRL if it is not already defined, so it must come |
44 | after terminal state releated include files like <term.h> and | |
45 | "gdb_curses.h". */ | |
46 | #include "readline/readline.h" | |
47 | ||
bcdf1568 AC |
48 | int |
49 | key_is_start_sequence (int ch) | |
50 | { | |
51 | return (ch == 27); | |
52 | } | |
53 | ||
54 | int | |
55 | key_is_end_sequence (int ch) | |
56 | { | |
57 | return (ch == 126); | |
58 | } | |
59 | ||
60 | int | |
61 | key_is_backspace (int ch) | |
62 | { | |
63 | return (ch == 8); | |
64 | } | |
65 | ||
66 | int | |
67 | key_is_command_char (int ch) | |
68 | { | |
69 | return ((ch == KEY_NPAGE) || (ch == KEY_PPAGE) | |
70 | || (ch == KEY_LEFT) || (ch == KEY_RIGHT) | |
71 | || (ch == KEY_UP) || (ch == KEY_DOWN) | |
72 | || (ch == KEY_SF) || (ch == KEY_SR) | |
e5908723 MS |
73 | || (ch == (int)'\f') |
74 | || key_is_start_sequence (ch)); | |
bcdf1568 AC |
75 | } |
76 | ||
ec6f8892 SC |
77 | /* Use definition from readline 4.3. */ |
78 | #undef CTRL_CHAR | |
08ef48c5 MS |
79 | #define CTRL_CHAR(c) \ |
80 | ((c) < control_character_threshold && (((c) & 0x80) == 0)) | |
ec6f8892 | 81 | |
a198b876 SC |
82 | /* This file controls the IO interactions between gdb and curses. |
83 | When the TUI is enabled, gdb has two modes a curses and a standard | |
84 | mode. | |
85 | ||
1cc6d956 MS |
86 | In curses mode, the gdb outputs are made in a curses command |
87 | window. For this, the gdb_stdout and gdb_stderr are redirected to | |
88 | the specific ui_file implemented by TUI. The output is handled by | |
89 | tui_puts(). The input is also controlled by curses with | |
90 | tui_getc(). The readline library uses this function to get its | |
91 | input. Several readline hooks are installed to redirect readline | |
92 | output to the TUI (see also the note below). | |
a198b876 SC |
93 | |
94 | In normal mode, the gdb outputs are restored to their origin, that | |
95 | is as if TUI is not used. Readline also uses its original getc() | |
96 | function with stdin. | |
97 | ||
1cc6d956 MS |
98 | Note SCz/2001-07-21: the current readline is not clean in its |
99 | management of the output. Even if we install a redisplay handler, | |
100 | it sometimes writes on a stdout file. It is important to redirect | |
101 | every output produced by readline, otherwise the curses window will | |
102 | be garbled. This is implemented with a pipe that TUI reads and | |
103 | readline writes to. A gdb input handler is created so that reading | |
104 | the pipe is handled automatically. This will probably not work on | |
105 | non-Unix platforms. The best fix is to make readline clean enougth | |
106 | so that is never write on stdout. | |
107 | ||
108 | Note SCz/2002-09-01: we now use more readline hooks and it seems | |
109 | that with them we don't need the pipe anymore (verified by creating | |
110 | the pipe and closing its end so that write causes a SIGPIPE). The | |
111 | old pipe code is still there and can be conditionally removed by | |
8cee930b SC |
112 | #undef TUI_USE_PIPE_FOR_READLINE. */ |
113 | ||
114 | /* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */ | |
a156a290 | 115 | #ifdef HAVE_PIPE |
8cee930b | 116 | #define TUI_USE_PIPE_FOR_READLINE |
a156a290 | 117 | #endif |
1cc6d956 | 118 | /* #undef TUI_USE_PIPE_FOR_READLINE */ |
a198b876 SC |
119 | |
120 | /* TUI output files. */ | |
121 | static struct ui_file *tui_stdout; | |
122 | static struct ui_file *tui_stderr; | |
2b68e2c5 | 123 | struct ui_out *tui_out; |
a198b876 SC |
124 | |
125 | /* GDB output files in non-curses mode. */ | |
126 | static struct ui_file *tui_old_stdout; | |
127 | static struct ui_file *tui_old_stderr; | |
2b68e2c5 | 128 | struct ui_out *tui_old_uiout; |
a198b876 SC |
129 | |
130 | /* Readline previous hooks. */ | |
840ed64d JK |
131 | static rl_getc_func_t *tui_old_rl_getc_function; |
132 | static rl_voidfunc_t *tui_old_rl_redisplay_function; | |
133 | static rl_vintfunc_t *tui_old_rl_prep_terminal; | |
134 | static rl_voidfunc_t *tui_old_rl_deprep_terminal; | |
ef0b411a | 135 | static rl_compdisp_func_t *tui_old_rl_display_matches_hook; |
cc88a640 | 136 | static int tui_old_rl_echoing_p; |
a198b876 SC |
137 | |
138 | /* Readline output stream. | |
139 | Should be removed when readline is clean. */ | |
140 | static FILE *tui_rl_outstream; | |
141 | static FILE *tui_old_rl_outstream; | |
8cee930b | 142 | #ifdef TUI_USE_PIPE_FOR_READLINE |
a198b876 | 143 | static int tui_readline_pipe[2]; |
8cee930b | 144 | #endif |
c906108c | 145 | |
57266a33 SC |
146 | /* The last gdb prompt that was registered in readline. |
147 | This may be the main gdb prompt or a secondary prompt. */ | |
148 | static char *tui_rl_saved_prompt; | |
149 | ||
82083d6d | 150 | static int tui_handle_resize_during_io (int, int); |
c906108c | 151 | |
8cee930b SC |
152 | static void |
153 | tui_putc (char c) | |
154 | { | |
155 | char buf[2]; | |
156 | ||
157 | buf[0] = c; | |
158 | buf[1] = 0; | |
159 | tui_puts (buf); | |
160 | } | |
c906108c | 161 | |
518be979 DE |
162 | /* Print the string in the curses command window. |
163 | The output is buffered. It is up to the caller to refresh the screen | |
164 | if necessary. */ | |
165 | ||
c906108c | 166 | void |
a198b876 | 167 | tui_puts (const char *string) |
c906108c | 168 | { |
a198b876 SC |
169 | static int tui_skip_line = -1; |
170 | char c; | |
171 | WINDOW *w; | |
c906108c | 172 | |
6d012f14 | 173 | w = TUI_CMD_WIN->generic.handle; |
a198b876 | 174 | while ((c = *string++) != 0) |
c906108c | 175 | { |
a198b876 SC |
176 | /* Catch annotation and discard them. We need two \032 and |
177 | discard until a \n is seen. */ | |
178 | if (c == '\032') | |
179 | { | |
180 | tui_skip_line++; | |
181 | } | |
182 | else if (tui_skip_line != 1) | |
183 | { | |
184 | tui_skip_line = -1; | |
312809f8 EZ |
185 | /* Expand TABs, since ncurses on MS-Windows doesn't. */ |
186 | if (c == '\t') | |
187 | { | |
188 | int line, col; | |
189 | ||
190 | getyx (w, line, col); | |
191 | do | |
192 | { | |
193 | waddch (w, ' '); | |
194 | col++; | |
195 | } while ((col % 8) != 0); | |
196 | } | |
197 | else | |
198 | waddch (w, c); | |
a198b876 SC |
199 | } |
200 | else if (c == '\n') | |
201 | tui_skip_line = -1; | |
202 | } | |
6d012f14 AC |
203 | getyx (w, TUI_CMD_WIN->detail.command_info.cur_line, |
204 | TUI_CMD_WIN->detail.command_info.curch); | |
9a2b4c1b MS |
205 | TUI_CMD_WIN->detail.command_info.start_line |
206 | = TUI_CMD_WIN->detail.command_info.cur_line; | |
a198b876 SC |
207 | } |
208 | ||
209 | /* Readline callback. | |
210 | Redisplay the command line with its prompt after readline has | |
211 | changed the edited text. */ | |
e09d2eba | 212 | void |
a198b876 SC |
213 | tui_redisplay_readline (void) |
214 | { | |
215 | int prev_col; | |
216 | int height; | |
217 | int col, line; | |
218 | int c_pos; | |
219 | int c_line; | |
220 | int in; | |
221 | WINDOW *w; | |
222 | char *prompt; | |
223 | int start_line; | |
e3da6fc5 SC |
224 | |
225 | /* Detect when we temporarily left SingleKey and now the readline | |
1cc6d956 | 226 | edit buffer is empty, automatically restore the SingleKey |
9b8d6827 SC |
227 | mode. The restore must only be done if the command has finished. |
228 | The command could call prompt_for_continue and we must not | |
229 | restore SingleKey so that the prompt and normal keymap are used. */ | |
230 | if (tui_current_key_mode == TUI_ONE_COMMAND_MODE && rl_end == 0 | |
231 | && immediate_quit == 0) | |
6d012f14 | 232 | tui_set_key_mode (TUI_SINGLE_KEY_MODE); |
e3da6fc5 | 233 | |
6d012f14 | 234 | if (tui_current_key_mode == TUI_SINGLE_KEY_MODE) |
e09d2eba SC |
235 | prompt = ""; |
236 | else | |
57266a33 | 237 | prompt = tui_rl_saved_prompt; |
a198b876 SC |
238 | |
239 | c_pos = -1; | |
240 | c_line = -1; | |
6d012f14 AC |
241 | w = TUI_CMD_WIN->generic.handle; |
242 | start_line = TUI_CMD_WIN->detail.command_info.start_line; | |
a198b876 SC |
243 | wmove (w, start_line, 0); |
244 | prev_col = 0; | |
245 | height = 1; | |
246 | for (in = 0; prompt && prompt[in]; in++) | |
247 | { | |
248 | waddch (w, prompt[in]); | |
249 | getyx (w, line, col); | |
588dcc3e | 250 | if (col <= prev_col) |
a198b876 SC |
251 | height++; |
252 | prev_col = col; | |
253 | } | |
588dcc3e | 254 | for (in = 0; in <= rl_end; in++) |
a198b876 SC |
255 | { |
256 | unsigned char c; | |
257 | ||
a198b876 SC |
258 | if (in == rl_point) |
259 | { | |
260 | getyx (w, c_line, c_pos); | |
261 | } | |
262 | ||
588dcc3e PP |
263 | if (in == rl_end) |
264 | break; | |
265 | ||
266 | c = (unsigned char) rl_line_buffer[in]; | |
a198b876 SC |
267 | if (CTRL_CHAR (c) || c == RUBOUT) |
268 | { | |
269 | waddch (w, '^'); | |
270 | waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?'); | |
271 | } | |
312809f8 EZ |
272 | else if (c == '\t') |
273 | { | |
274 | /* Expand TABs, since ncurses on MS-Windows doesn't. */ | |
275 | getyx (w, line, col); | |
276 | do | |
277 | { | |
278 | waddch (w, ' '); | |
279 | col++; | |
280 | } while ((col % 8) != 0); | |
281 | } | |
c906108c SS |
282 | else |
283 | { | |
a198b876 | 284 | waddch (w, c); |
c906108c | 285 | } |
a198b876 SC |
286 | if (c == '\n') |
287 | { | |
6d012f14 AC |
288 | getyx (w, TUI_CMD_WIN->detail.command_info.start_line, |
289 | TUI_CMD_WIN->detail.command_info.curch); | |
a198b876 SC |
290 | } |
291 | getyx (w, line, col); | |
292 | if (col < prev_col) | |
293 | height++; | |
294 | prev_col = col; | |
c906108c | 295 | } |
a198b876 | 296 | wclrtobot (w); |
6d012f14 AC |
297 | getyx (w, TUI_CMD_WIN->detail.command_info.start_line, |
298 | TUI_CMD_WIN->detail.command_info.curch); | |
a198b876 | 299 | if (c_line >= 0) |
d75e970c SC |
300 | { |
301 | wmove (w, c_line, c_pos); | |
6d012f14 AC |
302 | TUI_CMD_WIN->detail.command_info.cur_line = c_line; |
303 | TUI_CMD_WIN->detail.command_info.curch = c_pos; | |
d75e970c | 304 | } |
6d012f14 | 305 | TUI_CMD_WIN->detail.command_info.start_line -= height - 1; |
a198b876 | 306 | |
a198b876 SC |
307 | wrefresh (w); |
308 | fflush(stdout); | |
309 | } | |
310 | ||
1cc6d956 MS |
311 | /* Readline callback to prepare the terminal. It is called once each |
312 | time we enter readline. Terminal is already setup in curses | |
313 | mode. */ | |
a198b876 | 314 | static void |
88fa91b4 | 315 | tui_prep_terminal (int notused1) |
c906108c | 316 | { |
57266a33 SC |
317 | /* Save the prompt registered in readline to correctly display it. |
318 | (we can't use gdb_prompt() due to secondary prompts and can't use | |
319 | rl_prompt because it points to an alloca buffer). */ | |
320 | xfree (tui_rl_saved_prompt); | |
36d6eb95 | 321 | tui_rl_saved_prompt = rl_prompt != NULL ? xstrdup (rl_prompt) : NULL; |
a198b876 | 322 | } |
c906108c | 323 | |
1cc6d956 MS |
324 | /* Readline callback to restore the terminal. It is called once each |
325 | time we leave readline. There is nothing to do in curses mode. */ | |
a198b876 SC |
326 | static void |
327 | tui_deprep_terminal (void) | |
328 | { | |
329 | } | |
c906108c | 330 | |
8cee930b | 331 | #ifdef TUI_USE_PIPE_FOR_READLINE |
a198b876 SC |
332 | /* Read readline output pipe and feed the command window with it. |
333 | Should be removed when readline is clean. */ | |
334 | static void | |
01f69b38 | 335 | tui_readline_output (int error, gdb_client_data data) |
a198b876 SC |
336 | { |
337 | int size; | |
338 | char buf[256]; | |
c906108c | 339 | |
a198b876 SC |
340 | size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1); |
341 | if (size > 0 && tui_active) | |
c906108c | 342 | { |
a198b876 SC |
343 | buf[size] = 0; |
344 | tui_puts (buf); | |
c906108c | 345 | } |
a198b876 | 346 | } |
8cee930b SC |
347 | #endif |
348 | ||
82083d6d | 349 | /* TUI version of displayer.crlf. */ |
8cee930b | 350 | |
82083d6d DE |
351 | static void |
352 | tui_mld_crlf (const struct match_list_displayer *displayer) | |
8cee930b | 353 | { |
82083d6d | 354 | tui_putc ('\n'); |
8cee930b SC |
355 | } |
356 | ||
82083d6d | 357 | /* TUI version of displayer.putch. */ |
8cee930b | 358 | |
82083d6d DE |
359 | static void |
360 | tui_mld_putch (const struct match_list_displayer *displayer, int ch) | |
8cee930b | 361 | { |
82083d6d | 362 | tui_putc (ch); |
8cee930b SC |
363 | } |
364 | ||
82083d6d DE |
365 | /* TUI version of displayer.puts. */ |
366 | ||
367 | static void | |
368 | tui_mld_puts (const struct match_list_displayer *displayer, const char *s) | |
8cee930b | 369 | { |
82083d6d DE |
370 | tui_puts (s); |
371 | } | |
8cee930b | 372 | |
82083d6d DE |
373 | /* TUI version of displayer.flush. */ |
374 | ||
375 | static void | |
376 | tui_mld_flush (const struct match_list_displayer *displayer) | |
377 | { | |
378 | wrefresh (TUI_CMD_WIN->generic.handle); | |
8cee930b SC |
379 | } |
380 | ||
82083d6d | 381 | /* TUI version of displayer.erase_entire_line. */ |
8cee930b | 382 | |
8cee930b | 383 | static void |
82083d6d | 384 | tui_mld_erase_entire_line (const struct match_list_displayer *displayer) |
8cee930b | 385 | { |
82083d6d | 386 | WINDOW *w = TUI_CMD_WIN->generic.handle; |
8cee930b | 387 | |
82083d6d DE |
388 | wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0); |
389 | wclrtoeol (w); | |
390 | wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0); | |
391 | } | |
8cee930b | 392 | |
82083d6d | 393 | /* TUI version of displayer.beep. */ |
8cee930b | 394 | |
82083d6d DE |
395 | static void |
396 | tui_mld_beep (const struct match_list_displayer *displayer) | |
397 | { | |
398 | beep (); | |
399 | } | |
400 | ||
401 | /* Helper function for tui_mld_read_key. | |
402 | This temporarily replaces tui_getc for use during tab-completion | |
403 | match list display. */ | |
404 | ||
405 | static int | |
406 | tui_mld_getc (FILE *fp) | |
407 | { | |
408 | WINDOW *w = TUI_CMD_WIN->generic.handle; | |
409 | int c = wgetch (w); | |
8cee930b | 410 | |
82083d6d | 411 | c = tui_handle_resize_during_io (c, 1); |
8cee930b | 412 | |
82083d6d DE |
413 | return c; |
414 | } | |
8cee930b | 415 | |
82083d6d | 416 | /* TUI version of displayer.read_key. */ |
8cee930b | 417 | |
82083d6d DE |
418 | static int |
419 | tui_mld_read_key (const struct match_list_displayer *displayer) | |
420 | { | |
421 | rl_getc_func_t *prev = rl_getc_function; | |
422 | int c; | |
8cee930b | 423 | |
82083d6d DE |
424 | /* We can't use tui_getc as we need NEWLINE to not get emitted. */ |
425 | rl_getc_function = tui_mld_getc; | |
426 | c = rl_read_key (); | |
427 | rl_getc_function = prev; | |
428 | return c; | |
429 | } | |
8cee930b | 430 | |
82083d6d DE |
431 | /* TUI version of rl_completion_display_matches_hook. |
432 | See gdb_display_match_list for a description of the arguments. */ | |
8cee930b | 433 | |
82083d6d DE |
434 | static void |
435 | tui_rl_display_match_list (char **matches, int len, int max) | |
436 | { | |
437 | struct match_list_displayer displayer; | |
438 | ||
439 | rl_get_screen_size (&displayer.height, &displayer.width); | |
440 | displayer.crlf = tui_mld_crlf; | |
441 | displayer.putch = tui_mld_putch; | |
442 | displayer.puts = tui_mld_puts; | |
443 | displayer.flush = tui_mld_flush; | |
444 | displayer.erase_entire_line = tui_mld_erase_entire_line; | |
445 | displayer.beep = tui_mld_beep; | |
446 | displayer.read_key = tui_mld_read_key; | |
447 | ||
448 | gdb_display_match_list (matches, len, max, &displayer); | |
8cee930b | 449 | } |
a198b876 SC |
450 | |
451 | /* Setup the IO for curses or non-curses mode. | |
452 | - In non-curses mode, readline and gdb use the standard input and | |
453 | standard output/error directly. | |
454 | - In curses mode, the standard output/error is controlled by TUI | |
455 | with the tui_stdout and tui_stderr. The output is redirected in | |
456 | the curses command window. Several readline callbacks are installed | |
457 | so that readline asks for its input to the curses command window | |
458 | with wgetch(). */ | |
459 | void | |
460 | tui_setup_io (int mode) | |
461 | { | |
cc88a640 JK |
462 | extern int _rl_echoing_p; |
463 | ||
a198b876 | 464 | if (mode) |
c906108c | 465 | { |
a198b876 SC |
466 | /* Redirect readline to TUI. */ |
467 | tui_old_rl_redisplay_function = rl_redisplay_function; | |
468 | tui_old_rl_deprep_terminal = rl_deprep_term_function; | |
469 | tui_old_rl_prep_terminal = rl_prep_term_function; | |
470 | tui_old_rl_getc_function = rl_getc_function; | |
ef0b411a | 471 | tui_old_rl_display_matches_hook = rl_completion_display_matches_hook; |
a198b876 | 472 | tui_old_rl_outstream = rl_outstream; |
cc88a640 | 473 | tui_old_rl_echoing_p = _rl_echoing_p; |
a198b876 SC |
474 | rl_redisplay_function = tui_redisplay_readline; |
475 | rl_deprep_term_function = tui_deprep_terminal; | |
476 | rl_prep_term_function = tui_prep_terminal; | |
477 | rl_getc_function = tui_getc; | |
cc88a640 | 478 | _rl_echoing_p = 0; |
a198b876 SC |
479 | rl_outstream = tui_rl_outstream; |
480 | rl_prompt = 0; | |
8cee930b SC |
481 | rl_completion_display_matches_hook = tui_rl_display_match_list; |
482 | rl_already_prompted = 0; | |
a198b876 SC |
483 | |
484 | /* Keep track of previous gdb output. */ | |
485 | tui_old_stdout = gdb_stdout; | |
486 | tui_old_stderr = gdb_stderr; | |
79a45e25 | 487 | tui_old_uiout = current_uiout; |
a198b876 SC |
488 | |
489 | /* Reconfigure gdb output. */ | |
490 | gdb_stdout = tui_stdout; | |
491 | gdb_stderr = tui_stderr; | |
492 | gdb_stdlog = gdb_stdout; /* for moment */ | |
493 | gdb_stdtarg = gdb_stderr; /* for moment */ | |
8d4d924b | 494 | gdb_stdtargerr = gdb_stderr; /* for moment */ |
79a45e25 | 495 | current_uiout = tui_out; |
9d876a16 SC |
496 | |
497 | /* Save tty for SIGCONT. */ | |
498 | savetty (); | |
c906108c | 499 | } |
a198b876 | 500 | else |
c906108c | 501 | { |
a198b876 SC |
502 | /* Restore gdb output. */ |
503 | gdb_stdout = tui_old_stdout; | |
504 | gdb_stderr = tui_old_stderr; | |
505 | gdb_stdlog = gdb_stdout; /* for moment */ | |
506 | gdb_stdtarg = gdb_stderr; /* for moment */ | |
8d4d924b | 507 | gdb_stdtargerr = gdb_stderr; /* for moment */ |
79a45e25 | 508 | current_uiout = tui_old_uiout; |
a198b876 SC |
509 | |
510 | /* Restore readline. */ | |
511 | rl_redisplay_function = tui_old_rl_redisplay_function; | |
512 | rl_deprep_term_function = tui_old_rl_deprep_terminal; | |
513 | rl_prep_term_function = tui_old_rl_prep_terminal; | |
514 | rl_getc_function = tui_old_rl_getc_function; | |
ef0b411a | 515 | rl_completion_display_matches_hook = tui_old_rl_display_matches_hook; |
a198b876 | 516 | rl_outstream = tui_old_rl_outstream; |
cc88a640 | 517 | _rl_echoing_p = tui_old_rl_echoing_p; |
bd9b0abf | 518 | rl_already_prompted = 0; |
9d876a16 SC |
519 | |
520 | /* Save tty for SIGCONT. */ | |
521 | savetty (); | |
522 | } | |
523 | } | |
524 | ||
525 | #ifdef SIGCONT | |
526 | /* Catch SIGCONT to restore the terminal and refresh the screen. */ | |
527 | static void | |
528 | tui_cont_sig (int sig) | |
529 | { | |
530 | if (tui_active) | |
531 | { | |
532 | /* Restore the terminal setting because another process (shell) | |
533 | might have changed it. */ | |
534 | resetty (); | |
535 | ||
536 | /* Force a refresh of the screen. */ | |
a21fcd8f | 537 | tui_refresh_all_win (); |
d75e970c SC |
538 | |
539 | /* Update cursor position on the screen. */ | |
6d012f14 AC |
540 | wmove (TUI_CMD_WIN->generic.handle, |
541 | TUI_CMD_WIN->detail.command_info.start_line, | |
542 | TUI_CMD_WIN->detail.command_info.curch); | |
543 | wrefresh (TUI_CMD_WIN->generic.handle); | |
c906108c | 544 | } |
9d876a16 | 545 | signal (sig, tui_cont_sig); |
a198b876 | 546 | } |
9d876a16 | 547 | #endif |
c906108c | 548 | |
a198b876 SC |
549 | /* Initialize the IO for gdb in curses mode. */ |
550 | void | |
d02c80cd | 551 | tui_initialize_io (void) |
a198b876 | 552 | { |
9d876a16 SC |
553 | #ifdef SIGCONT |
554 | signal (SIGCONT, tui_cont_sig); | |
555 | #endif | |
556 | ||
a198b876 SC |
557 | /* Create tui output streams. */ |
558 | tui_stdout = tui_fileopen (stdout); | |
559 | tui_stderr = tui_fileopen (stderr); | |
560 | tui_out = tui_out_new (tui_stdout); | |
561 | ||
43df09d9 | 562 | /* Create the default UI. */ |
4801a9a3 | 563 | tui_old_uiout = cli_out_new (gdb_stdout); |
a198b876 | 564 | |
8cee930b | 565 | #ifdef TUI_USE_PIPE_FOR_READLINE |
1cc6d956 MS |
566 | /* Temporary solution for readline writing to stdout: redirect |
567 | readline output in a pipe, read that pipe and output the content | |
568 | in the curses command window. */ | |
614c279d | 569 | if (gdb_pipe_cloexec (tui_readline_pipe) != 0) |
e0e6bcab GB |
570 | error (_("Cannot create pipe for readline")); |
571 | ||
a198b876 SC |
572 | tui_rl_outstream = fdopen (tui_readline_pipe[1], "w"); |
573 | if (tui_rl_outstream == 0) | |
e0e6bcab GB |
574 | error (_("Cannot redirect readline output")); |
575 | ||
0f59c96f | 576 | setvbuf (tui_rl_outstream, (char*) NULL, _IOLBF, 0); |
c906108c | 577 | |
a198b876 SC |
578 | #ifdef O_NONBLOCK |
579 | (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK); | |
c906108c | 580 | #else |
a198b876 SC |
581 | #ifdef O_NDELAY |
582 | (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY); | |
c906108c | 583 | #endif |
a198b876 | 584 | #endif |
a198b876 | 585 | add_file_handler (tui_readline_pipe[0], tui_readline_output, 0); |
8cee930b SC |
586 | #else |
587 | tui_rl_outstream = stdout; | |
588 | #endif | |
a198b876 SC |
589 | } |
590 | ||
1cc6d956 MS |
591 | /* Get a character from the command window. This is called from the |
592 | readline package. */ | |
a198b876 SC |
593 | int |
594 | tui_getc (FILE *fp) | |
595 | { | |
596 | int ch; | |
597 | WINDOW *w; | |
598 | ||
6d012f14 | 599 | w = TUI_CMD_WIN->generic.handle; |
a198b876 | 600 | |
8cee930b | 601 | #ifdef TUI_USE_PIPE_FOR_READLINE |
a198b876 | 602 | /* Flush readline output. */ |
01f69b38 | 603 | tui_readline_output (0, 0); |
8cee930b SC |
604 | #endif |
605 | ||
a198b876 | 606 | ch = wgetch (w); |
82083d6d | 607 | ch = tui_handle_resize_during_io (ch, 0); |
c906108c | 608 | |
1cc6d956 MS |
609 | /* The \n must be echoed because it will not be printed by |
610 | readline. */ | |
a198b876 SC |
611 | if (ch == '\n') |
612 | { | |
613 | /* When hitting return with an empty input, gdb executes the last | |
614 | command. If we emit a newline, this fills up the command window | |
615 | with empty lines with gdb prompt at beginning. Instead of that, | |
616 | stay on the same line but provide a visual effect to show the | |
617 | user we recognized the command. */ | |
618 | if (rl_end == 0) | |
619 | { | |
6d012f14 | 620 | wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0); |
a198b876 SC |
621 | |
622 | /* Clear the line. This will blink the gdb prompt since | |
623 | it will be redrawn at the same line. */ | |
624 | wclrtoeol (w); | |
625 | wrefresh (w); | |
626 | napms (20); | |
627 | } | |
628 | else | |
629 | { | |
d9080678 PP |
630 | /* Move cursor to the end of the command line before emitting the |
631 | newline. We need to do so because when ncurses outputs a newline | |
632 | it truncates any text that appears past the end of the cursor. */ | |
633 | int px = TUI_CMD_WIN->detail.command_info.curch; | |
634 | int py = TUI_CMD_WIN->detail.command_info.cur_line; | |
635 | px += rl_end - rl_point; | |
636 | py += px / TUI_CMD_WIN->generic.width; | |
637 | px %= TUI_CMD_WIN->generic.width; | |
638 | wmove (w, py, px); | |
639 | waddch (w, ch); | |
a198b876 SC |
640 | } |
641 | } | |
642 | ||
bcdf1568 | 643 | if (key_is_command_char (ch)) |
1cc6d956 | 644 | { /* Handle prev/next/up/down here. */ |
b0a30fce | 645 | ch = tui_dispatch_ctrl_char (ch); |
c906108c | 646 | } |
a198b876 | 647 | |
c906108c | 648 | if (ch == '\n' || ch == '\r' || ch == '\f') |
6d012f14 | 649 | TUI_CMD_WIN->detail.command_info.curch = 0; |
a198b876 SC |
650 | if (ch == KEY_BACKSPACE) |
651 | return '\b'; | |
d64e57fa PP |
652 | |
653 | if (async_command_editing_p && key_is_start_sequence (ch)) | |
654 | { | |
655 | int ch_pending; | |
656 | ||
657 | nodelay (w, TRUE); | |
658 | ch_pending = wgetch (w); | |
659 | nodelay (w, FALSE); | |
660 | ||
661 | /* If we have pending input following a start sequence, call the stdin | |
662 | event handler again because ncurses may have already read and stored | |
663 | the input into its internal buffer, meaning that we won't get an stdin | |
664 | event for it. If we don't compensate for this missed stdin event, key | |
665 | sequences as Alt_F (^[f) will not behave promptly. | |
666 | ||
667 | (We only compensates for the missed 2nd byte of a key sequence because | |
668 | 2-byte sequences are by far the most commonly used. ncurses may have | |
669 | buffered a larger, 3+-byte key sequence though it remains to be seen | |
670 | whether it is useful to compensate for all the bytes of such | |
671 | sequences.) */ | |
672 | if (ch_pending != ERR) | |
673 | { | |
674 | ungetch (ch_pending); | |
675 | call_stdin_event_handler_again_p = 1; | |
676 | } | |
677 | } | |
678 | ||
c906108c | 679 | return ch; |
a198b876 | 680 | } |
c906108c | 681 | |
312809f8 EZ |
682 | /* Utility function to expand TABs in a STRING into spaces. STRING |
683 | will be displayed starting at column COL, and is assumed to include | |
684 | no newlines. The returned expanded string is malloc'ed. */ | |
685 | ||
686 | char * | |
687 | tui_expand_tabs (const char *string, int col) | |
688 | { | |
689 | int n_adjust; | |
690 | const char *s; | |
691 | char *ret, *q; | |
692 | ||
693 | /* 1. How many additional characters do we need? */ | |
694 | for (n_adjust = 0, s = string; s; ) | |
695 | { | |
696 | s = strpbrk (s, "\t"); | |
697 | if (s) | |
698 | { | |
699 | col += (s - string) + n_adjust; | |
700 | /* Adjustment for the next tab stop, minus one for the TAB | |
701 | we replace with spaces. */ | |
702 | n_adjust += 8 - (col % 8) - 1; | |
703 | s++; | |
704 | } | |
705 | } | |
706 | ||
707 | /* Allocate the copy. */ | |
708 | ret = q = xmalloc (strlen (string) + n_adjust + 1); | |
709 | ||
710 | /* 2. Copy the original string while replacing TABs with spaces. */ | |
711 | for (s = string; s; ) | |
712 | { | |
713 | char *s1 = strpbrk (s, "\t"); | |
714 | if (s1) | |
715 | { | |
716 | if (s1 > s) | |
717 | { | |
718 | strncpy (q, s, s1 - s); | |
719 | q += s1 - s; | |
720 | col += s1 - s; | |
721 | } | |
722 | do { | |
723 | *q++ = ' '; | |
724 | col++; | |
725 | } while ((col % 8) != 0); | |
726 | s1++; | |
727 | } | |
728 | else | |
729 | strcpy (q, s); | |
730 | s = s1; | |
731 | } | |
732 | ||
733 | return ret; | |
734 | } | |
c906108c | 735 | |
a198b876 SC |
736 | /* Cleanup when a resize has occured. |
737 | Returns the character that must be processed. */ | |
82083d6d DE |
738 | |
739 | static int | |
740 | tui_handle_resize_during_io (int original_ch, int for_completion) | |
c906108c | 741 | { |
dd1abb8c | 742 | if (tui_win_resized ()) |
c906108c | 743 | { |
36900355 | 744 | tui_resize_all (); |
a21fcd8f | 745 | tui_refresh_all_win (); |
fc6b1256 | 746 | tui_update_gdb_sizes (); |
dd1abb8c | 747 | tui_set_win_resized_to (FALSE); |
82083d6d DE |
748 | if (!for_completion) |
749 | { | |
750 | dont_repeat (); | |
751 | return '\n'; | |
752 | } | |
c906108c | 753 | } |
82083d6d DE |
754 | |
755 | return original_ch; | |
a198b876 | 756 | } |