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
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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
26 #include "language.h" /* For value_true */
30 #include "gdb_string.h"
31 #include "exceptions.h"
33 #include "breakpoint.h"
34 #include "cli/cli-cmds.h"
35 #include "cli/cli-decode.h"
36 #include "cli/cli-script.h"
37 #include "gdb_assert.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 static 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 /* ignore illegal command type and try next */
235 /* Handle pre-post hooks. */
238 clear_hook_in_cleanup (void *data)
240 struct cmd_list_element *c = data;
241 c->hook_in = 0; /* Allow hook to work again once it is complete */
245 execute_cmd_pre_hook (struct cmd_list_element *c)
247 if ((c->hook_pre) && (!c->hook_in))
249 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
250 c->hook_in = 1; /* Prevent recursive hooking */
251 execute_user_command (c->hook_pre, (char *) 0);
252 do_cleanups (cleanups);
257 execute_cmd_post_hook (struct cmd_list_element *c)
259 if ((c->hook_post) && (!c->hook_in))
261 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
262 c->hook_in = 1; /* Prevent recursive hooking */
263 execute_user_command (c->hook_post, (char *) 0);
264 do_cleanups (cleanups);
268 /* Execute the command in CMD. */
270 do_restore_user_call_depth (void * call_depth)
272 int * depth = call_depth;
280 execute_user_command (struct cmd_list_element *c, char *args)
282 struct command_line *cmdlines;
283 struct cleanup *old_chain;
284 enum command_control_type ret;
285 static int user_call_depth = 0;
286 extern int max_user_call_depth;
288 old_chain = setup_user_args (args);
290 cmdlines = c->user_commands;
295 if (++user_call_depth > max_user_call_depth)
296 error (_("Max user call depth exceeded -- command aborted."));
298 make_cleanup (do_restore_user_call_depth, &user_call_depth);
300 /* Set the instream to 0, indicating execution of a
301 user-defined function. */
302 make_cleanup (do_restore_instream_cleanup, instream);
303 instream = (FILE *) 0;
305 /* Also set the global in_user_command, so that NULL instream is
306 not confused with Insight. */
309 command_nest_depth++;
312 ret = execute_control_command (cmdlines);
313 if (ret != simple_control && ret != break_control)
315 warning (_("Error executing canned sequence of commands."));
318 cmdlines = cmdlines->next;
320 command_nest_depth--;
321 do_cleanups (old_chain);
324 /* This function is called every time GDB prints a prompt.
325 It ensures that errors and the like to not confuse the command tracing. */
328 reset_command_nest_depth (void)
330 command_nest_depth = 1;
333 suppress_next_print_command_trace = 0;
336 /* Print the command, prefixed with '+' to represent the call depth.
337 This is slightly complicated because this function may be called
338 from execute_command and execute_control_command. Unfortunately
339 execute_command also prints the top level control commands.
340 In these cases execute_command will call execute_control_command
341 via while_command or if_command. Inner levels of 'if' and 'while'
342 are dealt with directly. Therefore we can use these functions
343 to determine whether the command has been printed already or not. */
345 print_command_trace (const char *cmd)
349 if (suppress_next_print_command_trace)
351 suppress_next_print_command_trace = 0;
355 if (!source_verbose && !trace_commands)
358 for (i=0; i < command_nest_depth; i++)
359 printf_filtered ("+");
361 printf_filtered ("%s\n", cmd);
364 enum command_control_type
365 execute_control_command (struct command_line *cmd)
367 struct expression *expr;
368 struct command_line *current;
369 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
371 struct value *val_mark;
373 enum command_control_type ret;
376 /* Start by assuming failure, if a problem is detected, the code
377 below will simply "break" out of the switch. */
378 ret = invalid_control;
380 switch (cmd->control_type)
383 /* A simple command, execute it and return. */
384 new_line = insert_args (cmd->line);
387 make_cleanup (free_current_contents, &new_line);
388 execute_command (new_line, 0);
389 ret = cmd->control_type;
392 case continue_control:
393 print_command_trace ("loop_continue");
395 /* Return for "continue", and "break" so we can either
396 continue the loop at the top, or break out. */
397 ret = cmd->control_type;
401 print_command_trace ("loop_break");
403 /* Return for "continue", and "break" so we can either
404 continue the loop at the top, or break out. */
405 ret = cmd->control_type;
410 char *buffer = alloca (strlen (cmd->line) + 7);
411 sprintf (buffer, "while %s", cmd->line);
412 print_command_trace (buffer);
414 /* Parse the loop control expression for the while statement. */
415 new_line = insert_args (cmd->line);
418 make_cleanup (free_current_contents, &new_line);
419 expr = parse_expression (new_line);
420 make_cleanup (free_current_contents, &expr);
422 ret = simple_control;
425 /* Keep iterating so long as the expression is true. */
432 /* Evaluate the expression. */
433 val_mark = value_mark ();
434 val = evaluate_expression (expr);
435 cond_result = value_true (val);
436 value_free_to_mark (val_mark);
438 /* If the value is false, then break out of the loop. */
442 /* Execute the body of the while statement. */
443 current = *cmd->body_list;
446 command_nest_depth++;
447 ret = execute_control_command (current);
448 command_nest_depth--;
450 /* If we got an error, or a "break" command, then stop
452 if (ret == invalid_control || ret == break_control)
458 /* If we got a "continue" command, then restart the loop
460 if (ret == continue_control)
463 /* Get the next statement. */
464 current = current->next;
468 /* Reset RET so that we don't recurse the break all the way down. */
469 if (ret == break_control)
470 ret = simple_control;
477 char *buffer = alloca (strlen (cmd->line) + 4);
478 sprintf (buffer, "if %s", cmd->line);
479 print_command_trace (buffer);
481 new_line = insert_args (cmd->line);
484 make_cleanup (free_current_contents, &new_line);
485 /* Parse the conditional for the if statement. */
486 expr = parse_expression (new_line);
487 make_cleanup (free_current_contents, &expr);
490 ret = simple_control;
492 /* Evaluate the conditional. */
493 val_mark = value_mark ();
494 val = evaluate_expression (expr);
496 /* Choose which arm to take commands from based on the value of the
497 conditional expression. */
498 if (value_true (val))
499 current = *cmd->body_list;
500 else if (cmd->body_count == 2)
501 current = *(cmd->body_list + 1);
502 value_free_to_mark (val_mark);
504 /* Execute commands in the given arm. */
507 command_nest_depth++;
508 ret = execute_control_command (current);
509 command_nest_depth--;
511 /* If we got an error, get out. */
512 if (ret != simple_control)
515 /* Get the next statement in the body. */
516 current = current->next;
521 case commands_control:
523 /* Breakpoint commands list, record the commands in the breakpoint's
524 command list and return. */
525 new_line = insert_args (cmd->line);
528 make_cleanup (free_current_contents, &new_line);
529 ret = commands_from_control_command (new_line, cmd);
534 warning (_("Invalid control type in canned commands structure."));
538 do_cleanups (old_chain);
543 /* "while" command support. Executes a body of statements while the
544 loop condition is nonzero. */
547 while_command (char *arg, int from_tty)
549 struct command_line *command = NULL;
552 command = get_command_line (while_control, arg);
557 suppress_next_print_command_trace = 1;
558 execute_control_command (command);
559 free_command_lines (&command);
562 /* "if" command support. Execute either the true or false arm depending
563 on the value of the if conditional. */
566 if_command (char *arg, int from_tty)
568 struct command_line *command = NULL;
571 command = get_command_line (if_control, arg);
576 suppress_next_print_command_trace = 1;
577 execute_control_command (command);
578 free_command_lines (&command);
583 arg_cleanup (void *ignore)
585 struct user_args *oargs = user_args;
587 internal_error (__FILE__, __LINE__,
588 _("arg_cleanup called with no user args.\n"));
590 user_args = user_args->next;
591 xfree (oargs->command);
595 /* Bind the incomming arguments for a user defined command to
596 $arg0, $arg1 ... $argMAXUSERARGS. */
598 static struct cleanup *
599 setup_user_args (char *p)
601 struct user_args *args;
602 struct cleanup *old_chain;
603 unsigned int arg_count = 0;
605 args = (struct user_args *) xmalloc (sizeof (struct user_args));
606 memset (args, 0, sizeof (struct user_args));
608 args->next = user_args;
611 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
616 user_args->command = p = xstrdup (p);
625 if (arg_count >= MAXUSERARGS)
627 error (_("user defined function may only have %d arguments."),
632 /* Strip whitespace. */
633 while (*p == ' ' || *p == '\t')
636 /* P now points to an argument. */
638 user_args->a[arg_count].arg = p;
640 /* Get to the end of this argument. */
643 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
672 user_args->a[arg_count].len = p - start_arg;
679 /* Given character string P, return a point to the first argument ($arg),
680 or NULL if P contains no arguments. */
685 while ((p = strchr (p, '$')))
687 if (strncmp (p, "$arg", 4) == 0
688 && (isdigit (p[4]) || p[4] == 'c'))
695 /* Insert the user defined arguments stored in user_arg into the $arg
696 arguments found in line, with the updated copy being placed into nline. */
699 insert_args (char *line)
701 char *p, *save_line, *new_line;
704 /* If we are not in a user-defined function, treat $argc, $arg0, et
705 cetera as normal convenience variables. */
706 if (user_args == NULL)
707 return xstrdup (line);
709 /* First we need to know how much memory to allocate for the new line. */
712 while ((p = locate_arg (line)))
719 /* $argc. Number will be <=10. */
720 len += user_args->count == 10 ? 2 : 1;
722 else if (i >= user_args->count)
724 error (_("Missing argument %d in user function."), i);
729 len += user_args->a[i].len;
734 /* Don't forget the tail. */
735 len += strlen (line);
737 /* Allocate space for the new line and fill it in. */
738 new_line = (char *) xmalloc (len + 1);
739 if (new_line == NULL)
742 /* Restore pointer to beginning of old line. */
745 /* Save pointer to beginning of new line. */
746 save_line = new_line;
748 while ((p = locate_arg (line)))
752 memcpy (new_line, line, p - line);
753 new_line += p - line;
757 gdb_assert (user_args->count >= 0 && user_args->count <= 10);
758 if (user_args->count == 10)
764 *(new_line++) = user_args->count + '0';
769 len = user_args->a[i].len;
772 memcpy (new_line, user_args->a[i].arg, len);
778 /* Don't forget the tail. */
779 strcpy (new_line, line);
781 /* Return a pointer to the beginning of the new line. */
786 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
787 code bodies. This is typically used when we encounter an "else"
788 clause for an "if" command. */
791 realloc_body_list (struct command_line *command, int new_length)
794 struct command_line **body_list;
796 n = command->body_count;
802 body_list = (struct command_line **)
803 xmalloc (sizeof (struct command_line *) * new_length);
805 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
806 memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
808 xfree (command->body_list);
809 command->body_list = body_list;
810 command->body_count = new_length;
813 /* Read one line from the input stream. If the command is an "else" or
814 "end", return such an indication to the caller. */
816 static enum misc_command_type
817 read_next_line (struct command_line **command)
819 char *p, *p1, *prompt_ptr, control_prompt[256];
822 if (control_level >= 254)
823 error (_("Control nesting too deep!"));
825 /* Set a prompt based on the nesting of the control commands. */
826 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
828 for (i = 0; i < control_level; i++)
829 control_prompt[i] = ' ';
830 control_prompt[i] = '>';
831 control_prompt[i + 1] = '\0';
832 prompt_ptr = (char *) &control_prompt[0];
837 p = command_line_input (prompt_ptr, instream == stdin, "commands");
839 /* Not sure what to do here. */
843 /* Strip leading and trailing whitespace. */
844 while (*p == ' ' || *p == '\t')
848 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
851 /* Blanks and comments don't really do anything, but we need to
852 distinguish them from else, end and other commands which can be
854 if (p1 == p || p[0] == '#')
857 /* Is this the end of a simple, while, or if control structure? */
858 if (p1 - p == 3 && !strncmp (p, "end", 3))
861 /* Is the else clause of an if control structure? */
862 if (p1 - p == 4 && !strncmp (p, "else", 4))
865 /* Check for while, if, break, continue, etc and build a new command
866 line structure for them. */
867 if (p1 - p > 5 && !strncmp (p, "while", 5))
871 while (first_arg < p1 && isspace (*first_arg))
873 *command = build_command_line (while_control, first_arg);
875 else if (p1 - p > 2 && !strncmp (p, "if", 2))
879 while (first_arg < p1 && isspace (*first_arg))
881 *command = build_command_line (if_control, first_arg);
883 else if (p1 - p >= 8 && !strncmp (p, "commands", 8))
887 while (first_arg < p1 && isspace (*first_arg))
889 *command = build_command_line (commands_control, first_arg);
891 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
893 *command = (struct command_line *)
894 xmalloc (sizeof (struct command_line));
895 (*command)->next = NULL;
896 (*command)->line = NULL;
897 (*command)->control_type = break_control;
898 (*command)->body_count = 0;
899 (*command)->body_list = NULL;
901 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
903 *command = (struct command_line *)
904 xmalloc (sizeof (struct command_line));
905 (*command)->next = NULL;
906 (*command)->line = NULL;
907 (*command)->control_type = continue_control;
908 (*command)->body_count = 0;
909 (*command)->body_list = NULL;
913 /* A normal command. */
914 *command = (struct command_line *)
915 xmalloc (sizeof (struct command_line));
916 (*command)->next = NULL;
917 (*command)->line = savestring (p, p1 - p);
918 (*command)->control_type = simple_control;
919 (*command)->body_count = 0;
920 (*command)->body_list = NULL;
923 /* Nothing special. */
927 /* Recursively read in the control structures and create a command_line
930 The parent_control parameter is the control structure in which the
931 following commands are nested. */
933 static enum command_control_type
934 recurse_read_control_structure (struct command_line *current_cmd)
937 enum misc_command_type val;
938 enum command_control_type ret;
939 struct command_line **body_ptr, *child_tail, *next;
945 if (current_cmd->control_type == simple_control)
946 error (_("Recursed on a simple control type."));
948 if (current_body > current_cmd->body_count)
949 error (_("Allocated body is smaller than this command type needs."));
951 /* Read lines from the input stream and build control structures. */
957 val = read_next_line (&next);
959 /* Just skip blanks and comments. */
960 if (val == nop_command)
963 if (val == end_command)
965 if (current_cmd->control_type == while_control
966 || current_cmd->control_type == if_control
967 || current_cmd->control_type == commands_control)
969 /* Success reading an entire canned sequence of commands. */
970 ret = simple_control;
975 ret = invalid_control;
980 /* Not the end of a control structure. */
981 if (val == else_command)
983 if (current_cmd->control_type == if_control
984 && current_body == 1)
986 realloc_body_list (current_cmd, 2);
993 ret = invalid_control;
1000 child_tail->next = next;
1004 body_ptr = current_cmd->body_list;
1005 for (i = 1; i < current_body; i++)
1014 /* If the latest line is another control structure, then recurse
1016 if (next->control_type == while_control
1017 || next->control_type == if_control
1018 || next->control_type == commands_control)
1021 ret = recurse_read_control_structure (next);
1024 if (ret != simple_control)
1034 /* Read lines from the input stream and accumulate them in a chain of
1035 struct command_line's, which is then returned. For input from a
1036 terminal, the special command "end" is used to mark the end of the
1037 input, and is not included in the returned chain of commands. */
1039 #define END_MESSAGE "End with a line saying just \"end\"."
1041 struct command_line *
1042 read_command_lines (char *prompt_arg, int from_tty)
1044 struct command_line *head, *tail, *next;
1045 struct cleanup *old_chain;
1046 enum command_control_type ret;
1047 enum misc_command_type val;
1051 if (from_tty && input_from_terminal_p ())
1053 if (deprecated_readline_begin_hook)
1055 /* Note - intentional to merge messages with no newline */
1056 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1060 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1061 gdb_flush (gdb_stdout);
1071 val = read_next_line (&next);
1073 /* Ignore blank lines or comments. */
1074 if (val == nop_command)
1077 if (val == end_command)
1079 ret = simple_control;
1083 if (val != ok_command)
1085 ret = invalid_control;
1089 if (next->control_type == while_control
1090 || next->control_type == if_control
1091 || next->control_type == commands_control)
1094 ret = recurse_read_control_structure (next);
1097 if (ret == invalid_control)
1108 old_chain = make_cleanup_free_command_lines (&head);
1117 if (ret != invalid_control)
1119 discard_cleanups (old_chain);
1122 do_cleanups (old_chain);
1125 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1127 (*deprecated_readline_end_hook) ();
1132 /* Free a chain of struct command_line's. */
1135 free_command_lines (struct command_line **lptr)
1137 struct command_line *l = *lptr;
1138 struct command_line *next;
1139 struct command_line **blist;
1144 if (l->body_count > 0)
1146 blist = l->body_list;
1147 for (i = 0; i < l->body_count; i++, blist++)
1148 free_command_lines (blist);
1159 do_free_command_lines_cleanup (void *arg)
1161 free_command_lines (arg);
1165 make_cleanup_free_command_lines (struct command_line **arg)
1167 return make_cleanup (do_free_command_lines_cleanup, arg);
1170 struct command_line *
1171 copy_command_lines (struct command_line *cmds)
1173 struct command_line *result = NULL;
1177 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1179 result->next = copy_command_lines (cmds->next);
1180 result->line = xstrdup (cmds->line);
1181 result->control_type = cmds->control_type;
1182 result->body_count = cmds->body_count;
1183 if (cmds->body_count > 0)
1187 result->body_list = (struct command_line **)
1188 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1190 for (i = 0; i < cmds->body_count; i++)
1191 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1194 result->body_list = NULL;
1201 validate_comname (char *comname)
1206 error_no_arg (_("name of command to define"));
1211 if (!isalnum (*p) && *p != '-' && *p != '_')
1212 error (_("Junk in argument list: \"%s\""), p);
1217 /* This is just a placeholder in the command data structures. */
1219 user_defined_command (char *ignore, int from_tty)
1224 define_command (char *comname, int from_tty)
1226 #define MAX_TMPBUF 128
1233 struct command_line *cmds;
1234 struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1235 char *tem = comname;
1237 char tmpbuf[MAX_TMPBUF];
1238 int hook_type = CMD_NO_HOOK;
1239 int hook_name_size = 0;
1241 #define HOOK_STRING "hook-"
1243 #define HOOK_POST_STRING "hookpost-"
1244 #define HOOK_POST_LEN 9
1246 validate_comname (comname);
1248 /* Look it up, and verify that we got an exact match. */
1249 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1250 if (c && strcmp (comname, c->name) != 0)
1256 if (c->class == class_user || c->class == class_alias)
1257 q = query (_("Redefine command \"%s\"? "), c->name);
1259 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1261 error (_("Command \"%s\" not redefined."), c->name);
1264 /* If this new command is a hook, then mark the command which it
1265 is hooking. Note that we allow hooking `help' commands, so that
1266 we can hook the `stop' pseudo-command. */
1268 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1270 hook_type = CMD_PRE_HOOK;
1271 hook_name_size = HOOK_LEN;
1273 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1275 hook_type = CMD_POST_HOOK;
1276 hook_name_size = HOOK_POST_LEN;
1279 if (hook_type != CMD_NO_HOOK)
1281 /* Look up cmd it hooks, and verify that we got an exact match. */
1282 tem = comname + hook_name_size;
1283 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1284 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1288 warning (_("Your new `%s' command does not hook any existing command."),
1290 if (!query ("Proceed? "))
1291 error (_("Not confirmed."));
1295 comname = savestring (comname, strlen (comname));
1297 /* If the rest of the commands will be case insensitive, this one
1298 should behave in the same manner. */
1299 for (tem = comname; *tem; tem++)
1301 *tem = tolower (*tem);
1303 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1304 cmds = read_command_lines (tmpbuf, from_tty);
1306 if (c && c->class == class_user)
1307 free_command_lines (&c->user_commands);
1309 newc = add_cmd (comname, class_user, user_defined_command,
1310 (c && c->class == class_user)
1311 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1312 newc->user_commands = cmds;
1314 /* If this new command is a hook, then mark both commands as being
1321 hookc->hook_pre = newc; /* Target gets hooked. */
1322 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1325 hookc->hook_post = newc; /* Target gets hooked. */
1326 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1329 /* Should never come here as hookc would be 0. */
1330 internal_error (__FILE__, __LINE__, _("bad switch"));
1336 document_command (char *comname, int from_tty)
1338 struct command_line *doclines;
1339 struct cmd_list_element *c;
1340 char *tem = comname;
1343 validate_comname (comname);
1345 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1347 if (c->class != class_user)
1348 error (_("Command \"%s\" is built-in."), comname);
1350 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1351 doclines = read_command_lines (tmpbuf, from_tty);
1357 struct command_line *cl1;
1360 for (cl1 = doclines; cl1; cl1 = cl1->next)
1361 len += strlen (cl1->line) + 1;
1363 c->doc = (char *) xmalloc (len + 1);
1366 for (cl1 = doclines; cl1; cl1 = cl1->next)
1368 strcat (c->doc, cl1->line);
1370 strcat (c->doc, "\n");
1374 free_command_lines (&doclines);
1377 struct source_cleanup_lines_args
1384 source_cleanup_lines (void *args)
1386 struct source_cleanup_lines_args *p =
1387 (struct source_cleanup_lines_args *) args;
1388 source_line_number = p->old_line;
1389 source_file_name = p->old_file;
1393 do_fclose_cleanup (void *stream)
1398 struct wrapped_read_command_file_args
1404 wrapped_read_command_file (struct ui_out *uiout, void *data)
1406 struct wrapped_read_command_file_args *args = data;
1407 read_command_file (args->stream);
1410 /* Used to implement source_command */
1413 script_from_file (FILE *stream, char *file)
1415 struct cleanup *old_cleanups;
1416 struct source_cleanup_lines_args old_lines;
1420 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1422 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1424 old_lines.old_line = source_line_number;
1425 old_lines.old_file = source_file_name;
1426 make_cleanup (source_cleanup_lines, &old_lines);
1427 source_line_number = 0;
1428 source_file_name = file;
1429 /* This will get set every time we read a line. So it won't stay "" for
1431 error_pre_print = "";
1434 struct gdb_exception e;
1435 struct wrapped_read_command_file_args args;
1436 args.stream = stream;
1437 e = catch_exception (uiout, wrapped_read_command_file, &args,
1444 /* Re-throw the error, but with the file name information
1446 throw_error (e.error,
1447 _("%s:%d: Error in sourced command file:\n%s"),
1448 source_file_name, source_line_number, e.message);
1450 internal_error (__FILE__, __LINE__, _("bad reason"));
1454 do_cleanups (old_cleanups);
1458 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1460 struct command_line *cmdlines;
1462 cmdlines = c->user_commands;
1465 fputs_filtered ("User command ", stream);
1466 fputs_filtered (c->name, stream);
1467 fputs_filtered (":\n", stream);
1469 print_command_lines (uiout, cmdlines, 1);
1470 fputs_filtered ("\n", stream);