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