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