cli-script.c revision 1.3 1 1.1 christos /* GDB CLI command scripting.
2 1.1 christos
3 1.3 christos Copyright (C) 1986-2015 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "value.h"
22 1.1 christos #include "language.h" /* For value_true */
23 1.1 christos #include <ctype.h>
24 1.1 christos
25 1.1 christos #include "ui-out.h"
26 1.1 christos #include "top.h"
27 1.1 christos #include "breakpoint.h"
28 1.1 christos #include "cli/cli-cmds.h"
29 1.1 christos #include "cli/cli-decode.h"
30 1.1 christos #include "cli/cli-script.h"
31 1.1 christos
32 1.3 christos #include "extension.h"
33 1.1 christos #include "interps.h"
34 1.3 christos #include "compile/compile.h"
35 1.1 christos
36 1.1 christos /* Prototypes for local functions. */
37 1.1 christos
38 1.1 christos static enum command_control_type
39 1.1 christos recurse_read_control_structure (char * (*read_next_line_func) (void),
40 1.1 christos struct command_line *current_cmd,
41 1.1 christos void (*validator)(char *, void *),
42 1.1 christos void *closure);
43 1.1 christos
44 1.1 christos static char *insert_args (char *line);
45 1.1 christos
46 1.1 christos static struct cleanup * setup_user_args (char *p);
47 1.1 christos
48 1.1 christos static char *read_next_line (void);
49 1.1 christos
50 1.1 christos /* Level of control structure when reading. */
51 1.1 christos static int control_level;
52 1.1 christos
53 1.1 christos /* Level of control structure when executing. */
54 1.1 christos static int command_nest_depth = 1;
55 1.1 christos
56 1.1 christos /* This is to prevent certain commands being printed twice. */
57 1.1 christos static int suppress_next_print_command_trace = 0;
58 1.1 christos
59 1.1 christos /* Structure for arguments to user defined functions. */
60 1.1 christos #define MAXUSERARGS 10
61 1.1 christos struct user_args
62 1.1 christos {
63 1.1 christos struct user_args *next;
64 1.1 christos /* It is necessary to store a malloced copy of the command line to
65 1.1 christos ensure that the arguments are not overwritten before they are
66 1.1 christos used. */
67 1.1 christos char *command;
68 1.1 christos struct
69 1.1 christos {
70 1.1 christos char *arg;
71 1.1 christos int len;
72 1.1 christos }
73 1.1 christos a[MAXUSERARGS];
74 1.1 christos int count;
75 1.1 christos }
76 1.1 christos *user_args;
77 1.1 christos
78 1.1 christos
79 1.1 christos /* Return non-zero if TYPE is a multi-line command (i.e., is terminated
81 1.1 christos by "end"). */
82 1.1 christos
83 1.1 christos static int
84 1.1 christos multi_line_command_p (enum command_control_type type)
85 1.1 christos {
86 1.1 christos switch (type)
87 1.1 christos {
88 1.1 christos case if_control:
89 1.1 christos case while_control:
90 1.1 christos case while_stepping_control:
91 1.3 christos case commands_control:
92 1.1 christos case compile_control:
93 1.3 christos case python_control:
94 1.1 christos case guile_control:
95 1.1 christos return 1;
96 1.1 christos default:
97 1.1 christos return 0;
98 1.1 christos }
99 1.1 christos }
100 1.1 christos
101 1.1 christos /* Allocate, initialize a new command line structure for one of the
102 1.1 christos control commands (if/while). */
103 1.1 christos
104 1.1 christos static struct command_line *
105 1.1 christos build_command_line (enum command_control_type type, char *args)
106 1.1 christos {
107 1.1 christos struct command_line *cmd;
108 1.1 christos
109 1.1 christos if (args == NULL && (type == if_control || type == while_control))
110 1.1 christos error (_("if/while commands require arguments."));
111 1.1 christos gdb_assert (args != NULL);
112 1.1 christos
113 1.1 christos cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
114 1.1 christos cmd->next = NULL;
115 1.1 christos cmd->control_type = type;
116 1.1 christos
117 1.1 christos cmd->body_count = 1;
118 1.1 christos cmd->body_list
119 1.1 christos = (struct command_line **) xmalloc (sizeof (struct command_line *)
120 1.1 christos * cmd->body_count);
121 1.1 christos memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
122 1.1 christos cmd->line = xstrdup (args);
123 1.1 christos
124 1.1 christos return cmd;
125 1.1 christos }
126 1.1 christos
127 1.1 christos /* Build and return a new command structure for the control commands
128 1.1 christos such as "if" and "while". */
129 1.1 christos
130 1.1 christos struct command_line *
131 1.1 christos get_command_line (enum command_control_type type, char *arg)
132 1.1 christos {
133 1.1 christos struct command_line *cmd;
134 1.1 christos struct cleanup *old_chain = NULL;
135 1.1 christos
136 1.1 christos /* Allocate and build a new command line structure. */
137 1.1 christos cmd = build_command_line (type, arg);
138 1.1 christos
139 1.1 christos old_chain = make_cleanup_free_command_lines (&cmd);
140 1.1 christos
141 1.1 christos /* Read in the body of this command. */
142 1.1 christos if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
143 1.1 christos == invalid_control)
144 1.1 christos {
145 1.1 christos warning (_("Error reading in canned sequence of commands."));
146 1.1 christos do_cleanups (old_chain);
147 1.1 christos return NULL;
148 1.1 christos }
149 1.1 christos
150 1.1 christos discard_cleanups (old_chain);
151 1.1 christos return cmd;
152 1.1 christos }
153 1.1 christos
154 1.1 christos /* Recursively print a command (including full control structures). */
155 1.1 christos
156 1.1 christos void
157 1.1 christos print_command_lines (struct ui_out *uiout, struct command_line *cmd,
158 1.1 christos unsigned int depth)
159 1.1 christos {
160 1.1 christos struct command_line *list;
161 1.1 christos
162 1.1 christos list = cmd;
163 1.1 christos while (list)
164 1.1 christos {
165 1.1 christos if (depth)
166 1.1 christos ui_out_spaces (uiout, 2 * depth);
167 1.1 christos
168 1.1 christos /* A simple command, print it and continue. */
169 1.1 christos if (list->control_type == simple_control)
170 1.1 christos {
171 1.1 christos ui_out_field_string (uiout, NULL, list->line);
172 1.1 christos ui_out_text (uiout, "\n");
173 1.1 christos list = list->next;
174 1.1 christos continue;
175 1.1 christos }
176 1.1 christos
177 1.1 christos /* loop_continue to jump to the start of a while loop, print it
178 1.1 christos and continue. */
179 1.1 christos if (list->control_type == continue_control)
180 1.1 christos {
181 1.1 christos ui_out_field_string (uiout, NULL, "loop_continue");
182 1.1 christos ui_out_text (uiout, "\n");
183 1.1 christos list = list->next;
184 1.1 christos continue;
185 1.1 christos }
186 1.1 christos
187 1.1 christos /* loop_break to break out of a while loop, print it and
188 1.1 christos continue. */
189 1.1 christos if (list->control_type == break_control)
190 1.1 christos {
191 1.1 christos ui_out_field_string (uiout, NULL, "loop_break");
192 1.1 christos ui_out_text (uiout, "\n");
193 1.1 christos list = list->next;
194 1.1 christos continue;
195 1.1 christos }
196 1.1 christos
197 1.1 christos /* A while command. Recursively print its subcommands and
198 1.1 christos continue. */
199 1.1 christos if (list->control_type == while_control
200 1.1 christos || list->control_type == while_stepping_control)
201 1.1 christos {
202 1.1 christos /* For while-stepping, the line includes the 'while-stepping'
203 1.1 christos token. See comment in process_next_line for explanation.
204 1.1 christos Here, take care not print 'while-stepping' twice. */
205 1.1 christos if (list->control_type == while_control)
206 1.1 christos ui_out_field_fmt (uiout, NULL, "while %s", list->line);
207 1.1 christos else
208 1.1 christos ui_out_field_string (uiout, NULL, list->line);
209 1.1 christos ui_out_text (uiout, "\n");
210 1.1 christos print_command_lines (uiout, *list->body_list, depth + 1);
211 1.1 christos if (depth)
212 1.1 christos ui_out_spaces (uiout, 2 * depth);
213 1.1 christos ui_out_field_string (uiout, NULL, "end");
214 1.1 christos ui_out_text (uiout, "\n");
215 1.1 christos list = list->next;
216 1.1 christos continue;
217 1.1 christos }
218 1.1 christos
219 1.1 christos /* An if command. Recursively print both arms before
220 1.1 christos continueing. */
221 1.1 christos if (list->control_type == if_control)
222 1.1 christos {
223 1.1 christos ui_out_field_fmt (uiout, NULL, "if %s", list->line);
224 1.1 christos ui_out_text (uiout, "\n");
225 1.1 christos /* The true arm. */
226 1.1 christos print_command_lines (uiout, list->body_list[0], depth + 1);
227 1.1 christos
228 1.1 christos /* Show the false arm if it exists. */
229 1.1 christos if (list->body_count == 2)
230 1.1 christos {
231 1.1 christos if (depth)
232 1.1 christos ui_out_spaces (uiout, 2 * depth);
233 1.1 christos ui_out_field_string (uiout, NULL, "else");
234 1.1 christos ui_out_text (uiout, "\n");
235 1.1 christos print_command_lines (uiout, list->body_list[1], depth + 1);
236 1.1 christos }
237 1.1 christos
238 1.1 christos if (depth)
239 1.1 christos ui_out_spaces (uiout, 2 * depth);
240 1.1 christos ui_out_field_string (uiout, NULL, "end");
241 1.1 christos ui_out_text (uiout, "\n");
242 1.1 christos list = list->next;
243 1.1 christos continue;
244 1.1 christos }
245 1.1 christos
246 1.1 christos /* A commands command. Print the breakpoint commands and
247 1.1 christos continue. */
248 1.1 christos if (list->control_type == commands_control)
249 1.1 christos {
250 1.1 christos if (*(list->line))
251 1.1 christos ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
252 1.1 christos else
253 1.1 christos ui_out_field_string (uiout, NULL, "commands");
254 1.1 christos ui_out_text (uiout, "\n");
255 1.1 christos print_command_lines (uiout, *list->body_list, depth + 1);
256 1.1 christos if (depth)
257 1.1 christos ui_out_spaces (uiout, 2 * depth);
258 1.1 christos ui_out_field_string (uiout, NULL, "end");
259 1.1 christos ui_out_text (uiout, "\n");
260 1.1 christos list = list->next;
261 1.1 christos continue;
262 1.1 christos }
263 1.1 christos
264 1.1 christos if (list->control_type == python_control)
265 1.1 christos {
266 1.1 christos ui_out_field_string (uiout, NULL, "python");
267 1.1 christos ui_out_text (uiout, "\n");
268 1.1 christos /* Don't indent python code at all. */
269 1.1 christos print_command_lines (uiout, *list->body_list, 0);
270 1.1 christos if (depth)
271 1.1 christos ui_out_spaces (uiout, 2 * depth);
272 1.1 christos ui_out_field_string (uiout, NULL, "end");
273 1.1 christos ui_out_text (uiout, "\n");
274 1.1 christos list = list->next;
275 1.1 christos continue;
276 1.1 christos }
277 1.3 christos
278 1.3 christos if (list->control_type == compile_control)
279 1.3 christos {
280 1.3 christos ui_out_field_string (uiout, NULL, "compile expression");
281 1.3 christos ui_out_text (uiout, "\n");
282 1.3 christos print_command_lines (uiout, *list->body_list, 0);
283 1.3 christos if (depth)
284 1.3 christos ui_out_spaces (uiout, 2 * depth);
285 1.3 christos ui_out_field_string (uiout, NULL, "end");
286 1.3 christos ui_out_text (uiout, "\n");
287 1.3 christos list = list->next;
288 1.3 christos continue;
289 1.3 christos }
290 1.3 christos
291 1.3 christos if (list->control_type == guile_control)
292 1.3 christos {
293 1.3 christos ui_out_field_string (uiout, NULL, "guile");
294 1.3 christos ui_out_text (uiout, "\n");
295 1.3 christos print_command_lines (uiout, *list->body_list, depth + 1);
296 1.3 christos if (depth)
297 1.3 christos ui_out_spaces (uiout, 2 * depth);
298 1.3 christos ui_out_field_string (uiout, NULL, "end");
299 1.3 christos ui_out_text (uiout, "\n");
300 1.3 christos list = list->next;
301 1.3 christos continue;
302 1.3 christos }
303 1.1 christos
304 1.1 christos /* Ignore illegal command type and try next. */
305 1.1 christos list = list->next;
306 1.1 christos } /* while (list) */
307 1.1 christos }
308 1.1 christos
309 1.1 christos /* Handle pre-post hooks. */
310 1.1 christos
311 1.1 christos static void
312 1.1 christos clear_hook_in_cleanup (void *data)
313 1.1 christos {
314 1.1 christos struct cmd_list_element *c = data;
315 1.1 christos
316 1.1 christos c->hook_in = 0; /* Allow hook to work again once it is complete. */
317 1.1 christos }
318 1.1 christos
319 1.1 christos void
320 1.1 christos execute_cmd_pre_hook (struct cmd_list_element *c)
321 1.1 christos {
322 1.1 christos if ((c->hook_pre) && (!c->hook_in))
323 1.1 christos {
324 1.1 christos struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
325 1.1 christos c->hook_in = 1; /* Prevent recursive hooking. */
326 1.1 christos execute_user_command (c->hook_pre, (char *) 0);
327 1.1 christos do_cleanups (cleanups);
328 1.1 christos }
329 1.1 christos }
330 1.1 christos
331 1.1 christos void
332 1.1 christos execute_cmd_post_hook (struct cmd_list_element *c)
333 1.1 christos {
334 1.1 christos if ((c->hook_post) && (!c->hook_in))
335 1.1 christos {
336 1.1 christos struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
337 1.1 christos
338 1.1 christos c->hook_in = 1; /* Prevent recursive hooking. */
339 1.1 christos execute_user_command (c->hook_post, (char *) 0);
340 1.1 christos do_cleanups (cleanups);
341 1.1 christos }
342 1.1 christos }
343 1.1 christos
344 1.1 christos /* Execute the command in CMD. */
345 1.1 christos static void
346 1.1 christos do_restore_user_call_depth (void * call_depth)
347 1.1 christos {
348 1.1 christos int *depth = call_depth;
349 1.1 christos
350 1.1 christos (*depth)--;
351 1.1 christos if ((*depth) == 0)
352 1.1 christos in_user_command = 0;
353 1.1 christos }
354 1.1 christos
355 1.1 christos
356 1.1 christos void
357 1.1 christos execute_user_command (struct cmd_list_element *c, char *args)
358 1.1 christos {
359 1.1 christos struct command_line *cmdlines;
360 1.1 christos struct cleanup *old_chain;
361 1.1 christos enum command_control_type ret;
362 1.1 christos static int user_call_depth = 0;
363 1.1 christos extern unsigned int max_user_call_depth;
364 1.1 christos
365 1.1 christos cmdlines = c->user_commands;
366 1.1 christos if (cmdlines == 0)
367 1.1 christos /* Null command */
368 1.1 christos return;
369 1.1 christos
370 1.1 christos old_chain = setup_user_args (args);
371 1.1 christos
372 1.1 christos if (++user_call_depth > max_user_call_depth)
373 1.1 christos error (_("Max user call depth exceeded -- command aborted."));
374 1.1 christos
375 1.1 christos make_cleanup (do_restore_user_call_depth, &user_call_depth);
376 1.1 christos
377 1.1 christos /* Set the instream to 0, indicating execution of a
378 1.1 christos user-defined function. */
379 1.1 christos make_cleanup (do_restore_instream_cleanup, instream);
380 1.1 christos instream = (FILE *) 0;
381 1.1 christos
382 1.1 christos /* Also set the global in_user_command, so that NULL instream is
383 1.1 christos not confused with Insight. */
384 1.1 christos in_user_command = 1;
385 1.1 christos
386 1.1 christos make_cleanup_restore_integer (&interpreter_async);
387 1.1 christos interpreter_async = 0;
388 1.1 christos
389 1.1 christos command_nest_depth++;
390 1.1 christos while (cmdlines)
391 1.1 christos {
392 1.1 christos ret = execute_control_command (cmdlines);
393 1.1 christos if (ret != simple_control && ret != break_control)
394 1.1 christos {
395 1.1 christos warning (_("Error executing canned sequence of commands."));
396 1.1 christos break;
397 1.1 christos }
398 1.1 christos cmdlines = cmdlines->next;
399 1.1 christos }
400 1.1 christos command_nest_depth--;
401 1.1 christos do_cleanups (old_chain);
402 1.1 christos }
403 1.1 christos
404 1.1 christos /* This function is called every time GDB prints a prompt. It ensures
405 1.1 christos that errors and the like do not confuse the command tracing. */
406 1.1 christos
407 1.1 christos void
408 1.1 christos reset_command_nest_depth (void)
409 1.1 christos {
410 1.1 christos command_nest_depth = 1;
411 1.1 christos
412 1.1 christos /* Just in case. */
413 1.1 christos suppress_next_print_command_trace = 0;
414 1.1 christos }
415 1.1 christos
416 1.1 christos /* Print the command, prefixed with '+' to represent the call depth.
417 1.1 christos This is slightly complicated because this function may be called
418 1.1 christos from execute_command and execute_control_command. Unfortunately
419 1.1 christos execute_command also prints the top level control commands.
420 1.1 christos In these cases execute_command will call execute_control_command
421 1.1 christos via while_command or if_command. Inner levels of 'if' and 'while'
422 1.1 christos are dealt with directly. Therefore we can use these functions
423 1.1 christos to determine whether the command has been printed already or not. */
424 1.1 christos void
425 1.1 christos print_command_trace (const char *cmd)
426 1.1 christos {
427 1.1 christos int i;
428 1.1 christos
429 1.1 christos if (suppress_next_print_command_trace)
430 1.1 christos {
431 1.1 christos suppress_next_print_command_trace = 0;
432 1.1 christos return;
433 1.1 christos }
434 1.1 christos
435 1.1 christos if (!source_verbose && !trace_commands)
436 1.1 christos return;
437 1.1 christos
438 1.1 christos for (i=0; i < command_nest_depth; i++)
439 1.1 christos printf_filtered ("+");
440 1.1 christos
441 1.1 christos printf_filtered ("%s\n", cmd);
442 1.1 christos }
443 1.1 christos
444 1.1 christos enum command_control_type
445 1.1 christos execute_control_command (struct command_line *cmd)
446 1.1 christos {
447 1.1 christos struct expression *expr;
448 1.1 christos struct command_line *current;
449 1.1 christos struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
450 1.1 christos struct value *val;
451 1.1 christos struct value *val_mark;
452 1.1 christos int loop;
453 1.1 christos enum command_control_type ret;
454 1.1 christos char *new_line;
455 1.1 christos
456 1.1 christos /* Start by assuming failure, if a problem is detected, the code
457 1.1 christos below will simply "break" out of the switch. */
458 1.1 christos ret = invalid_control;
459 1.1 christos
460 1.1 christos switch (cmd->control_type)
461 1.1 christos {
462 1.1 christos case simple_control:
463 1.1 christos /* A simple command, execute it and return. */
464 1.1 christos new_line = insert_args (cmd->line);
465 1.1 christos if (!new_line)
466 1.1 christos break;
467 1.1 christos make_cleanup (free_current_contents, &new_line);
468 1.1 christos execute_command (new_line, 0);
469 1.1 christos ret = cmd->control_type;
470 1.1 christos break;
471 1.1 christos
472 1.1 christos case continue_control:
473 1.1 christos print_command_trace ("loop_continue");
474 1.1 christos
475 1.1 christos /* Return for "continue", and "break" so we can either
476 1.1 christos continue the loop at the top, or break out. */
477 1.1 christos ret = cmd->control_type;
478 1.1 christos break;
479 1.1 christos
480 1.1 christos case break_control:
481 1.1 christos print_command_trace ("loop_break");
482 1.1 christos
483 1.1 christos /* Return for "continue", and "break" so we can either
484 1.1 christos continue the loop at the top, or break out. */
485 1.1 christos ret = cmd->control_type;
486 1.1 christos break;
487 1.1 christos
488 1.1 christos case while_control:
489 1.1 christos {
490 1.1 christos int len = strlen (cmd->line) + 7;
491 1.1 christos char *buffer = alloca (len);
492 1.1 christos
493 1.1 christos xsnprintf (buffer, len, "while %s", cmd->line);
494 1.1 christos print_command_trace (buffer);
495 1.1 christos
496 1.1 christos /* Parse the loop control expression for the while statement. */
497 1.1 christos new_line = insert_args (cmd->line);
498 1.1 christos if (!new_line)
499 1.1 christos break;
500 1.1 christos make_cleanup (free_current_contents, &new_line);
501 1.1 christos expr = parse_expression (new_line);
502 1.1 christos make_cleanup (free_current_contents, &expr);
503 1.1 christos
504 1.1 christos ret = simple_control;
505 1.1 christos loop = 1;
506 1.1 christos
507 1.1 christos /* Keep iterating so long as the expression is true. */
508 1.1 christos while (loop == 1)
509 1.1 christos {
510 1.1 christos int cond_result;
511 1.1 christos
512 1.1 christos QUIT;
513 1.1 christos
514 1.1 christos /* Evaluate the expression. */
515 1.1 christos val_mark = value_mark ();
516 1.1 christos val = evaluate_expression (expr);
517 1.1 christos cond_result = value_true (val);
518 1.1 christos value_free_to_mark (val_mark);
519 1.1 christos
520 1.1 christos /* If the value is false, then break out of the loop. */
521 1.1 christos if (!cond_result)
522 1.1 christos break;
523 1.1 christos
524 1.1 christos /* Execute the body of the while statement. */
525 1.1 christos current = *cmd->body_list;
526 1.1 christos while (current)
527 1.1 christos {
528 1.1 christos command_nest_depth++;
529 1.1 christos ret = execute_control_command (current);
530 1.1 christos command_nest_depth--;
531 1.1 christos
532 1.1 christos /* If we got an error, or a "break" command, then stop
533 1.1 christos looping. */
534 1.1 christos if (ret == invalid_control || ret == break_control)
535 1.1 christos {
536 1.1 christos loop = 0;
537 1.1 christos break;
538 1.1 christos }
539 1.1 christos
540 1.1 christos /* If we got a "continue" command, then restart the loop
541 1.1 christos at this point. */
542 1.1 christos if (ret == continue_control)
543 1.1 christos break;
544 1.1 christos
545 1.1 christos /* Get the next statement. */
546 1.1 christos current = current->next;
547 1.1 christos }
548 1.1 christos }
549 1.1 christos
550 1.1 christos /* Reset RET so that we don't recurse the break all the way down. */
551 1.1 christos if (ret == break_control)
552 1.1 christos ret = simple_control;
553 1.1 christos
554 1.1 christos break;
555 1.1 christos }
556 1.1 christos
557 1.1 christos case if_control:
558 1.1 christos {
559 1.1 christos int len = strlen (cmd->line) + 4;
560 1.1 christos char *buffer = alloca (len);
561 1.1 christos
562 1.1 christos xsnprintf (buffer, len, "if %s", cmd->line);
563 1.1 christos print_command_trace (buffer);
564 1.1 christos
565 1.1 christos new_line = insert_args (cmd->line);
566 1.1 christos if (!new_line)
567 1.1 christos break;
568 1.1 christos make_cleanup (free_current_contents, &new_line);
569 1.1 christos /* Parse the conditional for the if statement. */
570 1.1 christos expr = parse_expression (new_line);
571 1.1 christos make_cleanup (free_current_contents, &expr);
572 1.1 christos
573 1.1 christos current = NULL;
574 1.1 christos ret = simple_control;
575 1.1 christos
576 1.1 christos /* Evaluate the conditional. */
577 1.1 christos val_mark = value_mark ();
578 1.1 christos val = evaluate_expression (expr);
579 1.1 christos
580 1.1 christos /* Choose which arm to take commands from based on the value
581 1.1 christos of the conditional expression. */
582 1.1 christos if (value_true (val))
583 1.1 christos current = *cmd->body_list;
584 1.1 christos else if (cmd->body_count == 2)
585 1.1 christos current = *(cmd->body_list + 1);
586 1.1 christos value_free_to_mark (val_mark);
587 1.1 christos
588 1.1 christos /* Execute commands in the given arm. */
589 1.1 christos while (current)
590 1.1 christos {
591 1.1 christos command_nest_depth++;
592 1.1 christos ret = execute_control_command (current);
593 1.1 christos command_nest_depth--;
594 1.1 christos
595 1.1 christos /* If we got an error, get out. */
596 1.1 christos if (ret != simple_control)
597 1.1 christos break;
598 1.1 christos
599 1.1 christos /* Get the next statement in the body. */
600 1.1 christos current = current->next;
601 1.1 christos }
602 1.1 christos
603 1.1 christos break;
604 1.1 christos }
605 1.1 christos
606 1.1 christos case commands_control:
607 1.1 christos {
608 1.1 christos /* Breakpoint commands list, record the commands in the
609 1.1 christos breakpoint's command list and return. */
610 1.1 christos new_line = insert_args (cmd->line);
611 1.1 christos if (!new_line)
612 1.1 christos break;
613 1.1 christos make_cleanup (free_current_contents, &new_line);
614 1.1 christos ret = commands_from_control_command (new_line, cmd);
615 1.1 christos break;
616 1.1 christos }
617 1.3 christos
618 1.3 christos case compile_control:
619 1.3 christos eval_compile_command (cmd, NULL, cmd->control_u.compile.scope);
620 1.3 christos ret = simple_control;
621 1.3 christos break;
622 1.1 christos
623 1.3 christos case python_control:
624 1.1 christos case guile_control:
625 1.3 christos {
626 1.1 christos eval_ext_lang_from_control_command (cmd);
627 1.1 christos ret = simple_control;
628 1.1 christos break;
629 1.1 christos }
630 1.1 christos
631 1.1 christos default:
632 1.1 christos warning (_("Invalid control type in canned commands structure."));
633 1.1 christos break;
634 1.1 christos }
635 1.1 christos
636 1.1 christos do_cleanups (old_chain);
637 1.1 christos
638 1.1 christos return ret;
639 1.1 christos }
640 1.1 christos
641 1.1 christos /* Like execute_control_command, but first set
642 1.1 christos suppress_next_print_command_trace. */
643 1.1 christos
644 1.1 christos enum command_control_type
645 1.1 christos execute_control_command_untraced (struct command_line *cmd)
646 1.1 christos {
647 1.1 christos suppress_next_print_command_trace = 1;
648 1.1 christos return execute_control_command (cmd);
649 1.1 christos }
650 1.1 christos
651 1.1 christos
652 1.1 christos /* "while" command support. Executes a body of statements while the
653 1.1 christos loop condition is nonzero. */
654 1.1 christos
655 1.1 christos static void
656 1.1 christos while_command (char *arg, int from_tty)
657 1.1 christos {
658 1.1 christos struct command_line *command = NULL;
659 1.1 christos struct cleanup *old_chain;
660 1.1 christos
661 1.1 christos control_level = 1;
662 1.1 christos command = get_command_line (while_control, arg);
663 1.1 christos
664 1.1 christos if (command == NULL)
665 1.1 christos return;
666 1.1 christos
667 1.1 christos old_chain = make_cleanup_restore_integer (&interpreter_async);
668 1.1 christos interpreter_async = 0;
669 1.1 christos
670 1.1 christos execute_control_command_untraced (command);
671 1.1 christos free_command_lines (&command);
672 1.1 christos
673 1.1 christos do_cleanups (old_chain);
674 1.1 christos }
675 1.1 christos
676 1.1 christos /* "if" command support. Execute either the true or false arm depending
677 1.1 christos on the value of the if conditional. */
678 1.1 christos
679 1.1 christos static void
680 1.1 christos if_command (char *arg, int from_tty)
681 1.1 christos {
682 1.1 christos struct command_line *command = NULL;
683 1.1 christos struct cleanup *old_chain;
684 1.1 christos
685 1.1 christos control_level = 1;
686 1.1 christos command = get_command_line (if_control, arg);
687 1.1 christos
688 1.1 christos if (command == NULL)
689 1.1 christos return;
690 1.1 christos
691 1.1 christos old_chain = make_cleanup_restore_integer (&interpreter_async);
692 1.1 christos interpreter_async = 0;
693 1.1 christos
694 1.1 christos execute_control_command_untraced (command);
695 1.1 christos free_command_lines (&command);
696 1.1 christos
697 1.1 christos do_cleanups (old_chain);
698 1.1 christos }
699 1.1 christos
700 1.1 christos /* Cleanup */
701 1.1 christos static void
702 1.1 christos arg_cleanup (void *ignore)
703 1.1 christos {
704 1.1 christos struct user_args *oargs = user_args;
705 1.1 christos
706 1.1 christos if (!user_args)
707 1.1 christos internal_error (__FILE__, __LINE__,
708 1.1 christos _("arg_cleanup called with no user args.\n"));
709 1.1 christos
710 1.1 christos user_args = user_args->next;
711 1.1 christos xfree (oargs->command);
712 1.1 christos xfree (oargs);
713 1.1 christos }
714 1.1 christos
715 1.1 christos /* Bind the incomming arguments for a user defined command to
716 1.1 christos $arg0, $arg1 ... $argMAXUSERARGS. */
717 1.1 christos
718 1.1 christos static struct cleanup *
719 1.1 christos setup_user_args (char *p)
720 1.1 christos {
721 1.1 christos struct user_args *args;
722 1.1 christos struct cleanup *old_chain;
723 1.1 christos unsigned int arg_count = 0;
724 1.1 christos
725 1.1 christos args = (struct user_args *) xmalloc (sizeof (struct user_args));
726 1.1 christos memset (args, 0, sizeof (struct user_args));
727 1.1 christos
728 1.1 christos args->next = user_args;
729 1.1 christos user_args = args;
730 1.1 christos
731 1.1 christos old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
732 1.1 christos
733 1.1 christos if (p == NULL)
734 1.1 christos return old_chain;
735 1.1 christos
736 1.1 christos user_args->command = p = xstrdup (p);
737 1.1 christos
738 1.1 christos while (*p)
739 1.1 christos {
740 1.1 christos char *start_arg;
741 1.1 christos int squote = 0;
742 1.1 christos int dquote = 0;
743 1.1 christos int bsquote = 0;
744 1.1 christos
745 1.1 christos if (arg_count >= MAXUSERARGS)
746 1.1 christos error (_("user defined function may only have %d arguments."),
747 1.1 christos MAXUSERARGS);
748 1.1 christos
749 1.1 christos /* Strip whitespace. */
750 1.1 christos while (*p == ' ' || *p == '\t')
751 1.1 christos p++;
752 1.1 christos
753 1.1 christos /* P now points to an argument. */
754 1.1 christos start_arg = p;
755 1.1 christos user_args->a[arg_count].arg = p;
756 1.1 christos
757 1.1 christos /* Get to the end of this argument. */
758 1.1 christos while (*p)
759 1.1 christos {
760 1.1 christos if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
761 1.1 christos break;
762 1.1 christos else
763 1.1 christos {
764 1.1 christos if (bsquote)
765 1.1 christos bsquote = 0;
766 1.1 christos else if (*p == '\\')
767 1.1 christos bsquote = 1;
768 1.1 christos else if (squote)
769 1.1 christos {
770 1.1 christos if (*p == '\'')
771 1.1 christos squote = 0;
772 1.1 christos }
773 1.1 christos else if (dquote)
774 1.1 christos {
775 1.1 christos if (*p == '"')
776 1.1 christos dquote = 0;
777 1.1 christos }
778 1.1 christos else
779 1.1 christos {
780 1.1 christos if (*p == '\'')
781 1.1 christos squote = 1;
782 1.1 christos else if (*p == '"')
783 1.1 christos dquote = 1;
784 1.1 christos }
785 1.1 christos p++;
786 1.1 christos }
787 1.1 christos }
788 1.1 christos
789 1.1 christos user_args->a[arg_count].len = p - start_arg;
790 1.1 christos arg_count++;
791 1.1 christos user_args->count++;
792 1.1 christos }
793 1.1 christos return old_chain;
794 1.1 christos }
795 1.1 christos
796 1.1 christos /* Given character string P, return a point to the first argument
797 1.1 christos ($arg), or NULL if P contains no arguments. */
798 1.1 christos
799 1.1 christos static char *
800 1.1 christos locate_arg (char *p)
801 1.1 christos {
802 1.1 christos while ((p = strchr (p, '$')))
803 1.1 christos {
804 1.1 christos if (strncmp (p, "$arg", 4) == 0
805 1.1 christos && (isdigit (p[4]) || p[4] == 'c'))
806 1.1 christos return p;
807 1.1 christos p++;
808 1.1 christos }
809 1.1 christos return NULL;
810 1.1 christos }
811 1.1 christos
812 1.1 christos /* Insert the user defined arguments stored in user_arg into the $arg
813 1.1 christos arguments found in line, with the updated copy being placed into
814 1.1 christos nline. */
815 1.1 christos
816 1.1 christos static char *
817 1.1 christos insert_args (char *line)
818 1.1 christos {
819 1.1 christos char *p, *save_line, *new_line;
820 1.1 christos unsigned len, i;
821 1.1 christos
822 1.1 christos /* If we are not in a user-defined function, treat $argc, $arg0, et
823 1.1 christos cetera as normal convenience variables. */
824 1.1 christos if (user_args == NULL)
825 1.1 christos return xstrdup (line);
826 1.1 christos
827 1.1 christos /* First we need to know how much memory to allocate for the new
828 1.1 christos line. */
829 1.1 christos save_line = line;
830 1.1 christos len = 0;
831 1.1 christos while ((p = locate_arg (line)))
832 1.1 christos {
833 1.1 christos len += p - line;
834 1.1 christos i = p[4] - '0';
835 1.1 christos
836 1.1 christos if (p[4] == 'c')
837 1.1 christos {
838 1.1 christos /* $argc. Number will be <=10. */
839 1.1 christos len += user_args->count == 10 ? 2 : 1;
840 1.1 christos }
841 1.1 christos else if (i >= user_args->count)
842 1.1 christos {
843 1.1 christos error (_("Missing argument %d in user function."), i);
844 1.1 christos return NULL;
845 1.1 christos }
846 1.1 christos else
847 1.1 christos {
848 1.1 christos len += user_args->a[i].len;
849 1.1 christos }
850 1.1 christos line = p + 5;
851 1.1 christos }
852 1.1 christos
853 1.1 christos /* Don't forget the tail. */
854 1.1 christos len += strlen (line);
855 1.1 christos
856 1.1 christos /* Allocate space for the new line and fill it in. */
857 1.1 christos new_line = (char *) xmalloc (len + 1);
858 1.1 christos if (new_line == NULL)
859 1.1 christos return NULL;
860 1.1 christos
861 1.1 christos /* Restore pointer to beginning of old line. */
862 1.1 christos line = save_line;
863 1.1 christos
864 1.1 christos /* Save pointer to beginning of new line. */
865 1.1 christos save_line = new_line;
866 1.1 christos
867 1.1 christos while ((p = locate_arg (line)))
868 1.1 christos {
869 1.1 christos int i, len;
870 1.1 christos
871 1.1 christos memcpy (new_line, line, p - line);
872 1.1 christos new_line += p - line;
873 1.1 christos
874 1.1 christos if (p[4] == 'c')
875 1.1 christos {
876 1.1 christos gdb_assert (user_args->count >= 0 && user_args->count <= 10);
877 1.1 christos if (user_args->count == 10)
878 1.1 christos {
879 1.1 christos *(new_line++) = '1';
880 1.1 christos *(new_line++) = '0';
881 1.1 christos }
882 1.1 christos else
883 1.1 christos *(new_line++) = user_args->count + '0';
884 1.1 christos }
885 1.1 christos else
886 1.1 christos {
887 1.1 christos i = p[4] - '0';
888 1.1 christos len = user_args->a[i].len;
889 1.1 christos if (len)
890 1.1 christos {
891 1.1 christos memcpy (new_line, user_args->a[i].arg, len);
892 1.1 christos new_line += len;
893 1.1 christos }
894 1.1 christos }
895 1.1 christos line = p + 5;
896 1.1 christos }
897 1.1 christos /* Don't forget the tail. */
898 1.1 christos strcpy (new_line, line);
899 1.1 christos
900 1.1 christos /* Return a pointer to the beginning of the new line. */
901 1.1 christos return save_line;
902 1.1 christos }
903 1.1 christos
904 1.1 christos
905 1.1 christos /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
907 1.1 christos code bodies. This is typically used when we encounter an "else"
908 1.1 christos clause for an "if" command. */
909 1.1 christos
910 1.1 christos static void
911 1.1 christos realloc_body_list (struct command_line *command, int new_length)
912 1.1 christos {
913 1.1 christos int n;
914 1.1 christos struct command_line **body_list;
915 1.1 christos
916 1.1 christos n = command->body_count;
917 1.1 christos
918 1.1 christos /* Nothing to do? */
919 1.1 christos if (new_length <= n)
920 1.1 christos return;
921 1.1 christos
922 1.1 christos body_list = (struct command_line **)
923 1.1 christos xmalloc (sizeof (struct command_line *) * new_length);
924 1.1 christos
925 1.1 christos memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
926 1.1 christos memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
927 1.1 christos
928 1.1 christos xfree (command->body_list);
929 1.1 christos command->body_list = body_list;
930 1.1 christos command->body_count = new_length;
931 1.1 christos }
932 1.1 christos
933 1.1 christos /* Read next line from stdout. Passed to read_command_line_1 and
934 1.1 christos recurse_read_control_structure whenever we need to read commands
935 1.1 christos from stdout. */
936 1.1 christos
937 1.1 christos static char *
938 1.1 christos read_next_line (void)
939 1.1 christos {
940 1.1 christos char *prompt_ptr, control_prompt[256];
941 1.1 christos int i = 0;
942 1.1 christos
943 1.1 christos if (control_level >= 254)
944 1.1 christos error (_("Control nesting too deep!"));
945 1.1 christos
946 1.1 christos /* Set a prompt based on the nesting of the control commands. */
947 1.1 christos if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
948 1.1 christos {
949 1.1 christos for (i = 0; i < control_level; i++)
950 1.1 christos control_prompt[i] = ' ';
951 1.1 christos control_prompt[i] = '>';
952 1.1 christos control_prompt[i + 1] = '\0';
953 1.1 christos prompt_ptr = (char *) &control_prompt[0];
954 1.1 christos }
955 1.1 christos else
956 1.1 christos prompt_ptr = NULL;
957 1.1 christos
958 1.1 christos return command_line_input (prompt_ptr, instream == stdin, "commands");
959 1.1 christos }
960 1.1 christos
961 1.1 christos /* Process one input line. If the command is an "end", return such an
962 1.1 christos indication to the caller. If PARSE_COMMANDS is true, strip leading
963 1.1 christos whitespace (trailing whitespace is always stripped) in the line,
964 1.1 christos attempt to recognize GDB control commands, and also return an
965 1.1 christos indication if the command is an "else" or a nop.
966 1.1 christos
967 1.1 christos Otherwise, only "end" is recognized. */
968 1.1 christos
969 1.1 christos static enum misc_command_type
970 1.1 christos process_next_line (char *p, struct command_line **command, int parse_commands,
971 1.1 christos void (*validator)(char *, void *), void *closure)
972 1.1 christos {
973 1.1 christos char *p_end;
974 1.1 christos char *p_start;
975 1.1 christos int not_handled = 0;
976 1.1 christos
977 1.1 christos /* Not sure what to do here. */
978 1.1 christos if (p == NULL)
979 1.1 christos return end_command;
980 1.1 christos
981 1.1 christos /* Strip trailing whitespace. */
982 1.1 christos p_end = p + strlen (p);
983 1.1 christos while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
984 1.1 christos p_end--;
985 1.1 christos
986 1.1 christos p_start = p;
987 1.1 christos /* Strip leading whitespace. */
988 1.1 christos while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
989 1.1 christos p_start++;
990 1.1 christos
991 1.1 christos /* 'end' is always recognized, regardless of parse_commands value.
992 1.1 christos We also permit whitespace before end and after. */
993 1.1 christos if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
994 1.1 christos return end_command;
995 1.1 christos
996 1.1 christos if (parse_commands)
997 1.1 christos {
998 1.1 christos /* If commands are parsed, we skip initial spaces. Otherwise,
999 1.1 christos which is the case for Python commands and documentation
1000 1.1 christos (see the 'document' command), spaces are preserved. */
1001 1.1 christos p = p_start;
1002 1.1 christos
1003 1.1 christos /* Blanks and comments don't really do anything, but we need to
1004 1.1 christos distinguish them from else, end and other commands which can
1005 1.1 christos be executed. */
1006 1.1 christos if (p_end == p || p[0] == '#')
1007 1.1 christos return nop_command;
1008 1.1 christos
1009 1.1 christos /* Is the else clause of an if control structure? */
1010 1.1 christos if (p_end - p == 4 && !strncmp (p, "else", 4))
1011 1.1 christos return else_command;
1012 1.1 christos
1013 1.1 christos /* Check for while, if, break, continue, etc and build a new
1014 1.1 christos command line structure for them. */
1015 1.1 christos if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
1016 1.1 christos || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
1017 1.1 christos || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
1018 1.1 christos {
1019 1.1 christos /* Because validate_actionline and encode_action lookup
1020 1.1 christos command's line as command, we need the line to
1021 1.1 christos include 'while-stepping'.
1022 1.1 christos
1023 1.1 christos For 'ws' alias, the command will have 'ws', not expanded
1024 1.1 christos to 'while-stepping'. This is intentional -- we don't
1025 1.1 christos really want frontend to send a command list with 'ws',
1026 1.1 christos and next break-info returning command line with
1027 1.1 christos 'while-stepping'. This should work, but might cause the
1028 1.1 christos breakpoint to be marked as changed while it's actually
1029 1.1 christos not. */
1030 1.1 christos *command = build_command_line (while_stepping_control, p);
1031 1.1 christos }
1032 1.1 christos else if (p_end - p > 5 && !strncmp (p, "while", 5))
1033 1.1 christos {
1034 1.1 christos char *first_arg;
1035 1.1 christos
1036 1.1 christos first_arg = p + 5;
1037 1.1 christos while (first_arg < p_end && isspace (*first_arg))
1038 1.1 christos first_arg++;
1039 1.1 christos *command = build_command_line (while_control, first_arg);
1040 1.1 christos }
1041 1.1 christos else if (p_end - p > 2 && !strncmp (p, "if", 2))
1042 1.1 christos {
1043 1.1 christos char *first_arg;
1044 1.1 christos
1045 1.1 christos first_arg = p + 2;
1046 1.1 christos while (first_arg < p_end && isspace (*first_arg))
1047 1.1 christos first_arg++;
1048 1.1 christos *command = build_command_line (if_control, first_arg);
1049 1.1 christos }
1050 1.1 christos else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
1051 1.1 christos {
1052 1.1 christos char *first_arg;
1053 1.1 christos
1054 1.1 christos first_arg = p + 8;
1055 1.1 christos while (first_arg < p_end && isspace (*first_arg))
1056 1.1 christos first_arg++;
1057 1.1 christos *command = build_command_line (commands_control, first_arg);
1058 1.1 christos }
1059 1.1 christos else if (p_end - p == 6 && !strncmp (p, "python", 6))
1060 1.1 christos {
1061 1.1 christos /* Note that we ignore the inline "python command" form
1062 1.1 christos here. */
1063 1.3 christos *command = build_command_line (python_control, "");
1064 1.3 christos }
1065 1.3 christos else if (p_end - p == 6 && !strncmp (p, "compile", 7))
1066 1.3 christos {
1067 1.3 christos /* Note that we ignore the inline "compile command" form
1068 1.3 christos here. */
1069 1.3 christos *command = build_command_line (compile_control, "");
1070 1.3 christos (*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
1071 1.3 christos }
1072 1.3 christos
1073 1.3 christos else if (p_end - p == 5 && !strncmp (p, "guile", 5))
1074 1.3 christos {
1075 1.3 christos /* Note that we ignore the inline "guile command" form here. */
1076 1.1 christos *command = build_command_line (guile_control, "");
1077 1.1 christos }
1078 1.1 christos else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
1079 1.1 christos {
1080 1.1 christos *command = (struct command_line *)
1081 1.1 christos xmalloc (sizeof (struct command_line));
1082 1.1 christos (*command)->next = NULL;
1083 1.1 christos (*command)->line = NULL;
1084 1.1 christos (*command)->control_type = break_control;
1085 1.1 christos (*command)->body_count = 0;
1086 1.1 christos (*command)->body_list = NULL;
1087 1.1 christos }
1088 1.1 christos else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
1089 1.1 christos {
1090 1.1 christos *command = (struct command_line *)
1091 1.1 christos xmalloc (sizeof (struct command_line));
1092 1.1 christos (*command)->next = NULL;
1093 1.1 christos (*command)->line = NULL;
1094 1.1 christos (*command)->control_type = continue_control;
1095 1.1 christos (*command)->body_count = 0;
1096 1.1 christos (*command)->body_list = NULL;
1097 1.1 christos }
1098 1.1 christos else
1099 1.1 christos not_handled = 1;
1100 1.1 christos }
1101 1.1 christos
1102 1.1 christos if (!parse_commands || not_handled)
1103 1.1 christos {
1104 1.1 christos /* A normal command. */
1105 1.1 christos *command = (struct command_line *)
1106 1.1 christos xmalloc (sizeof (struct command_line));
1107 1.1 christos (*command)->next = NULL;
1108 1.1 christos (*command)->line = savestring (p, p_end - p);
1109 1.1 christos (*command)->control_type = simple_control;
1110 1.1 christos (*command)->body_count = 0;
1111 1.1 christos (*command)->body_list = NULL;
1112 1.1 christos }
1113 1.1 christos
1114 1.1 christos if (validator)
1115 1.1 christos {
1116 1.1 christos volatile struct gdb_exception ex;
1117 1.1 christos
1118 1.1 christos TRY_CATCH (ex, RETURN_MASK_ALL)
1119 1.1 christos {
1120 1.1 christos validator ((*command)->line, closure);
1121 1.1 christos }
1122 1.1 christos if (ex.reason < 0)
1123 1.1 christos {
1124 1.1 christos xfree (*command);
1125 1.1 christos throw_exception (ex);
1126 1.1 christos }
1127 1.1 christos }
1128 1.1 christos
1129 1.1 christos /* Nothing special. */
1130 1.1 christos return ok_command;
1131 1.1 christos }
1132 1.1 christos
1133 1.1 christos /* Recursively read in the control structures and create a
1134 1.1 christos command_line structure from them. Use read_next_line_func to
1135 1.1 christos obtain lines of the command. */
1136 1.1 christos
1137 1.1 christos static enum command_control_type
1138 1.1 christos recurse_read_control_structure (char * (*read_next_line_func) (void),
1139 1.1 christos struct command_line *current_cmd,
1140 1.1 christos void (*validator)(char *, void *),
1141 1.1 christos void *closure)
1142 1.1 christos {
1143 1.1 christos int current_body, i;
1144 1.1 christos enum misc_command_type val;
1145 1.1 christos enum command_control_type ret;
1146 1.1 christos struct command_line **body_ptr, *child_tail, *next;
1147 1.1 christos
1148 1.1 christos child_tail = NULL;
1149 1.1 christos current_body = 1;
1150 1.1 christos
1151 1.1 christos /* Sanity checks. */
1152 1.1 christos if (current_cmd->control_type == simple_control)
1153 1.1 christos error (_("Recursed on a simple control type."));
1154 1.1 christos
1155 1.1 christos if (current_body > current_cmd->body_count)
1156 1.1 christos error (_("Allocated body is smaller than this command type needs."));
1157 1.1 christos
1158 1.1 christos /* Read lines from the input stream and build control structures. */
1159 1.1 christos while (1)
1160 1.1 christos {
1161 1.1 christos dont_repeat ();
1162 1.1 christos
1163 1.3 christos next = NULL;
1164 1.3 christos val = process_next_line (read_next_line_func (), &next,
1165 1.3 christos current_cmd->control_type != python_control
1166 1.1 christos && current_cmd->control_type != guile_control
1167 1.1 christos && current_cmd->control_type != compile_control,
1168 1.1 christos validator, closure);
1169 1.1 christos
1170 1.1 christos /* Just skip blanks and comments. */
1171 1.1 christos if (val == nop_command)
1172 1.1 christos continue;
1173 1.1 christos
1174 1.1 christos if (val == end_command)
1175 1.1 christos {
1176 1.1 christos if (multi_line_command_p (current_cmd->control_type))
1177 1.1 christos {
1178 1.1 christos /* Success reading an entire canned sequence of commands. */
1179 1.1 christos ret = simple_control;
1180 1.1 christos break;
1181 1.1 christos }
1182 1.1 christos else
1183 1.1 christos {
1184 1.1 christos ret = invalid_control;
1185 1.1 christos break;
1186 1.1 christos }
1187 1.1 christos }
1188 1.1 christos
1189 1.1 christos /* Not the end of a control structure. */
1190 1.1 christos if (val == else_command)
1191 1.1 christos {
1192 1.1 christos if (current_cmd->control_type == if_control
1193 1.1 christos && current_body == 1)
1194 1.1 christos {
1195 1.1 christos realloc_body_list (current_cmd, 2);
1196 1.1 christos current_body = 2;
1197 1.1 christos child_tail = NULL;
1198 1.1 christos continue;
1199 1.1 christos }
1200 1.1 christos else
1201 1.1 christos {
1202 1.1 christos ret = invalid_control;
1203 1.1 christos break;
1204 1.1 christos }
1205 1.1 christos }
1206 1.1 christos
1207 1.1 christos if (child_tail)
1208 1.1 christos {
1209 1.1 christos child_tail->next = next;
1210 1.1 christos }
1211 1.1 christos else
1212 1.1 christos {
1213 1.1 christos body_ptr = current_cmd->body_list;
1214 1.1 christos for (i = 1; i < current_body; i++)
1215 1.1 christos body_ptr++;
1216 1.1 christos
1217 1.1 christos *body_ptr = next;
1218 1.1 christos
1219 1.1 christos }
1220 1.1 christos
1221 1.1 christos child_tail = next;
1222 1.1 christos
1223 1.1 christos /* If the latest line is another control structure, then recurse
1224 1.1 christos on it. */
1225 1.1 christos if (multi_line_command_p (next->control_type))
1226 1.1 christos {
1227 1.1 christos control_level++;
1228 1.1 christos ret = recurse_read_control_structure (read_next_line_func, next,
1229 1.1 christos validator, closure);
1230 1.1 christos control_level--;
1231 1.1 christos
1232 1.1 christos if (ret != simple_control)
1233 1.1 christos break;
1234 1.1 christos }
1235 1.1 christos }
1236 1.1 christos
1237 1.1 christos dont_repeat ();
1238 1.1 christos
1239 1.1 christos return ret;
1240 1.1 christos }
1241 1.1 christos
1242 1.1 christos static void
1243 1.1 christos restore_interp (void *arg)
1244 1.1 christos {
1245 1.1 christos interp_set_temp (interp_name ((struct interp *)arg));
1246 1.1 christos }
1247 1.1 christos
1248 1.1 christos /* Read lines from the input stream and accumulate them in a chain of
1249 1.1 christos struct command_line's, which is then returned. For input from a
1250 1.1 christos terminal, the special command "end" is used to mark the end of the
1251 1.1 christos input, and is not included in the returned chain of commands.
1252 1.1 christos
1253 1.1 christos If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1254 1.1 christos is always stripped) in the line and attempt to recognize GDB control
1255 1.1 christos commands. Otherwise, only "end" is recognized. */
1256 1.1 christos
1257 1.1 christos #define END_MESSAGE "End with a line saying just \"end\"."
1258 1.1 christos
1259 1.1 christos struct command_line *
1260 1.1 christos read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1261 1.1 christos void (*validator)(char *, void *), void *closure)
1262 1.1 christos {
1263 1.1 christos struct command_line *head;
1264 1.1 christos
1265 1.1 christos if (from_tty && input_from_terminal_p ())
1266 1.1 christos {
1267 1.1 christos if (deprecated_readline_begin_hook)
1268 1.1 christos {
1269 1.1 christos /* Note - intentional to merge messages with no newline. */
1270 1.1 christos (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg,
1271 1.1 christos END_MESSAGE);
1272 1.1 christos }
1273 1.1 christos else
1274 1.1 christos {
1275 1.1 christos printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1276 1.1 christos gdb_flush (gdb_stdout);
1277 1.1 christos }
1278 1.1 christos }
1279 1.1 christos
1280 1.1 christos
1281 1.1 christos /* Reading commands assumes the CLI behavior, so temporarily
1282 1.1 christos override the current interpreter with CLI. */
1283 1.1 christos if (current_interp_named_p (INTERP_CONSOLE))
1284 1.1 christos head = read_command_lines_1 (read_next_line, parse_commands,
1285 1.1 christos validator, closure);
1286 1.1 christos else
1287 1.1 christos {
1288 1.1 christos struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
1289 1.1 christos struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
1290 1.1 christos
1291 1.1 christos head = read_command_lines_1 (read_next_line, parse_commands,
1292 1.1 christos validator, closure);
1293 1.1 christos do_cleanups (old_chain);
1294 1.1 christos }
1295 1.1 christos
1296 1.1 christos if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1297 1.1 christos {
1298 1.1 christos (*deprecated_readline_end_hook) ();
1299 1.1 christos }
1300 1.1 christos return (head);
1301 1.1 christos }
1302 1.1 christos
1303 1.1 christos /* Act the same way as read_command_lines, except that each new line is
1304 1.1 christos obtained using READ_NEXT_LINE_FUNC. */
1305 1.1 christos
1306 1.1 christos struct command_line *
1307 1.1 christos read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1308 1.1 christos void (*validator)(char *, void *), void *closure)
1309 1.1 christos {
1310 1.1 christos struct command_line *head, *tail, *next;
1311 1.1 christos struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
1312 1.1 christos enum command_control_type ret;
1313 1.1 christos enum misc_command_type val;
1314 1.1 christos
1315 1.1 christos control_level = 0;
1316 1.1 christos head = tail = NULL;
1317 1.1 christos
1318 1.1 christos while (1)
1319 1.1 christos {
1320 1.1 christos dont_repeat ();
1321 1.1 christos val = process_next_line (read_next_line_func (), &next, parse_commands,
1322 1.1 christos validator, closure);
1323 1.1 christos
1324 1.1 christos /* Ignore blank lines or comments. */
1325 1.1 christos if (val == nop_command)
1326 1.1 christos continue;
1327 1.1 christos
1328 1.1 christos if (val == end_command)
1329 1.1 christos {
1330 1.1 christos ret = simple_control;
1331 1.1 christos break;
1332 1.1 christos }
1333 1.1 christos
1334 1.1 christos if (val != ok_command)
1335 1.1 christos {
1336 1.1 christos ret = invalid_control;
1337 1.1 christos break;
1338 1.1 christos }
1339 1.1 christos
1340 1.1 christos if (multi_line_command_p (next->control_type))
1341 1.1 christos {
1342 1.1 christos control_level++;
1343 1.1 christos ret = recurse_read_control_structure (read_next_line_func, next,
1344 1.1 christos validator, closure);
1345 1.1 christos control_level--;
1346 1.1 christos
1347 1.1 christos if (ret == invalid_control)
1348 1.1 christos break;
1349 1.1 christos }
1350 1.1 christos
1351 1.1 christos if (tail)
1352 1.1 christos {
1353 1.1 christos tail->next = next;
1354 1.1 christos }
1355 1.1 christos else
1356 1.1 christos {
1357 1.1 christos head = next;
1358 1.1 christos make_cleanup_free_command_lines (&head);
1359 1.1 christos }
1360 1.1 christos tail = next;
1361 1.1 christos }
1362 1.1 christos
1363 1.1 christos dont_repeat ();
1364 1.1 christos
1365 1.1 christos if (ret != invalid_control)
1366 1.1 christos discard_cleanups (old_chain);
1367 1.1 christos else
1368 1.1 christos do_cleanups (old_chain);
1369 1.1 christos
1370 1.1 christos return head;
1371 1.1 christos }
1372 1.1 christos
1373 1.1 christos /* Free a chain of struct command_line's. */
1374 1.1 christos
1375 1.1 christos void
1376 1.1 christos free_command_lines (struct command_line **lptr)
1377 1.1 christos {
1378 1.1 christos struct command_line *l = *lptr;
1379 1.1 christos struct command_line *next;
1380 1.1 christos struct command_line **blist;
1381 1.1 christos int i;
1382 1.1 christos
1383 1.1 christos while (l)
1384 1.1 christos {
1385 1.1 christos if (l->body_count > 0)
1386 1.1 christos {
1387 1.1 christos blist = l->body_list;
1388 1.1 christos for (i = 0; i < l->body_count; i++, blist++)
1389 1.1 christos free_command_lines (blist);
1390 1.1 christos }
1391 1.1 christos next = l->next;
1392 1.1 christos xfree (l->line);
1393 1.1 christos xfree (l);
1394 1.1 christos l = next;
1395 1.1 christos }
1396 1.1 christos *lptr = NULL;
1397 1.1 christos }
1398 1.1 christos
1399 1.1 christos static void
1400 1.1 christos do_free_command_lines_cleanup (void *arg)
1401 1.1 christos {
1402 1.1 christos free_command_lines (arg);
1403 1.1 christos }
1404 1.1 christos
1405 1.1 christos struct cleanup *
1406 1.1 christos make_cleanup_free_command_lines (struct command_line **arg)
1407 1.1 christos {
1408 1.1 christos return make_cleanup (do_free_command_lines_cleanup, arg);
1409 1.1 christos }
1410 1.1 christos
1411 1.1 christos struct command_line *
1412 1.1 christos copy_command_lines (struct command_line *cmds)
1413 1.1 christos {
1414 1.1 christos struct command_line *result = NULL;
1415 1.1 christos
1416 1.1 christos if (cmds)
1417 1.1 christos {
1418 1.1 christos result = (struct command_line *) xmalloc (sizeof (struct command_line));
1419 1.1 christos
1420 1.1 christos result->next = copy_command_lines (cmds->next);
1421 1.1 christos result->line = xstrdup (cmds->line);
1422 1.1 christos result->control_type = cmds->control_type;
1423 1.1 christos result->body_count = cmds->body_count;
1424 1.1 christos if (cmds->body_count > 0)
1425 1.1 christos {
1426 1.1 christos int i;
1427 1.1 christos
1428 1.1 christos result->body_list = (struct command_line **)
1429 1.1 christos xmalloc (sizeof (struct command_line *) * cmds->body_count);
1430 1.1 christos
1431 1.1 christos for (i = 0; i < cmds->body_count; i++)
1432 1.1 christos result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1433 1.1 christos }
1434 1.1 christos else
1435 1.1 christos result->body_list = NULL;
1436 1.1 christos }
1437 1.1 christos
1438 1.1 christos return result;
1439 1.1 christos }
1440 1.1 christos
1441 1.1 christos /* Validate that *COMNAME is a valid name for a command. Return the
1443 1.1 christos containing command list, in case it starts with a prefix command.
1444 1.1 christos The prefix must already exist. *COMNAME is advanced to point after
1445 1.1 christos any prefix, and a NUL character overwrites the space after the
1446 1.1 christos prefix. */
1447 1.1 christos
1448 1.1 christos static struct cmd_list_element **
1449 1.1 christos validate_comname (char **comname)
1450 1.1 christos {
1451 1.1 christos struct cmd_list_element **list = &cmdlist;
1452 1.1 christos char *p, *last_word;
1453 1.1 christos
1454 1.1 christos if (*comname == 0)
1455 1.1 christos error_no_arg (_("name of command to define"));
1456 1.1 christos
1457 1.1 christos /* Find the last word of the argument. */
1458 1.1 christos p = *comname + strlen (*comname);
1459 1.1 christos while (p > *comname && isspace (p[-1]))
1460 1.1 christos p--;
1461 1.1 christos while (p > *comname && !isspace (p[-1]))
1462 1.1 christos p--;
1463 1.1 christos last_word = p;
1464 1.1 christos
1465 1.1 christos /* Find the corresponding command list. */
1466 1.1 christos if (last_word != *comname)
1467 1.1 christos {
1468 1.1 christos struct cmd_list_element *c;
1469 1.1 christos char saved_char;
1470 1.1 christos const char *tem = *comname;
1471 1.1 christos
1472 1.1 christos /* Separate the prefix and the command. */
1473 1.1 christos saved_char = last_word[-1];
1474 1.1 christos last_word[-1] = '\0';
1475 1.1 christos
1476 1.1 christos c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1477 1.1 christos if (c->prefixlist == NULL)
1478 1.1 christos error (_("\"%s\" is not a prefix command."), *comname);
1479 1.1 christos
1480 1.1 christos list = c->prefixlist;
1481 1.1 christos last_word[-1] = saved_char;
1482 1.1 christos *comname = last_word;
1483 1.1 christos }
1484 1.1 christos
1485 1.1 christos p = *comname;
1486 1.1 christos while (*p)
1487 1.1 christos {
1488 1.1 christos if (!isalnum (*p) && *p != '-' && *p != '_')
1489 1.1 christos error (_("Junk in argument list: \"%s\""), p);
1490 1.1 christos p++;
1491 1.1 christos }
1492 1.1 christos
1493 1.1 christos return list;
1494 1.1 christos }
1495 1.1 christos
1496 1.1 christos /* This is just a placeholder in the command data structures. */
1497 1.1 christos static void
1498 1.1 christos user_defined_command (char *ignore, int from_tty)
1499 1.1 christos {
1500 1.1 christos }
1501 1.1 christos
1502 1.1 christos static void
1503 1.1 christos define_command (char *comname, int from_tty)
1504 1.1 christos {
1505 1.1 christos #define MAX_TMPBUF 128
1506 1.1 christos enum cmd_hook_type
1507 1.1 christos {
1508 1.1 christos CMD_NO_HOOK = 0,
1509 1.1 christos CMD_PRE_HOOK,
1510 1.1 christos CMD_POST_HOOK
1511 1.1 christos };
1512 1.1 christos struct command_line *cmds;
1513 1.1 christos struct cmd_list_element *c, *newc, *hookc = 0, **list;
1514 1.1 christos char *tem, *comfull;
1515 1.1 christos const char *tem_c;
1516 1.1 christos char tmpbuf[MAX_TMPBUF];
1517 1.1 christos int hook_type = CMD_NO_HOOK;
1518 1.1 christos int hook_name_size = 0;
1519 1.1 christos
1520 1.1 christos #define HOOK_STRING "hook-"
1521 1.1 christos #define HOOK_LEN 5
1522 1.1 christos #define HOOK_POST_STRING "hookpost-"
1523 1.1 christos #define HOOK_POST_LEN 9
1524 1.1 christos
1525 1.1 christos comfull = comname;
1526 1.1 christos list = validate_comname (&comname);
1527 1.1 christos
1528 1.1 christos /* Look it up, and verify that we got an exact match. */
1529 1.1 christos tem_c = comname;
1530 1.1 christos c = lookup_cmd (&tem_c, *list, "", -1, 1);
1531 1.1 christos if (c && strcmp (comname, c->name) != 0)
1532 1.1 christos c = 0;
1533 1.1 christos
1534 1.1 christos if (c)
1535 1.1 christos {
1536 1.1 christos int q;
1537 1.1 christos
1538 1.1 christos if (c->class == class_user || c->class == class_alias)
1539 1.1 christos q = query (_("Redefine command \"%s\"? "), c->name);
1540 1.1 christos else
1541 1.1 christos q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1542 1.1 christos if (!q)
1543 1.1 christos error (_("Command \"%s\" not redefined."), c->name);
1544 1.1 christos }
1545 1.1 christos
1546 1.1 christos /* If this new command is a hook, then mark the command which it
1547 1.1 christos is hooking. Note that we allow hooking `help' commands, so that
1548 1.1 christos we can hook the `stop' pseudo-command. */
1549 1.1 christos
1550 1.1 christos if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1551 1.1 christos {
1552 1.1 christos hook_type = CMD_PRE_HOOK;
1553 1.1 christos hook_name_size = HOOK_LEN;
1554 1.1 christos }
1555 1.1 christos else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1556 1.1 christos {
1557 1.1 christos hook_type = CMD_POST_HOOK;
1558 1.1 christos hook_name_size = HOOK_POST_LEN;
1559 1.1 christos }
1560 1.1 christos
1561 1.1 christos if (hook_type != CMD_NO_HOOK)
1562 1.1 christos {
1563 1.1 christos /* Look up cmd it hooks, and verify that we got an exact match. */
1564 1.1 christos tem_c = comname + hook_name_size;
1565 1.1 christos hookc = lookup_cmd (&tem_c, *list, "", -1, 0);
1566 1.1 christos if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1567 1.1 christos hookc = 0;
1568 1.1 christos if (!hookc)
1569 1.1 christos {
1570 1.1 christos warning (_("Your new `%s' command does not "
1571 1.1 christos "hook any existing command."),
1572 1.1 christos comfull);
1573 1.1 christos if (!query (_("Proceed? ")))
1574 1.1 christos error (_("Not confirmed."));
1575 1.1 christos }
1576 1.1 christos }
1577 1.1 christos
1578 1.1 christos comname = xstrdup (comname);
1579 1.1 christos
1580 1.1 christos /* If the rest of the commands will be case insensitive, this one
1581 1.1 christos should behave in the same manner. */
1582 1.1 christos for (tem = comname; *tem; tem++)
1583 1.1 christos if (isupper (*tem))
1584 1.1 christos *tem = tolower (*tem);
1585 1.1 christos
1586 1.1 christos xsnprintf (tmpbuf, sizeof (tmpbuf),
1587 1.1 christos "Type commands for definition of \"%s\".", comfull);
1588 1.1 christos cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
1589 1.1 christos
1590 1.1 christos if (c && c->class == class_user)
1591 1.1 christos free_command_lines (&c->user_commands);
1592 1.1 christos
1593 1.1 christos newc = add_cmd (comname, class_user, user_defined_command,
1594 1.1 christos (c && c->class == class_user)
1595 1.1 christos ? c->doc : xstrdup ("User-defined."), list);
1596 1.1 christos newc->user_commands = cmds;
1597 1.1 christos
1598 1.1 christos /* If this new command is a hook, then mark both commands as being
1599 1.1 christos tied. */
1600 1.1 christos if (hookc)
1601 1.1 christos {
1602 1.1 christos switch (hook_type)
1603 1.1 christos {
1604 1.1 christos case CMD_PRE_HOOK:
1605 1.1 christos hookc->hook_pre = newc; /* Target gets hooked. */
1606 1.1 christos newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1607 1.1 christos break;
1608 1.1 christos case CMD_POST_HOOK:
1609 1.1 christos hookc->hook_post = newc; /* Target gets hooked. */
1610 1.1 christos newc->hookee_post = hookc; /* We are marked as hooking
1611 1.1 christos target cmd. */
1612 1.1 christos break;
1613 1.1 christos default:
1614 1.1 christos /* Should never come here as hookc would be 0. */
1615 1.1 christos internal_error (__FILE__, __LINE__, _("bad switch"));
1616 1.1 christos }
1617 1.1 christos }
1618 1.1 christos }
1619 1.1 christos
1620 1.1 christos static void
1621 1.1 christos document_command (char *comname, int from_tty)
1622 1.1 christos {
1623 1.1 christos struct command_line *doclines;
1624 1.1 christos struct cmd_list_element *c, **list;
1625 1.1 christos const char *tem;
1626 1.1 christos char *comfull;
1627 1.1 christos char tmpbuf[128];
1628 1.1 christos
1629 1.1 christos comfull = comname;
1630 1.1 christos list = validate_comname (&comname);
1631 1.1 christos
1632 1.1 christos tem = comname;
1633 1.1 christos c = lookup_cmd (&tem, *list, "", 0, 1);
1634 1.1 christos
1635 1.1 christos if (c->class != class_user)
1636 1.1 christos error (_("Command \"%s\" is built-in."), comfull);
1637 1.1 christos
1638 1.1 christos xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
1639 1.1 christos comfull);
1640 1.3 christos doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
1641 1.1 christos
1642 1.1 christos if (c->doc)
1643 1.1 christos xfree ((char *) c->doc);
1644 1.1 christos
1645 1.3 christos {
1646 1.1 christos struct command_line *cl1;
1647 1.1 christos int len = 0;
1648 1.1 christos char *doc;
1649 1.1 christos
1650 1.3 christos for (cl1 = doclines; cl1; cl1 = cl1->next)
1651 1.3 christos len += strlen (cl1->line) + 1;
1652 1.1 christos
1653 1.1 christos doc = (char *) xmalloc (len + 1);
1654 1.1 christos *doc = 0;
1655 1.3 christos
1656 1.1 christos for (cl1 = doclines; cl1; cl1 = cl1->next)
1657 1.3 christos {
1658 1.1 christos strcat (doc, cl1->line);
1659 1.3 christos if (cl1->next)
1660 1.3 christos strcat (doc, "\n");
1661 1.1 christos }
1662 1.1 christos
1663 1.1 christos c->doc = doc;
1664 1.1 christos }
1665 1.1 christos
1666 1.1 christos free_command_lines (&doclines);
1667 1.1 christos }
1668 1.1 christos
1669 1.1 christos struct source_cleanup_lines_args
1671 1.1 christos {
1672 1.1 christos int old_line;
1673 1.1 christos const char *old_file;
1674 1.1 christos };
1675 1.1 christos
1676 1.1 christos static void
1677 1.1 christos source_cleanup_lines (void *args)
1678 1.1 christos {
1679 1.1 christos struct source_cleanup_lines_args *p =
1680 1.1 christos (struct source_cleanup_lines_args *) args;
1681 1.1 christos
1682 1.1 christos source_line_number = p->old_line;
1683 1.1 christos source_file_name = p->old_file;
1684 1.1 christos }
1685 1.1 christos
1686 1.1 christos /* Used to implement source_command. */
1687 1.1 christos
1688 1.1 christos void
1689 1.1 christos script_from_file (FILE *stream, const char *file)
1690 1.1 christos {
1691 1.1 christos struct cleanup *old_cleanups;
1692 1.1 christos struct source_cleanup_lines_args old_lines;
1693 1.1 christos
1694 1.1 christos if (stream == NULL)
1695 1.1 christos internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1696 1.1 christos
1697 1.1 christos old_lines.old_line = source_line_number;
1698 1.1 christos old_lines.old_file = source_file_name;
1699 1.3 christos old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
1700 1.3 christos source_line_number = 0;
1701 1.3 christos source_file_name = file;
1702 1.1 christos
1703 1.1 christos make_cleanup_restore_integer (&interpreter_async);
1704 1.1 christos interpreter_async = 0;
1705 1.1 christos
1706 1.1 christos {
1707 1.1 christos volatile struct gdb_exception e;
1708 1.1 christos
1709 1.1 christos TRY_CATCH (e, RETURN_MASK_ERROR)
1710 1.1 christos {
1711 1.1 christos read_command_file (stream);
1712 1.1 christos }
1713 1.1 christos switch (e.reason)
1714 1.1 christos {
1715 1.1 christos case 0:
1716 1.1 christos break;
1717 1.1 christos case RETURN_ERROR:
1718 1.1 christos /* Re-throw the error, but with the file name information
1719 1.1 christos prepended. */
1720 1.1 christos throw_error (e.error,
1721 1.1 christos _("%s:%d: Error in sourced command file:\n%s"),
1722 1.1 christos source_file_name, source_line_number, e.message);
1723 1.1 christos default:
1724 1.1 christos internal_error (__FILE__, __LINE__, _("bad reason"));
1725 1.1 christos }
1726 1.1 christos }
1727 1.1 christos
1728 1.1 christos do_cleanups (old_cleanups);
1729 1.1 christos }
1730 1.1 christos
1731 1.1 christos /* Print the definition of user command C to STREAM. Or, if C is a
1732 1.1 christos prefix command, show the definitions of all user commands under C
1733 1.1 christos (recursively). PREFIX and NAME combined are the name of the
1734 1.1 christos current command. */
1735 1.1 christos void
1736 1.1 christos show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
1737 1.1 christos struct ui_file *stream)
1738 1.1 christos {
1739 1.3 christos struct command_line *cmdlines;
1740 1.1 christos
1741 1.1 christos if (c->prefixlist != NULL)
1742 1.1 christos {
1743 1.1 christos const char *prefixname = c->prefixname;
1744 1.1 christos
1745 1.1 christos for (c = *c->prefixlist; c != NULL; c = c->next)
1746 1.1 christos if (c->class == class_user || c->prefixlist != NULL)
1747 1.1 christos show_user_1 (c, prefixname, c->name, gdb_stdout);
1748 1.3 christos return;
1749 1.3 christos }
1750 1.1 christos
1751 1.1 christos cmdlines = c->user_commands;
1752 1.1 christos fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1753 1.1 christos
1754 1.1 christos if (!cmdlines)
1755 1.1 christos return;
1756 1.1 christos print_command_lines (current_uiout, cmdlines, 1);
1757 1.1 christos fputs_filtered ("\n", stream);
1758 1.1 christos }
1759 1.1 christos
1760 1.1 christos
1761 1.1 christos
1763 1.1 christos initialize_file_ftype _initialize_cli_script;
1764 1.1 christos
1765 1.1 christos void
1766 1.1 christos _initialize_cli_script (void)
1767 1.1 christos {
1768 1.1 christos add_com ("document", class_support, document_command, _("\
1769 1.1 christos Document a user-defined command.\n\
1770 1.1 christos Give command name as argument. Give documentation on following lines.\n\
1771 1.1 christos End with a line of just \"end\"."));
1772 1.1 christos add_com ("define", class_support, define_command, _("\
1773 1.1 christos Define a new command name. Command name is argument.\n\
1774 1.1 christos Definition appears on following lines, one command per line.\n\
1775 1.1 christos End with a line of just \"end\".\n\
1776 1.1 christos Use the \"document\" command to give documentation for the new command.\n\
1777 1.1 christos Commands defined in this way may have up to ten arguments."));
1778 1.1 christos
1779 1.1 christos add_com ("while", class_support, while_command, _("\
1780 1.1 christos Execute nested commands WHILE the conditional expression is non zero.\n\
1781 1.1 christos The conditional expression must follow the word `while' and must in turn be\n\
1782 1.1 christos followed by a new line. The nested commands must be entered one per line,\n\
1783 1.1 christos and should be terminated by the word `end'."));
1784 1.1 christos
1785 1.1 christos add_com ("if", class_support, if_command, _("\
1786 1.1 christos Execute nested commands once IF the conditional expression is non zero.\n\
1787 The conditional expression must follow the word `if' and must in turn be\n\
1788 followed by a new line. The nested commands must be entered one per line,\n\
1789 and should be terminated by the word 'else' or `end'. If an else clause\n\
1790 is used, the same rules apply to its nested commands as to the first ones."));
1791 }
1792