1 /* GDB CLI command scripting.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "language.h" /* For value_true */
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-script.h"
38 extern void dont_repeat (void);
40 extern void do_restore_instream_cleanup (void *stream);
42 /* Prototypes for local functions */
44 static struct cleanup *
45 make_cleanup_free_command_lines (struct command_line **arg);
47 static enum command_control_type
48 recurse_read_control_structure (struct command_line *current_cmd);
50 static char *insert_args (char *line);
52 static struct cleanup * setup_user_args (char *p);
54 static void validate_comname (char *);
56 /* Level of control structure. */
57 static int control_level;
59 /* Source command state variable. */
60 static int source_error_allocated;
62 /* Structure for arguments to user defined functions. */
63 #define MAXUSERARGS 10
66 struct user_args *next;
78 /* Allocate, initialize a new command line structure for one of the
79 control commands (if/while). */
81 static struct command_line *
82 build_command_line (enum command_control_type type, char *args)
84 struct command_line *cmd;
87 error ("if/while commands require arguments.\n");
89 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
91 cmd->control_type = type;
95 = (struct command_line **) xmalloc (sizeof (struct command_line *)
97 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
98 cmd->line = savestring (args, strlen (args));
102 /* Build and return a new command structure for the control commands
103 such as "if" and "while". */
105 static struct command_line *
106 get_command_line (enum command_control_type type, char *arg)
108 struct command_line *cmd;
109 struct cleanup *old_chain = NULL;
111 /* Allocate and build a new command line structure. */
112 cmd = build_command_line (type, arg);
114 old_chain = make_cleanup_free_command_lines (&cmd);
116 /* Read in the body of this command. */
117 if (recurse_read_control_structure (cmd) == invalid_control)
119 warning ("error reading in control structure\n");
120 do_cleanups (old_chain);
124 discard_cleanups (old_chain);
128 /* Recursively print a command (including full control structures). */
131 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
134 struct command_line *list;
141 ui_out_spaces (uiout, 2 * depth);
143 /* A simple command, print it and continue. */
144 if (list->control_type == simple_control)
146 ui_out_field_string (uiout, NULL, list->line);
147 ui_out_text (uiout, "\n");
152 /* loop_continue to jump to the start of a while loop, print it
154 if (list->control_type == continue_control)
156 ui_out_field_string (uiout, NULL, "loop_continue");
157 ui_out_text (uiout, "\n");
162 /* loop_break to break out of a while loop, print it and continue. */
163 if (list->control_type == break_control)
165 ui_out_field_string (uiout, NULL, "loop_break");
166 ui_out_text (uiout, "\n");
171 /* A while command. Recursively print its subcommands and continue. */
172 if (list->control_type == while_control)
174 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
175 ui_out_text (uiout, "\n");
176 print_command_lines (uiout, *list->body_list, depth + 1);
178 ui_out_spaces (uiout, 2 * depth);
179 ui_out_field_string (uiout, NULL, "end");
180 ui_out_text (uiout, "\n");
185 /* An if command. Recursively print both arms before continueing. */
186 if (list->control_type == if_control)
188 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
189 ui_out_text (uiout, "\n");
191 print_command_lines (uiout, list->body_list[0], depth + 1);
193 /* Show the false arm if it exists. */
194 if (list->body_count == 2)
197 ui_out_spaces (uiout, 2 * depth);
198 ui_out_field_string (uiout, NULL, "else");
199 ui_out_text (uiout, "\n");
200 print_command_lines (uiout, list->body_list[1], depth + 1);
204 ui_out_spaces (uiout, 2 * depth);
205 ui_out_field_string (uiout, NULL, "end");
206 ui_out_text (uiout, "\n");
211 /* ignore illegal command type and try next */
217 print_command_line (struct command_line *cmd, unsigned int depth,
218 struct ui_file *stream)
224 for (i = 0; i < depth; i++)
225 fputs_filtered (" ", stream);
228 /* A simple command, print it and return. */
229 if (cmd->control_type == simple_control)
231 fputs_filtered (cmd->line, stream);
232 fputs_filtered ("\n", stream);
236 /* loop_continue to jump to the start of a while loop, print it
238 if (cmd->control_type == continue_control)
240 fputs_filtered ("loop_continue\n", stream);
244 /* loop_break to break out of a while loop, print it and return. */
245 if (cmd->control_type == break_control)
247 fputs_filtered ("loop_break\n", stream);
251 /* A while command. Recursively print its subcommands before returning. */
252 if (cmd->control_type == while_control)
254 struct command_line *list;
255 fputs_filtered ("while ", stream);
256 fputs_filtered (cmd->line, stream);
257 fputs_filtered ("\n", stream);
258 list = *cmd->body_list;
261 print_command_line (list, depth + 1, stream);
266 /* An if command. Recursively print both arms before returning. */
267 if (cmd->control_type == if_control)
269 fputs_filtered ("if ", stream);
270 fputs_filtered (cmd->line, stream);
271 fputs_filtered ("\n", stream);
273 print_command_line (cmd->body_list[0], depth + 1, stream);
275 /* Show the false arm if it exists. */
276 if (cmd->body_count == 2)
280 for (i = 0; i < depth; i++)
281 fputs_filtered (" ", stream);
283 fputs_filtered ("else\n", stream);
284 print_command_line (cmd->body_list[1], depth + 1, stream);
288 for (i = 0; i < depth; i++)
289 fputs_filtered (" ", stream);
291 fputs_filtered ("end\n", stream);
296 /* Execute the command in CMD. */
299 execute_user_command (struct cmd_list_element *c, char *args)
301 register struct command_line *cmdlines;
302 struct cleanup *old_chain;
303 enum command_control_type ret;
305 old_chain = setup_user_args (args);
307 cmdlines = c->user_commands;
312 /* Set the instream to 0, indicating execution of a
313 user-defined function. */
314 old_chain = make_cleanup (do_restore_instream_cleanup, instream);
315 instream = (FILE *) 0;
318 ret = execute_control_command (cmdlines);
319 if (ret != simple_control && ret != break_control)
321 warning ("Error in control structure.\n");
324 cmdlines = cmdlines->next;
326 do_cleanups (old_chain);
329 enum command_control_type
330 execute_control_command (struct command_line *cmd)
332 struct expression *expr;
333 struct command_line *current;
334 struct cleanup *old_chain = 0;
338 enum command_control_type ret;
341 switch (cmd->control_type)
344 /* A simple command, execute it and return. */
345 new_line = insert_args (cmd->line);
347 return invalid_control;
348 old_chain = make_cleanup (free_current_contents, &new_line);
349 execute_command (new_line, 0);
350 ret = cmd->control_type;
353 case continue_control:
355 /* Return for "continue", and "break" so we can either
356 continue the loop at the top, or break out. */
357 ret = cmd->control_type;
362 /* Parse the loop control expression for the while statement. */
363 new_line = insert_args (cmd->line);
365 return invalid_control;
366 old_chain = make_cleanup (free_current_contents, &new_line);
367 expr = parse_expression (new_line);
368 make_cleanup (free_current_contents, &expr);
370 ret = simple_control;
373 /* Keep iterating so long as the expression is true. */
380 /* Evaluate the expression. */
381 val_mark = value_mark ();
382 val = evaluate_expression (expr);
383 cond_result = value_true (val);
384 value_free_to_mark (val_mark);
386 /* If the value is false, then break out of the loop. */
390 /* Execute the body of the while statement. */
391 current = *cmd->body_list;
394 ret = execute_control_command (current);
396 /* If we got an error, or a "break" command, then stop
398 if (ret == invalid_control || ret == break_control)
404 /* If we got a "continue" command, then restart the loop
406 if (ret == continue_control)
409 /* Get the next statement. */
410 current = current->next;
414 /* Reset RET so that we don't recurse the break all the way down. */
415 if (ret == break_control)
416 ret = simple_control;
423 new_line = insert_args (cmd->line);
425 return invalid_control;
426 old_chain = make_cleanup (free_current_contents, &new_line);
427 /* Parse the conditional for the if statement. */
428 expr = parse_expression (new_line);
429 make_cleanup (free_current_contents, &expr);
432 ret = simple_control;
434 /* Evaluate the conditional. */
435 val_mark = value_mark ();
436 val = evaluate_expression (expr);
438 /* Choose which arm to take commands from based on the value of the
439 conditional expression. */
440 if (value_true (val))
441 current = *cmd->body_list;
442 else if (cmd->body_count == 2)
443 current = *(cmd->body_list + 1);
444 value_free_to_mark (val_mark);
446 /* Execute commands in the given arm. */
449 ret = execute_control_command (current);
451 /* If we got an error, get out. */
452 if (ret != simple_control)
455 /* Get the next statement in the body. */
456 current = current->next;
463 warning ("Invalid control type in command structure.");
464 return invalid_control;
468 do_cleanups (old_chain);
473 /* "while" command support. Executes a body of statements while the
474 loop condition is nonzero. */
477 while_command (char *arg, int from_tty)
479 struct command_line *command = NULL;
482 command = get_command_line (while_control, arg);
487 execute_control_command (command);
488 free_command_lines (&command);
491 /* "if" command support. Execute either the true or false arm depending
492 on the value of the if conditional. */
495 if_command (char *arg, int from_tty)
497 struct command_line *command = NULL;
500 command = get_command_line (if_control, arg);
505 execute_control_command (command);
506 free_command_lines (&command);
511 arg_cleanup (void *ignore)
513 struct user_args *oargs = user_args;
515 internal_error (__FILE__, __LINE__,
516 "arg_cleanup called with no user args.\n");
518 user_args = user_args->next;
522 /* Bind the incomming arguments for a user defined command to
523 $arg0, $arg1 ... $argMAXUSERARGS. */
525 static struct cleanup *
526 setup_user_args (char *p)
528 struct user_args *args;
529 struct cleanup *old_chain;
530 unsigned int arg_count = 0;
532 args = (struct user_args *) xmalloc (sizeof (struct user_args));
533 memset (args, 0, sizeof (struct user_args));
535 args->next = user_args;
538 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
550 if (arg_count >= MAXUSERARGS)
552 error ("user defined function may only have %d arguments.\n",
557 /* Strip whitespace. */
558 while (*p == ' ' || *p == '\t')
561 /* P now points to an argument. */
563 user_args->a[arg_count].arg = p;
565 /* Get to the end of this argument. */
568 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
597 user_args->a[arg_count].len = p - start_arg;
604 /* Given character string P, return a point to the first argument ($arg),
605 or NULL if P contains no arguments. */
610 while ((p = strchr (p, '$')))
612 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
619 /* Insert the user defined arguments stored in user_arg into the $arg
620 arguments found in line, with the updated copy being placed into nline. */
623 insert_args (char *line)
625 char *p, *save_line, *new_line;
628 /* First we need to know how much memory to allocate for the new line. */
631 while ((p = locate_arg (line)))
636 if (i >= user_args->count)
638 error ("Missing argument %d in user function.\n", i);
641 len += user_args->a[i].len;
645 /* Don't forget the tail. */
646 len += strlen (line);
648 /* Allocate space for the new line and fill it in. */
649 new_line = (char *) xmalloc (len + 1);
650 if (new_line == NULL)
653 /* Restore pointer to beginning of old line. */
656 /* Save pointer to beginning of new line. */
657 save_line = new_line;
659 while ((p = locate_arg (line)))
663 memcpy (new_line, line, p - line);
664 new_line += p - line;
667 len = user_args->a[i].len;
670 memcpy (new_line, user_args->a[i].arg, len);
675 /* Don't forget the tail. */
676 strcpy (new_line, line);
678 /* Return a pointer to the beginning of the new line. */
683 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
684 code bodies. This is typically used when we encounter an "else"
685 clause for an "if" command. */
688 realloc_body_list (struct command_line *command, int new_length)
691 struct command_line **body_list;
693 n = command->body_count;
699 body_list = (struct command_line **)
700 xmalloc (sizeof (struct command_line *) * new_length);
702 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
704 xfree (command->body_list);
705 command->body_list = body_list;
706 command->body_count = new_length;
709 /* Read one line from the input stream. If the command is an "else" or
710 "end", return such an indication to the caller. */
712 static enum misc_command_type
713 read_next_line (struct command_line **command)
715 char *p, *p1, *prompt_ptr, control_prompt[256];
718 if (control_level >= 254)
719 error ("Control nesting too deep!\n");
721 /* Set a prompt based on the nesting of the control commands. */
722 if (instream == stdin || (instream == 0 && readline_hook != NULL))
724 for (i = 0; i < control_level; i++)
725 control_prompt[i] = ' ';
726 control_prompt[i] = '>';
727 control_prompt[i + 1] = '\0';
728 prompt_ptr = (char *) &control_prompt[0];
733 p = command_line_input (prompt_ptr, instream == stdin, "commands");
735 /* Not sure what to do here. */
739 /* Strip leading and trailing whitespace. */
740 while (*p == ' ' || *p == '\t')
744 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
747 /* Blanks and comments don't really do anything, but we need to
748 distinguish them from else, end and other commands which can be
750 if (p1 == p || p[0] == '#')
753 /* Is this the end of a simple, while, or if control structure? */
754 if (p1 - p == 3 && !strncmp (p, "end", 3))
757 /* Is the else clause of an if control structure? */
758 if (p1 - p == 4 && !strncmp (p, "else", 4))
761 /* Check for while, if, break, continue, etc and build a new command
762 line structure for them. */
763 if (p1 - p > 5 && !strncmp (p, "while", 5))
764 *command = build_command_line (while_control, p + 6);
765 else if (p1 - p > 2 && !strncmp (p, "if", 2))
766 *command = build_command_line (if_control, p + 3);
767 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
769 *command = (struct command_line *)
770 xmalloc (sizeof (struct command_line));
771 (*command)->next = NULL;
772 (*command)->line = NULL;
773 (*command)->control_type = break_control;
774 (*command)->body_count = 0;
775 (*command)->body_list = NULL;
777 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
779 *command = (struct command_line *)
780 xmalloc (sizeof (struct command_line));
781 (*command)->next = NULL;
782 (*command)->line = NULL;
783 (*command)->control_type = continue_control;
784 (*command)->body_count = 0;
785 (*command)->body_list = NULL;
789 /* A normal command. */
790 *command = (struct command_line *)
791 xmalloc (sizeof (struct command_line));
792 (*command)->next = NULL;
793 (*command)->line = savestring (p, p1 - p);
794 (*command)->control_type = simple_control;
795 (*command)->body_count = 0;
796 (*command)->body_list = NULL;
799 /* Nothing special. */
803 /* Recursively read in the control structures and create a command_line
806 The parent_control parameter is the control structure in which the
807 following commands are nested. */
809 static enum command_control_type
810 recurse_read_control_structure (struct command_line *current_cmd)
813 enum misc_command_type val;
814 enum command_control_type ret;
815 struct command_line **body_ptr, *child_tail, *next;
821 if (current_cmd->control_type == simple_control)
823 error ("Recursed on a simple control type\n");
824 return invalid_control;
827 if (current_body > current_cmd->body_count)
829 error ("Allocated body is smaller than this command type needs\n");
830 return invalid_control;
833 /* Read lines from the input stream and build control structures. */
839 val = read_next_line (&next);
841 /* Just skip blanks and comments. */
842 if (val == nop_command)
845 if (val == end_command)
847 if (current_cmd->control_type == while_control
848 || current_cmd->control_type == if_control)
850 /* Success reading an entire control structure. */
851 ret = simple_control;
856 ret = invalid_control;
861 /* Not the end of a control structure. */
862 if (val == else_command)
864 if (current_cmd->control_type == if_control
865 && current_body == 1)
867 realloc_body_list (current_cmd, 2);
874 ret = invalid_control;
881 child_tail->next = next;
885 body_ptr = current_cmd->body_list;
886 for (i = 1; i < current_body; i++)
895 /* If the latest line is another control structure, then recurse
897 if (next->control_type == while_control
898 || next->control_type == if_control)
901 ret = recurse_read_control_structure (next);
904 if (ret != simple_control)
914 /* Read lines from the input stream and accumulate them in a chain of
915 struct command_line's, which is then returned. For input from a
916 terminal, the special command "end" is used to mark the end of the
917 input, and is not included in the returned chain of commands. */
919 #define END_MESSAGE "End with a line saying just \"end\"."
921 struct command_line *
922 read_command_lines (char *prompt_arg, int from_tty)
924 struct command_line *head, *tail, *next;
925 struct cleanup *old_chain;
926 enum command_control_type ret;
927 enum misc_command_type val;
930 if (readline_begin_hook)
932 /* Note - intentional to merge messages with no newline */
933 (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
935 else if (from_tty && input_from_terminal_p ())
937 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
938 gdb_flush (gdb_stdout);
946 val = read_next_line (&next);
948 /* Ignore blank lines or comments. */
949 if (val == nop_command)
952 if (val == end_command)
954 ret = simple_control;
958 if (val != ok_command)
960 ret = invalid_control;
964 if (next->control_type == while_control
965 || next->control_type == if_control)
968 ret = recurse_read_control_structure (next);
971 if (ret == invalid_control)
982 old_chain = make_cleanup_free_command_lines (&head);
991 if (ret != invalid_control)
993 discard_cleanups (old_chain);
996 do_cleanups (old_chain);
999 if (readline_end_hook)
1001 (*readline_end_hook) ();
1006 /* Free a chain of struct command_line's. */
1009 free_command_lines (struct command_line **lptr)
1011 register struct command_line *l = *lptr;
1012 register struct command_line *next;
1013 struct command_line **blist;
1018 if (l->body_count > 0)
1020 blist = l->body_list;
1021 for (i = 0; i < l->body_count; i++, blist++)
1022 free_command_lines (blist);
1033 do_free_command_lines_cleanup (void *arg)
1035 free_command_lines (arg);
1038 static struct cleanup *
1039 make_cleanup_free_command_lines (struct command_line **arg)
1041 return make_cleanup (do_free_command_lines_cleanup, arg);
1045 validate_comname (char *comname)
1050 error_no_arg ("name of command to define");
1055 if (!isalnum (*p) && *p != '-' && *p != '_')
1056 error ("Junk in argument list: \"%s\"", p);
1061 /* This is just a placeholder in the command data structures. */
1063 user_defined_command (char *ignore, int from_tty)
1068 define_command (char *comname, int from_tty)
1070 #define MAX_TMPBUF 128
1077 register struct command_line *cmds;
1078 register struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1079 char *tem = comname;
1081 char tmpbuf[MAX_TMPBUF];
1082 int hook_type = CMD_NO_HOOK;
1083 int hook_name_size = 0;
1085 #define HOOK_STRING "hook-"
1087 #define HOOK_POST_STRING "hookpost-"
1088 #define HOOK_POST_LEN 9
1090 validate_comname (comname);
1092 /* Look it up, and verify that we got an exact match. */
1093 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1094 if (c && !STREQ (comname, c->name))
1099 if (c->class == class_user || c->class == class_alias)
1100 tem = "Redefine command \"%s\"? ";
1102 tem = "Really redefine built-in command \"%s\"? ";
1103 if (!query (tem, c->name))
1104 error ("Command \"%s\" not redefined.", c->name);
1107 /* If this new command is a hook, then mark the command which it
1108 is hooking. Note that we allow hooking `help' commands, so that
1109 we can hook the `stop' pseudo-command. */
1111 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1113 hook_type = CMD_PRE_HOOK;
1114 hook_name_size = HOOK_LEN;
1116 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1118 hook_type = CMD_POST_HOOK;
1119 hook_name_size = HOOK_POST_LEN;
1122 if (hook_type != CMD_NO_HOOK)
1124 /* Look up cmd it hooks, and verify that we got an exact match. */
1125 tem = comname + hook_name_size;
1126 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1127 if (hookc && !STREQ (comname + hook_name_size, hookc->name))
1131 warning ("Your new `%s' command does not hook any existing command.",
1133 if (!query ("Proceed? "))
1134 error ("Not confirmed.");
1138 comname = savestring (comname, strlen (comname));
1140 /* If the rest of the commands will be case insensitive, this one
1141 should behave in the same manner. */
1142 for (tem = comname; *tem; tem++)
1144 *tem = tolower (*tem);
1146 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1147 cmds = read_command_lines (tmpbuf, from_tty);
1149 if (c && c->class == class_user)
1150 free_command_lines (&c->user_commands);
1152 newc = add_cmd (comname, class_user, user_defined_command,
1153 (c && c->class == class_user)
1154 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1155 newc->user_commands = cmds;
1157 /* If this new command is a hook, then mark both commands as being
1164 hookc->hook_pre = newc; /* Target gets hooked. */
1165 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1168 hookc->hook_post = newc; /* Target gets hooked. */
1169 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1172 /* Should never come here as hookc would be 0. */
1173 internal_error (__FILE__, __LINE__, "bad switch");
1179 document_command (char *comname, int from_tty)
1181 struct command_line *doclines;
1182 register struct cmd_list_element *c;
1183 char *tem = comname;
1186 validate_comname (comname);
1188 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1190 if (c->class != class_user)
1191 error ("Command \"%s\" is built-in.", comname);
1193 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1194 doclines = read_command_lines (tmpbuf, from_tty);
1200 register struct command_line *cl1;
1201 register int len = 0;
1203 for (cl1 = doclines; cl1; cl1 = cl1->next)
1204 len += strlen (cl1->line) + 1;
1206 c->doc = (char *) xmalloc (len + 1);
1209 for (cl1 = doclines; cl1; cl1 = cl1->next)
1211 strcat (c->doc, cl1->line);
1213 strcat (c->doc, "\n");
1217 free_command_lines (&doclines);
1220 struct source_cleanup_lines_args
1224 char *old_pre_error;
1225 char *old_error_pre_print;
1229 source_cleanup_lines (PTR args)
1231 struct source_cleanup_lines_args *p =
1232 (struct source_cleanup_lines_args *) args;
1233 source_line_number = p->old_line;
1234 source_file_name = p->old_file;
1235 source_pre_error = p->old_pre_error;
1236 error_pre_print = p->old_error_pre_print;
1241 do_fclose_cleanup (void *stream)
1246 /* Used to implement source_command */
1249 script_from_file (FILE *stream, char *file)
1251 struct cleanup *old_cleanups;
1252 struct source_cleanup_lines_args old_lines;
1257 internal_error (__FILE__, __LINE__, "called with NULL file pointer!");
1260 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1262 old_lines.old_line = source_line_number;
1263 old_lines.old_file = source_file_name;
1264 old_lines.old_pre_error = source_pre_error;
1265 old_lines.old_error_pre_print = error_pre_print;
1266 make_cleanup (source_cleanup_lines, &old_lines);
1267 source_line_number = 0;
1268 source_file_name = file;
1269 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
1270 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
1271 make_cleanup (xfree, source_pre_error);
1272 /* This will get set every time we read a line. So it won't stay "" for
1274 error_pre_print = "";
1276 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
1277 if (source_error_allocated < needed_length)
1279 source_error_allocated *= 2;
1280 if (source_error_allocated < needed_length)
1281 source_error_allocated = needed_length;
1282 if (source_error == NULL)
1283 source_error = xmalloc (source_error_allocated);
1285 source_error = xrealloc (source_error, source_error_allocated);
1288 read_command_file (stream);
1290 do_cleanups (old_cleanups);
1294 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1296 register struct command_line *cmdlines;
1298 cmdlines = c->user_commands;
1301 fputs_filtered ("User command ", stream);
1302 fputs_filtered (c->name, stream);
1303 fputs_filtered (":\n", stream);
1306 print_command_lines (uiout, cmdlines, 1);
1307 fputs_filtered ("\n", stream);
1311 print_command_line (cmdlines, 4, stream);
1312 cmdlines = cmdlines->next;
1314 fputs_filtered ("\n", stream);