1 /* GDB CLI command scripting.
3 Copyright (C) 1986-2014 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 */
26 #include "exceptions.h"
28 #include "breakpoint.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-decode.h"
31 #include "cli/cli-script.h"
33 #include "extension.h"
36 /* Prototypes for local functions. */
38 static enum command_control_type
39 recurse_read_control_structure (char * (*read_next_line_func
) (void),
40 struct command_line
*current_cmd
,
41 void (*validator
)(char *, void *),
44 static char *insert_args (char *line
);
46 static struct cleanup
* setup_user_args (char *p
);
48 static char *read_next_line (void);
50 /* Level of control structure when reading. */
51 static int control_level
;
53 /* Level of control structure when executing. */
54 static int command_nest_depth
= 1;
56 /* This is to prevent certain commands being printed twice. */
57 static int suppress_next_print_command_trace
= 0;
59 /* Structure for arguments to user defined functions. */
60 #define MAXUSERARGS 10
63 struct user_args
*next
;
64 /* It is necessary to store a malloced copy of the command line to
65 ensure that the arguments are not overwritten before they are
79 /* Return non-zero if TYPE is a multi-line command (i.e., is terminated
83 multi_line_command_p (enum command_control_type type
)
89 case while_stepping_control
:
90 case commands_control
:
99 /* Allocate, initialize a new command line structure for one of the
100 control commands (if/while). */
102 static struct command_line
*
103 build_command_line (enum command_control_type type
, char *args
)
105 struct command_line
*cmd
;
107 if (args
== NULL
&& (type
== if_control
|| type
== while_control
))
108 error (_("if/while commands require arguments."));
109 gdb_assert (args
!= NULL
);
111 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
113 cmd
->control_type
= type
;
117 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
119 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
120 cmd
->line
= xstrdup (args
);
125 /* Build and return a new command structure for the control commands
126 such as "if" and "while". */
128 struct command_line
*
129 get_command_line (enum command_control_type type
, char *arg
)
131 struct command_line
*cmd
;
132 struct cleanup
*old_chain
= NULL
;
134 /* Allocate and build a new command line structure. */
135 cmd
= build_command_line (type
, arg
);
137 old_chain
= make_cleanup_free_command_lines (&cmd
);
139 /* Read in the body of this command. */
140 if (recurse_read_control_structure (read_next_line
, cmd
, 0, 0)
143 warning (_("Error reading in canned sequence of commands."));
144 do_cleanups (old_chain
);
148 discard_cleanups (old_chain
);
152 /* Recursively print a command (including full control structures). */
155 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
158 struct command_line
*list
;
164 ui_out_spaces (uiout
, 2 * depth
);
166 /* A simple command, print it and continue. */
167 if (list
->control_type
== simple_control
)
169 ui_out_field_string (uiout
, NULL
, list
->line
);
170 ui_out_text (uiout
, "\n");
175 /* loop_continue to jump to the start of a while loop, print it
177 if (list
->control_type
== continue_control
)
179 ui_out_field_string (uiout
, NULL
, "loop_continue");
180 ui_out_text (uiout
, "\n");
185 /* loop_break to break out of a while loop, print it and
187 if (list
->control_type
== break_control
)
189 ui_out_field_string (uiout
, NULL
, "loop_break");
190 ui_out_text (uiout
, "\n");
195 /* A while command. Recursively print its subcommands and
197 if (list
->control_type
== while_control
198 || list
->control_type
== while_stepping_control
)
200 /* For while-stepping, the line includes the 'while-stepping'
201 token. See comment in process_next_line for explanation.
202 Here, take care not print 'while-stepping' twice. */
203 if (list
->control_type
== while_control
)
204 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
206 ui_out_field_string (uiout
, NULL
, list
->line
);
207 ui_out_text (uiout
, "\n");
208 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
210 ui_out_spaces (uiout
, 2 * depth
);
211 ui_out_field_string (uiout
, NULL
, "end");
212 ui_out_text (uiout
, "\n");
217 /* An if command. Recursively print both arms before
219 if (list
->control_type
== if_control
)
221 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
222 ui_out_text (uiout
, "\n");
224 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
226 /* Show the false arm if it exists. */
227 if (list
->body_count
== 2)
230 ui_out_spaces (uiout
, 2 * depth
);
231 ui_out_field_string (uiout
, NULL
, "else");
232 ui_out_text (uiout
, "\n");
233 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
237 ui_out_spaces (uiout
, 2 * depth
);
238 ui_out_field_string (uiout
, NULL
, "end");
239 ui_out_text (uiout
, "\n");
244 /* A commands command. Print the breakpoint commands and
246 if (list
->control_type
== commands_control
)
249 ui_out_field_fmt (uiout
, NULL
, "commands %s", list
->line
);
251 ui_out_field_string (uiout
, NULL
, "commands");
252 ui_out_text (uiout
, "\n");
253 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
255 ui_out_spaces (uiout
, 2 * depth
);
256 ui_out_field_string (uiout
, NULL
, "end");
257 ui_out_text (uiout
, "\n");
262 if (list
->control_type
== python_control
)
264 ui_out_field_string (uiout
, NULL
, "python");
265 ui_out_text (uiout
, "\n");
266 /* Don't indent python code at all. */
267 print_command_lines (uiout
, *list
->body_list
, 0);
269 ui_out_spaces (uiout
, 2 * depth
);
270 ui_out_field_string (uiout
, NULL
, "end");
271 ui_out_text (uiout
, "\n");
276 if (list
->control_type
== guile_control
)
278 ui_out_field_string (uiout
, NULL
, "guile");
279 ui_out_text (uiout
, "\n");
280 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
282 ui_out_spaces (uiout
, 2 * depth
);
283 ui_out_field_string (uiout
, NULL
, "end");
284 ui_out_text (uiout
, "\n");
289 /* Ignore illegal command type and try next. */
294 /* Handle pre-post hooks. */
297 clear_hook_in_cleanup (void *data
)
299 struct cmd_list_element
*c
= data
;
301 c
->hook_in
= 0; /* Allow hook to work again once it is complete. */
305 execute_cmd_pre_hook (struct cmd_list_element
*c
)
307 if ((c
->hook_pre
) && (!c
->hook_in
))
309 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
310 c
->hook_in
= 1; /* Prevent recursive hooking. */
311 execute_user_command (c
->hook_pre
, (char *) 0);
312 do_cleanups (cleanups
);
317 execute_cmd_post_hook (struct cmd_list_element
*c
)
319 if ((c
->hook_post
) && (!c
->hook_in
))
321 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
323 c
->hook_in
= 1; /* Prevent recursive hooking. */
324 execute_user_command (c
->hook_post
, (char *) 0);
325 do_cleanups (cleanups
);
329 /* Execute the command in CMD. */
331 do_restore_user_call_depth (void * call_depth
)
333 int *depth
= call_depth
;
342 execute_user_command (struct cmd_list_element
*c
, char *args
)
344 struct command_line
*cmdlines
;
345 struct cleanup
*old_chain
;
346 enum command_control_type ret
;
347 static int user_call_depth
= 0;
348 extern unsigned int max_user_call_depth
;
350 cmdlines
= c
->user_commands
;
355 old_chain
= setup_user_args (args
);
357 if (++user_call_depth
> max_user_call_depth
)
358 error (_("Max user call depth exceeded -- command aborted."));
360 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
362 /* Set the instream to 0, indicating execution of a
363 user-defined function. */
364 make_cleanup (do_restore_instream_cleanup
, instream
);
365 instream
= (FILE *) 0;
367 /* Also set the global in_user_command, so that NULL instream is
368 not confused with Insight. */
371 make_cleanup_restore_integer (&interpreter_async
);
372 interpreter_async
= 0;
374 command_nest_depth
++;
377 ret
= execute_control_command (cmdlines
);
378 if (ret
!= simple_control
&& ret
!= break_control
)
380 warning (_("Error executing canned sequence of commands."));
383 cmdlines
= cmdlines
->next
;
385 command_nest_depth
--;
386 do_cleanups (old_chain
);
389 /* This function is called every time GDB prints a prompt. It ensures
390 that errors and the like do not confuse the command tracing. */
393 reset_command_nest_depth (void)
395 command_nest_depth
= 1;
398 suppress_next_print_command_trace
= 0;
401 /* Print the command, prefixed with '+' to represent the call depth.
402 This is slightly complicated because this function may be called
403 from execute_command and execute_control_command. Unfortunately
404 execute_command also prints the top level control commands.
405 In these cases execute_command will call execute_control_command
406 via while_command or if_command. Inner levels of 'if' and 'while'
407 are dealt with directly. Therefore we can use these functions
408 to determine whether the command has been printed already or not. */
410 print_command_trace (const char *cmd
)
414 if (suppress_next_print_command_trace
)
416 suppress_next_print_command_trace
= 0;
420 if (!source_verbose
&& !trace_commands
)
423 for (i
=0; i
< command_nest_depth
; i
++)
424 printf_filtered ("+");
426 printf_filtered ("%s\n", cmd
);
429 enum command_control_type
430 execute_control_command (struct command_line
*cmd
)
432 struct expression
*expr
;
433 struct command_line
*current
;
434 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
436 struct value
*val_mark
;
438 enum command_control_type ret
;
441 /* Start by assuming failure, if a problem is detected, the code
442 below will simply "break" out of the switch. */
443 ret
= invalid_control
;
445 switch (cmd
->control_type
)
448 /* A simple command, execute it and return. */
449 new_line
= insert_args (cmd
->line
);
452 make_cleanup (free_current_contents
, &new_line
);
453 execute_command (new_line
, 0);
454 ret
= cmd
->control_type
;
457 case continue_control
:
458 print_command_trace ("loop_continue");
460 /* Return for "continue", and "break" so we can either
461 continue the loop at the top, or break out. */
462 ret
= cmd
->control_type
;
466 print_command_trace ("loop_break");
468 /* Return for "continue", and "break" so we can either
469 continue the loop at the top, or break out. */
470 ret
= cmd
->control_type
;
475 int len
= strlen (cmd
->line
) + 7;
476 char *buffer
= alloca (len
);
478 xsnprintf (buffer
, len
, "while %s", cmd
->line
);
479 print_command_trace (buffer
);
481 /* Parse the loop control expression for the while statement. */
482 new_line
= insert_args (cmd
->line
);
485 make_cleanup (free_current_contents
, &new_line
);
486 expr
= parse_expression (new_line
);
487 make_cleanup (free_current_contents
, &expr
);
489 ret
= simple_control
;
492 /* Keep iterating so long as the expression is true. */
499 /* Evaluate the expression. */
500 val_mark
= value_mark ();
501 val
= evaluate_expression (expr
);
502 cond_result
= value_true (val
);
503 value_free_to_mark (val_mark
);
505 /* If the value is false, then break out of the loop. */
509 /* Execute the body of the while statement. */
510 current
= *cmd
->body_list
;
513 command_nest_depth
++;
514 ret
= execute_control_command (current
);
515 command_nest_depth
--;
517 /* If we got an error, or a "break" command, then stop
519 if (ret
== invalid_control
|| ret
== break_control
)
525 /* If we got a "continue" command, then restart the loop
527 if (ret
== continue_control
)
530 /* Get the next statement. */
531 current
= current
->next
;
535 /* Reset RET so that we don't recurse the break all the way down. */
536 if (ret
== break_control
)
537 ret
= simple_control
;
544 int len
= strlen (cmd
->line
) + 4;
545 char *buffer
= alloca (len
);
547 xsnprintf (buffer
, len
, "if %s", cmd
->line
);
548 print_command_trace (buffer
);
550 new_line
= insert_args (cmd
->line
);
553 make_cleanup (free_current_contents
, &new_line
);
554 /* Parse the conditional for the if statement. */
555 expr
= parse_expression (new_line
);
556 make_cleanup (free_current_contents
, &expr
);
559 ret
= simple_control
;
561 /* Evaluate the conditional. */
562 val_mark
= value_mark ();
563 val
= evaluate_expression (expr
);
565 /* Choose which arm to take commands from based on the value
566 of the conditional expression. */
567 if (value_true (val
))
568 current
= *cmd
->body_list
;
569 else if (cmd
->body_count
== 2)
570 current
= *(cmd
->body_list
+ 1);
571 value_free_to_mark (val_mark
);
573 /* Execute commands in the given arm. */
576 command_nest_depth
++;
577 ret
= execute_control_command (current
);
578 command_nest_depth
--;
580 /* If we got an error, get out. */
581 if (ret
!= simple_control
)
584 /* Get the next statement in the body. */
585 current
= current
->next
;
591 case commands_control
:
593 /* Breakpoint commands list, record the commands in the
594 breakpoint's command list and return. */
595 new_line
= insert_args (cmd
->line
);
598 make_cleanup (free_current_contents
, &new_line
);
599 ret
= commands_from_control_command (new_line
, cmd
);
606 eval_ext_lang_from_control_command (cmd
);
607 ret
= simple_control
;
612 warning (_("Invalid control type in canned commands structure."));
616 do_cleanups (old_chain
);
621 /* Like execute_control_command, but first set
622 suppress_next_print_command_trace. */
624 enum command_control_type
625 execute_control_command_untraced (struct command_line
*cmd
)
627 suppress_next_print_command_trace
= 1;
628 return execute_control_command (cmd
);
632 /* "while" command support. Executes a body of statements while the
633 loop condition is nonzero. */
636 while_command (char *arg
, int from_tty
)
638 struct command_line
*command
= NULL
;
639 struct cleanup
*old_chain
;
642 command
= get_command_line (while_control
, arg
);
647 old_chain
= make_cleanup_restore_integer (&interpreter_async
);
648 interpreter_async
= 0;
650 execute_control_command_untraced (command
);
651 free_command_lines (&command
);
653 do_cleanups (old_chain
);
656 /* "if" command support. Execute either the true or false arm depending
657 on the value of the if conditional. */
660 if_command (char *arg
, int from_tty
)
662 struct command_line
*command
= NULL
;
663 struct cleanup
*old_chain
;
666 command
= get_command_line (if_control
, arg
);
671 old_chain
= make_cleanup_restore_integer (&interpreter_async
);
672 interpreter_async
= 0;
674 execute_control_command_untraced (command
);
675 free_command_lines (&command
);
677 do_cleanups (old_chain
);
682 arg_cleanup (void *ignore
)
684 struct user_args
*oargs
= user_args
;
687 internal_error (__FILE__
, __LINE__
,
688 _("arg_cleanup called with no user args.\n"));
690 user_args
= user_args
->next
;
691 xfree (oargs
->command
);
695 /* Bind the incomming arguments for a user defined command to
696 $arg0, $arg1 ... $argMAXUSERARGS. */
698 static struct cleanup
*
699 setup_user_args (char *p
)
701 struct user_args
*args
;
702 struct cleanup
*old_chain
;
703 unsigned int arg_count
= 0;
705 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
706 memset (args
, 0, sizeof (struct user_args
));
708 args
->next
= user_args
;
711 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
716 user_args
->command
= p
= xstrdup (p
);
725 if (arg_count
>= MAXUSERARGS
)
726 error (_("user defined function may only have %d arguments."),
729 /* Strip whitespace. */
730 while (*p
== ' ' || *p
== '\t')
733 /* P now points to an argument. */
735 user_args
->a
[arg_count
].arg
= p
;
737 /* Get to the end of this argument. */
740 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
769 user_args
->a
[arg_count
].len
= p
- start_arg
;
776 /* Given character string P, return a point to the first argument
777 ($arg), or NULL if P contains no arguments. */
782 while ((p
= strchr (p
, '$')))
784 if (strncmp (p
, "$arg", 4) == 0
785 && (isdigit (p
[4]) || p
[4] == 'c'))
792 /* Insert the user defined arguments stored in user_arg into the $arg
793 arguments found in line, with the updated copy being placed into
797 insert_args (char *line
)
799 char *p
, *save_line
, *new_line
;
802 /* If we are not in a user-defined function, treat $argc, $arg0, et
803 cetera as normal convenience variables. */
804 if (user_args
== NULL
)
805 return xstrdup (line
);
807 /* First we need to know how much memory to allocate for the new
811 while ((p
= locate_arg (line
)))
818 /* $argc. Number will be <=10. */
819 len
+= user_args
->count
== 10 ? 2 : 1;
821 else if (i
>= user_args
->count
)
823 error (_("Missing argument %d in user function."), i
);
828 len
+= user_args
->a
[i
].len
;
833 /* Don't forget the tail. */
834 len
+= strlen (line
);
836 /* Allocate space for the new line and fill it in. */
837 new_line
= (char *) xmalloc (len
+ 1);
838 if (new_line
== NULL
)
841 /* Restore pointer to beginning of old line. */
844 /* Save pointer to beginning of new line. */
845 save_line
= new_line
;
847 while ((p
= locate_arg (line
)))
851 memcpy (new_line
, line
, p
- line
);
852 new_line
+= p
- line
;
856 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
857 if (user_args
->count
== 10)
863 *(new_line
++) = user_args
->count
+ '0';
868 len
= user_args
->a
[i
].len
;
871 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
877 /* Don't forget the tail. */
878 strcpy (new_line
, line
);
880 /* Return a pointer to the beginning of the new line. */
885 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
886 code bodies. This is typically used when we encounter an "else"
887 clause for an "if" command. */
890 realloc_body_list (struct command_line
*command
, int new_length
)
893 struct command_line
**body_list
;
895 n
= command
->body_count
;
901 body_list
= (struct command_line
**)
902 xmalloc (sizeof (struct command_line
*) * new_length
);
904 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
905 memset (body_list
+ n
, 0, sizeof (struct command_line
*) * (new_length
- n
));
907 xfree (command
->body_list
);
908 command
->body_list
= body_list
;
909 command
->body_count
= new_length
;
912 /* Read next line from stdout. Passed to read_command_line_1 and
913 recurse_read_control_structure whenever we need to read commands
917 read_next_line (void)
919 char *prompt_ptr
, control_prompt
[256];
922 if (control_level
>= 254)
923 error (_("Control nesting too deep!"));
925 /* Set a prompt based on the nesting of the control commands. */
926 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
928 for (i
= 0; i
< control_level
; i
++)
929 control_prompt
[i
] = ' ';
930 control_prompt
[i
] = '>';
931 control_prompt
[i
+ 1] = '\0';
932 prompt_ptr
= (char *) &control_prompt
[0];
937 return command_line_input (prompt_ptr
, instream
== stdin
, "commands");
940 /* Process one input line. If the command is an "end", return such an
941 indication to the caller. If PARSE_COMMANDS is true, strip leading
942 whitespace (trailing whitespace is always stripped) in the line,
943 attempt to recognize GDB control commands, and also return an
944 indication if the command is an "else" or a nop.
946 Otherwise, only "end" is recognized. */
948 static enum misc_command_type
949 process_next_line (char *p
, struct command_line
**command
, int parse_commands
,
950 void (*validator
)(char *, void *), void *closure
)
956 /* Not sure what to do here. */
960 /* Strip trailing whitespace. */
961 p_end
= p
+ strlen (p
);
962 while (p_end
> p
&& (p_end
[-1] == ' ' || p_end
[-1] == '\t'))
966 /* Strip leading whitespace. */
967 while (p_start
< p_end
&& (*p_start
== ' ' || *p_start
== '\t'))
970 /* 'end' is always recognized, regardless of parse_commands value.
971 We also permit whitespace before end and after. */
972 if (p_end
- p_start
== 3 && !strncmp (p_start
, "end", 3))
977 /* If commands are parsed, we skip initial spaces. Otherwise,
978 which is the case for Python commands and documentation
979 (see the 'document' command), spaces are preserved. */
982 /* Blanks and comments don't really do anything, but we need to
983 distinguish them from else, end and other commands which can
985 if (p_end
== p
|| p
[0] == '#')
988 /* Is the else clause of an if control structure? */
989 if (p_end
- p
== 4 && !strncmp (p
, "else", 4))
992 /* Check for while, if, break, continue, etc and build a new
993 command line structure for them. */
994 if ((p_end
- p
>= 14 && !strncmp (p
, "while-stepping", 14))
995 || (p_end
- p
>= 8 && !strncmp (p
, "stepping", 8))
996 || (p_end
- p
>= 2 && !strncmp (p
, "ws", 2)))
998 /* Because validate_actionline and encode_action lookup
999 command's line as command, we need the line to
1000 include 'while-stepping'.
1002 For 'ws' alias, the command will have 'ws', not expanded
1003 to 'while-stepping'. This is intentional -- we don't
1004 really want frontend to send a command list with 'ws',
1005 and next break-info returning command line with
1006 'while-stepping'. This should work, but might cause the
1007 breakpoint to be marked as changed while it's actually
1009 *command
= build_command_line (while_stepping_control
, p
);
1011 else if (p_end
- p
> 5 && !strncmp (p
, "while", 5))
1016 while (first_arg
< p_end
&& isspace (*first_arg
))
1018 *command
= build_command_line (while_control
, first_arg
);
1020 else if (p_end
- p
> 2 && !strncmp (p
, "if", 2))
1025 while (first_arg
< p_end
&& isspace (*first_arg
))
1027 *command
= build_command_line (if_control
, first_arg
);
1029 else if (p_end
- p
>= 8 && !strncmp (p
, "commands", 8))
1034 while (first_arg
< p_end
&& isspace (*first_arg
))
1036 *command
= build_command_line (commands_control
, first_arg
);
1038 else if (p_end
- p
== 6 && !strncmp (p
, "python", 6))
1040 /* Note that we ignore the inline "python command" form
1042 *command
= build_command_line (python_control
, "");
1044 else if (p_end
- p
== 5 && !strncmp (p
, "guile", 5))
1046 /* Note that we ignore the inline "guile command" form here. */
1047 *command
= build_command_line (guile_control
, "");
1049 else if (p_end
- p
== 10 && !strncmp (p
, "loop_break", 10))
1051 *command
= (struct command_line
*)
1052 xmalloc (sizeof (struct command_line
));
1053 (*command
)->next
= NULL
;
1054 (*command
)->line
= NULL
;
1055 (*command
)->control_type
= break_control
;
1056 (*command
)->body_count
= 0;
1057 (*command
)->body_list
= NULL
;
1059 else if (p_end
- p
== 13 && !strncmp (p
, "loop_continue", 13))
1061 *command
= (struct command_line
*)
1062 xmalloc (sizeof (struct command_line
));
1063 (*command
)->next
= NULL
;
1064 (*command
)->line
= NULL
;
1065 (*command
)->control_type
= continue_control
;
1066 (*command
)->body_count
= 0;
1067 (*command
)->body_list
= NULL
;
1073 if (!parse_commands
|| not_handled
)
1075 /* A normal command. */
1076 *command
= (struct command_line
*)
1077 xmalloc (sizeof (struct command_line
));
1078 (*command
)->next
= NULL
;
1079 (*command
)->line
= savestring (p
, p_end
- p
);
1080 (*command
)->control_type
= simple_control
;
1081 (*command
)->body_count
= 0;
1082 (*command
)->body_list
= NULL
;
1087 volatile struct gdb_exception ex
;
1089 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1091 validator ((*command
)->line
, closure
);
1096 throw_exception (ex
);
1100 /* Nothing special. */
1104 /* Recursively read in the control structures and create a
1105 command_line structure from them. Use read_next_line_func to
1106 obtain lines of the command. */
1108 static enum command_control_type
1109 recurse_read_control_structure (char * (*read_next_line_func
) (void),
1110 struct command_line
*current_cmd
,
1111 void (*validator
)(char *, void *),
1114 int current_body
, i
;
1115 enum misc_command_type val
;
1116 enum command_control_type ret
;
1117 struct command_line
**body_ptr
, *child_tail
, *next
;
1122 /* Sanity checks. */
1123 if (current_cmd
->control_type
== simple_control
)
1124 error (_("Recursed on a simple control type."));
1126 if (current_body
> current_cmd
->body_count
)
1127 error (_("Allocated body is smaller than this command type needs."));
1129 /* Read lines from the input stream and build control structures. */
1135 val
= process_next_line (read_next_line_func (), &next
,
1136 current_cmd
->control_type
!= python_control
1137 && current_cmd
->control_type
!= guile_control
,
1138 validator
, closure
);
1140 /* Just skip blanks and comments. */
1141 if (val
== nop_command
)
1144 if (val
== end_command
)
1146 if (multi_line_command_p (current_cmd
->control_type
))
1148 /* Success reading an entire canned sequence of commands. */
1149 ret
= simple_control
;
1154 ret
= invalid_control
;
1159 /* Not the end of a control structure. */
1160 if (val
== else_command
)
1162 if (current_cmd
->control_type
== if_control
1163 && current_body
== 1)
1165 realloc_body_list (current_cmd
, 2);
1172 ret
= invalid_control
;
1179 child_tail
->next
= next
;
1183 body_ptr
= current_cmd
->body_list
;
1184 for (i
= 1; i
< current_body
; i
++)
1193 /* If the latest line is another control structure, then recurse
1195 if (multi_line_command_p (next
->control_type
))
1198 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1199 validator
, closure
);
1202 if (ret
!= simple_control
)
1213 restore_interp (void *arg
)
1215 interp_set_temp (interp_name ((struct interp
*)arg
));
1218 /* Read lines from the input stream and accumulate them in a chain of
1219 struct command_line's, which is then returned. For input from a
1220 terminal, the special command "end" is used to mark the end of the
1221 input, and is not included in the returned chain of commands.
1223 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1224 is always stripped) in the line and attempt to recognize GDB control
1225 commands. Otherwise, only "end" is recognized. */
1227 #define END_MESSAGE "End with a line saying just \"end\"."
1229 struct command_line
*
1230 read_command_lines (char *prompt_arg
, int from_tty
, int parse_commands
,
1231 void (*validator
)(char *, void *), void *closure
)
1233 struct command_line
*head
;
1235 if (from_tty
&& input_from_terminal_p ())
1237 if (deprecated_readline_begin_hook
)
1239 /* Note - intentional to merge messages with no newline. */
1240 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
,
1245 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1246 gdb_flush (gdb_stdout
);
1251 /* Reading commands assumes the CLI behavior, so temporarily
1252 override the current interpreter with CLI. */
1253 if (current_interp_named_p (INTERP_CONSOLE
))
1254 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1255 validator
, closure
);
1258 struct interp
*old_interp
= interp_set_temp (INTERP_CONSOLE
);
1259 struct cleanup
*old_chain
= make_cleanup (restore_interp
, old_interp
);
1261 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1262 validator
, closure
);
1263 do_cleanups (old_chain
);
1266 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1268 (*deprecated_readline_end_hook
) ();
1273 /* Act the same way as read_command_lines, except that each new line is
1274 obtained using READ_NEXT_LINE_FUNC. */
1276 struct command_line
*
1277 read_command_lines_1 (char * (*read_next_line_func
) (void), int parse_commands
,
1278 void (*validator
)(char *, void *), void *closure
)
1280 struct command_line
*head
, *tail
, *next
;
1281 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
1282 enum command_control_type ret
;
1283 enum misc_command_type val
;
1291 val
= process_next_line (read_next_line_func (), &next
, parse_commands
,
1292 validator
, closure
);
1294 /* Ignore blank lines or comments. */
1295 if (val
== nop_command
)
1298 if (val
== end_command
)
1300 ret
= simple_control
;
1304 if (val
!= ok_command
)
1306 ret
= invalid_control
;
1310 if (multi_line_command_p (next
->control_type
))
1313 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1314 validator
, closure
);
1317 if (ret
== invalid_control
)
1328 make_cleanup_free_command_lines (&head
);
1335 if (ret
!= invalid_control
)
1336 discard_cleanups (old_chain
);
1338 do_cleanups (old_chain
);
1343 /* Free a chain of struct command_line's. */
1346 free_command_lines (struct command_line
**lptr
)
1348 struct command_line
*l
= *lptr
;
1349 struct command_line
*next
;
1350 struct command_line
**blist
;
1355 if (l
->body_count
> 0)
1357 blist
= l
->body_list
;
1358 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1359 free_command_lines (blist
);
1370 do_free_command_lines_cleanup (void *arg
)
1372 free_command_lines (arg
);
1376 make_cleanup_free_command_lines (struct command_line
**arg
)
1378 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1381 struct command_line
*
1382 copy_command_lines (struct command_line
*cmds
)
1384 struct command_line
*result
= NULL
;
1388 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1390 result
->next
= copy_command_lines (cmds
->next
);
1391 result
->line
= xstrdup (cmds
->line
);
1392 result
->control_type
= cmds
->control_type
;
1393 result
->body_count
= cmds
->body_count
;
1394 if (cmds
->body_count
> 0)
1398 result
->body_list
= (struct command_line
**)
1399 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1401 for (i
= 0; i
< cmds
->body_count
; i
++)
1402 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1405 result
->body_list
= NULL
;
1411 /* Validate that *COMNAME is a valid name for a command. Return the
1412 containing command list, in case it starts with a prefix command.
1413 The prefix must already exist. *COMNAME is advanced to point after
1414 any prefix, and a NUL character overwrites the space after the
1417 static struct cmd_list_element
**
1418 validate_comname (char **comname
)
1420 struct cmd_list_element
**list
= &cmdlist
;
1421 char *p
, *last_word
;
1424 error_no_arg (_("name of command to define"));
1426 /* Find the last word of the argument. */
1427 p
= *comname
+ strlen (*comname
);
1428 while (p
> *comname
&& isspace (p
[-1]))
1430 while (p
> *comname
&& !isspace (p
[-1]))
1434 /* Find the corresponding command list. */
1435 if (last_word
!= *comname
)
1437 struct cmd_list_element
*c
;
1439 const char *tem
= *comname
;
1441 /* Separate the prefix and the command. */
1442 saved_char
= last_word
[-1];
1443 last_word
[-1] = '\0';
1445 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1446 if (c
->prefixlist
== NULL
)
1447 error (_("\"%s\" is not a prefix command."), *comname
);
1449 list
= c
->prefixlist
;
1450 last_word
[-1] = saved_char
;
1451 *comname
= last_word
;
1457 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1458 error (_("Junk in argument list: \"%s\""), p
);
1465 /* This is just a placeholder in the command data structures. */
1467 user_defined_command (char *ignore
, int from_tty
)
1472 define_command (char *comname
, int from_tty
)
1474 #define MAX_TMPBUF 128
1481 struct command_line
*cmds
;
1482 struct cmd_list_element
*c
, *newc
, *hookc
= 0, **list
;
1483 char *tem
, *comfull
;
1485 char tmpbuf
[MAX_TMPBUF
];
1486 int hook_type
= CMD_NO_HOOK
;
1487 int hook_name_size
= 0;
1489 #define HOOK_STRING "hook-"
1491 #define HOOK_POST_STRING "hookpost-"
1492 #define HOOK_POST_LEN 9
1495 list
= validate_comname (&comname
);
1497 /* Look it up, and verify that we got an exact match. */
1499 c
= lookup_cmd (&tem_c
, *list
, "", -1, 1);
1500 if (c
&& strcmp (comname
, c
->name
) != 0)
1507 if (c
->class == class_user
|| c
->class == class_alias
)
1508 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1510 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1512 error (_("Command \"%s\" not redefined."), c
->name
);
1515 /* If this new command is a hook, then mark the command which it
1516 is hooking. Note that we allow hooking `help' commands, so that
1517 we can hook the `stop' pseudo-command. */
1519 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1521 hook_type
= CMD_PRE_HOOK
;
1522 hook_name_size
= HOOK_LEN
;
1524 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1526 hook_type
= CMD_POST_HOOK
;
1527 hook_name_size
= HOOK_POST_LEN
;
1530 if (hook_type
!= CMD_NO_HOOK
)
1532 /* Look up cmd it hooks, and verify that we got an exact match. */
1533 tem_c
= comname
+ hook_name_size
;
1534 hookc
= lookup_cmd (&tem_c
, *list
, "", -1, 0);
1535 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1539 warning (_("Your new `%s' command does not "
1540 "hook any existing command."),
1542 if (!query (_("Proceed? ")))
1543 error (_("Not confirmed."));
1547 comname
= xstrdup (comname
);
1549 /* If the rest of the commands will be case insensitive, this one
1550 should behave in the same manner. */
1551 for (tem
= comname
; *tem
; tem
++)
1553 *tem
= tolower (*tem
);
1555 xsnprintf (tmpbuf
, sizeof (tmpbuf
),
1556 "Type commands for definition of \"%s\".", comfull
);
1557 cmds
= read_command_lines (tmpbuf
, from_tty
, 1, 0, 0);
1559 if (c
&& c
->class == class_user
)
1560 free_command_lines (&c
->user_commands
);
1562 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1563 (c
&& c
->class == class_user
)
1564 ? c
->doc
: xstrdup ("User-defined."), list
);
1565 newc
->user_commands
= cmds
;
1567 /* If this new command is a hook, then mark both commands as being
1574 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1575 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1578 hookc
->hook_post
= newc
; /* Target gets hooked. */
1579 newc
->hookee_post
= hookc
; /* We are marked as hooking
1583 /* Should never come here as hookc would be 0. */
1584 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1590 document_command (char *comname
, int from_tty
)
1592 struct command_line
*doclines
;
1593 struct cmd_list_element
*c
, **list
;
1599 list
= validate_comname (&comname
);
1602 c
= lookup_cmd (&tem
, *list
, "", 0, 1);
1604 if (c
->class != class_user
)
1605 error (_("Command \"%s\" is built-in."), comfull
);
1607 xsnprintf (tmpbuf
, sizeof (tmpbuf
), "Type documentation for \"%s\".",
1609 doclines
= read_command_lines (tmpbuf
, from_tty
, 0, 0, 0);
1612 xfree ((char *) c
->doc
);
1615 struct command_line
*cl1
;
1619 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1620 len
+= strlen (cl1
->line
) + 1;
1622 doc
= (char *) xmalloc (len
+ 1);
1625 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1627 strcat (doc
, cl1
->line
);
1635 free_command_lines (&doclines
);
1638 struct source_cleanup_lines_args
1641 const char *old_file
;
1645 source_cleanup_lines (void *args
)
1647 struct source_cleanup_lines_args
*p
=
1648 (struct source_cleanup_lines_args
*) args
;
1650 source_line_number
= p
->old_line
;
1651 source_file_name
= p
->old_file
;
1654 /* Used to implement source_command. */
1657 script_from_file (FILE *stream
, const char *file
)
1659 struct cleanup
*old_cleanups
;
1660 struct source_cleanup_lines_args old_lines
;
1663 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1665 old_lines
.old_line
= source_line_number
;
1666 old_lines
.old_file
= source_file_name
;
1667 old_cleanups
= make_cleanup (source_cleanup_lines
, &old_lines
);
1668 source_line_number
= 0;
1669 source_file_name
= file
;
1671 make_cleanup_restore_integer (&interpreter_async
);
1672 interpreter_async
= 0;
1675 volatile struct gdb_exception e
;
1677 TRY_CATCH (e
, RETURN_MASK_ERROR
)
1679 read_command_file (stream
);
1686 /* Re-throw the error, but with the file name information
1688 throw_error (e
.error
,
1689 _("%s:%d: Error in sourced command file:\n%s"),
1690 source_file_name
, source_line_number
, e
.message
);
1692 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1696 do_cleanups (old_cleanups
);
1699 /* Print the definition of user command C to STREAM. Or, if C is a
1700 prefix command, show the definitions of all user commands under C
1701 (recursively). PREFIX and NAME combined are the name of the
1704 show_user_1 (struct cmd_list_element
*c
, const char *prefix
, const char *name
,
1705 struct ui_file
*stream
)
1707 struct command_line
*cmdlines
;
1709 if (c
->prefixlist
!= NULL
)
1711 const char *prefixname
= c
->prefixname
;
1713 for (c
= *c
->prefixlist
; c
!= NULL
; c
= c
->next
)
1714 if (c
->class == class_user
|| c
->prefixlist
!= NULL
)
1715 show_user_1 (c
, prefixname
, c
->name
, gdb_stdout
);
1719 cmdlines
= c
->user_commands
;
1720 fprintf_filtered (stream
, "User command \"%s%s\":\n", prefix
, name
);
1724 print_command_lines (current_uiout
, cmdlines
, 1);
1725 fputs_filtered ("\n", stream
);
1730 initialize_file_ftype _initialize_cli_script
;
1733 _initialize_cli_script (void)
1735 add_com ("document", class_support
, document_command
, _("\
1736 Document a user-defined command.\n\
1737 Give command name as argument. Give documentation on following lines.\n\
1738 End with a line of just \"end\"."));
1739 add_com ("define", class_support
, define_command
, _("\
1740 Define a new command name. Command name is argument.\n\
1741 Definition appears on following lines, one command per line.\n\
1742 End with a line of just \"end\".\n\
1743 Use the \"document\" command to give documentation for the new command.\n\
1744 Commands defined in this way may have up to ten arguments."));
1746 add_com ("while", class_support
, while_command
, _("\
1747 Execute nested commands WHILE the conditional expression is non zero.\n\
1748 The conditional expression must follow the word `while' and must in turn be\n\
1749 followed by a new line. The nested commands must be entered one per line,\n\
1750 and should be terminated by the word `end'."));
1752 add_com ("if", class_support
, if_command
, _("\
1753 Execute nested commands once IF the conditional expression is non zero.\n\
1754 The conditional expression must follow the word `if' and must in turn be\n\
1755 followed by a new line. The nested commands must be entered one per line,\n\
1756 and should be terminated by the word 'else' or `end'. If an else clause\n\
1757 is used, the same rules apply to its nested commands as to the first ones."));