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 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 (struct command_line *current_cmd);
44 static char *insert_args (char *line);
46 static struct cleanup * setup_user_args (char *p);
48 static void validate_comname (char *);
50 /* Level of control structure when reading. */
51 static int control_level;
53 /* Level of control structure when executing. */
54 static int command_nest_depth = 1;
56 /* This is to prevent certain commands being printed twice. */
57 static int suppress_next_print_command_trace = 0;
59 /* Structure for arguments to user defined functions. */
60 #define MAXUSERARGS 10
63 struct user_args *next;
64 /* It is necessary to store a malloced copy of the command line to
65 ensure that the arguments are not overwritten before they are used. */
78 /* Allocate, initialize a new command line structure for one of the
79 control commands (if/while). */
81 static struct command_line *
82 build_command_line (enum command_control_type type, char *args)
84 struct command_line *cmd;
86 if (args == NULL && (type == if_control || type == while_control))
87 error (_("if/while commands require arguments."));
88 gdb_assert (args != NULL);
90 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
92 cmd->control_type = type;
96 = (struct command_line **) xmalloc (sizeof (struct command_line *)
98 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
99 cmd->line = savestring (args, strlen (args));
104 /* Build and return a new command structure for the control commands
105 such as "if" and "while". */
107 struct command_line *
108 get_command_line (enum command_control_type type, char *arg)
110 struct command_line *cmd;
111 struct cleanup *old_chain = NULL;
113 /* Allocate and build a new command line structure. */
114 cmd = build_command_line (type, arg);
116 old_chain = make_cleanup_free_command_lines (&cmd);
118 /* Read in the body of this command. */
119 if (recurse_read_control_structure (cmd) == invalid_control)
121 warning (_("Error reading in canned sequence of commands."));
122 do_cleanups (old_chain);
126 discard_cleanups (old_chain);
130 /* Recursively print a command (including full control structures). */
133 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
136 struct command_line *list;
143 ui_out_spaces (uiout, 2 * depth);
145 /* A simple command, print it and continue. */
146 if (list->control_type == simple_control)
148 ui_out_field_string (uiout, NULL, list->line);
149 ui_out_text (uiout, "\n");
154 /* loop_continue to jump to the start of a while loop, print it
156 if (list->control_type == continue_control)
158 ui_out_field_string (uiout, NULL, "loop_continue");
159 ui_out_text (uiout, "\n");
164 /* loop_break to break out of a while loop, print it and continue. */
165 if (list->control_type == break_control)
167 ui_out_field_string (uiout, NULL, "loop_break");
168 ui_out_text (uiout, "\n");
173 /* A while command. Recursively print its subcommands and continue. */
174 if (list->control_type == while_control)
176 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
177 ui_out_text (uiout, "\n");
178 print_command_lines (uiout, *list->body_list, depth + 1);
180 ui_out_spaces (uiout, 2 * depth);
181 ui_out_field_string (uiout, NULL, "end");
182 ui_out_text (uiout, "\n");
187 /* An if command. Recursively print both arms before continueing. */
188 if (list->control_type == if_control)
190 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
191 ui_out_text (uiout, "\n");
193 print_command_lines (uiout, list->body_list[0], depth + 1);
195 /* Show the false arm if it exists. */
196 if (list->body_count == 2)
199 ui_out_spaces (uiout, 2 * depth);
200 ui_out_field_string (uiout, NULL, "else");
201 ui_out_text (uiout, "\n");
202 print_command_lines (uiout, list->body_list[1], depth + 1);
206 ui_out_spaces (uiout, 2 * depth);
207 ui_out_field_string (uiout, NULL, "end");
208 ui_out_text (uiout, "\n");
213 /* A commands command. Print the breakpoint commands and continue. */
214 if (list->control_type == commands_control)
217 ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
219 ui_out_field_string (uiout, NULL, "commands");
220 ui_out_text (uiout, "\n");
221 print_command_lines (uiout, *list->body_list, depth + 1);
223 ui_out_spaces (uiout, 2 * depth);
224 ui_out_field_string (uiout, NULL, "end");
225 ui_out_text (uiout, "\n");
230 if (list->control_type == python_control)
232 ui_out_field_string (uiout, NULL, "python");
233 ui_out_text (uiout, "\n");
234 /* Don't indent python code at all. */
235 print_command_lines (uiout, *list->body_list, 0);
237 ui_out_spaces (uiout, 2 * depth);
238 ui_out_field_string (uiout, NULL, "end");
239 ui_out_text (uiout, "\n");
244 /* ignore illegal command type and try next */
249 /* Handle pre-post hooks. */
252 clear_hook_in_cleanup (void *data)
254 struct cmd_list_element *c = data;
255 c->hook_in = 0; /* Allow hook to work again once it is complete */
259 execute_cmd_pre_hook (struct cmd_list_element *c)
261 if ((c->hook_pre) && (!c->hook_in))
263 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
264 c->hook_in = 1; /* Prevent recursive hooking */
265 execute_user_command (c->hook_pre, (char *) 0);
266 do_cleanups (cleanups);
271 execute_cmd_post_hook (struct cmd_list_element *c)
273 if ((c->hook_post) && (!c->hook_in))
275 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
276 c->hook_in = 1; /* Prevent recursive hooking */
277 execute_user_command (c->hook_post, (char *) 0);
278 do_cleanups (cleanups);
282 /* Execute the command in CMD. */
284 do_restore_user_call_depth (void * call_depth)
286 int * depth = call_depth;
294 execute_user_command (struct cmd_list_element *c, char *args)
296 struct command_line *cmdlines;
297 struct cleanup *old_chain;
298 enum command_control_type ret;
299 static int user_call_depth = 0;
300 extern int max_user_call_depth;
302 old_chain = setup_user_args (args);
304 cmdlines = c->user_commands;
309 if (++user_call_depth > max_user_call_depth)
310 error (_("Max user call depth exceeded -- command aborted."));
312 make_cleanup (do_restore_user_call_depth, &user_call_depth);
314 /* Set the instream to 0, indicating execution of a
315 user-defined function. */
316 make_cleanup (do_restore_instream_cleanup, instream);
317 instream = (FILE *) 0;
319 /* Also set the global in_user_command, so that NULL instream is
320 not confused with Insight. */
323 command_nest_depth++;
326 ret = execute_control_command (cmdlines);
327 if (ret != simple_control && ret != break_control)
329 warning (_("Error executing canned sequence of commands."));
332 cmdlines = cmdlines->next;
334 command_nest_depth--;
335 do_cleanups (old_chain);
338 /* This function is called every time GDB prints a prompt.
339 It ensures that errors and the like to not confuse the command tracing. */
342 reset_command_nest_depth (void)
344 command_nest_depth = 1;
347 suppress_next_print_command_trace = 0;
350 /* Print the command, prefixed with '+' to represent the call depth.
351 This is slightly complicated because this function may be called
352 from execute_command and execute_control_command. Unfortunately
353 execute_command also prints the top level control commands.
354 In these cases execute_command will call execute_control_command
355 via while_command or if_command. Inner levels of 'if' and 'while'
356 are dealt with directly. Therefore we can use these functions
357 to determine whether the command has been printed already or not. */
359 print_command_trace (const char *cmd)
363 if (suppress_next_print_command_trace)
365 suppress_next_print_command_trace = 0;
369 if (!source_verbose && !trace_commands)
372 for (i=0; i < command_nest_depth; i++)
373 printf_filtered ("+");
375 printf_filtered ("%s\n", cmd);
378 enum command_control_type
379 execute_control_command (struct command_line *cmd)
381 struct expression *expr;
382 struct command_line *current;
383 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
385 struct value *val_mark;
387 enum command_control_type ret;
390 /* Start by assuming failure, if a problem is detected, the code
391 below will simply "break" out of the switch. */
392 ret = invalid_control;
394 switch (cmd->control_type)
397 /* A simple command, execute it and return. */
398 new_line = insert_args (cmd->line);
401 make_cleanup (free_current_contents, &new_line);
402 execute_command (new_line, 0);
403 ret = cmd->control_type;
406 case continue_control:
407 print_command_trace ("loop_continue");
409 /* Return for "continue", and "break" so we can either
410 continue the loop at the top, or break out. */
411 ret = cmd->control_type;
415 print_command_trace ("loop_break");
417 /* Return for "continue", and "break" so we can either
418 continue the loop at the top, or break out. */
419 ret = cmd->control_type;
424 char *buffer = alloca (strlen (cmd->line) + 7);
425 sprintf (buffer, "while %s", cmd->line);
426 print_command_trace (buffer);
428 /* Parse the loop control expression for the while statement. */
429 new_line = insert_args (cmd->line);
432 make_cleanup (free_current_contents, &new_line);
433 expr = parse_expression (new_line);
434 make_cleanup (free_current_contents, &expr);
436 ret = simple_control;
439 /* Keep iterating so long as the expression is true. */
446 /* Evaluate the expression. */
447 val_mark = value_mark ();
448 val = evaluate_expression (expr);
449 cond_result = value_true (val);
450 value_free_to_mark (val_mark);
452 /* If the value is false, then break out of the loop. */
456 /* Execute the body of the while statement. */
457 current = *cmd->body_list;
460 command_nest_depth++;
461 ret = execute_control_command (current);
462 command_nest_depth--;
464 /* If we got an error, or a "break" command, then stop
466 if (ret == invalid_control || ret == break_control)
472 /* If we got a "continue" command, then restart the loop
474 if (ret == continue_control)
477 /* Get the next statement. */
478 current = current->next;
482 /* Reset RET so that we don't recurse the break all the way down. */
483 if (ret == break_control)
484 ret = simple_control;
491 char *buffer = alloca (strlen (cmd->line) + 4);
492 sprintf (buffer, "if %s", cmd->line);
493 print_command_trace (buffer);
495 new_line = insert_args (cmd->line);
498 make_cleanup (free_current_contents, &new_line);
499 /* Parse the conditional for the if statement. */
500 expr = parse_expression (new_line);
501 make_cleanup (free_current_contents, &expr);
504 ret = simple_control;
506 /* Evaluate the conditional. */
507 val_mark = value_mark ();
508 val = evaluate_expression (expr);
510 /* Choose which arm to take commands from based on the value of the
511 conditional expression. */
512 if (value_true (val))
513 current = *cmd->body_list;
514 else if (cmd->body_count == 2)
515 current = *(cmd->body_list + 1);
516 value_free_to_mark (val_mark);
518 /* Execute commands in the given arm. */
521 command_nest_depth++;
522 ret = execute_control_command (current);
523 command_nest_depth--;
525 /* If we got an error, get out. */
526 if (ret != simple_control)
529 /* Get the next statement in the body. */
530 current = current->next;
535 case commands_control:
537 /* Breakpoint commands list, record the commands in the breakpoint's
538 command list and return. */
539 new_line = insert_args (cmd->line);
542 make_cleanup (free_current_contents, &new_line);
543 ret = commands_from_control_command (new_line, cmd);
548 eval_python_from_control_command (cmd);
549 ret = simple_control;
554 warning (_("Invalid control type in canned commands structure."));
558 do_cleanups (old_chain);
563 /* Like execute_control_command, but first set
564 suppress_next_print_command_trace. */
566 enum command_control_type
567 execute_control_command_untraced (struct command_line *cmd)
569 suppress_next_print_command_trace = 1;
570 return execute_control_command (cmd);
574 /* "while" command support. Executes a body of statements while the
575 loop condition is nonzero. */
578 while_command (char *arg, int from_tty)
580 struct command_line *command = NULL;
583 command = get_command_line (while_control, arg);
588 execute_control_command_untraced (command);
589 free_command_lines (&command);
592 /* "if" command support. Execute either the true or false arm depending
593 on the value of the if conditional. */
596 if_command (char *arg, int from_tty)
598 struct command_line *command = NULL;
601 command = get_command_line (if_control, arg);
606 execute_control_command_untraced (command);
607 free_command_lines (&command);
612 arg_cleanup (void *ignore)
614 struct user_args *oargs = user_args;
616 internal_error (__FILE__, __LINE__,
617 _("arg_cleanup called with no user args.\n"));
619 user_args = user_args->next;
620 xfree (oargs->command);
624 /* Bind the incomming arguments for a user defined command to
625 $arg0, $arg1 ... $argMAXUSERARGS. */
627 static struct cleanup *
628 setup_user_args (char *p)
630 struct user_args *args;
631 struct cleanup *old_chain;
632 unsigned int arg_count = 0;
634 args = (struct user_args *) xmalloc (sizeof (struct user_args));
635 memset (args, 0, sizeof (struct user_args));
637 args->next = user_args;
640 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
645 user_args->command = p = xstrdup (p);
654 if (arg_count >= MAXUSERARGS)
656 error (_("user defined function may only have %d arguments."),
661 /* Strip whitespace. */
662 while (*p == ' ' || *p == '\t')
665 /* P now points to an argument. */
667 user_args->a[arg_count].arg = p;
669 /* Get to the end of this argument. */
672 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
701 user_args->a[arg_count].len = p - start_arg;
708 /* Given character string P, return a point to the first argument ($arg),
709 or NULL if P contains no arguments. */
714 while ((p = strchr (p, '$')))
716 if (strncmp (p, "$arg", 4) == 0
717 && (isdigit (p[4]) || p[4] == 'c'))
724 /* Insert the user defined arguments stored in user_arg into the $arg
725 arguments found in line, with the updated copy being placed into nline. */
728 insert_args (char *line)
730 char *p, *save_line, *new_line;
733 /* If we are not in a user-defined function, treat $argc, $arg0, et
734 cetera as normal convenience variables. */
735 if (user_args == NULL)
736 return xstrdup (line);
738 /* First we need to know how much memory to allocate for the new line. */
741 while ((p = locate_arg (line)))
748 /* $argc. Number will be <=10. */
749 len += user_args->count == 10 ? 2 : 1;
751 else if (i >= user_args->count)
753 error (_("Missing argument %d in user function."), i);
758 len += user_args->a[i].len;
763 /* Don't forget the tail. */
764 len += strlen (line);
766 /* Allocate space for the new line and fill it in. */
767 new_line = (char *) xmalloc (len + 1);
768 if (new_line == NULL)
771 /* Restore pointer to beginning of old line. */
774 /* Save pointer to beginning of new line. */
775 save_line = new_line;
777 while ((p = locate_arg (line)))
781 memcpy (new_line, line, p - line);
782 new_line += p - line;
786 gdb_assert (user_args->count >= 0 && user_args->count <= 10);
787 if (user_args->count == 10)
793 *(new_line++) = user_args->count + '0';
798 len = user_args->a[i].len;
801 memcpy (new_line, user_args->a[i].arg, len);
807 /* Don't forget the tail. */
808 strcpy (new_line, line);
810 /* Return a pointer to the beginning of the new line. */
815 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
816 code bodies. This is typically used when we encounter an "else"
817 clause for an "if" command. */
820 realloc_body_list (struct command_line *command, int new_length)
823 struct command_line **body_list;
825 n = command->body_count;
831 body_list = (struct command_line **)
832 xmalloc (sizeof (struct command_line *) * new_length);
834 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
835 memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
837 xfree (command->body_list);
838 command->body_list = body_list;
839 command->body_count = new_length;
842 /* Read one line from the input stream. If the command is an "else" or
843 "end", return such an indication to the caller. */
845 static enum misc_command_type
846 read_next_line (struct command_line **command)
848 char *p, *p1, *prompt_ptr, control_prompt[256];
851 if (control_level >= 254)
852 error (_("Control nesting too deep!"));
854 /* Set a prompt based on the nesting of the control commands. */
855 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
857 for (i = 0; i < control_level; i++)
858 control_prompt[i] = ' ';
859 control_prompt[i] = '>';
860 control_prompt[i + 1] = '\0';
861 prompt_ptr = (char *) &control_prompt[0];
866 p = command_line_input (prompt_ptr, instream == stdin, "commands");
868 /* Not sure what to do here. */
872 /* Strip leading and trailing whitespace. */
873 while (*p == ' ' || *p == '\t')
877 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
880 /* Blanks and comments don't really do anything, but we need to
881 distinguish them from else, end and other commands which can be
883 if (p1 == p || p[0] == '#')
886 /* Is this the end of a simple, while, or if control structure? */
887 if (p1 - p == 3 && !strncmp (p, "end", 3))
890 /* Is the else clause of an if control structure? */
891 if (p1 - p == 4 && !strncmp (p, "else", 4))
894 /* Check for while, if, break, continue, etc and build a new command
895 line structure for them. */
896 if (p1 - p > 5 && !strncmp (p, "while", 5))
900 while (first_arg < p1 && isspace (*first_arg))
902 *command = build_command_line (while_control, first_arg);
904 else if (p1 - p > 2 && !strncmp (p, "if", 2))
908 while (first_arg < p1 && isspace (*first_arg))
910 *command = build_command_line (if_control, first_arg);
912 else if (p1 - p >= 8 && !strncmp (p, "commands", 8))
916 while (first_arg < p1 && isspace (*first_arg))
918 *command = build_command_line (commands_control, first_arg);
920 else if (p1 - p == 6 && !strncmp (p, "python", 6))
922 /* Note that we ignore the inline "python command" form
924 *command = build_command_line (python_control, "");
926 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
928 *command = (struct command_line *)
929 xmalloc (sizeof (struct command_line));
930 (*command)->next = NULL;
931 (*command)->line = NULL;
932 (*command)->control_type = break_control;
933 (*command)->body_count = 0;
934 (*command)->body_list = NULL;
936 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
938 *command = (struct command_line *)
939 xmalloc (sizeof (struct command_line));
940 (*command)->next = NULL;
941 (*command)->line = NULL;
942 (*command)->control_type = continue_control;
943 (*command)->body_count = 0;
944 (*command)->body_list = NULL;
948 /* A normal command. */
949 *command = (struct command_line *)
950 xmalloc (sizeof (struct command_line));
951 (*command)->next = NULL;
952 (*command)->line = savestring (p, p1 - p);
953 (*command)->control_type = simple_control;
954 (*command)->body_count = 0;
955 (*command)->body_list = NULL;
958 /* Nothing special. */
962 /* Recursively read in the control structures and create a command_line
965 The parent_control parameter is the control structure in which the
966 following commands are nested. */
968 static enum command_control_type
969 recurse_read_control_structure (struct command_line *current_cmd)
972 enum misc_command_type val;
973 enum command_control_type ret;
974 struct command_line **body_ptr, *child_tail, *next;
980 if (current_cmd->control_type == simple_control)
981 error (_("Recursed on a simple control type."));
983 if (current_body > current_cmd->body_count)
984 error (_("Allocated body is smaller than this command type needs."));
986 /* Read lines from the input stream and build control structures. */
992 val = read_next_line (&next);
994 /* Just skip blanks and comments. */
995 if (val == nop_command)
998 if (val == end_command)
1000 if (current_cmd->control_type == while_control
1001 || current_cmd->control_type == if_control
1002 || current_cmd->control_type == python_control
1003 || current_cmd->control_type == commands_control)
1005 /* Success reading an entire canned sequence of commands. */
1006 ret = simple_control;
1011 ret = invalid_control;
1016 /* Not the end of a control structure. */
1017 if (val == else_command)
1019 if (current_cmd->control_type == if_control
1020 && current_body == 1)
1022 realloc_body_list (current_cmd, 2);
1029 ret = invalid_control;
1036 child_tail->next = next;
1040 body_ptr = current_cmd->body_list;
1041 for (i = 1; i < current_body; i++)
1050 /* If the latest line is another control structure, then recurse
1052 if (next->control_type == while_control
1053 || next->control_type == if_control
1054 || next->control_type == python_control
1055 || next->control_type == commands_control)
1058 ret = recurse_read_control_structure (next);
1061 if (ret != simple_control)
1071 /* Read lines from the input stream and accumulate them in a chain of
1072 struct command_line's, which is then returned. For input from a
1073 terminal, the special command "end" is used to mark the end of the
1074 input, and is not included in the returned chain of commands. */
1076 #define END_MESSAGE "End with a line saying just \"end\"."
1078 struct command_line *
1079 read_command_lines (char *prompt_arg, int from_tty)
1081 struct command_line *head, *tail, *next;
1082 struct cleanup *old_chain;
1083 enum command_control_type ret;
1084 enum misc_command_type val;
1088 if (from_tty && input_from_terminal_p ())
1090 if (deprecated_readline_begin_hook)
1092 /* Note - intentional to merge messages with no newline */
1093 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1097 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1098 gdb_flush (gdb_stdout);
1108 val = read_next_line (&next);
1110 /* Ignore blank lines or comments. */
1111 if (val == nop_command)
1114 if (val == end_command)
1116 ret = simple_control;
1120 if (val != ok_command)
1122 ret = invalid_control;
1126 if (next->control_type == while_control
1127 || next->control_type == if_control
1128 || next->control_type == python_control
1129 || next->control_type == commands_control)
1132 ret = recurse_read_control_structure (next);
1135 if (ret == invalid_control)
1146 old_chain = make_cleanup_free_command_lines (&head);
1155 if (ret != invalid_control)
1157 discard_cleanups (old_chain);
1160 do_cleanups (old_chain);
1163 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1165 (*deprecated_readline_end_hook) ();
1170 /* Free a chain of struct command_line's. */
1173 free_command_lines (struct command_line **lptr)
1175 struct command_line *l = *lptr;
1176 struct command_line *next;
1177 struct command_line **blist;
1182 if (l->body_count > 0)
1184 blist = l->body_list;
1185 for (i = 0; i < l->body_count; i++, blist++)
1186 free_command_lines (blist);
1197 do_free_command_lines_cleanup (void *arg)
1199 free_command_lines (arg);
1203 make_cleanup_free_command_lines (struct command_line **arg)
1205 return make_cleanup (do_free_command_lines_cleanup, arg);
1208 struct command_line *
1209 copy_command_lines (struct command_line *cmds)
1211 struct command_line *result = NULL;
1215 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1217 result->next = copy_command_lines (cmds->next);
1218 result->line = xstrdup (cmds->line);
1219 result->control_type = cmds->control_type;
1220 result->body_count = cmds->body_count;
1221 if (cmds->body_count > 0)
1225 result->body_list = (struct command_line **)
1226 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1228 for (i = 0; i < cmds->body_count; i++)
1229 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1232 result->body_list = NULL;
1239 validate_comname (char *comname)
1244 error_no_arg (_("name of command to define"));
1249 if (!isalnum (*p) && *p != '-' && *p != '_')
1250 error (_("Junk in argument list: \"%s\""), p);
1255 /* This is just a placeholder in the command data structures. */
1257 user_defined_command (char *ignore, int from_tty)
1262 define_command (char *comname, int from_tty)
1264 #define MAX_TMPBUF 128
1271 struct command_line *cmds;
1272 struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1273 char *tem = comname;
1275 char tmpbuf[MAX_TMPBUF];
1276 int hook_type = CMD_NO_HOOK;
1277 int hook_name_size = 0;
1279 #define HOOK_STRING "hook-"
1281 #define HOOK_POST_STRING "hookpost-"
1282 #define HOOK_POST_LEN 9
1284 validate_comname (comname);
1286 /* Look it up, and verify that we got an exact match. */
1287 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1288 if (c && strcmp (comname, c->name) != 0)
1294 if (c->class == class_user || c->class == class_alias)
1295 q = query (_("Redefine command \"%s\"? "), c->name);
1297 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1299 error (_("Command \"%s\" not redefined."), c->name);
1302 /* If this new command is a hook, then mark the command which it
1303 is hooking. Note that we allow hooking `help' commands, so that
1304 we can hook the `stop' pseudo-command. */
1306 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1308 hook_type = CMD_PRE_HOOK;
1309 hook_name_size = HOOK_LEN;
1311 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1313 hook_type = CMD_POST_HOOK;
1314 hook_name_size = HOOK_POST_LEN;
1317 if (hook_type != CMD_NO_HOOK)
1319 /* Look up cmd it hooks, and verify that we got an exact match. */
1320 tem = comname + hook_name_size;
1321 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1322 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1326 warning (_("Your new `%s' command does not hook any existing command."),
1328 if (!query ("Proceed? "))
1329 error (_("Not confirmed."));
1333 comname = savestring (comname, strlen (comname));
1335 /* If the rest of the commands will be case insensitive, this one
1336 should behave in the same manner. */
1337 for (tem = comname; *tem; tem++)
1339 *tem = tolower (*tem);
1341 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1342 cmds = read_command_lines (tmpbuf, from_tty);
1344 if (c && c->class == class_user)
1345 free_command_lines (&c->user_commands);
1347 newc = add_cmd (comname, class_user, user_defined_command,
1348 (c && c->class == class_user)
1349 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1350 newc->user_commands = cmds;
1352 /* If this new command is a hook, then mark both commands as being
1359 hookc->hook_pre = newc; /* Target gets hooked. */
1360 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1363 hookc->hook_post = newc; /* Target gets hooked. */
1364 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1367 /* Should never come here as hookc would be 0. */
1368 internal_error (__FILE__, __LINE__, _("bad switch"));
1374 document_command (char *comname, int from_tty)
1376 struct command_line *doclines;
1377 struct cmd_list_element *c;
1378 char *tem = comname;
1381 validate_comname (comname);
1383 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1385 if (c->class != class_user)
1386 error (_("Command \"%s\" is built-in."), comname);
1388 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1389 doclines = read_command_lines (tmpbuf, from_tty);
1395 struct command_line *cl1;
1398 for (cl1 = doclines; cl1; cl1 = cl1->next)
1399 len += strlen (cl1->line) + 1;
1401 c->doc = (char *) xmalloc (len + 1);
1404 for (cl1 = doclines; cl1; cl1 = cl1->next)
1406 strcat (c->doc, cl1->line);
1408 strcat (c->doc, "\n");
1412 free_command_lines (&doclines);
1415 struct source_cleanup_lines_args
1422 source_cleanup_lines (void *args)
1424 struct source_cleanup_lines_args *p =
1425 (struct source_cleanup_lines_args *) args;
1426 source_line_number = p->old_line;
1427 source_file_name = p->old_file;
1431 do_fclose_cleanup (void *stream)
1436 struct wrapped_read_command_file_args
1442 wrapped_read_command_file (struct ui_out *uiout, void *data)
1444 struct wrapped_read_command_file_args *args = data;
1445 read_command_file (args->stream);
1448 /* Used to implement source_command */
1451 script_from_file (FILE *stream, char *file)
1453 struct cleanup *old_cleanups;
1454 struct source_cleanup_lines_args old_lines;
1458 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1460 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1462 old_lines.old_line = source_line_number;
1463 old_lines.old_file = source_file_name;
1464 make_cleanup (source_cleanup_lines, &old_lines);
1465 source_line_number = 0;
1466 source_file_name = file;
1467 /* This will get set every time we read a line. So it won't stay "" for
1469 error_pre_print = "";
1472 struct gdb_exception e;
1473 struct wrapped_read_command_file_args args;
1474 args.stream = stream;
1475 e = catch_exception (uiout, wrapped_read_command_file, &args,
1482 /* Re-throw the error, but with the file name information
1484 throw_error (e.error,
1485 _("%s:%d: Error in sourced command file:\n%s"),
1486 source_file_name, source_line_number, e.message);
1488 internal_error (__FILE__, __LINE__, _("bad reason"));
1492 do_cleanups (old_cleanups);
1496 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1498 struct command_line *cmdlines;
1500 cmdlines = c->user_commands;
1503 fputs_filtered ("User command ", stream);
1504 fputs_filtered (c->name, stream);
1505 fputs_filtered (":\n", stream);
1507 print_command_lines (uiout, cmdlines, 1);
1508 fputs_filtered ("\n", stream);