* tui-hooks.c: Update include order.
[deliverable/binutils-gdb.git] / gdb / tui / tui.c
CommitLineData
f377b406 1/* General functions for the WDB TUI.
f33c6cbf 2
96ec9981 3 Copyright 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
f33c6cbf
AC
4 Inc.
5
f377b406
SC
6 Contributed by Hewlett-Packard Company.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c
SS
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <ctype.h>
28#include <malloc.h>
c906108c
SS
29#ifdef HAVE_TERM_H
30#include <term.h>
31#endif
32#include <signal.h>
33#include <fcntl.h>
6d7fbb5a 34#if 0
c906108c 35#include <termio.h>
6d7fbb5a 36#endif
c906108c
SS
37#include <setjmp.h>
38#include "defs.h"
39#include "gdbcmd.h"
40#include "tui.h"
41#include "tuiData.h"
42#include "tuiLayout.h"
43#include "tuiIO.h"
44#include "tuiRegs.h"
377c38ea 45#include "tuiStack.h"
c906108c 46#include "tuiWin.h"
c6f60bcd 47#include "tuiSourceWin.h"
39db33d6
SC
48#include "readline/readline.h"
49#include "target.h"
50#include "frame.h"
51#include "breakpoint.h"
c6f60bcd 52#include "inferior.h"
52575520
EZ
53#include "symtab.h"
54#include "source.h"
39db33d6 55
96ec9981
DJ
56#ifdef HAVE_NCURSES_H
57#include <ncurses.h>
58#else
59#ifdef HAVE_CURSES_H
60#include <curses.h>
61#endif
62#endif
63
39db33d6
SC
64/* Tells whether the TUI is active or not. */
65int tui_active = 0;
66static int tui_finish_init = 1;
67
e09d2eba
SC
68enum tui_key_mode tui_current_key_mode = tui_command_mode;
69
70struct tui_char_command
71{
72 unsigned char key;
73 const char* cmd;
74};
75
76/* Key mapping to gdb commands when the TUI is using the single key mode. */
77static const struct tui_char_command tui_commands[] = {
78 { 'c', "continue" },
79 { 'd', "down" },
80 { 'f', "finish" },
81 { 'n', "next" },
82 { 'r', "run" },
83 { 's', "step" },
84 { 'u', "up" },
85 { 'v', "info locals" },
86 { 'w', "where" },
87 { 0, 0 },
88};
89
90static Keymap tui_keymap;
91static Keymap tui_readline_standard_keymap;
92
93/* TUI readline command.
94 Switch the output mode between TUI/standard gdb. */
39db33d6 95static int
88fa91b4 96tui_rl_switch_mode (int notused1, int notused2)
c906108c 97{
39db33d6 98 if (tui_active)
c906108c 99 {
39db33d6 100 tui_disable ();
c6f60bcd 101 rl_prep_terminal (0);
39db33d6
SC
102 }
103 else
104 {
c6f60bcd 105 rl_deprep_terminal ();
39db33d6 106 tui_enable ();
c906108c 107 }
c906108c 108
39db33d6
SC
109 /* Clear the readline in case switching occurred in middle of something. */
110 if (rl_end)
111 rl_kill_text (0, rl_end);
112
113 /* Since we left the curses mode, the terminal mode is restored to
114 some previous state. That state may not be suitable for readline
115 to work correctly (it may be restored in line mode). We force an
116 exit of the current readline so that readline is re-entered and it
117 will be able to setup the terminal for its needs. By re-entering
118 in readline, we also redisplay its prompt in the non-curses mode. */
119 rl_newline (1, '\n');
c6f60bcd
SC
120
121 /* Make sure the \n we are returning does not repeat the last command. */
122 dont_repeat ();
39db33d6
SC
123 return 0;
124}
c906108c 125
6ba6ffa2
SC
126/* TUI readline command.
127 Change the TUI layout to show a next layout.
377c38ea
SC
128 This function is bound to CTRL-X 2. It is intended to provide
129 a functionality close to the Emacs split-window command. We always
130 show two windows (src+asm), (src+regs) or (asm+regs). */
131static int
88fa91b4 132tui_rl_change_windows (int notused1, int notused2)
377c38ea
SC
133{
134 if (!tui_active)
88fa91b4 135 tui_rl_switch_mode (0/*notused*/, 0/*notused*/);
377c38ea
SC
136
137 if (tui_active)
138 {
139 TuiLayoutType new_layout;
140 TuiRegisterDisplayType regs_type = TUI_UNDEFINED_REGS;
141
142 new_layout = currentLayout ();
143
144 /* Select a new layout to have a rolling layout behavior
145 with always two windows (except when undefined). */
146 switch (new_layout)
147 {
148 case SRC_COMMAND:
149 new_layout = SRC_DISASSEM_COMMAND;
150 break;
151
152 case DISASSEM_COMMAND:
153 new_layout = SRC_DISASSEM_COMMAND;
154 break;
155
156 case SRC_DATA_COMMAND:
157 new_layout = SRC_DISASSEM_COMMAND;
158 break;
159
160 case SRC_DISASSEM_COMMAND:
161 new_layout = DISASSEM_DATA_COMMAND;
162 break;
163
164 case DISASSEM_DATA_COMMAND:
165 new_layout = SRC_DATA_COMMAND;
166 break;
167
168 default:
169 new_layout = SRC_COMMAND;
170 break;
171 }
172 tuiSetLayout (new_layout, regs_type);
173 }
174 return 0;
175}
176
6ba6ffa2
SC
177/* TUI readline command.
178 Delete the second TUI window to only show one. */
377c38ea 179static int
88fa91b4 180tui_rl_delete_other_windows (int notused1, int notused2)
377c38ea
SC
181{
182 if (!tui_active)
88fa91b4 183 tui_rl_switch_mode (0/*notused*/, 0/*notused*/);
377c38ea
SC
184
185 if (tui_active)
186 {
187 TuiLayoutType new_layout;
188 TuiRegisterDisplayType regs_type = TUI_UNDEFINED_REGS;
189
190 new_layout = currentLayout ();
191
192 /* Kill one window. */
193 switch (new_layout)
194 {
195 case SRC_COMMAND:
196 case SRC_DATA_COMMAND:
197 case SRC_DISASSEM_COMMAND:
198 default:
199 new_layout = SRC_COMMAND;
200 break;
201
202 case DISASSEM_COMMAND:
203 case DISASSEM_DATA_COMMAND:
204 new_layout = DISASSEM_COMMAND;
205 break;
206 }
207 tuiSetLayout (new_layout, regs_type);
208 }
209 return 0;
210}
211
e09d2eba
SC
212/* TUI readline command.
213 Execute the gdb command bound to the specified key. */
214static int
215tui_rl_command_key (int count, int key)
216{
217 int i;
218
219 reinitialize_more_filter ();
220 for (i = 0; tui_commands[i].cmd; i++)
221 {
222 if (tui_commands[i].key == key)
223 {
224 /* Must save the command because it can be modified
225 by execute_command. */
226 char* cmd = alloca (strlen (tui_commands[i].cmd) + 1);
227 strcpy (cmd, tui_commands[i].cmd);
228 execute_command (cmd, TRUE);
229 return 0;
230 }
231 }
232 return 0;
233}
234
235/* TUI readline command.
236 Temporarily leave the TUI SingleKey mode to allow editing
237 a gdb command with the normal readline. Once the command
238 is executed, the TUI SingleKey mode is installed back. */
239static int
240tui_rl_command_mode (int count, int key)
241{
242 tui_set_key_mode (tui_one_command_mode);
243 return rl_insert (count, key);
244}
245
246/* TUI readline command.
247 Switch between TUI SingleKey mode and gdb readline editing. */
248static int
88fa91b4 249tui_rl_next_keymap (int notused1, int notused2)
e09d2eba
SC
250{
251 tui_set_key_mode (tui_current_key_mode == tui_command_mode
252 ? tui_single_key_mode : tui_command_mode);
253 return 0;
254}
255
256/* Readline hook to redisplay ourself the gdb prompt.
257 In the SingleKey mode, the prompt is not printed so that
258 the command window is cleaner. It will be displayed if
259 we temporarily leave the SingleKey mode. */
260static int
261tui_rl_startup_hook ()
262{
8cee930b
SC
263 rl_already_prompted = 1;
264 if (tui_current_key_mode != tui_command_mode)
265 tui_set_key_mode (tui_single_key_mode);
266 tui_redisplay_readline ();
e09d2eba
SC
267 return 0;
268}
269
270/* Change the TUI key mode by installing the appropriate readline keymap. */
271void
272tui_set_key_mode (enum tui_key_mode mode)
273{
274 tui_current_key_mode = mode;
275 rl_set_keymap (mode == tui_single_key_mode
276 ? tui_keymap : tui_readline_standard_keymap);
277 tuiShowLocatorContent ();
278}
279
39db33d6
SC
280/* Initialize readline and configure the keymap for the switching
281 key shortcut. */
c906108c 282void
39db33d6 283tui_initialize_readline ()
c906108c 284{
e09d2eba
SC
285 int i;
286 Keymap tui_ctlx_keymap;
287
39db33d6 288 rl_initialize ();
c906108c 289
6ba6ffa2 290 rl_add_defun ("tui-switch-mode", tui_rl_switch_mode, -1);
e09d2eba
SC
291 rl_add_defun ("gdb-command", tui_rl_command_key, -1);
292 rl_add_defun ("next-keymap", tui_rl_next_keymap, -1);
293
294 tui_keymap = rl_make_bare_keymap ();
295 tui_ctlx_keymap = rl_make_bare_keymap ();
296 tui_readline_standard_keymap = rl_get_keymap ();
297
298 for (i = 0; tui_commands[i].cmd; i++)
299 rl_bind_key_in_map (tui_commands[i].key, tui_rl_command_key, tui_keymap);
300
301 rl_generic_bind (ISKMAP, "\\C-x", (char*) tui_ctlx_keymap, tui_keymap);
302
303 /* Bind all other keys to tui_rl_command_mode so that we switch
304 temporarily from SingleKey mode and can enter a gdb command. */
e3da6fc5 305 for (i = ' '; i < 0x7f; i++)
e09d2eba
SC
306 {
307 int j;
308
309 for (j = 0; tui_commands[j].cmd; j++)
310 if (tui_commands[j].key == i)
311 break;
312
313 if (tui_commands[j].cmd)
314 continue;
315
316 rl_bind_key_in_map (i, tui_rl_command_mode, tui_keymap);
317 }
318
6ba6ffa2 319 rl_bind_key_in_map ('a', tui_rl_switch_mode, emacs_ctlx_keymap);
e09d2eba 320 rl_bind_key_in_map ('a', tui_rl_switch_mode, tui_ctlx_keymap);
6ba6ffa2 321 rl_bind_key_in_map ('A', tui_rl_switch_mode, emacs_ctlx_keymap);
e09d2eba 322 rl_bind_key_in_map ('A', tui_rl_switch_mode, tui_ctlx_keymap);
6ba6ffa2 323 rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, emacs_ctlx_keymap);
e09d2eba 324 rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, tui_ctlx_keymap);
6ba6ffa2 325 rl_bind_key_in_map ('1', tui_rl_delete_other_windows, emacs_ctlx_keymap);
e09d2eba 326 rl_bind_key_in_map ('1', tui_rl_delete_other_windows, tui_ctlx_keymap);
6ba6ffa2 327 rl_bind_key_in_map ('2', tui_rl_change_windows, emacs_ctlx_keymap);
e09d2eba
SC
328 rl_bind_key_in_map ('2', tui_rl_change_windows, tui_ctlx_keymap);
329 rl_bind_key_in_map ('q', tui_rl_next_keymap, tui_keymap);
330 rl_bind_key_in_map ('s', tui_rl_next_keymap, emacs_ctlx_keymap);
331 rl_bind_key_in_map ('s', tui_rl_next_keymap, tui_ctlx_keymap);
39db33d6 332}
c906108c 333
39db33d6
SC
334/* Enter in the tui mode (curses).
335 When in normal mode, it installs the tui hooks in gdb, redirects
336 the gdb output, configures the readline to work in tui mode.
337 When in curses mode, it does nothing. */
c906108c 338void
39db33d6 339tui_enable (void)
c906108c 340{
39db33d6
SC
341 if (tui_active)
342 return;
343
344 /* To avoid to initialize curses when gdb starts, there is a defered
345 curses initialization. This initialization is made only once
346 and the first time the curses mode is entered. */
347 if (tui_finish_init)
c906108c 348 {
39db33d6
SC
349 WINDOW *w;
350
351 w = initscr ();
352
377c38ea
SC
353 cbreak ();
354 noecho ();
39db33d6
SC
355 /*timeout (1);*/
356 nodelay(w, FALSE);
357 nl();
358 keypad (w, TRUE);
359 rl_initialize ();
360 setTermHeightTo (LINES);
361 setTermWidthTo (COLS);
362 def_prog_mode ();
363
c7037be1
SC
364 tuiShowFrameInfo (0);
365 tuiSetLayout (SRC_COMMAND, TUI_UNDEFINED_REGS);
39db33d6 366 tuiSetWinFocusTo (srcWin);
377c38ea 367 keypad (cmdWin->generic.handle, TRUE);
39db33d6
SC
368 wrefresh (cmdWin->generic.handle);
369 tui_finish_init = 0;
c906108c 370 }
39db33d6
SC
371 else
372 {
373 /* Save the current gdb setting of the terminal.
374 Curses will restore this state when endwin() is called. */
375 def_shell_mode ();
376 clearok (stdscr, TRUE);
377 }
c906108c 378
39db33d6
SC
379 /* Install the TUI specific hooks. */
380 tui_install_hooks ();
e09d2eba 381 rl_startup_hook = tui_rl_startup_hook;
c906108c 382
377c38ea
SC
383 tui_update_variables ();
384
39db33d6 385 tui_setup_io (1);
c906108c 386
39db33d6 387 tui_active = 1;
6e7f8b9c
AC
388 if (deprecated_selected_frame)
389 tuiShowFrameInfo (deprecated_selected_frame);
297d1607 390
e3da6fc5
SC
391 /* Restore TUI keymap. */
392 tui_set_key_mode (tui_current_key_mode);
39db33d6 393 refresh ();
1533ce99
SC
394
395 /* Update gdb's knowledge of its terminal. */
396 target_terminal_save_ours ();
3e752b04 397 tui_update_gdb_sizes ();
39db33d6
SC
398}
399
400/* Leave the tui mode.
401 Remove the tui hooks and configure the gdb output and readline
402 back to their original state. The curses mode is left so that
403 the terminal setting is restored to the point when we entered. */
c906108c 404void
39db33d6 405tui_disable (void)
c906108c 406{
39db33d6
SC
407 if (!tui_active)
408 return;
c906108c 409
e3da6fc5
SC
410 /* Restore initial readline keymap. */
411 rl_set_keymap (tui_readline_standard_keymap);
412
39db33d6
SC
413 /* Remove TUI hooks. */
414 tui_remove_hooks ();
e09d2eba
SC
415 rl_startup_hook = 0;
416 rl_already_prompted = 0;
c906108c 417
39db33d6
SC
418 /* Leave curses and restore previous gdb terminal setting. */
419 endwin ();
c906108c 420
39db33d6
SC
421 /* gdb terminal has changed, update gdb internal copy of it
422 so that terminal management with the inferior works. */
423 tui_setup_io (0);
c906108c 424
1533ce99
SC
425 /* Update gdb's knowledge of its terminal. */
426 target_terminal_save_ours ();
427
39db33d6 428 tui_active = 0;
3e752b04 429 tui_update_gdb_sizes ();
39db33d6 430}
c906108c 431
39db33d6
SC
432/* Wrapper on top of free() to ensure that input address
433 is greater than 0x0. */
c906108c 434void
eca6576c 435tuiFree (char *ptr)
c906108c
SS
436{
437 if (ptr != (char *) NULL)
438 {
b8c9b27d 439 xfree (ptr);
c906108c 440 }
39db33d6 441}
c906108c 442
c906108c 443void
297d1607 444strcat_to_buf (char *buf, int buflen, const char *itemToAdd)
c906108c
SS
445{
446 if (itemToAdd != (char *) NULL && buf != (char *) NULL)
447 {
448 if ((strlen (buf) + strlen (itemToAdd)) <= buflen)
449 strcat (buf, itemToAdd);
450 else
451 strncat (buf, itemToAdd, (buflen - strlen (buf)));
452 }
c906108c
SS
453}
454
39db33d6
SC
455#if 0
456/* Solaris <sys/termios.h> defines CTRL. */
457#ifndef CTRL
458#define CTRL(x) (x & ~0140)
459#endif
c906108c 460
39db33d6
SC
461#define FILEDES 2
462#define CHK(val, dft) (val<=0 ? dft : val)
c906108c
SS
463
464static void
c906108c 465_tuiReset (void)
c906108c
SS
466{
467 struct termio mode;
468
469 /*
c5aa993b
JM
470 ** reset the teletype mode bits to a sensible state.
471 ** Copied tset.c
472 */
c906108c
SS
473#if ! defined (USG) && defined (TIOCGETC)
474 struct tchars tbuf;
475#endif /* !USG && TIOCGETC */
476#ifdef UCB_NTTY
477 struct ltchars ltc;
478
479 if (ldisc == NTTYDISC)
480 {
481 ioctl (FILEDES, TIOCGLTC, &ltc);
482 ltc.t_suspc = CHK (ltc.t_suspc, CTRL ('Z'));
483 ltc.t_dsuspc = CHK (ltc.t_dsuspc, CTRL ('Y'));
484 ltc.t_rprntc = CHK (ltc.t_rprntc, CTRL ('R'));
485 ltc.t_flushc = CHK (ltc.t_flushc, CTRL ('O'));
486 ltc.t_werasc = CHK (ltc.t_werasc, CTRL ('W'));
487 ltc.t_lnextc = CHK (ltc.t_lnextc, CTRL ('V'));
488 ioctl (FILEDES, TIOCSLTC, &ltc);
489 }
490#endif /* UCB_NTTY */
491#ifndef USG
492#ifdef TIOCGETC
493 ioctl (FILEDES, TIOCGETC, &tbuf);
494 tbuf.t_intrc = CHK (tbuf.t_intrc, CTRL ('?'));
495 tbuf.t_quitc = CHK (tbuf.t_quitc, CTRL ('\\'));
496 tbuf.t_startc = CHK (tbuf.t_startc, CTRL ('Q'));
497 tbuf.t_stopc = CHK (tbuf.t_stopc, CTRL ('S'));
498 tbuf.t_eofc = CHK (tbuf.t_eofc, CTRL ('D'));
499 /* brkc is left alone */
500 ioctl (FILEDES, TIOCSETC, &tbuf);
501#endif /* TIOCGETC */
502 mode.sg_flags &= ~(RAW
503#ifdef CBREAK
504 | CBREAK
505#endif /* CBREAK */
506 | VTDELAY | ALLDELAY);
507 mode.sg_flags |= XTABS | ECHO | CRMOD | ANYP;
c5aa993b 508#else /*USG */
c906108c
SS
509 ioctl (FILEDES, TCGETA, &mode);
510 mode.c_cc[VINTR] = CHK (mode.c_cc[VINTR], CTRL ('?'));
511 mode.c_cc[VQUIT] = CHK (mode.c_cc[VQUIT], CTRL ('\\'));
512 mode.c_cc[VEOF] = CHK (mode.c_cc[VEOF], CTRL ('D'));
513
514 mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | INLCR | IGNCR | IUCLC | IXOFF);
515 mode.c_iflag |= (BRKINT | ISTRIP | ICRNL | IXON);
516 mode.c_oflag &= ~(OLCUC | OCRNL | ONOCR | ONLRET | OFILL | OFDEL |
517 NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
518 mode.c_oflag |= (OPOST | ONLCR);
519 mode.c_cflag &= ~(CSIZE | PARODD | CLOCAL);
520#ifndef hp9000s800
521 mode.c_cflag |= (CS8 | CREAD);
c5aa993b 522#else /*hp9000s800 */
c906108c
SS
523 mode.c_cflag |= (CS8 | CSTOPB | CREAD);
524#endif /* hp9000s800 */
525 mode.c_lflag &= ~(XCASE | ECHONL | NOFLSH);
526 mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOK);
527 ioctl (FILEDES, TCSETAW, &mode);
528#endif /* USG */
529
530 return;
531} /* _tuiReset */
39db33d6
SC
532#endif
533
c6f60bcd
SC
534void
535tui_show_source (const char *file, int line)
536{
52575520 537 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
c6f60bcd
SC
538 /* make sure that the source window is displayed */
539 tuiAddWinToLayout (SRC_WIN);
540
52575520 541 tuiUpdateSourceWindowsWithLine (cursal.symtab, line);
c6f60bcd
SC
542 tuiUpdateLocatorFilename (file);
543}
1403b519
SC
544
545void
546tui_show_assembly (CORE_ADDR addr)
547{
548 tuiAddWinToLayout (DISASSEM_WIN);
549 tuiUpdateSourceWindowsWithAddr (addr);
550}
551
552int
553tui_is_window_visible (TuiWinType type)
554{
021e7609 555 if (tui_active == 0)
1403b519
SC
556 return 0;
557
558 if (winList[type] == 0)
559 return 0;
560
561 return winList[type]->generic.isVisible;
562}
563
564int
565tui_get_command_dimension (int *width, int *height)
566{
021e7609 567 if (!tui_active || !m_winPtrNotNull (cmdWin))
1403b519
SC
568 {
569 return 0;
570 }
571
572 *width = cmdWin->generic.width;
573 *height = cmdWin->generic.height;
574 return 1;
575}
This page took 0.380732 seconds and 4 git commands to generate.