*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / tui / tui.c
CommitLineData
f377b406 1/* General functions for the WDB TUI.
f33c6cbf
AC
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
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 24
f33c6cbf
AC
25/* FIXME: cagney/2002-02-28: The GDB coding standard indicates that
26 "defs.h" should be included first. Unfortunatly some systems
27 (currently Debian GNU/Linux) include the <stdbool.h> via <curses.h>
28 and they clash with "bfd.h"'s definiton of true/false. The correct
29 fix is to remove true/false from "bfd.h", however, until that
30 happens, hack around it by including "config.h" and <curses.h>
31 first. */
32
4e8f7a8b
DJ
33#include "config.h"
34#ifdef HAVE_NCURSES_H
35#include <ncurses.h>
36#else
37#ifdef HAVE_CURSES_H
38#include <curses.h>
39#endif
40#endif
41
c906108c
SS
42#include <stdio.h>
43#include <stdlib.h>
44#include <ctype.h>
45#include <malloc.h>
c906108c
SS
46#ifdef HAVE_TERM_H
47#include <term.h>
48#endif
49#include <signal.h>
50#include <fcntl.h>
6d7fbb5a 51#if 0
c906108c 52#include <termio.h>
6d7fbb5a 53#endif
c906108c
SS
54#include <setjmp.h>
55#include "defs.h"
56#include "gdbcmd.h"
57#include "tui.h"
58#include "tuiData.h"
59#include "tuiLayout.h"
60#include "tuiIO.h"
61#include "tuiRegs.h"
377c38ea 62#include "tuiStack.h"
c906108c 63#include "tuiWin.h"
c6f60bcd 64#include "tuiSourceWin.h"
39db33d6
SC
65#include "readline/readline.h"
66#include "target.h"
67#include "frame.h"
68#include "breakpoint.h"
c6f60bcd 69#include "inferior.h"
39db33d6
SC
70
71/* Tells whether the TUI is active or not. */
72int tui_active = 0;
73static int tui_finish_init = 1;
74
75/* Switch the output mode between TUI/standard gdb. */
76static int
6ba6ffa2 77tui_rl_switch_mode (void)
c906108c 78{
39db33d6 79 if (tui_active)
c906108c 80 {
39db33d6 81 tui_disable ();
c6f60bcd
SC
82 rl_prep_terminal (0);
83
39db33d6
SC
84 printf_filtered ("Left the TUI mode\n");
85 }
86 else
87 {
c6f60bcd 88 rl_deprep_terminal ();
39db33d6
SC
89 tui_enable ();
90 printf_filtered ("Entered the TUI mode\n");
c906108c 91 }
c906108c 92
39db33d6
SC
93 /* Clear the readline in case switching occurred in middle of something. */
94 if (rl_end)
95 rl_kill_text (0, rl_end);
96
97 /* Since we left the curses mode, the terminal mode is restored to
98 some previous state. That state may not be suitable for readline
99 to work correctly (it may be restored in line mode). We force an
100 exit of the current readline so that readline is re-entered and it
101 will be able to setup the terminal for its needs. By re-entering
102 in readline, we also redisplay its prompt in the non-curses mode. */
103 rl_newline (1, '\n');
c6f60bcd
SC
104
105 /* Make sure the \n we are returning does not repeat the last command. */
106 dont_repeat ();
39db33d6
SC
107 return 0;
108}
c906108c 109
6ba6ffa2
SC
110/* TUI readline command.
111 Change the TUI layout to show a next layout.
377c38ea
SC
112 This function is bound to CTRL-X 2. It is intended to provide
113 a functionality close to the Emacs split-window command. We always
114 show two windows (src+asm), (src+regs) or (asm+regs). */
115static int
6ba6ffa2 116tui_rl_change_windows (void)
377c38ea
SC
117{
118 if (!tui_active)
6ba6ffa2 119 tui_rl_switch_mode ();
377c38ea
SC
120
121 if (tui_active)
122 {
123 TuiLayoutType new_layout;
124 TuiRegisterDisplayType regs_type = TUI_UNDEFINED_REGS;
125
126 new_layout = currentLayout ();
127
128 /* Select a new layout to have a rolling layout behavior
129 with always two windows (except when undefined). */
130 switch (new_layout)
131 {
132 case SRC_COMMAND:
133 new_layout = SRC_DISASSEM_COMMAND;
134 break;
135
136 case DISASSEM_COMMAND:
137 new_layout = SRC_DISASSEM_COMMAND;
138 break;
139
140 case SRC_DATA_COMMAND:
141 new_layout = SRC_DISASSEM_COMMAND;
142 break;
143
144 case SRC_DISASSEM_COMMAND:
145 new_layout = DISASSEM_DATA_COMMAND;
146 break;
147
148 case DISASSEM_DATA_COMMAND:
149 new_layout = SRC_DATA_COMMAND;
150 break;
151
152 default:
153 new_layout = SRC_COMMAND;
154 break;
155 }
156 tuiSetLayout (new_layout, regs_type);
157 }
158 return 0;
159}
160
6ba6ffa2
SC
161/* TUI readline command.
162 Delete the second TUI window to only show one. */
377c38ea 163static int
6ba6ffa2 164tui_rl_delete_other_windows (void)
377c38ea
SC
165{
166 if (!tui_active)
6ba6ffa2 167 tui_rl_switch_mode ();
377c38ea
SC
168
169 if (tui_active)
170 {
171 TuiLayoutType new_layout;
172 TuiRegisterDisplayType regs_type = TUI_UNDEFINED_REGS;
173
174 new_layout = currentLayout ();
175
176 /* Kill one window. */
177 switch (new_layout)
178 {
179 case SRC_COMMAND:
180 case SRC_DATA_COMMAND:
181 case SRC_DISASSEM_COMMAND:
182 default:
183 new_layout = SRC_COMMAND;
184 break;
185
186 case DISASSEM_COMMAND:
187 case DISASSEM_DATA_COMMAND:
188 new_layout = DISASSEM_COMMAND;
189 break;
190 }
191 tuiSetLayout (new_layout, regs_type);
192 }
193 return 0;
194}
195
39db33d6
SC
196/* Initialize readline and configure the keymap for the switching
197 key shortcut. */
c906108c 198void
39db33d6 199tui_initialize_readline ()
c906108c 200{
39db33d6 201 rl_initialize ();
c906108c 202
6ba6ffa2
SC
203 rl_add_defun ("tui-switch-mode", tui_rl_switch_mode, -1);
204 rl_bind_key_in_map ('a', tui_rl_switch_mode, emacs_ctlx_keymap);
205 rl_bind_key_in_map ('A', tui_rl_switch_mode, emacs_ctlx_keymap);
206 rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, emacs_ctlx_keymap);
207 rl_bind_key_in_map ('1', tui_rl_delete_other_windows, emacs_ctlx_keymap);
208 rl_bind_key_in_map ('2', tui_rl_change_windows, emacs_ctlx_keymap);
39db33d6 209}
c906108c 210
39db33d6
SC
211/* Enter in the tui mode (curses).
212 When in normal mode, it installs the tui hooks in gdb, redirects
213 the gdb output, configures the readline to work in tui mode.
214 When in curses mode, it does nothing. */
c906108c 215void
39db33d6 216tui_enable (void)
c906108c 217{
39db33d6
SC
218 if (tui_active)
219 return;
220
221 /* To avoid to initialize curses when gdb starts, there is a defered
222 curses initialization. This initialization is made only once
223 and the first time the curses mode is entered. */
224 if (tui_finish_init)
c906108c 225 {
39db33d6
SC
226 WINDOW *w;
227
228 w = initscr ();
229
377c38ea
SC
230 cbreak ();
231 noecho ();
39db33d6
SC
232 /*timeout (1);*/
233 nodelay(w, FALSE);
234 nl();
235 keypad (w, TRUE);
236 rl_initialize ();
237 setTermHeightTo (LINES);
238 setTermWidthTo (COLS);
239 def_prog_mode ();
240
c7037be1
SC
241 tuiShowFrameInfo (0);
242 tuiSetLayout (SRC_COMMAND, TUI_UNDEFINED_REGS);
39db33d6 243 tuiSetWinFocusTo (srcWin);
377c38ea 244 keypad (cmdWin->generic.handle, TRUE);
39db33d6
SC
245 wrefresh (cmdWin->generic.handle);
246 tui_finish_init = 0;
c906108c 247 }
39db33d6
SC
248 else
249 {
250 /* Save the current gdb setting of the terminal.
251 Curses will restore this state when endwin() is called. */
252 def_shell_mode ();
253 clearok (stdscr, TRUE);
254 }
c906108c 255
39db33d6
SC
256 /* Install the TUI specific hooks. */
257 tui_install_hooks ();
c906108c 258
377c38ea
SC
259 tui_update_variables ();
260
39db33d6 261 tui_setup_io (1);
c906108c 262
39db33d6
SC
263 tui_version = 1;
264 tui_active = 1;
297d1607
SC
265 if (selected_frame)
266 tuiShowFrameInfo (selected_frame);
267
39db33d6 268 refresh ();
1533ce99
SC
269
270 /* Update gdb's knowledge of its terminal. */
271 target_terminal_save_ours ();
3e752b04 272 tui_update_gdb_sizes ();
39db33d6
SC
273}
274
275/* Leave the tui mode.
276 Remove the tui hooks and configure the gdb output and readline
277 back to their original state. The curses mode is left so that
278 the terminal setting is restored to the point when we entered. */
c906108c 279void
39db33d6 280tui_disable (void)
c906108c 281{
39db33d6
SC
282 if (!tui_active)
283 return;
c906108c 284
39db33d6
SC
285 /* Remove TUI hooks. */
286 tui_remove_hooks ();
c906108c 287
39db33d6
SC
288 /* Leave curses and restore previous gdb terminal setting. */
289 endwin ();
c906108c 290
39db33d6
SC
291 /* gdb terminal has changed, update gdb internal copy of it
292 so that terminal management with the inferior works. */
293 tui_setup_io (0);
c906108c 294
1533ce99
SC
295 /* Update gdb's knowledge of its terminal. */
296 target_terminal_save_ours ();
297
39db33d6
SC
298 tui_version = 0;
299 tui_active = 0;
3e752b04 300 tui_update_gdb_sizes ();
39db33d6 301}
c906108c 302
39db33d6
SC
303/* Wrapper on top of free() to ensure that input address
304 is greater than 0x0. */
c906108c 305void
eca6576c 306tuiFree (char *ptr)
c906108c
SS
307{
308 if (ptr != (char *) NULL)
309 {
b8c9b27d 310 xfree (ptr);
c906108c 311 }
39db33d6 312}
c906108c 313
c906108c 314void
297d1607 315strcat_to_buf (char *buf, int buflen, const char *itemToAdd)
c906108c
SS
316{
317 if (itemToAdd != (char *) NULL && buf != (char *) NULL)
318 {
319 if ((strlen (buf) + strlen (itemToAdd)) <= buflen)
320 strcat (buf, itemToAdd);
321 else
322 strncat (buf, itemToAdd, (buflen - strlen (buf)));
323 }
c906108c
SS
324}
325
39db33d6
SC
326#if 0
327/* Solaris <sys/termios.h> defines CTRL. */
328#ifndef CTRL
329#define CTRL(x) (x & ~0140)
330#endif
c906108c 331
39db33d6
SC
332#define FILEDES 2
333#define CHK(val, dft) (val<=0 ? dft : val)
c906108c
SS
334
335static void
c906108c 336_tuiReset (void)
c906108c
SS
337{
338 struct termio mode;
339
340 /*
c5aa993b
JM
341 ** reset the teletype mode bits to a sensible state.
342 ** Copied tset.c
343 */
c906108c
SS
344#if ! defined (USG) && defined (TIOCGETC)
345 struct tchars tbuf;
346#endif /* !USG && TIOCGETC */
347#ifdef UCB_NTTY
348 struct ltchars ltc;
349
350 if (ldisc == NTTYDISC)
351 {
352 ioctl (FILEDES, TIOCGLTC, &ltc);
353 ltc.t_suspc = CHK (ltc.t_suspc, CTRL ('Z'));
354 ltc.t_dsuspc = CHK (ltc.t_dsuspc, CTRL ('Y'));
355 ltc.t_rprntc = CHK (ltc.t_rprntc, CTRL ('R'));
356 ltc.t_flushc = CHK (ltc.t_flushc, CTRL ('O'));
357 ltc.t_werasc = CHK (ltc.t_werasc, CTRL ('W'));
358 ltc.t_lnextc = CHK (ltc.t_lnextc, CTRL ('V'));
359 ioctl (FILEDES, TIOCSLTC, &ltc);
360 }
361#endif /* UCB_NTTY */
362#ifndef USG
363#ifdef TIOCGETC
364 ioctl (FILEDES, TIOCGETC, &tbuf);
365 tbuf.t_intrc = CHK (tbuf.t_intrc, CTRL ('?'));
366 tbuf.t_quitc = CHK (tbuf.t_quitc, CTRL ('\\'));
367 tbuf.t_startc = CHK (tbuf.t_startc, CTRL ('Q'));
368 tbuf.t_stopc = CHK (tbuf.t_stopc, CTRL ('S'));
369 tbuf.t_eofc = CHK (tbuf.t_eofc, CTRL ('D'));
370 /* brkc is left alone */
371 ioctl (FILEDES, TIOCSETC, &tbuf);
372#endif /* TIOCGETC */
373 mode.sg_flags &= ~(RAW
374#ifdef CBREAK
375 | CBREAK
376#endif /* CBREAK */
377 | VTDELAY | ALLDELAY);
378 mode.sg_flags |= XTABS | ECHO | CRMOD | ANYP;
c5aa993b 379#else /*USG */
c906108c
SS
380 ioctl (FILEDES, TCGETA, &mode);
381 mode.c_cc[VINTR] = CHK (mode.c_cc[VINTR], CTRL ('?'));
382 mode.c_cc[VQUIT] = CHK (mode.c_cc[VQUIT], CTRL ('\\'));
383 mode.c_cc[VEOF] = CHK (mode.c_cc[VEOF], CTRL ('D'));
384
385 mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | INLCR | IGNCR | IUCLC | IXOFF);
386 mode.c_iflag |= (BRKINT | ISTRIP | ICRNL | IXON);
387 mode.c_oflag &= ~(OLCUC | OCRNL | ONOCR | ONLRET | OFILL | OFDEL |
388 NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
389 mode.c_oflag |= (OPOST | ONLCR);
390 mode.c_cflag &= ~(CSIZE | PARODD | CLOCAL);
391#ifndef hp9000s800
392 mode.c_cflag |= (CS8 | CREAD);
c5aa993b 393#else /*hp9000s800 */
c906108c
SS
394 mode.c_cflag |= (CS8 | CSTOPB | CREAD);
395#endif /* hp9000s800 */
396 mode.c_lflag &= ~(XCASE | ECHONL | NOFLSH);
397 mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOK);
398 ioctl (FILEDES, TCSETAW, &mode);
399#endif /* USG */
400
401 return;
402} /* _tuiReset */
39db33d6
SC
403#endif
404
c6f60bcd
SC
405void
406tui_show_source (const char *file, int line)
407{
408 /* make sure that the source window is displayed */
409 tuiAddWinToLayout (SRC_WIN);
410
411 tuiUpdateSourceWindowsWithLine (current_source_symtab, line);
412 tuiUpdateLocatorFilename (file);
413}
1403b519
SC
414
415void
416tui_show_assembly (CORE_ADDR addr)
417{
418 tuiAddWinToLayout (DISASSEM_WIN);
419 tuiUpdateSourceWindowsWithAddr (addr);
420}
421
422int
423tui_is_window_visible (TuiWinType type)
424{
425 if (tui_version == 0)
426 return 0;
427
428 if (winList[type] == 0)
429 return 0;
430
431 return winList[type]->generic.isVisible;
432}
433
434int
435tui_get_command_dimension (int *width, int *height)
436{
437 if (!tui_version || !m_winPtrNotNull (cmdWin))
438 {
439 return 0;
440 }
441
442 *width = cmdWin->generic.width;
443 *height = cmdWin->generic.height;
444 return 1;
445}
This page took 0.290757 seconds and 4 git commands to generate.