1 /* GDB CLI command scripting.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "language.h" /* For value_true */
27 #include "breakpoint.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-decode.h"
30 #include "cli/cli-script.h"
32 #include "extension.h"
34 #include "compile/compile.h"
35 #include "common/gdb_string_view.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 *read_next_line (void);
49 /* Level of control structure when reading. */
50 static int control_level
;
52 /* Level of control structure when executing. */
53 static int command_nest_depth
= 1;
55 /* This is to prevent certain commands being printed twice. */
56 static int suppress_next_print_command_trace
= 0;
58 /* Structure for arguments to user defined functions. */
63 /* Save the command line and store the locations of arguments passed
64 to the user defined function. */
65 explicit user_args (const char *line
);
67 /* Insert the stored user defined arguments into the $arg arguments
69 std::string
insert_args (const char *line
) const;
72 /* Disable copy/assignment. (Since the elements of A point inside
73 COMMAND, copying would need to reconstruct the A vector in the
75 user_args (const user_args
&) =delete;
76 user_args
&operator= (const user_args
&) =delete;
78 /* It is necessary to store a copy of the command line to ensure
79 that the arguments are not overwritten before they are used. */
80 std::string m_command_line
;
82 /* The arguments. Each element points inside M_COMMAND_LINE. */
83 std::vector
<gdb::string_view
> m_args
;
86 /* The stack of arguments passed to user defined functions. We need a
87 stack because user-defined functions can call other user-defined
89 static std::vector
<std::unique_ptr
<user_args
>> user_args_stack
;
91 /* An RAII-base class used to push/pop args on the user args
93 struct scoped_user_args_level
95 /* Parse the command line and push the arguments in the user args
97 explicit scoped_user_args_level (const char *line
)
99 user_args_stack
.emplace_back (new user_args (line
));
102 /* Pop the current user arguments from the stack. */
103 ~scoped_user_args_level ()
105 user_args_stack
.pop_back ();
110 /* Return non-zero if TYPE is a multi-line command (i.e., is terminated
114 multi_line_command_p (enum command_control_type type
)
120 case while_stepping_control
:
121 case commands_control
:
122 case compile_control
:
131 /* Allocate, initialize a new command line structure for one of the
132 control commands (if/while). */
134 static struct command_line
*
135 build_command_line (enum command_control_type type
, const char *args
)
137 struct command_line
*cmd
;
139 if ((args
== NULL
|| *args
== '\0')
140 && (type
== if_control
|| type
== while_control
))
141 error (_("if/while commands require arguments."));
142 gdb_assert (args
!= NULL
);
144 cmd
= XNEW (struct command_line
);
146 cmd
->control_type
= type
;
149 cmd
->body_list
= XCNEWVEC (struct command_line
*, cmd
->body_count
);
150 cmd
->line
= xstrdup (args
);
155 /* Build and return a new command structure for the control commands
156 such as "if" and "while". */
159 get_command_line (enum command_control_type type
, const char *arg
)
161 /* Allocate and build a new command line structure. */
162 command_line_up
cmd (build_command_line (type
, arg
));
164 /* Read in the body of this command. */
165 if (recurse_read_control_structure (read_next_line
, cmd
.get (), 0, 0)
168 warning (_("Error reading in canned sequence of commands."));
175 /* Recursively print a command (including full control structures). */
178 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
181 struct command_line
*list
;
187 uiout
->spaces (2 * depth
);
189 /* A simple command, print it and continue. */
190 if (list
->control_type
== simple_control
)
192 uiout
->field_string (NULL
, list
->line
);
198 /* loop_continue to jump to the start of a while loop, print it
200 if (list
->control_type
== continue_control
)
202 uiout
->field_string (NULL
, "loop_continue");
208 /* loop_break to break out of a while loop, print it and
210 if (list
->control_type
== break_control
)
212 uiout
->field_string (NULL
, "loop_break");
218 /* A while command. Recursively print its subcommands and
220 if (list
->control_type
== while_control
221 || list
->control_type
== while_stepping_control
)
223 /* For while-stepping, the line includes the 'while-stepping'
224 token. See comment in process_next_line for explanation.
225 Here, take care not print 'while-stepping' twice. */
226 if (list
->control_type
== while_control
)
227 uiout
->field_fmt (NULL
, "while %s", list
->line
);
229 uiout
->field_string (NULL
, list
->line
);
231 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
233 uiout
->spaces (2 * depth
);
234 uiout
->field_string (NULL
, "end");
240 /* An if command. Recursively print both arms before
242 if (list
->control_type
== if_control
)
244 uiout
->field_fmt (NULL
, "if %s", list
->line
);
247 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
249 /* Show the false arm if it exists. */
250 if (list
->body_count
== 2)
253 uiout
->spaces (2 * depth
);
254 uiout
->field_string (NULL
, "else");
256 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
260 uiout
->spaces (2 * depth
);
261 uiout
->field_string (NULL
, "end");
267 /* A commands command. Print the breakpoint commands and
269 if (list
->control_type
== commands_control
)
272 uiout
->field_fmt (NULL
, "commands %s", list
->line
);
274 uiout
->field_string (NULL
, "commands");
276 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
278 uiout
->spaces (2 * depth
);
279 uiout
->field_string (NULL
, "end");
285 if (list
->control_type
== python_control
)
287 uiout
->field_string (NULL
, "python");
289 /* Don't indent python code at all. */
290 print_command_lines (uiout
, *list
->body_list
, 0);
292 uiout
->spaces (2 * depth
);
293 uiout
->field_string (NULL
, "end");
299 if (list
->control_type
== compile_control
)
301 uiout
->field_string (NULL
, "compile expression");
303 print_command_lines (uiout
, *list
->body_list
, 0);
305 uiout
->spaces (2 * depth
);
306 uiout
->field_string (NULL
, "end");
312 if (list
->control_type
== guile_control
)
314 uiout
->field_string (NULL
, "guile");
316 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
318 uiout
->spaces (2 * depth
);
319 uiout
->field_string (NULL
, "end");
325 /* Ignore illegal command type and try next. */
330 /* Handle pre-post hooks. */
332 class scoped_restore_hook_in
336 scoped_restore_hook_in (struct cmd_list_element
*c
)
341 ~scoped_restore_hook_in ()
346 scoped_restore_hook_in (const scoped_restore_hook_in
&) = delete;
347 scoped_restore_hook_in
&operator= (const scoped_restore_hook_in
&) = delete;
351 struct cmd_list_element
*m_cmd
;
355 execute_cmd_pre_hook (struct cmd_list_element
*c
)
357 if ((c
->hook_pre
) && (!c
->hook_in
))
359 scoped_restore_hook_in
restore_hook (c
);
360 c
->hook_in
= 1; /* Prevent recursive hooking. */
361 execute_user_command (c
->hook_pre
, nullptr);
366 execute_cmd_post_hook (struct cmd_list_element
*c
)
368 if ((c
->hook_post
) && (!c
->hook_in
))
370 scoped_restore_hook_in
restore_hook (c
);
371 c
->hook_in
= 1; /* Prevent recursive hooking. */
372 execute_user_command (c
->hook_post
, nullptr);
377 execute_user_command (struct cmd_list_element
*c
, const char *args
)
379 struct ui
*ui
= current_ui
;
380 struct command_line
*cmdlines
;
381 enum command_control_type ret
;
382 extern unsigned int max_user_call_depth
;
384 cmdlines
= c
->user_commands
;
389 scoped_user_args_level
push_user_args (args
);
391 if (user_args_stack
.size () > max_user_call_depth
)
392 error (_("Max user call depth exceeded -- command aborted."));
394 /* Set the instream to 0, indicating execution of a
395 user-defined function. */
396 scoped_restore restore_instream
397 = make_scoped_restore (&ui
->instream
, nullptr);
399 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
401 scoped_restore save_nesting
402 = make_scoped_restore (&command_nest_depth
, command_nest_depth
+ 1);
405 ret
= execute_control_command (cmdlines
);
406 if (ret
!= simple_control
&& ret
!= break_control
)
408 warning (_("Error executing canned sequence of commands."));
411 cmdlines
= cmdlines
->next
;
415 /* This function is called every time GDB prints a prompt. It ensures
416 that errors and the like do not confuse the command tracing. */
419 reset_command_nest_depth (void)
421 command_nest_depth
= 1;
424 suppress_next_print_command_trace
= 0;
427 /* Print the command, prefixed with '+' to represent the call depth.
428 This is slightly complicated because this function may be called
429 from execute_command and execute_control_command. Unfortunately
430 execute_command also prints the top level control commands.
431 In these cases execute_command will call execute_control_command
432 via while_command or if_command. Inner levels of 'if' and 'while'
433 are dealt with directly. Therefore we can use these functions
434 to determine whether the command has been printed already or not. */
436 print_command_trace (const char *cmd
)
440 if (suppress_next_print_command_trace
)
442 suppress_next_print_command_trace
= 0;
446 if (!source_verbose
&& !trace_commands
)
449 for (i
=0; i
< command_nest_depth
; i
++)
450 printf_filtered ("+");
452 printf_filtered ("%s\n", cmd
);
455 /* Helper for execute_control_command. */
457 static enum command_control_type
458 execute_control_command_1 (struct command_line
*cmd
)
460 struct command_line
*current
;
462 struct value
*val_mark
;
464 enum command_control_type ret
;
466 /* Start by assuming failure, if a problem is detected, the code
467 below will simply "break" out of the switch. */
468 ret
= invalid_control
;
470 switch (cmd
->control_type
)
474 /* A simple command, execute it and return. */
475 std::string new_line
= insert_user_defined_cmd_args (cmd
->line
);
476 execute_command (new_line
.c_str (), 0);
477 ret
= cmd
->control_type
;
481 case continue_control
:
482 print_command_trace ("loop_continue");
484 /* Return for "continue", and "break" so we can either
485 continue the loop at the top, or break out. */
486 ret
= cmd
->control_type
;
490 print_command_trace ("loop_break");
492 /* Return for "continue", and "break" so we can either
493 continue the loop at the top, or break out. */
494 ret
= cmd
->control_type
;
499 int len
= strlen (cmd
->line
) + 7;
500 char *buffer
= (char *) alloca (len
);
502 xsnprintf (buffer
, len
, "while %s", cmd
->line
);
503 print_command_trace (buffer
);
505 /* Parse the loop control expression for the while statement. */
506 std::string new_line
= insert_user_defined_cmd_args (cmd
->line
);
507 expression_up expr
= parse_expression (new_line
.c_str ());
509 ret
= simple_control
;
512 /* Keep iterating so long as the expression is true. */
519 /* Evaluate the expression. */
520 val_mark
= value_mark ();
521 val
= evaluate_expression (expr
.get ());
522 cond_result
= value_true (val
);
523 value_free_to_mark (val_mark
);
525 /* If the value is false, then break out of the loop. */
529 /* Execute the body of the while statement. */
530 current
= *cmd
->body_list
;
533 scoped_restore save_nesting
534 = make_scoped_restore (&command_nest_depth
, command_nest_depth
+ 1);
535 ret
= execute_control_command_1 (current
);
537 /* If we got an error, or a "break" command, then stop
539 if (ret
== invalid_control
|| ret
== break_control
)
545 /* If we got a "continue" command, then restart the loop
547 if (ret
== continue_control
)
550 /* Get the next statement. */
551 current
= current
->next
;
555 /* Reset RET so that we don't recurse the break all the way down. */
556 if (ret
== break_control
)
557 ret
= simple_control
;
564 int len
= strlen (cmd
->line
) + 4;
565 char *buffer
= (char *) alloca (len
);
567 xsnprintf (buffer
, len
, "if %s", cmd
->line
);
568 print_command_trace (buffer
);
570 /* Parse the conditional for the if statement. */
571 std::string new_line
= insert_user_defined_cmd_args (cmd
->line
);
572 expression_up expr
= parse_expression (new_line
.c_str ());
575 ret
= simple_control
;
577 /* Evaluate the conditional. */
578 val_mark
= value_mark ();
579 val
= evaluate_expression (expr
.get ());
581 /* Choose which arm to take commands from based on the value
582 of the conditional expression. */
583 if (value_true (val
))
584 current
= *cmd
->body_list
;
585 else if (cmd
->body_count
== 2)
586 current
= *(cmd
->body_list
+ 1);
587 value_free_to_mark (val_mark
);
589 /* Execute commands in the given arm. */
592 scoped_restore save_nesting
593 = make_scoped_restore (&command_nest_depth
, command_nest_depth
+ 1);
594 ret
= execute_control_command_1 (current
);
596 /* If we got an error, get out. */
597 if (ret
!= simple_control
)
600 /* Get the next statement in the body. */
601 current
= current
->next
;
607 case commands_control
:
609 /* Breakpoint commands list, record the commands in the
610 breakpoint's command list and return. */
611 std::string new_line
= insert_user_defined_cmd_args (cmd
->line
);
612 ret
= commands_from_control_command (new_line
.c_str (), cmd
);
616 case compile_control
:
617 eval_compile_command (cmd
, NULL
, cmd
->control_u
.compile
.scope
,
618 cmd
->control_u
.compile
.scope_data
);
619 ret
= simple_control
;
625 eval_ext_lang_from_control_command (cmd
);
626 ret
= simple_control
;
631 warning (_("Invalid control type in canned commands structure."));
638 enum command_control_type
639 execute_control_command (struct command_line
*cmd
)
641 /* Make sure we use the console uiout. It's possible that we are executing
642 breakpoint commands while running the MI interpreter. */
643 interp
*console
= interp_lookup (current_ui
, INTERP_CONSOLE
);
644 scoped_restore save_uiout
645 = make_scoped_restore (¤t_uiout
, interp_ui_out (console
));
647 return execute_control_command_1 (cmd
);
650 /* Like execute_control_command, but first set
651 suppress_next_print_command_trace. */
653 enum command_control_type
654 execute_control_command_untraced (struct command_line
*cmd
)
656 suppress_next_print_command_trace
= 1;
657 return execute_control_command (cmd
);
661 /* "while" command support. Executes a body of statements while the
662 loop condition is nonzero. */
665 while_command (const char *arg
, int from_tty
)
668 command_line_up command
= get_command_line (while_control
, arg
);
673 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
675 execute_control_command_untraced (command
.get ());
678 /* "if" command support. Execute either the true or false arm depending
679 on the value of the if conditional. */
682 if_command (const char *arg
, int from_tty
)
685 command_line_up command
= get_command_line (if_control
, arg
);
690 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
692 execute_control_command_untraced (command
.get ());
695 /* Bind the incoming arguments for a user defined command to $arg0,
698 user_args::user_args (const char *command_line
)
702 if (command_line
== NULL
)
705 m_command_line
= command_line
;
706 p
= m_command_line
.c_str ();
710 const char *start_arg
;
715 /* Strip whitespace. */
716 while (*p
== ' ' || *p
== '\t')
719 /* P now points to an argument. */
722 /* Get to the end of this argument. */
725 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
754 m_args
.emplace_back (start_arg
, p
- start_arg
);
758 /* Given character string P, return a point to the first argument
759 ($arg), or NULL if P contains no arguments. */
762 locate_arg (const char *p
)
764 while ((p
= strchr (p
, '$')))
766 if (startswith (p
, "$arg")
767 && (isdigit (p
[4]) || p
[4] == 'c'))
774 /* See cli-script.h. */
777 insert_user_defined_cmd_args (const char *line
)
779 /* If we are not in a user-defined command, treat $argc, $arg0, et
780 cetera as normal convenience variables. */
781 if (user_args_stack
.empty ())
784 const std::unique_ptr
<user_args
> &args
= user_args_stack
.back ();
785 return args
->insert_args (line
);
788 /* Insert the user defined arguments stored in user_args into the $arg
789 arguments found in line. */
792 user_args::insert_args (const char *line
) const
794 std::string new_line
;
797 while ((p
= locate_arg (line
)))
799 new_line
.append (line
, p
- line
);
803 new_line
+= std::to_string (m_args
.size ());
812 i
= strtoul (p
+ 4, &tmp
, 10);
813 if ((i
== 0 && tmp
== p
+ 4) || errno
!= 0)
815 else if (i
>= m_args
.size ())
816 error (_("Missing argument %ld in user function."), i
);
819 new_line
.append (m_args
[i
].data (), m_args
[i
].length ());
824 /* Don't forget the tail. */
825 new_line
.append (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
= XCNEWVEC (struct command_line
*, new_length
);
849 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
851 xfree (command
->body_list
);
852 command
->body_list
= body_list
;
853 command
->body_count
= new_length
;
856 /* Read next line from stdin. Passed to read_command_line_1 and
857 recurse_read_control_structure whenever we need to read commands
861 read_next_line (void)
863 struct ui
*ui
= current_ui
;
864 char *prompt_ptr
, control_prompt
[256];
866 int from_tty
= ui
->instream
== ui
->stdin_stream
;
868 if (control_level
>= 254)
869 error (_("Control nesting too deep!"));
871 /* Set a prompt based on the nesting of the control commands. */
873 || (ui
->instream
== 0 && deprecated_readline_hook
!= NULL
))
875 for (i
= 0; i
< control_level
; i
++)
876 control_prompt
[i
] = ' ';
877 control_prompt
[i
] = '>';
878 control_prompt
[i
+ 1] = '\0';
879 prompt_ptr
= (char *) &control_prompt
[0];
884 return command_line_input (prompt_ptr
, from_tty
, "commands");
887 /* Return true if CMD's name is NAME. */
890 command_name_equals (struct cmd_list_element
*cmd
, const char *name
)
893 && cmd
!= CMD_LIST_AMBIGUOUS
894 && strcmp (cmd
->name
, name
) == 0);
897 /* Given an input line P, skip the command and return a pointer to the
901 line_first_arg (const char *p
)
903 const char *first_arg
= p
+ find_command_name_length (p
);
905 return skip_spaces (first_arg
);
908 /* Process one input line. If the command is an "end", return such an
909 indication to the caller. If PARSE_COMMANDS is true, strip leading
910 whitespace (trailing whitespace is always stripped) in the line,
911 attempt to recognize GDB control commands, and also return an
912 indication if the command is an "else" or a nop.
914 Otherwise, only "end" is recognized. */
916 static enum misc_command_type
917 process_next_line (char *p
, struct command_line
**command
, int parse_commands
,
918 void (*validator
)(char *, void *), void *closure
)
924 /* Not sure what to do here. */
928 /* Strip trailing whitespace. */
929 p_end
= p
+ strlen (p
);
930 while (p_end
> p
&& (p_end
[-1] == ' ' || p_end
[-1] == '\t'))
934 /* Strip leading whitespace. */
935 while (p_start
< p_end
&& (*p_start
== ' ' || *p_start
== '\t'))
938 /* 'end' is always recognized, regardless of parse_commands value.
939 We also permit whitespace before end and after. */
940 if (p_end
- p_start
== 3 && startswith (p_start
, "end"))
945 /* Resolve command abbreviations (e.g. 'ws' for 'while-stepping'). */
946 const char *cmd_name
= p
;
947 struct cmd_list_element
*cmd
948 = lookup_cmd_1 (&cmd_name
, cmdlist
, NULL
, 1);
949 cmd_name
= skip_spaces (cmd_name
);
950 bool inline_cmd
= *cmd_name
!= '\0';
952 /* If commands are parsed, we skip initial spaces. Otherwise,
953 which is the case for Python commands and documentation
954 (see the 'document' command), spaces are preserved. */
957 /* Blanks and comments don't really do anything, but we need to
958 distinguish them from else, end and other commands which can
960 if (p_end
== p
|| p
[0] == '#')
963 /* Is the else clause of an if control structure? */
964 if (p_end
- p
== 4 && startswith (p
, "else"))
967 /* Check for while, if, break, continue, etc and build a new
968 command line structure for them. */
969 if (command_name_equals (cmd
, "while-stepping"))
971 /* Because validate_actionline and encode_action lookup
972 command's line as command, we need the line to
973 include 'while-stepping'.
975 For 'ws' alias, the command will have 'ws', not expanded
976 to 'while-stepping'. This is intentional -- we don't
977 really want frontend to send a command list with 'ws',
978 and next break-info returning command line with
979 'while-stepping'. This should work, but might cause the
980 breakpoint to be marked as changed while it's actually
982 *command
= build_command_line (while_stepping_control
, p
);
984 else if (command_name_equals (cmd
, "while"))
986 *command
= build_command_line (while_control
, line_first_arg (p
));
988 else if (command_name_equals (cmd
, "if"))
990 *command
= build_command_line (if_control
, line_first_arg (p
));
992 else if (command_name_equals (cmd
, "commands"))
994 *command
= build_command_line (commands_control
, line_first_arg (p
));
996 else if (command_name_equals (cmd
, "python") && !inline_cmd
)
998 /* Note that we ignore the inline "python command" form
1000 *command
= build_command_line (python_control
, "");
1002 else if (command_name_equals (cmd
, "compile") && !inline_cmd
)
1004 /* Note that we ignore the inline "compile command" form
1006 *command
= build_command_line (compile_control
, "");
1007 (*command
)->control_u
.compile
.scope
= COMPILE_I_INVALID_SCOPE
;
1009 else if (command_name_equals (cmd
, "guile") && !inline_cmd
)
1011 /* Note that we ignore the inline "guile command" form here. */
1012 *command
= build_command_line (guile_control
, "");
1014 else if (p_end
- p
== 10 && startswith (p
, "loop_break"))
1016 *command
= XNEW (struct command_line
);
1017 (*command
)->next
= NULL
;
1018 (*command
)->line
= NULL
;
1019 (*command
)->control_type
= break_control
;
1020 (*command
)->body_count
= 0;
1021 (*command
)->body_list
= NULL
;
1023 else if (p_end
- p
== 13 && startswith (p
, "loop_continue"))
1025 *command
= XNEW (struct command_line
);
1026 (*command
)->next
= NULL
;
1027 (*command
)->line
= NULL
;
1028 (*command
)->control_type
= continue_control
;
1029 (*command
)->body_count
= 0;
1030 (*command
)->body_list
= NULL
;
1036 if (!parse_commands
|| not_handled
)
1038 /* A normal command. */
1039 *command
= XNEW (struct command_line
);
1040 (*command
)->next
= NULL
;
1041 (*command
)->line
= savestring (p
, p_end
- p
);
1042 (*command
)->control_type
= simple_control
;
1043 (*command
)->body_count
= 0;
1044 (*command
)->body_list
= NULL
;
1052 validator ((*command
)->line
, closure
);
1054 CATCH (ex
, RETURN_MASK_ALL
)
1057 throw_exception (ex
);
1062 /* Nothing special. */
1066 /* Recursively read in the control structures and create a
1067 command_line structure from them. Use read_next_line_func to
1068 obtain lines of the command. */
1070 static enum command_control_type
1071 recurse_read_control_structure (char * (*read_next_line_func
) (void),
1072 struct command_line
*current_cmd
,
1073 void (*validator
)(char *, void *),
1076 int current_body
, i
;
1077 enum misc_command_type val
;
1078 enum command_control_type ret
;
1079 struct command_line
**body_ptr
, *child_tail
, *next
;
1084 /* Sanity checks. */
1085 if (current_cmd
->control_type
== simple_control
)
1086 error (_("Recursed on a simple control type."));
1088 if (current_body
> current_cmd
->body_count
)
1089 error (_("Allocated body is smaller than this command type needs."));
1091 /* Read lines from the input stream and build control structures. */
1097 val
= process_next_line (read_next_line_func (), &next
,
1098 current_cmd
->control_type
!= python_control
1099 && current_cmd
->control_type
!= guile_control
1100 && current_cmd
->control_type
!= compile_control
,
1101 validator
, closure
);
1103 /* Just skip blanks and comments. */
1104 if (val
== nop_command
)
1107 if (val
== end_command
)
1109 if (multi_line_command_p (current_cmd
->control_type
))
1111 /* Success reading an entire canned sequence of commands. */
1112 ret
= simple_control
;
1117 ret
= invalid_control
;
1122 /* Not the end of a control structure. */
1123 if (val
== else_command
)
1125 if (current_cmd
->control_type
== if_control
1126 && current_body
== 1)
1128 realloc_body_list (current_cmd
, 2);
1135 ret
= invalid_control
;
1142 child_tail
->next
= next
;
1146 body_ptr
= current_cmd
->body_list
;
1147 for (i
= 1; i
< current_body
; i
++)
1156 /* If the latest line is another control structure, then recurse
1158 if (multi_line_command_p (next
->control_type
))
1161 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1162 validator
, closure
);
1165 if (ret
!= simple_control
)
1175 /* Read lines from the input stream and accumulate them in a chain of
1176 struct command_line's, which is then returned. For input from a
1177 terminal, the special command "end" is used to mark the end of the
1178 input, and is not included in the returned chain of commands.
1180 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1181 is always stripped) in the line and attempt to recognize GDB control
1182 commands. Otherwise, only "end" is recognized. */
1184 #define END_MESSAGE "End with a line saying just \"end\"."
1187 read_command_lines (char *prompt_arg
, int from_tty
, int parse_commands
,
1188 void (*validator
)(char *, void *), void *closure
)
1190 if (from_tty
&& input_interactive_p (current_ui
))
1192 if (deprecated_readline_begin_hook
)
1194 /* Note - intentional to merge messages with no newline. */
1195 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
,
1200 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1201 gdb_flush (gdb_stdout
);
1206 /* Reading commands assumes the CLI behavior, so temporarily
1207 override the current interpreter with CLI. */
1208 command_line_up head
;
1209 if (current_interp_named_p (INTERP_CONSOLE
))
1210 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1211 validator
, closure
);
1214 scoped_restore_interp
interp_restorer (INTERP_CONSOLE
);
1216 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1217 validator
, closure
);
1220 if (from_tty
&& input_interactive_p (current_ui
)
1221 && deprecated_readline_end_hook
)
1223 (*deprecated_readline_end_hook
) ();
1228 /* Act the same way as read_command_lines, except that each new line is
1229 obtained using READ_NEXT_LINE_FUNC. */
1232 read_command_lines_1 (char * (*read_next_line_func
) (void), int parse_commands
,
1233 void (*validator
)(char *, void *), void *closure
)
1235 struct command_line
*tail
, *next
;
1236 command_line_up head
;
1237 enum command_control_type ret
;
1238 enum misc_command_type val
;
1246 val
= process_next_line (read_next_line_func (), &next
, parse_commands
,
1247 validator
, closure
);
1249 /* Ignore blank lines or comments. */
1250 if (val
== nop_command
)
1253 if (val
== end_command
)
1255 ret
= simple_control
;
1259 if (val
!= ok_command
)
1261 ret
= invalid_control
;
1265 if (multi_line_command_p (next
->control_type
))
1268 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1269 validator
, closure
);
1272 if (ret
== invalid_control
)
1289 if (ret
== invalid_control
)
1295 /* Free a chain of struct command_line's. */
1298 free_command_lines (struct command_line
**lptr
)
1300 struct command_line
*l
= *lptr
;
1301 struct command_line
*next
;
1302 struct command_line
**blist
;
1307 if (l
->body_count
> 0)
1309 blist
= l
->body_list
;
1310 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1311 free_command_lines (blist
);
1322 copy_command_lines (struct command_line
*cmds
)
1324 struct command_line
*result
= NULL
;
1328 result
= XNEW (struct command_line
);
1330 result
->next
= copy_command_lines (cmds
->next
).release ();
1331 result
->line
= xstrdup (cmds
->line
);
1332 result
->control_type
= cmds
->control_type
;
1333 result
->body_count
= cmds
->body_count
;
1334 if (cmds
->body_count
> 0)
1338 result
->body_list
= XNEWVEC (struct command_line
*, cmds
->body_count
);
1340 for (i
= 0; i
< cmds
->body_count
; i
++)
1341 result
->body_list
[i
]
1342 = copy_command_lines (cmds
->body_list
[i
]).release ();
1345 result
->body_list
= NULL
;
1348 return command_line_up (result
);
1351 /* Validate that *COMNAME is a valid name for a command. Return the
1352 containing command list, in case it starts with a prefix command.
1353 The prefix must already exist. *COMNAME is advanced to point after
1354 any prefix, and a NUL character overwrites the space after the
1357 static struct cmd_list_element
**
1358 validate_comname (const char **comname
)
1360 struct cmd_list_element
**list
= &cmdlist
;
1361 const char *p
, *last_word
;
1364 error_no_arg (_("name of command to define"));
1366 /* Find the last word of the argument. */
1367 p
= *comname
+ strlen (*comname
);
1368 while (p
> *comname
&& isspace (p
[-1]))
1370 while (p
> *comname
&& !isspace (p
[-1]))
1374 /* Find the corresponding command list. */
1375 if (last_word
!= *comname
)
1377 struct cmd_list_element
*c
;
1379 /* Separate the prefix and the command. */
1380 std::string
prefix (*comname
, last_word
- 1);
1381 const char *tem
= prefix
.c_str ();
1383 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1384 if (c
->prefixlist
== NULL
)
1385 error (_("\"%s\" is not a prefix command."), prefix
.c_str ());
1387 list
= c
->prefixlist
;
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 (const char *ignore
, int from_tty
)
1409 define_command (const char *comname
, int from_tty
)
1411 #define MAX_TMPBUF 128
1418 struct cmd_list_element
*c
, *newc
, *hookc
= 0, **list
;
1419 const char *tem
, *comfull
;
1420 char tmpbuf
[MAX_TMPBUF
];
1421 int hook_type
= CMD_NO_HOOK
;
1422 int hook_name_size
= 0;
1424 #define HOOK_STRING "hook-"
1426 #define HOOK_POST_STRING "hookpost-"
1427 #define HOOK_POST_LEN 9
1430 list
= validate_comname (&comname
);
1432 /* Look it up, and verify that we got an exact match. */
1434 c
= lookup_cmd (&tem
, *list
, "", -1, 1);
1435 if (c
&& strcmp (comname
, c
->name
) != 0)
1442 if (c
->theclass
== class_user
|| c
->theclass
== class_alias
)
1443 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1445 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1447 error (_("Command \"%s\" not redefined."), c
->name
);
1450 /* If this new command is a hook, then mark the command which it
1451 is hooking. Note that we allow hooking `help' commands, so that
1452 we can hook the `stop' pseudo-command. */
1454 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1456 hook_type
= CMD_PRE_HOOK
;
1457 hook_name_size
= HOOK_LEN
;
1459 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1461 hook_type
= CMD_POST_HOOK
;
1462 hook_name_size
= HOOK_POST_LEN
;
1465 if (hook_type
!= CMD_NO_HOOK
)
1467 /* Look up cmd it hooks, and verify that we got an exact match. */
1468 tem
= comname
+ hook_name_size
;
1469 hookc
= lookup_cmd (&tem
, *list
, "", -1, 0);
1470 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1474 warning (_("Your new `%s' command does not "
1475 "hook any existing command."),
1477 if (!query (_("Proceed? ")))
1478 error (_("Not confirmed."));
1482 comname
= xstrdup (comname
);
1484 xsnprintf (tmpbuf
, sizeof (tmpbuf
),
1485 "Type commands for definition of \"%s\".", comfull
);
1486 command_line_up cmds
= read_command_lines (tmpbuf
, from_tty
, 1, 0, 0);
1488 if (c
&& c
->theclass
== class_user
)
1489 free_command_lines (&c
->user_commands
);
1491 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1492 (c
&& c
->theclass
== class_user
)
1493 ? c
->doc
: xstrdup ("User-defined."), list
);
1494 newc
->user_commands
= cmds
.release ();
1496 /* If this new command is a hook, then mark both commands as being
1503 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1504 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1507 hookc
->hook_post
= newc
; /* Target gets hooked. */
1508 newc
->hookee_post
= hookc
; /* We are marked as hooking
1512 /* Should never come here as hookc would be 0. */
1513 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1519 document_command (const char *comname
, int from_tty
)
1521 struct cmd_list_element
*c
, **list
;
1523 const char *comfull
;
1527 list
= validate_comname (&comname
);
1530 c
= lookup_cmd (&tem
, *list
, "", 0, 1);
1532 if (c
->theclass
!= class_user
)
1533 error (_("Command \"%s\" is built-in."), comfull
);
1535 xsnprintf (tmpbuf
, sizeof (tmpbuf
), "Type documentation for \"%s\".",
1537 command_line_up doclines
= read_command_lines (tmpbuf
, from_tty
, 0, 0, 0);
1540 xfree ((char *) c
->doc
);
1543 struct command_line
*cl1
;
1547 for (cl1
= doclines
.get (); cl1
; cl1
= cl1
->next
)
1548 len
+= strlen (cl1
->line
) + 1;
1550 doc
= (char *) xmalloc (len
+ 1);
1553 for (cl1
= doclines
.get (); cl1
; cl1
= cl1
->next
)
1555 strcat (doc
, cl1
->line
);
1564 /* Used to implement source_command. */
1567 script_from_file (FILE *stream
, const char *file
)
1570 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1572 scoped_restore restore_line_number
1573 = make_scoped_restore (&source_line_number
, 0);
1574 scoped_restore resotre_file
1575 = make_scoped_restore (&source_file_name
, file
);
1577 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
1581 read_command_file (stream
);
1583 CATCH (e
, RETURN_MASK_ERROR
)
1585 /* Re-throw the error, but with the file name information
1587 throw_error (e
.error
,
1588 _("%s:%d: Error in sourced command file:\n%s"),
1589 source_file_name
, source_line_number
, e
.message
);
1594 /* Print the definition of user command C to STREAM. Or, if C is a
1595 prefix command, show the definitions of all user commands under C
1596 (recursively). PREFIX and NAME combined are the name of the
1599 show_user_1 (struct cmd_list_element
*c
, const char *prefix
, const char *name
,
1600 struct ui_file
*stream
)
1602 struct command_line
*cmdlines
;
1604 if (c
->prefixlist
!= NULL
)
1606 const char *prefixname
= c
->prefixname
;
1608 for (c
= *c
->prefixlist
; c
!= NULL
; c
= c
->next
)
1609 if (c
->theclass
== class_user
|| c
->prefixlist
!= NULL
)
1610 show_user_1 (c
, prefixname
, c
->name
, gdb_stdout
);
1614 cmdlines
= c
->user_commands
;
1615 fprintf_filtered (stream
, "User command \"%s%s\":\n", prefix
, name
);
1619 print_command_lines (current_uiout
, cmdlines
, 1);
1620 fputs_filtered ("\n", stream
);
1624 _initialize_cli_script (void)
1626 add_com ("document", class_support
, document_command
, _("\
1627 Document a user-defined command.\n\
1628 Give command name as argument. Give documentation on following lines.\n\
1629 End with a line of just \"end\"."));
1630 add_com ("define", class_support
, define_command
, _("\
1631 Define a new command name. Command name is argument.\n\
1632 Definition appears on following lines, one command per line.\n\
1633 End with a line of just \"end\".\n\
1634 Use the \"document\" command to give documentation for the new command.\n\
1635 Commands defined in this way may have up to ten arguments."));
1637 add_com ("while", class_support
, while_command
, _("\
1638 Execute nested commands WHILE the conditional expression is non zero.\n\
1639 The conditional expression must follow the word `while' and must in turn be\n\
1640 followed by a new line. The nested commands must be entered one per line,\n\
1641 and should be terminated by the word `end'."));
1643 add_com ("if", class_support
, if_command
, _("\
1644 Execute nested commands once IF the conditional expression is non zero.\n\
1645 The conditional expression must follow the word `if' and must in turn be\n\
1646 followed by a new line. The nested commands must be entered one per line,\n\
1647 and should be terminated by the word 'else' or `end'. If an else clause\n\
1648 is used, the same rules apply to its nested commands as to the first ones."));