1 /* GDB CLI command scripting.
3 Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
26 #include "language.h" /* For value_true */
30 #include "gdb_string.h"
31 #include "exceptions.h"
33 #include "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. */
289 command_nest_depth
++;
292 ret
= execute_control_command (cmdlines
);
293 if (ret
!= simple_control
&& ret
!= break_control
)
295 warning (_("Error in control structure."));
298 cmdlines
= cmdlines
->next
;
300 command_nest_depth
--;
301 do_cleanups (old_chain
);
304 /* This function is called every time GDB prints a prompt.
305 It ensures that errors and the like to not confuse the command tracing. */
308 reset_command_nest_depth (void)
310 command_nest_depth
= 1;
313 suppress_next_print_command_trace
= 0;
316 /* Print the command, prefixed with '+' to represent the call depth.
317 This is slightly complicated because this function may be called
318 from execute_command and execute_control_command. Unfortunately
319 execute_command also prints the top level control commands.
320 In these cases execute_command will call execute_control_command
321 via while_command or if_command. Inner levels of 'if' and 'while'
322 are dealt with directly. Therefore we can use these functions
323 to determine whether the command has been printed already or not. */
325 print_command_trace (const char *cmd
)
329 if (suppress_next_print_command_trace
)
331 suppress_next_print_command_trace
= 0;
335 if (!source_verbose
&& !trace_commands
)
338 for (i
=0; i
< command_nest_depth
; i
++)
339 printf_filtered ("+");
341 printf_filtered ("%s\n", cmd
);
344 enum command_control_type
345 execute_control_command (struct command_line
*cmd
)
347 struct expression
*expr
;
348 struct command_line
*current
;
349 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
351 struct value
*val_mark
;
353 enum command_control_type ret
;
356 /* Start by assuming failure, if a problem is detected, the code
357 below will simply "break" out of the switch. */
358 ret
= invalid_control
;
360 switch (cmd
->control_type
)
363 /* A simple command, execute it and return. */
364 new_line
= insert_args (cmd
->line
);
367 make_cleanup (free_current_contents
, &new_line
);
368 execute_command (new_line
, 0);
369 ret
= cmd
->control_type
;
372 case continue_control
:
373 print_command_trace ("loop_continue");
375 /* Return for "continue", and "break" so we can either
376 continue the loop at the top, or break out. */
377 ret
= cmd
->control_type
;
381 print_command_trace ("loop_break");
383 /* Return for "continue", and "break" so we can either
384 continue the loop at the top, or break out. */
385 ret
= cmd
->control_type
;
390 char *buffer
= alloca (strlen (cmd
->line
) + 7);
391 sprintf (buffer
, "while %s", cmd
->line
);
392 print_command_trace (buffer
);
394 /* Parse the loop control expression for the while statement. */
395 new_line
= insert_args (cmd
->line
);
398 make_cleanup (free_current_contents
, &new_line
);
399 expr
= parse_expression (new_line
);
400 make_cleanup (free_current_contents
, &expr
);
402 ret
= simple_control
;
405 /* Keep iterating so long as the expression is true. */
412 /* Evaluate the expression. */
413 val_mark
= value_mark ();
414 val
= evaluate_expression (expr
);
415 cond_result
= value_true (val
);
416 value_free_to_mark (val_mark
);
418 /* If the value is false, then break out of the loop. */
422 /* Execute the body of the while statement. */
423 current
= *cmd
->body_list
;
426 command_nest_depth
++;
427 ret
= execute_control_command (current
);
428 command_nest_depth
--;
430 /* If we got an error, or a "break" command, then stop
432 if (ret
== invalid_control
|| ret
== break_control
)
438 /* If we got a "continue" command, then restart the loop
440 if (ret
== continue_control
)
443 /* Get the next statement. */
444 current
= current
->next
;
448 /* Reset RET so that we don't recurse the break all the way down. */
449 if (ret
== break_control
)
450 ret
= simple_control
;
457 char *buffer
= alloca (strlen (cmd
->line
) + 4);
458 sprintf (buffer
, "if %s", cmd
->line
);
459 print_command_trace (buffer
);
461 new_line
= insert_args (cmd
->line
);
464 make_cleanup (free_current_contents
, &new_line
);
465 /* Parse the conditional for the if statement. */
466 expr
= parse_expression (new_line
);
467 make_cleanup (free_current_contents
, &expr
);
470 ret
= simple_control
;
472 /* Evaluate the conditional. */
473 val_mark
= value_mark ();
474 val
= evaluate_expression (expr
);
476 /* Choose which arm to take commands from based on the value of the
477 conditional expression. */
478 if (value_true (val
))
479 current
= *cmd
->body_list
;
480 else if (cmd
->body_count
== 2)
481 current
= *(cmd
->body_list
+ 1);
482 value_free_to_mark (val_mark
);
484 /* Execute commands in the given arm. */
487 command_nest_depth
++;
488 ret
= execute_control_command (current
);
489 command_nest_depth
--;
491 /* If we got an error, get out. */
492 if (ret
!= simple_control
)
495 /* Get the next statement in the body. */
496 current
= current
->next
;
503 warning (_("Invalid control type in command structure."));
507 do_cleanups (old_chain
);
512 /* "while" command support. Executes a body of statements while the
513 loop condition is nonzero. */
516 while_command (char *arg
, int from_tty
)
518 struct command_line
*command
= NULL
;
521 command
= get_command_line (while_control
, arg
);
526 suppress_next_print_command_trace
= 1;
527 execute_control_command (command
);
528 free_command_lines (&command
);
531 /* "if" command support. Execute either the true or false arm depending
532 on the value of the if conditional. */
535 if_command (char *arg
, int from_tty
)
537 struct command_line
*command
= NULL
;
540 command
= get_command_line (if_control
, arg
);
545 suppress_next_print_command_trace
= 1;
546 execute_control_command (command
);
547 free_command_lines (&command
);
552 arg_cleanup (void *ignore
)
554 struct user_args
*oargs
= user_args
;
556 internal_error (__FILE__
, __LINE__
,
557 _("arg_cleanup called with no user args.\n"));
559 user_args
= user_args
->next
;
560 xfree (oargs
->command
);
564 /* Bind the incomming arguments for a user defined command to
565 $arg0, $arg1 ... $argMAXUSERARGS. */
567 static struct cleanup
*
568 setup_user_args (char *p
)
570 struct user_args
*args
;
571 struct cleanup
*old_chain
;
572 unsigned int arg_count
= 0;
574 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
575 memset (args
, 0, sizeof (struct user_args
));
577 args
->next
= user_args
;
580 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
585 user_args
->command
= p
= xstrdup (p
);
594 if (arg_count
>= MAXUSERARGS
)
596 error (_("user defined function may only have %d arguments."),
601 /* Strip whitespace. */
602 while (*p
== ' ' || *p
== '\t')
605 /* P now points to an argument. */
607 user_args
->a
[arg_count
].arg
= p
;
609 /* Get to the end of this argument. */
612 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
641 user_args
->a
[arg_count
].len
= p
- start_arg
;
648 /* Given character string P, return a point to the first argument ($arg),
649 or NULL if P contains no arguments. */
654 while ((p
= strchr (p
, '$')))
656 if (strncmp (p
, "$arg", 4) == 0
657 && (isdigit (p
[4]) || p
[4] == 'c'))
664 /* Insert the user defined arguments stored in user_arg into the $arg
665 arguments found in line, with the updated copy being placed into nline. */
668 insert_args (char *line
)
670 char *p
, *save_line
, *new_line
;
673 /* If we are not in a user-defined function, treat $argc, $arg0, et
674 cetera as normal convenience variables. */
675 if (user_args
== NULL
)
676 return xstrdup (line
);
678 /* First we need to know how much memory to allocate for the new line. */
681 while ((p
= locate_arg (line
)))
688 /* $argc. Number will be <=10. */
689 len
+= user_args
->count
== 10 ? 2 : 1;
691 else if (i
>= user_args
->count
)
693 error (_("Missing argument %d in user function."), i
);
698 len
+= user_args
->a
[i
].len
;
703 /* Don't forget the tail. */
704 len
+= strlen (line
);
706 /* Allocate space for the new line and fill it in. */
707 new_line
= (char *) xmalloc (len
+ 1);
708 if (new_line
== NULL
)
711 /* Restore pointer to beginning of old line. */
714 /* Save pointer to beginning of new line. */
715 save_line
= new_line
;
717 while ((p
= locate_arg (line
)))
721 memcpy (new_line
, line
, p
- line
);
722 new_line
+= p
- line
;
726 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
727 if (user_args
->count
== 10)
733 *(new_line
++) = user_args
->count
+ '0';
738 len
= user_args
->a
[i
].len
;
741 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
747 /* Don't forget the tail. */
748 strcpy (new_line
, line
);
750 /* Return a pointer to the beginning of the new line. */
755 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
756 code bodies. This is typically used when we encounter an "else"
757 clause for an "if" command. */
760 realloc_body_list (struct command_line
*command
, int new_length
)
763 struct command_line
**body_list
;
765 n
= command
->body_count
;
771 body_list
= (struct command_line
**)
772 xmalloc (sizeof (struct command_line
*) * new_length
);
774 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
775 memset (body_list
+ n
, 0, sizeof (struct command_line
*) * (new_length
- n
));
777 xfree (command
->body_list
);
778 command
->body_list
= body_list
;
779 command
->body_count
= new_length
;
782 /* Read one line from the input stream. If the command is an "else" or
783 "end", return such an indication to the caller. */
785 static enum misc_command_type
786 read_next_line (struct command_line
**command
)
788 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
791 if (control_level
>= 254)
792 error (_("Control nesting too deep!"));
794 /* Set a prompt based on the nesting of the control commands. */
795 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
797 for (i
= 0; i
< control_level
; i
++)
798 control_prompt
[i
] = ' ';
799 control_prompt
[i
] = '>';
800 control_prompt
[i
+ 1] = '\0';
801 prompt_ptr
= (char *) &control_prompt
[0];
806 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
808 /* Not sure what to do here. */
812 /* Strip leading and trailing whitespace. */
813 while (*p
== ' ' || *p
== '\t')
817 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
820 /* Blanks and comments don't really do anything, but we need to
821 distinguish them from else, end and other commands which can be
823 if (p1
== p
|| p
[0] == '#')
826 /* Is this the end of a simple, while, or if control structure? */
827 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
830 /* Is the else clause of an if control structure? */
831 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
834 /* Check for while, if, break, continue, etc and build a new command
835 line structure for them. */
836 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
840 while (first_arg
< p1
&& isspace (*first_arg
))
842 *command
= build_command_line (while_control
, first_arg
);
844 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
848 while (first_arg
< p1
&& isspace (*first_arg
))
850 *command
= build_command_line (if_control
, first_arg
);
852 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
854 *command
= (struct command_line
*)
855 xmalloc (sizeof (struct command_line
));
856 (*command
)->next
= NULL
;
857 (*command
)->line
= NULL
;
858 (*command
)->control_type
= break_control
;
859 (*command
)->body_count
= 0;
860 (*command
)->body_list
= NULL
;
862 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
864 *command
= (struct command_line
*)
865 xmalloc (sizeof (struct command_line
));
866 (*command
)->next
= NULL
;
867 (*command
)->line
= NULL
;
868 (*command
)->control_type
= continue_control
;
869 (*command
)->body_count
= 0;
870 (*command
)->body_list
= NULL
;
874 /* A normal command. */
875 *command
= (struct command_line
*)
876 xmalloc (sizeof (struct command_line
));
877 (*command
)->next
= NULL
;
878 (*command
)->line
= savestring (p
, p1
- p
);
879 (*command
)->control_type
= simple_control
;
880 (*command
)->body_count
= 0;
881 (*command
)->body_list
= NULL
;
884 /* Nothing special. */
888 /* Recursively read in the control structures and create a command_line
891 The parent_control parameter is the control structure in which the
892 following commands are nested. */
894 static enum command_control_type
895 recurse_read_control_structure (struct command_line
*current_cmd
)
898 enum misc_command_type val
;
899 enum command_control_type ret
;
900 struct command_line
**body_ptr
, *child_tail
, *next
;
906 if (current_cmd
->control_type
== simple_control
)
907 error (_("Recursed on a simple control type."));
909 if (current_body
> current_cmd
->body_count
)
910 error (_("Allocated body is smaller than this command type needs."));
912 /* Read lines from the input stream and build control structures. */
918 val
= read_next_line (&next
);
920 /* Just skip blanks and comments. */
921 if (val
== nop_command
)
924 if (val
== end_command
)
926 if (current_cmd
->control_type
== while_control
927 || current_cmd
->control_type
== if_control
)
929 /* Success reading an entire control structure. */
930 ret
= simple_control
;
935 ret
= invalid_control
;
940 /* Not the end of a control structure. */
941 if (val
== else_command
)
943 if (current_cmd
->control_type
== if_control
944 && current_body
== 1)
946 realloc_body_list (current_cmd
, 2);
953 ret
= invalid_control
;
960 child_tail
->next
= next
;
964 body_ptr
= current_cmd
->body_list
;
965 for (i
= 1; i
< current_body
; i
++)
974 /* If the latest line is another control structure, then recurse
976 if (next
->control_type
== while_control
977 || next
->control_type
== if_control
)
980 ret
= recurse_read_control_structure (next
);
983 if (ret
!= simple_control
)
993 /* Read lines from the input stream and accumulate them in a chain of
994 struct command_line's, which is then returned. For input from a
995 terminal, the special command "end" is used to mark the end of the
996 input, and is not included in the returned chain of commands. */
998 #define END_MESSAGE "End with a line saying just \"end\"."
1000 struct command_line
*
1001 read_command_lines (char *prompt_arg
, int from_tty
)
1003 struct command_line
*head
, *tail
, *next
;
1004 struct cleanup
*old_chain
;
1005 enum command_control_type ret
;
1006 enum misc_command_type val
;
1010 if (from_tty
&& input_from_terminal_p ())
1012 if (deprecated_readline_begin_hook
)
1014 /* Note - intentional to merge messages with no newline */
1015 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
1019 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1020 gdb_flush (gdb_stdout
);
1029 val
= read_next_line (&next
);
1031 /* Ignore blank lines or comments. */
1032 if (val
== nop_command
)
1035 if (val
== end_command
)
1037 ret
= simple_control
;
1041 if (val
!= ok_command
)
1043 ret
= invalid_control
;
1047 if (next
->control_type
== while_control
1048 || next
->control_type
== if_control
)
1051 ret
= recurse_read_control_structure (next
);
1054 if (ret
== invalid_control
)
1065 old_chain
= make_cleanup_free_command_lines (&head
);
1074 if (ret
!= invalid_control
)
1076 discard_cleanups (old_chain
);
1079 do_cleanups (old_chain
);
1082 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1084 (*deprecated_readline_end_hook
) ();
1089 /* Free a chain of struct command_line's. */
1092 free_command_lines (struct command_line
**lptr
)
1094 struct command_line
*l
= *lptr
;
1095 struct command_line
*next
;
1096 struct command_line
**blist
;
1101 if (l
->body_count
> 0)
1103 blist
= l
->body_list
;
1104 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1105 free_command_lines (blist
);
1116 do_free_command_lines_cleanup (void *arg
)
1118 free_command_lines (arg
);
1122 make_cleanup_free_command_lines (struct command_line
**arg
)
1124 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1127 struct command_line
*
1128 copy_command_lines (struct command_line
*cmds
)
1130 struct command_line
*result
= NULL
;
1134 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1136 result
->next
= copy_command_lines (cmds
->next
);
1137 result
->line
= xstrdup (cmds
->line
);
1138 result
->control_type
= cmds
->control_type
;
1139 result
->body_count
= cmds
->body_count
;
1140 if (cmds
->body_count
> 0)
1144 result
->body_list
= (struct command_line
**)
1145 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1147 for (i
= 0; i
< cmds
->body_count
; i
++)
1148 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1151 result
->body_list
= NULL
;
1158 validate_comname (char *comname
)
1163 error_no_arg (_("name of command to define"));
1168 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1169 error (_("Junk in argument list: \"%s\""), p
);
1174 /* This is just a placeholder in the command data structures. */
1176 user_defined_command (char *ignore
, int from_tty
)
1181 define_command (char *comname
, int from_tty
)
1183 #define MAX_TMPBUF 128
1190 struct command_line
*cmds
;
1191 struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1192 char *tem
= comname
;
1194 char tmpbuf
[MAX_TMPBUF
];
1195 int hook_type
= CMD_NO_HOOK
;
1196 int hook_name_size
= 0;
1198 #define HOOK_STRING "hook-"
1200 #define HOOK_POST_STRING "hookpost-"
1201 #define HOOK_POST_LEN 9
1203 validate_comname (comname
);
1205 /* Look it up, and verify that we got an exact match. */
1206 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1207 if (c
&& strcmp (comname
, c
->name
) != 0)
1213 if (c
->class == class_user
|| c
->class == class_alias
)
1214 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1216 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1218 error (_("Command \"%s\" not redefined."), c
->name
);
1221 /* If this new command is a hook, then mark the command which it
1222 is hooking. Note that we allow hooking `help' commands, so that
1223 we can hook the `stop' pseudo-command. */
1225 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1227 hook_type
= CMD_PRE_HOOK
;
1228 hook_name_size
= HOOK_LEN
;
1230 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1232 hook_type
= CMD_POST_HOOK
;
1233 hook_name_size
= HOOK_POST_LEN
;
1236 if (hook_type
!= CMD_NO_HOOK
)
1238 /* Look up cmd it hooks, and verify that we got an exact match. */
1239 tem
= comname
+ hook_name_size
;
1240 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1241 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1245 warning (_("Your new `%s' command does not hook any existing command."),
1247 if (!query ("Proceed? "))
1248 error (_("Not confirmed."));
1252 comname
= savestring (comname
, strlen (comname
));
1254 /* If the rest of the commands will be case insensitive, this one
1255 should behave in the same manner. */
1256 for (tem
= comname
; *tem
; tem
++)
1258 *tem
= tolower (*tem
);
1260 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1261 cmds
= read_command_lines (tmpbuf
, from_tty
);
1263 if (c
&& c
->class == class_user
)
1264 free_command_lines (&c
->user_commands
);
1266 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1267 (c
&& c
->class == class_user
)
1268 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1269 newc
->user_commands
= cmds
;
1271 /* If this new command is a hook, then mark both commands as being
1278 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1279 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1282 hookc
->hook_post
= newc
; /* Target gets hooked. */
1283 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1286 /* Should never come here as hookc would be 0. */
1287 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1293 document_command (char *comname
, int from_tty
)
1295 struct command_line
*doclines
;
1296 struct cmd_list_element
*c
;
1297 char *tem
= comname
;
1300 validate_comname (comname
);
1302 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1304 if (c
->class != class_user
)
1305 error (_("Command \"%s\" is built-in."), comname
);
1307 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1308 doclines
= read_command_lines (tmpbuf
, from_tty
);
1314 struct command_line
*cl1
;
1317 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1318 len
+= strlen (cl1
->line
) + 1;
1320 c
->doc
= (char *) xmalloc (len
+ 1);
1323 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1325 strcat (c
->doc
, cl1
->line
);
1327 strcat (c
->doc
, "\n");
1331 free_command_lines (&doclines
);
1334 struct source_cleanup_lines_args
1341 source_cleanup_lines (void *args
)
1343 struct source_cleanup_lines_args
*p
=
1344 (struct source_cleanup_lines_args
*) args
;
1345 source_line_number
= p
->old_line
;
1346 source_file_name
= p
->old_file
;
1350 do_fclose_cleanup (void *stream
)
1355 struct wrapped_read_command_file_args
1361 wrapped_read_command_file (struct ui_out
*uiout
, void *data
)
1363 struct wrapped_read_command_file_args
*args
= data
;
1364 read_command_file (args
->stream
);
1367 /* Used to implement source_command */
1370 script_from_file (FILE *stream
, char *file
)
1372 struct cleanup
*old_cleanups
;
1373 struct source_cleanup_lines_args old_lines
;
1377 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1379 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1381 old_lines
.old_line
= source_line_number
;
1382 old_lines
.old_file
= source_file_name
;
1383 make_cleanup (source_cleanup_lines
, &old_lines
);
1384 source_line_number
= 0;
1385 source_file_name
= file
;
1386 /* This will get set every time we read a line. So it won't stay "" for
1388 error_pre_print
= "";
1391 struct gdb_exception e
;
1392 struct wrapped_read_command_file_args args
;
1393 args
.stream
= stream
;
1394 e
= catch_exception (uiout
, wrapped_read_command_file
, &args
,
1401 /* Re-throw the error, but with the file name information
1403 throw_error (e
.error
,
1404 _("%s:%d: Error in sourced command file:\n%s"),
1405 source_file_name
, source_line_number
, e
.message
);
1407 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1411 do_cleanups (old_cleanups
);
1415 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1417 struct command_line
*cmdlines
;
1419 cmdlines
= c
->user_commands
;
1422 fputs_filtered ("User command ", stream
);
1423 fputs_filtered (c
->name
, stream
);
1424 fputs_filtered (":\n", stream
);
1426 print_command_lines (uiout
, cmdlines
, 1);
1427 fputs_filtered ("\n", stream
);