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 Free
5 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
;
244 /* We will be returning_to_top_level() at this point, so we want to
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 old_chain
= 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 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
274 instream
= (FILE *) 0;
277 ret
= execute_control_command (cmdlines
);
278 if (ret
!= simple_control
&& ret
!= break_control
)
280 warning (_("Error in control structure."));
283 cmdlines
= cmdlines
->next
;
285 do_cleanups (old_chain
);
290 enum command_control_type
291 execute_control_command (struct command_line
*cmd
)
293 struct expression
*expr
;
294 struct command_line
*current
;
295 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
297 struct value
*val_mark
;
299 enum command_control_type ret
;
302 /* Start by assuming failure, if a problem is detected, the code
303 below will simply "break" out of the switch. */
304 ret
= invalid_control
;
306 switch (cmd
->control_type
)
309 /* A simple command, execute it and return. */
310 new_line
= insert_args (cmd
->line
);
313 make_cleanup (free_current_contents
, &new_line
);
314 execute_command (new_line
, 0);
315 ret
= cmd
->control_type
;
318 case continue_control
:
320 /* Return for "continue", and "break" so we can either
321 continue the loop at the top, or break out. */
322 ret
= cmd
->control_type
;
327 /* Parse the loop control expression for the while statement. */
328 new_line
= insert_args (cmd
->line
);
331 make_cleanup (free_current_contents
, &new_line
);
332 expr
= parse_expression (new_line
);
333 make_cleanup (free_current_contents
, &expr
);
335 ret
= simple_control
;
338 /* Keep iterating so long as the expression is true. */
345 /* Evaluate the expression. */
346 val_mark
= value_mark ();
347 val
= evaluate_expression (expr
);
348 cond_result
= value_true (val
);
349 value_free_to_mark (val_mark
);
351 /* If the value is false, then break out of the loop. */
355 /* Execute the body of the while statement. */
356 current
= *cmd
->body_list
;
359 ret
= execute_control_command (current
);
361 /* If we got an error, or a "break" command, then stop
363 if (ret
== invalid_control
|| ret
== break_control
)
369 /* If we got a "continue" command, then restart the loop
371 if (ret
== continue_control
)
374 /* Get the next statement. */
375 current
= current
->next
;
379 /* Reset RET so that we don't recurse the break all the way down. */
380 if (ret
== break_control
)
381 ret
= simple_control
;
388 new_line
= insert_args (cmd
->line
);
391 make_cleanup (free_current_contents
, &new_line
);
392 /* Parse the conditional for the if statement. */
393 expr
= parse_expression (new_line
);
394 make_cleanup (free_current_contents
, &expr
);
397 ret
= simple_control
;
399 /* Evaluate the conditional. */
400 val_mark
= value_mark ();
401 val
= evaluate_expression (expr
);
403 /* Choose which arm to take commands from based on the value of the
404 conditional expression. */
405 if (value_true (val
))
406 current
= *cmd
->body_list
;
407 else if (cmd
->body_count
== 2)
408 current
= *(cmd
->body_list
+ 1);
409 value_free_to_mark (val_mark
);
411 /* Execute commands in the given arm. */
414 ret
= execute_control_command (current
);
416 /* If we got an error, get out. */
417 if (ret
!= simple_control
)
420 /* Get the next statement in the body. */
421 current
= current
->next
;
428 warning (_("Invalid control type in command structure."));
432 do_cleanups (old_chain
);
437 /* "while" command support. Executes a body of statements while the
438 loop condition is nonzero. */
441 while_command (char *arg
, int from_tty
)
443 struct command_line
*command
= NULL
;
446 command
= get_command_line (while_control
, arg
);
451 execute_control_command (command
);
452 free_command_lines (&command
);
455 /* "if" command support. Execute either the true or false arm depending
456 on the value of the if conditional. */
459 if_command (char *arg
, int from_tty
)
461 struct command_line
*command
= NULL
;
464 command
= get_command_line (if_control
, arg
);
469 execute_control_command (command
);
470 free_command_lines (&command
);
475 arg_cleanup (void *ignore
)
477 struct user_args
*oargs
= user_args
;
479 internal_error (__FILE__
, __LINE__
,
480 _("arg_cleanup called with no user args.\n"));
482 user_args
= user_args
->next
;
486 /* Bind the incomming arguments for a user defined command to
487 $arg0, $arg1 ... $argMAXUSERARGS. */
489 static struct cleanup
*
490 setup_user_args (char *p
)
492 struct user_args
*args
;
493 struct cleanup
*old_chain
;
494 unsigned int arg_count
= 0;
496 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
497 memset (args
, 0, sizeof (struct user_args
));
499 args
->next
= user_args
;
502 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
514 if (arg_count
>= MAXUSERARGS
)
516 error (_("user defined function may only have %d arguments."),
521 /* Strip whitespace. */
522 while (*p
== ' ' || *p
== '\t')
525 /* P now points to an argument. */
527 user_args
->a
[arg_count
].arg
= p
;
529 /* Get to the end of this argument. */
532 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
561 user_args
->a
[arg_count
].len
= p
- start_arg
;
568 /* Given character string P, return a point to the first argument ($arg),
569 or NULL if P contains no arguments. */
574 while ((p
= strchr (p
, '$')))
576 if (strncmp (p
, "$arg", 4) == 0
577 && (isdigit (p
[4]) || p
[4] == 'c'))
584 /* Insert the user defined arguments stored in user_arg into the $arg
585 arguments found in line, with the updated copy being placed into nline. */
588 insert_args (char *line
)
590 char *p
, *save_line
, *new_line
;
593 /* First we need to know how much memory to allocate for the new line. */
596 while ((p
= locate_arg (line
)))
603 /* $argc. Number will be <=10. */
604 len
+= user_args
->count
== 10 ? 2 : 1;
606 else if (i
>= user_args
->count
)
608 error (_("Missing argument %d in user function."), i
);
613 len
+= user_args
->a
[i
].len
;
618 /* Don't forget the tail. */
619 len
+= strlen (line
);
621 /* Allocate space for the new line and fill it in. */
622 new_line
= (char *) xmalloc (len
+ 1);
623 if (new_line
== NULL
)
626 /* Restore pointer to beginning of old line. */
629 /* Save pointer to beginning of new line. */
630 save_line
= new_line
;
632 while ((p
= locate_arg (line
)))
636 memcpy (new_line
, line
, p
- line
);
637 new_line
+= p
- line
;
641 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
642 if (user_args
->count
== 10)
648 *(new_line
++) = user_args
->count
+ '0';
653 len
= user_args
->a
[i
].len
;
656 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
662 /* Don't forget the tail. */
663 strcpy (new_line
, line
);
665 /* Return a pointer to the beginning of the new line. */
670 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
671 code bodies. This is typically used when we encounter an "else"
672 clause for an "if" command. */
675 realloc_body_list (struct command_line
*command
, int new_length
)
678 struct command_line
**body_list
;
680 n
= command
->body_count
;
686 body_list
= (struct command_line
**)
687 xmalloc (sizeof (struct command_line
*) * new_length
);
689 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
691 xfree (command
->body_list
);
692 command
->body_list
= body_list
;
693 command
->body_count
= new_length
;
696 /* Read one line from the input stream. If the command is an "else" or
697 "end", return such an indication to the caller. */
699 static enum misc_command_type
700 read_next_line (struct command_line
**command
)
702 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
705 if (control_level
>= 254)
706 error (_("Control nesting too deep!"));
708 /* Set a prompt based on the nesting of the control commands. */
709 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
711 for (i
= 0; i
< control_level
; i
++)
712 control_prompt
[i
] = ' ';
713 control_prompt
[i
] = '>';
714 control_prompt
[i
+ 1] = '\0';
715 prompt_ptr
= (char *) &control_prompt
[0];
720 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
722 /* Not sure what to do here. */
726 /* Strip leading and trailing whitespace. */
727 while (*p
== ' ' || *p
== '\t')
731 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
734 /* Blanks and comments don't really do anything, but we need to
735 distinguish them from else, end and other commands which can be
737 if (p1
== p
|| p
[0] == '#')
740 /* Is this the end of a simple, while, or if control structure? */
741 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
744 /* Is the else clause of an if control structure? */
745 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
748 /* Check for while, if, break, continue, etc and build a new command
749 line structure for them. */
750 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
754 while (first_arg
< p1
&& isspace (*first_arg
))
756 *command
= build_command_line (while_control
, first_arg
);
758 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
762 while (first_arg
< p1
&& isspace (*first_arg
))
764 *command
= build_command_line (if_control
, first_arg
);
766 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
768 *command
= (struct command_line
*)
769 xmalloc (sizeof (struct command_line
));
770 (*command
)->next
= NULL
;
771 (*command
)->line
= NULL
;
772 (*command
)->control_type
= break_control
;
773 (*command
)->body_count
= 0;
774 (*command
)->body_list
= NULL
;
776 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
778 *command
= (struct command_line
*)
779 xmalloc (sizeof (struct command_line
));
780 (*command
)->next
= NULL
;
781 (*command
)->line
= NULL
;
782 (*command
)->control_type
= continue_control
;
783 (*command
)->body_count
= 0;
784 (*command
)->body_list
= NULL
;
788 /* A normal command. */
789 *command
= (struct command_line
*)
790 xmalloc (sizeof (struct command_line
));
791 (*command
)->next
= NULL
;
792 (*command
)->line
= savestring (p
, p1
- p
);
793 (*command
)->control_type
= simple_control
;
794 (*command
)->body_count
= 0;
795 (*command
)->body_list
= NULL
;
798 /* Nothing special. */
802 /* Recursively read in the control structures and create a command_line
805 The parent_control parameter is the control structure in which the
806 following commands are nested. */
808 static enum command_control_type
809 recurse_read_control_structure (struct command_line
*current_cmd
)
812 enum misc_command_type val
;
813 enum command_control_type ret
;
814 struct command_line
**body_ptr
, *child_tail
, *next
;
820 if (current_cmd
->control_type
== simple_control
)
821 error (_("Recursed on a simple control type."));
823 if (current_body
> current_cmd
->body_count
)
824 error (_("Allocated body is smaller than this command type needs."));
826 /* Read lines from the input stream and build control structures. */
832 val
= read_next_line (&next
);
834 /* Just skip blanks and comments. */
835 if (val
== nop_command
)
838 if (val
== end_command
)
840 if (current_cmd
->control_type
== while_control
841 || current_cmd
->control_type
== if_control
)
843 /* Success reading an entire control structure. */
844 ret
= simple_control
;
849 ret
= invalid_control
;
854 /* Not the end of a control structure. */
855 if (val
== else_command
)
857 if (current_cmd
->control_type
== if_control
858 && current_body
== 1)
860 realloc_body_list (current_cmd
, 2);
867 ret
= invalid_control
;
874 child_tail
->next
= next
;
878 body_ptr
= current_cmd
->body_list
;
879 for (i
= 1; i
< current_body
; i
++)
888 /* If the latest line is another control structure, then recurse
890 if (next
->control_type
== while_control
891 || next
->control_type
== if_control
)
894 ret
= recurse_read_control_structure (next
);
897 if (ret
!= simple_control
)
907 /* Read lines from the input stream and accumulate them in a chain of
908 struct command_line's, which is then returned. For input from a
909 terminal, the special command "end" is used to mark the end of the
910 input, and is not included in the returned chain of commands. */
912 #define END_MESSAGE "End with a line saying just \"end\"."
914 struct command_line
*
915 read_command_lines (char *prompt_arg
, int from_tty
)
917 struct command_line
*head
, *tail
, *next
;
918 struct cleanup
*old_chain
;
919 enum command_control_type ret
;
920 enum misc_command_type val
;
923 if (deprecated_readline_begin_hook
)
925 /* Note - intentional to merge messages with no newline */
926 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
928 else if (from_tty
&& input_from_terminal_p ())
930 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
931 gdb_flush (gdb_stdout
);
939 val
= read_next_line (&next
);
941 /* Ignore blank lines or comments. */
942 if (val
== nop_command
)
945 if (val
== end_command
)
947 ret
= simple_control
;
951 if (val
!= ok_command
)
953 ret
= invalid_control
;
957 if (next
->control_type
== while_control
958 || next
->control_type
== if_control
)
961 ret
= recurse_read_control_structure (next
);
964 if (ret
== invalid_control
)
975 old_chain
= make_cleanup_free_command_lines (&head
);
984 if (ret
!= invalid_control
)
986 discard_cleanups (old_chain
);
989 do_cleanups (old_chain
);
992 if (deprecated_readline_end_hook
)
994 (*deprecated_readline_end_hook
) ();
999 /* Free a chain of struct command_line's. */
1002 free_command_lines (struct command_line
**lptr
)
1004 struct command_line
*l
= *lptr
;
1005 struct command_line
*next
;
1006 struct command_line
**blist
;
1011 if (l
->body_count
> 0)
1013 blist
= l
->body_list
;
1014 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1015 free_command_lines (blist
);
1026 do_free_command_lines_cleanup (void *arg
)
1028 free_command_lines (arg
);
1032 make_cleanup_free_command_lines (struct command_line
**arg
)
1034 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1037 struct command_line
*
1038 copy_command_lines (struct command_line
*cmds
)
1040 struct command_line
*result
= NULL
;
1044 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1046 result
->next
= copy_command_lines (cmds
->next
);
1047 result
->line
= xstrdup (cmds
->line
);
1048 result
->control_type
= cmds
->control_type
;
1049 result
->body_count
= cmds
->body_count
;
1050 if (cmds
->body_count
> 0)
1054 result
->body_list
= (struct command_line
**)
1055 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1057 for (i
= 0; i
< cmds
->body_count
; i
++)
1058 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1061 result
->body_list
= NULL
;
1068 validate_comname (char *comname
)
1073 error_no_arg (_("name of command to define"));
1078 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1079 error (_("Junk in argument list: \"%s\""), p
);
1084 /* This is just a placeholder in the command data structures. */
1086 user_defined_command (char *ignore
, int from_tty
)
1091 define_command (char *comname
, int from_tty
)
1093 #define MAX_TMPBUF 128
1100 struct command_line
*cmds
;
1101 struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1102 char *tem
= comname
;
1104 char tmpbuf
[MAX_TMPBUF
];
1105 int hook_type
= CMD_NO_HOOK
;
1106 int hook_name_size
= 0;
1108 #define HOOK_STRING "hook-"
1110 #define HOOK_POST_STRING "hookpost-"
1111 #define HOOK_POST_LEN 9
1113 validate_comname (comname
);
1115 /* Look it up, and verify that we got an exact match. */
1116 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1117 if (c
&& strcmp (comname
, c
->name
) != 0)
1123 if (c
->class == class_user
|| c
->class == class_alias
)
1124 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1126 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1128 error (_("Command \"%s\" not redefined."), c
->name
);
1131 /* If this new command is a hook, then mark the command which it
1132 is hooking. Note that we allow hooking `help' commands, so that
1133 we can hook the `stop' pseudo-command. */
1135 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1137 hook_type
= CMD_PRE_HOOK
;
1138 hook_name_size
= HOOK_LEN
;
1140 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1142 hook_type
= CMD_POST_HOOK
;
1143 hook_name_size
= HOOK_POST_LEN
;
1146 if (hook_type
!= CMD_NO_HOOK
)
1148 /* Look up cmd it hooks, and verify that we got an exact match. */
1149 tem
= comname
+ hook_name_size
;
1150 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1151 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1155 warning (_("Your new `%s' command does not hook any existing command."),
1157 if (!query ("Proceed? "))
1158 error (_("Not confirmed."));
1162 comname
= savestring (comname
, strlen (comname
));
1164 /* If the rest of the commands will be case insensitive, this one
1165 should behave in the same manner. */
1166 for (tem
= comname
; *tem
; tem
++)
1168 *tem
= tolower (*tem
);
1170 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1171 cmds
= read_command_lines (tmpbuf
, from_tty
);
1173 if (c
&& c
->class == class_user
)
1174 free_command_lines (&c
->user_commands
);
1176 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1177 (c
&& c
->class == class_user
)
1178 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1179 newc
->user_commands
= cmds
;
1181 /* If this new command is a hook, then mark both commands as being
1188 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1189 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1192 hookc
->hook_post
= newc
; /* Target gets hooked. */
1193 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1196 /* Should never come here as hookc would be 0. */
1197 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1203 document_command (char *comname
, int from_tty
)
1205 struct command_line
*doclines
;
1206 struct cmd_list_element
*c
;
1207 char *tem
= comname
;
1210 validate_comname (comname
);
1212 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1214 if (c
->class != class_user
)
1215 error (_("Command \"%s\" is built-in."), comname
);
1217 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1218 doclines
= read_command_lines (tmpbuf
, from_tty
);
1224 struct command_line
*cl1
;
1227 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1228 len
+= strlen (cl1
->line
) + 1;
1230 c
->doc
= (char *) xmalloc (len
+ 1);
1233 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1235 strcat (c
->doc
, cl1
->line
);
1237 strcat (c
->doc
, "\n");
1241 free_command_lines (&doclines
);
1244 struct source_cleanup_lines_args
1251 source_cleanup_lines (void *args
)
1253 struct source_cleanup_lines_args
*p
=
1254 (struct source_cleanup_lines_args
*) args
;
1255 source_line_number
= p
->old_line
;
1256 source_file_name
= p
->old_file
;
1260 do_fclose_cleanup (void *stream
)
1265 struct wrapped_read_command_file_args
1271 wrapped_read_command_file (struct ui_out
*uiout
, void *data
)
1273 struct wrapped_read_command_file_args
*args
= data
;
1274 read_command_file (args
->stream
);
1277 /* Used to implement source_command */
1280 script_from_file (FILE *stream
, char *file
)
1282 struct cleanup
*old_cleanups
;
1283 struct source_cleanup_lines_args old_lines
;
1287 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1289 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1291 old_lines
.old_line
= source_line_number
;
1292 old_lines
.old_file
= source_file_name
;
1293 make_cleanup (source_cleanup_lines
, &old_lines
);
1294 source_line_number
= 0;
1295 source_file_name
= file
;
1296 /* This will get set every time we read a line. So it won't stay "" for
1298 error_pre_print
= "";
1301 struct gdb_exception e
;
1302 struct wrapped_read_command_file_args args
;
1303 args
.stream
= stream
;
1304 e
= catch_exception (uiout
, wrapped_read_command_file
, &args
,
1311 /* Re-throw the error, but with the file name information
1313 throw_error (e
.error
,
1314 _("%s:%d: Error in sourced command file:\n%s"),
1315 source_file_name
, source_line_number
, e
.message
);
1317 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1321 do_cleanups (old_cleanups
);
1325 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1327 struct command_line
*cmdlines
;
1329 cmdlines
= c
->user_commands
;
1332 fputs_filtered ("User command ", stream
);
1333 fputs_filtered (c
->name
, stream
);
1334 fputs_filtered (":\n", stream
);
1336 print_command_lines (uiout
, cmdlines
, 1);
1337 fputs_filtered ("\n", stream
);