summaryrefslogtreecommitdiff
path: root/gdb/gdbtk/generic/gdbtk-interp.c
blob: 1fc1b64a1ff22e452efa86c28715ee4319ee708f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Insight Definitions for GDB, the GNU debugger.
   Written by Keith Seitz <kseitz@uglyboxes.com>

   Copyright 2003 Free Software Foundation, Inc.

   This file is part of Insight.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include "defs.h"
#include "interps.h"
#include "ui-file.h"
#include "ui-out.h"
#include "cli-out.h"
#include "gdb_string.h"
#include "cli/cli-cmds.h"
#include "cli/cli-decode.h"

#include "tcl.h"
#include "tk.h"
#include "gdbtk.h"

static void gdbtk_command_loop (void);
static void hack_disable_interpreter_exec (char *, int);

struct gdbtk_interp_data
{
  struct ui_file *_stdout;
  struct ui_file *_stderr;
  struct ui_file *_stdlog;
  struct ui_file *_stdtarg;
};

static struct gdbtk_interp_data *gdbtk_data;

/* See note in gdbtk_interpreter_init */
static void
hack_disable_interpreter_exec (char *args, int from_tty)
{
  error ("interpreter-exec not available when running Insight");
}

static void *
gdbtk_interpreter_init (void)
{
  /* Disable interpreter-exec. It causes us big trouble right now. */
  struct cmd_list_element *cmd = NULL;
  struct cmd_list_element *alias = NULL;
  struct cmd_list_element *prefix = NULL;

  gdbtk_init ();

  if (lookup_cmd_composition ("interpreter-exec", &alias, &prefix, &cmd))
    {
      set_cmd_cfunc (cmd, hack_disable_interpreter_exec);
    }

  return gdbtk_data;
}

static int
gdbtk_interpreter_resume (void *data)
{
  static int started = 0;
  struct gdbtk_interp_data *d = (struct gdbtk_interp_data *) data;
  gdbtk_add_hooks ();

  gdb_stdout = d->_stdout;
  gdb_stderr = d->_stderr;
  gdb_stdlog = d->_stdlog;
  gdb_stdtarg = d->_stdtarg;

  command_loop_hook = gdbtk_command_loop;

  /* 2003-02-11 keiths: We cannot actually source our main Tcl file in
     our interpreter's init function because any errors that may
     get generated will go to the wrong gdb_stderr. Instead of hacking
     our interpreter init function to force gdb_stderr to our ui_file,
     we defer sourcing the startup file until now, when gdb is ready
     to let our interpreter run. */
  if (!started)
    {
      started = 1;
      gdbtk_source_start_file ();
    }

  return 1;
}

static int
gdbtk_interpreter_suspend (void *data)
{
  return 1;
}

static int
gdbtk_interpreter_display_prompt_p (void *data)
{
  return 1;
}

static int
gdbtk_interpreter_exec (void *data, const char *command_str)
{
  return 1;
}

/* This function is called instead of gdb's internal command loop.  This is the
   last chance to do anything before entering the main Tk event loop. 
   At the end of the command, we enter the main loop. */

static void
gdbtk_command_loop (void)
{
  extern FILE *instream;

  /* We no longer want to use stdin as the command input stream */
  instream = NULL;

  if (Tcl_Eval (gdbtk_interp, "gdbtk_tcl_preloop") != TCL_OK)
    {
      const char *msg;

      /* Force errorInfo to be set up propertly.  */
      Tcl_AddErrorInfo (gdbtk_interp, "");

      msg = Tcl_GetVar (gdbtk_interp, "errorInfo", TCL_GLOBAL_ONLY);
#ifdef _WIN32
      MessageBox (NULL, msg, NULL, MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
      fputs_unfiltered (msg, gdb_stderr);
#endif
    }

#ifdef _WIN32
  close_bfds ();
#endif

  Tk_MainLoop ();
}

void
_initialize_gdbtk_interp (void)
{
  static const struct interp_procs procs = {
    gdbtk_interpreter_init,             /* init_proc */
    gdbtk_interpreter_resume,           /* resume_proc */
    gdbtk_interpreter_suspend,	        /* suspend_proc */
    gdbtk_interpreter_exec,             /* exec_proc */
    gdbtk_interpreter_display_prompt_p  /* prompt_proc_p */
  };
  struct interp *gdbtk_interp;

  gdbtk_data = 
    (struct gdbtk_interp_data *) xmalloc (sizeof (struct gdbtk_interp_data));
  memset (gdbtk_data, 0, sizeof (struct gdbtk_interp_data));
  gdbtk_data->_stdout = gdbtk_fileopen ();
  gdbtk_data->_stderr = gdbtk_fileopen ();
  gdbtk_data->_stdlog = gdbtk_fileopen ();
  gdbtk_data->_stdtarg = gdbtk_fileopen ();
  gdbtk_interp = interp_new ("insight", gdbtk_data, cli_out_new (gdbtk_data->_stdout),
			     &procs);
  interp_add (gdbtk_interp);
}