1 /* GDB CLI command scripting.
3 Copyright (C) 1986-2013 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 "gdb_string.h"
27 #include "exceptions.h"
29 #include "breakpoint.h"
30 #include "cli/cli-cmds.h"
31 #include "cli/cli-decode.h"
32 #include "cli/cli-script.h"
33 #include "gdb_assert.h"
35 #include "python/python.h"
38 /* Prototypes for local functions. */
40 static enum command_control_type
41 recurse_read_control_structure (char * (*read_next_line_func
) (void),
42 struct command_line
*current_cmd
,
43 void (*validator
)(char *, void *),
46 static char *insert_args (char *line
);
48 static struct cleanup
* setup_user_args (char *p
);
50 static char *read_next_line (void);
52 /* Level of control structure when reading. */
53 static int control_level
;
55 /* Level of control structure when executing. */
56 static int command_nest_depth
= 1;
58 /* This is to prevent certain commands being printed twice. */
59 static int suppress_next_print_command_trace
= 0;
61 /* Structure for arguments to user defined functions. */
62 #define MAXUSERARGS 10
65 struct user_args
*next
;
66 /* It is necessary to store a malloced copy of the command line to
67 ensure that the arguments are not overwritten before they are
81 /* Allocate, initialize a new command line structure for one of the
82 control commands (if/while). */
84 static struct command_line
*
85 build_command_line (enum command_control_type type
, char *args
)
87 struct command_line
*cmd
;
89 if (args
== NULL
&& (type
== if_control
|| type
== while_control
))
90 error (_("if/while commands require arguments."));
91 gdb_assert (args
!= NULL
);
93 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
95 cmd
->control_type
= type
;
99 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
101 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
102 cmd
->line
= xstrdup (args
);
107 /* Build and return a new command structure for the control commands
108 such as "if" and "while". */
110 struct command_line
*
111 get_command_line (enum command_control_type type
, char *arg
)
113 struct command_line
*cmd
;
114 struct cleanup
*old_chain
= NULL
;
116 /* Allocate and build a new command line structure. */
117 cmd
= build_command_line (type
, arg
);
119 old_chain
= make_cleanup_free_command_lines (&cmd
);
121 /* Read in the body of this command. */
122 if (recurse_read_control_structure (read_next_line
, cmd
, 0, 0)
125 warning (_("Error reading in canned sequence of commands."));
126 do_cleanups (old_chain
);
130 discard_cleanups (old_chain
);
134 /* Recursively print a command (including full control structures). */
137 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
140 struct command_line
*list
;
146 ui_out_spaces (uiout
, 2 * depth
);
148 /* A simple command, print it and continue. */
149 if (list
->control_type
== simple_control
)
151 ui_out_field_string (uiout
, NULL
, list
->line
);
152 ui_out_text (uiout
, "\n");
157 /* loop_continue to jump to the start of a while loop, print it
159 if (list
->control_type
== continue_control
)
161 ui_out_field_string (uiout
, NULL
, "loop_continue");
162 ui_out_text (uiout
, "\n");
167 /* loop_break to break out of a while loop, print it and
169 if (list
->control_type
== break_control
)
171 ui_out_field_string (uiout
, NULL
, "loop_break");
172 ui_out_text (uiout
, "\n");
177 /* A while command. Recursively print its subcommands and
179 if (list
->control_type
== while_control
180 || list
->control_type
== while_stepping_control
)
182 /* For while-stepping, the line includes the 'while-stepping'
183 token. See comment in process_next_line for explanation.
184 Here, take care not print 'while-stepping' twice. */
185 if (list
->control_type
== while_control
)
186 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
188 ui_out_field_string (uiout
, NULL
, list
->line
);
189 ui_out_text (uiout
, "\n");
190 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
192 ui_out_spaces (uiout
, 2 * depth
);
193 ui_out_field_string (uiout
, NULL
, "end");
194 ui_out_text (uiout
, "\n");
199 /* An if command. Recursively print both arms before
201 if (list
->control_type
== if_control
)
203 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
204 ui_out_text (uiout
, "\n");
206 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
208 /* Show the false arm if it exists. */
209 if (list
->body_count
== 2)
212 ui_out_spaces (uiout
, 2 * depth
);
213 ui_out_field_string (uiout
, NULL
, "else");
214 ui_out_text (uiout
, "\n");
215 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
219 ui_out_spaces (uiout
, 2 * depth
);
220 ui_out_field_string (uiout
, NULL
, "end");
221 ui_out_text (uiout
, "\n");
226 /* A commands command. Print the breakpoint commands and
228 if (list
->control_type
== commands_control
)
231 ui_out_field_fmt (uiout
, NULL
, "commands %s", list
->line
);
233 ui_out_field_string (uiout
, NULL
, "commands");
234 ui_out_text (uiout
, "\n");
235 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
237 ui_out_spaces (uiout
, 2 * depth
);
238 ui_out_field_string (uiout
, NULL
, "end");
239 ui_out_text (uiout
, "\n");
244 if (list
->control_type
== python_control
)
246 ui_out_field_string (uiout
, NULL
, "python");
247 ui_out_text (uiout
, "\n");
248 /* Don't indent python code at all. */
249 print_command_lines (uiout
, *list
->body_list
, 0);
251 ui_out_spaces (uiout
, 2 * depth
);
252 ui_out_field_string (uiout
, NULL
, "end");
253 ui_out_text (uiout
, "\n");
258 /* Ignore illegal command type and try next. */
263 /* Handle pre-post hooks. */
266 clear_hook_in_cleanup (void *data
)
268 struct cmd_list_element
*c
= data
;
270 c
->hook_in
= 0; /* Allow hook to work again once it is complete. */
274 execute_cmd_pre_hook (struct cmd_list_element
*c
)
276 if ((c
->hook_pre
) && (!c
->hook_in
))
278 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
279 c
->hook_in
= 1; /* Prevent recursive hooking. */
280 execute_user_command (c
->hook_pre
, (char *) 0);
281 do_cleanups (cleanups
);
286 execute_cmd_post_hook (struct cmd_list_element
*c
)
288 if ((c
->hook_post
) && (!c
->hook_in
))
290 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
292 c
->hook_in
= 1; /* Prevent recursive hooking. */
293 execute_user_command (c
->hook_post
, (char *) 0);
294 do_cleanups (cleanups
);
298 /* Execute the command in CMD. */
300 do_restore_user_call_depth (void * call_depth
)
302 int *depth
= call_depth
;
311 execute_user_command (struct cmd_list_element
*c
, char *args
)
313 struct command_line
*cmdlines
;
314 struct cleanup
*old_chain
;
315 enum command_control_type ret
;
316 static int user_call_depth
= 0;
317 extern unsigned int max_user_call_depth
;
319 cmdlines
= c
->user_commands
;
324 old_chain
= setup_user_args (args
);
326 if (++user_call_depth
> max_user_call_depth
)
327 error (_("Max user call depth exceeded -- command aborted."));
329 make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
331 /* Set the instream to 0, indicating execution of a
332 user-defined function. */
333 make_cleanup (do_restore_instream_cleanup
, instream
);
334 instream
= (FILE *) 0;
336 /* Also set the global in_user_command, so that NULL instream is
337 not confused with Insight. */
340 make_cleanup_restore_integer (&interpreter_async
);
341 interpreter_async
= 0;
343 command_nest_depth
++;
346 ret
= execute_control_command (cmdlines
);
347 if (ret
!= simple_control
&& ret
!= break_control
)
349 warning (_("Error executing canned sequence of commands."));
352 cmdlines
= cmdlines
->next
;
354 command_nest_depth
--;
355 do_cleanups (old_chain
);
358 /* This function is called every time GDB prints a prompt. It ensures
359 that errors and the like do not confuse the command tracing. */
362 reset_command_nest_depth (void)
364 command_nest_depth
= 1;
367 suppress_next_print_command_trace
= 0;
370 /* Print the command, prefixed with '+' to represent the call depth.
371 This is slightly complicated because this function may be called
372 from execute_command and execute_control_command. Unfortunately
373 execute_command also prints the top level control commands.
374 In these cases execute_command will call execute_control_command
375 via while_command or if_command. Inner levels of 'if' and 'while'
376 are dealt with directly. Therefore we can use these functions
377 to determine whether the command has been printed already or not. */
379 print_command_trace (const char *cmd
)
383 if (suppress_next_print_command_trace
)
385 suppress_next_print_command_trace
= 0;
389 if (!source_verbose
&& !trace_commands
)
392 for (i
=0; i
< command_nest_depth
; i
++)
393 printf_filtered ("+");
395 printf_filtered ("%s\n", cmd
);
398 enum command_control_type
399 execute_control_command (struct command_line
*cmd
)
401 struct expression
*expr
;
402 struct command_line
*current
;
403 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
405 struct value
*val_mark
;
407 enum command_control_type ret
;
410 /* Start by assuming failure, if a problem is detected, the code
411 below will simply "break" out of the switch. */
412 ret
= invalid_control
;
414 switch (cmd
->control_type
)
417 /* A simple command, execute it and return. */
418 new_line
= insert_args (cmd
->line
);
421 make_cleanup (free_current_contents
, &new_line
);
422 execute_command (new_line
, 0);
423 ret
= cmd
->control_type
;
426 case continue_control
:
427 print_command_trace ("loop_continue");
429 /* Return for "continue", and "break" so we can either
430 continue the loop at the top, or break out. */
431 ret
= cmd
->control_type
;
435 print_command_trace ("loop_break");
437 /* Return for "continue", and "break" so we can either
438 continue the loop at the top, or break out. */
439 ret
= cmd
->control_type
;
444 int len
= strlen (cmd
->line
) + 7;
445 char *buffer
= alloca (len
);
447 xsnprintf (buffer
, len
, "while %s", cmd
->line
);
448 print_command_trace (buffer
);
450 /* Parse the loop control expression for the while statement. */
451 new_line
= insert_args (cmd
->line
);
454 make_cleanup (free_current_contents
, &new_line
);
455 expr
= parse_expression (new_line
);
456 make_cleanup (free_current_contents
, &expr
);
458 ret
= simple_control
;
461 /* Keep iterating so long as the expression is true. */
468 /* Evaluate the expression. */
469 val_mark
= value_mark ();
470 val
= evaluate_expression (expr
);
471 cond_result
= value_true (val
);
472 value_free_to_mark (val_mark
);
474 /* If the value is false, then break out of the loop. */
478 /* Execute the body of the while statement. */
479 current
= *cmd
->body_list
;
482 command_nest_depth
++;
483 ret
= execute_control_command (current
);
484 command_nest_depth
--;
486 /* If we got an error, or a "break" command, then stop
488 if (ret
== invalid_control
|| ret
== break_control
)
494 /* If we got a "continue" command, then restart the loop
496 if (ret
== continue_control
)
499 /* Get the next statement. */
500 current
= current
->next
;
504 /* Reset RET so that we don't recurse the break all the way down. */
505 if (ret
== break_control
)
506 ret
= simple_control
;
513 int len
= strlen (cmd
->line
) + 4;
514 char *buffer
= alloca (len
);
516 xsnprintf (buffer
, len
, "if %s", cmd
->line
);
517 print_command_trace (buffer
);
519 new_line
= insert_args (cmd
->line
);
522 make_cleanup (free_current_contents
, &new_line
);
523 /* Parse the conditional for the if statement. */
524 expr
= parse_expression (new_line
);
525 make_cleanup (free_current_contents
, &expr
);
528 ret
= simple_control
;
530 /* Evaluate the conditional. */
531 val_mark
= value_mark ();
532 val
= evaluate_expression (expr
);
534 /* Choose which arm to take commands from based on the value
535 of the conditional expression. */
536 if (value_true (val
))
537 current
= *cmd
->body_list
;
538 else if (cmd
->body_count
== 2)
539 current
= *(cmd
->body_list
+ 1);
540 value_free_to_mark (val_mark
);
542 /* Execute commands in the given arm. */
545 command_nest_depth
++;
546 ret
= execute_control_command (current
);
547 command_nest_depth
--;
549 /* If we got an error, get out. */
550 if (ret
!= simple_control
)
553 /* Get the next statement in the body. */
554 current
= current
->next
;
559 case commands_control
:
561 /* Breakpoint commands list, record the commands in the
562 breakpoint's command list and return. */
563 new_line
= insert_args (cmd
->line
);
566 make_cleanup (free_current_contents
, &new_line
);
567 ret
= commands_from_control_command (new_line
, cmd
);
572 eval_python_from_control_command (cmd
);
573 ret
= simple_control
;
578 warning (_("Invalid control type in canned commands structure."));
582 do_cleanups (old_chain
);
587 /* Like execute_control_command, but first set
588 suppress_next_print_command_trace. */
590 enum command_control_type
591 execute_control_command_untraced (struct command_line
*cmd
)
593 suppress_next_print_command_trace
= 1;
594 return execute_control_command (cmd
);
598 /* "while" command support. Executes a body of statements while the
599 loop condition is nonzero. */
602 while_command (char *arg
, int from_tty
)
604 struct command_line
*command
= NULL
;
605 struct cleanup
*old_chain
;
608 command
= get_command_line (while_control
, arg
);
613 old_chain
= make_cleanup_restore_integer (&interpreter_async
);
614 interpreter_async
= 0;
616 execute_control_command_untraced (command
);
617 free_command_lines (&command
);
619 do_cleanups (old_chain
);
622 /* "if" command support. Execute either the true or false arm depending
623 on the value of the if conditional. */
626 if_command (char *arg
, int from_tty
)
628 struct command_line
*command
= NULL
;
629 struct cleanup
*old_chain
;
632 command
= get_command_line (if_control
, arg
);
637 old_chain
= make_cleanup_restore_integer (&interpreter_async
);
638 interpreter_async
= 0;
640 execute_control_command_untraced (command
);
641 free_command_lines (&command
);
643 do_cleanups (old_chain
);
648 arg_cleanup (void *ignore
)
650 struct user_args
*oargs
= user_args
;
653 internal_error (__FILE__
, __LINE__
,
654 _("arg_cleanup called with no user args.\n"));
656 user_args
= user_args
->next
;
657 xfree (oargs
->command
);
661 /* Bind the incomming arguments for a user defined command to
662 $arg0, $arg1 ... $argMAXUSERARGS. */
664 static struct cleanup
*
665 setup_user_args (char *p
)
667 struct user_args
*args
;
668 struct cleanup
*old_chain
;
669 unsigned int arg_count
= 0;
671 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
672 memset (args
, 0, sizeof (struct user_args
));
674 args
->next
= user_args
;
677 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
682 user_args
->command
= p
= xstrdup (p
);
691 if (arg_count
>= MAXUSERARGS
)
692 error (_("user defined function may only have %d arguments."),
695 /* Strip whitespace. */
696 while (*p
== ' ' || *p
== '\t')
699 /* P now points to an argument. */
701 user_args
->a
[arg_count
].arg
= p
;
703 /* Get to the end of this argument. */
706 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
735 user_args
->a
[arg_count
].len
= p
- start_arg
;
742 /* Given character string P, return a point to the first argument
743 ($arg), or NULL if P contains no arguments. */
748 while ((p
= strchr (p
, '$')))
750 if (strncmp (p
, "$arg", 4) == 0
751 && (isdigit (p
[4]) || p
[4] == 'c'))
758 /* Insert the user defined arguments stored in user_arg into the $arg
759 arguments found in line, with the updated copy being placed into
763 insert_args (char *line
)
765 char *p
, *save_line
, *new_line
;
768 /* If we are not in a user-defined function, treat $argc, $arg0, et
769 cetera as normal convenience variables. */
770 if (user_args
== NULL
)
771 return xstrdup (line
);
773 /* First we need to know how much memory to allocate for the new
777 while ((p
= locate_arg (line
)))
784 /* $argc. Number will be <=10. */
785 len
+= user_args
->count
== 10 ? 2 : 1;
787 else if (i
>= user_args
->count
)
789 error (_("Missing argument %d in user function."), i
);
794 len
+= user_args
->a
[i
].len
;
799 /* Don't forget the tail. */
800 len
+= strlen (line
);
802 /* Allocate space for the new line and fill it in. */
803 new_line
= (char *) xmalloc (len
+ 1);
804 if (new_line
== NULL
)
807 /* Restore pointer to beginning of old line. */
810 /* Save pointer to beginning of new line. */
811 save_line
= new_line
;
813 while ((p
= locate_arg (line
)))
817 memcpy (new_line
, line
, p
- line
);
818 new_line
+= p
- line
;
822 gdb_assert (user_args
->count
>= 0 && user_args
->count
<= 10);
823 if (user_args
->count
== 10)
829 *(new_line
++) = user_args
->count
+ '0';
834 len
= user_args
->a
[i
].len
;
837 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
843 /* Don't forget the tail. */
844 strcpy (new_line
, line
);
846 /* Return a pointer to the beginning of the new line. */
851 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
852 code bodies. This is typically used when we encounter an "else"
853 clause for an "if" command. */
856 realloc_body_list (struct command_line
*command
, int new_length
)
859 struct command_line
**body_list
;
861 n
= command
->body_count
;
867 body_list
= (struct command_line
**)
868 xmalloc (sizeof (struct command_line
*) * new_length
);
870 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
871 memset (body_list
+ n
, 0, sizeof (struct command_line
*) * (new_length
- n
));
873 xfree (command
->body_list
);
874 command
->body_list
= body_list
;
875 command
->body_count
= new_length
;
878 /* Read next line from stdout. Passed to read_command_line_1 and
879 recurse_read_control_structure whenever we need to read commands
883 read_next_line (void)
885 char *prompt_ptr
, control_prompt
[256];
888 if (control_level
>= 254)
889 error (_("Control nesting too deep!"));
891 /* Set a prompt based on the nesting of the control commands. */
892 if (instream
== stdin
|| (instream
== 0 && deprecated_readline_hook
!= NULL
))
894 for (i
= 0; i
< control_level
; i
++)
895 control_prompt
[i
] = ' ';
896 control_prompt
[i
] = '>';
897 control_prompt
[i
+ 1] = '\0';
898 prompt_ptr
= (char *) &control_prompt
[0];
903 return command_line_input (prompt_ptr
, instream
== stdin
, "commands");
906 /* Process one input line. If the command is an "end", return such an
907 indication to the caller. If PARSE_COMMANDS is true, strip leading
908 whitespace (trailing whitespace is always stripped) in the line,
909 attempt to recognize GDB control commands, and also return an
910 indication if the command is an "else" or a nop.
912 Otherwise, only "end" is recognized. */
914 static enum misc_command_type
915 process_next_line (char *p
, struct command_line
**command
, int parse_commands
,
916 void (*validator
)(char *, void *), void *closure
)
922 /* Not sure what to do here. */
926 /* Strip trailing whitespace. */
927 p_end
= p
+ strlen (p
);
928 while (p_end
> p
&& (p_end
[-1] == ' ' || p_end
[-1] == '\t'))
932 /* Strip leading whitespace. */
933 while (p_start
< p_end
&& (*p_start
== ' ' || *p_start
== '\t'))
936 /* 'end' is always recognized, regardless of parse_commands value.
937 We also permit whitespace before end and after. */
938 if (p_end
- p_start
== 3 && !strncmp (p_start
, "end", 3))
943 /* If commands are parsed, we skip initial spaces. Otherwise,
944 which is the case for Python commands and documentation
945 (see the 'document' command), spaces are preserved. */
948 /* Blanks and comments don't really do anything, but we need to
949 distinguish them from else, end and other commands which can
951 if (p_end
== p
|| p
[0] == '#')
954 /* Is the else clause of an if control structure? */
955 if (p_end
- p
== 4 && !strncmp (p
, "else", 4))
958 /* Check for while, if, break, continue, etc and build a new
959 command line structure for them. */
960 if ((p_end
- p
>= 14 && !strncmp (p
, "while-stepping", 14))
961 || (p_end
- p
>= 8 && !strncmp (p
, "stepping", 8))
962 || (p_end
- p
>= 2 && !strncmp (p
, "ws", 2)))
964 /* Because validate_actionline and encode_action lookup
965 command's line as command, we need the line to
966 include 'while-stepping'.
968 For 'ws' alias, the command will have 'ws', not expanded
969 to 'while-stepping'. This is intentional -- we don't
970 really want frontend to send a command list with 'ws',
971 and next break-info returning command line with
972 'while-stepping'. This should work, but might cause the
973 breakpoint to be marked as changed while it's actually
975 *command
= build_command_line (while_stepping_control
, p
);
977 else if (p_end
- p
> 5 && !strncmp (p
, "while", 5))
982 while (first_arg
< p_end
&& isspace (*first_arg
))
984 *command
= build_command_line (while_control
, first_arg
);
986 else if (p_end
- p
> 2 && !strncmp (p
, "if", 2))
991 while (first_arg
< p_end
&& isspace (*first_arg
))
993 *command
= build_command_line (if_control
, first_arg
);
995 else if (p_end
- p
>= 8 && !strncmp (p
, "commands", 8))
1000 while (first_arg
< p_end
&& isspace (*first_arg
))
1002 *command
= build_command_line (commands_control
, first_arg
);
1004 else if (p_end
- p
== 6 && !strncmp (p
, "python", 6))
1006 /* Note that we ignore the inline "python command" form
1008 *command
= build_command_line (python_control
, "");
1010 else if (p_end
- p
== 10 && !strncmp (p
, "loop_break", 10))
1012 *command
= (struct command_line
*)
1013 xmalloc (sizeof (struct command_line
));
1014 (*command
)->next
= NULL
;
1015 (*command
)->line
= NULL
;
1016 (*command
)->control_type
= break_control
;
1017 (*command
)->body_count
= 0;
1018 (*command
)->body_list
= NULL
;
1020 else if (p_end
- p
== 13 && !strncmp (p
, "loop_continue", 13))
1022 *command
= (struct command_line
*)
1023 xmalloc (sizeof (struct command_line
));
1024 (*command
)->next
= NULL
;
1025 (*command
)->line
= NULL
;
1026 (*command
)->control_type
= continue_control
;
1027 (*command
)->body_count
= 0;
1028 (*command
)->body_list
= NULL
;
1034 if (!parse_commands
|| not_handled
)
1036 /* A normal command. */
1037 *command
= (struct command_line
*)
1038 xmalloc (sizeof (struct command_line
));
1039 (*command
)->next
= NULL
;
1040 (*command
)->line
= savestring (p
, p_end
- p
);
1041 (*command
)->control_type
= simple_control
;
1042 (*command
)->body_count
= 0;
1043 (*command
)->body_list
= NULL
;
1048 volatile struct gdb_exception ex
;
1050 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1052 validator ((*command
)->line
, closure
);
1057 throw_exception (ex
);
1061 /* Nothing special. */
1065 /* Recursively read in the control structures and create a
1066 command_line structure from them. Use read_next_line_func to
1067 obtain lines of the command. */
1069 static enum command_control_type
1070 recurse_read_control_structure (char * (*read_next_line_func
) (void),
1071 struct command_line
*current_cmd
,
1072 void (*validator
)(char *, void *),
1075 int current_body
, i
;
1076 enum misc_command_type val
;
1077 enum command_control_type ret
;
1078 struct command_line
**body_ptr
, *child_tail
, *next
;
1083 /* Sanity checks. */
1084 if (current_cmd
->control_type
== simple_control
)
1085 error (_("Recursed on a simple control type."));
1087 if (current_body
> current_cmd
->body_count
)
1088 error (_("Allocated body is smaller than this command type needs."));
1090 /* Read lines from the input stream and build control structures. */
1096 val
= process_next_line (read_next_line_func (), &next
,
1097 current_cmd
->control_type
!= python_control
,
1098 validator
, closure
);
1100 /* Just skip blanks and comments. */
1101 if (val
== nop_command
)
1104 if (val
== end_command
)
1106 if (current_cmd
->control_type
== while_control
1107 || current_cmd
->control_type
== while_stepping_control
1108 || current_cmd
->control_type
== if_control
1109 || current_cmd
->control_type
== python_control
1110 || current_cmd
->control_type
== commands_control
)
1112 /* Success reading an entire canned sequence of commands. */
1113 ret
= simple_control
;
1118 ret
= invalid_control
;
1123 /* Not the end of a control structure. */
1124 if (val
== else_command
)
1126 if (current_cmd
->control_type
== if_control
1127 && current_body
== 1)
1129 realloc_body_list (current_cmd
, 2);
1136 ret
= invalid_control
;
1143 child_tail
->next
= next
;
1147 body_ptr
= current_cmd
->body_list
;
1148 for (i
= 1; i
< current_body
; i
++)
1157 /* If the latest line is another control structure, then recurse
1159 if (next
->control_type
== while_control
1160 || next
->control_type
== while_stepping_control
1161 || next
->control_type
== if_control
1162 || next
->control_type
== python_control
1163 || next
->control_type
== commands_control
)
1166 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1167 validator
, closure
);
1170 if (ret
!= simple_control
)
1181 restore_interp (void *arg
)
1183 interp_set_temp (interp_name ((struct interp
*)arg
));
1186 /* Read lines from the input stream and accumulate them in a chain of
1187 struct command_line's, which is then returned. For input from a
1188 terminal, the special command "end" is used to mark the end of the
1189 input, and is not included in the returned chain of commands.
1191 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1192 is always stripped) in the line and attempt to recognize GDB control
1193 commands. Otherwise, only "end" is recognized. */
1195 #define END_MESSAGE "End with a line saying just \"end\"."
1197 struct command_line
*
1198 read_command_lines (char *prompt_arg
, int from_tty
, int parse_commands
,
1199 void (*validator
)(char *, void *), void *closure
)
1201 struct command_line
*head
;
1203 if (from_tty
&& input_from_terminal_p ())
1205 if (deprecated_readline_begin_hook
)
1207 /* Note - intentional to merge messages with no newline. */
1208 (*deprecated_readline_begin_hook
) ("%s %s\n", prompt_arg
,
1213 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
1214 gdb_flush (gdb_stdout
);
1219 /* Reading commands assumes the CLI behavior, so temporarily
1220 override the current interpreter with CLI. */
1221 if (current_interp_named_p (INTERP_CONSOLE
))
1222 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1223 validator
, closure
);
1226 struct interp
*old_interp
= interp_set_temp (INTERP_CONSOLE
);
1227 struct cleanup
*old_chain
= make_cleanup (restore_interp
, old_interp
);
1229 head
= read_command_lines_1 (read_next_line
, parse_commands
,
1230 validator
, closure
);
1231 do_cleanups (old_chain
);
1234 if (deprecated_readline_end_hook
&& from_tty
&& input_from_terminal_p ())
1236 (*deprecated_readline_end_hook
) ();
1241 /* Act the same way as read_command_lines, except that each new line is
1242 obtained using READ_NEXT_LINE_FUNC. */
1244 struct command_line
*
1245 read_command_lines_1 (char * (*read_next_line_func
) (void), int parse_commands
,
1246 void (*validator
)(char *, void *), void *closure
)
1248 struct command_line
*head
, *tail
, *next
;
1249 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
1250 enum command_control_type ret
;
1251 enum misc_command_type val
;
1259 val
= process_next_line (read_next_line_func (), &next
, parse_commands
,
1260 validator
, closure
);
1262 /* Ignore blank lines or comments. */
1263 if (val
== nop_command
)
1266 if (val
== end_command
)
1268 ret
= simple_control
;
1272 if (val
!= ok_command
)
1274 ret
= invalid_control
;
1278 if (next
->control_type
== while_control
1279 || next
->control_type
== if_control
1280 || next
->control_type
== python_control
1281 || next
->control_type
== commands_control
1282 || next
->control_type
== while_stepping_control
)
1285 ret
= recurse_read_control_structure (read_next_line_func
, next
,
1286 validator
, closure
);
1289 if (ret
== invalid_control
)
1300 make_cleanup_free_command_lines (&head
);
1307 if (ret
!= invalid_control
)
1308 discard_cleanups (old_chain
);
1310 do_cleanups (old_chain
);
1315 /* Free a chain of struct command_line's. */
1318 free_command_lines (struct command_line
**lptr
)
1320 struct command_line
*l
= *lptr
;
1321 struct command_line
*next
;
1322 struct command_line
**blist
;
1327 if (l
->body_count
> 0)
1329 blist
= l
->body_list
;
1330 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1331 free_command_lines (blist
);
1342 do_free_command_lines_cleanup (void *arg
)
1344 free_command_lines (arg
);
1348 make_cleanup_free_command_lines (struct command_line
**arg
)
1350 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1353 struct command_line
*
1354 copy_command_lines (struct command_line
*cmds
)
1356 struct command_line
*result
= NULL
;
1360 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1362 result
->next
= copy_command_lines (cmds
->next
);
1363 result
->line
= xstrdup (cmds
->line
);
1364 result
->control_type
= cmds
->control_type
;
1365 result
->body_count
= cmds
->body_count
;
1366 if (cmds
->body_count
> 0)
1370 result
->body_list
= (struct command_line
**)
1371 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1373 for (i
= 0; i
< cmds
->body_count
; i
++)
1374 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1377 result
->body_list
= NULL
;
1383 /* Validate that *COMNAME is a valid name for a command. Return the
1384 containing command list, in case it starts with a prefix command.
1385 The prefix must already exist. *COMNAME is advanced to point after
1386 any prefix, and a NUL character overwrites the space after the
1389 static struct cmd_list_element
**
1390 validate_comname (char **comname
)
1392 struct cmd_list_element
**list
= &cmdlist
;
1393 char *p
, *last_word
;
1396 error_no_arg (_("name of command to define"));
1398 /* Find the last word of the argument. */
1399 p
= *comname
+ strlen (*comname
);
1400 while (p
> *comname
&& isspace (p
[-1]))
1402 while (p
> *comname
&& !isspace (p
[-1]))
1406 /* Find the corresponding command list. */
1407 if (last_word
!= *comname
)
1409 struct cmd_list_element
*c
;
1411 const char *tem
= *comname
;
1413 /* Separate the prefix and the command. */
1414 saved_char
= last_word
[-1];
1415 last_word
[-1] = '\0';
1417 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1418 if (c
->prefixlist
== NULL
)
1419 error (_("\"%s\" is not a prefix command."), *comname
);
1421 list
= c
->prefixlist
;
1422 last_word
[-1] = saved_char
;
1423 *comname
= last_word
;
1429 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1430 error (_("Junk in argument list: \"%s\""), p
);
1437 /* This is just a placeholder in the command data structures. */
1439 user_defined_command (char *ignore
, int from_tty
)
1444 define_command (char *comname
, int from_tty
)
1446 #define MAX_TMPBUF 128
1453 struct command_line
*cmds
;
1454 struct cmd_list_element
*c
, *newc
, *hookc
= 0, **list
;
1455 char *tem
, *comfull
;
1457 char tmpbuf
[MAX_TMPBUF
];
1458 int hook_type
= CMD_NO_HOOK
;
1459 int hook_name_size
= 0;
1461 #define HOOK_STRING "hook-"
1463 #define HOOK_POST_STRING "hookpost-"
1464 #define HOOK_POST_LEN 9
1467 list
= validate_comname (&comname
);
1469 /* Look it up, and verify that we got an exact match. */
1471 c
= lookup_cmd (&tem_c
, *list
, "", -1, 1);
1472 if (c
&& strcmp (comname
, c
->name
) != 0)
1479 if (c
->class == class_user
|| c
->class == class_alias
)
1480 q
= query (_("Redefine command \"%s\"? "), c
->name
);
1482 q
= query (_("Really redefine built-in command \"%s\"? "), c
->name
);
1484 error (_("Command \"%s\" not redefined."), c
->name
);
1487 /* If this new command is a hook, then mark the command which it
1488 is hooking. Note that we allow hooking `help' commands, so that
1489 we can hook the `stop' pseudo-command. */
1491 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1493 hook_type
= CMD_PRE_HOOK
;
1494 hook_name_size
= HOOK_LEN
;
1496 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1498 hook_type
= CMD_POST_HOOK
;
1499 hook_name_size
= HOOK_POST_LEN
;
1502 if (hook_type
!= CMD_NO_HOOK
)
1504 /* Look up cmd it hooks, and verify that we got an exact match. */
1505 tem_c
= comname
+ hook_name_size
;
1506 hookc
= lookup_cmd (&tem_c
, *list
, "", -1, 0);
1507 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1511 warning (_("Your new `%s' command does not "
1512 "hook any existing command."),
1514 if (!query (_("Proceed? ")))
1515 error (_("Not confirmed."));
1519 comname
= xstrdup (comname
);
1521 /* If the rest of the commands will be case insensitive, this one
1522 should behave in the same manner. */
1523 for (tem
= comname
; *tem
; tem
++)
1525 *tem
= tolower (*tem
);
1527 xsnprintf (tmpbuf
, sizeof (tmpbuf
),
1528 "Type commands for definition of \"%s\".", comfull
);
1529 cmds
= read_command_lines (tmpbuf
, from_tty
, 1, 0, 0);
1531 if (c
&& c
->class == class_user
)
1532 free_command_lines (&c
->user_commands
);
1534 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1535 (c
&& c
->class == class_user
)
1536 ? c
->doc
: xstrdup ("User-defined."), list
);
1537 newc
->user_commands
= cmds
;
1539 /* If this new command is a hook, then mark both commands as being
1546 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1547 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1550 hookc
->hook_post
= newc
; /* Target gets hooked. */
1551 newc
->hookee_post
= hookc
; /* We are marked as hooking
1555 /* Should never come here as hookc would be 0. */
1556 internal_error (__FILE__
, __LINE__
, _("bad switch"));
1562 document_command (char *comname
, int from_tty
)
1564 struct command_line
*doclines
;
1565 struct cmd_list_element
*c
, **list
;
1571 list
= validate_comname (&comname
);
1574 c
= lookup_cmd (&tem
, *list
, "", 0, 1);
1576 if (c
->class != class_user
)
1577 error (_("Command \"%s\" is built-in."), comfull
);
1579 xsnprintf (tmpbuf
, sizeof (tmpbuf
), "Type documentation for \"%s\".",
1581 doclines
= read_command_lines (tmpbuf
, from_tty
, 0, 0, 0);
1587 struct command_line
*cl1
;
1590 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1591 len
+= strlen (cl1
->line
) + 1;
1593 c
->doc
= (char *) xmalloc (len
+ 1);
1596 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1598 strcat (c
->doc
, cl1
->line
);
1600 strcat (c
->doc
, "\n");
1604 free_command_lines (&doclines
);
1607 struct source_cleanup_lines_args
1610 const char *old_file
;
1614 source_cleanup_lines (void *args
)
1616 struct source_cleanup_lines_args
*p
=
1617 (struct source_cleanup_lines_args
*) args
;
1619 source_line_number
= p
->old_line
;
1620 source_file_name
= p
->old_file
;
1623 /* Used to implement source_command. */
1626 script_from_file (FILE *stream
, const char *file
)
1628 struct cleanup
*old_cleanups
;
1629 struct source_cleanup_lines_args old_lines
;
1632 internal_error (__FILE__
, __LINE__
, _("called with NULL file pointer!"));
1634 old_lines
.old_line
= source_line_number
;
1635 old_lines
.old_file
= source_file_name
;
1636 old_cleanups
= make_cleanup (source_cleanup_lines
, &old_lines
);
1637 source_line_number
= 0;
1638 source_file_name
= file
;
1641 volatile struct gdb_exception e
;
1643 TRY_CATCH (e
, RETURN_MASK_ERROR
)
1645 read_command_file (stream
);
1652 /* Re-throw the error, but with the file name information
1654 throw_error (e
.error
,
1655 _("%s:%d: Error in sourced command file:\n%s"),
1656 source_file_name
, source_line_number
, e
.message
);
1658 internal_error (__FILE__
, __LINE__
, _("bad reason"));
1662 do_cleanups (old_cleanups
);
1665 /* Print the definition of user command C to STREAM. Or, if C is a
1666 prefix command, show the definitions of all user commands under C
1667 (recursively). PREFIX and NAME combined are the name of the
1670 show_user_1 (struct cmd_list_element
*c
, const char *prefix
, const char *name
,
1671 struct ui_file
*stream
)
1673 struct command_line
*cmdlines
;
1675 if (c
->prefixlist
!= NULL
)
1677 char *prefixname
= c
->prefixname
;
1679 for (c
= *c
->prefixlist
; c
!= NULL
; c
= c
->next
)
1680 if (c
->class == class_user
|| c
->prefixlist
!= NULL
)
1681 show_user_1 (c
, prefixname
, c
->name
, gdb_stdout
);
1685 cmdlines
= c
->user_commands
;
1688 fprintf_filtered (stream
, "User command \"%s%s\":\n", prefix
, name
);
1690 print_command_lines (current_uiout
, cmdlines
, 1);
1691 fputs_filtered ("\n", stream
);
1696 initialize_file_ftype _initialize_cli_script
;
1699 _initialize_cli_script (void)
1701 add_com ("document", class_support
, document_command
, _("\
1702 Document a user-defined command.\n\
1703 Give command name as argument. Give documentation on following lines.\n\
1704 End with a line of just \"end\"."));
1705 add_com ("define", class_support
, define_command
, _("\
1706 Define a new command name. Command name is argument.\n\
1707 Definition appears on following lines, one command per line.\n\
1708 End with a line of just \"end\".\n\
1709 Use the \"document\" command to give documentation for the new command.\n\
1710 Commands defined in this way may have up to ten arguments."));
1712 add_com ("while", class_support
, while_command
, _("\
1713 Execute nested commands WHILE the conditional expression is non zero.\n\
1714 The conditional expression must follow the word `while' and must in turn be\n\
1715 followed by a new line. The nested commands must be entered one per line,\n\
1716 and should be terminated by the word `end'."));
1718 add_com ("if", class_support
, if_command
, _("\
1719 Execute nested commands once IF the conditional expression is non zero.\n\
1720 The conditional expression must follow the word `if' and must in turn be\n\
1721 followed by a new line. The nested commands must be entered one per line,\n\
1722 and should be terminated by the word 'else' or `end'. If an else clause\n\
1723 is used, the same rules apply to its nested commands as to the first ones."));