Commit | Line | Data |
---|---|---|
4a8f6654 AC |
1 | /* Manages interpreters for GDB, the GNU debugger. |
2 | ||
618f726f | 3 | Copyright (C) 2000-2016 Free Software Foundation, Inc. |
4a8f6654 AC |
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 | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
4a8f6654 AC |
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 | |
1777feb0 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4a8f6654 AC |
21 | |
22 | /* This is just a first cut at separating out the "interpreter" | |
23 | functions of gdb into self-contained modules. There are a couple | |
24 | of open areas that need to be sorted out: | |
25 | ||
26 | 1) The interpreter explicitly contains a UI_OUT, and can insert itself | |
27 | into the event loop, but it doesn't explicitly contain hooks for readline. | |
28 | I did this because it seems to me many interpreters won't want to use | |
29 | the readline command interface, and it is probably simpler to just let | |
30 | them take over the input in their resume proc. */ | |
31 | ||
32 | #include "defs.h" | |
33 | #include "gdbcmd.h" | |
34 | #include "ui-out.h" | |
35 | #include "event-loop.h" | |
36 | #include "event-top.h" | |
37 | #include "interps.h" | |
38 | #include "completer.h" | |
4a8f6654 | 39 | #include "top.h" /* For command_loop. */ |
be34f849 | 40 | #include "continuations.h" |
4a8f6654 | 41 | |
cb814510 PA |
42 | /* Each UI has its own independent set of interpreters. */ |
43 | ||
44 | struct ui_interp_info | |
45 | { | |
46 | /* Each top level has its own independent set of interpreters. */ | |
47 | struct interp *interp_list; | |
48 | struct interp *current_interpreter; | |
49 | struct interp *top_level_interpreter; | |
50 | ||
51 | /* The interpreter that is active while `interp_exec' is active, NULL | |
52 | at all other times. */ | |
53 | struct interp *command_interpreter; | |
54 | }; | |
55 | ||
8322445e | 56 | /* Get UI's ui_interp_info object. Never returns NULL. */ |
cb814510 PA |
57 | |
58 | static struct ui_interp_info * | |
8322445e | 59 | get_interp_info (struct ui *ui) |
cb814510 | 60 | { |
cb814510 PA |
61 | if (ui->interp_info == NULL) |
62 | ui->interp_info = XCNEW (struct ui_interp_info); | |
63 | return ui->interp_info; | |
64 | } | |
b4a14fd0 | 65 | |
8322445e PA |
66 | /* Get the current UI's ui_interp_info object. Never returns |
67 | NULL. */ | |
68 | ||
69 | static struct ui_interp_info * | |
70 | get_current_interp_info (void) | |
71 | { | |
72 | return get_interp_info (current_ui); | |
73 | } | |
74 | ||
4a8f6654 AC |
75 | struct interp |
76 | { | |
1777feb0 | 77 | /* This is the name in "-i=" and set interpreter. */ |
4a8f6654 AC |
78 | const char *name; |
79 | ||
80 | /* Interpreters are stored in a linked list, this is the next | |
81 | one... */ | |
82 | struct interp *next; | |
83 | ||
84 | /* This is a cookie that an instance of the interpreter can use. | |
85 | This is a bit confused right now as the exact initialization | |
86 | sequence for it, and how it relates to the interpreter's uiout | |
87 | object is a bit confused. */ | |
88 | void *data; | |
89 | ||
1777feb0 | 90 | /* Has the init_proc been run? */ |
4a8f6654 AC |
91 | int inited; |
92 | ||
4a8f6654 AC |
93 | const struct interp_procs *procs; |
94 | int quiet_p; | |
95 | }; | |
96 | ||
1777feb0 | 97 | /* The magic initialization routine for this module. */ |
4a8f6654 AC |
98 | |
99 | void _initialize_interpreter (void); | |
100 | ||
8322445e PA |
101 | static struct interp *interp_lookup_existing (struct ui *ui, |
102 | const char *name); | |
103 | ||
4a8f6654 AC |
104 | /* interp_new - This allocates space for a new interpreter, |
105 | fills the fields from the inputs, and returns a pointer to the | |
1777feb0 | 106 | interpreter. */ |
4a8f6654 | 107 | struct interp * |
8322445e | 108 | interp_new (const char *name, const struct interp_procs *procs, void *data) |
4a8f6654 AC |
109 | { |
110 | struct interp *new_interp; | |
111 | ||
70ba0933 | 112 | new_interp = XNEW (struct interp); |
4a8f6654 AC |
113 | |
114 | new_interp->name = xstrdup (name); | |
8322445e | 115 | new_interp->data = data; |
4a8f6654 AC |
116 | new_interp->quiet_p = 0; |
117 | new_interp->procs = procs; | |
118 | new_interp->inited = 0; | |
119 | ||
4d09c5b4 AB |
120 | /* Check for required procs. */ |
121 | gdb_assert (procs->command_loop_proc != NULL); | |
122 | ||
4a8f6654 AC |
123 | return new_interp; |
124 | } | |
125 | ||
8322445e PA |
126 | /* An interpreter factory. Maps an interpreter name to the factory |
127 | function that instantiates an interpreter by that name. */ | |
128 | ||
129 | struct interp_factory | |
130 | { | |
131 | /* This is the name in "-i=INTERP" and "interpreter-exec INTERP". */ | |
132 | const char *name; | |
133 | ||
134 | /* The function that creates the interpreter. */ | |
135 | interp_factory_func func; | |
136 | }; | |
137 | ||
138 | typedef struct interp_factory *interp_factory_p; | |
139 | DEF_VEC_P(interp_factory_p); | |
140 | ||
141 | /* The registered interpreter factories. */ | |
142 | static VEC(interp_factory_p) *interpreter_factories = NULL; | |
143 | ||
144 | /* See interps.h. */ | |
145 | ||
146 | void | |
147 | interp_factory_register (const char *name, interp_factory_func func) | |
148 | { | |
149 | struct interp_factory *f; | |
150 | int ix; | |
151 | ||
152 | /* Assert that no factory for NAME is already registered. */ | |
153 | for (ix = 0; | |
154 | VEC_iterate (interp_factory_p, interpreter_factories, ix, f); | |
155 | ++ix) | |
156 | if (strcmp (f->name, name) == 0) | |
157 | { | |
158 | internal_error (__FILE__, __LINE__, | |
159 | _("interpreter factory already registered: \"%s\"\n"), | |
160 | name); | |
161 | } | |
162 | ||
163 | f = XNEW (struct interp_factory); | |
164 | f->name = name; | |
165 | f->func = func; | |
166 | VEC_safe_push (interp_factory_p, interpreter_factories, f); | |
167 | } | |
168 | ||
4a8f6654 AC |
169 | /* Add interpreter INTERP to the gdb interpreter list. The |
170 | interpreter must not have previously been added. */ | |
171 | void | |
8322445e | 172 | interp_add (struct ui *ui, struct interp *interp) |
4a8f6654 | 173 | { |
8322445e | 174 | struct ui_interp_info *ui_interp = get_interp_info (ui); |
cb814510 | 175 | |
8322445e | 176 | gdb_assert (interp_lookup_existing (ui, interp->name) == NULL); |
4a8f6654 | 177 | |
cb814510 PA |
178 | interp->next = ui_interp->interp_list; |
179 | ui_interp->interp_list = interp; | |
4a8f6654 AC |
180 | } |
181 | ||
182 | /* This sets the current interpreter to be INTERP. If INTERP has not | |
183 | been initialized, then this will also run the init proc. If the | |
184 | init proc is successful, return 1, if it fails, set the old | |
185 | interpreter back in place and return 0. If we can't restore the | |
186 | old interpreter, then raise an internal error, since we are in | |
1777feb0 | 187 | pretty bad shape at this point. |
683f2885 VP |
188 | |
189 | The TOP_LEVEL parameter tells if this new interpreter is | |
190 | the top-level one. The top-level is what is requested | |
191 | on the command line, and is responsible for reporting general | |
192 | notification about target state changes. For example, if | |
193 | MI is the top-level interpreter, then it will always report | |
194 | events such as target stops and new thread creation, even if they | |
195 | are caused by CLI commands. */ | |
4a8f6654 | 196 | int |
683f2885 | 197 | interp_set (struct interp *interp, int top_level) |
4a8f6654 | 198 | { |
cb814510 PA |
199 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
200 | struct interp *old_interp = ui_interp->current_interpreter; | |
4a8f6654 | 201 | int first_time = 0; |
4a8f6654 AC |
202 | char buffer[64]; |
203 | ||
683f2885 VP |
204 | /* If we already have an interpreter, then trying to |
205 | set top level interpreter is kinda pointless. */ | |
cb814510 PA |
206 | gdb_assert (!top_level || !ui_interp->current_interpreter); |
207 | gdb_assert (!top_level || !ui_interp->top_level_interpreter); | |
683f2885 | 208 | |
cb814510 | 209 | if (old_interp != NULL) |
4a8f6654 | 210 | { |
79a45e25 | 211 | ui_out_flush (current_uiout); |
cb814510 PA |
212 | if (old_interp->procs->suspend_proc |
213 | && !old_interp->procs->suspend_proc (old_interp->data)) | |
4a8f6654 | 214 | { |
8a3fe4f8 | 215 | error (_("Could not suspend interpreter \"%s\"."), |
cb814510 | 216 | old_interp->name); |
4a8f6654 AC |
217 | } |
218 | } | |
219 | else | |
220 | { | |
221 | first_time = 1; | |
222 | } | |
223 | ||
cb814510 | 224 | ui_interp->current_interpreter = interp; |
683f2885 | 225 | if (top_level) |
cb814510 | 226 | ui_interp->top_level_interpreter = interp; |
4a8f6654 AC |
227 | |
228 | /* We use interpreter_p for the "set interpreter" variable, so we need | |
1777feb0 | 229 | to make sure we have a malloc'ed copy for the set command to free. */ |
4a8f6654 | 230 | if (interpreter_p != NULL |
cb814510 | 231 | && strcmp (interp->name, interpreter_p) != 0) |
4a8f6654 AC |
232 | { |
233 | xfree (interpreter_p); | |
234 | ||
cb814510 | 235 | interpreter_p = xstrdup (interp->name); |
4a8f6654 AC |
236 | } |
237 | ||
1777feb0 | 238 | /* Run the init proc. If it fails, try to restore the old interp. */ |
4a8f6654 AC |
239 | |
240 | if (!interp->inited) | |
241 | { | |
242 | if (interp->procs->init_proc != NULL) | |
243 | { | |
4801a9a3 | 244 | interp->data = interp->procs->init_proc (interp, top_level); |
4a8f6654 AC |
245 | } |
246 | interp->inited = 1; | |
247 | } | |
248 | ||
4801a9a3 PA |
249 | /* Do this only after the interpreter is initialized. */ |
250 | current_uiout = interp->procs->ui_out_proc (interp); | |
251 | ||
907d819a | 252 | /* Clear out any installed interpreter hooks/event handlers. */ |
4a8f6654 AC |
253 | clear_interpreter_hooks (); |
254 | ||
255 | if (interp->procs->resume_proc != NULL | |
256 | && (!interp->procs->resume_proc (interp->data))) | |
257 | { | |
683f2885 | 258 | if (old_interp == NULL || !interp_set (old_interp, 0)) |
4a8f6654 | 259 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 260 | _("Failed to initialize new interp \"%s\" %s"), |
4a8f6654 AC |
261 | interp->name, "and could not restore old interp!\n"); |
262 | return 0; | |
263 | } | |
264 | ||
92bcb5f9 | 265 | if (!first_time && !interp_quiet_p (interp)) |
4a8f6654 | 266 | { |
92bcb5f9 PA |
267 | xsnprintf (buffer, sizeof (buffer), |
268 | "Switching to interpreter \"%.24s\".\n", interp->name); | |
269 | ui_out_text (current_uiout, buffer); | |
4a8f6654 AC |
270 | } |
271 | ||
272 | return 1; | |
273 | } | |
274 | ||
8322445e PA |
275 | /* Look up the interpreter for NAME. If no such interpreter exists, |
276 | return NULL, otherwise return a pointer to the interpreter. */ | |
277 | ||
278 | static struct interp * | |
279 | interp_lookup_existing (struct ui *ui, const char *name) | |
4a8f6654 | 280 | { |
8322445e | 281 | struct ui_interp_info *ui_interp = get_interp_info (ui); |
4a8f6654 AC |
282 | struct interp *interp; |
283 | ||
cb814510 PA |
284 | for (interp = ui_interp->interp_list; |
285 | interp != NULL; | |
286 | interp = interp->next) | |
4a8f6654 AC |
287 | { |
288 | if (strcmp (interp->name, name) == 0) | |
289 | return interp; | |
290 | } | |
291 | ||
292 | return NULL; | |
293 | } | |
294 | ||
8322445e PA |
295 | /* See interps.h. */ |
296 | ||
297 | struct interp * | |
298 | interp_lookup (struct ui *ui, const char *name) | |
299 | { | |
300 | struct interp_factory *factory; | |
301 | struct interp *interp; | |
302 | int ix; | |
303 | ||
304 | if (name == NULL || strlen (name) == 0) | |
305 | return NULL; | |
306 | ||
307 | /* Only create each interpreter once per top level. */ | |
308 | interp = interp_lookup_existing (ui, name); | |
309 | if (interp != NULL) | |
310 | return interp; | |
311 | ||
312 | for (ix = 0; | |
313 | VEC_iterate (interp_factory_p, interpreter_factories, ix, factory); | |
314 | ++ix) | |
315 | if (strcmp (factory->name, name) == 0) | |
316 | { | |
317 | interp = factory->func (name); | |
318 | interp_add (ui, interp); | |
319 | return interp; | |
320 | } | |
321 | ||
322 | return NULL; | |
323 | } | |
324 | ||
1777feb0 | 325 | /* Returns the current interpreter. */ |
4a8f6654 AC |
326 | |
327 | struct ui_out * | |
328 | interp_ui_out (struct interp *interp) | |
329 | { | |
cb814510 | 330 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
4801a9a3 | 331 | |
cb814510 PA |
332 | if (interp == NULL) |
333 | interp = ui_interp->current_interpreter; | |
334 | return interp->procs->ui_out_proc (interp); | |
4801a9a3 PA |
335 | } |
336 | ||
37ce89eb SS |
337 | int |
338 | current_interp_set_logging (int start_log, struct ui_file *out, | |
339 | struct ui_file *logfile) | |
340 | { | |
cb814510 PA |
341 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
342 | struct interp *interp = ui_interp->current_interpreter; | |
343 | ||
344 | if (interp == NULL | |
345 | || interp->procs->set_logging_proc == NULL) | |
37ce89eb SS |
346 | return 0; |
347 | ||
cb814510 | 348 | return interp->procs->set_logging_proc (interp, start_log, out, logfile); |
37ce89eb SS |
349 | } |
350 | ||
c41535fd EZ |
351 | /* Temporarily overrides the current interpreter. */ |
352 | struct interp * | |
353 | interp_set_temp (const char *name) | |
354 | { | |
cb814510 | 355 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
8322445e | 356 | struct interp *interp = interp_lookup (current_ui, name); |
cb814510 | 357 | struct interp *old_interp = ui_interp->current_interpreter; |
c41535fd EZ |
358 | |
359 | if (interp) | |
cb814510 | 360 | ui_interp->current_interpreter = interp; |
c41535fd EZ |
361 | return old_interp; |
362 | } | |
363 | ||
4801a9a3 | 364 | /* Returns the interpreter's cookie. */ |
4a8f6654 | 365 | |
4801a9a3 PA |
366 | void * |
367 | interp_data (struct interp *interp) | |
368 | { | |
369 | return interp->data; | |
370 | } | |
371 | ||
372 | /* Returns the interpreter's name. */ | |
373 | ||
374 | const char * | |
375 | interp_name (struct interp *interp) | |
376 | { | |
377 | return interp->name; | |
4a8f6654 AC |
378 | } |
379 | ||
1777feb0 | 380 | /* Returns true if the current interp is the passed in name. */ |
4a8f6654 AC |
381 | int |
382 | current_interp_named_p (const char *interp_name) | |
383 | { | |
cb814510 PA |
384 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
385 | struct interp *interp = ui_interp->current_interpreter; | |
386 | ||
387 | if (interp != NULL) | |
388 | return (strcmp (interp->name, interp_name) == 0); | |
4a8f6654 AC |
389 | |
390 | return 0; | |
391 | } | |
392 | ||
17b2616c PA |
393 | /* The interpreter that was active when a command was executed. |
394 | Normally that'd always be CURRENT_INTERPRETER, except that MI's | |
395 | -interpreter-exec command doesn't actually flip the current | |
396 | interpreter when running its sub-command. The | |
397 | `command_interpreter' global tracks when interp_exec is called | |
398 | (IOW, when -interpreter-exec is called). If that is set, it is | |
399 | INTERP in '-interpreter-exec INTERP "CMD"' or in 'interpreter-exec | |
400 | INTERP "CMD". Otherwise, interp_exec isn't active, and so the | |
401 | interpreter running the command is the current interpreter. */ | |
402 | ||
403 | struct interp * | |
404 | command_interp (void) | |
405 | { | |
cb814510 PA |
406 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
407 | ||
408 | if (ui_interp->command_interpreter != NULL) | |
409 | return ui_interp->command_interpreter; | |
17b2616c | 410 | else |
cb814510 | 411 | return ui_interp->current_interpreter; |
17b2616c PA |
412 | } |
413 | ||
4a8f6654 AC |
414 | /* Run the current command interpreter's main loop. */ |
415 | void | |
416 | current_interp_command_loop (void) | |
417 | { | |
cb814510 PA |
418 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
419 | struct interp *interp = ui_interp->current_interpreter; | |
420 | ||
421 | gdb_assert (ui_interp->current_interpreter != NULL); | |
4d09c5b4 | 422 | |
cb814510 | 423 | interp->procs->command_loop_proc (interp->data); |
4a8f6654 AC |
424 | } |
425 | ||
426 | int | |
427 | interp_quiet_p (struct interp *interp) | |
428 | { | |
cb814510 PA |
429 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
430 | ||
4a8f6654 AC |
431 | if (interp != NULL) |
432 | return interp->quiet_p; | |
433 | else | |
cb814510 | 434 | return ui_interp->current_interpreter->quiet_p; |
4a8f6654 AC |
435 | } |
436 | ||
b9362cc7 | 437 | static int |
4a8f6654 AC |
438 | interp_set_quiet (struct interp *interp, int quiet) |
439 | { | |
440 | int old_val = interp->quiet_p; | |
abbb1732 | 441 | |
4a8f6654 AC |
442 | interp->quiet_p = quiet; |
443 | return old_val; | |
444 | } | |
445 | ||
446 | /* interp_exec - This executes COMMAND_STR in the current | |
1777feb0 | 447 | interpreter. */ |
4a8f6654 | 448 | |
71fff37b | 449 | struct gdb_exception |
4a8f6654 AC |
450 | interp_exec (struct interp *interp, const char *command_str) |
451 | { | |
cb814510 PA |
452 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
453 | ||
17b2616c PA |
454 | struct gdb_exception ex; |
455 | struct interp *save_command_interp; | |
456 | ||
34dc884e DE |
457 | gdb_assert (interp->procs->exec_proc != NULL); |
458 | ||
17b2616c | 459 | /* See `command_interp' for why we do this. */ |
cb814510 PA |
460 | save_command_interp = ui_interp->command_interpreter; |
461 | ui_interp->command_interpreter = interp; | |
17b2616c PA |
462 | |
463 | ex = interp->procs->exec_proc (interp->data, command_str); | |
464 | ||
cb814510 | 465 | ui_interp->command_interpreter = save_command_interp; |
17b2616c PA |
466 | |
467 | return ex; | |
4a8f6654 AC |
468 | } |
469 | ||
907d819a MS |
470 | /* A convenience routine that nulls out all the common command hooks. |
471 | Use it when removing your interpreter in its suspend proc. */ | |
4a8f6654 | 472 | void |
11308a41 | 473 | clear_interpreter_hooks (void) |
4a8f6654 | 474 | { |
9a4105ab | 475 | deprecated_print_frame_info_listing_hook = 0; |
4a8f6654 | 476 | /*print_frame_more_info_hook = 0; */ |
9a4105ab AC |
477 | deprecated_query_hook = 0; |
478 | deprecated_warning_hook = 0; | |
9a4105ab | 479 | deprecated_interactive_hook = 0; |
9a4105ab AC |
480 | deprecated_readline_begin_hook = 0; |
481 | deprecated_readline_hook = 0; | |
482 | deprecated_readline_end_hook = 0; | |
9a4105ab AC |
483 | deprecated_context_hook = 0; |
484 | deprecated_target_wait_hook = 0; | |
485 | deprecated_call_command_hook = 0; | |
9a4105ab | 486 | deprecated_error_begin_hook = 0; |
4a8f6654 AC |
487 | } |
488 | ||
b9362cc7 | 489 | static void |
4a8f6654 AC |
490 | interpreter_exec_cmd (char *args, int from_tty) |
491 | { | |
cb814510 | 492 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
4a8f6654 AC |
493 | struct interp *old_interp, *interp_to_use; |
494 | char **prules = NULL; | |
495 | char **trule = NULL; | |
496 | unsigned int nrules; | |
497 | unsigned int i; | |
498 | int old_quiet, use_quiet; | |
5b3fca71 | 499 | struct cleanup *cleanup; |
4a8f6654 | 500 | |
d1a41061 PP |
501 | if (args == NULL) |
502 | error_no_arg (_("interpreter-exec command")); | |
503 | ||
504 | prules = gdb_buildargv (args); | |
5b3fca71 | 505 | cleanup = make_cleanup_freeargv (prules); |
4a8f6654 AC |
506 | |
507 | nrules = 0; | |
d1a41061 PP |
508 | for (trule = prules; *trule != NULL; trule++) |
509 | nrules++; | |
4a8f6654 AC |
510 | |
511 | if (nrules < 2) | |
8a3fe4f8 | 512 | error (_("usage: interpreter-exec <interpreter> [ <command> ... ]")); |
4a8f6654 | 513 | |
cb814510 | 514 | old_interp = ui_interp->current_interpreter; |
4a8f6654 | 515 | |
8322445e | 516 | interp_to_use = interp_lookup (current_ui, prules[0]); |
4a8f6654 | 517 | if (interp_to_use == NULL) |
8a3fe4f8 | 518 | error (_("Could not find interpreter \"%s\"."), prules[0]); |
4a8f6654 | 519 | |
1777feb0 | 520 | /* Temporarily set interpreters quiet. */ |
4a8f6654 AC |
521 | old_quiet = interp_set_quiet (old_interp, 1); |
522 | use_quiet = interp_set_quiet (interp_to_use, 1); | |
523 | ||
683f2885 | 524 | if (!interp_set (interp_to_use, 0)) |
8a3fe4f8 | 525 | error (_("Could not switch to interpreter \"%s\"."), prules[0]); |
4a8f6654 AC |
526 | |
527 | for (i = 1; i < nrules; i++) | |
528 | { | |
71fff37b | 529 | struct gdb_exception e = interp_exec (interp_to_use, prules[i]); |
abbb1732 | 530 | |
a7479e7e | 531 | if (e.reason < 0) |
4a8f6654 | 532 | { |
683f2885 | 533 | interp_set (old_interp, 0); |
04d1f770 DJ |
534 | interp_set_quiet (interp_to_use, use_quiet); |
535 | interp_set_quiet (old_interp, old_quiet); | |
8a3fe4f8 | 536 | error (_("error in command: \"%s\"."), prules[i]); |
4a8f6654 AC |
537 | } |
538 | } | |
539 | ||
683f2885 | 540 | interp_set (old_interp, 0); |
4a8f6654 AC |
541 | interp_set_quiet (interp_to_use, use_quiet); |
542 | interp_set_quiet (old_interp, old_quiet); | |
5b3fca71 TT |
543 | |
544 | do_cleanups (cleanup); | |
4a8f6654 AC |
545 | } |
546 | ||
1777feb0 | 547 | /* List the possible interpreters which could complete the given text. */ |
49c4e619 | 548 | static VEC (char_ptr) * |
6f937416 PA |
549 | interpreter_completer (struct cmd_list_element *ignore, |
550 | const char *text, const char *word) | |
4a8f6654 | 551 | { |
8322445e | 552 | struct interp_factory *interp; |
4a8f6654 | 553 | int textlen; |
49c4e619 | 554 | VEC (char_ptr) *matches = NULL; |
8322445e | 555 | int ix; |
4a8f6654 | 556 | |
4a8f6654 | 557 | textlen = strlen (text); |
8322445e PA |
558 | for (ix = 0; |
559 | VEC_iterate (interp_factory_p, interpreter_factories, ix, interp); | |
560 | ++ix) | |
4a8f6654 AC |
561 | { |
562 | if (strncmp (interp->name, text, textlen) == 0) | |
563 | { | |
49c4e619 TT |
564 | char *match; |
565 | ||
566 | match = (char *) xmalloc (strlen (word) + strlen (interp->name) + 1); | |
4a8f6654 | 567 | if (word == text) |
49c4e619 | 568 | strcpy (match, interp->name); |
4a8f6654 AC |
569 | else if (word > text) |
570 | { | |
1777feb0 | 571 | /* Return some portion of interp->name. */ |
49c4e619 | 572 | strcpy (match, interp->name + (word - text)); |
4a8f6654 AC |
573 | } |
574 | else | |
575 | { | |
1777feb0 | 576 | /* Return some of text plus interp->name. */ |
49c4e619 TT |
577 | strncpy (match, word, text - word); |
578 | match[text - word] = '\0'; | |
579 | strcat (match, interp->name); | |
4a8f6654 | 580 | } |
49c4e619 | 581 | VEC_safe_push (char_ptr, matches, match); |
4a8f6654 AC |
582 | } |
583 | } | |
584 | ||
4a8f6654 AC |
585 | return matches; |
586 | } | |
587 | ||
eb18d289 NR |
588 | struct interp * |
589 | top_level_interpreter (void) | |
590 | { | |
cb814510 PA |
591 | struct ui_interp_info *ui_interp = get_current_interp_info (); |
592 | ||
593 | return ui_interp->top_level_interpreter; | |
eb18d289 NR |
594 | } |
595 | ||
596 | void * | |
683f2885 VP |
597 | top_level_interpreter_data (void) |
598 | { | |
cb814510 PA |
599 | struct interp *interp; |
600 | ||
601 | interp = top_level_interpreter (); | |
602 | gdb_assert (interp != NULL); | |
603 | return interp->data; | |
683f2885 VP |
604 | } |
605 | ||
4a8f6654 AC |
606 | /* This just adds the "interpreter-exec" command. */ |
607 | void | |
608 | _initialize_interpreter (void) | |
609 | { | |
610 | struct cmd_list_element *c; | |
611 | ||
612 | c = add_cmd ("interpreter-exec", class_support, | |
1a966eab AC |
613 | interpreter_exec_cmd, _("\ |
614 | Execute a command in an interpreter. It takes two arguments:\n\ | |
4a8f6654 | 615 | The first argument is the name of the interpreter to use.\n\ |
1a966eab | 616 | The second argument is the command to execute.\n"), &cmdlist); |
4a8f6654 AC |
617 | set_cmd_completer (c, interpreter_completer); |
618 | } |