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, 2011 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
82 /* Allocate, initialize a new command line structure for one of the
83 control commands (if/while). */
85 static struct command_line
*
86 build_command_line (enum command_control_type type
, char *args
)
88 struct command_line
*cmd
;
90 if (args
== NULL
&& (type
== if_control
|| type
== while_control
))
91 error (_("if/while commands require arguments."));
92 gdb_assert (args
!= NULL
);
94 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
96 cmd
->control_type
= type
;
100 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
102 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
103 cmd
->line
= xstrdup (args
);
108 /* Build and return a new command structure for the control commands
109 such as "if" and "while". */
111 struct command_line
*
112 get_command_line (enum command_control_type type
, char *arg
)
114 struct command_line
*cmd
;
115 struct cleanup
*old_chain
= NULL
;
117 /* Allocate and build a new command line structure. */
118 cmd
= build_command_line (type
, arg
);
120 old_chain
= make_cleanup_free_command_lines (&cmd
);
122 /* Read in the body of this command. */
123 if (recurse_read_control_structure (read_next_line
, cmd
, 0, 0)
126 warning (_("Error reading in canned sequence of commands."));
127 do_cleanups (old_chain
);
131 discard_cleanups (old_chain
);
135 /* Recursively print a command (including full control structures). */
138 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
141 struct command_line
*list
;
147 ui_out_spaces (uiout
, 2 * depth
);
149 /* A simple command, print it and continue. */
150 if (list
->control_type
== simple_control
)
152 ui_out_field_string (uiout
, NULL
, list
->line
);
153 ui_out_text (uiout
, "\n");
158 /* loop_continue to jump to the start of a while loop, print it
160 if (list
->control_type
== continue_control
)
162 ui_out_field_string (uiout
, NULL
, "loop_continue");
163 ui_out_text (uiout
, "\n");
168 /* loop_break to break out of a while loop, print it and
170 if (list
->control_type
== break_control
)
172 ui_out_field_string (uiout
, NULL
, "loop_break");
173 ui_out_text (uiout
, "\n");
178 /* A while command. Recursively print its subcommands and
180 if (list
->control_type
== while_control
181 || list
->control_type
== while_stepping_control
)
183 /* For while-stepping, the line includes the 'while-stepping'
184 token. See comment in process_next_line for explanation.
185 Here, take care not print 'while-stepping' twice. */
186 if (list
->control_type
== while_control
)
187 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
189 ui_out_field_string (uiout
, NULL
, list
->line
);
190 ui_out_text (uiout
, "\n");
191 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
193 ui_out_spaces (uiout
, 2 * depth
);
194 ui_out_field_string (uiout
, NULL
, "end");
195 ui_out_text (uiout
, "\n");
200 /* An if command. Recursively print both arms before
202 if (list
->control_type
== if_control
)
204 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
205 ui_out_text (uiout
, "\n");
207 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
209 /* Show the false arm if it exists. */
210 if (list
->body_count
== 2)
213 ui_out_spaces (uiout
, 2 * depth
);
214 ui_out_field_string (uiout
, NULL
, "else");
215 ui_out_text (uiout
, "\n");
216 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
220 ui_out_spaces (uiout
, 2 * depth
);
221 ui_out_field_string (uiout
, NULL
, "end");
222 ui_out_text (uiout
, "\n");
227 /* A commands command. Print the breakpoint commands and
229 if (list
->control_type
== commands_control
)
232 ui_out_field_fmt (uiout
, NULL
, "commands %s", list
->line
);
234 ui_out_field_string (uiout
, NULL
, "commands");
235 ui_out_text (uiout
, "\n");
236 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
238 ui_out_spaces (uiout
, 2 * depth
);
239 ui_out_field_string (uiout
, NULL
, "end");
240 ui_out_text (uiout
, "\n");
245 if (list
->control_type
== python_control
)
247 ui_out_field_string (uiout
, NULL
, "python");
248 ui_out_text (uiout
, "\n");
249 /* Don't indent python code at all. */
250 print_command_lines (uiout
, *list
->body_list
, 0);
252 ui_out_spaces (uiout
, 2 * depth
);
253 ui_out_field_string (uiout
, NULL
, "end");
254 ui_out_text (uiout
, "\n");
259 /* Ignore illegal command type and try next. */
264 /* Handle pre-post hooks. */
267 clear_hook_in_cleanup (void *data
)
269 struct cmd_list_element
*c
= data
;
271 c
->hook_in
= 0; /* Allow hook to work again once it is complete. */
275 execute_cmd_pre_hook (struct cmd_list_element
*c
)
277 if ((c
->hook_pre
) && (!c
->hook_in
))
279 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
280 c
->hook_in
= 1; /* Prevent recursive hooking. */
281 execute_user_command (c
->hook_pre
, (char *) 0);
282 do_cleanups (cleanups
);
287 execute_cmd_post_hook (struct cmd_list_element
*c
)
289 if ((c
->hook_post
) && (!c
->hook_in
))
291 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
293 c
->hook_in
= 1; /* Prevent recursive hooking. */
294 execute_user_command (c
->hook_post
, (char *) 0);
295 do_cleanups (cleanups
);
299 /* Execute the command in CMD. */
301 do_restore_user_call_depth (void * call_depth
)
303 int *depth
= call_depth
;
312 execute_user_command (struct cmd_list_element
*c
, char *args
)
314 struct command_line
*cmdlines
;
315 struct cleanup
*old_chain
;
316 enum command_control_type ret
;
317 static int user_call_depth
= 0;
318 extern int max_user_call_depth
;
320 old_chain
= setup_user_args (args
);
322 cmdlines
= c
->user_commands
;
327 if (++user_call_depth
> max_user_call_depth
)
328 error (_("Max user call depth exceeded -- command aborted."));
330 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
332 /* Set the instream to 0, indicating execution of a
333 user-defined function. */
334 make_cleanup (do_restore_instream_cleanup
, instream
);
335 instream
= (FILE *) 0;
337 /* Also set the global in_user_command, so that NULL instream is
338 not confused with Insight. */
341 command_nest_depth
++;
344 ret
= execute_control_command (cmdlines
);
345 if (ret
!= simple_control
&& ret
!= break_control
)
347 warning (_("Error executing canned sequence of commands."));
350 cmdlines
= cmdlines
->next
;
352 command_nest_depth
--;
353 do_cleanups (old_chain
);
356 /* This function is called every time GDB prints a prompt. It ensures
357 that errors and the like do not confuse the command tracing. */
360 reset_command_nest_depth (void)
362 command_nest_depth
= 1;
365 suppress_next_print_command_trace
= 0;
368 /* Print the command, prefixed with '+' to represent the call depth.
369 This is slightly complicated because this function may be called
370 from execute_command and execute_control_command. Unfortunately
371 execute_command also prints the top level control commands.
372 In these cases execute_command will call execute_control_command
373 via while_command or if_command. Inner levels of 'if' and 'while'
374 are dealt with directly. Therefore we can use these functions
375 to determine whether the command has been printed already or not. */
377 print_command_trace (const char *cmd
)
381 if (suppress_next_print_command_trace
)
383 suppress_next_print_command_trace
= 0;
387 if (!source_verbose
&& !trace_commands
)
390 for (i
=0; i
< command_nest_depth
; i
++)
391 printf_filtered ("+");
393 printf_filtered ("%s\n", cmd
);
396 enum command_control_type
397 execute_control_command (struct command_line
*cmd
)
399 struct expression
*expr
;
400 struct command_line
*current
;
401 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
403 struct value
*val_mark
;
405 enum command_control_type ret
;
408 /* Start by assuming failure, if a problem is detected, the code
409 below will simply "break" out of the switch. */
410 ret
= invalid_control
;
412 switch (cmd
->control_type
)
415 /* A simple command, execute it and return. */
416 new_line
= insert_args (cmd
->line
);
419 make_cleanup (free_current_contents
, &new_line
);
420 execute_command (new_line
, 0);
421 ret
= cmd
->control_type
;
424 case continue_control
:
425 print_command_trace ("loop_continue");
427 /* Return for "continue", and "break" so we can either
428 continue the loop at the top, or break out. */
429 ret
= cmd
->control_type
;
433 print_command_trace ("loop_break");
435 /* Return for "continue", and "break" so we can either
436 continue the loop at the top, or break out. */
437 ret
= cmd
->control_type
;
442 char *buffer
= alloca (strlen (cmd
->line
) + 7);
444 sprintf (buffer
, "while %s", cmd
->line
);
445 print_command_trace (buffer
);
447 /* Parse the loop control expression for the while statement. */
448 new_line
= insert_args (cmd
->line
);
451 make_cleanup (free_current_contents
, &new_line
);
452 expr
= parse_expression (new_line
);
453 make_cleanup (free_current_contents
, &expr
);
455 ret
= simple_control
;
458 /* Keep iterating so long as the expression is true. */
465 /* Evaluate the expression. */
466 val_mark
= value_mark ();
467 val
= evaluate_expression (expr
);
468 cond_result
= value_true (val
);
469 value_free_to_mark (val_mark
);
471 /* If the value is false, then break out of the loop. */
475 /* Execute the body of the while statement. */
476 current
= *cmd
->body_list
;
479 command_nest_depth
++;
480 ret
= execute_control_command (current
);
481 command_nest_depth
--;
483 /* If we got an error, or a "break" command, then stop
485 if (ret
== invalid_control
|| ret
== break_control
)
491 /* If we got a "continue" command, then restart the loop
493 if (ret
== continue_control
)
496 /* Get the next statement. */
497 current
= current
->next
;
501 /* Reset RET so that we don't recurse the break all the way down. */
502 if (ret
== break_control
)
503 ret
= simple_control
;
510 char *buffer
= alloca (strlen (cmd
->line
) + 4);
512 sprintf (buffer
, "if %s", cmd
->line
);
513 print_command_trace (buffer
);
515 new_line
= insert_args (cmd
->line
);
518 make_cleanup (free_current_contents
, &new_line
);
519 /* Parse the conditional for the if statement. */
520 expr
= parse_expression (new_line
);
521 make_cleanup (free_current_contents
, &expr
);
524 ret
= simple_control
;
526 /* Evaluate the conditional. */
527 val_mark
= value_mark ();
528 val
= evaluate_expression (expr
);
530 /* Choose which arm to take commands from based on the value
531 of the conditional expression. */
532 if (value_true (val
))
533 current
= *cmd
->body_list
;
534 else if (cmd
->body_count
== 2)
535 current
= *(cmd
->body_list
+ 1);
536 value_free_to_mark (val_mark
);
538 /* Execute commands in the given arm. */
541 command_nest_depth
++;
542 ret
= execute_control_command (current
);
543 command_nest_depth
--;
545 /* If we got an error, get out. */
546 if (ret
!= simple_control
)
549 /* Get the next statement in the body. */
550 current
= current
->next
;
555 case commands_control
:
557 /* Breakpoint commands list, record the commands in the
558 breakpoint's command list and return. */
559 new_line
= insert_args (cmd
->line
);
562 make_cleanup (free_current_contents
, &new_line
);
563 ret
= commands_from_control_command (new_line
, cmd
);
568 eval_python_from_control_command (cmd
);
569 ret
= simple_control
;
574 warning (_("Invalid control type in canned commands structure."));
578 do_cleanups (old_chain
);
583 /* Like execute_control_command, but first set
584 suppress_next_print_command_trace. */
586 enum command_control_type
587 execute_control_command_untraced (struct command_line
*cmd
)
589 suppress_next_print_command_trace
= 1;
590 return execute_control_command (cmd
);
594 /* "while" command support. Executes a body of statements while the
595 loop condition is nonzero. */
598 while_command (char *arg
, int from_tty
)
600 struct command_line
*command
= NULL
;
603 command
= get_command_line (while_control
, arg
);
608 execute_control_command_untraced (command
);
609 free_command_lines (&command
);
612 /* "if" command support. Execute either the true or false arm depending
613 on the value of the if conditional. */
616 if_command (char *arg
, int from_tty
)
618 struct command_line
*command
= NULL
;
621 command
= get_command_line (if_control
, arg
);
626 execute_control_command_untraced (command
);
627 free_command_lines (&command
);
632 arg_cleanup (void *ignore
)
634 struct user_args
*oargs
= user_args
;
637 internal_error (__FILE__
, __LINE__
,
638 _("arg_cleanup called with no user args.\n"));
640 user_args
= user_args
->next
;
641 xfree (oargs
->command
);
645 /* Bind the incomming arguments for a user defined command to
646 $arg0, $arg1 ... $argMAXUSERARGS. */
648 static struct cleanup
*
649 setup_user_args (char *p
)
651 struct user_args
*args
;
652 struct cleanup
*old_chain
;
653 unsigned int arg_count
= 0;
655 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
656 memset (args
, 0, sizeof (struct user_args
));
658 args
->next
= user_args
;
661 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
666 user_args
->command
= p
= xstrdup (p
);
675 if (arg_count
>= MAXUSERARGS
)
677 error (_("user defined function may only have %d arguments."),
682 /* Strip whitespace. */
683 while (*p
== ' ' || *p
== '\t')
686 /* P now points to an argument. */
688 user_args
->a
[arg_count
].arg
= p
;
690 /* Get to the end of this argument. */
693 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
722 user_args
->a
[arg_count
].len
= p
- start_arg
;
729 /* Given character string P, return a point to the first argument
730 ($arg), or NULL if P contains no arguments. */
735 while ((p
= strchr (p
, '$')))
737 if (strncmp (p
, "$arg", 4) == 0
738 && (isdigit (p
[4]) || p
[4] == 'c'))
745 /* Insert the user defined arguments stored in user_arg into the $arg
746 arguments found in line, with the updated copy being placed into
750 insert_args (char *line
)
752 char *p
, *save_line
, *new_line
;
755 /* If we are not in a user-defined function, treat $argc, $arg0, et
756 cetera as normal convenience variables. */
757 if (user_args
== NULL
)
758 return xstrdup (line
);
760 /* First we need to know how much memory to allocate for the new
764 while ((p
= locate_arg (line
)))
771 /* $argc. Number will be <=10. */
772 len
+= user_args
->count
== 10 ? 2 : 1;
774 else if (i
>= user_args
->count
)
776 error (_("Missing argument %d in user function."), i
);
781 len
+= user_args
->a
[i
].len
;
786 /* Don't forget the tail. */
787 len
+= strlen (line
);
789 /* Allocate space for the new line and fill it in. */
790 new_line
= (char *) xmalloc (len
+ 1);
791 if (new_line
== NULL
)
794 /* Restore pointer to beginning of old line. */
797 /* Save pointer to beginning of new line. */
798 save_line
= new_line
;
800 while ((p
= locate_arg (line
)))
804 memcpy (new_line
, line
, p
- line
);
805 new_line
+= p
- line
;
809 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
810 if (user_args
->count
== 10)
816 *(new_line
++) = user_args
->count
+ '0';
821 len
= user_args
->a
[i
].len
;
824 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
830 /* Don't forget the tail. */
831 strcpy (new_line
, line
);
833 /* Return a pointer to the beginning of the new line. */
838 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
839 code bodies. This is typically used when we encounter an "else"
840 clause for an "if" command. */
843 realloc_body_list (struct command_line
*command
, int new_length
)
846 struct command_line
**body_list
;
848 n
= command
->body_count
;
854 body_list
= (struct command_line
**)
855 xmalloc (sizeof (struct command_line
*) * new_length
);
857 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
858 memset (body_list
+ n
, 0, sizeof (struct command_line
*) * (new_length
- n
));
860 xfree (command
->body_list
);
861 command
->body_list
= body_list
;
862 command
->body_count
= new_length
;
865 /* Read next line from stdout. Passed to read_command_line_1 and
866 recurse_read_control_structure whenever we need to read commands
870 read_next_line (void)
872 char *prompt_ptr
, control_prompt
[256];
875 if (control_level
>= 254)
876 error (_("Control nesting too deep!"));
878 /* Set a prompt based on the nesting of the control commands. */
879 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
881 for (i
= 0; i
< control_level
; i
++)
882 control_prompt
[i
] = ' ';
883 control_prompt
[i
] = '>';
884 control_prompt
[i
+ 1] = '\0';
885 prompt_ptr
= (char *) &control_prompt
[0];
890 return command_line_input (prompt_ptr
, instream
== stdin
, "commands");
893 /* Process one input line. If the command is an "end", return such an
894 indication to the caller. If PARSE_COMMANDS is true, strip leading
895 whitespace (trailing whitespace is always stripped) in the line,
896 attempt to recognize GDB control commands, and also return an
897 indication if the command is an "else" or a nop.
899 Otherwise, only "end" is recognized. */
901 static enum misc_command_type
902 process_next_line (char *p
, struct command_line
**command
, int parse_commands
,
903 void (*validator
)(char *, void *), void *closure
)
909 /* Not sure what to do here. */
913 /* Strip trailing whitespace. */
914 p_end
= p
+ strlen (p
);
915 while (p_end
> p
&& (p_end
[-1] == ' ' || p_end
[-1] == '\t'))
919 /* Strip leading whitespace. */
920 while (p_start
< p_end
&& (*p_start
== ' ' || *p_start
== '\t'))
923 /* 'end' is always recognized, regardless of parse_commands value.
924 We also permit whitespace before end and after. */
925 if (p_end
- p_start
== 3 && !strncmp (p_start
, "end", 3))
930 /* If commands are parsed, we skip initial spaces. Otherwise,
931 which is the case for Python commands and documentation
932 (see the 'document' command), spaces are preserved. */
935 /* Blanks and comments don't really do anything, but we need to
936 distinguish them from else, end and other commands which can
938 if (p_end
== p
|| p
[0] == '#')
941 /* Is the else clause of an if control structure? */
942 if (p_end
- p
== 4 && !strncmp (p
, "else", 4))
945 /* Check for while, if, break, continue, etc and build a new
946 command line structure for them. */
947 if ((p_end
- p
>= 14 && !strncmp (p
, "while-stepping", 14))
948 || (p_end
- p
>= 8 && !strncmp (p
, "stepping", 8))
949 || (p_end
- p
>= 2 && !strncmp (p
, "ws", 2)))
951 /* Because validate_actionline and encode_action lookup
952 command's line as command, we need the line to
953 include 'while-stepping'.
955 For 'ws' alias, the command will have 'ws', not expanded
956 to 'while-stepping'. This is intentional -- we don't
957 really want frontend to send a command list with 'ws',
958 and next break-info returning command line with
959 'while-stepping'. This should work, but might cause the
960 breakpoint to be marked as changed while it's actually
962 *command
= build_command_line (while_stepping_control
, p
);
964 else if (p_end
- p
> 5 && !strncmp (p
, "while", 5))
969 while (first_arg
< p_end
&& isspace (*first_arg
))
971 *command
= build_command_line (while_control
, first_arg
);
973 else if (p_end
- p
> 2 && !strncmp (p
, "if", 2))
978 while (first_arg
< p_end
&& isspace (*first_arg
))
980 *command
= build_command_line (if_control
, first_arg
);
982 else if (p_end
- p
>= 8 && !strncmp (p
, "commands", 8))
987 while (first_arg
< p_end
&& isspace (*first_arg
))
989 *command
= build_command_line (commands_control
, first_arg
);
991 else if (p_end
- p
== 6 && !strncmp (p
, "python", 6))
993 /* Note that we ignore the inline "python command" form
995 *command
= build_command_line (python_control
, "");
997 else if (p_end
- p
== 10 && !strncmp (p
, "loop_break", 10))
999 *command
= (struct command_line
*)
1000 xmalloc (sizeof (struct command_line
));
1001 (*command
)->next
= NULL
;
1002 (*command
)->line
= NULL
;
1003 (*command
)->control_type
= break_control
;
1004 (*command
)->body_count
= 0;
1005 (*command
)->body_list
= NULL
;
1007 else if (p_end
- p
== 13 && !strncmp (p
, "loop_continue", 13))
1009 *command
= (struct command_line
*)
1010 xmalloc (sizeof (struct command_line
));
1011 (*command
)->next
= NULL
;
1012 (*command
)->line
= NULL
;
1013 (*command
)->control_type
= continue_control
;
1014 (*command
)->body_count
= 0;
1015 (*command
)->body_list
= NULL
;
1021 if (!parse_commands
|| not_handled
)
1023 /* A normal command. */
1024 *command
= (struct command_line
*)
1025 xmalloc (sizeof (struct command_line
));
1026 (*command
)->next
= NULL
;
1027 (*command
)->line
= savestring (p
, p_end
- p
);
1028 (*command
)->control_type
= simple_control
;
1029 (*command
)->body_count
= 0;
1030 (*command
)->body_list
= NULL
;
1035 volatile struct gdb_exception ex
;
1037 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1039 validator ((*command
)->line
, closure
);
1044 throw_exception (ex
);
1048 /* Nothing special. */
1052 /* Recursively read in the control structures and create a
1053 command_line structure from them. Use read_next_line_func to
1054 obtain lines of the command. */
1056 static enum command_control_type
1057 recurse_read_control_structure (char * (*read_next_line_func
) (void),
1058 struct command_line
*current_cmd
,
1059 void (*validator
)(char *, void *),
1062 int current_body
, i
;
1063 enum misc_command_type val
;
1064 enum command_control_type ret
;
1065 struct command_line
**body_ptr
, *child_tail
, *next
;
1070 /* Sanity checks. */
1071 if (current_cmd
->control_type
== simple_control
)
1072 error (_("Recursed on a simple control type."));
1074 if (current_body
> current_cmd
->body_count
)
1075 error (_("Allocated body is smaller than this command type needs."));
1077 /* Read lines from the input stream and build control structures. */
1083 val
= process_next_line (read_next_line_func (), &next
,
1084 current_cmd
->control_type
!= python_control
,
1085 validator
, closure
);
1087 /* Just skip blanks and comments. */
1088 if (val
== nop_command
)
1091 if (val
== end_command
)
1093 if (current_cmd
->control_type
== while_control
1094 || current_cmd
->control_type
== while_stepping_control
1095 || current_cmd
->control_type
== if_control
1096 || current_cmd
->control_type
== python_control
1097 || current_cmd
->control_type
== commands_control
)
1099 /* Success reading an entire canned sequence of commands. */
1100 ret
= simple_control
;
1105 ret
= invalid_control
;
1110 /* Not the end of a control structure. */
1111 if (val
== else_command
)
1113 if (current_cmd
->control_type
== if_control
1114 && current_body
== 1)
1116 realloc_body_list (current_cmd
, 2);
1123 ret
= invalid_control
;
1130 child_tail
->next
= next
;
1134 body_ptr
= current_cmd
->body_list
;
1135 for (i
= 1; i
< current_body
; i
++)
1144 /* If the latest line is another control structure, then recurse
1146 if (next
->control_type
== while_control
1147 || next
->control_type
== while_stepping_control
1148 || next
->control_type
== if_control
1149 || next
->control_type
== python_control
1150 || next
->control_type
== commands_control
)
1153 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1154 validator
, closure
);
1157 if (ret
!= simple_control
)
1167 /* Read lines from the input stream and accumulate them in a chain of
1168 struct command_line's, which is then returned. For input from a
1169 terminal, the special command "end" is used to mark the end of the
1170 input, and is not included in the returned chain of commands.
1172 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1173 is always stripped) in the line and attempt to recognize GDB control
1174 commands. Otherwise, only "end" is recognized. */
1176 #define END_MESSAGE "End with a line saying just \"end\"."
1178 struct command_line
*
1179 read_command_lines (char *prompt_arg
, int from_tty
, int parse_commands
,
1180 void (*validator
)(char *, void *), void *closure
)
1182 struct command_line
*head
;
1184 if (from_tty
&& input_from_terminal_p ())
1186 if (deprecated_readline_begin_hook
)
1188 /* Note - intentional to merge messages with no newline. */
1189 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
,
1194 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1195 gdb_flush (gdb_stdout
);
1199 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1200 validator
, closure
);
1202 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1204 (*deprecated_readline_end_hook
) ();
1209 /* Act the same way as read_command_lines, except that each new line is
1210 obtained using READ_NEXT_LINE_FUNC. */
1212 struct command_line
*
1213 read_command_lines_1 (char * (*read_next_line_func
) (void), int parse_commands
,
1214 void (*validator
)(char *, void *), void *closure
)
1216 struct command_line
*head
, *tail
, *next
;
1217 struct cleanup
*old_chain
;
1218 enum command_control_type ret
;
1219 enum misc_command_type val
;
1228 val
= process_next_line (read_next_line_func (), &next
, parse_commands
,
1229 validator
, closure
);
1231 /* Ignore blank lines or comments. */
1232 if (val
== nop_command
)
1235 if (val
== end_command
)
1237 ret
= simple_control
;
1241 if (val
!= ok_command
)
1243 ret
= invalid_control
;
1247 if (next
->control_type
== while_control
1248 || next
->control_type
== if_control
1249 || next
->control_type
== python_control
1250 || next
->control_type
== commands_control
1251 || next
->control_type
== while_stepping_control
)
1254 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1255 validator
, closure
);
1258 if (ret
== invalid_control
)
1269 old_chain
= make_cleanup_free_command_lines (&head
);
1278 if (ret
!= invalid_control
)
1280 discard_cleanups (old_chain
);
1283 do_cleanups (old_chain
);
1289 /* Free a chain of struct command_line's. */
1292 free_command_lines (struct command_line
**lptr
)
1294 struct command_line
*l
= *lptr
;
1295 struct command_line
*next
;
1296 struct command_line
**blist
;
1301 if (l
->body_count
> 0)
1303 blist
= l
->body_list
;
1304 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1305 free_command_lines (blist
);
1316 do_free_command_lines_cleanup (void *arg
)
1318 free_command_lines (arg
);
1322 make_cleanup_free_command_lines (struct command_line
**arg
)
1324 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1327 struct command_line
*
1328 copy_command_lines (struct command_line
*cmds
)
1330 struct command_line
*result
= NULL
;
1334 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1336 result
->next
= copy_command_lines (cmds
->next
);
1337 result
->line
= xstrdup (cmds
->line
);
1338 result
->control_type
= cmds
->control_type
;
1339 result
->body_count
= cmds
->body_count
;
1340 if (cmds
->body_count
> 0)
1344 result
->body_list
= (struct command_line
**)
1345 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1347 for (i
= 0; i
< cmds
->body_count
; i
++)
1348 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1351 result
->body_list
= NULL
;
1357 /* Validate that *COMNAME is a valid name for a command. Return the
1358 containing command list, in case it starts with a prefix command.
1359 The prefix must already exist. *COMNAME is advanced to point after
1360 any prefix, and a NUL character overwrites the space after the
1363 static struct cmd_list_element
**
1364 validate_comname (char **comname
)
1366 struct cmd_list_element
**list
= &cmdlist
;
1367 char *p
, *last_word
;
1370 error_no_arg (_("name of command to define"));
1372 /* Find the last word of the argument. */
1373 p
= *comname
+ strlen (*comname
);
1374 while (p
> *comname
&& isspace (p
[-1]))
1376 while (p
> *comname
&& !isspace (p
[-1]))
1380 /* Find the corresponding command list. */
1381 if (last_word
!= *comname
)
1383 struct cmd_list_element
*c
;
1384 char saved_char
, *tem
= *comname
;
1386 /* Separate the prefix and the command. */
1387 saved_char
= last_word
[-1];
1388 last_word
[-1] = '\0';
1390 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1391 if (c
->prefixlist
== NULL
)
1392 error (_("\"%s\" is not a prefix command."), *comname
);
1394 list
= c
->prefixlist
;
1395 last_word
[-1] = saved_char
;
1396 *comname
= last_word
;
1402 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1403 error (_("Junk in argument list: \"%s\""), p
);
1410 /* This is just a placeholder in the command data structures. */
1412 user_defined_command (char *ignore
, int from_tty
)
1417 define_command (char *comname
, int from_tty
)
1419 #define MAX_TMPBUF 128
1426 struct command_line
*cmds
;
1427 struct cmd_list_element
*c
, *newc
, *hookc
= 0, **list
;
1428 char *tem
, *comfull
;
1429 char tmpbuf
[MAX_TMPBUF
];
1430 int hook_type
= CMD_NO_HOOK
;
1431 int hook_name_size
= 0;
1433 #define HOOK_STRING "hook-"
1435 #define HOOK_POST_STRING "hookpost-"
1436 #define HOOK_POST_LEN 9
1439 list
= validate_comname (&comname
);
1441 /* Look it up, and verify that we got an exact match. */
1443 c
= lookup_cmd (&tem
, *list
, "", -1, 1);
1444 if (c
&& strcmp (comname
, c
->name
) != 0)
1451 if (c
->class == class_user
|| c
->class == class_alias
)
1452 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1454 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1456 error (_("Command \"%s\" not redefined."), c
->name
);
1459 /* If this new command is a hook, then mark the command which it
1460 is hooking. Note that we allow hooking `help' commands, so that
1461 we can hook the `stop' pseudo-command. */
1463 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1465 hook_type
= CMD_PRE_HOOK
;
1466 hook_name_size
= HOOK_LEN
;
1468 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1470 hook_type
= CMD_POST_HOOK
;
1471 hook_name_size
= HOOK_POST_LEN
;
1474 if (hook_type
!= CMD_NO_HOOK
)
1476 /* Look up cmd it hooks, and verify that we got an exact match. */
1477 tem
= comname
+ hook_name_size
;
1478 hookc
= lookup_cmd (&tem
, *list
, "", -1, 0);
1479 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1483 warning (_("Your new `%s' command does not "
1484 "hook any existing command."),
1486 if (!query (_("Proceed? ")))
1487 error (_("Not confirmed."));
1491 comname
= xstrdup (comname
);
1493 /* If the rest of the commands will be case insensitive, this one
1494 should behave in the same manner. */
1495 for (tem
= comname
; *tem
; tem
++)
1497 *tem
= tolower (*tem
);
1499 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comfull
);
1500 cmds
= read_command_lines (tmpbuf
, from_tty
, 1, 0, 0);
1502 if (c
&& c
->class == class_user
)
1503 free_command_lines (&c
->user_commands
);
1505 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1506 (c
&& c
->class == class_user
)
1507 ? c
->doc
: xstrdup ("User-defined."), list
);
1508 newc
->user_commands
= cmds
;
1510 /* If this new command is a hook, then mark both commands as being
1517 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1518 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1521 hookc
->hook_post
= newc
; /* Target gets hooked. */
1522 newc
->hookee_post
= hookc
; /* We are marked as hooking
1526 /* Should never come here as hookc would be 0. */
1527 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1533 document_command (char *comname
, int from_tty
)
1535 struct command_line
*doclines
;
1536 struct cmd_list_element
*c
, **list
;
1537 char *tem
, *comfull
;
1541 list
= validate_comname (&comname
);
1544 c
= lookup_cmd (&tem
, *list
, "", 0, 1);
1546 if (c
->class != class_user
)
1547 error (_("Command \"%s\" is built-in."), comfull
);
1549 sprintf (tmpbuf
, "Type documentation for \"%s\".", comfull
);
1550 doclines
= read_command_lines (tmpbuf
, from_tty
, 0, 0, 0);
1556 struct command_line
*cl1
;
1559 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1560 len
+= strlen (cl1
->line
) + 1;
1562 c
->doc
= (char *) xmalloc (len
+ 1);
1565 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1567 strcat (c
->doc
, cl1
->line
);
1569 strcat (c
->doc
, "\n");
1573 free_command_lines (&doclines
);
1576 struct source_cleanup_lines_args
1579 const char *old_file
;
1583 source_cleanup_lines (void *args
)
1585 struct source_cleanup_lines_args
*p
=
1586 (struct source_cleanup_lines_args
*) args
;
1588 source_line_number
= p
->old_line
;
1589 source_file_name
= p
->old_file
;
1592 /* Used to implement source_command. */
1595 script_from_file (FILE *stream
, const char *file
)
1597 struct cleanup
*old_cleanups
;
1598 struct source_cleanup_lines_args old_lines
;
1601 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1603 old_cleanups
= make_cleanup_fclose (stream
);
1605 old_lines
.old_line
= source_line_number
;
1606 old_lines
.old_file
= source_file_name
;
1607 make_cleanup (source_cleanup_lines
, &old_lines
);
1608 source_line_number
= 0;
1609 source_file_name
= file
;
1610 /* This will get set every time we read a line. So it won't stay ""
1612 error_pre_print
= "";
1615 volatile struct gdb_exception e
;
1617 TRY_CATCH (e
, RETURN_MASK_ERROR
)
1619 read_command_file (stream
);
1626 /* Re-throw the error, but with the file name information
1628 throw_error (e
.error
,
1629 _("%s:%d: Error in sourced command file:\n%s"),
1630 source_file_name
, source_line_number
, e
.message
);
1632 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1636 do_cleanups (old_cleanups
);
1639 /* Print the definition of user command C to STREAM. Or, if C is a
1640 prefix command, show the definitions of all user commands under C
1641 (recursively). PREFIX and NAME combined are the name of the
1644 show_user_1 (struct cmd_list_element
*c
, char *prefix
, char *name
,
1645 struct ui_file
*stream
)
1647 struct command_line
*cmdlines
;
1649 if (c
->prefixlist
!= NULL
)
1651 char *prefixname
= c
->prefixname
;
1653 for (c
= *c
->prefixlist
; c
!= NULL
; c
= c
->next
)
1654 if (c
->class == class_user
|| c
->prefixlist
!= NULL
)
1655 show_user_1 (c
, prefixname
, c
->name
, gdb_stdout
);
1659 cmdlines
= c
->user_commands
;
1662 fprintf_filtered (stream
, "User command \"%s%s\":\n", prefix
, name
);
1664 print_command_lines (uiout
, cmdlines
, 1);
1665 fputs_filtered ("\n", stream
);