xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/interps.h (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* Manages interpreters for GDB, the GNU debugger.
2 
3    Copyright (C) 2000-2023 Free Software Foundation, Inc.
4 
5    Written by Jim Ingham <jingham@apple.com> of Apple Computer, Inc.
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
11    the Free Software Foundation; either version 3 of the License, or
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
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 #ifndef INTERPS_H
23 #define INTERPS_H
24 
25 struct ui_out;
26 struct interp;
27 struct ui;
28 class completion_tracker;
29 
30 typedef struct interp *(*interp_factory_func) (const char *name);
31 
32 /* Each interpreter kind (CLI, MI, etc.) registers itself with a call
33    to this function, passing along its name, and a pointer to a
34    function that creates a new instance of an interpreter with that
35    name.  */
36 extern void interp_factory_register (const char *name,
37 				     interp_factory_func func);
38 
39 extern struct gdb_exception interp_exec (struct interp *interp,
40 					 const char *command);
41 
42 class interp
43 {
44 public:
45   explicit interp (const char *name);
46   virtual ~interp () = 0;
47 
48   virtual void init (bool top_level)
49   {}
50 
51   virtual void resume () = 0;
52   virtual void suspend () = 0;
53 
54   virtual gdb_exception exec (const char *command) = 0;
55 
56   /* Returns the ui_out currently used to collect results for this
57      interpreter.  It can be a formatter for stdout, as is the case
58      for the console & mi outputs, or it might be a result
59      formatter.  */
60   virtual ui_out *interp_ui_out () = 0;
61 
62   /* Provides a hook for interpreters to do any additional
63      setup/cleanup that they might need when logging is enabled or
64      disabled.  */
65   virtual void set_logging (ui_file_up logfile, bool logging_redirect,
66 			    bool debug_redirect) = 0;
67 
68   /* Called before starting an event loop, to give the interpreter a
69      chance to e.g., print a prompt.  */
70   virtual void pre_command_loop ()
71   {}
72 
73   /* Returns true if this interpreter supports using the readline
74      library; false if it uses GDB's own simplified readline
75      emulation.  */
76   virtual bool supports_command_editing ()
77   { return false; }
78 
79   const char *name () const
80   {
81     return m_name.get ();
82   }
83 
84 private:
85   /* This is the name in "-i=" and "set interpreter".  */
86   gdb::unique_xmalloc_ptr<char> m_name;
87 
88 public:
89   /* Interpreters are stored in a linked list, this is the next
90      one...  */
91   struct interp *next;
92 
93   /* Has the init method been run?  */
94   bool inited = false;
95 };
96 
97 /* Look up the interpreter for NAME, creating one if none exists yet.
98    If NAME is not a interpreter type previously registered with
99    interp_factory_register, return NULL; otherwise return a pointer to
100    the interpreter.  */
101 extern struct interp *interp_lookup (struct ui *ui, const char *name);
102 
103 /* Set the current UI's top level interpreter to the interpreter named
104    NAME.  Throws an error if NAME is not a known interpreter or the
105    interpreter fails to initialize.  */
106 extern void set_top_level_interpreter (const char *name);
107 
108 /* Temporarily set the current interpreter, and reset it on
109    destruction.  */
110 class scoped_restore_interp
111 {
112 public:
113 
114   scoped_restore_interp (const char *name)
115     : m_interp (set_interp (name))
116   {
117   }
118 
119   ~scoped_restore_interp ()
120   {
121     set_interp (m_interp->name ());
122   }
123 
124   scoped_restore_interp (const scoped_restore_interp &) = delete;
125   scoped_restore_interp &operator= (const scoped_restore_interp &) = delete;
126 
127 private:
128 
129   struct interp *set_interp (const char *name);
130 
131   struct interp *m_interp;
132 };
133 
134 extern int current_interp_named_p (const char *name);
135 
136 /* Call this function to give the current interpreter an opportunity
137    to do any special handling of streams when logging is enabled or
138    disabled.  LOGFILE is the stream for the log file when logging is
139    starting and is NULL when logging is ending.  LOGGING_REDIRECT is
140    the value of the "set logging redirect" setting.  If true, the
141    interpreter should configure the output streams to send output only
142    to the logfile.  If false, the interpreter should configure the
143    output streams to send output to both the current output stream
144    (i.e., the terminal) and the log file.  DEBUG_REDIRECT is same as
145    LOGGING_REDIRECT, but for the value of "set logging debugredirect"
146    instead.  */
147 extern void current_interp_set_logging (ui_file_up logfile,
148 					bool logging_redirect,
149 					bool debug_redirect);
150 
151 /* Returns the top-level interpreter.  */
152 extern struct interp *top_level_interpreter (void);
153 
154 /* Return the current UI's current interpreter.  */
155 extern struct interp *current_interpreter (void);
156 
157 extern struct interp *command_interp (void);
158 
159 extern void clear_interpreter_hooks (void);
160 
161 /* Returns true if INTERP supports using the readline library; false
162    if it uses GDB's own simplified form of readline.  */
163 extern int interp_supports_command_editing (struct interp *interp);
164 
165 /* Called before starting an event loop, to give the interpreter a
166    chance to e.g., print a prompt.  */
167 extern void interp_pre_command_loop (struct interp *interp);
168 
169 /* List the possible interpreters which could complete the given
170    text.  */
171 extern void interpreter_completer (struct cmd_list_element *ignore,
172 				   completion_tracker &tracker,
173 				   const char *text,
174 				   const char *word);
175 
176 /* well-known interpreters */
177 #define INTERP_CONSOLE		"console"
178 #define INTERP_MI1             "mi1"
179 #define INTERP_MI2             "mi2"
180 #define INTERP_MI3             "mi3"
181 #define INTERP_MI4             "mi4"
182 #define INTERP_MI		"mi"
183 #define INTERP_TUI		"tui"
184 #define INTERP_INSIGHT		"insight"
185 
186 #endif
187