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)
884 /* Not sure what to do here. */
890 /* Strip leading whitespace. */
891 while (*p == ' ' || *p == '\t')
895 /* Strip trailing whitespace. */
897 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
900 /* Is this the end of a simple, while, or if control structure? */
901 if (p1 - p == 3 && !strncmp (p, "end", 3))
906 /* Blanks and comments don't really do anything, but we need to
907 distinguish them from else, end and other commands which can be
909 if (p1 == p || p[0] == '#')
912 /* Is the else clause of an if control structure? */
913 if (p1 - p == 4 && !strncmp (p, "else", 4))
916 /* Check for while, if, break, continue, etc and build a new command
917 line structure for them. */
918 if (p1 - p > 5 && !strncmp (p, "while", 5))
922 while (first_arg < p1 && isspace (*first_arg))
924 *command = build_command_line (while_control, first_arg);
926 else if (p1 - p > 2 && !strncmp (p, "if", 2))
930 while (first_arg < p1 && isspace (*first_arg))
932 *command = build_command_line (if_control, first_arg);
934 else if (p1 - p >= 8 && !strncmp (p, "commands", 8))
938 while (first_arg < p1 && isspace (*first_arg))
940 *command = build_command_line (commands_control, first_arg);
942 else if (p1 - p == 6 && !strncmp (p, "python", 6))
944 /* Note that we ignore the inline "python command" form
946 *command = build_command_line (python_control, "");
948 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
950 *command = (struct command_line *)
951 xmalloc (sizeof (struct command_line));
952 (*command)->next = NULL;
953 (*command)->line = NULL;
954 (*command)->control_type = break_control;
955 (*command)->body_count = 0;
956 (*command)->body_list = NULL;
958 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
960 *command = (struct command_line *)
961 xmalloc (sizeof (struct command_line));
962 (*command)->next = NULL;
963 (*command)->line = NULL;
964 (*command)->control_type = continue_control;
965 (*command)->body_count = 0;
966 (*command)->body_list = NULL;
972 if (!parse_commands || not_handled)
974 /* A normal command. */
975 *command = (struct command_line *)
976 xmalloc (sizeof (struct command_line));
977 (*command)->next = NULL;
978 (*command)->line = savestring (p, p1 - p);
979 (*command)->control_type = simple_control;
980 (*command)->body_count = 0;
981 (*command)->body_list = NULL;
984 /* Nothing special. */
988 /* Recursively read in the control structures and create a command_line
989 structure from them. Use read_next_line_func to obtain lines of
994 static enum command_control_type
995 recurse_read_control_structure (char * (*read_next_line_func) (),
996 struct command_line *current_cmd)
999 enum misc_command_type val;
1000 enum command_control_type ret;
1001 struct command_line **body_ptr, *child_tail, *next;
1007 /* Sanity checks. */
1008 if (current_cmd->control_type == simple_control)
1009 error (_("Recursed on a simple control type."));
1011 if (current_body > current_cmd->body_count)
1012 error (_("Allocated body is smaller than this command type needs."));
1014 /* Read lines from the input stream and build control structures. */
1020 val = process_next_line (read_next_line_func (), &next,
1021 current_cmd->control_type != python_control);
1023 /* Just skip blanks and comments. */
1024 if (val == nop_command)
1027 if (val == end_command)
1029 if (current_cmd->control_type == while_control
1030 || current_cmd->control_type == if_control
1031 || current_cmd->control_type == python_control
1032 || current_cmd->control_type == commands_control)
1034 /* Success reading an entire canned sequence of commands. */
1035 ret = simple_control;
1040 ret = invalid_control;
1045 /* Not the end of a control structure. */
1046 if (val == else_command)
1048 if (current_cmd->control_type == if_control
1049 && current_body == 1)
1051 realloc_body_list (current_cmd, 2);
1058 ret = invalid_control;
1065 child_tail->next = next;
1069 body_ptr = current_cmd->body_list;
1070 for (i = 1; i < current_body; i++)
1079 /* If the latest line is another control structure, then recurse
1081 if (next->control_type == while_control
1082 || next->control_type == if_control
1083 || next->control_type == python_control
1084 || next->control_type == commands_control)
1087 ret = recurse_read_control_structure (read_next_line_func, next);
1090 if (ret != simple_control)
1100 /* Read lines from the input stream and accumulate them in a chain of
1101 struct command_line's, which is then returned. For input from a
1102 terminal, the special command "end" is used to mark the end of the
1103 input, and is not included in the returned chain of commands.
1105 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1106 is always stripped) in the line and attempt to recognize GDB control
1107 commands. Otherwise, only "end" is recognized. */
1109 #define END_MESSAGE "End with a line saying just \"end\"."
1111 struct command_line *
1112 read_command_lines (char *prompt_arg, int from_tty, int parse_commands)
1114 struct command_line *head;
1116 if (from_tty && input_from_terminal_p ())
1118 if (deprecated_readline_begin_hook)
1120 /* Note - intentional to merge messages with no newline */
1121 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1125 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1126 gdb_flush (gdb_stdout);
1130 head = read_command_lines_1 (read_next_line, parse_commands);
1132 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1134 (*deprecated_readline_end_hook) ();
1139 /* Act the same way as read_command_lines, except that each new line is
1140 obtained using READ_NEXT_LINE_FUNC. */
1142 struct command_line *
1143 read_command_lines_1 (char * (*read_next_line_func) (), int parse_commands)
1145 struct command_line *head, *tail, *next;
1146 struct cleanup *old_chain;
1147 enum command_control_type ret;
1148 enum misc_command_type val;
1157 val = process_next_line (read_next_line_func (), &next, parse_commands);
1159 /* Ignore blank lines or comments. */
1160 if (val == nop_command)
1163 if (val == end_command)
1165 ret = simple_control;
1169 if (val != ok_command)
1171 ret = invalid_control;
1175 if (next->control_type == while_control
1176 || next->control_type == if_control
1177 || next->control_type == python_control
1178 || next->control_type == commands_control)
1181 ret = recurse_read_control_structure (read_next_line_func, next);
1184 if (ret == invalid_control)
1195 old_chain = make_cleanup_free_command_lines (&head);
1204 if (ret != invalid_control)
1206 discard_cleanups (old_chain);
1209 do_cleanups (old_chain);
1215 /* Free a chain of struct command_line's. */
1218 free_command_lines (struct command_line **lptr)
1220 struct command_line *l = *lptr;
1221 struct command_line *next;
1222 struct command_line **blist;
1227 if (l->body_count > 0)
1229 blist = l->body_list;
1230 for (i = 0; i < l->body_count; i++, blist++)
1231 free_command_lines (blist);
1242 do_free_command_lines_cleanup (void *arg)
1244 free_command_lines (arg);
1248 make_cleanup_free_command_lines (struct command_line **arg)
1250 return make_cleanup (do_free_command_lines_cleanup, arg);
1253 struct command_line *
1254 copy_command_lines (struct command_line *cmds)
1256 struct command_line *result = NULL;
1260 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1262 result->next = copy_command_lines (cmds->next);
1263 result->line = xstrdup (cmds->line);
1264 result->control_type = cmds->control_type;
1265 result->body_count = cmds->body_count;
1266 if (cmds->body_count > 0)
1270 result->body_list = (struct command_line **)
1271 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1273 for (i = 0; i < cmds->body_count; i++)
1274 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1277 result->body_list = NULL;
1283 /* Validate that *COMNAME is a valid name for a command. Return the
1284 containing command list, in case it starts with a prefix command.
1285 The prefix must already exist. *COMNAME is advanced to point after
1286 any prefix, and a NUL character overwrites the space after the
1289 static struct cmd_list_element **
1290 validate_comname (char **comname)
1292 struct cmd_list_element **list = &cmdlist;
1293 char *p, *last_word;
1296 error_no_arg (_("name of command to define"));
1298 /* Find the last word of the argument. */
1299 p = *comname + strlen (*comname);
1300 while (p > *comname && isspace (p[-1]))
1302 while (p > *comname && !isspace (p[-1]))
1306 /* Find the corresponding command list. */
1307 if (last_word != *comname)
1309 struct cmd_list_element *c;
1310 char saved_char, *tem = *comname;
1312 /* Separate the prefix and the command. */
1313 saved_char = last_word[-1];
1314 last_word[-1] = '\0';
1316 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1317 if (c->prefixlist == NULL)
1318 error (_("\"%s\" is not a prefix command."), *comname);
1320 list = c->prefixlist;
1321 last_word[-1] = saved_char;
1322 *comname = last_word;
1328 if (!isalnum (*p) && *p != '-' && *p != '_')
1329 error (_("Junk in argument list: \"%s\""), p);
1336 /* This is just a placeholder in the command data structures. */
1338 user_defined_command (char *ignore, int from_tty)
1343 define_command (char *comname, int from_tty)
1345 #define MAX_TMPBUF 128
1352 struct command_line *cmds;
1353 struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
1354 char *tem, *tem2, *comfull;
1355 char tmpbuf[MAX_TMPBUF];
1356 int hook_type = CMD_NO_HOOK;
1357 int hook_name_size = 0;
1359 #define HOOK_STRING "hook-"
1361 #define HOOK_POST_STRING "hookpost-"
1362 #define HOOK_POST_LEN 9
1365 list = validate_comname (&comname);
1367 /* Look it up, and verify that we got an exact match. */
1369 c = lookup_cmd (&tem, *list, "", -1, 1);
1370 if (c && strcmp (comname, c->name) != 0)
1376 if (c->class == class_user || c->class == class_alias)
1377 q = query (_("Redefine command \"%s\"? "), c->name);
1379 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1381 error (_("Command \"%s\" not redefined."), c->name);
1384 /* If this new command is a hook, then mark the command which it
1385 is hooking. Note that we allow hooking `help' commands, so that
1386 we can hook the `stop' pseudo-command. */
1388 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1390 hook_type = CMD_PRE_HOOK;
1391 hook_name_size = HOOK_LEN;
1393 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1395 hook_type = CMD_POST_HOOK;
1396 hook_name_size = HOOK_POST_LEN;
1399 if (hook_type != CMD_NO_HOOK)
1401 /* Look up cmd it hooks, and verify that we got an exact match. */
1402 tem = comname + hook_name_size;
1403 hookc = lookup_cmd (&tem, *list, "", -1, 0);
1404 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1408 warning (_("Your new `%s' command does not hook any existing command."),
1410 if (!query (_("Proceed? ")))
1411 error (_("Not confirmed."));
1415 comname = xstrdup (comname);
1417 /* If the rest of the commands will be case insensitive, this one
1418 should behave in the same manner. */
1419 for (tem = comname; *tem; tem++)
1421 *tem = tolower (*tem);
1423 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
1424 cmds = read_command_lines (tmpbuf, from_tty, 1);
1426 if (c && c->class == class_user)
1427 free_command_lines (&c->user_commands);
1429 newc = add_cmd (comname, class_user, user_defined_command,
1430 (c && c->class == class_user)
1431 ? c->doc : xstrdup ("User-defined."), list);
1432 newc->user_commands = cmds;
1434 /* If this new command is a hook, then mark both commands as being
1441 hookc->hook_pre = newc; /* Target gets hooked. */
1442 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1445 hookc->hook_post = newc; /* Target gets hooked. */
1446 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1449 /* Should never come here as hookc would be 0. */
1450 internal_error (__FILE__, __LINE__, _("bad switch"));
1456 document_command (char *comname, int from_tty)
1458 struct command_line *doclines;
1459 struct cmd_list_element *c, **list;
1460 char *tem, *comfull;
1464 list = validate_comname (&comname);
1467 c = lookup_cmd (&tem, *list, "", 0, 1);
1469 if (c->class != class_user)
1470 error (_("Command \"%s\" is built-in."), comfull);
1472 sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
1473 doclines = read_command_lines (tmpbuf, from_tty, 0);
1479 struct command_line *cl1;
1482 for (cl1 = doclines; cl1; cl1 = cl1->next)
1483 len += strlen (cl1->line) + 1;
1485 c->doc = (char *) xmalloc (len + 1);
1488 for (cl1 = doclines; cl1; cl1 = cl1->next)
1490 strcat (c->doc, cl1->line);
1492 strcat (c->doc, "\n");
1496 free_command_lines (&doclines);
1499 struct source_cleanup_lines_args
1506 source_cleanup_lines (void *args)
1508 struct source_cleanup_lines_args *p =
1509 (struct source_cleanup_lines_args *) args;
1510 source_line_number = p->old_line;
1511 source_file_name = p->old_file;
1514 struct wrapped_read_command_file_args
1520 wrapped_read_command_file (struct ui_out *uiout, void *data)
1522 struct wrapped_read_command_file_args *args = data;
1523 read_command_file (args->stream);
1526 /* Used to implement source_command */
1529 script_from_file (FILE *stream, char *file)
1531 struct cleanup *old_cleanups;
1532 struct source_cleanup_lines_args old_lines;
1536 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1538 old_cleanups = make_cleanup_fclose (stream);
1540 old_lines.old_line = source_line_number;
1541 old_lines.old_file = source_file_name;
1542 make_cleanup (source_cleanup_lines, &old_lines);
1543 source_line_number = 0;
1544 source_file_name = file;
1545 /* This will get set every time we read a line. So it won't stay "" for
1547 error_pre_print = "";
1550 struct gdb_exception e;
1551 struct wrapped_read_command_file_args args;
1552 args.stream = stream;
1553 e = catch_exception (uiout, wrapped_read_command_file, &args,
1560 /* Re-throw the error, but with the file name information
1562 throw_error (e.error,
1563 _("%s:%d: Error in sourced command file:\n%s"),
1564 source_file_name, source_line_number, e.message);
1566 internal_error (__FILE__, __LINE__, _("bad reason"));
1570 do_cleanups (old_cleanups);
1573 /* Print the definition of user command C to STREAM. Or, if C is a
1574 prefix command, show the definitions of all user commands under C
1575 (recursively). PREFIX and NAME combined are the name of the
1578 show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1579 struct ui_file *stream)
1581 struct command_line *cmdlines;
1583 if (c->prefixlist != NULL)
1585 char *prefixname = c->prefixname;
1586 for (c = *c->prefixlist; c != NULL; c = c->next)
1587 if (c->class == class_user || c->prefixlist != NULL)
1588 show_user_1 (c, prefixname, c->name, gdb_stdout);
1592 cmdlines = c->user_commands;
1595 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1597 print_command_lines (uiout, cmdlines, 1);
1598 fputs_filtered ("\n", stream);