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 >= 8 && !strncmp (p, "stepping", 8))
936 || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
938 /* Because validate_actionline and encode_action lookup
939 command's line as command, we need the line to
940 include 'while-stepping'.
942 For 'ws' alias, the command will have 'ws', not expanded
943 to 'while-stepping'. This is intentional -- we don't
944 really want frontend to send a command list with 'ws',
945 and next break-info returning command line with 'while-stepping'.
946 This should work, but might cause the breakpoint to be marked as
947 changed while it's actually not. */
948 *command = build_command_line (while_stepping_control, p);
950 else if (p_end - p > 5 && !strncmp (p, "while", 5))
954 while (first_arg < p_end && isspace (*first_arg))
956 *command = build_command_line (while_control, first_arg);
958 else if (p_end - p > 2 && !strncmp (p, "if", 2))
962 while (first_arg < p_end && isspace (*first_arg))
964 *command = build_command_line (if_control, first_arg);
966 else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
970 while (first_arg < p_end && isspace (*first_arg))
972 *command = build_command_line (commands_control, first_arg);
974 else if (p_end - p == 6 && !strncmp (p, "python", 6))
976 /* Note that we ignore the inline "python command" form
978 *command = build_command_line (python_control, "");
980 else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
982 *command = (struct command_line *)
983 xmalloc (sizeof (struct command_line));
984 (*command)->next = NULL;
985 (*command)->line = NULL;
986 (*command)->control_type = break_control;
987 (*command)->body_count = 0;
988 (*command)->body_list = NULL;
990 else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
992 *command = (struct command_line *)
993 xmalloc (sizeof (struct command_line));
994 (*command)->next = NULL;
995 (*command)->line = NULL;
996 (*command)->control_type = continue_control;
997 (*command)->body_count = 0;
998 (*command)->body_list = NULL;
1004 if (!parse_commands || not_handled)
1006 /* A normal command. */
1007 *command = (struct command_line *)
1008 xmalloc (sizeof (struct command_line));
1009 (*command)->next = NULL;
1010 (*command)->line = savestring (p, p_end - p);
1011 (*command)->control_type = simple_control;
1012 (*command)->body_count = 0;
1013 (*command)->body_list = NULL;
1018 volatile struct gdb_exception ex;
1019 TRY_CATCH (ex, RETURN_MASK_ALL)
1021 validator ((*command)->line, closure);
1026 throw_exception (ex);
1030 /* Nothing special. */
1034 /* Recursively read in the control structures and create a command_line
1035 structure from them. Use read_next_line_func to obtain lines of
1040 static enum command_control_type
1041 recurse_read_control_structure (char * (*read_next_line_func) (void),
1042 struct command_line *current_cmd,
1043 void (*validator)(char *, void *),
1046 int current_body, i;
1047 enum misc_command_type val;
1048 enum command_control_type ret;
1049 struct command_line **body_ptr, *child_tail, *next;
1055 /* Sanity checks. */
1056 if (current_cmd->control_type == simple_control)
1057 error (_("Recursed on a simple control type."));
1059 if (current_body > current_cmd->body_count)
1060 error (_("Allocated body is smaller than this command type needs."));
1062 /* Read lines from the input stream and build control structures. */
1068 val = process_next_line (read_next_line_func (), &next,
1069 current_cmd->control_type != python_control,
1070 validator, closure);
1072 /* Just skip blanks and comments. */
1073 if (val == nop_command)
1076 if (val == end_command)
1078 if (current_cmd->control_type == while_control
1079 || current_cmd->control_type == while_stepping_control
1080 || current_cmd->control_type == if_control
1081 || current_cmd->control_type == python_control
1082 || current_cmd->control_type == commands_control)
1084 /* Success reading an entire canned sequence of commands. */
1085 ret = simple_control;
1090 ret = invalid_control;
1095 /* Not the end of a control structure. */
1096 if (val == else_command)
1098 if (current_cmd->control_type == if_control
1099 && current_body == 1)
1101 realloc_body_list (current_cmd, 2);
1108 ret = invalid_control;
1115 child_tail->next = next;
1119 body_ptr = current_cmd->body_list;
1120 for (i = 1; i < current_body; i++)
1129 /* If the latest line is another control structure, then recurse
1131 if (next->control_type == while_control
1132 || next->control_type == while_stepping_control
1133 || next->control_type == if_control
1134 || next->control_type == python_control
1135 || next->control_type == commands_control)
1138 ret = recurse_read_control_structure (read_next_line_func, next,
1139 validator, closure);
1142 if (ret != simple_control)
1152 /* Read lines from the input stream and accumulate them in a chain of
1153 struct command_line's, which is then returned. For input from a
1154 terminal, the special command "end" is used to mark the end of the
1155 input, and is not included in the returned chain of commands.
1157 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1158 is always stripped) in the line and attempt to recognize GDB control
1159 commands. Otherwise, only "end" is recognized. */
1161 #define END_MESSAGE "End with a line saying just \"end\"."
1163 struct command_line *
1164 read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1165 void (*validator)(char *, void *), void *closure)
1167 struct command_line *head;
1169 if (from_tty && input_from_terminal_p ())
1171 if (deprecated_readline_begin_hook)
1173 /* Note - intentional to merge messages with no newline */
1174 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1178 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1179 gdb_flush (gdb_stdout);
1183 head = read_command_lines_1 (read_next_line, parse_commands,
1184 validator, closure);
1186 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1188 (*deprecated_readline_end_hook) ();
1193 /* Act the same way as read_command_lines, except that each new line is
1194 obtained using READ_NEXT_LINE_FUNC. */
1196 struct command_line *
1197 read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1198 void (*validator)(char *, void *), void *closure)
1200 struct command_line *head, *tail, *next;
1201 struct cleanup *old_chain;
1202 enum command_control_type ret;
1203 enum misc_command_type val;
1212 val = process_next_line (read_next_line_func (), &next, parse_commands,
1213 validator, closure);
1215 /* Ignore blank lines or comments. */
1216 if (val == nop_command)
1219 if (val == end_command)
1221 ret = simple_control;
1225 if (val != ok_command)
1227 ret = invalid_control;
1231 if (next->control_type == while_control
1232 || next->control_type == if_control
1233 || next->control_type == python_control
1234 || next->control_type == commands_control
1235 || next->control_type == while_stepping_control)
1238 ret = recurse_read_control_structure (read_next_line_func, next,
1239 validator, closure);
1242 if (ret == invalid_control)
1253 old_chain = make_cleanup_free_command_lines (&head);
1262 if (ret != invalid_control)
1264 discard_cleanups (old_chain);
1267 do_cleanups (old_chain);
1273 /* Free a chain of struct command_line's. */
1276 free_command_lines (struct command_line **lptr)
1278 struct command_line *l = *lptr;
1279 struct command_line *next;
1280 struct command_line **blist;
1285 if (l->body_count > 0)
1287 blist = l->body_list;
1288 for (i = 0; i < l->body_count; i++, blist++)
1289 free_command_lines (blist);
1300 do_free_command_lines_cleanup (void *arg)
1302 free_command_lines (arg);
1306 make_cleanup_free_command_lines (struct command_line **arg)
1308 return make_cleanup (do_free_command_lines_cleanup, arg);
1311 struct command_line *
1312 copy_command_lines (struct command_line *cmds)
1314 struct command_line *result = NULL;
1318 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1320 result->next = copy_command_lines (cmds->next);
1321 result->line = xstrdup (cmds->line);
1322 result->control_type = cmds->control_type;
1323 result->body_count = cmds->body_count;
1324 if (cmds->body_count > 0)
1328 result->body_list = (struct command_line **)
1329 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1331 for (i = 0; i < cmds->body_count; i++)
1332 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1335 result->body_list = NULL;
1341 /* Validate that *COMNAME is a valid name for a command. Return the
1342 containing command list, in case it starts with a prefix command.
1343 The prefix must already exist. *COMNAME is advanced to point after
1344 any prefix, and a NUL character overwrites the space after the
1347 static struct cmd_list_element **
1348 validate_comname (char **comname)
1350 struct cmd_list_element **list = &cmdlist;
1351 char *p, *last_word;
1354 error_no_arg (_("name of command to define"));
1356 /* Find the last word of the argument. */
1357 p = *comname + strlen (*comname);
1358 while (p > *comname && isspace (p[-1]))
1360 while (p > *comname && !isspace (p[-1]))
1364 /* Find the corresponding command list. */
1365 if (last_word != *comname)
1367 struct cmd_list_element *c;
1368 char saved_char, *tem = *comname;
1370 /* Separate the prefix and the command. */
1371 saved_char = last_word[-1];
1372 last_word[-1] = '\0';
1374 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1375 if (c->prefixlist == NULL)
1376 error (_("\"%s\" is not a prefix command."), *comname);
1378 list = c->prefixlist;
1379 last_word[-1] = saved_char;
1380 *comname = last_word;
1386 if (!isalnum (*p) && *p != '-' && *p != '_')
1387 error (_("Junk in argument list: \"%s\""), p);
1394 /* This is just a placeholder in the command data structures. */
1396 user_defined_command (char *ignore, int from_tty)
1401 define_command (char *comname, int from_tty)
1403 #define MAX_TMPBUF 128
1410 struct command_line *cmds;
1411 struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
1412 char *tem, *tem2, *comfull;
1413 char tmpbuf[MAX_TMPBUF];
1414 int hook_type = CMD_NO_HOOK;
1415 int hook_name_size = 0;
1417 #define HOOK_STRING "hook-"
1419 #define HOOK_POST_STRING "hookpost-"
1420 #define HOOK_POST_LEN 9
1423 list = validate_comname (&comname);
1425 /* Look it up, and verify that we got an exact match. */
1427 c = lookup_cmd (&tem, *list, "", -1, 1);
1428 if (c && strcmp (comname, c->name) != 0)
1434 if (c->class == class_user || c->class == class_alias)
1435 q = query (_("Redefine command \"%s\"? "), c->name);
1437 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1439 error (_("Command \"%s\" not redefined."), c->name);
1442 /* If this new command is a hook, then mark the command which it
1443 is hooking. Note that we allow hooking `help' commands, so that
1444 we can hook the `stop' pseudo-command. */
1446 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1448 hook_type = CMD_PRE_HOOK;
1449 hook_name_size = HOOK_LEN;
1451 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1453 hook_type = CMD_POST_HOOK;
1454 hook_name_size = HOOK_POST_LEN;
1457 if (hook_type != CMD_NO_HOOK)
1459 /* Look up cmd it hooks, and verify that we got an exact match. */
1460 tem = comname + hook_name_size;
1461 hookc = lookup_cmd (&tem, *list, "", -1, 0);
1462 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1466 warning (_("Your new `%s' command does not hook any existing command."),
1468 if (!query (_("Proceed? ")))
1469 error (_("Not confirmed."));
1473 comname = xstrdup (comname);
1475 /* If the rest of the commands will be case insensitive, this one
1476 should behave in the same manner. */
1477 for (tem = comname; *tem; tem++)
1479 *tem = tolower (*tem);
1481 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
1482 cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
1484 if (c && c->class == class_user)
1485 free_command_lines (&c->user_commands);
1487 newc = add_cmd (comname, class_user, user_defined_command,
1488 (c && c->class == class_user)
1489 ? c->doc : xstrdup ("User-defined."), list);
1490 newc->user_commands = cmds;
1492 /* If this new command is a hook, then mark both commands as being
1499 hookc->hook_pre = newc; /* Target gets hooked. */
1500 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1503 hookc->hook_post = newc; /* Target gets hooked. */
1504 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1507 /* Should never come here as hookc would be 0. */
1508 internal_error (__FILE__, __LINE__, _("bad switch"));
1514 document_command (char *comname, int from_tty)
1516 struct command_line *doclines;
1517 struct cmd_list_element *c, **list;
1518 char *tem, *comfull;
1522 list = validate_comname (&comname);
1525 c = lookup_cmd (&tem, *list, "", 0, 1);
1527 if (c->class != class_user)
1528 error (_("Command \"%s\" is built-in."), comfull);
1530 sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
1531 doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
1537 struct command_line *cl1;
1540 for (cl1 = doclines; cl1; cl1 = cl1->next)
1541 len += strlen (cl1->line) + 1;
1543 c->doc = (char *) xmalloc (len + 1);
1546 for (cl1 = doclines; cl1; cl1 = cl1->next)
1548 strcat (c->doc, cl1->line);
1550 strcat (c->doc, "\n");
1554 free_command_lines (&doclines);
1557 struct source_cleanup_lines_args
1560 const char *old_file;
1564 source_cleanup_lines (void *args)
1566 struct source_cleanup_lines_args *p =
1567 (struct source_cleanup_lines_args *) args;
1568 source_line_number = p->old_line;
1569 source_file_name = p->old_file;
1572 struct wrapped_read_command_file_args
1578 wrapped_read_command_file (struct ui_out *uiout, void *data)
1580 struct wrapped_read_command_file_args *args = data;
1581 read_command_file (args->stream);
1584 /* Used to implement source_command */
1587 script_from_file (FILE *stream, const char *file)
1589 struct cleanup *old_cleanups;
1590 struct source_cleanup_lines_args old_lines;
1594 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1596 old_cleanups = make_cleanup_fclose (stream);
1598 old_lines.old_line = source_line_number;
1599 old_lines.old_file = source_file_name;
1600 make_cleanup (source_cleanup_lines, &old_lines);
1601 source_line_number = 0;
1602 source_file_name = file;
1603 /* This will get set every time we read a line. So it won't stay "" for
1605 error_pre_print = "";
1608 struct gdb_exception e;
1609 struct wrapped_read_command_file_args args;
1610 args.stream = stream;
1611 e = catch_exception (uiout, wrapped_read_command_file, &args,
1618 /* Re-throw the error, but with the file name information
1620 throw_error (e.error,
1621 _("%s:%d: Error in sourced command file:\n%s"),
1622 source_file_name, source_line_number, e.message);
1624 internal_error (__FILE__, __LINE__, _("bad reason"));
1628 do_cleanups (old_cleanups);
1631 /* Print the definition of user command C to STREAM. Or, if C is a
1632 prefix command, show the definitions of all user commands under C
1633 (recursively). PREFIX and NAME combined are the name of the
1636 show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1637 struct ui_file *stream)
1639 struct command_line *cmdlines;
1641 if (c->prefixlist != NULL)
1643 char *prefixname = c->prefixname;
1644 for (c = *c->prefixlist; c != NULL; c = c->next)
1645 if (c->class == class_user || c->prefixlist != NULL)
1646 show_user_1 (c, prefixname, c->name, gdb_stdout);
1650 cmdlines = c->user_commands;
1653 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1655 print_command_lines (uiout, cmdlines, 1);
1656 fputs_filtered ("\n", stream);