1 /* GDB CLI command scripting.
3 Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008,
5 2009 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "language.h" /* For value_true */
28 #include "gdb_string.h"
29 #include "exceptions.h"
31 #include "breakpoint.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-script.h"
35 #include "gdb_assert.h"
37 #include "python/python.h"
39 /* Prototypes for local functions */
41 static enum command_control_type
42 recurse_read_control_structure (char * (*read_next_line_func) (),
43 struct command_line *current_cmd);
45 static char *insert_args (char *line);
47 static struct cleanup * setup_user_args (char *p);
49 static char *read_next_line ();
51 /* Level of control structure when reading. */
52 static int control_level;
54 /* Level of control structure when executing. */
55 static int command_nest_depth = 1;
57 /* This is to prevent certain commands being printed twice. */
58 static int suppress_next_print_command_trace = 0;
60 /* Structure for arguments to user defined functions. */
61 #define MAXUSERARGS 10
64 struct user_args *next;
65 /* It is necessary to store a malloced copy of the command line to
66 ensure that the arguments are not overwritten before they are used. */
79 /* Allocate, initialize a new command line structure for one of the
80 control commands (if/while). */
82 static struct command_line *
83 build_command_line (enum command_control_type type, char *args)
85 struct command_line *cmd;
87 if (args == NULL && (type == if_control || type == while_control))
88 error (_("if/while commands require arguments."));
89 gdb_assert (args != NULL);
91 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
93 cmd->control_type = type;
97 = (struct command_line **) xmalloc (sizeof (struct command_line *)
99 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
100 cmd->line = xstrdup (args);
105 /* Build and return a new command structure for the control commands
106 such as "if" and "while". */
108 struct command_line *
109 get_command_line (enum command_control_type type, char *arg)
111 struct command_line *cmd;
112 struct cleanup *old_chain = NULL;
114 /* Allocate and build a new command line structure. */
115 cmd = build_command_line (type, arg);
117 old_chain = make_cleanup_free_command_lines (&cmd);
119 /* Read in the body of this command. */
120 if (recurse_read_control_structure (read_next_line, cmd) == invalid_control)
122 warning (_("Error reading in canned sequence of commands."));
123 do_cleanups (old_chain);
127 discard_cleanups (old_chain);
131 /* Recursively print a command (including full control structures). */
134 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
137 struct command_line *list;
144 ui_out_spaces (uiout, 2 * depth);
146 /* A simple command, print it and continue. */
147 if (list->control_type == simple_control)
149 ui_out_field_string (uiout, NULL, list->line);
150 ui_out_text (uiout, "\n");
155 /* loop_continue to jump to the start of a while loop, print it
157 if (list->control_type == continue_control)
159 ui_out_field_string (uiout, NULL, "loop_continue");
160 ui_out_text (uiout, "\n");
165 /* loop_break to break out of a while loop, print it and continue. */
166 if (list->control_type == break_control)
168 ui_out_field_string (uiout, NULL, "loop_break");
169 ui_out_text (uiout, "\n");
174 /* A while command. Recursively print its subcommands and continue. */
175 if (list->control_type == while_control)
177 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
178 ui_out_text (uiout, "\n");
179 print_command_lines (uiout, *list->body_list, depth + 1);
181 ui_out_spaces (uiout, 2 * depth);
182 ui_out_field_string (uiout, NULL, "end");
183 ui_out_text (uiout, "\n");
188 /* An if command. Recursively print both arms before continueing. */
189 if (list->control_type == if_control)
191 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
192 ui_out_text (uiout, "\n");
194 print_command_lines (uiout, list->body_list[0], depth + 1);
196 /* Show the false arm if it exists. */
197 if (list->body_count == 2)
200 ui_out_spaces (uiout, 2 * depth);
201 ui_out_field_string (uiout, NULL, "else");
202 ui_out_text (uiout, "\n");
203 print_command_lines (uiout, list->body_list[1], depth + 1);
207 ui_out_spaces (uiout, 2 * depth);
208 ui_out_field_string (uiout, NULL, "end");
209 ui_out_text (uiout, "\n");
214 /* A commands command. Print the breakpoint commands and continue. */
215 if (list->control_type == commands_control)
218 ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
220 ui_out_field_string (uiout, NULL, "commands");
221 ui_out_text (uiout, "\n");
222 print_command_lines (uiout, *list->body_list, depth + 1);
224 ui_out_spaces (uiout, 2 * depth);
225 ui_out_field_string (uiout, NULL, "end");
226 ui_out_text (uiout, "\n");
231 if (list->control_type == python_control)
233 ui_out_field_string (uiout, NULL, "python");
234 ui_out_text (uiout, "\n");
235 /* Don't indent python code at all. */
236 print_command_lines (uiout, *list->body_list, 0);
238 ui_out_spaces (uiout, 2 * depth);
239 ui_out_field_string (uiout, NULL, "end");
240 ui_out_text (uiout, "\n");
245 /* ignore illegal command type and try next */
250 /* Handle pre-post hooks. */
253 clear_hook_in_cleanup (void *data)
255 struct cmd_list_element *c = data;
256 c->hook_in = 0; /* Allow hook to work again once it is complete */
260 execute_cmd_pre_hook (struct cmd_list_element *c)
262 if ((c->hook_pre) && (!c->hook_in))
264 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
265 c->hook_in = 1; /* Prevent recursive hooking */
266 execute_user_command (c->hook_pre, (char *) 0);
267 do_cleanups (cleanups);
272 execute_cmd_post_hook (struct cmd_list_element *c)
274 if ((c->hook_post) && (!c->hook_in))
276 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
277 c->hook_in = 1; /* Prevent recursive hooking */
278 execute_user_command (c->hook_post, (char *) 0);
279 do_cleanups (cleanups);
283 /* Execute the command in CMD. */
285 do_restore_user_call_depth (void * call_depth)
287 int * depth = call_depth;
295 execute_user_command (struct cmd_list_element *c, char *args)
297 struct command_line *cmdlines;
298 struct cleanup *old_chain;
299 enum command_control_type ret;
300 static int user_call_depth = 0;
301 extern int max_user_call_depth;
303 old_chain = setup_user_args (args);
305 cmdlines = c->user_commands;
310 if (++user_call_depth > max_user_call_depth)
311 error (_("Max user call depth exceeded -- command aborted."));
313 make_cleanup (do_restore_user_call_depth, &user_call_depth);
315 /* Set the instream to 0, indicating execution of a
316 user-defined function. */
317 make_cleanup (do_restore_instream_cleanup, instream);
318 instream = (FILE *) 0;
320 /* Also set the global in_user_command, so that NULL instream is
321 not confused with Insight. */
324 command_nest_depth++;
327 ret = execute_control_command (cmdlines);
328 if (ret != simple_control && ret != break_control)
330 warning (_("Error executing canned sequence of commands."));
333 cmdlines = cmdlines->next;
335 command_nest_depth--;
336 do_cleanups (old_chain);
339 /* This function is called every time GDB prints a prompt.
340 It ensures that errors and the like to not confuse the command tracing. */
343 reset_command_nest_depth (void)
345 command_nest_depth = 1;
348 suppress_next_print_command_trace = 0;
351 /* Print the command, prefixed with '+' to represent the call depth.
352 This is slightly complicated because this function may be called
353 from execute_command and execute_control_command. Unfortunately
354 execute_command also prints the top level control commands.
355 In these cases execute_command will call execute_control_command
356 via while_command or if_command. Inner levels of 'if' and 'while'
357 are dealt with directly. Therefore we can use these functions
358 to determine whether the command has been printed already or not. */
360 print_command_trace (const char *cmd)
364 if (suppress_next_print_command_trace)
366 suppress_next_print_command_trace = 0;
370 if (!source_verbose && !trace_commands)
373 for (i=0; i < command_nest_depth; i++)
374 printf_filtered ("+");
376 printf_filtered ("%s\n", cmd);
379 enum command_control_type
380 execute_control_command (struct command_line *cmd)
382 struct expression *expr;
383 struct command_line *current;
384 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
386 struct value *val_mark;
388 enum command_control_type ret;
391 /* Start by assuming failure, if a problem is detected, the code
392 below will simply "break" out of the switch. */
393 ret = invalid_control;
395 switch (cmd->control_type)
398 /* A simple command, execute it and return. */
399 new_line = insert_args (cmd->line);
402 make_cleanup (free_current_contents, &new_line);
403 execute_command (new_line, 0);
404 ret = cmd->control_type;
407 case continue_control:
408 print_command_trace ("loop_continue");
410 /* Return for "continue", and "break" so we can either
411 continue the loop at the top, or break out. */
412 ret = cmd->control_type;
416 print_command_trace ("loop_break");
418 /* Return for "continue", and "break" so we can either
419 continue the loop at the top, or break out. */
420 ret = cmd->control_type;
425 char *buffer = alloca (strlen (cmd->line) + 7);
426 sprintf (buffer, "while %s", cmd->line);
427 print_command_trace (buffer);
429 /* Parse the loop control expression for the while statement. */
430 new_line = insert_args (cmd->line);
433 make_cleanup (free_current_contents, &new_line);
434 expr = parse_expression (new_line);
435 make_cleanup (free_current_contents, &expr);
437 ret = simple_control;
440 /* Keep iterating so long as the expression is true. */
447 /* Evaluate the expression. */
448 val_mark = value_mark ();
449 val = evaluate_expression (expr);
450 cond_result = value_true (val);
451 value_free_to_mark (val_mark);
453 /* If the value is false, then break out of the loop. */
457 /* Execute the body of the while statement. */
458 current = *cmd->body_list;
461 command_nest_depth++;
462 ret = execute_control_command (current);
463 command_nest_depth--;
465 /* If we got an error, or a "break" command, then stop
467 if (ret == invalid_control || ret == break_control)
473 /* If we got a "continue" command, then restart the loop
475 if (ret == continue_control)
478 /* Get the next statement. */
479 current = current->next;
483 /* Reset RET so that we don't recurse the break all the way down. */
484 if (ret == break_control)
485 ret = simple_control;
492 char *buffer = alloca (strlen (cmd->line) + 4);
493 sprintf (buffer, "if %s", cmd->line);
494 print_command_trace (buffer);
496 new_line = insert_args (cmd->line);
499 make_cleanup (free_current_contents, &new_line);
500 /* Parse the conditional for the if statement. */
501 expr = parse_expression (new_line);
502 make_cleanup (free_current_contents, &expr);
505 ret = simple_control;
507 /* Evaluate the conditional. */
508 val_mark = value_mark ();
509 val = evaluate_expression (expr);
511 /* Choose which arm to take commands from based on the value of the
512 conditional expression. */
513 if (value_true (val))
514 current = *cmd->body_list;
515 else if (cmd->body_count == 2)
516 current = *(cmd->body_list + 1);
517 value_free_to_mark (val_mark);
519 /* Execute commands in the given arm. */
522 command_nest_depth++;
523 ret = execute_control_command (current);
524 command_nest_depth--;
526 /* If we got an error, get out. */
527 if (ret != simple_control)
530 /* Get the next statement in the body. */
531 current = current->next;
536 case commands_control:
538 /* Breakpoint commands list, record the commands in the breakpoint's
539 command list and return. */
540 new_line = insert_args (cmd->line);
543 make_cleanup (free_current_contents, &new_line);
544 ret = commands_from_control_command (new_line, cmd);
549 eval_python_from_control_command (cmd);
550 ret = simple_control;
555 warning (_("Invalid control type in canned commands structure."));
559 do_cleanups (old_chain);
564 /* Like execute_control_command, but first set
565 suppress_next_print_command_trace. */
567 enum command_control_type
568 execute_control_command_untraced (struct command_line *cmd)
570 suppress_next_print_command_trace = 1;
571 return execute_control_command (cmd);
575 /* "while" command support. Executes a body of statements while the
576 loop condition is nonzero. */
579 while_command (char *arg, int from_tty)
581 struct command_line *command = NULL;
584 command = get_command_line (while_control, arg);
589 execute_control_command_untraced (command);
590 free_command_lines (&command);
593 /* "if" command support. Execute either the true or false arm depending
594 on the value of the if conditional. */
597 if_command (char *arg, int from_tty)
599 struct command_line *command = NULL;
602 command = get_command_line (if_control, arg);
607 execute_control_command_untraced (command);
608 free_command_lines (&command);
613 arg_cleanup (void *ignore)
615 struct user_args *oargs = user_args;
617 internal_error (__FILE__, __LINE__,
618 _("arg_cleanup called with no user args.\n"));
620 user_args = user_args->next;
621 xfree (oargs->command);
625 /* Bind the incomming arguments for a user defined command to
626 $arg0, $arg1 ... $argMAXUSERARGS. */
628 static struct cleanup *
629 setup_user_args (char *p)
631 struct user_args *args;
632 struct cleanup *old_chain;
633 unsigned int arg_count = 0;
635 args = (struct user_args *) xmalloc (sizeof (struct user_args));
636 memset (args, 0, sizeof (struct user_args));
638 args->next = user_args;
641 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
646 user_args->command = p = xstrdup (p);
655 if (arg_count >= MAXUSERARGS)
657 error (_("user defined function may only have %d arguments."),
662 /* Strip whitespace. */
663 while (*p == ' ' || *p == '\t')
666 /* P now points to an argument. */
668 user_args->a[arg_count].arg = p;
670 /* Get to the end of this argument. */
673 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
702 user_args->a[arg_count].len = p - start_arg;
709 /* Given character string P, return a point to the first argument ($arg),
710 or NULL if P contains no arguments. */
715 while ((p = strchr (p, '$')))
717 if (strncmp (p, "$arg", 4) == 0
718 && (isdigit (p[4]) || p[4] == 'c'))
725 /* Insert the user defined arguments stored in user_arg into the $arg
726 arguments found in line, with the updated copy being placed into nline. */
729 insert_args (char *line)
731 char *p, *save_line, *new_line;
734 /* If we are not in a user-defined function, treat $argc, $arg0, et
735 cetera as normal convenience variables. */
736 if (user_args == NULL)
737 return xstrdup (line);
739 /* First we need to know how much memory to allocate for the new line. */
742 while ((p = locate_arg (line)))
749 /* $argc. Number will be <=10. */
750 len += user_args->count == 10 ? 2 : 1;
752 else if (i >= user_args->count)
754 error (_("Missing argument %d in user function."), i);
759 len += user_args->a[i].len;
764 /* Don't forget the tail. */
765 len += strlen (line);
767 /* Allocate space for the new line and fill it in. */
768 new_line = (char *) xmalloc (len + 1);
769 if (new_line == NULL)
772 /* Restore pointer to beginning of old line. */
775 /* Save pointer to beginning of new line. */
776 save_line = new_line;
778 while ((p = locate_arg (line)))
782 memcpy (new_line, line, p - line);
783 new_line += p - line;
787 gdb_assert (user_args->count >= 0 && user_args->count <= 10);
788 if (user_args->count == 10)
794 *(new_line++) = user_args->count + '0';
799 len = user_args->a[i].len;
802 memcpy (new_line, user_args->a[i].arg, len);
808 /* Don't forget the tail. */
809 strcpy (new_line, line);
811 /* Return a pointer to the beginning of the new line. */
816 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
817 code bodies. This is typically used when we encounter an "else"
818 clause for an "if" command. */
821 realloc_body_list (struct command_line *command, int new_length)
824 struct command_line **body_list;
826 n = command->body_count;
832 body_list = (struct command_line **)
833 xmalloc (sizeof (struct command_line *) * new_length);
835 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
836 memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
838 xfree (command->body_list);
839 command->body_list = body_list;
840 command->body_count = new_length;
843 /* Read next line from stdout. Passed to read_command_line_1 and
844 recurse_read_control_structure whenever we need to read commands
850 char *prompt_ptr, control_prompt[256];
853 if (control_level >= 254)
854 error (_("Control nesting too deep!"));
856 /* Set a prompt based on the nesting of the control commands. */
857 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
859 for (i = 0; i < control_level; i++)
860 control_prompt[i] = ' ';
861 control_prompt[i] = '>';
862 control_prompt[i + 1] = '\0';
863 prompt_ptr = (char *) &control_prompt[0];
868 return command_line_input (prompt_ptr, instream == stdin, "commands");
871 /* Process one input line. If the command is an "end",
872 return such an indication to the caller. If PARSE_COMMANDS is true,
873 strip leading whitespace (trailing whitespace is always stripped)
874 in the line, attempt to recognize GDB control commands, and also
875 return an indication if the command is an "else" or a nop.
876 Otherwise, only "end" is recognized. */
878 static enum misc_command_type
879 process_next_line (char *p, struct command_line **command, int parse_commands)
885 /* Not sure what to do here. */
889 /* Strip trailing whitespace. */
891 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
895 /* Strip leading whitespace. */
896 while (*p2 == ' ' || *p2 == '\t')
899 /* 'end' is always recognized, regardless of parse_commands value.
900 We also permit whitespace before end and after. */
901 if (p1 - p2 == 3 && !strncmp (p2, "end", 3))
906 /* If commands are parsed, we skip initial spaces. Otherwise,
907 which is the case for Python commands and documentation
908 (see the 'document' command), spaces are preserved. */
911 /* Blanks and comments don't really do anything, but we need to
912 distinguish them from else, end and other commands which can be
914 if (p1 == p || p[0] == '#')
917 /* Is the else clause of an if control structure? */
918 if (p1 - p == 4 && !strncmp (p, "else", 4))
921 /* Check for while, if, break, continue, etc and build a new command
922 line structure for them. */
923 if (p1 - p > 5 && !strncmp (p, "while", 5))
927 while (first_arg < p1 && isspace (*first_arg))
929 *command = build_command_line (while_control, first_arg);
931 else if (p1 - p > 2 && !strncmp (p, "if", 2))
935 while (first_arg < p1 && isspace (*first_arg))
937 *command = build_command_line (if_control, first_arg);
939 else if (p1 - p >= 8 && !strncmp (p, "commands", 8))
943 while (first_arg < p1 && isspace (*first_arg))
945 *command = build_command_line (commands_control, first_arg);
947 else if (p1 - p == 6 && !strncmp (p, "python", 6))
949 /* Note that we ignore the inline "python command" form
951 *command = build_command_line (python_control, "");
953 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
955 *command = (struct command_line *)
956 xmalloc (sizeof (struct command_line));
957 (*command)->next = NULL;
958 (*command)->line = NULL;
959 (*command)->control_type = break_control;
960 (*command)->body_count = 0;
961 (*command)->body_list = NULL;
963 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
965 *command = (struct command_line *)
966 xmalloc (sizeof (struct command_line));
967 (*command)->next = NULL;
968 (*command)->line = NULL;
969 (*command)->control_type = continue_control;
970 (*command)->body_count = 0;
971 (*command)->body_list = NULL;
977 if (!parse_commands || not_handled)
979 /* A normal command. */
980 *command = (struct command_line *)
981 xmalloc (sizeof (struct command_line));
982 (*command)->next = NULL;
983 (*command)->line = savestring (p, p1 - p);
984 (*command)->control_type = simple_control;
985 (*command)->body_count = 0;
986 (*command)->body_list = NULL;
989 /* Nothing special. */
993 /* Recursively read in the control structures and create a command_line
994 structure from them. Use read_next_line_func to obtain lines of
999 static enum command_control_type
1000 recurse_read_control_structure (char * (*read_next_line_func) (),
1001 struct command_line *current_cmd)
1003 int current_body, i;
1004 enum misc_command_type val;
1005 enum command_control_type ret;
1006 struct command_line **body_ptr, *child_tail, *next;
1012 /* Sanity checks. */
1013 if (current_cmd->control_type == simple_control)
1014 error (_("Recursed on a simple control type."));
1016 if (current_body > current_cmd->body_count)
1017 error (_("Allocated body is smaller than this command type needs."));
1019 /* Read lines from the input stream and build control structures. */
1025 val = process_next_line (read_next_line_func (), &next,
1026 current_cmd->control_type != python_control);
1028 /* Just skip blanks and comments. */
1029 if (val == nop_command)
1032 if (val == end_command)
1034 if (current_cmd->control_type == while_control
1035 || current_cmd->control_type == if_control
1036 || current_cmd->control_type == python_control
1037 || current_cmd->control_type == commands_control)
1039 /* Success reading an entire canned sequence of commands. */
1040 ret = simple_control;
1045 ret = invalid_control;
1050 /* Not the end of a control structure. */
1051 if (val == else_command)
1053 if (current_cmd->control_type == if_control
1054 && current_body == 1)
1056 realloc_body_list (current_cmd, 2);
1063 ret = invalid_control;
1070 child_tail->next = next;
1074 body_ptr = current_cmd->body_list;
1075 for (i = 1; i < current_body; i++)
1084 /* If the latest line is another control structure, then recurse
1086 if (next->control_type == while_control
1087 || next->control_type == if_control
1088 || next->control_type == python_control
1089 || next->control_type == commands_control)
1092 ret = recurse_read_control_structure (read_next_line_func, next);
1095 if (ret != simple_control)
1105 /* Read lines from the input stream and accumulate them in a chain of
1106 struct command_line's, which is then returned. For input from a
1107 terminal, the special command "end" is used to mark the end of the
1108 input, and is not included in the returned chain of commands.
1110 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1111 is always stripped) in the line and attempt to recognize GDB control
1112 commands. Otherwise, only "end" is recognized. */
1114 #define END_MESSAGE "End with a line saying just \"end\"."
1116 struct command_line *
1117 read_command_lines (char *prompt_arg, int from_tty, int parse_commands)
1119 struct command_line *head;
1121 if (from_tty && input_from_terminal_p ())
1123 if (deprecated_readline_begin_hook)
1125 /* Note - intentional to merge messages with no newline */
1126 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1130 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1131 gdb_flush (gdb_stdout);
1135 head = read_command_lines_1 (read_next_line, parse_commands);
1137 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1139 (*deprecated_readline_end_hook) ();
1144 /* Act the same way as read_command_lines, except that each new line is
1145 obtained using READ_NEXT_LINE_FUNC. */
1147 struct command_line *
1148 read_command_lines_1 (char * (*read_next_line_func) (), int parse_commands)
1150 struct command_line *head, *tail, *next;
1151 struct cleanup *old_chain;
1152 enum command_control_type ret;
1153 enum misc_command_type val;
1162 val = process_next_line (read_next_line_func (), &next, parse_commands);
1164 /* Ignore blank lines or comments. */
1165 if (val == nop_command)
1168 if (val == end_command)
1170 ret = simple_control;
1174 if (val != ok_command)
1176 ret = invalid_control;
1180 if (next->control_type == while_control
1181 || next->control_type == if_control
1182 || next->control_type == python_control
1183 || next->control_type == commands_control)
1186 ret = recurse_read_control_structure (read_next_line_func, next);
1189 if (ret == invalid_control)
1200 old_chain = make_cleanup_free_command_lines (&head);
1209 if (ret != invalid_control)
1211 discard_cleanups (old_chain);
1214 do_cleanups (old_chain);
1220 /* Free a chain of struct command_line's. */
1223 free_command_lines (struct command_line **lptr)
1225 struct command_line *l = *lptr;
1226 struct command_line *next;
1227 struct command_line **blist;
1232 if (l->body_count > 0)
1234 blist = l->body_list;
1235 for (i = 0; i < l->body_count; i++, blist++)
1236 free_command_lines (blist);
1247 do_free_command_lines_cleanup (void *arg)
1249 free_command_lines (arg);
1253 make_cleanup_free_command_lines (struct command_line **arg)
1255 return make_cleanup (do_free_command_lines_cleanup, arg);
1258 struct command_line *
1259 copy_command_lines (struct command_line *cmds)
1261 struct command_line *result = NULL;
1265 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1267 result->next = copy_command_lines (cmds->next);
1268 result->line = xstrdup (cmds->line);
1269 result->control_type = cmds->control_type;
1270 result->body_count = cmds->body_count;
1271 if (cmds->body_count > 0)
1275 result->body_list = (struct command_line **)
1276 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1278 for (i = 0; i < cmds->body_count; i++)
1279 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1282 result->body_list = NULL;
1288 /* Validate that *COMNAME is a valid name for a command. Return the
1289 containing command list, in case it starts with a prefix command.
1290 The prefix must already exist. *COMNAME is advanced to point after
1291 any prefix, and a NUL character overwrites the space after the
1294 static struct cmd_list_element **
1295 validate_comname (char **comname)
1297 struct cmd_list_element **list = &cmdlist;
1298 char *p, *last_word;
1301 error_no_arg (_("name of command to define"));
1303 /* Find the last word of the argument. */
1304 p = *comname + strlen (*comname);
1305 while (p > *comname && isspace (p[-1]))
1307 while (p > *comname && !isspace (p[-1]))
1311 /* Find the corresponding command list. */
1312 if (last_word != *comname)
1314 struct cmd_list_element *c;
1315 char saved_char, *tem = *comname;
1317 /* Separate the prefix and the command. */
1318 saved_char = last_word[-1];
1319 last_word[-1] = '\0';
1321 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1322 if (c->prefixlist == NULL)
1323 error (_("\"%s\" is not a prefix command."), *comname);
1325 list = c->prefixlist;
1326 last_word[-1] = saved_char;
1327 *comname = last_word;
1333 if (!isalnum (*p) && *p != '-' && *p != '_')
1334 error (_("Junk in argument list: \"%s\""), p);
1341 /* This is just a placeholder in the command data structures. */
1343 user_defined_command (char *ignore, int from_tty)
1348 define_command (char *comname, int from_tty)
1350 #define MAX_TMPBUF 128
1357 struct command_line *cmds;
1358 struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
1359 char *tem, *tem2, *comfull;
1360 char tmpbuf[MAX_TMPBUF];
1361 int hook_type = CMD_NO_HOOK;
1362 int hook_name_size = 0;
1364 #define HOOK_STRING "hook-"
1366 #define HOOK_POST_STRING "hookpost-"
1367 #define HOOK_POST_LEN 9
1370 list = validate_comname (&comname);
1372 /* Look it up, and verify that we got an exact match. */
1374 c = lookup_cmd (&tem, *list, "", -1, 1);
1375 if (c && strcmp (comname, c->name) != 0)
1381 if (c->class == class_user || c->class == class_alias)
1382 q = query (_("Redefine command \"%s\"? "), c->name);
1384 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1386 error (_("Command \"%s\" not redefined."), c->name);
1389 /* If this new command is a hook, then mark the command which it
1390 is hooking. Note that we allow hooking `help' commands, so that
1391 we can hook the `stop' pseudo-command. */
1393 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1395 hook_type = CMD_PRE_HOOK;
1396 hook_name_size = HOOK_LEN;
1398 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1400 hook_type = CMD_POST_HOOK;
1401 hook_name_size = HOOK_POST_LEN;
1404 if (hook_type != CMD_NO_HOOK)
1406 /* Look up cmd it hooks, and verify that we got an exact match. */
1407 tem = comname + hook_name_size;
1408 hookc = lookup_cmd (&tem, *list, "", -1, 0);
1409 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1413 warning (_("Your new `%s' command does not hook any existing command."),
1415 if (!query (_("Proceed? ")))
1416 error (_("Not confirmed."));
1420 comname = xstrdup (comname);
1422 /* If the rest of the commands will be case insensitive, this one
1423 should behave in the same manner. */
1424 for (tem = comname; *tem; tem++)
1426 *tem = tolower (*tem);
1428 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
1429 cmds = read_command_lines (tmpbuf, from_tty, 1);
1431 if (c && c->class == class_user)
1432 free_command_lines (&c->user_commands);
1434 newc = add_cmd (comname, class_user, user_defined_command,
1435 (c && c->class == class_user)
1436 ? c->doc : xstrdup ("User-defined."), list);
1437 newc->user_commands = cmds;
1439 /* If this new command is a hook, then mark both commands as being
1446 hookc->hook_pre = newc; /* Target gets hooked. */
1447 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1450 hookc->hook_post = newc; /* Target gets hooked. */
1451 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1454 /* Should never come here as hookc would be 0. */
1455 internal_error (__FILE__, __LINE__, _("bad switch"));
1461 document_command (char *comname, int from_tty)
1463 struct command_line *doclines;
1464 struct cmd_list_element *c, **list;
1465 char *tem, *comfull;
1469 list = validate_comname (&comname);
1472 c = lookup_cmd (&tem, *list, "", 0, 1);
1474 if (c->class != class_user)
1475 error (_("Command \"%s\" is built-in."), comfull);
1477 sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
1478 doclines = read_command_lines (tmpbuf, from_tty, 0);
1484 struct command_line *cl1;
1487 for (cl1 = doclines; cl1; cl1 = cl1->next)
1488 len += strlen (cl1->line) + 1;
1490 c->doc = (char *) xmalloc (len + 1);
1493 for (cl1 = doclines; cl1; cl1 = cl1->next)
1495 strcat (c->doc, cl1->line);
1497 strcat (c->doc, "\n");
1501 free_command_lines (&doclines);
1504 struct source_cleanup_lines_args
1511 source_cleanup_lines (void *args)
1513 struct source_cleanup_lines_args *p =
1514 (struct source_cleanup_lines_args *) args;
1515 source_line_number = p->old_line;
1516 source_file_name = p->old_file;
1519 struct wrapped_read_command_file_args
1525 wrapped_read_command_file (struct ui_out *uiout, void *data)
1527 struct wrapped_read_command_file_args *args = data;
1528 read_command_file (args->stream);
1531 /* Used to implement source_command */
1534 script_from_file (FILE *stream, char *file)
1536 struct cleanup *old_cleanups;
1537 struct source_cleanup_lines_args old_lines;
1541 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1543 old_cleanups = make_cleanup_fclose (stream);
1545 old_lines.old_line = source_line_number;
1546 old_lines.old_file = source_file_name;
1547 make_cleanup (source_cleanup_lines, &old_lines);
1548 source_line_number = 0;
1549 source_file_name = file;
1550 /* This will get set every time we read a line. So it won't stay "" for
1552 error_pre_print = "";
1555 struct gdb_exception e;
1556 struct wrapped_read_command_file_args args;
1557 args.stream = stream;
1558 e = catch_exception (uiout, wrapped_read_command_file, &args,
1565 /* Re-throw the error, but with the file name information
1567 throw_error (e.error,
1568 _("%s:%d: Error in sourced command file:\n%s"),
1569 source_file_name, source_line_number, e.message);
1571 internal_error (__FILE__, __LINE__, _("bad reason"));
1575 do_cleanups (old_cleanups);
1578 /* Print the definition of user command C to STREAM. Or, if C is a
1579 prefix command, show the definitions of all user commands under C
1580 (recursively). PREFIX and NAME combined are the name of the
1583 show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1584 struct ui_file *stream)
1586 struct command_line *cmdlines;
1588 if (c->prefixlist != NULL)
1590 char *prefixname = c->prefixname;
1591 for (c = *c->prefixlist; c != NULL; c = c->next)
1592 if (c->class == class_user || c->prefixlist != NULL)
1593 show_user_1 (c, prefixname, c->name, gdb_stdout);
1597 cmdlines = c->user_commands;
1600 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1602 print_command_lines (uiout, cmdlines, 1);
1603 fputs_filtered ("\n", stream);