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, 2008
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 3 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, see <http://www.gnu.org/licenses/>. */
24 #include "language.h" /* For value_true */
28 #include "gdb_string.h"
29 #include "exceptions.h"
31 #include "breakpoint.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-script.h"
35 #include "gdb_assert.h"
37 /* Prototypes for local functions */
39 static enum command_control_type
40 recurse_read_control_structure (struct command_line
*current_cmd
);
42 static char *insert_args (char *line
);
44 static struct cleanup
* setup_user_args (char *p
);
46 static void validate_comname (char *);
48 /* Level of control structure when reading. */
49 static int control_level
;
51 /* Level of control structure when executing. */
52 static int command_nest_depth
= 1;
54 /* This is to prevent certain commands being printed twice. */
55 static int suppress_next_print_command_trace
= 0;
57 /* Structure for arguments to user defined functions. */
58 #define MAXUSERARGS 10
61 struct user_args
*next
;
62 /* It is necessary to store a malloced copy of the command line to
63 ensure that the arguments are not overwritten before they are used. */
76 /* Allocate, initialize a new command line structure for one of the
77 control commands (if/while). */
79 static struct command_line
*
80 build_command_line (enum command_control_type type
, char *args
)
82 struct command_line
*cmd
;
84 if (args
== NULL
&& (type
== if_control
|| type
== while_control
))
85 error (_("if/while commands require arguments."));
86 gdb_assert (args
!= NULL
);
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
));
102 /* Build and return a new command structure for the control commands
103 such as "if" and "while". */
105 static struct command_line
*
106 get_command_line (enum command_control_type type
, char *arg
)
108 struct command_line
*cmd
;
109 struct cleanup
*old_chain
= NULL
;
111 /* Allocate and build a new command line structure. */
112 cmd
= build_command_line (type
, arg
);
114 old_chain
= make_cleanup_free_command_lines (&cmd
);
116 /* Read in the body of this command. */
117 if (recurse_read_control_structure (cmd
) == invalid_control
)
119 warning (_("Error reading in canned sequence of commands."));
120 do_cleanups (old_chain
);
124 discard_cleanups (old_chain
);
128 /* Recursively print a command (including full control structures). */
131 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
134 struct command_line
*list
;
141 ui_out_spaces (uiout
, 2 * depth
);
143 /* A simple command, print it and continue. */
144 if (list
->control_type
== simple_control
)
146 ui_out_field_string (uiout
, NULL
, list
->line
);
147 ui_out_text (uiout
, "\n");
152 /* loop_continue to jump to the start of a while loop, print it
154 if (list
->control_type
== continue_control
)
156 ui_out_field_string (uiout
, NULL
, "loop_continue");
157 ui_out_text (uiout
, "\n");
162 /* loop_break to break out of a while loop, print it and continue. */
163 if (list
->control_type
== break_control
)
165 ui_out_field_string (uiout
, NULL
, "loop_break");
166 ui_out_text (uiout
, "\n");
171 /* A while command. Recursively print its subcommands and continue. */
172 if (list
->control_type
== while_control
)
174 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
175 ui_out_text (uiout
, "\n");
176 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
178 ui_out_spaces (uiout
, 2 * depth
);
179 ui_out_field_string (uiout
, NULL
, "end");
180 ui_out_text (uiout
, "\n");
185 /* An if command. Recursively print both arms before continueing. */
186 if (list
->control_type
== if_control
)
188 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
189 ui_out_text (uiout
, "\n");
191 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
193 /* Show the false arm if it exists. */
194 if (list
->body_count
== 2)
197 ui_out_spaces (uiout
, 2 * depth
);
198 ui_out_field_string (uiout
, NULL
, "else");
199 ui_out_text (uiout
, "\n");
200 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
204 ui_out_spaces (uiout
, 2 * depth
);
205 ui_out_field_string (uiout
, NULL
, "end");
206 ui_out_text (uiout
, "\n");
211 /* A commands command. Print the breakpoint commands and continue. */
212 if (list
->control_type
== commands_control
)
215 ui_out_field_fmt (uiout
, NULL
, "commands %s", list
->line
);
217 ui_out_field_string (uiout
, NULL
, "commands");
218 ui_out_text (uiout
, "\n");
219 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
221 ui_out_spaces (uiout
, 2 * depth
);
222 ui_out_field_string (uiout
, NULL
, "end");
223 ui_out_text (uiout
, "\n");
228 /* ignore illegal command type and try next */
233 /* Handle pre-post hooks. */
236 clear_hook_in_cleanup (void *data
)
238 struct cmd_list_element
*c
= data
;
239 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
243 execute_cmd_pre_hook (struct cmd_list_element
*c
)
245 if ((c
->hook_pre
) && (!c
->hook_in
))
247 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
248 c
->hook_in
= 1; /* Prevent recursive hooking */
249 execute_user_command (c
->hook_pre
, (char *) 0);
250 do_cleanups (cleanups
);
255 execute_cmd_post_hook (struct cmd_list_element
*c
)
257 if ((c
->hook_post
) && (!c
->hook_in
))
259 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
260 c
->hook_in
= 1; /* Prevent recursive hooking */
261 execute_user_command (c
->hook_post
, (char *) 0);
262 do_cleanups (cleanups
);
266 /* Execute the command in CMD. */
268 do_restore_user_call_depth (void * call_depth
)
270 int * depth
= call_depth
;
278 execute_user_command (struct cmd_list_element
*c
, char *args
)
280 struct command_line
*cmdlines
;
281 struct cleanup
*old_chain
;
282 enum command_control_type ret
;
283 static int user_call_depth
= 0;
284 extern int max_user_call_depth
;
286 old_chain
= setup_user_args (args
);
288 cmdlines
= c
->user_commands
;
293 if (++user_call_depth
> max_user_call_depth
)
294 error (_("Max user call depth exceeded -- command aborted."));
296 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
298 /* Set the instream to 0, indicating execution of a
299 user-defined function. */
300 make_cleanup (do_restore_instream_cleanup
, instream
);
301 instream
= (FILE *) 0;
303 /* Also set the global in_user_command, so that NULL instream is
304 not confused with Insight. */
307 command_nest_depth
++;
310 ret
= execute_control_command (cmdlines
);
311 if (ret
!= simple_control
&& ret
!= break_control
)
313 warning (_("Error executing canned sequence of commands."));
316 cmdlines
= cmdlines
->next
;
318 command_nest_depth
--;
319 do_cleanups (old_chain
);
322 /* This function is called every time GDB prints a prompt.
323 It ensures that errors and the like to not confuse the command tracing. */
326 reset_command_nest_depth (void)
328 command_nest_depth
= 1;
331 suppress_next_print_command_trace
= 0;
334 /* Print the command, prefixed with '+' to represent the call depth.
335 This is slightly complicated because this function may be called
336 from execute_command and execute_control_command. Unfortunately
337 execute_command also prints the top level control commands.
338 In these cases execute_command will call execute_control_command
339 via while_command or if_command. Inner levels of 'if' and 'while'
340 are dealt with directly. Therefore we can use these functions
341 to determine whether the command has been printed already or not. */
343 print_command_trace (const char *cmd
)
347 if (suppress_next_print_command_trace
)
349 suppress_next_print_command_trace
= 0;
353 if (!source_verbose
&& !trace_commands
)
356 for (i
=0; i
< command_nest_depth
; i
++)
357 printf_filtered ("+");
359 printf_filtered ("%s\n", cmd
);
362 enum command_control_type
363 execute_control_command (struct command_line
*cmd
)
365 struct expression
*expr
;
366 struct command_line
*current
;
367 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
369 struct value
*val_mark
;
371 enum command_control_type ret
;
374 /* Start by assuming failure, if a problem is detected, the code
375 below will simply "break" out of the switch. */
376 ret
= invalid_control
;
378 switch (cmd
->control_type
)
381 /* A simple command, execute it and return. */
382 new_line
= insert_args (cmd
->line
);
385 make_cleanup (free_current_contents
, &new_line
);
386 execute_command (new_line
, 0);
387 ret
= cmd
->control_type
;
390 case continue_control
:
391 print_command_trace ("loop_continue");
393 /* Return for "continue", and "break" so we can either
394 continue the loop at the top, or break out. */
395 ret
= cmd
->control_type
;
399 print_command_trace ("loop_break");
401 /* Return for "continue", and "break" so we can either
402 continue the loop at the top, or break out. */
403 ret
= cmd
->control_type
;
408 char *buffer
= alloca (strlen (cmd
->line
) + 7);
409 sprintf (buffer
, "while %s", cmd
->line
);
410 print_command_trace (buffer
);
412 /* Parse the loop control expression for the while statement. */
413 new_line
= insert_args (cmd
->line
);
416 make_cleanup (free_current_contents
, &new_line
);
417 expr
= parse_expression (new_line
);
418 make_cleanup (free_current_contents
, &expr
);
420 ret
= simple_control
;
423 /* Keep iterating so long as the expression is true. */
430 /* Evaluate the expression. */
431 val_mark
= value_mark ();
432 val
= evaluate_expression (expr
);
433 cond_result
= value_true (val
);
434 value_free_to_mark (val_mark
);
436 /* If the value is false, then break out of the loop. */
440 /* Execute the body of the while statement. */
441 current
= *cmd
->body_list
;
444 command_nest_depth
++;
445 ret
= execute_control_command (current
);
446 command_nest_depth
--;
448 /* If we got an error, or a "break" command, then stop
450 if (ret
== invalid_control
|| ret
== break_control
)
456 /* If we got a "continue" command, then restart the loop
458 if (ret
== continue_control
)
461 /* Get the next statement. */
462 current
= current
->next
;
466 /* Reset RET so that we don't recurse the break all the way down. */
467 if (ret
== break_control
)
468 ret
= simple_control
;
475 char *buffer
= alloca (strlen (cmd
->line
) + 4);
476 sprintf (buffer
, "if %s", cmd
->line
);
477 print_command_trace (buffer
);
479 new_line
= insert_args (cmd
->line
);
482 make_cleanup (free_current_contents
, &new_line
);
483 /* Parse the conditional for the if statement. */
484 expr
= parse_expression (new_line
);
485 make_cleanup (free_current_contents
, &expr
);
488 ret
= simple_control
;
490 /* Evaluate the conditional. */
491 val_mark
= value_mark ();
492 val
= evaluate_expression (expr
);
494 /* Choose which arm to take commands from based on the value of the
495 conditional expression. */
496 if (value_true (val
))
497 current
= *cmd
->body_list
;
498 else if (cmd
->body_count
== 2)
499 current
= *(cmd
->body_list
+ 1);
500 value_free_to_mark (val_mark
);
502 /* Execute commands in the given arm. */
505 command_nest_depth
++;
506 ret
= execute_control_command (current
);
507 command_nest_depth
--;
509 /* If we got an error, get out. */
510 if (ret
!= simple_control
)
513 /* Get the next statement in the body. */
514 current
= current
->next
;
519 case commands_control
:
521 /* Breakpoint commands list, record the commands in the breakpoint's
522 command list and return. */
523 new_line
= insert_args (cmd
->line
);
526 make_cleanup (free_current_contents
, &new_line
);
527 ret
= commands_from_control_command (new_line
, cmd
);
532 warning (_("Invalid control type in canned commands structure."));
536 do_cleanups (old_chain
);
541 /* "while" command support. Executes a body of statements while the
542 loop condition is nonzero. */
545 while_command (char *arg
, int from_tty
)
547 struct command_line
*command
= NULL
;
550 command
= get_command_line (while_control
, arg
);
555 suppress_next_print_command_trace
= 1;
556 execute_control_command (command
);
557 free_command_lines (&command
);
560 /* "if" command support. Execute either the true or false arm depending
561 on the value of the if conditional. */
564 if_command (char *arg
, int from_tty
)
566 struct command_line
*command
= NULL
;
569 command
= get_command_line (if_control
, arg
);
574 suppress_next_print_command_trace
= 1;
575 execute_control_command (command
);
576 free_command_lines (&command
);
581 arg_cleanup (void *ignore
)
583 struct user_args
*oargs
= user_args
;
585 internal_error (__FILE__
, __LINE__
,
586 _("arg_cleanup called with no user args.\n"));
588 user_args
= user_args
->next
;
589 xfree (oargs
->command
);
593 /* Bind the incomming arguments for a user defined command to
594 $arg0, $arg1 ... $argMAXUSERARGS. */
596 static struct cleanup
*
597 setup_user_args (char *p
)
599 struct user_args
*args
;
600 struct cleanup
*old_chain
;
601 unsigned int arg_count
= 0;
603 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
604 memset (args
, 0, sizeof (struct user_args
));
606 args
->next
= user_args
;
609 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
614 user_args
->command
= p
= xstrdup (p
);
623 if (arg_count
>= MAXUSERARGS
)
625 error (_("user defined function may only have %d arguments."),
630 /* Strip whitespace. */
631 while (*p
== ' ' || *p
== '\t')
634 /* P now points to an argument. */
636 user_args
->a
[arg_count
].arg
= p
;
638 /* Get to the end of this argument. */
641 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
670 user_args
->a
[arg_count
].len
= p
- start_arg
;
677 /* Given character string P, return a point to the first argument ($arg),
678 or NULL if P contains no arguments. */
683 while ((p
= strchr (p
, '$')))
685 if (strncmp (p
, "$arg", 4) == 0
686 && (isdigit (p
[4]) || p
[4] == 'c'))
693 /* Insert the user defined arguments stored in user_arg into the $arg
694 arguments found in line, with the updated copy being placed into nline. */
697 insert_args (char *line
)
699 char *p
, *save_line
, *new_line
;
702 /* If we are not in a user-defined function, treat $argc, $arg0, et
703 cetera as normal convenience variables. */
704 if (user_args
== NULL
)
705 return xstrdup (line
);
707 /* First we need to know how much memory to allocate for the new line. */
710 while ((p
= locate_arg (line
)))
717 /* $argc. Number will be <=10. */
718 len
+= user_args
->count
== 10 ? 2 : 1;
720 else if (i
>= user_args
->count
)
722 error (_("Missing argument %d in user function."), i
);
727 len
+= user_args
->a
[i
].len
;
732 /* Don't forget the tail. */
733 len
+= strlen (line
);
735 /* Allocate space for the new line and fill it in. */
736 new_line
= (char *) xmalloc (len
+ 1);
737 if (new_line
== NULL
)
740 /* Restore pointer to beginning of old line. */
743 /* Save pointer to beginning of new line. */
744 save_line
= new_line
;
746 while ((p
= locate_arg (line
)))
750 memcpy (new_line
, line
, p
- line
);
751 new_line
+= p
- line
;
755 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
756 if (user_args
->count
== 10)
762 *(new_line
++) = user_args
->count
+ '0';
767 len
= user_args
->a
[i
].len
;
770 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
776 /* Don't forget the tail. */
777 strcpy (new_line
, line
);
779 /* Return a pointer to the beginning of the new line. */
784 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
785 code bodies. This is typically used when we encounter an "else"
786 clause for an "if" command. */
789 realloc_body_list (struct command_line
*command
, int new_length
)
792 struct command_line
**body_list
;
794 n
= command
->body_count
;
800 body_list
= (struct command_line
**)
801 xmalloc (sizeof (struct command_line
*) * new_length
);
803 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
804 memset (body_list
+ n
, 0, sizeof (struct command_line
*) * (new_length
- n
));
806 xfree (command
->body_list
);
807 command
->body_list
= body_list
;
808 command
->body_count
= new_length
;
811 /* Read one line from the input stream. If the command is an "else" or
812 "end", return such an indication to the caller. */
814 static enum misc_command_type
815 read_next_line (struct command_line
**command
)
817 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
820 if (control_level
>= 254)
821 error (_("Control nesting too deep!"));
823 /* Set a prompt based on the nesting of the control commands. */
824 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
826 for (i
= 0; i
< control_level
; i
++)
827 control_prompt
[i
] = ' ';
828 control_prompt
[i
] = '>';
829 control_prompt
[i
+ 1] = '\0';
830 prompt_ptr
= (char *) &control_prompt
[0];
835 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
837 /* Not sure what to do here. */
841 /* Strip leading and trailing whitespace. */
842 while (*p
== ' ' || *p
== '\t')
846 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
849 /* Blanks and comments don't really do anything, but we need to
850 distinguish them from else, end and other commands which can be
852 if (p1
== p
|| p
[0] == '#')
855 /* Is this the end of a simple, while, or if control structure? */
856 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
859 /* Is the else clause of an if control structure? */
860 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
863 /* Check for while, if, break, continue, etc and build a new command
864 line structure for them. */
865 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
869 while (first_arg
< p1
&& isspace (*first_arg
))
871 *command
= build_command_line (while_control
, first_arg
);
873 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
877 while (first_arg
< p1
&& isspace (*first_arg
))
879 *command
= build_command_line (if_control
, first_arg
);
881 else if (p1
- p
>= 8 && !strncmp (p
, "commands", 8))
885 while (first_arg
< p1
&& isspace (*first_arg
))
887 *command
= build_command_line (commands_control
, first_arg
);
889 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
891 *command
= (struct command_line
*)
892 xmalloc (sizeof (struct command_line
));
893 (*command
)->next
= NULL
;
894 (*command
)->line
= NULL
;
895 (*command
)->control_type
= break_control
;
896 (*command
)->body_count
= 0;
897 (*command
)->body_list
= NULL
;
899 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
901 *command
= (struct command_line
*)
902 xmalloc (sizeof (struct command_line
));
903 (*command
)->next
= NULL
;
904 (*command
)->line
= NULL
;
905 (*command
)->control_type
= continue_control
;
906 (*command
)->body_count
= 0;
907 (*command
)->body_list
= NULL
;
911 /* A normal command. */
912 *command
= (struct command_line
*)
913 xmalloc (sizeof (struct command_line
));
914 (*command
)->next
= NULL
;
915 (*command
)->line
= savestring (p
, p1
- p
);
916 (*command
)->control_type
= simple_control
;
917 (*command
)->body_count
= 0;
918 (*command
)->body_list
= NULL
;
921 /* Nothing special. */
925 /* Recursively read in the control structures and create a command_line
928 The parent_control parameter is the control structure in which the
929 following commands are nested. */
931 static enum command_control_type
932 recurse_read_control_structure (struct command_line
*current_cmd
)
935 enum misc_command_type val
;
936 enum command_control_type ret
;
937 struct command_line
**body_ptr
, *child_tail
, *next
;
943 if (current_cmd
->control_type
== simple_control
)
944 error (_("Recursed on a simple control type."));
946 if (current_body
> current_cmd
->body_count
)
947 error (_("Allocated body is smaller than this command type needs."));
949 /* Read lines from the input stream and build control structures. */
955 val
= read_next_line (&next
);
957 /* Just skip blanks and comments. */
958 if (val
== nop_command
)
961 if (val
== end_command
)
963 if (current_cmd
->control_type
== while_control
964 || current_cmd
->control_type
== if_control
965 || current_cmd
->control_type
== commands_control
)
967 /* Success reading an entire canned sequence of commands. */
968 ret
= simple_control
;
973 ret
= invalid_control
;
978 /* Not the end of a control structure. */
979 if (val
== else_command
)
981 if (current_cmd
->control_type
== if_control
982 && current_body
== 1)
984 realloc_body_list (current_cmd
, 2);
991 ret
= invalid_control
;
998 child_tail
->next
= next
;
1002 body_ptr
= current_cmd
->body_list
;
1003 for (i
= 1; i
< current_body
; i
++)
1012 /* If the latest line is another control structure, then recurse
1014 if (next
->control_type
== while_control
1015 || next
->control_type
== if_control
1016 || next
->control_type
== commands_control
)
1019 ret
= recurse_read_control_structure (next
);
1022 if (ret
!= simple_control
)
1032 /* Read lines from the input stream and accumulate them in a chain of
1033 struct command_line's, which is then returned. For input from a
1034 terminal, the special command "end" is used to mark the end of the
1035 input, and is not included in the returned chain of commands. */
1037 #define END_MESSAGE "End with a line saying just \"end\"."
1039 struct command_line
*
1040 read_command_lines (char *prompt_arg
, int from_tty
)
1042 struct command_line
*head
, *tail
, *next
;
1043 struct cleanup
*old_chain
;
1044 enum command_control_type ret
;
1045 enum misc_command_type val
;
1049 if (from_tty
&& input_from_terminal_p ())
1051 if (deprecated_readline_begin_hook
)
1053 /* Note - intentional to merge messages with no newline */
1054 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
1058 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1059 gdb_flush (gdb_stdout
);
1069 val
= read_next_line (&next
);
1071 /* Ignore blank lines or comments. */
1072 if (val
== nop_command
)
1075 if (val
== end_command
)
1077 ret
= simple_control
;
1081 if (val
!= ok_command
)
1083 ret
= invalid_control
;
1087 if (next
->control_type
== while_control
1088 || next
->control_type
== if_control
1089 || next
->control_type
== commands_control
)
1092 ret
= recurse_read_control_structure (next
);
1095 if (ret
== invalid_control
)
1106 old_chain
= make_cleanup_free_command_lines (&head
);
1115 if (ret
!= invalid_control
)
1117 discard_cleanups (old_chain
);
1120 do_cleanups (old_chain
);
1123 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1125 (*deprecated_readline_end_hook
) ();
1130 /* Free a chain of struct command_line's. */
1133 free_command_lines (struct command_line
**lptr
)
1135 struct command_line
*l
= *lptr
;
1136 struct command_line
*next
;
1137 struct command_line
**blist
;
1142 if (l
->body_count
> 0)
1144 blist
= l
->body_list
;
1145 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1146 free_command_lines (blist
);
1157 do_free_command_lines_cleanup (void *arg
)
1159 free_command_lines (arg
);
1163 make_cleanup_free_command_lines (struct command_line
**arg
)
1165 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1168 struct command_line
*
1169 copy_command_lines (struct command_line
*cmds
)
1171 struct command_line
*result
= NULL
;
1175 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1177 result
->next
= copy_command_lines (cmds
->next
);
1178 result
->line
= xstrdup (cmds
->line
);
1179 result
->control_type
= cmds
->control_type
;
1180 result
->body_count
= cmds
->body_count
;
1181 if (cmds
->body_count
> 0)
1185 result
->body_list
= (struct command_line
**)
1186 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1188 for (i
= 0; i
< cmds
->body_count
; i
++)
1189 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1192 result
->body_list
= NULL
;
1199 validate_comname (char *comname
)
1204 error_no_arg (_("name of command to define"));
1209 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1210 error (_("Junk in argument list: \"%s\""), p
);
1215 /* This is just a placeholder in the command data structures. */
1217 user_defined_command (char *ignore
, int from_tty
)
1222 define_command (char *comname
, int from_tty
)
1224 #define MAX_TMPBUF 128
1231 struct command_line
*cmds
;
1232 struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1233 char *tem
= comname
;
1235 char tmpbuf
[MAX_TMPBUF
];
1236 int hook_type
= CMD_NO_HOOK
;
1237 int hook_name_size
= 0;
1239 #define HOOK_STRING "hook-"
1241 #define HOOK_POST_STRING "hookpost-"
1242 #define HOOK_POST_LEN 9
1244 validate_comname (comname
);
1246 /* Look it up, and verify that we got an exact match. */
1247 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1248 if (c
&& strcmp (comname
, c
->name
) != 0)
1254 if (c
->class == class_user
|| c
->class == class_alias
)
1255 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1257 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1259 error (_("Command \"%s\" not redefined."), c
->name
);
1262 /* If this new command is a hook, then mark the command which it
1263 is hooking. Note that we allow hooking `help' commands, so that
1264 we can hook the `stop' pseudo-command. */
1266 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1268 hook_type
= CMD_PRE_HOOK
;
1269 hook_name_size
= HOOK_LEN
;
1271 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1273 hook_type
= CMD_POST_HOOK
;
1274 hook_name_size
= HOOK_POST_LEN
;
1277 if (hook_type
!= CMD_NO_HOOK
)
1279 /* Look up cmd it hooks, and verify that we got an exact match. */
1280 tem
= comname
+ hook_name_size
;
1281 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1282 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1286 warning (_("Your new `%s' command does not hook any existing command."),
1288 if (!query ("Proceed? "))
1289 error (_("Not confirmed."));
1293 comname
= savestring (comname
, strlen (comname
));
1295 /* If the rest of the commands will be case insensitive, this one
1296 should behave in the same manner. */
1297 for (tem
= comname
; *tem
; tem
++)
1299 *tem
= tolower (*tem
);
1301 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1302 cmds
= read_command_lines (tmpbuf
, from_tty
);
1304 if (c
&& c
->class == class_user
)
1305 free_command_lines (&c
->user_commands
);
1307 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1308 (c
&& c
->class == class_user
)
1309 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1310 newc
->user_commands
= cmds
;
1312 /* If this new command is a hook, then mark both commands as being
1319 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1320 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1323 hookc
->hook_post
= newc
; /* Target gets hooked. */
1324 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1327 /* Should never come here as hookc would be 0. */
1328 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1334 document_command (char *comname
, int from_tty
)
1336 struct command_line
*doclines
;
1337 struct cmd_list_element
*c
;
1338 char *tem
= comname
;
1341 validate_comname (comname
);
1343 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1345 if (c
->class != class_user
)
1346 error (_("Command \"%s\" is built-in."), comname
);
1348 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1349 doclines
= read_command_lines (tmpbuf
, from_tty
);
1355 struct command_line
*cl1
;
1358 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1359 len
+= strlen (cl1
->line
) + 1;
1361 c
->doc
= (char *) xmalloc (len
+ 1);
1364 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1366 strcat (c
->doc
, cl1
->line
);
1368 strcat (c
->doc
, "\n");
1372 free_command_lines (&doclines
);
1375 struct source_cleanup_lines_args
1382 source_cleanup_lines (void *args
)
1384 struct source_cleanup_lines_args
*p
=
1385 (struct source_cleanup_lines_args
*) args
;
1386 source_line_number
= p
->old_line
;
1387 source_file_name
= p
->old_file
;
1391 do_fclose_cleanup (void *stream
)
1396 struct wrapped_read_command_file_args
1402 wrapped_read_command_file (struct ui_out
*uiout
, void *data
)
1404 struct wrapped_read_command_file_args
*args
= data
;
1405 read_command_file (args
->stream
);
1408 /* Used to implement source_command */
1411 script_from_file (FILE *stream
, char *file
)
1413 struct cleanup
*old_cleanups
;
1414 struct source_cleanup_lines_args old_lines
;
1418 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1420 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1422 old_lines
.old_line
= source_line_number
;
1423 old_lines
.old_file
= source_file_name
;
1424 make_cleanup (source_cleanup_lines
, &old_lines
);
1425 source_line_number
= 0;
1426 source_file_name
= file
;
1427 /* This will get set every time we read a line. So it won't stay "" for
1429 error_pre_print
= "";
1432 struct gdb_exception e
;
1433 struct wrapped_read_command_file_args args
;
1434 args
.stream
= stream
;
1435 e
= catch_exception (uiout
, wrapped_read_command_file
, &args
,
1442 /* Re-throw the error, but with the file name information
1444 throw_error (e
.error
,
1445 _("%s:%d: Error in sourced command file:\n%s"),
1446 source_file_name
, source_line_number
, e
.message
);
1448 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1452 do_cleanups (old_cleanups
);
1456 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1458 struct command_line
*cmdlines
;
1460 cmdlines
= c
->user_commands
;
1463 fputs_filtered ("User command ", stream
);
1464 fputs_filtered (c
->name
, stream
);
1465 fputs_filtered (":\n", stream
);
1467 print_command_lines (uiout
, cmdlines
, 1);
1468 fputs_filtered ("\n", stream
);