1/* GDB hooks for TUI.
2
3   Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5   This file is part of GDB.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place - Suite 330,
20   Boston, MA 02111-1307, USA.  */
21
22#include "defs.h"
23#include "symtab.h"
24#include "inferior.h"
25#include "command.h"
26#include "bfd.h"
27#include "symfile.h"
28#include "objfiles.h"
29#include "target.h"
30#include "gdbcore.h"
31#include "event-loop.h"
32#include "event-top.h"
33#include "frame.h"
34#include "breakpoint.h"
35#include "gdb-events.h"
36#include "ui-out.h"
37#include "top.h"
38#include <unistd.h>
39#include <fcntl.h>
40
41#include "tui/tui.h"
42#include "tui/tui-hooks.h"
43#include "tui/tui-data.h"
44#include "tui/tui-layout.h"
45#include "tui/tui-io.h"
46#include "tui/tui-regs.h"
47#include "tui/tui-win.h"
48#include "tui/tui-stack.h"
49#include "tui/tui-windata.h"
50#include "tui/tui-winsource.h"
51
52#include "gdb_curses.h"
53
54/* This redefines CTRL if it is not already defined, so it must come
55   after terminal state releated include files like <term.h> and
56   "gdb_curses.h".  */
57#include "readline/readline.h"
58
59int tui_target_has_run = 0;
60
61static void (* tui_target_new_objfile_chain) (struct objfile*);
62
63static void
64tui_new_objfile_hook (struct objfile* objfile)
65{
66  if (tui_active)
67    tui_display_main ();
68
69  if (tui_target_new_objfile_chain)
70    tui_target_new_objfile_chain (objfile);
71}
72
73static int
74tui_query_hook (const char * msg, va_list argp)
75{
76  int retval;
77  int ans2;
78  int answer;
79
80  /* Automatically answer "yes" if input is not from a terminal.  */
81  if (!input_from_terminal_p ())
82    return 1;
83
84  echo ();
85  while (1)
86    {
87      wrap_here ("");		/* Flush any buffered output */
88      gdb_flush (gdb_stdout);
89
90      vfprintf_filtered (gdb_stdout, msg, argp);
91      printf_filtered ("(y or n) ");
92
93      wrap_here ("");
94      gdb_flush (gdb_stdout);
95
96      answer = tui_getc (stdin);
97      clearerr (stdin);		/* in case of C-d */
98      if (answer == EOF)	/* C-d */
99	{
100	  retval = 1;
101	  break;
102	}
103      /* Eat rest of input line, to EOF or newline */
104      if (answer != '\n')
105	do
106	  {
107            ans2 = tui_getc (stdin);
108	    clearerr (stdin);
109	  }
110	while (ans2 != EOF && ans2 != '\n' && ans2 != '\r');
111
112      if (answer >= 'a')
113	answer -= 040;
114      if (answer == 'Y')
115	{
116	  retval = 1;
117	  break;
118	}
119      if (answer == 'N')
120	{
121	  retval = 0;
122	  break;
123	}
124      printf_filtered ("Please answer y or n.\n");
125    }
126  noecho ();
127  return retval;
128}
129
130/* Prevent recursion of deprecated_registers_changed_hook().  */
131static int tui_refreshing_registers = 0;
132
133static void
134tui_registers_changed_hook (void)
135{
136  struct frame_info *fi;
137
138  fi = deprecated_selected_frame;
139  if (fi && tui_refreshing_registers == 0)
140    {
141      tui_refreshing_registers = 1;
142#if 0
143      tui_check_data_values (fi);
144#endif
145      tui_refreshing_registers = 0;
146    }
147}
148
149static void
150tui_register_changed_hook (int regno)
151{
152  struct frame_info *fi;
153
154  fi = deprecated_selected_frame;
155  if (fi && tui_refreshing_registers == 0)
156    {
157      tui_refreshing_registers = 1;
158      tui_check_data_values (fi);
159      tui_refreshing_registers = 0;
160    }
161}
162
163/* Breakpoint creation hook.
164   Update the screen to show the new breakpoint.  */
165static void
166tui_event_create_breakpoint (int number)
167{
168  tui_update_all_breakpoint_info ();
169}
170
171/* Breakpoint deletion hook.
172   Refresh the screen to update the breakpoint marks.  */
173static void
174tui_event_delete_breakpoint (int number)
175{
176  tui_update_all_breakpoint_info ();
177}
178
179static void
180tui_event_modify_breakpoint (int number)
181{
182  tui_update_all_breakpoint_info ();
183}
184
185static void
186tui_event_default (int number)
187{
188  ;
189}
190
191static struct gdb_events *tui_old_event_hooks;
192
193static struct gdb_events tui_event_hooks =
194{
195  tui_event_create_breakpoint,
196  tui_event_delete_breakpoint,
197  tui_event_modify_breakpoint,
198  tui_event_default,
199  tui_event_default,
200  tui_event_default
201};
202
203/* Called when going to wait for the target.
204   Leave curses mode and setup program mode.  */
205static ptid_t
206tui_target_wait_hook (ptid_t pid, struct target_waitstatus *status)
207{
208  ptid_t res;
209
210  /* Leave tui mode (optional).  */
211#if 0
212  if (tui_active)
213    {
214      target_terminal_ours ();
215      endwin ();
216      target_terminal_inferior ();
217    }
218#endif
219  tui_target_has_run = 1;
220  res = target_wait (pid, status);
221
222  if (tui_active)
223    {
224      /* TODO: need to refresh (optional).  */
225    }
226  return res;
227}
228
229/* The selected frame has changed.  This is happens after a target
230   stop or when the user explicitly changes the frame (up/down/thread/...).  */
231static void
232tui_selected_frame_level_changed_hook (int level)
233{
234  struct frame_info *fi;
235
236  fi = deprecated_selected_frame;
237  /* Ensure that symbols for this frame are read in.  Also, determine the
238     source language of this frame, and switch to it if desired.  */
239  if (fi)
240    {
241      struct symtab *s;
242
243      s = find_pc_symtab (get_frame_pc (fi));
244      /* elz: this if here fixes the problem with the pc not being displayed
245         in the tui asm layout, with no debug symbols. The value of s
246         would be 0 here, and select_source_symtab would abort the
247         command by calling the 'error' function */
248      if (s)
249        select_source_symtab (s);
250
251      /* Display the frame position (even if there is no symbols).  */
252      tui_show_frame_info (fi);
253
254      /* Refresh the register window if it's visible.  */
255      if (tui_is_window_visible (DATA_WIN))
256        {
257          tui_refreshing_registers = 1;
258          tui_check_data_values (fi);
259          tui_refreshing_registers = 0;
260        }
261    }
262}
263
264/* Called from print_frame_info to list the line we stopped in.  */
265static void
266tui_print_frame_info_listing_hook (struct symtab *s, int line,
267                                   int stopline, int noerror)
268{
269  select_source_symtab (s);
270  tui_show_frame_info (deprecated_selected_frame);
271}
272
273/* Called when the target process died or is detached.
274   Update the status line.  */
275static void
276tui_detach_hook (void)
277{
278  tui_show_frame_info (0);
279  tui_display_main ();
280}
281
282/* Install the TUI specific hooks.  */
283void
284tui_install_hooks (void)
285{
286  deprecated_target_wait_hook = tui_target_wait_hook;
287  deprecated_selected_frame_level_changed_hook = tui_selected_frame_level_changed_hook;
288  deprecated_print_frame_info_listing_hook = tui_print_frame_info_listing_hook;
289
290  deprecated_query_hook = tui_query_hook;
291
292  /* Install the event hooks.  */
293  tui_old_event_hooks = deprecated_set_gdb_event_hooks (&tui_event_hooks);
294
295  deprecated_registers_changed_hook = tui_registers_changed_hook;
296  deprecated_register_changed_hook = tui_register_changed_hook;
297  deprecated_detach_hook = tui_detach_hook;
298}
299
300/* Remove the TUI specific hooks.  */
301void
302tui_remove_hooks (void)
303{
304  deprecated_target_wait_hook = 0;
305  deprecated_selected_frame_level_changed_hook = 0;
306  deprecated_print_frame_info_listing_hook = 0;
307  deprecated_query_hook = 0;
308  deprecated_registers_changed_hook = 0;
309  deprecated_register_changed_hook = 0;
310  deprecated_detach_hook = 0;
311
312  /* Restore the previous event hooks.  */
313  deprecated_set_gdb_event_hooks (tui_old_event_hooks);
314}
315
316void _initialize_tui_hooks (void);
317
318void
319_initialize_tui_hooks (void)
320{
321  /* Install the permanent hooks.  */
322  tui_target_new_objfile_chain = deprecated_target_new_objfile_hook;
323  deprecated_target_new_objfile_hook = tui_new_objfile_hook;
324}
325