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
;
68 /* Allocate, initialize a new command line structure for one of the
69 control commands (if/while). */
71 static struct command_line
*
72 build_command_line (enum command_control_type type
, char *args
)
74 struct command_line
*cmd
;
77 error (_("if/while commands require arguments."));
79 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
81 cmd
->control_type
= type
;
85 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
87 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
88 cmd
->line
= savestring (args
, strlen (args
));
92 /* Build and return a new command structure for the control commands
93 such as "if" and "while". */
95 static struct command_line
*
96 get_command_line (enum command_control_type type
, char *arg
)
98 struct command_line
*cmd
;
99 struct cleanup
*old_chain
= NULL
;
101 /* Allocate and build a new command line structure. */
102 cmd
= build_command_line (type
, arg
);
104 old_chain
= make_cleanup_free_command_lines (&cmd
);
106 /* Read in the body of this command. */
107 if (recurse_read_control_structure (cmd
) == invalid_control
)
109 warning (_("Error reading in control structure."));
110 do_cleanups (old_chain
);
114 discard_cleanups (old_chain
);
118 /* Recursively print a command (including full control structures). */
121 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
124 struct command_line
*list
;
131 ui_out_spaces (uiout
, 2 * depth
);
133 /* A simple command, print it and continue. */
134 if (list
->control_type
== simple_control
)
136 ui_out_field_string (uiout
, NULL
, list
->line
);
137 ui_out_text (uiout
, "\n");
142 /* loop_continue to jump to the start of a while loop, print it
144 if (list
->control_type
== continue_control
)
146 ui_out_field_string (uiout
, NULL
, "loop_continue");
147 ui_out_text (uiout
, "\n");
152 /* loop_break to break out of a while loop, print it and continue. */
153 if (list
->control_type
== break_control
)
155 ui_out_field_string (uiout
, NULL
, "loop_break");
156 ui_out_text (uiout
, "\n");
161 /* A while command. Recursively print its subcommands and continue. */
162 if (list
->control_type
== while_control
)
164 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
165 ui_out_text (uiout
, "\n");
166 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
168 ui_out_spaces (uiout
, 2 * depth
);
169 ui_out_field_string (uiout
, NULL
, "end");
170 ui_out_text (uiout
, "\n");
175 /* An if command. Recursively print both arms before continueing. */
176 if (list
->control_type
== if_control
)
178 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
179 ui_out_text (uiout
, "\n");
181 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
183 /* Show the false arm if it exists. */
184 if (list
->body_count
== 2)
187 ui_out_spaces (uiout
, 2 * depth
);
188 ui_out_field_string (uiout
, NULL
, "else");
189 ui_out_text (uiout
, "\n");
190 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
194 ui_out_spaces (uiout
, 2 * depth
);
195 ui_out_field_string (uiout
, NULL
, "end");
196 ui_out_text (uiout
, "\n");
201 /* ignore illegal command type and try next */
206 /* Handle pre-post hooks. */
209 clear_hook_in_cleanup (void *data
)
211 struct cmd_list_element
*c
= data
;
212 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
216 execute_cmd_pre_hook (struct cmd_list_element
*c
)
218 if ((c
->hook_pre
) && (!c
->hook_in
))
220 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
221 c
->hook_in
= 1; /* Prevent recursive hooking */
222 execute_user_command (c
->hook_pre
, (char *) 0);
223 do_cleanups (cleanups
);
228 execute_cmd_post_hook (struct cmd_list_element
*c
)
230 if ((c
->hook_post
) && (!c
->hook_in
))
232 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
233 c
->hook_in
= 1; /* Prevent recursive hooking */
234 execute_user_command (c
->hook_post
, (char *) 0);
235 do_cleanups (cleanups
);
239 /* Execute the command in CMD. */
241 do_restore_user_call_depth (void * call_depth
)
243 int * depth
= call_depth
;
251 execute_user_command (struct cmd_list_element
*c
, char *args
)
253 struct command_line
*cmdlines
;
254 struct cleanup
*old_chain
;
255 enum command_control_type ret
;
256 static int user_call_depth
= 0;
257 extern int max_user_call_depth
;
259 old_chain
= setup_user_args (args
);
261 cmdlines
= c
->user_commands
;
266 if (++user_call_depth
> max_user_call_depth
)
267 error (_("Max user call depth exceeded -- command aborted."));
269 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
271 /* Set the instream to 0, indicating execution of a
272 user-defined function. */
273 make_cleanup (do_restore_instream_cleanup
, instream
);
274 instream
= (FILE *) 0;
276 /* Also set the global in_user_command, so that NULL instream is
277 not confused with Insight. */
282 ret
= execute_control_command (cmdlines
);
283 if (ret
!= simple_control
&& ret
!= break_control
)
285 warning (_("Error in control structure."));
288 cmdlines
= cmdlines
->next
;
290 do_cleanups (old_chain
);
293 enum command_control_type
294 execute_control_command (struct command_line
*cmd
)
296 struct expression
*expr
;
297 struct command_line
*current
;
298 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
300 struct value
*val_mark
;
302 enum command_control_type ret
;
305 /* Start by assuming failure, if a problem is detected, the code
306 below will simply "break" out of the switch. */
307 ret
= invalid_control
;
309 switch (cmd
->control_type
)
312 /* A simple command, execute it and return. */
313 new_line
= insert_args (cmd
->line
);
316 make_cleanup (free_current_contents
, &new_line
);
317 execute_command (new_line
, 0);
318 ret
= cmd
->control_type
;
321 case continue_control
:
323 /* Return for "continue", and "break" so we can either
324 continue the loop at the top, or break out. */
325 ret
= cmd
->control_type
;
330 /* Parse the loop control expression for the while statement. */
331 new_line
= insert_args (cmd
->line
);
334 make_cleanup (free_current_contents
, &new_line
);
335 expr
= parse_expression (new_line
);
336 make_cleanup (free_current_contents
, &expr
);
338 ret
= simple_control
;
341 /* Keep iterating so long as the expression is true. */
348 /* Evaluate the expression. */
349 val_mark
= value_mark ();
350 val
= evaluate_expression (expr
);
351 cond_result
= value_true (val
);
352 value_free_to_mark (val_mark
);
354 /* If the value is false, then break out of the loop. */
358 /* Execute the body of the while statement. */
359 current
= *cmd
->body_list
;
362 ret
= execute_control_command (current
);
364 /* If we got an error, or a "break" command, then stop
366 if (ret
== invalid_control
|| ret
== break_control
)
372 /* If we got a "continue" command, then restart the loop
374 if (ret
== continue_control
)
377 /* Get the next statement. */
378 current
= current
->next
;
382 /* Reset RET so that we don't recurse the break all the way down. */
383 if (ret
== break_control
)
384 ret
= simple_control
;
391 new_line
= insert_args (cmd
->line
);
394 make_cleanup (free_current_contents
, &new_line
);
395 /* Parse the conditional for the if statement. */
396 expr
= parse_expression (new_line
);
397 make_cleanup (free_current_contents
, &expr
);
400 ret
= simple_control
;
402 /* Evaluate the conditional. */
403 val_mark
= value_mark ();
404 val
= evaluate_expression (expr
);
406 /* Choose which arm to take commands from based on the value of the
407 conditional expression. */
408 if (value_true (val
))
409 current
= *cmd
->body_list
;
410 else if (cmd
->body_count
== 2)
411 current
= *(cmd
->body_list
+ 1);
412 value_free_to_mark (val_mark
);
414 /* Execute commands in the given arm. */
417 ret
= execute_control_command (current
);
419 /* If we got an error, get out. */
420 if (ret
!= simple_control
)
423 /* Get the next statement in the body. */
424 current
= current
->next
;
431 warning (_("Invalid control type in command structure."));
435 do_cleanups (old_chain
);
440 /* "while" command support. Executes a body of statements while the
441 loop condition is nonzero. */
444 while_command (char *arg
, int from_tty
)
446 struct command_line
*command
= NULL
;
449 command
= get_command_line (while_control
, arg
);
454 execute_control_command (command
);
455 free_command_lines (&command
);
458 /* "if" command support. Execute either the true or false arm depending
459 on the value of the if conditional. */
462 if_command (char *arg
, int from_tty
)
464 struct command_line
*command
= NULL
;
467 command
= get_command_line (if_control
, arg
);
472 execute_control_command (command
);
473 free_command_lines (&command
);
478 arg_cleanup (void *ignore
)
480 struct user_args
*oargs
= user_args
;
482 internal_error (__FILE__
, __LINE__
,
483 _("arg_cleanup called with no user args.\n"));
485 user_args
= user_args
->next
;
489 /* Bind the incomming arguments for a user defined command to
490 $arg0, $arg1 ... $argMAXUSERARGS. */
492 static struct cleanup
*
493 setup_user_args (char *p
)
495 struct user_args
*args
;
496 struct cleanup
*old_chain
;
497 unsigned int arg_count
= 0;
499 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
500 memset (args
, 0, sizeof (struct user_args
));
502 args
->next
= user_args
;
505 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
517 if (arg_count
>= MAXUSERARGS
)
519 error (_("user defined function may only have %d arguments."),
524 /* Strip whitespace. */
525 while (*p
== ' ' || *p
== '\t')
528 /* P now points to an argument. */
530 user_args
->a
[arg_count
].arg
= p
;
532 /* Get to the end of this argument. */
535 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
564 user_args
->a
[arg_count
].len
= p
- start_arg
;
571 /* Given character string P, return a point to the first argument ($arg),
572 or NULL if P contains no arguments. */
577 while ((p
= strchr (p
, '$')))
579 if (strncmp (p
, "$arg", 4) == 0
580 && (isdigit (p
[4]) || p
[4] == 'c'))
587 /* Insert the user defined arguments stored in user_arg into the $arg
588 arguments found in line, with the updated copy being placed into nline. */
591 insert_args (char *line
)
593 char *p
, *save_line
, *new_line
;
596 /* First we need to know how much memory to allocate for the new line. */
599 while ((p
= locate_arg (line
)))
606 /* $argc. Number will be <=10. */
607 len
+= user_args
->count
== 10 ? 2 : 1;
609 else if (i
>= user_args
->count
)
611 error (_("Missing argument %d in user function."), i
);
616 len
+= user_args
->a
[i
].len
;
621 /* Don't forget the tail. */
622 len
+= strlen (line
);
624 /* Allocate space for the new line and fill it in. */
625 new_line
= (char *) xmalloc (len
+ 1);
626 if (new_line
== NULL
)
629 /* Restore pointer to beginning of old line. */
632 /* Save pointer to beginning of new line. */
633 save_line
= new_line
;
635 while ((p
= locate_arg (line
)))
639 memcpy (new_line
, line
, p
- line
);
640 new_line
+= p
- line
;
644 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
645 if (user_args
->count
== 10)
651 *(new_line
++) = user_args
->count
+ '0';
656 len
= user_args
->a
[i
].len
;
659 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
665 /* Don't forget the tail. */
666 strcpy (new_line
, line
);
668 /* Return a pointer to the beginning of the new line. */
673 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
674 code bodies. This is typically used when we encounter an "else"
675 clause for an "if" command. */
678 realloc_body_list (struct command_line
*command
, int new_length
)
681 struct command_line
**body_list
;
683 n
= command
->body_count
;
689 body_list
= (struct command_line
**)
690 xmalloc (sizeof (struct command_line
*) * new_length
);
692 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
694 xfree (command
->body_list
);
695 command
->body_list
= body_list
;
696 command
->body_count
= new_length
;
699 /* Read one line from the input stream. If the command is an "else" or
700 "end", return such an indication to the caller. */
702 static enum misc_command_type
703 read_next_line (struct command_line
**command
)
705 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
708 if (control_level
>= 254)
709 error (_("Control nesting too deep!"));
711 /* Set a prompt based on the nesting of the control commands. */
712 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
714 for (i
= 0; i
< control_level
; i
++)
715 control_prompt
[i
] = ' ';
716 control_prompt
[i
] = '>';
717 control_prompt
[i
+ 1] = '\0';
718 prompt_ptr
= (char *) &control_prompt
[0];
723 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
725 /* Not sure what to do here. */
729 /* Strip leading and trailing whitespace. */
730 while (*p
== ' ' || *p
== '\t')
734 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
737 /* Blanks and comments don't really do anything, but we need to
738 distinguish them from else, end and other commands which can be
740 if (p1
== p
|| p
[0] == '#')
743 /* Is this the end of a simple, while, or if control structure? */
744 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
747 /* Is the else clause of an if control structure? */
748 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
751 /* Check for while, if, break, continue, etc and build a new command
752 line structure for them. */
753 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
757 while (first_arg
< p1
&& isspace (*first_arg
))
759 *command
= build_command_line (while_control
, first_arg
);
761 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
765 while (first_arg
< p1
&& isspace (*first_arg
))
767 *command
= build_command_line (if_control
, first_arg
);
769 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
771 *command
= (struct command_line
*)
772 xmalloc (sizeof (struct command_line
));
773 (*command
)->next
= NULL
;
774 (*command
)->line
= NULL
;
775 (*command
)->control_type
= break_control
;
776 (*command
)->body_count
= 0;
777 (*command
)->body_list
= NULL
;
779 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
781 *command
= (struct command_line
*)
782 xmalloc (sizeof (struct command_line
));
783 (*command
)->next
= NULL
;
784 (*command
)->line
= NULL
;
785 (*command
)->control_type
= continue_control
;
786 (*command
)->body_count
= 0;
787 (*command
)->body_list
= NULL
;
791 /* A normal command. */
792 *command
= (struct command_line
*)
793 xmalloc (sizeof (struct command_line
));
794 (*command
)->next
= NULL
;
795 (*command
)->line
= savestring (p
, p1
- p
);
796 (*command
)->control_type
= simple_control
;
797 (*command
)->body_count
= 0;
798 (*command
)->body_list
= NULL
;
801 /* Nothing special. */
805 /* Recursively read in the control structures and create a command_line
808 The parent_control parameter is the control structure in which the
809 following commands are nested. */
811 static enum command_control_type
812 recurse_read_control_structure (struct command_line
*current_cmd
)
815 enum misc_command_type val
;
816 enum command_control_type ret
;
817 struct command_line
**body_ptr
, *child_tail
, *next
;
823 if (current_cmd
->control_type
== simple_control
)
824 error (_("Recursed on a simple control type."));
826 if (current_body
> current_cmd
->body_count
)
827 error (_("Allocated body is smaller than this command type needs."));
829 /* Read lines from the input stream and build control structures. */
835 val
= read_next_line (&next
);
837 /* Just skip blanks and comments. */
838 if (val
== nop_command
)
841 if (val
== end_command
)
843 if (current_cmd
->control_type
== while_control
844 || current_cmd
->control_type
== if_control
)
846 /* Success reading an entire control structure. */
847 ret
= simple_control
;
852 ret
= invalid_control
;
857 /* Not the end of a control structure. */
858 if (val
== else_command
)
860 if (current_cmd
->control_type
== if_control
861 && current_body
== 1)
863 realloc_body_list (current_cmd
, 2);
870 ret
= invalid_control
;
877 child_tail
->next
= next
;
881 body_ptr
= current_cmd
->body_list
;
882 for (i
= 1; i
< current_body
; i
++)
891 /* If the latest line is another control structure, then recurse
893 if (next
->control_type
== while_control
894 || next
->control_type
== if_control
)
897 ret
= recurse_read_control_structure (next
);
900 if (ret
!= simple_control
)
910 /* Read lines from the input stream and accumulate them in a chain of
911 struct command_line's, which is then returned. For input from a
912 terminal, the special command "end" is used to mark the end of the
913 input, and is not included in the returned chain of commands. */
915 #define END_MESSAGE "End with a line saying just \"end\"."
917 struct command_line
*
918 read_command_lines (char *prompt_arg
, int from_tty
)
920 struct command_line
*head
, *tail
, *next
;
921 struct cleanup
*old_chain
;
922 enum command_control_type ret
;
923 enum misc_command_type val
;
927 if (from_tty
&& input_from_terminal_p ())
929 if (deprecated_readline_begin_hook
)
931 /* Note - intentional to merge messages with no newline */
932 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
936 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
937 gdb_flush (gdb_stdout
);
946 val
= read_next_line (&next
);
948 /* Ignore blank lines or comments. */
949 if (val
== nop_command
)
952 if (val
== end_command
)
954 ret
= simple_control
;
958 if (val
!= ok_command
)
960 ret
= invalid_control
;
964 if (next
->control_type
== while_control
965 || next
->control_type
== if_control
)
968 ret
= recurse_read_control_structure (next
);
971 if (ret
== invalid_control
)
982 old_chain
= make_cleanup_free_command_lines (&head
);
991 if (ret
!= invalid_control
)
993 discard_cleanups (old_chain
);
996 do_cleanups (old_chain
);
999 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1001 (*deprecated_readline_end_hook
) ();
1006 /* Free a chain of struct command_line's. */
1009 free_command_lines (struct command_line
**lptr
)
1011 struct command_line
*l
= *lptr
;
1012 struct command_line
*next
;
1013 struct command_line
**blist
;
1018 if (l
->body_count
> 0)
1020 blist
= l
->body_list
;
1021 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1022 free_command_lines (blist
);
1033 do_free_command_lines_cleanup (void *arg
)
1035 free_command_lines (arg
);
1039 make_cleanup_free_command_lines (struct command_line
**arg
)
1041 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1044 struct command_line
*
1045 copy_command_lines (struct command_line
*cmds
)
1047 struct command_line
*result
= NULL
;
1051 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1053 result
->next
= copy_command_lines (cmds
->next
);
1054 result
->line
= xstrdup (cmds
->line
);
1055 result
->control_type
= cmds
->control_type
;
1056 result
->body_count
= cmds
->body_count
;
1057 if (cmds
->body_count
> 0)
1061 result
->body_list
= (struct command_line
**)
1062 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1064 for (i
= 0; i
< cmds
->body_count
; i
++)
1065 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1068 result
->body_list
= NULL
;
1075 validate_comname (char *comname
)
1080 error_no_arg (_("name of command to define"));
1085 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1086 error (_("Junk in argument list: \"%s\""), p
);
1091 /* This is just a placeholder in the command data structures. */
1093 user_defined_command (char *ignore
, int from_tty
)
1098 define_command (char *comname
, int from_tty
)
1100 #define MAX_TMPBUF 128
1107 struct command_line
*cmds
;
1108 struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1109 char *tem
= comname
;
1111 char tmpbuf
[MAX_TMPBUF
];
1112 int hook_type
= CMD_NO_HOOK
;
1113 int hook_name_size
= 0;
1115 #define HOOK_STRING "hook-"
1117 #define HOOK_POST_STRING "hookpost-"
1118 #define HOOK_POST_LEN 9
1120 validate_comname (comname
);
1122 /* Look it up, and verify that we got an exact match. */
1123 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1124 if (c
&& strcmp (comname
, c
->name
) != 0)
1130 if (c
->class == class_user
|| c
->class == class_alias
)
1131 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1133 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1135 error (_("Command \"%s\" not redefined."), c
->name
);
1138 /* If this new command is a hook, then mark the command which it
1139 is hooking. Note that we allow hooking `help' commands, so that
1140 we can hook the `stop' pseudo-command. */
1142 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1144 hook_type
= CMD_PRE_HOOK
;
1145 hook_name_size
= HOOK_LEN
;
1147 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1149 hook_type
= CMD_POST_HOOK
;
1150 hook_name_size
= HOOK_POST_LEN
;
1153 if (hook_type
!= CMD_NO_HOOK
)
1155 /* Look up cmd it hooks, and verify that we got an exact match. */
1156 tem
= comname
+ hook_name_size
;
1157 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1158 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1162 warning (_("Your new `%s' command does not hook any existing command."),
1164 if (!query ("Proceed? "))
1165 error (_("Not confirmed."));
1169 comname
= savestring (comname
, strlen (comname
));
1171 /* If the rest of the commands will be case insensitive, this one
1172 should behave in the same manner. */
1173 for (tem
= comname
; *tem
; tem
++)
1175 *tem
= tolower (*tem
);
1177 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1178 cmds
= read_command_lines (tmpbuf
, from_tty
);
1180 if (c
&& c
->class == class_user
)
1181 free_command_lines (&c
->user_commands
);
1183 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1184 (c
&& c
->class == class_user
)
1185 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1186 newc
->user_commands
= cmds
;
1188 /* If this new command is a hook, then mark both commands as being
1195 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1196 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1199 hookc
->hook_post
= newc
; /* Target gets hooked. */
1200 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1203 /* Should never come here as hookc would be 0. */
1204 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1210 document_command (char *comname
, int from_tty
)
1212 struct command_line
*doclines
;
1213 struct cmd_list_element
*c
;
1214 char *tem
= comname
;
1217 validate_comname (comname
);
1219 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1221 if (c
->class != class_user
)
1222 error (_("Command \"%s\" is built-in."), comname
);
1224 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1225 doclines
= read_command_lines (tmpbuf
, from_tty
);
1231 struct command_line
*cl1
;
1234 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1235 len
+= strlen (cl1
->line
) + 1;
1237 c
->doc
= (char *) xmalloc (len
+ 1);
1240 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1242 strcat (c
->doc
, cl1
->line
);
1244 strcat (c
->doc
, "\n");
1248 free_command_lines (&doclines
);
1251 struct source_cleanup_lines_args
1258 source_cleanup_lines (void *args
)
1260 struct source_cleanup_lines_args
*p
=
1261 (struct source_cleanup_lines_args
*) args
;
1262 source_line_number
= p
->old_line
;
1263 source_file_name
= p
->old_file
;
1267 do_fclose_cleanup (void *stream
)
1272 struct wrapped_read_command_file_args
1278 wrapped_read_command_file (struct ui_out
*uiout
, void *data
)
1280 struct wrapped_read_command_file_args
*args
= data
;
1281 read_command_file (args
->stream
);
1284 /* Used to implement source_command */
1287 script_from_file (FILE *stream
, char *file
)
1289 struct cleanup
*old_cleanups
;
1290 struct source_cleanup_lines_args old_lines
;
1294 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1296 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1298 old_lines
.old_line
= source_line_number
;
1299 old_lines
.old_file
= source_file_name
;
1300 make_cleanup (source_cleanup_lines
, &old_lines
);
1301 source_line_number
= 0;
1302 source_file_name
= file
;
1303 /* This will get set every time we read a line. So it won't stay "" for
1305 error_pre_print
= "";
1308 struct gdb_exception e
;
1309 struct wrapped_read_command_file_args args
;
1310 args
.stream
= stream
;
1311 e
= catch_exception (uiout
, wrapped_read_command_file
, &args
,
1318 /* Re-throw the error, but with the file name information
1320 throw_error (e
.error
,
1321 _("%s:%d: Error in sourced command file:\n%s"),
1322 source_file_name
, source_line_number
, e
.message
);
1324 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1328 do_cleanups (old_cleanups
);
1332 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1334 struct command_line
*cmdlines
;
1336 cmdlines
= c
->user_commands
;
1339 fputs_filtered ("User command ", stream
);
1340 fputs_filtered (c
->name
, stream
);
1341 fputs_filtered (":\n", stream
);
1343 print_command_lines (uiout
, cmdlines
, 1);
1344 fputs_filtered ("\n", stream
);