1 /* GDB CLI command scripting.
3 Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008,
5 2009, 2010 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "language.h" /* For value_true */
28 #include "gdb_string.h"
29 #include "exceptions.h"
31 #include "breakpoint.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-script.h"
35 #include "gdb_assert.h"
37 #include "python/python.h"
39 /* Prototypes for local functions */
41 static enum command_control_type
42 recurse_read_control_structure (char * (*read_next_line_func) (void),
43 struct command_line *current_cmd,
44 void (*validator)(char *, void *),
47 static char *insert_args (char *line);
49 static struct cleanup * setup_user_args (char *p);
51 static char *read_next_line (void);
53 /* Level of control structure when reading. */
54 static int control_level;
56 /* Level of control structure when executing. */
57 static int command_nest_depth = 1;
59 /* This is to prevent certain commands being printed twice. */
60 static int suppress_next_print_command_trace = 0;
62 /* Structure for arguments to user defined functions. */
63 #define MAXUSERARGS 10
66 struct user_args *next;
67 /* It is necessary to store a malloced copy of the command line to
68 ensure that the arguments are not overwritten before they are used. */
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;
147 ui_out_spaces (uiout, 2 * depth);
149 /* A simple command, print it and continue. */
150 if (list->control_type == simple_control)
152 ui_out_field_string (uiout, NULL, list->line);
153 ui_out_text (uiout, "\n");
158 /* loop_continue to jump to the start of a while loop, print it
160 if (list->control_type == continue_control)
162 ui_out_field_string (uiout, NULL, "loop_continue");
163 ui_out_text (uiout, "\n");
168 /* loop_break to break out of a while loop, print it and continue. */
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 continue. */
178 if (list->control_type == while_control
179 || list->control_type == while_stepping_control)
181 /* For while-stepping, the line includes the 'while-stepping' token.
182 See comment in process_next_line for explanation. Here,
183 take care not print 'while-stepping' twice. */
184 if (list->control_type == while_control)
185 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
187 ui_out_field_string (uiout, NULL, list->line);
188 ui_out_text (uiout, "\n");
189 print_command_lines (uiout, *list->body_list, depth + 1);
191 ui_out_spaces (uiout, 2 * depth);
192 ui_out_field_string (uiout, NULL, "end");
193 ui_out_text (uiout, "\n");
198 /* An if command. Recursively print both arms before continueing. */
199 if (list->control_type == if_control)
201 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
202 ui_out_text (uiout, "\n");
204 print_command_lines (uiout, list->body_list[0], depth + 1);
206 /* Show the false arm if it exists. */
207 if (list->body_count == 2)
210 ui_out_spaces (uiout, 2 * depth);
211 ui_out_field_string (uiout, NULL, "else");
212 ui_out_text (uiout, "\n");
213 print_command_lines (uiout, list->body_list[1], depth + 1);
217 ui_out_spaces (uiout, 2 * depth);
218 ui_out_field_string (uiout, NULL, "end");
219 ui_out_text (uiout, "\n");
224 /* A commands command. Print the breakpoint commands and continue. */
225 if (list->control_type == commands_control)
228 ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
230 ui_out_field_string (uiout, NULL, "commands");
231 ui_out_text (uiout, "\n");
232 print_command_lines (uiout, *list->body_list, depth + 1);
234 ui_out_spaces (uiout, 2 * depth);
235 ui_out_field_string (uiout, NULL, "end");
236 ui_out_text (uiout, "\n");
241 if (list->control_type == python_control)
243 ui_out_field_string (uiout, NULL, "python");
244 ui_out_text (uiout, "\n");
245 /* Don't indent python code at all. */
246 print_command_lines (uiout, *list->body_list, 0);
248 ui_out_spaces (uiout, 2 * depth);
249 ui_out_field_string (uiout, NULL, "end");
250 ui_out_text (uiout, "\n");
255 /* ignore illegal command type and try next */
260 /* Handle pre-post hooks. */
263 clear_hook_in_cleanup (void *data)
265 struct cmd_list_element *c = data;
266 c->hook_in = 0; /* Allow hook to work again once it is complete */
270 execute_cmd_pre_hook (struct cmd_list_element *c)
272 if ((c->hook_pre) && (!c->hook_in))
274 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
275 c->hook_in = 1; /* Prevent recursive hooking */
276 execute_user_command (c->hook_pre, (char *) 0);
277 do_cleanups (cleanups);
282 execute_cmd_post_hook (struct cmd_list_element *c)
284 if ((c->hook_post) && (!c->hook_in))
286 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
287 c->hook_in = 1; /* Prevent recursive hooking */
288 execute_user_command (c->hook_post, (char *) 0);
289 do_cleanups (cleanups);
293 /* Execute the command in CMD. */
295 do_restore_user_call_depth (void * call_depth)
297 int * depth = call_depth;
305 execute_user_command (struct cmd_list_element *c, char *args)
307 struct command_line *cmdlines;
308 struct cleanup *old_chain;
309 enum command_control_type ret;
310 static int user_call_depth = 0;
311 extern int max_user_call_depth;
313 old_chain = setup_user_args (args);
315 cmdlines = c->user_commands;
320 if (++user_call_depth > max_user_call_depth)
321 error (_("Max user call depth exceeded -- command aborted."));
323 make_cleanup (do_restore_user_call_depth, &user_call_depth);
325 /* Set the instream to 0, indicating execution of a
326 user-defined function. */
327 make_cleanup (do_restore_instream_cleanup, instream);
328 instream = (FILE *) 0;
330 /* Also set the global in_user_command, so that NULL instream is
331 not confused with Insight. */
334 command_nest_depth++;
337 ret = execute_control_command (cmdlines);
338 if (ret != simple_control && ret != break_control)
340 warning (_("Error executing canned sequence of commands."));
343 cmdlines = cmdlines->next;
345 command_nest_depth--;
346 do_cleanups (old_chain);
349 /* This function is called every time GDB prints a prompt.
350 It ensures that errors and the like to not confuse the command tracing. */
353 reset_command_nest_depth (void)
355 command_nest_depth = 1;
358 suppress_next_print_command_trace = 0;
361 /* Print the command, prefixed with '+' to represent the call depth.
362 This is slightly complicated because this function may be called
363 from execute_command and execute_control_command. Unfortunately
364 execute_command also prints the top level control commands.
365 In these cases execute_command will call execute_control_command
366 via while_command or if_command. Inner levels of 'if' and 'while'
367 are dealt with directly. Therefore we can use these functions
368 to determine whether the command has been printed already or not. */
370 print_command_trace (const char *cmd)
374 if (suppress_next_print_command_trace)
376 suppress_next_print_command_trace = 0;
380 if (!source_verbose && !trace_commands)
383 for (i=0; i < command_nest_depth; i++)
384 printf_filtered ("+");
386 printf_filtered ("%s\n", cmd);
389 enum command_control_type
390 execute_control_command (struct command_line *cmd)
392 struct expression *expr;
393 struct command_line *current;
394 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
396 struct value *val_mark;
398 enum command_control_type ret;
401 /* Start by assuming failure, if a problem is detected, the code
402 below will simply "break" out of the switch. */
403 ret = invalid_control;
405 switch (cmd->control_type)
408 /* A simple command, execute it and return. */
409 new_line = insert_args (cmd->line);
412 make_cleanup (free_current_contents, &new_line);
413 execute_command (new_line, 0);
414 ret = cmd->control_type;
417 case continue_control:
418 print_command_trace ("loop_continue");
420 /* Return for "continue", and "break" so we can either
421 continue the loop at the top, or break out. */
422 ret = cmd->control_type;
426 print_command_trace ("loop_break");
428 /* Return for "continue", and "break" so we can either
429 continue the loop at the top, or break out. */
430 ret = cmd->control_type;
435 char *buffer = alloca (strlen (cmd->line) + 7);
436 sprintf (buffer, "while %s", cmd->line);
437 print_command_trace (buffer);
439 /* Parse the loop control expression for the while statement. */
440 new_line = insert_args (cmd->line);
443 make_cleanup (free_current_contents, &new_line);
444 expr = parse_expression (new_line);
445 make_cleanup (free_current_contents, &expr);
447 ret = simple_control;
450 /* Keep iterating so long as the expression is true. */
457 /* Evaluate the expression. */
458 val_mark = value_mark ();
459 val = evaluate_expression (expr);
460 cond_result = value_true (val);
461 value_free_to_mark (val_mark);
463 /* If the value is false, then break out of the loop. */
467 /* Execute the body of the while statement. */
468 current = *cmd->body_list;
471 command_nest_depth++;
472 ret = execute_control_command (current);
473 command_nest_depth--;
475 /* If we got an error, or a "break" command, then stop
477 if (ret == invalid_control || ret == break_control)
483 /* If we got a "continue" command, then restart the loop
485 if (ret == continue_control)
488 /* Get the next statement. */
489 current = current->next;
493 /* Reset RET so that we don't recurse the break all the way down. */
494 if (ret == break_control)
495 ret = simple_control;
502 char *buffer = alloca (strlen (cmd->line) + 4);
503 sprintf (buffer, "if %s", cmd->line);
504 print_command_trace (buffer);
506 new_line = insert_args (cmd->line);
509 make_cleanup (free_current_contents, &new_line);
510 /* Parse the conditional for the if statement. */
511 expr = parse_expression (new_line);
512 make_cleanup (free_current_contents, &expr);
515 ret = simple_control;
517 /* Evaluate the conditional. */
518 val_mark = value_mark ();
519 val = evaluate_expression (expr);
521 /* Choose which arm to take commands from based on the value of the
522 conditional expression. */
523 if (value_true (val))
524 current = *cmd->body_list;
525 else if (cmd->body_count == 2)
526 current = *(cmd->body_list + 1);
527 value_free_to_mark (val_mark);
529 /* Execute commands in the given arm. */
532 command_nest_depth++;
533 ret = execute_control_command (current);
534 command_nest_depth--;
536 /* If we got an error, get out. */
537 if (ret != simple_control)
540 /* Get the next statement in the body. */
541 current = current->next;
546 case commands_control:
548 /* Breakpoint commands list, record the commands in the breakpoint's
549 command list and return. */
550 new_line = insert_args (cmd->line);
553 make_cleanup (free_current_contents, &new_line);
554 ret = commands_from_control_command (new_line, cmd);
559 eval_python_from_control_command (cmd);
560 ret = simple_control;
565 warning (_("Invalid control type in canned commands structure."));
569 do_cleanups (old_chain);
574 /* Like execute_control_command, but first set
575 suppress_next_print_command_trace. */
577 enum command_control_type
578 execute_control_command_untraced (struct command_line *cmd)
580 suppress_next_print_command_trace = 1;
581 return execute_control_command (cmd);
585 /* "while" command support. Executes a body of statements while the
586 loop condition is nonzero. */
589 while_command (char *arg, int from_tty)
591 struct command_line *command = NULL;
594 command = get_command_line (while_control, arg);
599 execute_control_command_untraced (command);
600 free_command_lines (&command);
603 /* "if" command support. Execute either the true or false arm depending
604 on the value of the if conditional. */
607 if_command (char *arg, int from_tty)
609 struct command_line *command = NULL;
612 command = get_command_line (if_control, arg);
617 execute_control_command_untraced (command);
618 free_command_lines (&command);
623 arg_cleanup (void *ignore)
625 struct user_args *oargs = user_args;
627 internal_error (__FILE__, __LINE__,
628 _("arg_cleanup called with no user args.\n"));
630 user_args = user_args->next;
631 xfree (oargs->command);
635 /* Bind the incomming arguments for a user defined command to
636 $arg0, $arg1 ... $argMAXUSERARGS. */
638 static struct cleanup *
639 setup_user_args (char *p)
641 struct user_args *args;
642 struct cleanup *old_chain;
643 unsigned int arg_count = 0;
645 args = (struct user_args *) xmalloc (sizeof (struct user_args));
646 memset (args, 0, sizeof (struct user_args));
648 args->next = user_args;
651 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
656 user_args->command = p = xstrdup (p);
665 if (arg_count >= MAXUSERARGS)
667 error (_("user defined function may only have %d arguments."),
672 /* Strip whitespace. */
673 while (*p == ' ' || *p == '\t')
676 /* P now points to an argument. */
678 user_args->a[arg_count].arg = p;
680 /* Get to the end of this argument. */
683 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
712 user_args->a[arg_count].len = p - start_arg;
719 /* Given character string P, return a point to the first argument ($arg),
720 or NULL if P contains no arguments. */
725 while ((p = strchr (p, '$')))
727 if (strncmp (p, "$arg", 4) == 0
728 && (isdigit (p[4]) || p[4] == 'c'))
735 /* Insert the user defined arguments stored in user_arg into the $arg
736 arguments found in line, with the updated copy being placed into nline. */
739 insert_args (char *line)
741 char *p, *save_line, *new_line;
744 /* If we are not in a user-defined function, treat $argc, $arg0, et
745 cetera as normal convenience variables. */
746 if (user_args == NULL)
747 return xstrdup (line);
749 /* First we need to know how much memory to allocate for the new line. */
752 while ((p = locate_arg (line)))
759 /* $argc. Number will be <=10. */
760 len += user_args->count == 10 ? 2 : 1;
762 else if (i >= user_args->count)
764 error (_("Missing argument %d in user function."), i);
769 len += user_args->a[i].len;
774 /* Don't forget the tail. */
775 len += strlen (line);
777 /* Allocate space for the new line and fill it in. */
778 new_line = (char *) xmalloc (len + 1);
779 if (new_line == NULL)
782 /* Restore pointer to beginning of old line. */
785 /* Save pointer to beginning of new line. */
786 save_line = new_line;
788 while ((p = locate_arg (line)))
792 memcpy (new_line, line, p - line);
793 new_line += p - line;
797 gdb_assert (user_args->count >= 0 && user_args->count <= 10);
798 if (user_args->count == 10)
804 *(new_line++) = user_args->count + '0';
809 len = user_args->a[i].len;
812 memcpy (new_line, user_args->a[i].arg, len);
818 /* Don't forget the tail. */
819 strcpy (new_line, line);
821 /* Return a pointer to the beginning of the new line. */
826 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
827 code bodies. This is typically used when we encounter an "else"
828 clause for an "if" command. */
831 realloc_body_list (struct command_line *command, int new_length)
834 struct command_line **body_list;
836 n = command->body_count;
842 body_list = (struct command_line **)
843 xmalloc (sizeof (struct command_line *) * new_length);
845 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
846 memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
848 xfree (command->body_list);
849 command->body_list = body_list;
850 command->body_count = new_length;
853 /* Read next line from stdout. Passed to read_command_line_1 and
854 recurse_read_control_structure whenever we need to read commands
858 read_next_line (void)
860 char *prompt_ptr, control_prompt[256];
863 if (control_level >= 254)
864 error (_("Control nesting too deep!"));
866 /* Set a prompt based on the nesting of the control commands. */
867 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
869 for (i = 0; i < control_level; i++)
870 control_prompt[i] = ' ';
871 control_prompt[i] = '>';
872 control_prompt[i + 1] = '\0';
873 prompt_ptr = (char *) &control_prompt[0];
878 return command_line_input (prompt_ptr, instream == stdin, "commands");
881 /* Process one input line. If the command is an "end",
882 return such an indication to the caller. If PARSE_COMMANDS is true,
883 strip leading whitespace (trailing whitespace is always stripped)
884 in the line, attempt to recognize GDB control commands, and also
885 return an indication if the command is an "else" or a nop.
886 Otherwise, only "end" is recognized. */
888 static enum misc_command_type
889 process_next_line (char *p, struct command_line **command, int parse_commands,
890 void (*validator)(char *, void *), void *closure)
896 /* Not sure what to do here. */
900 /* Strip trailing whitespace. */
901 p_end = p + strlen (p);
902 while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
906 /* Strip leading whitespace. */
907 while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
910 /* 'end' is always recognized, regardless of parse_commands value.
911 We also permit whitespace before end and after. */
912 if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
917 /* If commands are parsed, we skip initial spaces. Otherwise,
918 which is the case for Python commands and documentation
919 (see the 'document' command), spaces are preserved. */
922 /* Blanks and comments don't really do anything, but we need to
923 distinguish them from else, end and other commands which can be
925 if (p_end == p || p[0] == '#')
928 /* Is the else clause of an if control structure? */
929 if (p_end - p == 4 && !strncmp (p, "else", 4))
932 /* Check for while, if, break, continue, etc and build a new command
933 line structure for them. */
934 if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
935 || (p_end -p >= 2 && !strncmp (p, "ws", 2)))
937 /* Because validate_actionline and encode_action lookup
938 command's line as command, we need the line to
939 include 'while-stepping'.
941 For 'ws' alias, the command will have 'ws', not expanded
942 to 'while-stepping'. This is intentional -- we don't
943 really want frontend to send a command list with 'ws',
944 and next break-info returning command line with 'while-stepping'.
945 This should work, but might cause the breakpoint to be marked as
946 changed while it's actually not. */
947 *command = build_command_line (while_stepping_control, p);
949 else if (p_end - p > 5 && !strncmp (p, "while", 5))
953 while (first_arg < p_end && isspace (*first_arg))
955 *command = build_command_line (while_control, first_arg);
957 else if (p_end - p > 2 && !strncmp (p, "if", 2))
961 while (first_arg < p_end && isspace (*first_arg))
963 *command = build_command_line (if_control, first_arg);
965 else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
969 while (first_arg < p_end && isspace (*first_arg))
971 *command = build_command_line (commands_control, first_arg);
973 else if (p_end - p == 6 && !strncmp (p, "python", 6))
975 /* Note that we ignore the inline "python command" form
977 *command = build_command_line (python_control, "");
979 else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
981 *command = (struct command_line *)
982 xmalloc (sizeof (struct command_line));
983 (*command)->next = NULL;
984 (*command)->line = NULL;
985 (*command)->control_type = break_control;
986 (*command)->body_count = 0;
987 (*command)->body_list = NULL;
989 else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
991 *command = (struct command_line *)
992 xmalloc (sizeof (struct command_line));
993 (*command)->next = NULL;
994 (*command)->line = NULL;
995 (*command)->control_type = continue_control;
996 (*command)->body_count = 0;
997 (*command)->body_list = NULL;
1003 if (!parse_commands || not_handled)
1005 /* A normal command. */
1006 *command = (struct command_line *)
1007 xmalloc (sizeof (struct command_line));
1008 (*command)->next = NULL;
1009 (*command)->line = savestring (p, p_end - p);
1010 (*command)->control_type = simple_control;
1011 (*command)->body_count = 0;
1012 (*command)->body_list = NULL;
1017 volatile struct gdb_exception ex;
1018 TRY_CATCH (ex, RETURN_MASK_ALL)
1020 validator ((*command)->line, closure);
1025 throw_exception (ex);
1029 /* Nothing special. */
1033 /* Recursively read in the control structures and create a command_line
1034 structure from them. Use read_next_line_func to obtain lines of
1039 static enum command_control_type
1040 recurse_read_control_structure (char * (*read_next_line_func) (void),
1041 struct command_line *current_cmd,
1042 void (*validator)(char *, void *),
1045 int current_body, i;
1046 enum misc_command_type val;
1047 enum command_control_type ret;
1048 struct command_line **body_ptr, *child_tail, *next;
1054 /* Sanity checks. */
1055 if (current_cmd->control_type == simple_control)
1056 error (_("Recursed on a simple control type."));
1058 if (current_body > current_cmd->body_count)
1059 error (_("Allocated body is smaller than this command type needs."));
1061 /* Read lines from the input stream and build control structures. */
1067 val = process_next_line (read_next_line_func (), &next,
1068 current_cmd->control_type != python_control,
1069 validator, closure);
1071 /* Just skip blanks and comments. */
1072 if (val == nop_command)
1075 if (val == end_command)
1077 if (current_cmd->control_type == while_control
1078 || current_cmd->control_type == while_stepping_control
1079 || current_cmd->control_type == if_control
1080 || current_cmd->control_type == python_control
1081 || current_cmd->control_type == commands_control)
1083 /* Success reading an entire canned sequence of commands. */
1084 ret = simple_control;
1089 ret = invalid_control;
1094 /* Not the end of a control structure. */
1095 if (val == else_command)
1097 if (current_cmd->control_type == if_control
1098 && current_body == 1)
1100 realloc_body_list (current_cmd, 2);
1107 ret = invalid_control;
1114 child_tail->next = next;
1118 body_ptr = current_cmd->body_list;
1119 for (i = 1; i < current_body; i++)
1128 /* If the latest line is another control structure, then recurse
1130 if (next->control_type == while_control
1131 || next->control_type == while_stepping_control
1132 || next->control_type == if_control
1133 || next->control_type == python_control
1134 || next->control_type == commands_control)
1137 ret = recurse_read_control_structure (read_next_line_func, next,
1138 validator, closure);
1141 if (ret != simple_control)
1151 /* Read lines from the input stream and accumulate them in a chain of
1152 struct command_line's, which is then returned. For input from a
1153 terminal, the special command "end" is used to mark the end of the
1154 input, and is not included in the returned chain of commands.
1156 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1157 is always stripped) in the line and attempt to recognize GDB control
1158 commands. Otherwise, only "end" is recognized. */
1160 #define END_MESSAGE "End with a line saying just \"end\"."
1162 struct command_line *
1163 read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1164 void (*validator)(char *, void *), void *closure)
1166 struct command_line *head;
1168 if (from_tty && input_from_terminal_p ())
1170 if (deprecated_readline_begin_hook)
1172 /* Note - intentional to merge messages with no newline */
1173 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1177 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1178 gdb_flush (gdb_stdout);
1182 head = read_command_lines_1 (read_next_line, parse_commands,
1183 validator, closure);
1185 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1187 (*deprecated_readline_end_hook) ();
1192 /* Act the same way as read_command_lines, except that each new line is
1193 obtained using READ_NEXT_LINE_FUNC. */
1195 struct command_line *
1196 read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1197 void (*validator)(char *, void *), void *closure)
1199 struct command_line *head, *tail, *next;
1200 struct cleanup *old_chain;
1201 enum command_control_type ret;
1202 enum misc_command_type val;
1211 val = process_next_line (read_next_line_func (), &next, parse_commands,
1212 validator, closure);
1214 /* Ignore blank lines or comments. */
1215 if (val == nop_command)
1218 if (val == end_command)
1220 ret = simple_control;
1224 if (val != ok_command)
1226 ret = invalid_control;
1230 if (next->control_type == while_control
1231 || next->control_type == if_control
1232 || next->control_type == python_control
1233 || next->control_type == commands_control
1234 || next->control_type == while_stepping_control)
1237 ret = recurse_read_control_structure (read_next_line_func, next,
1238 validator, closure);
1241 if (ret == invalid_control)
1252 old_chain = make_cleanup_free_command_lines (&head);
1261 if (ret != invalid_control)
1263 discard_cleanups (old_chain);
1266 do_cleanups (old_chain);
1272 /* Free a chain of struct command_line's. */
1275 free_command_lines (struct command_line **lptr)
1277 struct command_line *l = *lptr;
1278 struct command_line *next;
1279 struct command_line **blist;
1284 if (l->body_count > 0)
1286 blist = l->body_list;
1287 for (i = 0; i < l->body_count; i++, blist++)
1288 free_command_lines (blist);
1299 do_free_command_lines_cleanup (void *arg)
1301 free_command_lines (arg);
1305 make_cleanup_free_command_lines (struct command_line **arg)
1307 return make_cleanup (do_free_command_lines_cleanup, arg);
1310 struct command_line *
1311 copy_command_lines (struct command_line *cmds)
1313 struct command_line *result = NULL;
1317 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1319 result->next = copy_command_lines (cmds->next);
1320 result->line = xstrdup (cmds->line);
1321 result->control_type = cmds->control_type;
1322 result->body_count = cmds->body_count;
1323 if (cmds->body_count > 0)
1327 result->body_list = (struct command_line **)
1328 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1330 for (i = 0; i < cmds->body_count; i++)
1331 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1334 result->body_list = NULL;
1340 /* Validate that *COMNAME is a valid name for a command. Return the
1341 containing command list, in case it starts with a prefix command.
1342 The prefix must already exist. *COMNAME is advanced to point after
1343 any prefix, and a NUL character overwrites the space after the
1346 static struct cmd_list_element **
1347 validate_comname (char **comname)
1349 struct cmd_list_element **list = &cmdlist;
1350 char *p, *last_word;
1353 error_no_arg (_("name of command to define"));
1355 /* Find the last word of the argument. */
1356 p = *comname + strlen (*comname);
1357 while (p > *comname && isspace (p[-1]))
1359 while (p > *comname && !isspace (p[-1]))
1363 /* Find the corresponding command list. */
1364 if (last_word != *comname)
1366 struct cmd_list_element *c;
1367 char saved_char, *tem = *comname;
1369 /* Separate the prefix and the command. */
1370 saved_char = last_word[-1];
1371 last_word[-1] = '\0';
1373 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1374 if (c->prefixlist == NULL)
1375 error (_("\"%s\" is not a prefix command."), *comname);
1377 list = c->prefixlist;
1378 last_word[-1] = saved_char;
1379 *comname = last_word;
1385 if (!isalnum (*p) && *p != '-' && *p != '_')
1386 error (_("Junk in argument list: \"%s\""), p);
1393 /* This is just a placeholder in the command data structures. */
1395 user_defined_command (char *ignore, int from_tty)
1400 define_command (char *comname, int from_tty)
1402 #define MAX_TMPBUF 128
1409 struct command_line *cmds;
1410 struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
1411 char *tem, *tem2, *comfull;
1412 char tmpbuf[MAX_TMPBUF];
1413 int hook_type = CMD_NO_HOOK;
1414 int hook_name_size = 0;
1416 #define HOOK_STRING "hook-"
1418 #define HOOK_POST_STRING "hookpost-"
1419 #define HOOK_POST_LEN 9
1422 list = validate_comname (&comname);
1424 /* Look it up, and verify that we got an exact match. */
1426 c = lookup_cmd (&tem, *list, "", -1, 1);
1427 if (c && strcmp (comname, c->name) != 0)
1433 if (c->class == class_user || c->class == class_alias)
1434 q = query (_("Redefine command \"%s\"? "), c->name);
1436 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1438 error (_("Command \"%s\" not redefined."), c->name);
1441 /* If this new command is a hook, then mark the command which it
1442 is hooking. Note that we allow hooking `help' commands, so that
1443 we can hook the `stop' pseudo-command. */
1445 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1447 hook_type = CMD_PRE_HOOK;
1448 hook_name_size = HOOK_LEN;
1450 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1452 hook_type = CMD_POST_HOOK;
1453 hook_name_size = HOOK_POST_LEN;
1456 if (hook_type != CMD_NO_HOOK)
1458 /* Look up cmd it hooks, and verify that we got an exact match. */
1459 tem = comname + hook_name_size;
1460 hookc = lookup_cmd (&tem, *list, "", -1, 0);
1461 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1465 warning (_("Your new `%s' command does not hook any existing command."),
1467 if (!query (_("Proceed? ")))
1468 error (_("Not confirmed."));
1472 comname = xstrdup (comname);
1474 /* If the rest of the commands will be case insensitive, this one
1475 should behave in the same manner. */
1476 for (tem = comname; *tem; tem++)
1478 *tem = tolower (*tem);
1480 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
1481 cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
1483 if (c && c->class == class_user)
1484 free_command_lines (&c->user_commands);
1486 newc = add_cmd (comname, class_user, user_defined_command,
1487 (c && c->class == class_user)
1488 ? c->doc : xstrdup ("User-defined."), list);
1489 newc->user_commands = cmds;
1491 /* If this new command is a hook, then mark both commands as being
1498 hookc->hook_pre = newc; /* Target gets hooked. */
1499 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1502 hookc->hook_post = newc; /* Target gets hooked. */
1503 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1506 /* Should never come here as hookc would be 0. */
1507 internal_error (__FILE__, __LINE__, _("bad switch"));
1513 document_command (char *comname, int from_tty)
1515 struct command_line *doclines;
1516 struct cmd_list_element *c, **list;
1517 char *tem, *comfull;
1521 list = validate_comname (&comname);
1524 c = lookup_cmd (&tem, *list, "", 0, 1);
1526 if (c->class != class_user)
1527 error (_("Command \"%s\" is built-in."), comfull);
1529 sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
1530 doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
1536 struct command_line *cl1;
1539 for (cl1 = doclines; cl1; cl1 = cl1->next)
1540 len += strlen (cl1->line) + 1;
1542 c->doc = (char *) xmalloc (len + 1);
1545 for (cl1 = doclines; cl1; cl1 = cl1->next)
1547 strcat (c->doc, cl1->line);
1549 strcat (c->doc, "\n");
1553 free_command_lines (&doclines);
1556 struct source_cleanup_lines_args
1563 source_cleanup_lines (void *args)
1565 struct source_cleanup_lines_args *p =
1566 (struct source_cleanup_lines_args *) args;
1567 source_line_number = p->old_line;
1568 source_file_name = p->old_file;
1571 struct wrapped_read_command_file_args
1577 wrapped_read_command_file (struct ui_out *uiout, void *data)
1579 struct wrapped_read_command_file_args *args = data;
1580 read_command_file (args->stream);
1583 /* Used to implement source_command */
1586 script_from_file (FILE *stream, char *file)
1588 struct cleanup *old_cleanups;
1589 struct source_cleanup_lines_args old_lines;
1593 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1595 old_cleanups = make_cleanup_fclose (stream);
1597 old_lines.old_line = source_line_number;
1598 old_lines.old_file = source_file_name;
1599 make_cleanup (source_cleanup_lines, &old_lines);
1600 source_line_number = 0;
1601 source_file_name = file;
1602 /* This will get set every time we read a line. So it won't stay "" for
1604 error_pre_print = "";
1607 struct gdb_exception e;
1608 struct wrapped_read_command_file_args args;
1609 args.stream = stream;
1610 e = catch_exception (uiout, wrapped_read_command_file, &args,
1617 /* Re-throw the error, but with the file name information
1619 throw_error (e.error,
1620 _("%s:%d: Error in sourced command file:\n%s"),
1621 source_file_name, source_line_number, e.message);
1623 internal_error (__FILE__, __LINE__, _("bad reason"));
1627 do_cleanups (old_cleanups);
1630 /* Print the definition of user command C to STREAM. Or, if C is a
1631 prefix command, show the definitions of all user commands under C
1632 (recursively). PREFIX and NAME combined are the name of the
1635 show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1636 struct ui_file *stream)
1638 struct command_line *cmdlines;
1640 if (c->prefixlist != NULL)
1642 char *prefixname = c->prefixname;
1643 for (c = *c->prefixlist; c != NULL; c = c->next)
1644 if (c->class == class_user || c->prefixlist != NULL)
1645 show_user_1 (c, prefixname, c->name, gdb_stdout);
1649 cmdlines = c->user_commands;
1652 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1654 print_command_lines (uiout, cmdlines, 1);
1655 fputs_filtered ("\n", stream);