1 /* GDB CLI command scripting.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005 Free
5 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
26 #include "language.h" /* For value_true */
30 #include "gdb_string.h"
31 #include "exceptions.h"
33 #include "cli/cli-cmds.h"
34 #include "cli/cli-decode.h"
35 #include "cli/cli-script.h"
37 /* Prototypes for local functions */
39 static enum command_control_type
40 recurse_read_control_structure (struct command_line *current_cmd);
42 static char *insert_args (char *line);
44 static struct cleanup * setup_user_args (char *p);
46 static void validate_comname (char *);
48 /* Level of control structure. */
49 static int control_level;
51 /* Structure for arguments to user defined functions. */
52 #define MAXUSERARGS 10
55 struct user_args *next;
67 /* Allocate, initialize a new command line structure for one of the
68 control commands (if/while). */
70 static struct command_line *
71 build_command_line (enum command_control_type type, char *args)
73 struct command_line *cmd;
76 error ("if/while commands require arguments.\n");
78 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
80 cmd->control_type = type;
84 = (struct command_line **) xmalloc (sizeof (struct command_line *)
86 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
87 cmd->line = savestring (args, strlen (args));
91 /* Build and return a new command structure for the control commands
92 such as "if" and "while". */
94 static struct command_line *
95 get_command_line (enum command_control_type type, char *arg)
97 struct command_line *cmd;
98 struct cleanup *old_chain = NULL;
100 /* Allocate and build a new command line structure. */
101 cmd = build_command_line (type, arg);
103 old_chain = make_cleanup_free_command_lines (&cmd);
105 /* Read in the body of this command. */
106 if (recurse_read_control_structure (cmd) == invalid_control)
108 warning ("error reading in control structure\n");
109 do_cleanups (old_chain);
113 discard_cleanups (old_chain);
117 /* Recursively print a command (including full control structures). */
120 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
123 struct command_line *list;
130 ui_out_spaces (uiout, 2 * depth);
132 /* A simple command, print it and continue. */
133 if (list->control_type == simple_control)
135 ui_out_field_string (uiout, NULL, list->line);
136 ui_out_text (uiout, "\n");
141 /* loop_continue to jump to the start of a while loop, print it
143 if (list->control_type == continue_control)
145 ui_out_field_string (uiout, NULL, "loop_continue");
146 ui_out_text (uiout, "\n");
151 /* loop_break to break out of a while loop, print it and continue. */
152 if (list->control_type == break_control)
154 ui_out_field_string (uiout, NULL, "loop_break");
155 ui_out_text (uiout, "\n");
160 /* A while command. Recursively print its subcommands and continue. */
161 if (list->control_type == while_control)
163 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
164 ui_out_text (uiout, "\n");
165 print_command_lines (uiout, *list->body_list, depth + 1);
167 ui_out_spaces (uiout, 2 * depth);
168 ui_out_field_string (uiout, NULL, "end");
169 ui_out_text (uiout, "\n");
174 /* An if command. Recursively print both arms before continueing. */
175 if (list->control_type == if_control)
177 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
178 ui_out_text (uiout, "\n");
180 print_command_lines (uiout, list->body_list[0], depth + 1);
182 /* Show the false arm if it exists. */
183 if (list->body_count == 2)
186 ui_out_spaces (uiout, 2 * depth);
187 ui_out_field_string (uiout, NULL, "else");
188 ui_out_text (uiout, "\n");
189 print_command_lines (uiout, list->body_list[1], depth + 1);
193 ui_out_spaces (uiout, 2 * depth);
194 ui_out_field_string (uiout, NULL, "end");
195 ui_out_text (uiout, "\n");
200 /* ignore illegal command type and try next */
205 /* Handle pre-post hooks. */
208 clear_hook_in_cleanup (void *data)
210 struct cmd_list_element *c = data;
211 c->hook_in = 0; /* Allow hook to work again once it is complete */
215 execute_cmd_pre_hook (struct cmd_list_element *c)
217 if ((c->hook_pre) && (!c->hook_in))
219 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
220 c->hook_in = 1; /* Prevent recursive hooking */
221 execute_user_command (c->hook_pre, (char *) 0);
222 do_cleanups (cleanups);
227 execute_cmd_post_hook (struct cmd_list_element *c)
229 if ((c->hook_post) && (!c->hook_in))
231 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
232 c->hook_in = 1; /* Prevent recursive hooking */
233 execute_user_command (c->hook_post, (char *) 0);
234 do_cleanups (cleanups);
238 /* Execute the command in CMD. */
240 do_restore_user_call_depth (void * call_depth)
242 int * depth = call_depth;
243 /* We will be returning_to_top_level() at this point, so we want to
250 execute_user_command (struct cmd_list_element *c, char *args)
252 struct command_line *cmdlines;
253 struct cleanup *old_chain;
254 enum command_control_type ret;
255 static int user_call_depth = 0;
256 extern int max_user_call_depth;
258 old_chain = setup_user_args (args);
260 cmdlines = c->user_commands;
265 if (++user_call_depth > max_user_call_depth)
266 error ("Max user call depth exceeded -- command aborted\n");
268 old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth);
270 /* Set the instream to 0, indicating execution of a
271 user-defined function. */
272 old_chain = make_cleanup (do_restore_instream_cleanup, instream);
273 instream = (FILE *) 0;
276 ret = execute_control_command (cmdlines);
277 if (ret != simple_control && ret != break_control)
279 warning ("Error in control structure.\n");
282 cmdlines = cmdlines->next;
284 do_cleanups (old_chain);
289 enum command_control_type
290 execute_control_command (struct command_line *cmd)
292 struct expression *expr;
293 struct command_line *current;
294 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
296 struct value *val_mark;
298 enum command_control_type ret;
301 /* Start by assuming failure, if a problem is detected, the code
302 below will simply "break" out of the switch. */
303 ret = invalid_control;
305 switch (cmd->control_type)
308 /* A simple command, execute it and return. */
309 new_line = insert_args (cmd->line);
312 make_cleanup (free_current_contents, &new_line);
313 execute_command (new_line, 0);
314 ret = cmd->control_type;
317 case continue_control:
319 /* Return for "continue", and "break" so we can either
320 continue the loop at the top, or break out. */
321 ret = cmd->control_type;
326 /* Parse the loop control expression for the while statement. */
327 new_line = insert_args (cmd->line);
330 make_cleanup (free_current_contents, &new_line);
331 expr = parse_expression (new_line);
332 make_cleanup (free_current_contents, &expr);
334 ret = simple_control;
337 /* Keep iterating so long as the expression is true. */
344 /* Evaluate the expression. */
345 val_mark = value_mark ();
346 val = evaluate_expression (expr);
347 cond_result = value_true (val);
348 value_free_to_mark (val_mark);
350 /* If the value is false, then break out of the loop. */
354 /* Execute the body of the while statement. */
355 current = *cmd->body_list;
358 ret = execute_control_command (current);
360 /* If we got an error, or a "break" command, then stop
362 if (ret == invalid_control || ret == break_control)
368 /* If we got a "continue" command, then restart the loop
370 if (ret == continue_control)
373 /* Get the next statement. */
374 current = current->next;
378 /* Reset RET so that we don't recurse the break all the way down. */
379 if (ret == break_control)
380 ret = simple_control;
387 new_line = insert_args (cmd->line);
390 make_cleanup (free_current_contents, &new_line);
391 /* Parse the conditional for the if statement. */
392 expr = parse_expression (new_line);
393 make_cleanup (free_current_contents, &expr);
396 ret = simple_control;
398 /* Evaluate the conditional. */
399 val_mark = value_mark ();
400 val = evaluate_expression (expr);
402 /* Choose which arm to take commands from based on the value of the
403 conditional expression. */
404 if (value_true (val))
405 current = *cmd->body_list;
406 else if (cmd->body_count == 2)
407 current = *(cmd->body_list + 1);
408 value_free_to_mark (val_mark);
410 /* Execute commands in the given arm. */
413 ret = execute_control_command (current);
415 /* If we got an error, get out. */
416 if (ret != simple_control)
419 /* Get the next statement in the body. */
420 current = current->next;
427 warning ("Invalid control type in command structure.");
431 do_cleanups (old_chain);
436 /* "while" command support. Executes a body of statements while the
437 loop condition is nonzero. */
440 while_command (char *arg, int from_tty)
442 struct command_line *command = NULL;
445 command = get_command_line (while_control, arg);
450 execute_control_command (command);
451 free_command_lines (&command);
454 /* "if" command support. Execute either the true or false arm depending
455 on the value of the if conditional. */
458 if_command (char *arg, int from_tty)
460 struct command_line *command = NULL;
463 command = get_command_line (if_control, arg);
468 execute_control_command (command);
469 free_command_lines (&command);
474 arg_cleanup (void *ignore)
476 struct user_args *oargs = user_args;
478 internal_error (__FILE__, __LINE__,
479 "arg_cleanup called with no user args.\n");
481 user_args = user_args->next;
485 /* Bind the incomming arguments for a user defined command to
486 $arg0, $arg1 ... $argMAXUSERARGS. */
488 static struct cleanup *
489 setup_user_args (char *p)
491 struct user_args *args;
492 struct cleanup *old_chain;
493 unsigned int arg_count = 0;
495 args = (struct user_args *) xmalloc (sizeof (struct user_args));
496 memset (args, 0, sizeof (struct user_args));
498 args->next = user_args;
501 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
513 if (arg_count >= MAXUSERARGS)
515 error ("user defined function may only have %d arguments.\n",
520 /* Strip whitespace. */
521 while (*p == ' ' || *p == '\t')
524 /* P now points to an argument. */
526 user_args->a[arg_count].arg = p;
528 /* Get to the end of this argument. */
531 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
560 user_args->a[arg_count].len = p - start_arg;
567 /* Given character string P, return a point to the first argument ($arg),
568 or NULL if P contains no arguments. */
573 while ((p = strchr (p, '$')))
575 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
582 /* Insert the user defined arguments stored in user_arg into the $arg
583 arguments found in line, with the updated copy being placed into nline. */
586 insert_args (char *line)
588 char *p, *save_line, *new_line;
591 /* First we need to know how much memory to allocate for the new line. */
594 while ((p = locate_arg (line)))
599 if (i >= user_args->count)
601 error ("Missing argument %d in user function.\n", i);
604 len += user_args->a[i].len;
608 /* Don't forget the tail. */
609 len += strlen (line);
611 /* Allocate space for the new line and fill it in. */
612 new_line = (char *) xmalloc (len + 1);
613 if (new_line == NULL)
616 /* Restore pointer to beginning of old line. */
619 /* Save pointer to beginning of new line. */
620 save_line = new_line;
622 while ((p = locate_arg (line)))
626 memcpy (new_line, line, p - line);
627 new_line += p - line;
630 len = user_args->a[i].len;
633 memcpy (new_line, user_args->a[i].arg, len);
638 /* Don't forget the tail. */
639 strcpy (new_line, line);
641 /* Return a pointer to the beginning of the new line. */
646 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
647 code bodies. This is typically used when we encounter an "else"
648 clause for an "if" command. */
651 realloc_body_list (struct command_line *command, int new_length)
654 struct command_line **body_list;
656 n = command->body_count;
662 body_list = (struct command_line **)
663 xmalloc (sizeof (struct command_line *) * new_length);
665 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
667 xfree (command->body_list);
668 command->body_list = body_list;
669 command->body_count = new_length;
672 /* Read one line from the input stream. If the command is an "else" or
673 "end", return such an indication to the caller. */
675 static enum misc_command_type
676 read_next_line (struct command_line **command)
678 char *p, *p1, *prompt_ptr, control_prompt[256];
681 if (control_level >= 254)
682 error ("Control nesting too deep!\n");
684 /* Set a prompt based on the nesting of the control commands. */
685 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
687 for (i = 0; i < control_level; i++)
688 control_prompt[i] = ' ';
689 control_prompt[i] = '>';
690 control_prompt[i + 1] = '\0';
691 prompt_ptr = (char *) &control_prompt[0];
696 p = command_line_input (prompt_ptr, instream == stdin, "commands");
698 /* Not sure what to do here. */
702 /* Strip leading and trailing whitespace. */
703 while (*p == ' ' || *p == '\t')
707 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
710 /* Blanks and comments don't really do anything, but we need to
711 distinguish them from else, end and other commands which can be
713 if (p1 == p || p[0] == '#')
716 /* Is this the end of a simple, while, or if control structure? */
717 if (p1 - p == 3 && !strncmp (p, "end", 3))
720 /* Is the else clause of an if control structure? */
721 if (p1 - p == 4 && !strncmp (p, "else", 4))
724 /* Check for while, if, break, continue, etc and build a new command
725 line structure for them. */
726 if (p1 - p > 5 && !strncmp (p, "while", 5))
730 while (first_arg < p1 && isspace (*first_arg))
732 *command = build_command_line (while_control, first_arg);
734 else if (p1 - p > 2 && !strncmp (p, "if", 2))
738 while (first_arg < p1 && isspace (*first_arg))
740 *command = build_command_line (if_control, first_arg);
742 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
744 *command = (struct command_line *)
745 xmalloc (sizeof (struct command_line));
746 (*command)->next = NULL;
747 (*command)->line = NULL;
748 (*command)->control_type = break_control;
749 (*command)->body_count = 0;
750 (*command)->body_list = NULL;
752 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
754 *command = (struct command_line *)
755 xmalloc (sizeof (struct command_line));
756 (*command)->next = NULL;
757 (*command)->line = NULL;
758 (*command)->control_type = continue_control;
759 (*command)->body_count = 0;
760 (*command)->body_list = NULL;
764 /* A normal command. */
765 *command = (struct command_line *)
766 xmalloc (sizeof (struct command_line));
767 (*command)->next = NULL;
768 (*command)->line = savestring (p, p1 - p);
769 (*command)->control_type = simple_control;
770 (*command)->body_count = 0;
771 (*command)->body_list = NULL;
774 /* Nothing special. */
778 /* Recursively read in the control structures and create a command_line
781 The parent_control parameter is the control structure in which the
782 following commands are nested. */
784 static enum command_control_type
785 recurse_read_control_structure (struct command_line *current_cmd)
788 enum misc_command_type val;
789 enum command_control_type ret;
790 struct command_line **body_ptr, *child_tail, *next;
796 if (current_cmd->control_type == simple_control)
798 error ("Recursed on a simple control type\n");
799 return invalid_control;
802 if (current_body > current_cmd->body_count)
804 error ("Allocated body is smaller than this command type needs\n");
805 return invalid_control;
808 /* Read lines from the input stream and build control structures. */
814 val = read_next_line (&next);
816 /* Just skip blanks and comments. */
817 if (val == nop_command)
820 if (val == end_command)
822 if (current_cmd->control_type == while_control
823 || current_cmd->control_type == if_control)
825 /* Success reading an entire control structure. */
826 ret = simple_control;
831 ret = invalid_control;
836 /* Not the end of a control structure. */
837 if (val == else_command)
839 if (current_cmd->control_type == if_control
840 && current_body == 1)
842 realloc_body_list (current_cmd, 2);
849 ret = invalid_control;
856 child_tail->next = next;
860 body_ptr = current_cmd->body_list;
861 for (i = 1; i < current_body; i++)
870 /* If the latest line is another control structure, then recurse
872 if (next->control_type == while_control
873 || next->control_type == if_control)
876 ret = recurse_read_control_structure (next);
879 if (ret != simple_control)
889 /* Read lines from the input stream and accumulate them in a chain of
890 struct command_line's, which is then returned. For input from a
891 terminal, the special command "end" is used to mark the end of the
892 input, and is not included in the returned chain of commands. */
894 #define END_MESSAGE "End with a line saying just \"end\"."
896 struct command_line *
897 read_command_lines (char *prompt_arg, int from_tty)
899 struct command_line *head, *tail, *next;
900 struct cleanup *old_chain;
901 enum command_control_type ret;
902 enum misc_command_type val;
905 if (deprecated_readline_begin_hook)
907 /* Note - intentional to merge messages with no newline */
908 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
910 else if (from_tty && input_from_terminal_p ())
912 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
913 gdb_flush (gdb_stdout);
921 val = read_next_line (&next);
923 /* Ignore blank lines or comments. */
924 if (val == nop_command)
927 if (val == end_command)
929 ret = simple_control;
933 if (val != ok_command)
935 ret = invalid_control;
939 if (next->control_type == while_control
940 || next->control_type == if_control)
943 ret = recurse_read_control_structure (next);
946 if (ret == invalid_control)
957 old_chain = make_cleanup_free_command_lines (&head);
966 if (ret != invalid_control)
968 discard_cleanups (old_chain);
971 do_cleanups (old_chain);
974 if (deprecated_readline_end_hook)
976 (*deprecated_readline_end_hook) ();
981 /* Free a chain of struct command_line's. */
984 free_command_lines (struct command_line **lptr)
986 struct command_line *l = *lptr;
987 struct command_line *next;
988 struct command_line **blist;
993 if (l->body_count > 0)
995 blist = l->body_list;
996 for (i = 0; i < l->body_count; i++, blist++)
997 free_command_lines (blist);
1008 do_free_command_lines_cleanup (void *arg)
1010 free_command_lines (arg);
1014 make_cleanup_free_command_lines (struct command_line **arg)
1016 return make_cleanup (do_free_command_lines_cleanup, arg);
1019 struct command_line *
1020 copy_command_lines (struct command_line *cmds)
1022 struct command_line *result = NULL;
1026 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1028 result->next = copy_command_lines (cmds->next);
1029 result->line = xstrdup (cmds->line);
1030 result->control_type = cmds->control_type;
1031 result->body_count = cmds->body_count;
1032 if (cmds->body_count > 0)
1036 result->body_list = (struct command_line **)
1037 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1039 for (i = 0; i < cmds->body_count; i++)
1040 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1043 result->body_list = NULL;
1050 validate_comname (char *comname)
1055 error_no_arg ("name of command to define");
1060 if (!isalnum (*p) && *p != '-' && *p != '_')
1061 error ("Junk in argument list: \"%s\"", p);
1066 /* This is just a placeholder in the command data structures. */
1068 user_defined_command (char *ignore, int from_tty)
1073 define_command (char *comname, int from_tty)
1075 #define MAX_TMPBUF 128
1082 struct command_line *cmds;
1083 struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1084 char *tem = comname;
1086 char tmpbuf[MAX_TMPBUF];
1087 int hook_type = CMD_NO_HOOK;
1088 int hook_name_size = 0;
1090 #define HOOK_STRING "hook-"
1092 #define HOOK_POST_STRING "hookpost-"
1093 #define HOOK_POST_LEN 9
1095 validate_comname (comname);
1097 /* Look it up, and verify that we got an exact match. */
1098 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1099 if (c && strcmp (comname, c->name) != 0)
1105 if (c->class == class_user || c->class == class_alias)
1106 q = query ("Redefine command \"%s\"? ", c->name);
1108 q = query ("Really redefine built-in command \"%s\"? ", c->name);
1110 error ("Command \"%s\" not redefined.", c->name);
1113 /* If this new command is a hook, then mark the command which it
1114 is hooking. Note that we allow hooking `help' commands, so that
1115 we can hook the `stop' pseudo-command. */
1117 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1119 hook_type = CMD_PRE_HOOK;
1120 hook_name_size = HOOK_LEN;
1122 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1124 hook_type = CMD_POST_HOOK;
1125 hook_name_size = HOOK_POST_LEN;
1128 if (hook_type != CMD_NO_HOOK)
1130 /* Look up cmd it hooks, and verify that we got an exact match. */
1131 tem = comname + hook_name_size;
1132 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1133 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1137 warning ("Your new `%s' command does not hook any existing command.",
1139 if (!query ("Proceed? "))
1140 error ("Not confirmed.");
1144 comname = savestring (comname, strlen (comname));
1146 /* If the rest of the commands will be case insensitive, this one
1147 should behave in the same manner. */
1148 for (tem = comname; *tem; tem++)
1150 *tem = tolower (*tem);
1152 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1153 cmds = read_command_lines (tmpbuf, from_tty);
1155 if (c && c->class == class_user)
1156 free_command_lines (&c->user_commands);
1158 newc = add_cmd (comname, class_user, user_defined_command,
1159 (c && c->class == class_user)
1160 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1161 newc->user_commands = cmds;
1163 /* If this new command is a hook, then mark both commands as being
1170 hookc->hook_pre = newc; /* Target gets hooked. */
1171 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1174 hookc->hook_post = newc; /* Target gets hooked. */
1175 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1178 /* Should never come here as hookc would be 0. */
1179 internal_error (__FILE__, __LINE__, "bad switch");
1185 document_command (char *comname, int from_tty)
1187 struct command_line *doclines;
1188 struct cmd_list_element *c;
1189 char *tem = comname;
1192 validate_comname (comname);
1194 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1196 if (c->class != class_user)
1197 error ("Command \"%s\" is built-in.", comname);
1199 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1200 doclines = read_command_lines (tmpbuf, from_tty);
1206 struct command_line *cl1;
1209 for (cl1 = doclines; cl1; cl1 = cl1->next)
1210 len += strlen (cl1->line) + 1;
1212 c->doc = (char *) xmalloc (len + 1);
1215 for (cl1 = doclines; cl1; cl1 = cl1->next)
1217 strcat (c->doc, cl1->line);
1219 strcat (c->doc, "\n");
1223 free_command_lines (&doclines);
1226 struct source_cleanup_lines_args
1233 source_cleanup_lines (void *args)
1235 struct source_cleanup_lines_args *p =
1236 (struct source_cleanup_lines_args *) args;
1237 source_line_number = p->old_line;
1238 source_file_name = p->old_file;
1242 do_fclose_cleanup (void *stream)
1247 struct wrapped_read_command_file_args
1253 wrapped_read_command_file (struct ui_out *uiout, void *data)
1255 struct wrapped_read_command_file_args *args = data;
1256 read_command_file (args->stream);
1259 /* Used to implement source_command */
1262 script_from_file (FILE *stream, char *file)
1264 struct cleanup *old_cleanups;
1265 struct source_cleanup_lines_args old_lines;
1270 internal_error (__FILE__, __LINE__, "called with NULL file pointer!");
1273 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1275 old_lines.old_line = source_line_number;
1276 old_lines.old_file = source_file_name;
1277 make_cleanup (source_cleanup_lines, &old_lines);
1278 source_line_number = 0;
1279 source_file_name = file;
1280 /* This will get set every time we read a line. So it won't stay "" for
1282 error_pre_print = "";
1286 struct wrapped_read_command_file_args args;
1287 args.stream = stream;
1288 e = catch_exception (uiout, wrapped_read_command_file, &args,
1295 /* Re-throw the error, but with the file name information
1297 throw_error (e.error, "%s:%d: Error in sourced command file:\n%s",
1298 source_file_name, source_line_number, e.message);
1300 internal_error (__FILE__, __LINE__, "bad reason");
1304 do_cleanups (old_cleanups);
1308 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1310 struct command_line *cmdlines;
1312 cmdlines = c->user_commands;
1315 fputs_filtered ("User command ", stream);
1316 fputs_filtered (c->name, stream);
1317 fputs_filtered (":\n", stream);
1319 print_command_lines (uiout, cmdlines, 1);
1320 fputs_filtered ("\n", stream);