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, 2010 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 (char * (*read_next_line_func
) (void),
43 struct command_line
*current_cmd
,
44 void (*validator
)(char *, void *),
47 static char *insert_args (char *line
);
49 static struct cleanup
* setup_user_args (char *p
);
51 static char *read_next_line (void);
53 /* Level of control structure when reading. */
54 static int control_level
;
56 /* Level of control structure when executing. */
57 static int command_nest_depth
= 1;
59 /* This is to prevent certain commands being printed twice. */
60 static int suppress_next_print_command_trace
= 0;
62 /* Structure for arguments to user defined functions. */
63 #define MAXUSERARGS 10
66 struct user_args
*next
;
67 /* It is necessary to store a malloced copy of the command line to
68 ensure that the arguments are not overwritten before they are used. */
81 /* Allocate, initialize a new command line structure for one of the
82 control commands (if/while). */
84 static struct command_line
*
85 build_command_line (enum command_control_type type
, char *args
)
87 struct command_line
*cmd
;
89 if (args
== NULL
&& (type
== if_control
|| type
== while_control
))
90 error (_("if/while commands require arguments."));
91 gdb_assert (args
!= NULL
);
93 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
95 cmd
->control_type
= type
;
99 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
101 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
102 cmd
->line
= xstrdup (args
);
107 /* Build and return a new command structure for the control commands
108 such as "if" and "while". */
110 struct command_line
*
111 get_command_line (enum command_control_type type
, char *arg
)
113 struct command_line
*cmd
;
114 struct cleanup
*old_chain
= NULL
;
116 /* Allocate and build a new command line structure. */
117 cmd
= build_command_line (type
, arg
);
119 old_chain
= make_cleanup_free_command_lines (&cmd
);
121 /* Read in the body of this command. */
122 if (recurse_read_control_structure (read_next_line
, cmd
, 0, 0)
125 warning (_("Error reading in canned sequence of commands."));
126 do_cleanups (old_chain
);
130 discard_cleanups (old_chain
);
134 /* Recursively print a command (including full control structures). */
137 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
140 struct command_line
*list
;
146 ui_out_spaces (uiout
, 2 * depth
);
148 /* A simple command, print it and continue. */
149 if (list
->control_type
== simple_control
)
151 ui_out_field_string (uiout
, NULL
, list
->line
);
152 ui_out_text (uiout
, "\n");
157 /* loop_continue to jump to the start of a while loop, print it
159 if (list
->control_type
== continue_control
)
161 ui_out_field_string (uiout
, NULL
, "loop_continue");
162 ui_out_text (uiout
, "\n");
167 /* loop_break to break out of a while loop, print it and continue. */
168 if (list
->control_type
== break_control
)
170 ui_out_field_string (uiout
, NULL
, "loop_break");
171 ui_out_text (uiout
, "\n");
176 /* A while command. Recursively print its subcommands and continue. */
177 if (list
->control_type
== while_control
178 || list
->control_type
== while_stepping_control
)
180 /* For while-stepping, the line includes the 'while-stepping' token.
181 See comment in process_next_line for explanation. Here,
182 take care not print 'while-stepping' twice. */
183 if (list
->control_type
== while_control
)
184 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
186 ui_out_field_string (uiout
, NULL
, list
->line
);
187 ui_out_text (uiout
, "\n");
188 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
190 ui_out_spaces (uiout
, 2 * depth
);
191 ui_out_field_string (uiout
, NULL
, "end");
192 ui_out_text (uiout
, "\n");
197 /* An if command. Recursively print both arms before continueing. */
198 if (list
->control_type
== if_control
)
200 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
201 ui_out_text (uiout
, "\n");
203 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
205 /* Show the false arm if it exists. */
206 if (list
->body_count
== 2)
209 ui_out_spaces (uiout
, 2 * depth
);
210 ui_out_field_string (uiout
, NULL
, "else");
211 ui_out_text (uiout
, "\n");
212 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
216 ui_out_spaces (uiout
, 2 * depth
);
217 ui_out_field_string (uiout
, NULL
, "end");
218 ui_out_text (uiout
, "\n");
223 /* A commands command. Print the breakpoint commands and continue. */
224 if (list
->control_type
== commands_control
)
227 ui_out_field_fmt (uiout
, NULL
, "commands %s", list
->line
);
229 ui_out_field_string (uiout
, NULL
, "commands");
230 ui_out_text (uiout
, "\n");
231 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
233 ui_out_spaces (uiout
, 2 * depth
);
234 ui_out_field_string (uiout
, NULL
, "end");
235 ui_out_text (uiout
, "\n");
240 if (list
->control_type
== python_control
)
242 ui_out_field_string (uiout
, NULL
, "python");
243 ui_out_text (uiout
, "\n");
244 /* Don't indent python code at all. */
245 print_command_lines (uiout
, *list
->body_list
, 0);
247 ui_out_spaces (uiout
, 2 * depth
);
248 ui_out_field_string (uiout
, NULL
, "end");
249 ui_out_text (uiout
, "\n");
254 /* ignore illegal command type and try next */
259 /* Handle pre-post hooks. */
262 clear_hook_in_cleanup (void *data
)
264 struct cmd_list_element
*c
= data
;
266 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
270 execute_cmd_pre_hook (struct cmd_list_element
*c
)
272 if ((c
->hook_pre
) && (!c
->hook_in
))
274 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
275 c
->hook_in
= 1; /* Prevent recursive hooking */
276 execute_user_command (c
->hook_pre
, (char *) 0);
277 do_cleanups (cleanups
);
282 execute_cmd_post_hook (struct cmd_list_element
*c
)
284 if ((c
->hook_post
) && (!c
->hook_in
))
286 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
288 c
->hook_in
= 1; /* Prevent recursive hooking */
289 execute_user_command (c
->hook_post
, (char *) 0);
290 do_cleanups (cleanups
);
294 /* Execute the command in CMD. */
296 do_restore_user_call_depth (void * call_depth
)
298 int *depth
= call_depth
;
307 execute_user_command (struct cmd_list_element
*c
, char *args
)
309 struct command_line
*cmdlines
;
310 struct cleanup
*old_chain
;
311 enum command_control_type ret
;
312 static int user_call_depth
= 0;
313 extern int max_user_call_depth
;
315 old_chain
= setup_user_args (args
);
317 cmdlines
= c
->user_commands
;
322 if (++user_call_depth
> max_user_call_depth
)
323 error (_("Max user call depth exceeded -- command aborted."));
325 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
327 /* Set the instream to 0, indicating execution of a
328 user-defined function. */
329 make_cleanup (do_restore_instream_cleanup
, instream
);
330 instream
= (FILE *) 0;
332 /* Also set the global in_user_command, so that NULL instream is
333 not confused with Insight. */
336 command_nest_depth
++;
339 ret
= execute_control_command (cmdlines
);
340 if (ret
!= simple_control
&& ret
!= break_control
)
342 warning (_("Error executing canned sequence of commands."));
345 cmdlines
= cmdlines
->next
;
347 command_nest_depth
--;
348 do_cleanups (old_chain
);
351 /* This function is called every time GDB prints a prompt.
352 It ensures that errors and the like to not confuse the command tracing. */
355 reset_command_nest_depth (void)
357 command_nest_depth
= 1;
360 suppress_next_print_command_trace
= 0;
363 /* Print the command, prefixed with '+' to represent the call depth.
364 This is slightly complicated because this function may be called
365 from execute_command and execute_control_command. Unfortunately
366 execute_command also prints the top level control commands.
367 In these cases execute_command will call execute_control_command
368 via while_command or if_command. Inner levels of 'if' and 'while'
369 are dealt with directly. Therefore we can use these functions
370 to determine whether the command has been printed already or not. */
372 print_command_trace (const char *cmd
)
376 if (suppress_next_print_command_trace
)
378 suppress_next_print_command_trace
= 0;
382 if (!source_verbose
&& !trace_commands
)
385 for (i
=0; i
< command_nest_depth
; i
++)
386 printf_filtered ("+");
388 printf_filtered ("%s\n", cmd
);
391 enum command_control_type
392 execute_control_command (struct command_line
*cmd
)
394 struct expression
*expr
;
395 struct command_line
*current
;
396 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
398 struct value
*val_mark
;
400 enum command_control_type ret
;
403 /* Start by assuming failure, if a problem is detected, the code
404 below will simply "break" out of the switch. */
405 ret
= invalid_control
;
407 switch (cmd
->control_type
)
410 /* A simple command, execute it and return. */
411 new_line
= insert_args (cmd
->line
);
414 make_cleanup (free_current_contents
, &new_line
);
415 execute_command (new_line
, 0);
416 ret
= cmd
->control_type
;
419 case continue_control
:
420 print_command_trace ("loop_continue");
422 /* Return for "continue", and "break" so we can either
423 continue the loop at the top, or break out. */
424 ret
= cmd
->control_type
;
428 print_command_trace ("loop_break");
430 /* Return for "continue", and "break" so we can either
431 continue the loop at the top, or break out. */
432 ret
= cmd
->control_type
;
437 char *buffer
= alloca (strlen (cmd
->line
) + 7);
439 sprintf (buffer
, "while %s", cmd
->line
);
440 print_command_trace (buffer
);
442 /* Parse the loop control expression for the while statement. */
443 new_line
= insert_args (cmd
->line
);
446 make_cleanup (free_current_contents
, &new_line
);
447 expr
= parse_expression (new_line
);
448 make_cleanup (free_current_contents
, &expr
);
450 ret
= simple_control
;
453 /* Keep iterating so long as the expression is true. */
460 /* Evaluate the expression. */
461 val_mark
= value_mark ();
462 val
= evaluate_expression (expr
);
463 cond_result
= value_true (val
);
464 value_free_to_mark (val_mark
);
466 /* If the value is false, then break out of the loop. */
470 /* Execute the body of the while statement. */
471 current
= *cmd
->body_list
;
474 command_nest_depth
++;
475 ret
= execute_control_command (current
);
476 command_nest_depth
--;
478 /* If we got an error, or a "break" command, then stop
480 if (ret
== invalid_control
|| ret
== break_control
)
486 /* If we got a "continue" command, then restart the loop
488 if (ret
== continue_control
)
491 /* Get the next statement. */
492 current
= current
->next
;
496 /* Reset RET so that we don't recurse the break all the way down. */
497 if (ret
== break_control
)
498 ret
= simple_control
;
505 char *buffer
= alloca (strlen (cmd
->line
) + 4);
507 sprintf (buffer
, "if %s", cmd
->line
);
508 print_command_trace (buffer
);
510 new_line
= insert_args (cmd
->line
);
513 make_cleanup (free_current_contents
, &new_line
);
514 /* Parse the conditional for the if statement. */
515 expr
= parse_expression (new_line
);
516 make_cleanup (free_current_contents
, &expr
);
519 ret
= simple_control
;
521 /* Evaluate the conditional. */
522 val_mark
= value_mark ();
523 val
= evaluate_expression (expr
);
525 /* Choose which arm to take commands from based on the value of the
526 conditional expression. */
527 if (value_true (val
))
528 current
= *cmd
->body_list
;
529 else if (cmd
->body_count
== 2)
530 current
= *(cmd
->body_list
+ 1);
531 value_free_to_mark (val_mark
);
533 /* Execute commands in the given arm. */
536 command_nest_depth
++;
537 ret
= execute_control_command (current
);
538 command_nest_depth
--;
540 /* If we got an error, get out. */
541 if (ret
!= simple_control
)
544 /* Get the next statement in the body. */
545 current
= current
->next
;
550 case commands_control
:
552 /* Breakpoint commands list, record the commands in the breakpoint's
553 command list and return. */
554 new_line
= insert_args (cmd
->line
);
557 make_cleanup (free_current_contents
, &new_line
);
558 ret
= commands_from_control_command (new_line
, cmd
);
563 eval_python_from_control_command (cmd
);
564 ret
= simple_control
;
569 warning (_("Invalid control type in canned commands structure."));
573 do_cleanups (old_chain
);
578 /* Like execute_control_command, but first set
579 suppress_next_print_command_trace. */
581 enum command_control_type
582 execute_control_command_untraced (struct command_line
*cmd
)
584 suppress_next_print_command_trace
= 1;
585 return execute_control_command (cmd
);
589 /* "while" command support. Executes a body of statements while the
590 loop condition is nonzero. */
593 while_command (char *arg
, int from_tty
)
595 struct command_line
*command
= NULL
;
598 command
= get_command_line (while_control
, arg
);
603 execute_control_command_untraced (command
);
604 free_command_lines (&command
);
607 /* "if" command support. Execute either the true or false arm depending
608 on the value of the if conditional. */
611 if_command (char *arg
, int from_tty
)
613 struct command_line
*command
= NULL
;
616 command
= get_command_line (if_control
, arg
);
621 execute_control_command_untraced (command
);
622 free_command_lines (&command
);
627 arg_cleanup (void *ignore
)
629 struct user_args
*oargs
= user_args
;
632 internal_error (__FILE__
, __LINE__
,
633 _("arg_cleanup called with no user args.\n"));
635 user_args
= user_args
->next
;
636 xfree (oargs
->command
);
640 /* Bind the incomming arguments for a user defined command to
641 $arg0, $arg1 ... $argMAXUSERARGS. */
643 static struct cleanup
*
644 setup_user_args (char *p
)
646 struct user_args
*args
;
647 struct cleanup
*old_chain
;
648 unsigned int arg_count
= 0;
650 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
651 memset (args
, 0, sizeof (struct user_args
));
653 args
->next
= user_args
;
656 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
661 user_args
->command
= p
= xstrdup (p
);
670 if (arg_count
>= MAXUSERARGS
)
672 error (_("user defined function may only have %d arguments."),
677 /* Strip whitespace. */
678 while (*p
== ' ' || *p
== '\t')
681 /* P now points to an argument. */
683 user_args
->a
[arg_count
].arg
= p
;
685 /* Get to the end of this argument. */
688 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
717 user_args
->a
[arg_count
].len
= p
- start_arg
;
724 /* Given character string P, return a point to the first argument ($arg),
725 or NULL if P contains no arguments. */
730 while ((p
= strchr (p
, '$')))
732 if (strncmp (p
, "$arg", 4) == 0
733 && (isdigit (p
[4]) || p
[4] == 'c'))
740 /* Insert the user defined arguments stored in user_arg into the $arg
741 arguments found in line, with the updated copy being placed into nline. */
744 insert_args (char *line
)
746 char *p
, *save_line
, *new_line
;
749 /* If we are not in a user-defined function, treat $argc, $arg0, et
750 cetera as normal convenience variables. */
751 if (user_args
== NULL
)
752 return xstrdup (line
);
754 /* First we need to know how much memory to allocate for the new line. */
757 while ((p
= locate_arg (line
)))
764 /* $argc. Number will be <=10. */
765 len
+= user_args
->count
== 10 ? 2 : 1;
767 else if (i
>= user_args
->count
)
769 error (_("Missing argument %d in user function."), i
);
774 len
+= user_args
->a
[i
].len
;
779 /* Don't forget the tail. */
780 len
+= strlen (line
);
782 /* Allocate space for the new line and fill it in. */
783 new_line
= (char *) xmalloc (len
+ 1);
784 if (new_line
== NULL
)
787 /* Restore pointer to beginning of old line. */
790 /* Save pointer to beginning of new line. */
791 save_line
= new_line
;
793 while ((p
= locate_arg (line
)))
797 memcpy (new_line
, line
, p
- line
);
798 new_line
+= p
- line
;
802 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
803 if (user_args
->count
== 10)
809 *(new_line
++) = user_args
->count
+ '0';
814 len
= user_args
->a
[i
].len
;
817 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
823 /* Don't forget the tail. */
824 strcpy (new_line
, line
);
826 /* Return a pointer to the beginning of the new line. */
831 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
832 code bodies. This is typically used when we encounter an "else"
833 clause for an "if" command. */
836 realloc_body_list (struct command_line
*command
, int new_length
)
839 struct command_line
**body_list
;
841 n
= command
->body_count
;
847 body_list
= (struct command_line
**)
848 xmalloc (sizeof (struct command_line
*) * new_length
);
850 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
851 memset (body_list
+ n
, 0, sizeof (struct command_line
*) * (new_length
- n
));
853 xfree (command
->body_list
);
854 command
->body_list
= body_list
;
855 command
->body_count
= new_length
;
858 /* Read next line from stdout. Passed to read_command_line_1 and
859 recurse_read_control_structure whenever we need to read commands
863 read_next_line (void)
865 char *prompt_ptr
, control_prompt
[256];
868 if (control_level
>= 254)
869 error (_("Control nesting too deep!"));
871 /* Set a prompt based on the nesting of the control commands. */
872 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
874 for (i
= 0; i
< control_level
; i
++)
875 control_prompt
[i
] = ' ';
876 control_prompt
[i
] = '>';
877 control_prompt
[i
+ 1] = '\0';
878 prompt_ptr
= (char *) &control_prompt
[0];
883 return command_line_input (prompt_ptr
, instream
== stdin
, "commands");
886 /* Process one input line. If the command is an "end",
887 return such an indication to the caller. If PARSE_COMMANDS is true,
888 strip leading whitespace (trailing whitespace is always stripped)
889 in the line, attempt to recognize GDB control commands, and also
890 return an indication if the command is an "else" or a nop.
891 Otherwise, only "end" is recognized. */
893 static enum misc_command_type
894 process_next_line (char *p
, struct command_line
**command
, int parse_commands
,
895 void (*validator
)(char *, void *), void *closure
)
901 /* Not sure what to do here. */
905 /* Strip trailing whitespace. */
906 p_end
= p
+ strlen (p
);
907 while (p_end
> p
&& (p_end
[-1] == ' ' || p_end
[-1] == '\t'))
911 /* Strip leading whitespace. */
912 while (p_start
< p_end
&& (*p_start
== ' ' || *p_start
== '\t'))
915 /* 'end' is always recognized, regardless of parse_commands value.
916 We also permit whitespace before end and after. */
917 if (p_end
- p_start
== 3 && !strncmp (p_start
, "end", 3))
922 /* If commands are parsed, we skip initial spaces. Otherwise,
923 which is the case for Python commands and documentation
924 (see the 'document' command), spaces are preserved. */
927 /* Blanks and comments don't really do anything, but we need to
928 distinguish them from else, end and other commands which can be
930 if (p_end
== p
|| p
[0] == '#')
933 /* Is the else clause of an if control structure? */
934 if (p_end
- p
== 4 && !strncmp (p
, "else", 4))
937 /* Check for while, if, break, continue, etc and build a new command
938 line structure for them. */
939 if ((p_end
- p
>= 14 && !strncmp (p
, "while-stepping", 14))
940 || (p_end
- p
>= 8 && !strncmp (p
, "stepping", 8))
941 || (p_end
- p
>= 2 && !strncmp (p
, "ws", 2)))
943 /* Because validate_actionline and encode_action lookup
944 command's line as command, we need the line to
945 include 'while-stepping'.
947 For 'ws' alias, the command will have 'ws', not expanded
948 to 'while-stepping'. This is intentional -- we don't
949 really want frontend to send a command list with 'ws',
950 and next break-info returning command line with 'while-stepping'.
951 This should work, but might cause the breakpoint to be marked as
952 changed while it's actually not. */
953 *command
= build_command_line (while_stepping_control
, p
);
955 else if (p_end
- p
> 5 && !strncmp (p
, "while", 5))
960 while (first_arg
< p_end
&& isspace (*first_arg
))
962 *command
= build_command_line (while_control
, first_arg
);
964 else if (p_end
- p
> 2 && !strncmp (p
, "if", 2))
969 while (first_arg
< p_end
&& isspace (*first_arg
))
971 *command
= build_command_line (if_control
, first_arg
);
973 else if (p_end
- p
>= 8 && !strncmp (p
, "commands", 8))
978 while (first_arg
< p_end
&& isspace (*first_arg
))
980 *command
= build_command_line (commands_control
, first_arg
);
982 else if (p_end
- p
== 6 && !strncmp (p
, "python", 6))
984 /* Note that we ignore the inline "python command" form
986 *command
= build_command_line (python_control
, "");
988 else if (p_end
- p
== 10 && !strncmp (p
, "loop_break", 10))
990 *command
= (struct command_line
*)
991 xmalloc (sizeof (struct command_line
));
992 (*command
)->next
= NULL
;
993 (*command
)->line
= NULL
;
994 (*command
)->control_type
= break_control
;
995 (*command
)->body_count
= 0;
996 (*command
)->body_list
= NULL
;
998 else if (p_end
- p
== 13 && !strncmp (p
, "loop_continue", 13))
1000 *command
= (struct command_line
*)
1001 xmalloc (sizeof (struct command_line
));
1002 (*command
)->next
= NULL
;
1003 (*command
)->line
= NULL
;
1004 (*command
)->control_type
= continue_control
;
1005 (*command
)->body_count
= 0;
1006 (*command
)->body_list
= NULL
;
1012 if (!parse_commands
|| not_handled
)
1014 /* A normal command. */
1015 *command
= (struct command_line
*)
1016 xmalloc (sizeof (struct command_line
));
1017 (*command
)->next
= NULL
;
1018 (*command
)->line
= savestring (p
, p_end
- p
);
1019 (*command
)->control_type
= simple_control
;
1020 (*command
)->body_count
= 0;
1021 (*command
)->body_list
= NULL
;
1026 volatile struct gdb_exception ex
;
1028 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1030 validator ((*command
)->line
, closure
);
1035 throw_exception (ex
);
1039 /* Nothing special. */
1043 /* Recursively read in the control structures and create a command_line
1044 structure from them. Use read_next_line_func to obtain lines of
1049 static enum command_control_type
1050 recurse_read_control_structure (char * (*read_next_line_func
) (void),
1051 struct command_line
*current_cmd
,
1052 void (*validator
)(char *, void *),
1055 int current_body
, i
;
1056 enum misc_command_type val
;
1057 enum command_control_type ret
;
1058 struct command_line
**body_ptr
, *child_tail
, *next
;
1063 /* Sanity checks. */
1064 if (current_cmd
->control_type
== simple_control
)
1065 error (_("Recursed on a simple control type."));
1067 if (current_body
> current_cmd
->body_count
)
1068 error (_("Allocated body is smaller than this command type needs."));
1070 /* Read lines from the input stream and build control structures. */
1076 val
= process_next_line (read_next_line_func (), &next
,
1077 current_cmd
->control_type
!= python_control
,
1078 validator
, closure
);
1080 /* Just skip blanks and comments. */
1081 if (val
== nop_command
)
1084 if (val
== end_command
)
1086 if (current_cmd
->control_type
== while_control
1087 || current_cmd
->control_type
== while_stepping_control
1088 || current_cmd
->control_type
== if_control
1089 || current_cmd
->control_type
== python_control
1090 || current_cmd
->control_type
== commands_control
)
1092 /* Success reading an entire canned sequence of commands. */
1093 ret
= simple_control
;
1098 ret
= invalid_control
;
1103 /* Not the end of a control structure. */
1104 if (val
== else_command
)
1106 if (current_cmd
->control_type
== if_control
1107 && current_body
== 1)
1109 realloc_body_list (current_cmd
, 2);
1116 ret
= invalid_control
;
1123 child_tail
->next
= next
;
1127 body_ptr
= current_cmd
->body_list
;
1128 for (i
= 1; i
< current_body
; i
++)
1137 /* If the latest line is another control structure, then recurse
1139 if (next
->control_type
== while_control
1140 || next
->control_type
== while_stepping_control
1141 || next
->control_type
== if_control
1142 || next
->control_type
== python_control
1143 || next
->control_type
== commands_control
)
1146 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1147 validator
, closure
);
1150 if (ret
!= simple_control
)
1160 /* Read lines from the input stream and accumulate them in a chain of
1161 struct command_line's, which is then returned. For input from a
1162 terminal, the special command "end" is used to mark the end of the
1163 input, and is not included in the returned chain of commands.
1165 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1166 is always stripped) in the line and attempt to recognize GDB control
1167 commands. Otherwise, only "end" is recognized. */
1169 #define END_MESSAGE "End with a line saying just \"end\"."
1171 struct command_line
*
1172 read_command_lines (char *prompt_arg
, int from_tty
, int parse_commands
,
1173 void (*validator
)(char *, void *), void *closure
)
1175 struct command_line
*head
;
1177 if (from_tty
&& input_from_terminal_p ())
1179 if (deprecated_readline_begin_hook
)
1181 /* Note - intentional to merge messages with no newline */
1182 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
1186 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1187 gdb_flush (gdb_stdout
);
1191 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1192 validator
, closure
);
1194 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1196 (*deprecated_readline_end_hook
) ();
1201 /* Act the same way as read_command_lines, except that each new line is
1202 obtained using READ_NEXT_LINE_FUNC. */
1204 struct command_line
*
1205 read_command_lines_1 (char * (*read_next_line_func
) (void), int parse_commands
,
1206 void (*validator
)(char *, void *), void *closure
)
1208 struct command_line
*head
, *tail
, *next
;
1209 struct cleanup
*old_chain
;
1210 enum command_control_type ret
;
1211 enum misc_command_type val
;
1220 val
= process_next_line (read_next_line_func (), &next
, parse_commands
,
1221 validator
, closure
);
1223 /* Ignore blank lines or comments. */
1224 if (val
== nop_command
)
1227 if (val
== end_command
)
1229 ret
= simple_control
;
1233 if (val
!= ok_command
)
1235 ret
= invalid_control
;
1239 if (next
->control_type
== while_control
1240 || next
->control_type
== if_control
1241 || next
->control_type
== python_control
1242 || next
->control_type
== commands_control
1243 || next
->control_type
== while_stepping_control
)
1246 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1247 validator
, closure
);
1250 if (ret
== invalid_control
)
1261 old_chain
= make_cleanup_free_command_lines (&head
);
1270 if (ret
!= invalid_control
)
1272 discard_cleanups (old_chain
);
1275 do_cleanups (old_chain
);
1281 /* Free a chain of struct command_line's. */
1284 free_command_lines (struct command_line
**lptr
)
1286 struct command_line
*l
= *lptr
;
1287 struct command_line
*next
;
1288 struct command_line
**blist
;
1293 if (l
->body_count
> 0)
1295 blist
= l
->body_list
;
1296 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1297 free_command_lines (blist
);
1308 do_free_command_lines_cleanup (void *arg
)
1310 free_command_lines (arg
);
1314 make_cleanup_free_command_lines (struct command_line
**arg
)
1316 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1319 struct command_line
*
1320 copy_command_lines (struct command_line
*cmds
)
1322 struct command_line
*result
= NULL
;
1326 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1328 result
->next
= copy_command_lines (cmds
->next
);
1329 result
->line
= xstrdup (cmds
->line
);
1330 result
->control_type
= cmds
->control_type
;
1331 result
->body_count
= cmds
->body_count
;
1332 if (cmds
->body_count
> 0)
1336 result
->body_list
= (struct command_line
**)
1337 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1339 for (i
= 0; i
< cmds
->body_count
; i
++)
1340 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1343 result
->body_list
= NULL
;
1349 /* Validate that *COMNAME is a valid name for a command. Return the
1350 containing command list, in case it starts with a prefix command.
1351 The prefix must already exist. *COMNAME is advanced to point after
1352 any prefix, and a NUL character overwrites the space after the
1355 static struct cmd_list_element
**
1356 validate_comname (char **comname
)
1358 struct cmd_list_element
**list
= &cmdlist
;
1359 char *p
, *last_word
;
1362 error_no_arg (_("name of command to define"));
1364 /* Find the last word of the argument. */
1365 p
= *comname
+ strlen (*comname
);
1366 while (p
> *comname
&& isspace (p
[-1]))
1368 while (p
> *comname
&& !isspace (p
[-1]))
1372 /* Find the corresponding command list. */
1373 if (last_word
!= *comname
)
1375 struct cmd_list_element
*c
;
1376 char saved_char
, *tem
= *comname
;
1378 /* Separate the prefix and the command. */
1379 saved_char
= last_word
[-1];
1380 last_word
[-1] = '\0';
1382 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1383 if (c
->prefixlist
== NULL
)
1384 error (_("\"%s\" is not a prefix command."), *comname
);
1386 list
= c
->prefixlist
;
1387 last_word
[-1] = saved_char
;
1388 *comname
= last_word
;
1394 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1395 error (_("Junk in argument list: \"%s\""), p
);
1402 /* This is just a placeholder in the command data structures. */
1404 user_defined_command (char *ignore
, int from_tty
)
1409 define_command (char *comname
, int from_tty
)
1411 #define MAX_TMPBUF 128
1418 struct command_line
*cmds
;
1419 struct cmd_list_element
*c
, *newc
, *hookc
= 0, **list
;
1420 char *tem
, *comfull
;
1421 char tmpbuf
[MAX_TMPBUF
];
1422 int hook_type
= CMD_NO_HOOK
;
1423 int hook_name_size
= 0;
1425 #define HOOK_STRING "hook-"
1427 #define HOOK_POST_STRING "hookpost-"
1428 #define HOOK_POST_LEN 9
1431 list
= validate_comname (&comname
);
1433 /* Look it up, and verify that we got an exact match. */
1435 c
= lookup_cmd (&tem
, *list
, "", -1, 1);
1436 if (c
&& strcmp (comname
, c
->name
) != 0)
1443 if (c
->class == class_user
|| c
->class == class_alias
)
1444 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1446 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1448 error (_("Command \"%s\" not redefined."), c
->name
);
1451 /* If this new command is a hook, then mark the command which it
1452 is hooking. Note that we allow hooking `help' commands, so that
1453 we can hook the `stop' pseudo-command. */
1455 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1457 hook_type
= CMD_PRE_HOOK
;
1458 hook_name_size
= HOOK_LEN
;
1460 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1462 hook_type
= CMD_POST_HOOK
;
1463 hook_name_size
= HOOK_POST_LEN
;
1466 if (hook_type
!= CMD_NO_HOOK
)
1468 /* Look up cmd it hooks, and verify that we got an exact match. */
1469 tem
= comname
+ hook_name_size
;
1470 hookc
= lookup_cmd (&tem
, *list
, "", -1, 0);
1471 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1475 warning (_("Your new `%s' command does not hook any existing command."),
1477 if (!query (_("Proceed? ")))
1478 error (_("Not confirmed."));
1482 comname
= xstrdup (comname
);
1484 /* If the rest of the commands will be case insensitive, this one
1485 should behave in the same manner. */
1486 for (tem
= comname
; *tem
; tem
++)
1488 *tem
= tolower (*tem
);
1490 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comfull
);
1491 cmds
= read_command_lines (tmpbuf
, from_tty
, 1, 0, 0);
1493 if (c
&& c
->class == class_user
)
1494 free_command_lines (&c
->user_commands
);
1496 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1497 (c
&& c
->class == class_user
)
1498 ? c
->doc
: xstrdup ("User-defined."), list
);
1499 newc
->user_commands
= cmds
;
1501 /* If this new command is a hook, then mark both commands as being
1508 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1509 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1512 hookc
->hook_post
= newc
; /* Target gets hooked. */
1513 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1516 /* Should never come here as hookc would be 0. */
1517 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1523 document_command (char *comname
, int from_tty
)
1525 struct command_line
*doclines
;
1526 struct cmd_list_element
*c
, **list
;
1527 char *tem
, *comfull
;
1531 list
= validate_comname (&comname
);
1534 c
= lookup_cmd (&tem
, *list
, "", 0, 1);
1536 if (c
->class != class_user
)
1537 error (_("Command \"%s\" is built-in."), comfull
);
1539 sprintf (tmpbuf
, "Type documentation for \"%s\".", comfull
);
1540 doclines
= read_command_lines (tmpbuf
, from_tty
, 0, 0, 0);
1546 struct command_line
*cl1
;
1549 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1550 len
+= strlen (cl1
->line
) + 1;
1552 c
->doc
= (char *) xmalloc (len
+ 1);
1555 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1557 strcat (c
->doc
, cl1
->line
);
1559 strcat (c
->doc
, "\n");
1563 free_command_lines (&doclines
);
1566 struct source_cleanup_lines_args
1569 const char *old_file
;
1573 source_cleanup_lines (void *args
)
1575 struct source_cleanup_lines_args
*p
=
1576 (struct source_cleanup_lines_args
*) args
;
1578 source_line_number
= p
->old_line
;
1579 source_file_name
= p
->old_file
;
1582 struct wrapped_read_command_file_args
1588 wrapped_read_command_file (struct ui_out
*uiout
, void *data
)
1590 struct wrapped_read_command_file_args
*args
= data
;
1592 read_command_file (args
->stream
);
1595 /* Used to implement source_command */
1598 script_from_file (FILE *stream
, const char *file
)
1600 struct cleanup
*old_cleanups
;
1601 struct source_cleanup_lines_args old_lines
;
1604 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1606 old_cleanups
= make_cleanup_fclose (stream
);
1608 old_lines
.old_line
= source_line_number
;
1609 old_lines
.old_file
= source_file_name
;
1610 make_cleanup (source_cleanup_lines
, &old_lines
);
1611 source_line_number
= 0;
1612 source_file_name
= file
;
1613 /* This will get set every time we read a line. So it won't stay "" for
1615 error_pre_print
= "";
1618 struct gdb_exception e
;
1619 struct wrapped_read_command_file_args args
;
1621 args
.stream
= stream
;
1622 e
= catch_exception (uiout
, wrapped_read_command_file
, &args
,
1629 /* Re-throw the error, but with the file name information
1631 throw_error (e
.error
,
1632 _("%s:%d: Error in sourced command file:\n%s"),
1633 source_file_name
, source_line_number
, e
.message
);
1635 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1639 do_cleanups (old_cleanups
);
1642 /* Print the definition of user command C to STREAM. Or, if C is a
1643 prefix command, show the definitions of all user commands under C
1644 (recursively). PREFIX and NAME combined are the name of the
1647 show_user_1 (struct cmd_list_element
*c
, char *prefix
, char *name
,
1648 struct ui_file
*stream
)
1650 struct command_line
*cmdlines
;
1652 if (c
->prefixlist
!= NULL
)
1654 char *prefixname
= c
->prefixname
;
1656 for (c
= *c
->prefixlist
; c
!= NULL
; c
= c
->next
)
1657 if (c
->class == class_user
|| c
->prefixlist
!= NULL
)
1658 show_user_1 (c
, prefixname
, c
->name
, gdb_stdout
);
1662 cmdlines
= c
->user_commands
;
1665 fprintf_filtered (stream
, "User command \"%s%s\":\n", prefix
, name
);
1667 print_command_lines (uiout
, cmdlines
, 1);
1668 fputs_filtered ("\n", stream
);