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. */
50 static int control_level
;
52 /* Structure for arguments to user defined functions. */
53 #define MAXUSERARGS 10
56 struct user_args
*next
;
57 /* It is necessary to store a malloced copy of the command line to
58 ensure that the arguments are not overwritten before they are used. */
71 /* Allocate, initialize a new command line structure for one of the
72 control commands (if/while). */
74 static struct command_line
*
75 build_command_line (enum command_control_type type
, char *args
)
77 struct command_line
*cmd
;
80 error (_("if/while commands require arguments."));
82 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
84 cmd
->control_type
= type
;
88 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
90 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
91 cmd
->line
= savestring (args
, strlen (args
));
95 /* Build and return a new command structure for the control commands
96 such as "if" and "while". */
98 static struct command_line
*
99 get_command_line (enum command_control_type type
, char *arg
)
101 struct command_line
*cmd
;
102 struct cleanup
*old_chain
= NULL
;
104 /* Allocate and build a new command line structure. */
105 cmd
= build_command_line (type
, arg
);
107 old_chain
= make_cleanup_free_command_lines (&cmd
);
109 /* Read in the body of this command. */
110 if (recurse_read_control_structure (cmd
) == invalid_control
)
112 warning (_("Error reading in control structure."));
113 do_cleanups (old_chain
);
117 discard_cleanups (old_chain
);
121 /* Recursively print a command (including full control structures). */
124 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
127 struct command_line
*list
;
134 ui_out_spaces (uiout
, 2 * depth
);
136 /* A simple command, print it and continue. */
137 if (list
->control_type
== simple_control
)
139 ui_out_field_string (uiout
, NULL
, list
->line
);
140 ui_out_text (uiout
, "\n");
145 /* loop_continue to jump to the start of a while loop, print it
147 if (list
->control_type
== continue_control
)
149 ui_out_field_string (uiout
, NULL
, "loop_continue");
150 ui_out_text (uiout
, "\n");
155 /* loop_break to break out of a while loop, print it and continue. */
156 if (list
->control_type
== break_control
)
158 ui_out_field_string (uiout
, NULL
, "loop_break");
159 ui_out_text (uiout
, "\n");
164 /* A while command. Recursively print its subcommands and continue. */
165 if (list
->control_type
== while_control
)
167 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
168 ui_out_text (uiout
, "\n");
169 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
171 ui_out_spaces (uiout
, 2 * depth
);
172 ui_out_field_string (uiout
, NULL
, "end");
173 ui_out_text (uiout
, "\n");
178 /* An if command. Recursively print both arms before continueing. */
179 if (list
->control_type
== if_control
)
181 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
182 ui_out_text (uiout
, "\n");
184 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
186 /* Show the false arm if it exists. */
187 if (list
->body_count
== 2)
190 ui_out_spaces (uiout
, 2 * depth
);
191 ui_out_field_string (uiout
, NULL
, "else");
192 ui_out_text (uiout
, "\n");
193 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
197 ui_out_spaces (uiout
, 2 * depth
);
198 ui_out_field_string (uiout
, NULL
, "end");
199 ui_out_text (uiout
, "\n");
204 /* ignore illegal command type and try next */
209 /* Handle pre-post hooks. */
212 clear_hook_in_cleanup (void *data
)
214 struct cmd_list_element
*c
= data
;
215 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
219 execute_cmd_pre_hook (struct cmd_list_element
*c
)
221 if ((c
->hook_pre
) && (!c
->hook_in
))
223 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
224 c
->hook_in
= 1; /* Prevent recursive hooking */
225 execute_user_command (c
->hook_pre
, (char *) 0);
226 do_cleanups (cleanups
);
231 execute_cmd_post_hook (struct cmd_list_element
*c
)
233 if ((c
->hook_post
) && (!c
->hook_in
))
235 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
236 c
->hook_in
= 1; /* Prevent recursive hooking */
237 execute_user_command (c
->hook_post
, (char *) 0);
238 do_cleanups (cleanups
);
242 /* Execute the command in CMD. */
244 do_restore_user_call_depth (void * call_depth
)
246 int * depth
= call_depth
;
254 execute_user_command (struct cmd_list_element
*c
, char *args
)
256 struct command_line
*cmdlines
;
257 struct cleanup
*old_chain
;
258 enum command_control_type ret
;
259 static int user_call_depth
= 0;
260 extern int max_user_call_depth
;
262 old_chain
= setup_user_args (args
);
264 cmdlines
= c
->user_commands
;
269 if (++user_call_depth
> max_user_call_depth
)
270 error (_("Max user call depth exceeded -- command aborted."));
272 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
274 /* Set the instream to 0, indicating execution of a
275 user-defined function. */
276 make_cleanup (do_restore_instream_cleanup
, instream
);
277 instream
= (FILE *) 0;
279 /* Also set the global in_user_command, so that NULL instream is
280 not confused with Insight. */
285 ret
= execute_control_command (cmdlines
);
286 if (ret
!= simple_control
&& ret
!= break_control
)
288 warning (_("Error in control structure."));
291 cmdlines
= cmdlines
->next
;
293 do_cleanups (old_chain
);
296 enum command_control_type
297 execute_control_command (struct command_line
*cmd
)
299 struct expression
*expr
;
300 struct command_line
*current
;
301 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
303 struct value
*val_mark
;
305 enum command_control_type ret
;
308 /* Start by assuming failure, if a problem is detected, the code
309 below will simply "break" out of the switch. */
310 ret
= invalid_control
;
312 switch (cmd
->control_type
)
315 /* A simple command, execute it and return. */
316 new_line
= insert_args (cmd
->line
);
319 make_cleanup (free_current_contents
, &new_line
);
320 execute_command (new_line
, 0);
321 ret
= cmd
->control_type
;
324 case continue_control
:
326 /* Return for "continue", and "break" so we can either
327 continue the loop at the top, or break out. */
328 ret
= cmd
->control_type
;
333 /* Parse the loop control expression for the while statement. */
334 new_line
= insert_args (cmd
->line
);
337 make_cleanup (free_current_contents
, &new_line
);
338 expr
= parse_expression (new_line
);
339 make_cleanup (free_current_contents
, &expr
);
341 ret
= simple_control
;
344 /* Keep iterating so long as the expression is true. */
351 /* Evaluate the expression. */
352 val_mark
= value_mark ();
353 val
= evaluate_expression (expr
);
354 cond_result
= value_true (val
);
355 value_free_to_mark (val_mark
);
357 /* If the value is false, then break out of the loop. */
361 /* Execute the body of the while statement. */
362 current
= *cmd
->body_list
;
365 ret
= execute_control_command (current
);
367 /* If we got an error, or a "break" command, then stop
369 if (ret
== invalid_control
|| ret
== break_control
)
375 /* If we got a "continue" command, then restart the loop
377 if (ret
== continue_control
)
380 /* Get the next statement. */
381 current
= current
->next
;
385 /* Reset RET so that we don't recurse the break all the way down. */
386 if (ret
== break_control
)
387 ret
= simple_control
;
394 new_line
= insert_args (cmd
->line
);
397 make_cleanup (free_current_contents
, &new_line
);
398 /* Parse the conditional for the if statement. */
399 expr
= parse_expression (new_line
);
400 make_cleanup (free_current_contents
, &expr
);
403 ret
= simple_control
;
405 /* Evaluate the conditional. */
406 val_mark
= value_mark ();
407 val
= evaluate_expression (expr
);
409 /* Choose which arm to take commands from based on the value of the
410 conditional expression. */
411 if (value_true (val
))
412 current
= *cmd
->body_list
;
413 else if (cmd
->body_count
== 2)
414 current
= *(cmd
->body_list
+ 1);
415 value_free_to_mark (val_mark
);
417 /* Execute commands in the given arm. */
420 ret
= execute_control_command (current
);
422 /* If we got an error, get out. */
423 if (ret
!= simple_control
)
426 /* Get the next statement in the body. */
427 current
= current
->next
;
434 warning (_("Invalid control type in command structure."));
438 do_cleanups (old_chain
);
443 /* "while" command support. Executes a body of statements while the
444 loop condition is nonzero. */
447 while_command (char *arg
, int from_tty
)
449 struct command_line
*command
= NULL
;
452 command
= get_command_line (while_control
, arg
);
457 execute_control_command (command
);
458 free_command_lines (&command
);
461 /* "if" command support. Execute either the true or false arm depending
462 on the value of the if conditional. */
465 if_command (char *arg
, int from_tty
)
467 struct command_line
*command
= NULL
;
470 command
= get_command_line (if_control
, arg
);
475 execute_control_command (command
);
476 free_command_lines (&command
);
481 arg_cleanup (void *ignore
)
483 struct user_args
*oargs
= user_args
;
485 internal_error (__FILE__
, __LINE__
,
486 _("arg_cleanup called with no user args.\n"));
488 user_args
= user_args
->next
;
489 xfree (oargs
->command
);
493 /* Bind the incomming arguments for a user defined command to
494 $arg0, $arg1 ... $argMAXUSERARGS. */
496 static struct cleanup
*
497 setup_user_args (char *p
)
499 struct user_args
*args
;
500 struct cleanup
*old_chain
;
501 unsigned int arg_count
= 0;
503 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
504 memset (args
, 0, sizeof (struct user_args
));
506 args
->next
= user_args
;
509 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
514 user_args
->command
= p
= xstrdup (p
);
523 if (arg_count
>= MAXUSERARGS
)
525 error (_("user defined function may only have %d arguments."),
530 /* Strip whitespace. */
531 while (*p
== ' ' || *p
== '\t')
534 /* P now points to an argument. */
536 user_args
->a
[arg_count
].arg
= p
;
538 /* Get to the end of this argument. */
541 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
570 user_args
->a
[arg_count
].len
= p
- start_arg
;
577 /* Given character string P, return a point to the first argument ($arg),
578 or NULL if P contains no arguments. */
583 while ((p
= strchr (p
, '$')))
585 if (strncmp (p
, "$arg", 4) == 0
586 && (isdigit (p
[4]) || p
[4] == 'c'))
593 /* Insert the user defined arguments stored in user_arg into the $arg
594 arguments found in line, with the updated copy being placed into nline. */
597 insert_args (char *line
)
599 char *p
, *save_line
, *new_line
;
602 /* If we are not in a user-defined function, treat $argc, $arg0, et
603 cetera as normal convenience variables. */
604 if (user_args
== NULL
)
605 return xstrdup (line
);
607 /* First we need to know how much memory to allocate for the new line. */
610 while ((p
= locate_arg (line
)))
617 /* $argc. Number will be <=10. */
618 len
+= user_args
->count
== 10 ? 2 : 1;
620 else if (i
>= user_args
->count
)
622 error (_("Missing argument %d in user function."), i
);
627 len
+= user_args
->a
[i
].len
;
632 /* Don't forget the tail. */
633 len
+= strlen (line
);
635 /* Allocate space for the new line and fill it in. */
636 new_line
= (char *) xmalloc (len
+ 1);
637 if (new_line
== NULL
)
640 /* Restore pointer to beginning of old line. */
643 /* Save pointer to beginning of new line. */
644 save_line
= new_line
;
646 while ((p
= locate_arg (line
)))
650 memcpy (new_line
, line
, p
- line
);
651 new_line
+= p
- line
;
655 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
656 if (user_args
->count
== 10)
662 *(new_line
++) = user_args
->count
+ '0';
667 len
= user_args
->a
[i
].len
;
670 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
676 /* Don't forget the tail. */
677 strcpy (new_line
, line
);
679 /* Return a pointer to the beginning of the new line. */
684 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
685 code bodies. This is typically used when we encounter an "else"
686 clause for an "if" command. */
689 realloc_body_list (struct command_line
*command
, int new_length
)
692 struct command_line
**body_list
;
694 n
= command
->body_count
;
700 body_list
= (struct command_line
**)
701 xmalloc (sizeof (struct command_line
*) * new_length
);
703 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
705 xfree (command
->body_list
);
706 command
->body_list
= body_list
;
707 command
->body_count
= new_length
;
710 /* Read one line from the input stream. If the command is an "else" or
711 "end", return such an indication to the caller. */
713 static enum misc_command_type
714 read_next_line (struct command_line
**command
)
716 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
719 if (control_level
>= 254)
720 error (_("Control nesting too deep!"));
722 /* Set a prompt based on the nesting of the control commands. */
723 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
725 for (i
= 0; i
< control_level
; i
++)
726 control_prompt
[i
] = ' ';
727 control_prompt
[i
] = '>';
728 control_prompt
[i
+ 1] = '\0';
729 prompt_ptr
= (char *) &control_prompt
[0];
734 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
736 /* Not sure what to do here. */
740 /* Strip leading and trailing whitespace. */
741 while (*p
== ' ' || *p
== '\t')
745 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
748 /* Blanks and comments don't really do anything, but we need to
749 distinguish them from else, end and other commands which can be
751 if (p1
== p
|| p
[0] == '#')
754 /* Is this the end of a simple, while, or if control structure? */
755 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
758 /* Is the else clause of an if control structure? */
759 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
762 /* Check for while, if, break, continue, etc and build a new command
763 line structure for them. */
764 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
768 while (first_arg
< p1
&& isspace (*first_arg
))
770 *command
= build_command_line (while_control
, first_arg
);
772 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
776 while (first_arg
< p1
&& isspace (*first_arg
))
778 *command
= build_command_line (if_control
, first_arg
);
780 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
782 *command
= (struct command_line
*)
783 xmalloc (sizeof (struct command_line
));
784 (*command
)->next
= NULL
;
785 (*command
)->line
= NULL
;
786 (*command
)->control_type
= break_control
;
787 (*command
)->body_count
= 0;
788 (*command
)->body_list
= NULL
;
790 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
792 *command
= (struct command_line
*)
793 xmalloc (sizeof (struct command_line
));
794 (*command
)->next
= NULL
;
795 (*command
)->line
= NULL
;
796 (*command
)->control_type
= continue_control
;
797 (*command
)->body_count
= 0;
798 (*command
)->body_list
= NULL
;
802 /* A normal command. */
803 *command
= (struct command_line
*)
804 xmalloc (sizeof (struct command_line
));
805 (*command
)->next
= NULL
;
806 (*command
)->line
= savestring (p
, p1
- p
);
807 (*command
)->control_type
= simple_control
;
808 (*command
)->body_count
= 0;
809 (*command
)->body_list
= NULL
;
812 /* Nothing special. */
816 /* Recursively read in the control structures and create a command_line
819 The parent_control parameter is the control structure in which the
820 following commands are nested. */
822 static enum command_control_type
823 recurse_read_control_structure (struct command_line
*current_cmd
)
826 enum misc_command_type val
;
827 enum command_control_type ret
;
828 struct command_line
**body_ptr
, *child_tail
, *next
;
834 if (current_cmd
->control_type
== simple_control
)
835 error (_("Recursed on a simple control type."));
837 if (current_body
> current_cmd
->body_count
)
838 error (_("Allocated body is smaller than this command type needs."));
840 /* Read lines from the input stream and build control structures. */
846 val
= read_next_line (&next
);
848 /* Just skip blanks and comments. */
849 if (val
== nop_command
)
852 if (val
== end_command
)
854 if (current_cmd
->control_type
== while_control
855 || current_cmd
->control_type
== if_control
)
857 /* Success reading an entire control structure. */
858 ret
= simple_control
;
863 ret
= invalid_control
;
868 /* Not the end of a control structure. */
869 if (val
== else_command
)
871 if (current_cmd
->control_type
== if_control
872 && current_body
== 1)
874 realloc_body_list (current_cmd
, 2);
881 ret
= invalid_control
;
888 child_tail
->next
= next
;
892 body_ptr
= current_cmd
->body_list
;
893 for (i
= 1; i
< current_body
; i
++)
902 /* If the latest line is another control structure, then recurse
904 if (next
->control_type
== while_control
905 || next
->control_type
== if_control
)
908 ret
= recurse_read_control_structure (next
);
911 if (ret
!= simple_control
)
921 /* Read lines from the input stream and accumulate them in a chain of
922 struct command_line's, which is then returned. For input from a
923 terminal, the special command "end" is used to mark the end of the
924 input, and is not included in the returned chain of commands. */
926 #define END_MESSAGE "End with a line saying just \"end\"."
928 struct command_line
*
929 read_command_lines (char *prompt_arg
, int from_tty
)
931 struct command_line
*head
, *tail
, *next
;
932 struct cleanup
*old_chain
;
933 enum command_control_type ret
;
934 enum misc_command_type val
;
938 if (from_tty
&& input_from_terminal_p ())
940 if (deprecated_readline_begin_hook
)
942 /* Note - intentional to merge messages with no newline */
943 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
947 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
948 gdb_flush (gdb_stdout
);
957 val
= read_next_line (&next
);
959 /* Ignore blank lines or comments. */
960 if (val
== nop_command
)
963 if (val
== end_command
)
965 ret
= simple_control
;
969 if (val
!= ok_command
)
971 ret
= invalid_control
;
975 if (next
->control_type
== while_control
976 || next
->control_type
== if_control
)
979 ret
= recurse_read_control_structure (next
);
982 if (ret
== invalid_control
)
993 old_chain
= make_cleanup_free_command_lines (&head
);
1002 if (ret
!= invalid_control
)
1004 discard_cleanups (old_chain
);
1007 do_cleanups (old_chain
);
1010 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1012 (*deprecated_readline_end_hook
) ();
1017 /* Free a chain of struct command_line's. */
1020 free_command_lines (struct command_line
**lptr
)
1022 struct command_line
*l
= *lptr
;
1023 struct command_line
*next
;
1024 struct command_line
**blist
;
1029 if (l
->body_count
> 0)
1031 blist
= l
->body_list
;
1032 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1033 free_command_lines (blist
);
1044 do_free_command_lines_cleanup (void *arg
)
1046 free_command_lines (arg
);
1050 make_cleanup_free_command_lines (struct command_line
**arg
)
1052 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1055 struct command_line
*
1056 copy_command_lines (struct command_line
*cmds
)
1058 struct command_line
*result
= NULL
;
1062 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1064 result
->next
= copy_command_lines (cmds
->next
);
1065 result
->line
= xstrdup (cmds
->line
);
1066 result
->control_type
= cmds
->control_type
;
1067 result
->body_count
= cmds
->body_count
;
1068 if (cmds
->body_count
> 0)
1072 result
->body_list
= (struct command_line
**)
1073 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1075 for (i
= 0; i
< cmds
->body_count
; i
++)
1076 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1079 result
->body_list
= NULL
;
1086 validate_comname (char *comname
)
1091 error_no_arg (_("name of command to define"));
1096 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1097 error (_("Junk in argument list: \"%s\""), p
);
1102 /* This is just a placeholder in the command data structures. */
1104 user_defined_command (char *ignore
, int from_tty
)
1109 define_command (char *comname
, int from_tty
)
1111 #define MAX_TMPBUF 128
1118 struct command_line
*cmds
;
1119 struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1120 char *tem
= comname
;
1122 char tmpbuf
[MAX_TMPBUF
];
1123 int hook_type
= CMD_NO_HOOK
;
1124 int hook_name_size
= 0;
1126 #define HOOK_STRING "hook-"
1128 #define HOOK_POST_STRING "hookpost-"
1129 #define HOOK_POST_LEN 9
1131 validate_comname (comname
);
1133 /* Look it up, and verify that we got an exact match. */
1134 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1135 if (c
&& strcmp (comname
, c
->name
) != 0)
1141 if (c
->class == class_user
|| c
->class == class_alias
)
1142 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1144 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1146 error (_("Command \"%s\" not redefined."), c
->name
);
1149 /* If this new command is a hook, then mark the command which it
1150 is hooking. Note that we allow hooking `help' commands, so that
1151 we can hook the `stop' pseudo-command. */
1153 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1155 hook_type
= CMD_PRE_HOOK
;
1156 hook_name_size
= HOOK_LEN
;
1158 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1160 hook_type
= CMD_POST_HOOK
;
1161 hook_name_size
= HOOK_POST_LEN
;
1164 if (hook_type
!= CMD_NO_HOOK
)
1166 /* Look up cmd it hooks, and verify that we got an exact match. */
1167 tem
= comname
+ hook_name_size
;
1168 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1169 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1173 warning (_("Your new `%s' command does not hook any existing command."),
1175 if (!query ("Proceed? "))
1176 error (_("Not confirmed."));
1180 comname
= savestring (comname
, strlen (comname
));
1182 /* If the rest of the commands will be case insensitive, this one
1183 should behave in the same manner. */
1184 for (tem
= comname
; *tem
; tem
++)
1186 *tem
= tolower (*tem
);
1188 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1189 cmds
= read_command_lines (tmpbuf
, from_tty
);
1191 if (c
&& c
->class == class_user
)
1192 free_command_lines (&c
->user_commands
);
1194 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1195 (c
&& c
->class == class_user
)
1196 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1197 newc
->user_commands
= cmds
;
1199 /* If this new command is a hook, then mark both commands as being
1206 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1207 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1210 hookc
->hook_post
= newc
; /* Target gets hooked. */
1211 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1214 /* Should never come here as hookc would be 0. */
1215 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1221 document_command (char *comname
, int from_tty
)
1223 struct command_line
*doclines
;
1224 struct cmd_list_element
*c
;
1225 char *tem
= comname
;
1228 validate_comname (comname
);
1230 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1232 if (c
->class != class_user
)
1233 error (_("Command \"%s\" is built-in."), comname
);
1235 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1236 doclines
= read_command_lines (tmpbuf
, from_tty
);
1242 struct command_line
*cl1
;
1245 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1246 len
+= strlen (cl1
->line
) + 1;
1248 c
->doc
= (char *) xmalloc (len
+ 1);
1251 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1253 strcat (c
->doc
, cl1
->line
);
1255 strcat (c
->doc
, "\n");
1259 free_command_lines (&doclines
);
1262 struct source_cleanup_lines_args
1269 source_cleanup_lines (void *args
)
1271 struct source_cleanup_lines_args
*p
=
1272 (struct source_cleanup_lines_args
*) args
;
1273 source_line_number
= p
->old_line
;
1274 source_file_name
= p
->old_file
;
1278 do_fclose_cleanup (void *stream
)
1283 struct wrapped_read_command_file_args
1289 wrapped_read_command_file (struct ui_out
*uiout
, void *data
)
1291 struct wrapped_read_command_file_args
*args
= data
;
1292 read_command_file (args
->stream
);
1295 /* Used to implement source_command */
1298 script_from_file (FILE *stream
, char *file
)
1300 struct cleanup
*old_cleanups
;
1301 struct source_cleanup_lines_args old_lines
;
1305 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1307 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1309 old_lines
.old_line
= source_line_number
;
1310 old_lines
.old_file
= source_file_name
;
1311 make_cleanup (source_cleanup_lines
, &old_lines
);
1312 source_line_number
= 0;
1313 source_file_name
= file
;
1314 /* This will get set every time we read a line. So it won't stay "" for
1316 error_pre_print
= "";
1319 struct gdb_exception e
;
1320 struct wrapped_read_command_file_args args
;
1321 args
.stream
= stream
;
1322 e
= catch_exception (uiout
, wrapped_read_command_file
, &args
,
1329 /* Re-throw the error, but with the file name information
1331 throw_error (e
.error
,
1332 _("%s:%d: Error in sourced command file:\n%s"),
1333 source_file_name
, source_line_number
, e
.message
);
1335 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1339 do_cleanups (old_cleanups
);
1343 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1345 struct command_line
*cmdlines
;
1347 cmdlines
= c
->user_commands
;
1350 fputs_filtered ("User command ", stream
);
1351 fputs_filtered (c
->name
, stream
);
1352 fputs_filtered (":\n", stream
);
1354 print_command_lines (uiout
, cmdlines
, 1);
1355 fputs_filtered ("\n", stream
);