1 /* GDB CLI command scripting.
3 Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006
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 "cli/cli-cmds.h"
34 #include "cli/cli-decode.h"
35 #include "cli/cli-script.h"
36 #include "gdb_assert.h"
38 /* Prototypes for local functions */
40 static enum command_control_type
41 recurse_read_control_structure (struct command_line *current_cmd);
43 static char *insert_args (char *line);
45 static struct cleanup * setup_user_args (char *p);
47 static void validate_comname (char *);
49 /* Level of control structure when reading. */
50 static int control_level;
52 /* Level of control structure when executing. */
53 static int command_nest_depth = 1;
55 /* This is to prevent certain commands being printed twice. */
56 static int suppress_next_print_command_trace = 0;
58 /* Structure for arguments to user defined functions. */
59 #define MAXUSERARGS 10
62 struct user_args *next;
63 /* It is necessary to store a malloced copy of the command line to
64 ensure that the arguments are not overwritten before they are used. */
77 /* Allocate, initialize a new command line structure for one of the
78 control commands (if/while). */
80 static struct command_line *
81 build_command_line (enum command_control_type type, char *args)
83 struct command_line *cmd;
86 error (_("if/while commands require arguments."));
88 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
90 cmd->control_type = type;
94 = (struct command_line **) xmalloc (sizeof (struct command_line *)
96 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
97 cmd->line = savestring (args, strlen (args));
101 /* Build and return a new command structure for the control commands
102 such as "if" and "while". */
104 static struct command_line *
105 get_command_line (enum command_control_type type, char *arg)
107 struct command_line *cmd;
108 struct cleanup *old_chain = NULL;
110 /* Allocate and build a new command line structure. */
111 cmd = build_command_line (type, arg);
113 old_chain = make_cleanup_free_command_lines (&cmd);
115 /* Read in the body of this command. */
116 if (recurse_read_control_structure (cmd) == invalid_control)
118 warning (_("Error reading in control structure."));
119 do_cleanups (old_chain);
123 discard_cleanups (old_chain);
127 /* Recursively print a command (including full control structures). */
130 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
133 struct command_line *list;
140 ui_out_spaces (uiout, 2 * depth);
142 /* A simple command, print it and continue. */
143 if (list->control_type == simple_control)
145 ui_out_field_string (uiout, NULL, list->line);
146 ui_out_text (uiout, "\n");
151 /* loop_continue to jump to the start of a while loop, print it
153 if (list->control_type == continue_control)
155 ui_out_field_string (uiout, NULL, "loop_continue");
156 ui_out_text (uiout, "\n");
161 /* loop_break to break out of a while loop, print it and continue. */
162 if (list->control_type == break_control)
164 ui_out_field_string (uiout, NULL, "loop_break");
165 ui_out_text (uiout, "\n");
170 /* A while command. Recursively print its subcommands and continue. */
171 if (list->control_type == while_control)
173 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
174 ui_out_text (uiout, "\n");
175 print_command_lines (uiout, *list->body_list, depth + 1);
177 ui_out_spaces (uiout, 2 * depth);
178 ui_out_field_string (uiout, NULL, "end");
179 ui_out_text (uiout, "\n");
184 /* An if command. Recursively print both arms before continueing. */
185 if (list->control_type == if_control)
187 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
188 ui_out_text (uiout, "\n");
190 print_command_lines (uiout, list->body_list[0], depth + 1);
192 /* Show the false arm if it exists. */
193 if (list->body_count == 2)
196 ui_out_spaces (uiout, 2 * depth);
197 ui_out_field_string (uiout, NULL, "else");
198 ui_out_text (uiout, "\n");
199 print_command_lines (uiout, list->body_list[1], depth + 1);
203 ui_out_spaces (uiout, 2 * depth);
204 ui_out_field_string (uiout, NULL, "end");
205 ui_out_text (uiout, "\n");
210 /* ignore illegal command type and try next */
215 /* Handle pre-post hooks. */
218 clear_hook_in_cleanup (void *data)
220 struct cmd_list_element *c = data;
221 c->hook_in = 0; /* Allow hook to work again once it is complete */
225 execute_cmd_pre_hook (struct cmd_list_element *c)
227 if ((c->hook_pre) && (!c->hook_in))
229 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
230 c->hook_in = 1; /* Prevent recursive hooking */
231 execute_user_command (c->hook_pre, (char *) 0);
232 do_cleanups (cleanups);
237 execute_cmd_post_hook (struct cmd_list_element *c)
239 if ((c->hook_post) && (!c->hook_in))
241 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
242 c->hook_in = 1; /* Prevent recursive hooking */
243 execute_user_command (c->hook_post, (char *) 0);
244 do_cleanups (cleanups);
248 /* Execute the command in CMD. */
250 do_restore_user_call_depth (void * call_depth)
252 int * depth = call_depth;
260 execute_user_command (struct cmd_list_element *c, char *args)
262 struct command_line *cmdlines;
263 struct cleanup *old_chain;
264 enum command_control_type ret;
265 static int user_call_depth = 0;
266 extern int max_user_call_depth;
268 old_chain = setup_user_args (args);
270 cmdlines = c->user_commands;
275 if (++user_call_depth > max_user_call_depth)
276 error (_("Max user call depth exceeded -- command aborted."));
278 make_cleanup (do_restore_user_call_depth, &user_call_depth);
280 /* Set the instream to 0, indicating execution of a
281 user-defined function. */
282 make_cleanup (do_restore_instream_cleanup, instream);
283 instream = (FILE *) 0;
285 /* Also set the global in_user_command, so that NULL instream is
286 not confused with Insight. */
291 ret = execute_control_command (cmdlines);
292 if (ret != simple_control && ret != break_control)
294 warning (_("Error in control structure."));
297 cmdlines = cmdlines->next;
299 do_cleanups (old_chain);
302 /* This function is called every time GDB prints a prompt.
303 It ensures that errors and the like to not confuse the command tracing. */
306 reset_command_nest_depth (void)
308 command_nest_depth = 1;
311 suppress_next_print_command_trace = 0;
314 /* Print the command, prefixed with '+' to represent the call depth.
315 This is slightly complicated because this function may be called
316 from execute_command and execute_control_command. Unfortunately
317 execute_command also prints the top level control commands.
318 In these cases execute_command will call execute_control_command
319 via while_command or if_command. Inner levels of 'if' and 'while'
320 are dealt with directly. Therefore we can use these functions
321 to determine whether the command has been printed already or not. */
323 print_command_trace (const char *cmd)
327 if (suppress_next_print_command_trace)
329 suppress_next_print_command_trace = 0;
333 if (!source_verbose && !trace_commands)
336 for (i=0; i < command_nest_depth; i++)
337 printf_filtered ("+");
339 printf_filtered ("%s\n", cmd);
342 enum command_control_type
343 execute_control_command (struct command_line *cmd)
345 struct expression *expr;
346 struct command_line *current;
347 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
349 struct value *val_mark;
351 enum command_control_type ret;
354 /* Start by assuming failure, if a problem is detected, the code
355 below will simply "break" out of the switch. */
356 ret = invalid_control;
358 switch (cmd->control_type)
361 /* A simple command, execute it and return. */
362 new_line = insert_args (cmd->line);
365 make_cleanup (free_current_contents, &new_line);
366 execute_command (new_line, 0);
367 ret = cmd->control_type;
370 case continue_control:
371 print_command_trace ("loop_continue");
373 /* Return for "continue", and "break" so we can either
374 continue the loop at the top, or break out. */
375 ret = cmd->control_type;
379 print_command_trace ("loop_break");
381 /* Return for "continue", and "break" so we can either
382 continue the loop at the top, or break out. */
383 ret = cmd->control_type;
388 char *buffer = alloca (strlen (cmd->line) + 7);
389 sprintf (buffer, "while %s", cmd->line);
390 print_command_trace (buffer);
392 /* Parse the loop control expression for the while statement. */
393 new_line = insert_args (cmd->line);
396 make_cleanup (free_current_contents, &new_line);
397 expr = parse_expression (new_line);
398 make_cleanup (free_current_contents, &expr);
400 ret = simple_control;
403 /* Keep iterating so long as the expression is true. */
410 /* Evaluate the expression. */
411 val_mark = value_mark ();
412 val = evaluate_expression (expr);
413 cond_result = value_true (val);
414 value_free_to_mark (val_mark);
416 /* If the value is false, then break out of the loop. */
420 /* Execute the body of the while statement. */
421 current = *cmd->body_list;
424 command_nest_depth++;
425 ret = execute_control_command (current);
426 command_nest_depth--;
428 /* If we got an error, or a "break" command, then stop
430 if (ret == invalid_control || ret == break_control)
436 /* If we got a "continue" command, then restart the loop
438 if (ret == continue_control)
441 /* Get the next statement. */
442 current = current->next;
446 /* Reset RET so that we don't recurse the break all the way down. */
447 if (ret == break_control)
448 ret = simple_control;
455 char *buffer = alloca (strlen (cmd->line) + 4);
456 sprintf (buffer, "if %s", cmd->line);
457 print_command_trace (buffer);
459 new_line = insert_args (cmd->line);
462 make_cleanup (free_current_contents, &new_line);
463 /* Parse the conditional for the if statement. */
464 expr = parse_expression (new_line);
465 make_cleanup (free_current_contents, &expr);
468 ret = simple_control;
470 /* Evaluate the conditional. */
471 val_mark = value_mark ();
472 val = evaluate_expression (expr);
474 /* Choose which arm to take commands from based on the value of the
475 conditional expression. */
476 if (value_true (val))
477 current = *cmd->body_list;
478 else if (cmd->body_count == 2)
479 current = *(cmd->body_list + 1);
480 value_free_to_mark (val_mark);
482 /* Execute commands in the given arm. */
485 command_nest_depth++;
486 ret = execute_control_command (current);
487 command_nest_depth--;
489 /* If we got an error, get out. */
490 if (ret != simple_control)
493 /* Get the next statement in the body. */
494 current = current->next;
501 warning (_("Invalid control type in command structure."));
505 do_cleanups (old_chain);
510 /* "while" command support. Executes a body of statements while the
511 loop condition is nonzero. */
514 while_command (char *arg, int from_tty)
516 struct command_line *command = NULL;
519 command = get_command_line (while_control, arg);
524 suppress_next_print_command_trace = 1;
525 execute_control_command (command);
526 free_command_lines (&command);
529 /* "if" command support. Execute either the true or false arm depending
530 on the value of the if conditional. */
533 if_command (char *arg, int from_tty)
535 struct command_line *command = NULL;
538 command = get_command_line (if_control, arg);
543 suppress_next_print_command_trace = 1;
544 execute_control_command (command);
545 free_command_lines (&command);
550 arg_cleanup (void *ignore)
552 struct user_args *oargs = user_args;
554 internal_error (__FILE__, __LINE__,
555 _("arg_cleanup called with no user args.\n"));
557 user_args = user_args->next;
558 xfree (oargs->command);
562 /* Bind the incomming arguments for a user defined command to
563 $arg0, $arg1 ... $argMAXUSERARGS. */
565 static struct cleanup *
566 setup_user_args (char *p)
568 struct user_args *args;
569 struct cleanup *old_chain;
570 unsigned int arg_count = 0;
572 args = (struct user_args *) xmalloc (sizeof (struct user_args));
573 memset (args, 0, sizeof (struct user_args));
575 args->next = user_args;
578 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
583 user_args->command = p = xstrdup (p);
592 if (arg_count >= MAXUSERARGS)
594 error (_("user defined function may only have %d arguments."),
599 /* Strip whitespace. */
600 while (*p == ' ' || *p == '\t')
603 /* P now points to an argument. */
605 user_args->a[arg_count].arg = p;
607 /* Get to the end of this argument. */
610 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
639 user_args->a[arg_count].len = p - start_arg;
646 /* Given character string P, return a point to the first argument ($arg),
647 or NULL if P contains no arguments. */
652 while ((p = strchr (p, '$')))
654 if (strncmp (p, "$arg", 4) == 0
655 && (isdigit (p[4]) || p[4] == 'c'))
662 /* Insert the user defined arguments stored in user_arg into the $arg
663 arguments found in line, with the updated copy being placed into nline. */
666 insert_args (char *line)
668 char *p, *save_line, *new_line;
671 /* If we are not in a user-defined function, treat $argc, $arg0, et
672 cetera as normal convenience variables. */
673 if (user_args == NULL)
674 return xstrdup (line);
676 /* First we need to know how much memory to allocate for the new line. */
679 while ((p = locate_arg (line)))
686 /* $argc. Number will be <=10. */
687 len += user_args->count == 10 ? 2 : 1;
689 else if (i >= user_args->count)
691 error (_("Missing argument %d in user function."), i);
696 len += user_args->a[i].len;
701 /* Don't forget the tail. */
702 len += strlen (line);
704 /* Allocate space for the new line and fill it in. */
705 new_line = (char *) xmalloc (len + 1);
706 if (new_line == NULL)
709 /* Restore pointer to beginning of old line. */
712 /* Save pointer to beginning of new line. */
713 save_line = new_line;
715 while ((p = locate_arg (line)))
719 memcpy (new_line, line, p - line);
720 new_line += p - line;
724 gdb_assert (user_args->count >= 0 && user_args->count <= 10);
725 if (user_args->count == 10)
731 *(new_line++) = user_args->count + '0';
736 len = user_args->a[i].len;
739 memcpy (new_line, user_args->a[i].arg, len);
745 /* Don't forget the tail. */
746 strcpy (new_line, line);
748 /* Return a pointer to the beginning of the new line. */
753 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
754 code bodies. This is typically used when we encounter an "else"
755 clause for an "if" command. */
758 realloc_body_list (struct command_line *command, int new_length)
761 struct command_line **body_list;
763 n = command->body_count;
769 body_list = (struct command_line **)
770 xmalloc (sizeof (struct command_line *) * new_length);
772 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
773 memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
775 xfree (command->body_list);
776 command->body_list = body_list;
777 command->body_count = new_length;
780 /* Read one line from the input stream. If the command is an "else" or
781 "end", return such an indication to the caller. */
783 static enum misc_command_type
784 read_next_line (struct command_line **command)
786 char *p, *p1, *prompt_ptr, control_prompt[256];
789 if (control_level >= 254)
790 error (_("Control nesting too deep!"));
792 /* Set a prompt based on the nesting of the control commands. */
793 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
795 for (i = 0; i < control_level; i++)
796 control_prompt[i] = ' ';
797 control_prompt[i] = '>';
798 control_prompt[i + 1] = '\0';
799 prompt_ptr = (char *) &control_prompt[0];
804 p = command_line_input (prompt_ptr, instream == stdin, "commands");
806 /* Not sure what to do here. */
810 /* Strip leading and trailing whitespace. */
811 while (*p == ' ' || *p == '\t')
815 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
818 /* Blanks and comments don't really do anything, but we need to
819 distinguish them from else, end and other commands which can be
821 if (p1 == p || p[0] == '#')
824 /* Is this the end of a simple, while, or if control structure? */
825 if (p1 - p == 3 && !strncmp (p, "end", 3))
828 /* Is the else clause of an if control structure? */
829 if (p1 - p == 4 && !strncmp (p, "else", 4))
832 /* Check for while, if, break, continue, etc and build a new command
833 line structure for them. */
834 if (p1 - p > 5 && !strncmp (p, "while", 5))
838 while (first_arg < p1 && isspace (*first_arg))
840 *command = build_command_line (while_control, first_arg);
842 else if (p1 - p > 2 && !strncmp (p, "if", 2))
846 while (first_arg < p1 && isspace (*first_arg))
848 *command = build_command_line (if_control, first_arg);
850 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
852 *command = (struct command_line *)
853 xmalloc (sizeof (struct command_line));
854 (*command)->next = NULL;
855 (*command)->line = NULL;
856 (*command)->control_type = break_control;
857 (*command)->body_count = 0;
858 (*command)->body_list = NULL;
860 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
862 *command = (struct command_line *)
863 xmalloc (sizeof (struct command_line));
864 (*command)->next = NULL;
865 (*command)->line = NULL;
866 (*command)->control_type = continue_control;
867 (*command)->body_count = 0;
868 (*command)->body_list = NULL;
872 /* A normal command. */
873 *command = (struct command_line *)
874 xmalloc (sizeof (struct command_line));
875 (*command)->next = NULL;
876 (*command)->line = savestring (p, p1 - p);
877 (*command)->control_type = simple_control;
878 (*command)->body_count = 0;
879 (*command)->body_list = NULL;
882 /* Nothing special. */
886 /* Recursively read in the control structures and create a command_line
889 The parent_control parameter is the control structure in which the
890 following commands are nested. */
892 static enum command_control_type
893 recurse_read_control_structure (struct command_line *current_cmd)
896 enum misc_command_type val;
897 enum command_control_type ret;
898 struct command_line **body_ptr, *child_tail, *next;
904 if (current_cmd->control_type == simple_control)
905 error (_("Recursed on a simple control type."));
907 if (current_body > current_cmd->body_count)
908 error (_("Allocated body is smaller than this command type needs."));
910 /* Read lines from the input stream and build control structures. */
916 val = read_next_line (&next);
918 /* Just skip blanks and comments. */
919 if (val == nop_command)
922 if (val == end_command)
924 if (current_cmd->control_type == while_control
925 || current_cmd->control_type == if_control)
927 /* Success reading an entire control structure. */
928 ret = simple_control;
933 ret = invalid_control;
938 /* Not the end of a control structure. */
939 if (val == else_command)
941 if (current_cmd->control_type == if_control
942 && current_body == 1)
944 realloc_body_list (current_cmd, 2);
951 ret = invalid_control;
958 child_tail->next = next;
962 body_ptr = current_cmd->body_list;
963 for (i = 1; i < current_body; i++)
972 /* If the latest line is another control structure, then recurse
974 if (next->control_type == while_control
975 || next->control_type == if_control)
978 ret = recurse_read_control_structure (next);
981 if (ret != simple_control)
991 /* Read lines from the input stream and accumulate them in a chain of
992 struct command_line's, which is then returned. For input from a
993 terminal, the special command "end" is used to mark the end of the
994 input, and is not included in the returned chain of commands. */
996 #define END_MESSAGE "End with a line saying just \"end\"."
998 struct command_line *
999 read_command_lines (char *prompt_arg, int from_tty)
1001 struct command_line *head, *tail, *next;
1002 struct cleanup *old_chain;
1003 enum command_control_type ret;
1004 enum misc_command_type val;
1008 if (from_tty && input_from_terminal_p ())
1010 if (deprecated_readline_begin_hook)
1012 /* Note - intentional to merge messages with no newline */
1013 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1017 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1018 gdb_flush (gdb_stdout);
1027 val = read_next_line (&next);
1029 /* Ignore blank lines or comments. */
1030 if (val == nop_command)
1033 if (val == end_command)
1035 ret = simple_control;
1039 if (val != ok_command)
1041 ret = invalid_control;
1045 if (next->control_type == while_control
1046 || next->control_type == if_control)
1049 ret = recurse_read_control_structure (next);
1052 if (ret == invalid_control)
1063 old_chain = make_cleanup_free_command_lines (&head);
1072 if (ret != invalid_control)
1074 discard_cleanups (old_chain);
1077 do_cleanups (old_chain);
1080 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1082 (*deprecated_readline_end_hook) ();
1087 /* Free a chain of struct command_line's. */
1090 free_command_lines (struct command_line **lptr)
1092 struct command_line *l = *lptr;
1093 struct command_line *next;
1094 struct command_line **blist;
1099 if (l->body_count > 0)
1101 blist = l->body_list;
1102 for (i = 0; i < l->body_count; i++, blist++)
1103 free_command_lines (blist);
1114 do_free_command_lines_cleanup (void *arg)
1116 free_command_lines (arg);
1120 make_cleanup_free_command_lines (struct command_line **arg)
1122 return make_cleanup (do_free_command_lines_cleanup, arg);
1125 struct command_line *
1126 copy_command_lines (struct command_line *cmds)
1128 struct command_line *result = NULL;
1132 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1134 result->next = copy_command_lines (cmds->next);
1135 result->line = xstrdup (cmds->line);
1136 result->control_type = cmds->control_type;
1137 result->body_count = cmds->body_count;
1138 if (cmds->body_count > 0)
1142 result->body_list = (struct command_line **)
1143 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1145 for (i = 0; i < cmds->body_count; i++)
1146 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1149 result->body_list = NULL;
1156 validate_comname (char *comname)
1161 error_no_arg (_("name of command to define"));
1166 if (!isalnum (*p) && *p != '-' && *p != '_')
1167 error (_("Junk in argument list: \"%s\""), p);
1172 /* This is just a placeholder in the command data structures. */
1174 user_defined_command (char *ignore, int from_tty)
1179 define_command (char *comname, int from_tty)
1181 #define MAX_TMPBUF 128
1188 struct command_line *cmds;
1189 struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1190 char *tem = comname;
1192 char tmpbuf[MAX_TMPBUF];
1193 int hook_type = CMD_NO_HOOK;
1194 int hook_name_size = 0;
1196 #define HOOK_STRING "hook-"
1198 #define HOOK_POST_STRING "hookpost-"
1199 #define HOOK_POST_LEN 9
1201 validate_comname (comname);
1203 /* Look it up, and verify that we got an exact match. */
1204 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1205 if (c && strcmp (comname, c->name) != 0)
1211 if (c->class == class_user || c->class == class_alias)
1212 q = query (_("Redefine command \"%s\"? "), c->name);
1214 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1216 error (_("Command \"%s\" not redefined."), c->name);
1219 /* If this new command is a hook, then mark the command which it
1220 is hooking. Note that we allow hooking `help' commands, so that
1221 we can hook the `stop' pseudo-command. */
1223 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1225 hook_type = CMD_PRE_HOOK;
1226 hook_name_size = HOOK_LEN;
1228 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1230 hook_type = CMD_POST_HOOK;
1231 hook_name_size = HOOK_POST_LEN;
1234 if (hook_type != CMD_NO_HOOK)
1236 /* Look up cmd it hooks, and verify that we got an exact match. */
1237 tem = comname + hook_name_size;
1238 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1239 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1243 warning (_("Your new `%s' command does not hook any existing command."),
1245 if (!query ("Proceed? "))
1246 error (_("Not confirmed."));
1250 comname = savestring (comname, strlen (comname));
1252 /* If the rest of the commands will be case insensitive, this one
1253 should behave in the same manner. */
1254 for (tem = comname; *tem; tem++)
1256 *tem = tolower (*tem);
1258 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1259 cmds = read_command_lines (tmpbuf, from_tty);
1261 if (c && c->class == class_user)
1262 free_command_lines (&c->user_commands);
1264 newc = add_cmd (comname, class_user, user_defined_command,
1265 (c && c->class == class_user)
1266 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1267 newc->user_commands = cmds;
1269 /* If this new command is a hook, then mark both commands as being
1276 hookc->hook_pre = newc; /* Target gets hooked. */
1277 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1280 hookc->hook_post = newc; /* Target gets hooked. */
1281 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1284 /* Should never come here as hookc would be 0. */
1285 internal_error (__FILE__, __LINE__, _("bad switch"));
1291 document_command (char *comname, int from_tty)
1293 struct command_line *doclines;
1294 struct cmd_list_element *c;
1295 char *tem = comname;
1298 validate_comname (comname);
1300 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1302 if (c->class != class_user)
1303 error (_("Command \"%s\" is built-in."), comname);
1305 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1306 doclines = read_command_lines (tmpbuf, from_tty);
1312 struct command_line *cl1;
1315 for (cl1 = doclines; cl1; cl1 = cl1->next)
1316 len += strlen (cl1->line) + 1;
1318 c->doc = (char *) xmalloc (len + 1);
1321 for (cl1 = doclines; cl1; cl1 = cl1->next)
1323 strcat (c->doc, cl1->line);
1325 strcat (c->doc, "\n");
1329 free_command_lines (&doclines);
1332 struct source_cleanup_lines_args
1339 source_cleanup_lines (void *args)
1341 struct source_cleanup_lines_args *p =
1342 (struct source_cleanup_lines_args *) args;
1343 source_line_number = p->old_line;
1344 source_file_name = p->old_file;
1348 do_fclose_cleanup (void *stream)
1353 struct wrapped_read_command_file_args
1359 wrapped_read_command_file (struct ui_out *uiout, void *data)
1361 struct wrapped_read_command_file_args *args = data;
1362 read_command_file (args->stream);
1365 /* Used to implement source_command */
1368 script_from_file (FILE *stream, char *file)
1370 struct cleanup *old_cleanups;
1371 struct source_cleanup_lines_args old_lines;
1375 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1377 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1379 old_lines.old_line = source_line_number;
1380 old_lines.old_file = source_file_name;
1381 make_cleanup (source_cleanup_lines, &old_lines);
1382 source_line_number = 0;
1383 source_file_name = file;
1384 /* This will get set every time we read a line. So it won't stay "" for
1386 error_pre_print = "";
1389 struct gdb_exception e;
1390 struct wrapped_read_command_file_args args;
1391 args.stream = stream;
1392 e = catch_exception (uiout, wrapped_read_command_file, &args,
1399 /* Re-throw the error, but with the file name information
1401 throw_error (e.error,
1402 _("%s:%d: Error in sourced command file:\n%s"),
1403 source_file_name, source_line_number, e.message);
1405 internal_error (__FILE__, __LINE__, _("bad reason"));
1409 do_cleanups (old_cleanups);
1413 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1415 struct command_line *cmdlines;
1417 cmdlines = c->user_commands;
1420 fputs_filtered ("User command ", stream);
1421 fputs_filtered (c->name, stream);
1422 fputs_filtered (":\n", stream);
1424 print_command_lines (uiout, cmdlines, 1);
1425 fputs_filtered ("\n", stream);