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 2009 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 #include "python/python.h"
39 /* Prototypes for local functions */
41 static enum command_control_type
42 recurse_read_control_structure (struct command_line
*current_cmd
);
44 static char *insert_args (char *line
);
46 static struct cleanup
* setup_user_args (char *p
);
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 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 if (list
->control_type
== python_control
)
230 ui_out_field_string (uiout
, NULL
, "python");
231 ui_out_text (uiout
, "\n");
232 /* Don't indent python code at all. */
233 print_command_lines (uiout
, *list
->body_list
, 0);
235 ui_out_spaces (uiout
, 2 * depth
);
236 ui_out_field_string (uiout
, NULL
, "end");
237 ui_out_text (uiout
, "\n");
242 /* ignore illegal command type and try next */
247 /* Handle pre-post hooks. */
250 clear_hook_in_cleanup (void *data
)
252 struct cmd_list_element
*c
= data
;
253 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
257 execute_cmd_pre_hook (struct cmd_list_element
*c
)
259 if ((c
->hook_pre
) && (!c
->hook_in
))
261 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
262 c
->hook_in
= 1; /* Prevent recursive hooking */
263 execute_user_command (c
->hook_pre
, (char *) 0);
264 do_cleanups (cleanups
);
269 execute_cmd_post_hook (struct cmd_list_element
*c
)
271 if ((c
->hook_post
) && (!c
->hook_in
))
273 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
274 c
->hook_in
= 1; /* Prevent recursive hooking */
275 execute_user_command (c
->hook_post
, (char *) 0);
276 do_cleanups (cleanups
);
280 /* Execute the command in CMD. */
282 do_restore_user_call_depth (void * call_depth
)
284 int * depth
= call_depth
;
292 execute_user_command (struct cmd_list_element
*c
, char *args
)
294 struct command_line
*cmdlines
;
295 struct cleanup
*old_chain
;
296 enum command_control_type ret
;
297 static int user_call_depth
= 0;
298 extern int max_user_call_depth
;
300 old_chain
= setup_user_args (args
);
302 cmdlines
= c
->user_commands
;
307 if (++user_call_depth
> max_user_call_depth
)
308 error (_("Max user call depth exceeded -- command aborted."));
310 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
312 /* Set the instream to 0, indicating execution of a
313 user-defined function. */
314 make_cleanup (do_restore_instream_cleanup
, instream
);
315 instream
= (FILE *) 0;
317 /* Also set the global in_user_command, so that NULL instream is
318 not confused with Insight. */
321 command_nest_depth
++;
324 ret
= execute_control_command (cmdlines
);
325 if (ret
!= simple_control
&& ret
!= break_control
)
327 warning (_("Error executing canned sequence of commands."));
330 cmdlines
= cmdlines
->next
;
332 command_nest_depth
--;
333 do_cleanups (old_chain
);
336 /* This function is called every time GDB prints a prompt.
337 It ensures that errors and the like to not confuse the command tracing. */
340 reset_command_nest_depth (void)
342 command_nest_depth
= 1;
345 suppress_next_print_command_trace
= 0;
348 /* Print the command, prefixed with '+' to represent the call depth.
349 This is slightly complicated because this function may be called
350 from execute_command and execute_control_command. Unfortunately
351 execute_command also prints the top level control commands.
352 In these cases execute_command will call execute_control_command
353 via while_command or if_command. Inner levels of 'if' and 'while'
354 are dealt with directly. Therefore we can use these functions
355 to determine whether the command has been printed already or not. */
357 print_command_trace (const char *cmd
)
361 if (suppress_next_print_command_trace
)
363 suppress_next_print_command_trace
= 0;
367 if (!source_verbose
&& !trace_commands
)
370 for (i
=0; i
< command_nest_depth
; i
++)
371 printf_filtered ("+");
373 printf_filtered ("%s\n", cmd
);
376 enum command_control_type
377 execute_control_command (struct command_line
*cmd
)
379 struct expression
*expr
;
380 struct command_line
*current
;
381 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
383 struct value
*val_mark
;
385 enum command_control_type ret
;
388 /* Start by assuming failure, if a problem is detected, the code
389 below will simply "break" out of the switch. */
390 ret
= invalid_control
;
392 switch (cmd
->control_type
)
395 /* A simple command, execute it and return. */
396 new_line
= insert_args (cmd
->line
);
399 make_cleanup (free_current_contents
, &new_line
);
400 execute_command (new_line
, 0);
401 ret
= cmd
->control_type
;
404 case continue_control
:
405 print_command_trace ("loop_continue");
407 /* Return for "continue", and "break" so we can either
408 continue the loop at the top, or break out. */
409 ret
= cmd
->control_type
;
413 print_command_trace ("loop_break");
415 /* Return for "continue", and "break" so we can either
416 continue the loop at the top, or break out. */
417 ret
= cmd
->control_type
;
422 char *buffer
= alloca (strlen (cmd
->line
) + 7);
423 sprintf (buffer
, "while %s", cmd
->line
);
424 print_command_trace (buffer
);
426 /* Parse the loop control expression for the while statement. */
427 new_line
= insert_args (cmd
->line
);
430 make_cleanup (free_current_contents
, &new_line
);
431 expr
= parse_expression (new_line
);
432 make_cleanup (free_current_contents
, &expr
);
434 ret
= simple_control
;
437 /* Keep iterating so long as the expression is true. */
444 /* Evaluate the expression. */
445 val_mark
= value_mark ();
446 val
= evaluate_expression (expr
);
447 cond_result
= value_true (val
);
448 value_free_to_mark (val_mark
);
450 /* If the value is false, then break out of the loop. */
454 /* Execute the body of the while statement. */
455 current
= *cmd
->body_list
;
458 command_nest_depth
++;
459 ret
= execute_control_command (current
);
460 command_nest_depth
--;
462 /* If we got an error, or a "break" command, then stop
464 if (ret
== invalid_control
|| ret
== break_control
)
470 /* If we got a "continue" command, then restart the loop
472 if (ret
== continue_control
)
475 /* Get the next statement. */
476 current
= current
->next
;
480 /* Reset RET so that we don't recurse the break all the way down. */
481 if (ret
== break_control
)
482 ret
= simple_control
;
489 char *buffer
= alloca (strlen (cmd
->line
) + 4);
490 sprintf (buffer
, "if %s", cmd
->line
);
491 print_command_trace (buffer
);
493 new_line
= insert_args (cmd
->line
);
496 make_cleanup (free_current_contents
, &new_line
);
497 /* Parse the conditional for the if statement. */
498 expr
= parse_expression (new_line
);
499 make_cleanup (free_current_contents
, &expr
);
502 ret
= simple_control
;
504 /* Evaluate the conditional. */
505 val_mark
= value_mark ();
506 val
= evaluate_expression (expr
);
508 /* Choose which arm to take commands from based on the value of the
509 conditional expression. */
510 if (value_true (val
))
511 current
= *cmd
->body_list
;
512 else if (cmd
->body_count
== 2)
513 current
= *(cmd
->body_list
+ 1);
514 value_free_to_mark (val_mark
);
516 /* Execute commands in the given arm. */
519 command_nest_depth
++;
520 ret
= execute_control_command (current
);
521 command_nest_depth
--;
523 /* If we got an error, get out. */
524 if (ret
!= simple_control
)
527 /* Get the next statement in the body. */
528 current
= current
->next
;
533 case commands_control
:
535 /* Breakpoint commands list, record the commands in the breakpoint's
536 command list and return. */
537 new_line
= insert_args (cmd
->line
);
540 make_cleanup (free_current_contents
, &new_line
);
541 ret
= commands_from_control_command (new_line
, cmd
);
546 eval_python_from_control_command (cmd
);
547 ret
= simple_control
;
552 warning (_("Invalid control type in canned commands structure."));
556 do_cleanups (old_chain
);
561 /* Like execute_control_command, but first set
562 suppress_next_print_command_trace. */
564 enum command_control_type
565 execute_control_command_untraced (struct command_line
*cmd
)
567 suppress_next_print_command_trace
= 1;
568 return execute_control_command (cmd
);
572 /* "while" command support. Executes a body of statements while the
573 loop condition is nonzero. */
576 while_command (char *arg
, int from_tty
)
578 struct command_line
*command
= NULL
;
581 command
= get_command_line (while_control
, arg
);
586 execute_control_command_untraced (command
);
587 free_command_lines (&command
);
590 /* "if" command support. Execute either the true or false arm depending
591 on the value of the if conditional. */
594 if_command (char *arg
, int from_tty
)
596 struct command_line
*command
= NULL
;
599 command
= get_command_line (if_control
, arg
);
604 execute_control_command_untraced (command
);
605 free_command_lines (&command
);
610 arg_cleanup (void *ignore
)
612 struct user_args
*oargs
= user_args
;
614 internal_error (__FILE__
, __LINE__
,
615 _("arg_cleanup called with no user args.\n"));
617 user_args
= user_args
->next
;
618 xfree (oargs
->command
);
622 /* Bind the incomming arguments for a user defined command to
623 $arg0, $arg1 ... $argMAXUSERARGS. */
625 static struct cleanup
*
626 setup_user_args (char *p
)
628 struct user_args
*args
;
629 struct cleanup
*old_chain
;
630 unsigned int arg_count
= 0;
632 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
633 memset (args
, 0, sizeof (struct user_args
));
635 args
->next
= user_args
;
638 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
643 user_args
->command
= p
= xstrdup (p
);
652 if (arg_count
>= MAXUSERARGS
)
654 error (_("user defined function may only have %d arguments."),
659 /* Strip whitespace. */
660 while (*p
== ' ' || *p
== '\t')
663 /* P now points to an argument. */
665 user_args
->a
[arg_count
].arg
= p
;
667 /* Get to the end of this argument. */
670 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
699 user_args
->a
[arg_count
].len
= p
- start_arg
;
706 /* Given character string P, return a point to the first argument ($arg),
707 or NULL if P contains no arguments. */
712 while ((p
= strchr (p
, '$')))
714 if (strncmp (p
, "$arg", 4) == 0
715 && (isdigit (p
[4]) || p
[4] == 'c'))
722 /* Insert the user defined arguments stored in user_arg into the $arg
723 arguments found in line, with the updated copy being placed into nline. */
726 insert_args (char *line
)
728 char *p
, *save_line
, *new_line
;
731 /* If we are not in a user-defined function, treat $argc, $arg0, et
732 cetera as normal convenience variables. */
733 if (user_args
== NULL
)
734 return xstrdup (line
);
736 /* First we need to know how much memory to allocate for the new line. */
739 while ((p
= locate_arg (line
)))
746 /* $argc. Number will be <=10. */
747 len
+= user_args
->count
== 10 ? 2 : 1;
749 else if (i
>= user_args
->count
)
751 error (_("Missing argument %d in user function."), i
);
756 len
+= user_args
->a
[i
].len
;
761 /* Don't forget the tail. */
762 len
+= strlen (line
);
764 /* Allocate space for the new line and fill it in. */
765 new_line
= (char *) xmalloc (len
+ 1);
766 if (new_line
== NULL
)
769 /* Restore pointer to beginning of old line. */
772 /* Save pointer to beginning of new line. */
773 save_line
= new_line
;
775 while ((p
= locate_arg (line
)))
779 memcpy (new_line
, line
, p
- line
);
780 new_line
+= p
- line
;
784 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
785 if (user_args
->count
== 10)
791 *(new_line
++) = user_args
->count
+ '0';
796 len
= user_args
->a
[i
].len
;
799 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
805 /* Don't forget the tail. */
806 strcpy (new_line
, line
);
808 /* Return a pointer to the beginning of the new line. */
813 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
814 code bodies. This is typically used when we encounter an "else"
815 clause for an "if" command. */
818 realloc_body_list (struct command_line
*command
, int new_length
)
821 struct command_line
**body_list
;
823 n
= command
->body_count
;
829 body_list
= (struct command_line
**)
830 xmalloc (sizeof (struct command_line
*) * new_length
);
832 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
833 memset (body_list
+ n
, 0, sizeof (struct command_line
*) * (new_length
- n
));
835 xfree (command
->body_list
);
836 command
->body_list
= body_list
;
837 command
->body_count
= new_length
;
840 /* Read one line from the input stream. If the command is an "end",
841 return such an indication to the caller. If PARSE_COMMANDS is true,
842 strip leading whitespace (trailing whitespace is always stripped)
843 in the line, attempt to recognize GDB control commands, and also
844 return an indication if the command is an "else" or a nop.
845 Otherwise, only "end" is recognized. */
847 static enum misc_command_type
848 read_next_line (struct command_line
**command
, int parse_commands
)
850 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
854 if (control_level
>= 254)
855 error (_("Control nesting too deep!"));
857 /* Set a prompt based on the nesting of the control commands. */
858 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
860 for (i
= 0; i
< control_level
; i
++)
861 control_prompt
[i
] = ' ';
862 control_prompt
[i
] = '>';
863 control_prompt
[i
+ 1] = '\0';
864 prompt_ptr
= (char *) &control_prompt
[0];
869 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
871 /* Not sure what to do here. */
877 /* Strip leading whitespace. */
878 while (*p
== ' ' || *p
== '\t')
882 /* Strip trailing whitespace. */
884 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
887 /* Is this the end of a simple, while, or if control structure? */
888 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
893 /* Blanks and comments don't really do anything, but we need to
894 distinguish them from else, end and other commands which can be
896 if (p1
== p
|| p
[0] == '#')
899 /* Is the else clause of an if control structure? */
900 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
903 /* Check for while, if, break, continue, etc and build a new command
904 line structure for them. */
905 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
909 while (first_arg
< p1
&& isspace (*first_arg
))
911 *command
= build_command_line (while_control
, first_arg
);
913 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
917 while (first_arg
< p1
&& isspace (*first_arg
))
919 *command
= build_command_line (if_control
, first_arg
);
921 else if (p1
- p
>= 8 && !strncmp (p
, "commands", 8))
925 while (first_arg
< p1
&& isspace (*first_arg
))
927 *command
= build_command_line (commands_control
, first_arg
);
929 else if (p1
- p
== 6 && !strncmp (p
, "python", 6))
931 /* Note that we ignore the inline "python command" form
933 *command
= build_command_line (python_control
, "");
935 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
937 *command
= (struct command_line
*)
938 xmalloc (sizeof (struct command_line
));
939 (*command
)->next
= NULL
;
940 (*command
)->line
= NULL
;
941 (*command
)->control_type
= break_control
;
942 (*command
)->body_count
= 0;
943 (*command
)->body_list
= NULL
;
945 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
947 *command
= (struct command_line
*)
948 xmalloc (sizeof (struct command_line
));
949 (*command
)->next
= NULL
;
950 (*command
)->line
= NULL
;
951 (*command
)->control_type
= continue_control
;
952 (*command
)->body_count
= 0;
953 (*command
)->body_list
= NULL
;
959 if (!parse_commands
|| not_handled
)
961 /* A normal command. */
962 *command
= (struct command_line
*)
963 xmalloc (sizeof (struct command_line
));
964 (*command
)->next
= NULL
;
965 (*command
)->line
= savestring (p
, p1
- p
);
966 (*command
)->control_type
= simple_control
;
967 (*command
)->body_count
= 0;
968 (*command
)->body_list
= NULL
;
971 /* Nothing special. */
975 /* Recursively read in the control structures and create a command_line
978 The parent_control parameter is the control structure in which the
979 following commands are nested. */
981 static enum command_control_type
982 recurse_read_control_structure (struct command_line
*current_cmd
)
985 enum misc_command_type val
;
986 enum command_control_type ret
;
987 struct command_line
**body_ptr
, *child_tail
, *next
;
993 if (current_cmd
->control_type
== simple_control
)
994 error (_("Recursed on a simple control type."));
996 if (current_body
> current_cmd
->body_count
)
997 error (_("Allocated body is smaller than this command type needs."));
999 /* Read lines from the input stream and build control structures. */
1005 val
= read_next_line (&next
, current_cmd
->control_type
!= python_control
);
1007 /* Just skip blanks and comments. */
1008 if (val
== nop_command
)
1011 if (val
== end_command
)
1013 if (current_cmd
->control_type
== while_control
1014 || current_cmd
->control_type
== if_control
1015 || current_cmd
->control_type
== python_control
1016 || current_cmd
->control_type
== commands_control
)
1018 /* Success reading an entire canned sequence of commands. */
1019 ret
= simple_control
;
1024 ret
= invalid_control
;
1029 /* Not the end of a control structure. */
1030 if (val
== else_command
)
1032 if (current_cmd
->control_type
== if_control
1033 && current_body
== 1)
1035 realloc_body_list (current_cmd
, 2);
1042 ret
= invalid_control
;
1049 child_tail
->next
= next
;
1053 body_ptr
= current_cmd
->body_list
;
1054 for (i
= 1; i
< current_body
; i
++)
1063 /* If the latest line is another control structure, then recurse
1065 if (next
->control_type
== while_control
1066 || next
->control_type
== if_control
1067 || next
->control_type
== python_control
1068 || next
->control_type
== commands_control
)
1071 ret
= recurse_read_control_structure (next
);
1074 if (ret
!= simple_control
)
1084 /* Read lines from the input stream and accumulate them in a chain of
1085 struct command_line's, which is then returned. For input from a
1086 terminal, the special command "end" is used to mark the end of the
1087 input, and is not included in the returned chain of commands.
1089 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1090 is always stripped) in the line and attempt to recognize GDB control
1091 commands. Otherwise, only "end" is recognized. */
1093 #define END_MESSAGE "End with a line saying just \"end\"."
1095 struct command_line
*
1096 read_command_lines (char *prompt_arg
, int from_tty
, int parse_commands
)
1098 struct command_line
*head
, *tail
, *next
;
1099 struct cleanup
*old_chain
;
1100 enum command_control_type ret
;
1101 enum misc_command_type val
;
1105 if (from_tty
&& input_from_terminal_p ())
1107 if (deprecated_readline_begin_hook
)
1109 /* Note - intentional to merge messages with no newline */
1110 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
1114 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1115 gdb_flush (gdb_stdout
);
1125 val
= read_next_line (&next
, parse_commands
);
1127 /* Ignore blank lines or comments. */
1128 if (val
== nop_command
)
1131 if (val
== end_command
)
1133 ret
= simple_control
;
1137 if (val
!= ok_command
)
1139 ret
= invalid_control
;
1143 if (next
->control_type
== while_control
1144 || next
->control_type
== if_control
1145 || next
->control_type
== python_control
1146 || next
->control_type
== commands_control
)
1149 ret
= recurse_read_control_structure (next
);
1152 if (ret
== invalid_control
)
1163 old_chain
= make_cleanup_free_command_lines (&head
);
1172 if (ret
!= invalid_control
)
1174 discard_cleanups (old_chain
);
1177 do_cleanups (old_chain
);
1180 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1182 (*deprecated_readline_end_hook
) ();
1187 /* Free a chain of struct command_line's. */
1190 free_command_lines (struct command_line
**lptr
)
1192 struct command_line
*l
= *lptr
;
1193 struct command_line
*next
;
1194 struct command_line
**blist
;
1199 if (l
->body_count
> 0)
1201 blist
= l
->body_list
;
1202 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1203 free_command_lines (blist
);
1214 do_free_command_lines_cleanup (void *arg
)
1216 free_command_lines (arg
);
1220 make_cleanup_free_command_lines (struct command_line
**arg
)
1222 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1225 struct command_line
*
1226 copy_command_lines (struct command_line
*cmds
)
1228 struct command_line
*result
= NULL
;
1232 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1234 result
->next
= copy_command_lines (cmds
->next
);
1235 result
->line
= xstrdup (cmds
->line
);
1236 result
->control_type
= cmds
->control_type
;
1237 result
->body_count
= cmds
->body_count
;
1238 if (cmds
->body_count
> 0)
1242 result
->body_list
= (struct command_line
**)
1243 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1245 for (i
= 0; i
< cmds
->body_count
; i
++)
1246 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1249 result
->body_list
= NULL
;
1255 /* Validate that *COMNAME is a valid name for a command. Return the
1256 containing command list, in case it starts with a prefix command.
1257 The prefix must already exist. *COMNAME is advanced to point after
1258 any prefix, and a NUL character overwrites the space after the
1261 static struct cmd_list_element
**
1262 validate_comname (char **comname
)
1264 struct cmd_list_element
**list
= &cmdlist
;
1265 char *p
, *last_word
;
1268 error_no_arg (_("name of command to define"));
1270 /* Find the last word of the argument. */
1271 p
= *comname
+ strlen (*comname
);
1272 while (p
> *comname
&& isspace (p
[-1]))
1274 while (p
> *comname
&& !isspace (p
[-1]))
1278 /* Find the corresponding command list. */
1279 if (last_word
!= *comname
)
1281 struct cmd_list_element
*c
;
1282 char saved_char
, *tem
= *comname
;
1284 /* Separate the prefix and the command. */
1285 saved_char
= last_word
[-1];
1286 last_word
[-1] = '\0';
1288 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1289 if (c
->prefixlist
== NULL
)
1290 error (_("\"%s\" is not a prefix command."), *comname
);
1292 list
= c
->prefixlist
;
1293 last_word
[-1] = saved_char
;
1294 *comname
= last_word
;
1300 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1301 error (_("Junk in argument list: \"%s\""), p
);
1308 /* This is just a placeholder in the command data structures. */
1310 user_defined_command (char *ignore
, int from_tty
)
1315 define_command (char *comname
, int from_tty
)
1317 #define MAX_TMPBUF 128
1324 struct command_line
*cmds
;
1325 struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0, **list
;
1326 char *tem
, *tem2
, *comfull
;
1327 char tmpbuf
[MAX_TMPBUF
];
1328 int hook_type
= CMD_NO_HOOK
;
1329 int hook_name_size
= 0;
1331 #define HOOK_STRING "hook-"
1333 #define HOOK_POST_STRING "hookpost-"
1334 #define HOOK_POST_LEN 9
1337 list
= validate_comname (&comname
);
1339 /* Look it up, and verify that we got an exact match. */
1341 c
= lookup_cmd (&tem
, *list
, "", -1, 1);
1342 if (c
&& strcmp (comname
, c
->name
) != 0)
1348 if (c
->class == class_user
|| c
->class == class_alias
)
1349 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1351 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1353 error (_("Command \"%s\" not redefined."), c
->name
);
1356 /* If this new command is a hook, then mark the command which it
1357 is hooking. Note that we allow hooking `help' commands, so that
1358 we can hook the `stop' pseudo-command. */
1360 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1362 hook_type
= CMD_PRE_HOOK
;
1363 hook_name_size
= HOOK_LEN
;
1365 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1367 hook_type
= CMD_POST_HOOK
;
1368 hook_name_size
= HOOK_POST_LEN
;
1371 if (hook_type
!= CMD_NO_HOOK
)
1373 /* Look up cmd it hooks, and verify that we got an exact match. */
1374 tem
= comname
+ hook_name_size
;
1375 hookc
= lookup_cmd (&tem
, *list
, "", -1, 0);
1376 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1380 warning (_("Your new `%s' command does not hook any existing command."),
1382 if (!query ("Proceed? "))
1383 error (_("Not confirmed."));
1387 comname
= savestring (comname
, strlen (comname
));
1389 /* If the rest of the commands will be case insensitive, this one
1390 should behave in the same manner. */
1391 for (tem
= comname
; *tem
; tem
++)
1393 *tem
= tolower (*tem
);
1395 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comfull
);
1396 cmds
= read_command_lines (tmpbuf
, from_tty
, 1);
1398 if (c
&& c
->class == class_user
)
1399 free_command_lines (&c
->user_commands
);
1401 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1402 (c
&& c
->class == class_user
)
1403 ? c
->doc
: savestring ("User-defined.", 13), list
);
1404 newc
->user_commands
= cmds
;
1406 /* If this new command is a hook, then mark both commands as being
1413 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1414 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1417 hookc
->hook_post
= newc
; /* Target gets hooked. */
1418 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1421 /* Should never come here as hookc would be 0. */
1422 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1428 document_command (char *comname
, int from_tty
)
1430 struct command_line
*doclines
;
1431 struct cmd_list_element
*c
, **list
;
1432 char *tem
, *comfull
;
1436 list
= validate_comname (&comname
);
1439 c
= lookup_cmd (&tem
, *list
, "", 0, 1);
1441 if (c
->class != class_user
)
1442 error (_("Command \"%s\" is built-in."), comfull
);
1444 sprintf (tmpbuf
, "Type documentation for \"%s\".", comfull
);
1445 doclines
= read_command_lines (tmpbuf
, from_tty
, 0);
1451 struct command_line
*cl1
;
1454 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1455 len
+= strlen (cl1
->line
) + 1;
1457 c
->doc
= (char *) xmalloc (len
+ 1);
1460 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1462 strcat (c
->doc
, cl1
->line
);
1464 strcat (c
->doc
, "\n");
1468 free_command_lines (&doclines
);
1471 struct source_cleanup_lines_args
1478 source_cleanup_lines (void *args
)
1480 struct source_cleanup_lines_args
*p
=
1481 (struct source_cleanup_lines_args
*) args
;
1482 source_line_number
= p
->old_line
;
1483 source_file_name
= p
->old_file
;
1486 struct wrapped_read_command_file_args
1492 wrapped_read_command_file (struct ui_out
*uiout
, void *data
)
1494 struct wrapped_read_command_file_args
*args
= data
;
1495 read_command_file (args
->stream
);
1498 /* Used to implement source_command */
1501 script_from_file (FILE *stream
, char *file
)
1503 struct cleanup
*old_cleanups
;
1504 struct source_cleanup_lines_args old_lines
;
1508 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1510 old_cleanups
= make_cleanup_fclose (stream
);
1512 old_lines
.old_line
= source_line_number
;
1513 old_lines
.old_file
= source_file_name
;
1514 make_cleanup (source_cleanup_lines
, &old_lines
);
1515 source_line_number
= 0;
1516 source_file_name
= file
;
1517 /* This will get set every time we read a line. So it won't stay "" for
1519 error_pre_print
= "";
1522 struct gdb_exception e
;
1523 struct wrapped_read_command_file_args args
;
1524 args
.stream
= stream
;
1525 e
= catch_exception (uiout
, wrapped_read_command_file
, &args
,
1532 /* Re-throw the error, but with the file name information
1534 throw_error (e
.error
,
1535 _("%s:%d: Error in sourced command file:\n%s"),
1536 source_file_name
, source_line_number
, e
.message
);
1538 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1542 do_cleanups (old_cleanups
);
1545 /* Print the definition of user command C to STREAM. Or, if C is a
1546 prefix command, show the definitions of all user commands under C
1547 (recursively). PREFIX and NAME combined are the name of the
1550 show_user_1 (struct cmd_list_element
*c
, char *prefix
, char *name
,
1551 struct ui_file
*stream
)
1553 struct command_line
*cmdlines
;
1555 if (c
->prefixlist
!= NULL
)
1557 char *prefixname
= c
->prefixname
;
1558 for (c
= *c
->prefixlist
; c
!= NULL
; c
= c
->next
)
1559 if (c
->class == class_user
|| c
->prefixlist
!= NULL
)
1560 show_user_1 (c
, prefixname
, c
->name
, gdb_stdout
);
1564 cmdlines
= c
->user_commands
;
1567 fprintf_filtered (stream
, "User command \"%s%s\":\n", prefix
, name
);
1569 print_command_lines (uiout
, cmdlines
, 1);
1570 fputs_filtered ("\n", stream
);