1 /* GDB CLI command scripting.
3 Copyright (C) 1986-2016 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"
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
:
100 /* Allocate, initialize a new command line structure for one of the
101 control commands (if/while). */
103 static struct command_line
*
104 build_command_line (enum command_control_type type
, char *args
)
106 struct command_line
*cmd
;
108 if (args
== NULL
&& (type
== if_control
|| type
== while_control
))
109 error (_("if/while commands require arguments."));
110 gdb_assert (args
!= NULL
);
112 cmd
= XNEW (struct command_line
);
114 cmd
->control_type
= type
;
117 cmd
->body_list
= XCNEWVEC (struct command_line
*, cmd
->body_count
);
118 cmd
->line
= xstrdup (args
);
123 /* Build and return a new command structure for the control commands
124 such as "if" and "while". */
126 struct command_line
*
127 get_command_line (enum command_control_type type
, char *arg
)
129 struct command_line
*cmd
;
130 struct cleanup
*old_chain
= NULL
;
132 /* Allocate and build a new command line structure. */
133 cmd
= build_command_line (type
, arg
);
135 old_chain
= make_cleanup_free_command_lines (&cmd
);
137 /* Read in the body of this command. */
138 if (recurse_read_control_structure (read_next_line
, cmd
, 0, 0)
141 warning (_("Error reading in canned sequence of commands."));
142 do_cleanups (old_chain
);
146 discard_cleanups (old_chain
);
150 /* Recursively print a command (including full control structures). */
153 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
156 struct command_line
*list
;
162 ui_out_spaces (uiout
, 2 * depth
);
164 /* A simple command, print it and continue. */
165 if (list
->control_type
== simple_control
)
167 ui_out_field_string (uiout
, NULL
, list
->line
);
168 ui_out_text (uiout
, "\n");
173 /* loop_continue to jump to the start of a while loop, print it
175 if (list
->control_type
== continue_control
)
177 ui_out_field_string (uiout
, NULL
, "loop_continue");
178 ui_out_text (uiout
, "\n");
183 /* loop_break to break out of a while loop, print it and
185 if (list
->control_type
== break_control
)
187 ui_out_field_string (uiout
, NULL
, "loop_break");
188 ui_out_text (uiout
, "\n");
193 /* A while command. Recursively print its subcommands and
195 if (list
->control_type
== while_control
196 || list
->control_type
== while_stepping_control
)
198 /* For while-stepping, the line includes the 'while-stepping'
199 token. See comment in process_next_line for explanation.
200 Here, take care not print 'while-stepping' twice. */
201 if (list
->control_type
== while_control
)
202 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
204 ui_out_field_string (uiout
, NULL
, list
->line
);
205 ui_out_text (uiout
, "\n");
206 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
208 ui_out_spaces (uiout
, 2 * depth
);
209 ui_out_field_string (uiout
, NULL
, "end");
210 ui_out_text (uiout
, "\n");
215 /* An if command. Recursively print both arms before
217 if (list
->control_type
== if_control
)
219 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
220 ui_out_text (uiout
, "\n");
222 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
224 /* Show the false arm if it exists. */
225 if (list
->body_count
== 2)
228 ui_out_spaces (uiout
, 2 * depth
);
229 ui_out_field_string (uiout
, NULL
, "else");
230 ui_out_text (uiout
, "\n");
231 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
235 ui_out_spaces (uiout
, 2 * depth
);
236 ui_out_field_string (uiout
, NULL
, "end");
237 ui_out_text (uiout
, "\n");
242 /* A commands command. Print the breakpoint commands and
244 if (list
->control_type
== commands_control
)
247 ui_out_field_fmt (uiout
, NULL
, "commands %s", list
->line
);
249 ui_out_field_string (uiout
, NULL
, "commands");
250 ui_out_text (uiout
, "\n");
251 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
253 ui_out_spaces (uiout
, 2 * depth
);
254 ui_out_field_string (uiout
, NULL
, "end");
255 ui_out_text (uiout
, "\n");
260 if (list
->control_type
== python_control
)
262 ui_out_field_string (uiout
, NULL
, "python");
263 ui_out_text (uiout
, "\n");
264 /* Don't indent python code at all. */
265 print_command_lines (uiout
, *list
->body_list
, 0);
267 ui_out_spaces (uiout
, 2 * depth
);
268 ui_out_field_string (uiout
, NULL
, "end");
269 ui_out_text (uiout
, "\n");
274 if (list
->control_type
== compile_control
)
276 ui_out_field_string (uiout
, NULL
, "compile expression");
277 ui_out_text (uiout
, "\n");
278 print_command_lines (uiout
, *list
->body_list
, 0);
280 ui_out_spaces (uiout
, 2 * depth
);
281 ui_out_field_string (uiout
, NULL
, "end");
282 ui_out_text (uiout
, "\n");
287 if (list
->control_type
== guile_control
)
289 ui_out_field_string (uiout
, NULL
, "guile");
290 ui_out_text (uiout
, "\n");
291 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
293 ui_out_spaces (uiout
, 2 * depth
);
294 ui_out_field_string (uiout
, NULL
, "end");
295 ui_out_text (uiout
, "\n");
300 /* Ignore illegal command type and try next. */
305 /* Handle pre-post hooks. */
308 clear_hook_in_cleanup (void *data
)
310 struct cmd_list_element
*c
= (struct cmd_list_element
*) data
;
312 c
->hook_in
= 0; /* Allow hook to work again once it is complete. */
316 execute_cmd_pre_hook (struct cmd_list_element
*c
)
318 if ((c
->hook_pre
) && (!c
->hook_in
))
320 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
321 c
->hook_in
= 1; /* Prevent recursive hooking. */
322 execute_user_command (c
->hook_pre
, (char *) 0);
323 do_cleanups (cleanups
);
328 execute_cmd_post_hook (struct cmd_list_element
*c
)
330 if ((c
->hook_post
) && (!c
->hook_in
))
332 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
334 c
->hook_in
= 1; /* Prevent recursive hooking. */
335 execute_user_command (c
->hook_post
, (char *) 0);
336 do_cleanups (cleanups
);
340 /* Execute the command in CMD. */
342 do_restore_user_call_depth (void * call_depth
)
344 int *depth
= (int *) call_depth
;
353 execute_user_command (struct cmd_list_element
*c
, char *args
)
355 struct ui
*ui
= current_ui
;
356 struct command_line
*cmdlines
;
357 struct cleanup
*old_chain
;
358 enum command_control_type ret
;
359 static int user_call_depth
= 0;
360 extern unsigned int max_user_call_depth
;
362 cmdlines
= c
->user_commands
;
367 old_chain
= setup_user_args (args
);
369 if (++user_call_depth
> max_user_call_depth
)
370 error (_("Max user call depth exceeded -- command aborted."));
372 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
374 /* Set the instream to 0, indicating execution of a
375 user-defined function. */
376 make_cleanup (do_restore_instream_cleanup
, ui
->instream
);
379 /* Also set the global in_user_command, so that NULL instream is
380 not confused with Insight. */
383 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
385 command_nest_depth
++;
388 ret
= execute_control_command (cmdlines
);
389 if (ret
!= simple_control
&& ret
!= break_control
)
391 warning (_("Error executing canned sequence of commands."));
394 cmdlines
= cmdlines
->next
;
396 command_nest_depth
--;
397 do_cleanups (old_chain
);
400 /* This function is called every time GDB prints a prompt. It ensures
401 that errors and the like do not confuse the command tracing. */
404 reset_command_nest_depth (void)
406 command_nest_depth
= 1;
409 suppress_next_print_command_trace
= 0;
412 /* Print the command, prefixed with '+' to represent the call depth.
413 This is slightly complicated because this function may be called
414 from execute_command and execute_control_command. Unfortunately
415 execute_command also prints the top level control commands.
416 In these cases execute_command will call execute_control_command
417 via while_command or if_command. Inner levels of 'if' and 'while'
418 are dealt with directly. Therefore we can use these functions
419 to determine whether the command has been printed already or not. */
421 print_command_trace (const char *cmd
)
425 if (suppress_next_print_command_trace
)
427 suppress_next_print_command_trace
= 0;
431 if (!source_verbose
&& !trace_commands
)
434 for (i
=0; i
< command_nest_depth
; i
++)
435 printf_filtered ("+");
437 printf_filtered ("%s\n", cmd
);
440 enum command_control_type
441 execute_control_command (struct command_line
*cmd
)
443 struct expression
*expr
;
444 struct command_line
*current
;
445 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
447 struct value
*val_mark
;
449 enum command_control_type ret
;
452 /* Start by assuming failure, if a problem is detected, the code
453 below will simply "break" out of the switch. */
454 ret
= invalid_control
;
456 switch (cmd
->control_type
)
459 /* A simple command, execute it and return. */
460 new_line
= insert_args (cmd
->line
);
463 make_cleanup (free_current_contents
, &new_line
);
464 execute_command (new_line
, 0);
465 ret
= cmd
->control_type
;
468 case continue_control
:
469 print_command_trace ("loop_continue");
471 /* Return for "continue", and "break" so we can either
472 continue the loop at the top, or break out. */
473 ret
= cmd
->control_type
;
477 print_command_trace ("loop_break");
479 /* Return for "continue", and "break" so we can either
480 continue the loop at the top, or break out. */
481 ret
= cmd
->control_type
;
486 int len
= strlen (cmd
->line
) + 7;
487 char *buffer
= (char *) alloca (len
);
489 xsnprintf (buffer
, len
, "while %s", cmd
->line
);
490 print_command_trace (buffer
);
492 /* Parse the loop control expression for the while statement. */
493 new_line
= insert_args (cmd
->line
);
496 make_cleanup (free_current_contents
, &new_line
);
497 expr
= parse_expression (new_line
);
498 make_cleanup (free_current_contents
, &expr
);
500 ret
= simple_control
;
503 /* Keep iterating so long as the expression is true. */
510 /* Evaluate the expression. */
511 val_mark
= value_mark ();
512 val
= evaluate_expression (expr
);
513 cond_result
= value_true (val
);
514 value_free_to_mark (val_mark
);
516 /* If the value is false, then break out of the loop. */
520 /* Execute the body of the while statement. */
521 current
= *cmd
->body_list
;
524 command_nest_depth
++;
525 ret
= execute_control_command (current
);
526 command_nest_depth
--;
528 /* If we got an error, or a "break" command, then stop
530 if (ret
== invalid_control
|| ret
== break_control
)
536 /* If we got a "continue" command, then restart the loop
538 if (ret
== continue_control
)
541 /* Get the next statement. */
542 current
= current
->next
;
546 /* Reset RET so that we don't recurse the break all the way down. */
547 if (ret
== break_control
)
548 ret
= simple_control
;
555 int len
= strlen (cmd
->line
) + 4;
556 char *buffer
= (char *) alloca (len
);
558 xsnprintf (buffer
, len
, "if %s", cmd
->line
);
559 print_command_trace (buffer
);
561 new_line
= insert_args (cmd
->line
);
564 make_cleanup (free_current_contents
, &new_line
);
565 /* Parse the conditional for the if statement. */
566 expr
= parse_expression (new_line
);
567 make_cleanup (free_current_contents
, &expr
);
570 ret
= simple_control
;
572 /* Evaluate the conditional. */
573 val_mark
= value_mark ();
574 val
= evaluate_expression (expr
);
576 /* Choose which arm to take commands from based on the value
577 of the conditional expression. */
578 if (value_true (val
))
579 current
= *cmd
->body_list
;
580 else if (cmd
->body_count
== 2)
581 current
= *(cmd
->body_list
+ 1);
582 value_free_to_mark (val_mark
);
584 /* Execute commands in the given arm. */
587 command_nest_depth
++;
588 ret
= execute_control_command (current
);
589 command_nest_depth
--;
591 /* If we got an error, get out. */
592 if (ret
!= simple_control
)
595 /* Get the next statement in the body. */
596 current
= current
->next
;
602 case commands_control
:
604 /* Breakpoint commands list, record the commands in the
605 breakpoint's command list and return. */
606 new_line
= insert_args (cmd
->line
);
609 make_cleanup (free_current_contents
, &new_line
);
610 ret
= commands_from_control_command (new_line
, cmd
);
614 case compile_control
:
615 eval_compile_command (cmd
, NULL
, cmd
->control_u
.compile
.scope
,
616 cmd
->control_u
.compile
.scope_data
);
617 ret
= simple_control
;
623 eval_ext_lang_from_control_command (cmd
);
624 ret
= simple_control
;
629 warning (_("Invalid control type in canned commands structure."));
633 do_cleanups (old_chain
);
638 /* Like execute_control_command, but first set
639 suppress_next_print_command_trace. */
641 enum command_control_type
642 execute_control_command_untraced (struct command_line
*cmd
)
644 suppress_next_print_command_trace
= 1;
645 return execute_control_command (cmd
);
649 /* "while" command support. Executes a body of statements while the
650 loop condition is nonzero. */
653 while_command (char *arg
, int from_tty
)
655 struct command_line
*command
= NULL
;
658 command
= get_command_line (while_control
, arg
);
663 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
665 execute_control_command_untraced (command
);
666 free_command_lines (&command
);
669 /* "if" command support. Execute either the true or false arm depending
670 on the value of the if conditional. */
673 if_command (char *arg
, int from_tty
)
675 struct command_line
*command
= NULL
;
676 struct cleanup
*old_chain
;
679 command
= get_command_line (if_control
, arg
);
684 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
686 execute_control_command_untraced (command
);
687 free_command_lines (&command
);
692 arg_cleanup (void *ignore
)
694 struct user_args
*oargs
= user_args
;
697 internal_error (__FILE__
, __LINE__
,
698 _("arg_cleanup called with no user args.\n"));
700 user_args
= user_args
->next
;
701 xfree (oargs
->command
);
705 /* Bind the incomming arguments for a user defined command to
706 $arg0, $arg1 ... $argMAXUSERARGS. */
708 static struct cleanup
*
709 setup_user_args (char *p
)
711 struct user_args
*args
;
712 struct cleanup
*old_chain
;
713 unsigned int arg_count
= 0;
715 args
= XNEW (struct user_args
);
716 memset (args
, 0, sizeof (struct user_args
));
718 args
->next
= user_args
;
721 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
726 user_args
->command
= p
= xstrdup (p
);
735 if (arg_count
>= MAXUSERARGS
)
736 error (_("user defined function may only have %d arguments."),
739 /* Strip whitespace. */
740 while (*p
== ' ' || *p
== '\t')
743 /* P now points to an argument. */
745 user_args
->a
[arg_count
].arg
= p
;
747 /* Get to the end of this argument. */
750 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
779 user_args
->a
[arg_count
].len
= p
- start_arg
;
786 /* Given character string P, return a point to the first argument
787 ($arg), or NULL if P contains no arguments. */
792 while ((p
= strchr (p
, '$')))
794 if (startswith (p
, "$arg")
795 && (isdigit (p
[4]) || p
[4] == 'c'))
802 /* Insert the user defined arguments stored in user_arg into the $arg
803 arguments found in line, with the updated copy being placed into
807 insert_args (char *line
)
809 char *p
, *save_line
, *new_line
;
812 /* If we are not in a user-defined function, treat $argc, $arg0, et
813 cetera as normal convenience variables. */
814 if (user_args
== NULL
)
815 return xstrdup (line
);
817 /* First we need to know how much memory to allocate for the new
821 while ((p
= locate_arg (line
)))
828 /* $argc. Number will be <=10. */
829 len
+= user_args
->count
== 10 ? 2 : 1;
831 else if (i
>= user_args
->count
)
833 error (_("Missing argument %d in user function."), i
);
838 len
+= user_args
->a
[i
].len
;
843 /* Don't forget the tail. */
844 len
+= strlen (line
);
846 /* Allocate space for the new line and fill it in. */
847 new_line
= (char *) xmalloc (len
+ 1);
848 if (new_line
== NULL
)
851 /* Restore pointer to beginning of old line. */
854 /* Save pointer to beginning of new line. */
855 save_line
= new_line
;
857 while ((p
= locate_arg (line
)))
861 memcpy (new_line
, line
, p
- line
);
862 new_line
+= p
- line
;
866 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
867 if (user_args
->count
== 10)
873 *(new_line
++) = user_args
->count
+ '0';
878 len
= user_args
->a
[i
].len
;
881 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
887 /* Don't forget the tail. */
888 strcpy (new_line
, line
);
890 /* Return a pointer to the beginning of the new line. */
895 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
896 code bodies. This is typically used when we encounter an "else"
897 clause for an "if" command. */
900 realloc_body_list (struct command_line
*command
, int new_length
)
903 struct command_line
**body_list
;
905 n
= command
->body_count
;
911 body_list
= XCNEWVEC (struct command_line
*, new_length
);
913 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
915 xfree (command
->body_list
);
916 command
->body_list
= body_list
;
917 command
->body_count
= new_length
;
920 /* Read next line from stdin. Passed to read_command_line_1 and
921 recurse_read_control_structure whenever we need to read commands
925 read_next_line (void)
927 struct ui
*ui
= current_ui
;
928 char *prompt_ptr
, control_prompt
[256];
930 int from_tty
= ui
->instream
== ui
->stdin_stream
;
932 if (control_level
>= 254)
933 error (_("Control nesting too deep!"));
935 /* Set a prompt based on the nesting of the control commands. */
937 || (ui
->instream
== 0 && deprecated_readline_hook
!= NULL
))
939 for (i
= 0; i
< control_level
; i
++)
940 control_prompt
[i
] = ' ';
941 control_prompt
[i
] = '>';
942 control_prompt
[i
+ 1] = '\0';
943 prompt_ptr
= (char *) &control_prompt
[0];
948 return command_line_input (prompt_ptr
, from_tty
, "commands");
951 /* Process one input line. If the command is an "end", return such an
952 indication to the caller. If PARSE_COMMANDS is true, strip leading
953 whitespace (trailing whitespace is always stripped) in the line,
954 attempt to recognize GDB control commands, and also return an
955 indication if the command is an "else" or a nop.
957 Otherwise, only "end" is recognized. */
959 static enum misc_command_type
960 process_next_line (char *p
, struct command_line
**command
, int parse_commands
,
961 void (*validator
)(char *, void *), void *closure
)
967 /* Not sure what to do here. */
971 /* Strip trailing whitespace. */
972 p_end
= p
+ strlen (p
);
973 while (p_end
> p
&& (p_end
[-1] == ' ' || p_end
[-1] == '\t'))
977 /* Strip leading whitespace. */
978 while (p_start
< p_end
&& (*p_start
== ' ' || *p_start
== '\t'))
981 /* 'end' is always recognized, regardless of parse_commands value.
982 We also permit whitespace before end and after. */
983 if (p_end
- p_start
== 3 && startswith (p_start
, "end"))
988 /* If commands are parsed, we skip initial spaces. Otherwise,
989 which is the case for Python commands and documentation
990 (see the 'document' command), spaces are preserved. */
993 /* Blanks and comments don't really do anything, but we need to
994 distinguish them from else, end and other commands which can
996 if (p_end
== p
|| p
[0] == '#')
999 /* Is the else clause of an if control structure? */
1000 if (p_end
- p
== 4 && startswith (p
, "else"))
1001 return else_command
;
1003 /* Check for while, if, break, continue, etc and build a new
1004 command line structure for them. */
1005 if ((p_end
- p
>= 14 && startswith (p
, "while-stepping"))
1006 || (p_end
- p
>= 8 && startswith (p
, "stepping"))
1007 || (p_end
- p
>= 2 && startswith (p
, "ws")))
1009 /* Because validate_actionline and encode_action lookup
1010 command's line as command, we need the line to
1011 include 'while-stepping'.
1013 For 'ws' alias, the command will have 'ws', not expanded
1014 to 'while-stepping'. This is intentional -- we don't
1015 really want frontend to send a command list with 'ws',
1016 and next break-info returning command line with
1017 'while-stepping'. This should work, but might cause the
1018 breakpoint to be marked as changed while it's actually
1020 *command
= build_command_line (while_stepping_control
, p
);
1022 else if (p_end
- p
> 5 && startswith (p
, "while"))
1027 while (first_arg
< p_end
&& isspace (*first_arg
))
1029 *command
= build_command_line (while_control
, first_arg
);
1031 else if (p_end
- p
> 2 && startswith (p
, "if"))
1036 while (first_arg
< p_end
&& isspace (*first_arg
))
1038 *command
= build_command_line (if_control
, first_arg
);
1040 else if (p_end
- p
>= 8 && startswith (p
, "commands"))
1045 while (first_arg
< p_end
&& isspace (*first_arg
))
1047 *command
= build_command_line (commands_control
, first_arg
);
1049 else if (p_end
- p
== 6 && startswith (p
, "python"))
1051 /* Note that we ignore the inline "python command" form
1053 *command
= build_command_line (python_control
, "");
1055 else if (p_end
- p
== 6 && startswith (p
, "compile"))
1057 /* Note that we ignore the inline "compile command" form
1059 *command
= build_command_line (compile_control
, "");
1060 (*command
)->control_u
.compile
.scope
= COMPILE_I_INVALID_SCOPE
;
1063 else if (p_end
- p
== 5 && startswith (p
, "guile"))
1065 /* Note that we ignore the inline "guile command" form here. */
1066 *command
= build_command_line (guile_control
, "");
1068 else if (p_end
- p
== 10 && startswith (p
, "loop_break"))
1070 *command
= XNEW (struct command_line
);
1071 (*command
)->next
= NULL
;
1072 (*command
)->line
= NULL
;
1073 (*command
)->control_type
= break_control
;
1074 (*command
)->body_count
= 0;
1075 (*command
)->body_list
= NULL
;
1077 else if (p_end
- p
== 13 && startswith (p
, "loop_continue"))
1079 *command
= XNEW (struct command_line
);
1080 (*command
)->next
= NULL
;
1081 (*command
)->line
= NULL
;
1082 (*command
)->control_type
= continue_control
;
1083 (*command
)->body_count
= 0;
1084 (*command
)->body_list
= NULL
;
1090 if (!parse_commands
|| not_handled
)
1092 /* A normal command. */
1093 *command
= XNEW (struct command_line
);
1094 (*command
)->next
= NULL
;
1095 (*command
)->line
= savestring (p
, p_end
- p
);
1096 (*command
)->control_type
= simple_control
;
1097 (*command
)->body_count
= 0;
1098 (*command
)->body_list
= NULL
;
1106 validator ((*command
)->line
, closure
);
1108 CATCH (ex
, RETURN_MASK_ALL
)
1111 throw_exception (ex
);
1116 /* Nothing special. */
1120 /* Recursively read in the control structures and create a
1121 command_line structure from them. Use read_next_line_func to
1122 obtain lines of the command. */
1124 static enum command_control_type
1125 recurse_read_control_structure (char * (*read_next_line_func
) (void),
1126 struct command_line
*current_cmd
,
1127 void (*validator
)(char *, void *),
1130 int current_body
, i
;
1131 enum misc_command_type val
;
1132 enum command_control_type ret
;
1133 struct command_line
**body_ptr
, *child_tail
, *next
;
1138 /* Sanity checks. */
1139 if (current_cmd
->control_type
== simple_control
)
1140 error (_("Recursed on a simple control type."));
1142 if (current_body
> current_cmd
->body_count
)
1143 error (_("Allocated body is smaller than this command type needs."));
1145 /* Read lines from the input stream and build control structures. */
1151 val
= process_next_line (read_next_line_func (), &next
,
1152 current_cmd
->control_type
!= python_control
1153 && current_cmd
->control_type
!= guile_control
1154 && current_cmd
->control_type
!= compile_control
,
1155 validator
, closure
);
1157 /* Just skip blanks and comments. */
1158 if (val
== nop_command
)
1161 if (val
== end_command
)
1163 if (multi_line_command_p (current_cmd
->control_type
))
1165 /* Success reading an entire canned sequence of commands. */
1166 ret
= simple_control
;
1171 ret
= invalid_control
;
1176 /* Not the end of a control structure. */
1177 if (val
== else_command
)
1179 if (current_cmd
->control_type
== if_control
1180 && current_body
== 1)
1182 realloc_body_list (current_cmd
, 2);
1189 ret
= invalid_control
;
1196 child_tail
->next
= next
;
1200 body_ptr
= current_cmd
->body_list
;
1201 for (i
= 1; i
< current_body
; i
++)
1210 /* If the latest line is another control structure, then recurse
1212 if (multi_line_command_p (next
->control_type
))
1215 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1216 validator
, closure
);
1219 if (ret
!= simple_control
)
1230 restore_interp (void *arg
)
1232 interp_set_temp (interp_name ((struct interp
*)arg
));
1235 /* Read lines from the input stream and accumulate them in a chain of
1236 struct command_line's, which is then returned. For input from a
1237 terminal, the special command "end" is used to mark the end of the
1238 input, and is not included in the returned chain of commands.
1240 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1241 is always stripped) in the line and attempt to recognize GDB control
1242 commands. Otherwise, only "end" is recognized. */
1244 #define END_MESSAGE "End with a line saying just \"end\"."
1246 struct command_line
*
1247 read_command_lines (char *prompt_arg
, int from_tty
, int parse_commands
,
1248 void (*validator
)(char *, void *), void *closure
)
1250 struct command_line
*head
;
1252 if (from_tty
&& input_interactive_p (current_ui
))
1254 if (deprecated_readline_begin_hook
)
1256 /* Note - intentional to merge messages with no newline. */
1257 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
,
1262 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1263 gdb_flush (gdb_stdout
);
1268 /* Reading commands assumes the CLI behavior, so temporarily
1269 override the current interpreter with CLI. */
1270 if (current_interp_named_p (INTERP_CONSOLE
))
1271 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1272 validator
, closure
);
1275 struct interp
*old_interp
= interp_set_temp (INTERP_CONSOLE
);
1276 struct cleanup
*old_chain
= make_cleanup (restore_interp
, old_interp
);
1278 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1279 validator
, closure
);
1280 do_cleanups (old_chain
);
1283 if (from_tty
&& input_interactive_p (current_ui
)
1284 && deprecated_readline_end_hook
)
1286 (*deprecated_readline_end_hook
) ();
1291 /* Act the same way as read_command_lines, except that each new line is
1292 obtained using READ_NEXT_LINE_FUNC. */
1294 struct command_line
*
1295 read_command_lines_1 (char * (*read_next_line_func
) (void), int parse_commands
,
1296 void (*validator
)(char *, void *), void *closure
)
1298 struct command_line
*head
, *tail
, *next
;
1299 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
1300 enum command_control_type ret
;
1301 enum misc_command_type val
;
1309 val
= process_next_line (read_next_line_func (), &next
, parse_commands
,
1310 validator
, closure
);
1312 /* Ignore blank lines or comments. */
1313 if (val
== nop_command
)
1316 if (val
== end_command
)
1318 ret
= simple_control
;
1322 if (val
!= ok_command
)
1324 ret
= invalid_control
;
1328 if (multi_line_command_p (next
->control_type
))
1331 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1332 validator
, closure
);
1335 if (ret
== invalid_control
)
1346 make_cleanup_free_command_lines (&head
);
1353 if (ret
!= invalid_control
)
1354 discard_cleanups (old_chain
);
1356 do_cleanups (old_chain
);
1361 /* Free a chain of struct command_line's. */
1364 free_command_lines (struct command_line
**lptr
)
1366 struct command_line
*l
= *lptr
;
1367 struct command_line
*next
;
1368 struct command_line
**blist
;
1373 if (l
->body_count
> 0)
1375 blist
= l
->body_list
;
1376 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1377 free_command_lines (blist
);
1388 do_free_command_lines_cleanup (void *arg
)
1390 free_command_lines ((struct command_line
**) arg
);
1394 make_cleanup_free_command_lines (struct command_line
**arg
)
1396 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1399 struct command_line
*
1400 copy_command_lines (struct command_line
*cmds
)
1402 struct command_line
*result
= NULL
;
1406 result
= XNEW (struct command_line
);
1408 result
->next
= copy_command_lines (cmds
->next
);
1409 result
->line
= xstrdup (cmds
->line
);
1410 result
->control_type
= cmds
->control_type
;
1411 result
->body_count
= cmds
->body_count
;
1412 if (cmds
->body_count
> 0)
1416 result
->body_list
= XNEWVEC (struct command_line
*, cmds
->body_count
);
1418 for (i
= 0; i
< cmds
->body_count
; i
++)
1419 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1422 result
->body_list
= NULL
;
1428 /* Validate that *COMNAME is a valid name for a command. Return the
1429 containing command list, in case it starts with a prefix command.
1430 The prefix must already exist. *COMNAME is advanced to point after
1431 any prefix, and a NUL character overwrites the space after the
1434 static struct cmd_list_element
**
1435 validate_comname (char **comname
)
1437 struct cmd_list_element
**list
= &cmdlist
;
1438 char *p
, *last_word
;
1441 error_no_arg (_("name of command to define"));
1443 /* Find the last word of the argument. */
1444 p
= *comname
+ strlen (*comname
);
1445 while (p
> *comname
&& isspace (p
[-1]))
1447 while (p
> *comname
&& !isspace (p
[-1]))
1451 /* Find the corresponding command list. */
1452 if (last_word
!= *comname
)
1454 struct cmd_list_element
*c
;
1456 const char *tem
= *comname
;
1458 /* Separate the prefix and the command. */
1459 saved_char
= last_word
[-1];
1460 last_word
[-1] = '\0';
1462 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1463 if (c
->prefixlist
== NULL
)
1464 error (_("\"%s\" is not a prefix command."), *comname
);
1466 list
= c
->prefixlist
;
1467 last_word
[-1] = saved_char
;
1468 *comname
= last_word
;
1474 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1475 error (_("Junk in argument list: \"%s\""), p
);
1482 /* This is just a placeholder in the command data structures. */
1484 user_defined_command (char *ignore
, int from_tty
)
1489 define_command (char *comname
, int from_tty
)
1491 #define MAX_TMPBUF 128
1498 struct command_line
*cmds
;
1499 struct cmd_list_element
*c
, *newc
, *hookc
= 0, **list
;
1500 char *tem
, *comfull
;
1502 char tmpbuf
[MAX_TMPBUF
];
1503 int hook_type
= CMD_NO_HOOK
;
1504 int hook_name_size
= 0;
1506 #define HOOK_STRING "hook-"
1508 #define HOOK_POST_STRING "hookpost-"
1509 #define HOOK_POST_LEN 9
1512 list
= validate_comname (&comname
);
1514 /* Look it up, and verify that we got an exact match. */
1516 c
= lookup_cmd (&tem_c
, *list
, "", -1, 1);
1517 if (c
&& strcmp (comname
, c
->name
) != 0)
1524 if (c
->theclass
== class_user
|| c
->theclass
== class_alias
)
1525 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1527 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1529 error (_("Command \"%s\" not redefined."), c
->name
);
1532 /* If this new command is a hook, then mark the command which it
1533 is hooking. Note that we allow hooking `help' commands, so that
1534 we can hook the `stop' pseudo-command. */
1536 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1538 hook_type
= CMD_PRE_HOOK
;
1539 hook_name_size
= HOOK_LEN
;
1541 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1543 hook_type
= CMD_POST_HOOK
;
1544 hook_name_size
= HOOK_POST_LEN
;
1547 if (hook_type
!= CMD_NO_HOOK
)
1549 /* Look up cmd it hooks, and verify that we got an exact match. */
1550 tem_c
= comname
+ hook_name_size
;
1551 hookc
= lookup_cmd (&tem_c
, *list
, "", -1, 0);
1552 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1556 warning (_("Your new `%s' command does not "
1557 "hook any existing command."),
1559 if (!query (_("Proceed? ")))
1560 error (_("Not confirmed."));
1564 comname
= xstrdup (comname
);
1566 /* If the rest of the commands will be case insensitive, this one
1567 should behave in the same manner. */
1568 for (tem
= comname
; *tem
; tem
++)
1570 *tem
= tolower (*tem
);
1572 xsnprintf (tmpbuf
, sizeof (tmpbuf
),
1573 "Type commands for definition of \"%s\".", comfull
);
1574 cmds
= read_command_lines (tmpbuf
, from_tty
, 1, 0, 0);
1576 if (c
&& c
->theclass
== class_user
)
1577 free_command_lines (&c
->user_commands
);
1579 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1580 (c
&& c
->theclass
== class_user
)
1581 ? c
->doc
: xstrdup ("User-defined."), list
);
1582 newc
->user_commands
= cmds
;
1584 /* If this new command is a hook, then mark both commands as being
1591 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1592 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1595 hookc
->hook_post
= newc
; /* Target gets hooked. */
1596 newc
->hookee_post
= hookc
; /* We are marked as hooking
1600 /* Should never come here as hookc would be 0. */
1601 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1607 document_command (char *comname
, int from_tty
)
1609 struct command_line
*doclines
;
1610 struct cmd_list_element
*c
, **list
;
1616 list
= validate_comname (&comname
);
1619 c
= lookup_cmd (&tem
, *list
, "", 0, 1);
1621 if (c
->theclass
!= class_user
)
1622 error (_("Command \"%s\" is built-in."), comfull
);
1624 xsnprintf (tmpbuf
, sizeof (tmpbuf
), "Type documentation for \"%s\".",
1626 doclines
= read_command_lines (tmpbuf
, from_tty
, 0, 0, 0);
1629 xfree ((char *) c
->doc
);
1632 struct command_line
*cl1
;
1636 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1637 len
+= strlen (cl1
->line
) + 1;
1639 doc
= (char *) xmalloc (len
+ 1);
1642 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1644 strcat (doc
, cl1
->line
);
1652 free_command_lines (&doclines
);
1655 struct source_cleanup_lines_args
1658 const char *old_file
;
1662 source_cleanup_lines (void *args
)
1664 struct source_cleanup_lines_args
*p
=
1665 (struct source_cleanup_lines_args
*) args
;
1667 source_line_number
= p
->old_line
;
1668 source_file_name
= p
->old_file
;
1671 /* Used to implement source_command. */
1674 script_from_file (FILE *stream
, const char *file
)
1676 struct cleanup
*old_cleanups
;
1677 struct source_cleanup_lines_args old_lines
;
1680 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1682 old_lines
.old_line
= source_line_number
;
1683 old_lines
.old_file
= source_file_name
;
1684 old_cleanups
= make_cleanup (source_cleanup_lines
, &old_lines
);
1685 source_line_number
= 0;
1686 source_file_name
= file
;
1689 scoped_restore save_async
= make_scoped_restore (¤t_ui
->async
, 0);
1693 read_command_file (stream
);
1695 CATCH (e
, RETURN_MASK_ERROR
)
1697 /* Re-throw the error, but with the file name information
1699 throw_error (e
.error
,
1700 _("%s:%d: Error in sourced command file:\n%s"),
1701 source_file_name
, source_line_number
, e
.message
);
1706 do_cleanups (old_cleanups
);
1709 /* Print the definition of user command C to STREAM. Or, if C is a
1710 prefix command, show the definitions of all user commands under C
1711 (recursively). PREFIX and NAME combined are the name of the
1714 show_user_1 (struct cmd_list_element
*c
, const char *prefix
, const char *name
,
1715 struct ui_file
*stream
)
1717 struct command_line
*cmdlines
;
1719 if (c
->prefixlist
!= NULL
)
1721 const char *prefixname
= c
->prefixname
;
1723 for (c
= *c
->prefixlist
; c
!= NULL
; c
= c
->next
)
1724 if (c
->theclass
== class_user
|| c
->prefixlist
!= NULL
)
1725 show_user_1 (c
, prefixname
, c
->name
, gdb_stdout
);
1729 cmdlines
= c
->user_commands
;
1730 fprintf_filtered (stream
, "User command \"%s%s\":\n", prefix
, name
);
1734 print_command_lines (current_uiout
, cmdlines
, 1);
1735 fputs_filtered ("\n", stream
);
1740 initialize_file_ftype _initialize_cli_script
;
1743 _initialize_cli_script (void)
1745 add_com ("document", class_support
, document_command
, _("\
1746 Document a user-defined command.\n\
1747 Give command name as argument. Give documentation on following lines.\n\
1748 End with a line of just \"end\"."));
1749 add_com ("define", class_support
, define_command
, _("\
1750 Define a new command name. Command name is argument.\n\
1751 Definition appears on following lines, one command per line.\n\
1752 End with a line of just \"end\".\n\
1753 Use the \"document\" command to give documentation for the new command.\n\
1754 Commands defined in this way may have up to ten arguments."));
1756 add_com ("while", class_support
, while_command
, _("\
1757 Execute nested commands WHILE the conditional expression is non zero.\n\
1758 The conditional expression must follow the word `while' and must in turn be\n\
1759 followed by a new line. The nested commands must be entered one per line,\n\
1760 and should be terminated by the word `end'."));
1762 add_com ("if", class_support
, if_command
, _("\
1763 Execute nested commands once IF the conditional expression is non zero.\n\
1764 The conditional expression must follow the word `if' and must in turn be\n\
1765 followed by a new line. The nested commands must be entered one per line,\n\
1766 and should be terminated by the word 'else' or `end'. If an else clause\n\
1767 is used, the same rules apply to its nested commands as to the first ones."));