1 /* GDB CLI command scripting.
2 Copyright 1986-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "language.h" /* For value_true */
31 #include "cli/cli-cmds.h"
32 #include "cli/cli-decode.h"
33 #include "cli/cli-script.h"
37 extern void dont_repeat (void);
39 extern void do_restore_instream_cleanup (void *stream);
41 /* Prototypes for local functions */
43 static struct cleanup *
44 make_cleanup_free_command_lines (struct command_line **arg);
46 static enum command_control_type
47 recurse_read_control_structure (struct command_line *current_cmd);
49 static char *insert_args (char *line);
51 static struct cleanup * setup_user_args (char *p);
53 static void validate_comname (char *);
55 /* Level of control structure. */
56 static int control_level;
58 /* Source command state variable. */
59 static int source_error_allocated;
61 /* Structure for arguments to user defined functions. */
62 #define MAXUSERARGS 10
65 struct user_args *next;
77 /* Allocate, initialize a new command line structure for one of the
78 control commands (if/while). */
80 static struct command_line *
81 build_command_line (enum command_control_type type, char *args)
83 struct command_line *cmd;
86 error ("if/while commands require arguments.\n");
88 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
90 cmd->control_type = type;
94 = (struct command_line **) xmalloc (sizeof (struct command_line *)
96 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
97 cmd->line = savestring (args, strlen (args));
101 /* Build and return a new command structure for the control commands
102 such as "if" and "while". */
104 static struct command_line *
105 get_command_line (enum command_control_type type, char *arg)
107 struct command_line *cmd;
108 struct cleanup *old_chain = NULL;
110 /* Allocate and build a new command line structure. */
111 cmd = build_command_line (type, arg);
113 old_chain = make_cleanup_free_command_lines (&cmd);
115 /* Read in the body of this command. */
116 if (recurse_read_control_structure (cmd) == invalid_control)
118 warning ("error reading in control structure\n");
119 do_cleanups (old_chain);
123 discard_cleanups (old_chain);
127 /* Recursively print a command (including full control structures). */
130 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
133 struct command_line *list;
140 ui_out_spaces (uiout, 2 * depth);
142 /* A simple command, print it and continue. */
143 if (list->control_type == simple_control)
145 ui_out_field_string (uiout, NULL, list->line);
146 ui_out_text (uiout, "\n");
151 /* loop_continue to jump to the start of a while loop, print it
153 if (list->control_type == continue_control)
155 ui_out_field_string (uiout, NULL, "loop_continue");
156 ui_out_text (uiout, "\n");
161 /* loop_break to break out of a while loop, print it and continue. */
162 if (list->control_type == break_control)
164 ui_out_field_string (uiout, NULL, "loop_break");
165 ui_out_text (uiout, "\n");
170 /* A while command. Recursively print its subcommands and continue. */
171 if (list->control_type == while_control)
173 ui_out_text (uiout, "while ");
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);
177 ui_out_field_string (uiout, NULL, "end");
179 ui_out_spaces (uiout, 2 * depth);
180 ui_out_text (uiout, "end\n");
185 /* An if command. Recursively print both arms before continueing. */
186 if (list->control_type == if_control)
188 ui_out_text (uiout, "if ");
189 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
190 ui_out_text (uiout, "\n");
192 print_command_lines (uiout, list->body_list[0], depth + 1);
194 /* Show the false arm if it exists. */
195 if (list->body_count == 2)
198 ui_out_spaces (uiout, 2 * depth);
199 ui_out_field_string (uiout, NULL, "else");
200 ui_out_text (uiout, "else\n");
201 print_command_lines (uiout, list->body_list[1], depth + 1);
204 ui_out_field_string (uiout, NULL, "end");
206 ui_out_spaces (uiout, 2 * depth);
207 ui_out_text (uiout, "end\n");
212 /* ignore illegal command type and try next */
218 print_command_line (struct command_line *cmd, unsigned int depth,
219 struct ui_file *stream)
225 for (i = 0; i < depth; i++)
226 fputs_filtered (" ", stream);
229 /* A simple command, print it and return. */
230 if (cmd->control_type == simple_control)
232 fputs_filtered (cmd->line, stream);
233 fputs_filtered ("\n", stream);
237 /* loop_continue to jump to the start of a while loop, print it
239 if (cmd->control_type == continue_control)
241 fputs_filtered ("loop_continue\n", stream);
245 /* loop_break to break out of a while loop, print it and return. */
246 if (cmd->control_type == break_control)
248 fputs_filtered ("loop_break\n", stream);
252 /* A while command. Recursively print its subcommands before returning. */
253 if (cmd->control_type == while_control)
255 struct command_line *list;
256 fputs_filtered ("while ", stream);
257 fputs_filtered (cmd->line, stream);
258 fputs_filtered ("\n", stream);
259 list = *cmd->body_list;
262 print_command_line (list, depth + 1, stream);
267 /* An if command. Recursively print both arms before returning. */
268 if (cmd->control_type == if_control)
270 fputs_filtered ("if ", stream);
271 fputs_filtered (cmd->line, stream);
272 fputs_filtered ("\n", stream);
274 print_command_line (cmd->body_list[0], depth + 1, stream);
276 /* Show the false arm if it exists. */
277 if (cmd->body_count == 2)
281 for (i = 0; i < depth; i++)
282 fputs_filtered (" ", stream);
284 fputs_filtered ("else\n", stream);
285 print_command_line (cmd->body_list[1], depth + 1, stream);
289 for (i = 0; i < depth; i++)
290 fputs_filtered (" ", stream);
292 fputs_filtered ("end\n", stream);
297 /* Execute the command in CMD. */
300 execute_user_command (struct cmd_list_element *c, char *args)
302 register struct command_line *cmdlines;
303 struct cleanup *old_chain;
304 enum command_control_type ret;
306 old_chain = setup_user_args (args);
308 cmdlines = c->user_commands;
313 /* Set the instream to 0, indicating execution of a
314 user-defined function. */
315 old_chain = make_cleanup (do_restore_instream_cleanup, instream);
316 instream = (FILE *) 0;
319 ret = execute_control_command (cmdlines);
320 if (ret != simple_control && ret != break_control)
322 warning ("Error in control structure.\n");
325 cmdlines = cmdlines->next;
327 do_cleanups (old_chain);
330 enum command_control_type
331 execute_control_command (struct command_line *cmd)
333 struct expression *expr;
334 struct command_line *current;
335 struct cleanup *old_chain = 0;
339 enum command_control_type ret;
342 switch (cmd->control_type)
345 /* A simple command, execute it and return. */
346 new_line = insert_args (cmd->line);
348 return invalid_control;
349 old_chain = make_cleanup (free_current_contents, &new_line);
350 execute_command (new_line, 0);
351 ret = cmd->control_type;
354 case continue_control:
356 /* Return for "continue", and "break" so we can either
357 continue the loop at the top, or break out. */
358 ret = cmd->control_type;
363 /* Parse the loop control expression for the while statement. */
364 new_line = insert_args (cmd->line);
366 return invalid_control;
367 old_chain = make_cleanup (free_current_contents, &new_line);
368 expr = parse_expression (new_line);
369 make_cleanup (free_current_contents, &expr);
371 ret = simple_control;
374 /* Keep iterating so long as the expression is true. */
381 /* Evaluate the expression. */
382 val_mark = value_mark ();
383 val = evaluate_expression (expr);
384 cond_result = value_true (val);
385 value_free_to_mark (val_mark);
387 /* If the value is false, then break out of the loop. */
391 /* Execute the body of the while statement. */
392 current = *cmd->body_list;
395 ret = execute_control_command (current);
397 /* If we got an error, or a "break" command, then stop
399 if (ret == invalid_control || ret == break_control)
405 /* If we got a "continue" command, then restart the loop
407 if (ret == continue_control)
410 /* Get the next statement. */
411 current = current->next;
415 /* Reset RET so that we don't recurse the break all the way down. */
416 if (ret == break_control)
417 ret = simple_control;
424 new_line = insert_args (cmd->line);
426 return invalid_control;
427 old_chain = make_cleanup (free_current_contents, &new_line);
428 /* Parse the conditional for the if statement. */
429 expr = parse_expression (new_line);
430 make_cleanup (free_current_contents, &expr);
433 ret = simple_control;
435 /* Evaluate the conditional. */
436 val_mark = value_mark ();
437 val = evaluate_expression (expr);
439 /* Choose which arm to take commands from based on the value of the
440 conditional expression. */
441 if (value_true (val))
442 current = *cmd->body_list;
443 else if (cmd->body_count == 2)
444 current = *(cmd->body_list + 1);
445 value_free_to_mark (val_mark);
447 /* Execute commands in the given arm. */
450 ret = execute_control_command (current);
452 /* If we got an error, get out. */
453 if (ret != simple_control)
456 /* Get the next statement in the body. */
457 current = current->next;
464 warning ("Invalid control type in command structure.");
465 return invalid_control;
469 do_cleanups (old_chain);
474 /* "while" command support. Executes a body of statements while the
475 loop condition is nonzero. */
478 while_command (char *arg, int from_tty)
480 struct command_line *command = NULL;
483 command = get_command_line (while_control, arg);
488 execute_control_command (command);
489 free_command_lines (&command);
492 /* "if" command support. Execute either the true or false arm depending
493 on the value of the if conditional. */
496 if_command (char *arg, int from_tty)
498 struct command_line *command = NULL;
501 command = get_command_line (if_control, arg);
506 execute_control_command (command);
507 free_command_lines (&command);
512 arg_cleanup (void *ignore)
514 struct user_args *oargs = user_args;
516 internal_error ("Internal error, 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 free (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);
1032 do_free_command_lines_cleanup (void *arg)
1034 free_command_lines (arg);
1037 static struct cleanup *
1038 make_cleanup_free_command_lines (struct command_line **arg)
1040 return make_cleanup (do_free_command_lines_cleanup, arg);
1044 validate_comname (char *comname)
1049 error_no_arg ("name of command to define");
1054 if (!isalnum (*p) && *p != '-' && *p != '_')
1055 error ("Junk in argument list: \"%s\"", p);
1060 /* This is just a placeholder in the command data structures. */
1062 user_defined_command (char *ignore, int from_tty)
1067 define_command (char *comname, int from_tty)
1069 #define MAX_TMPBUF 128
1076 register struct command_line *cmds;
1077 register struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1078 char *tem = comname;
1080 char tmpbuf[MAX_TMPBUF];
1081 int hook_type = CMD_NO_HOOK;
1082 int hook_name_size = 0;
1084 #define HOOK_STRING "hook-"
1086 #define HOOK_POST_STRING "hookpost-"
1087 #define HOOK_POST_LEN 9
1089 validate_comname (comname);
1091 /* Look it up, and verify that we got an exact match. */
1092 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1093 if (c && !STREQ (comname, c->name))
1098 if (c->class == class_user || c->class == class_alias)
1099 tem = "Redefine command \"%s\"? ";
1101 tem = "Really redefine built-in command \"%s\"? ";
1102 if (!query (tem, c->name))
1103 error ("Command \"%s\" not redefined.", c->name);
1106 /* If this new command is a hook, then mark the command which it
1107 is hooking. Note that we allow hooking `help' commands, so that
1108 we can hook the `stop' pseudo-command. */
1110 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1112 hook_type = CMD_PRE_HOOK;
1113 hook_name_size = HOOK_LEN;
1115 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1117 hook_type = CMD_POST_HOOK;
1118 hook_name_size = HOOK_POST_LEN;
1121 if (hook_type != CMD_NO_HOOK)
1123 /* Look up cmd it hooks, and verify that we got an exact match. */
1124 tem = comname + hook_name_size;
1125 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1126 if (hookc && !STREQ (comname + hook_name_size, hookc->name))
1130 warning ("Your new `%s' command does not hook any existing command.",
1132 if (!query ("Proceed? "))
1133 error ("Not confirmed.");
1137 comname = savestring (comname, strlen (comname));
1139 /* If the rest of the commands will be case insensitive, this one
1140 should behave in the same manner. */
1141 for (tem = comname; *tem; tem++)
1143 *tem = tolower (*tem);
1145 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1146 cmds = read_command_lines (tmpbuf, from_tty);
1148 if (c && c->class == class_user)
1149 free_command_lines (&c->user_commands);
1151 newc = add_cmd (comname, class_user, user_defined_command,
1152 (c && c->class == class_user)
1153 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1154 newc->user_commands = cmds;
1156 /* If this new command is a hook, then mark both commands as being
1163 hookc->hook_pre = newc; /* Target gets hooked. */
1164 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1167 hookc->hook_pre = newc; /* Target gets hooked. */
1168 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1171 /* Should never come here as hookc would be 0. */
1177 document_command (char *comname, int from_tty)
1179 struct command_line *doclines;
1180 register struct cmd_list_element *c;
1181 char *tem = comname;
1184 validate_comname (comname);
1186 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1188 if (c->class != class_user)
1189 error ("Command \"%s\" is built-in.", comname);
1191 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1192 doclines = read_command_lines (tmpbuf, from_tty);
1198 register struct command_line *cl1;
1199 register int len = 0;
1201 for (cl1 = doclines; cl1; cl1 = cl1->next)
1202 len += strlen (cl1->line) + 1;
1204 c->doc = (char *) xmalloc (len + 1);
1207 for (cl1 = doclines; cl1; cl1 = cl1->next)
1209 strcat (c->doc, cl1->line);
1211 strcat (c->doc, "\n");
1215 free_command_lines (&doclines);
1218 struct source_cleanup_lines_args
1222 char *old_pre_error;
1223 char *old_error_pre_print;
1227 source_cleanup_lines (PTR args)
1229 struct source_cleanup_lines_args *p =
1230 (struct source_cleanup_lines_args *) args;
1231 source_line_number = p->old_line;
1232 source_file_name = p->old_file;
1233 source_pre_error = p->old_pre_error;
1234 error_pre_print = p->old_error_pre_print;
1239 do_fclose_cleanup (void *stream)
1244 /* Used to implement source_command */
1247 script_from_file (FILE *stream, char *file)
1249 struct cleanup *old_cleanups;
1250 struct source_cleanup_lines_args old_lines;
1255 internal_error ("called with NULL file pointer!");
1258 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1260 old_lines.old_line = source_line_number;
1261 old_lines.old_file = source_file_name;
1262 old_lines.old_pre_error = source_pre_error;
1263 old_lines.old_error_pre_print = error_pre_print;
1264 make_cleanup (source_cleanup_lines, &old_lines);
1265 source_line_number = 0;
1266 source_file_name = file;
1267 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
1268 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
1269 make_cleanup (free, source_pre_error);
1270 /* This will get set every time we read a line. So it won't stay "" for
1272 error_pre_print = "";
1274 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
1275 if (source_error_allocated < needed_length)
1277 source_error_allocated *= 2;
1278 if (source_error_allocated < needed_length)
1279 source_error_allocated = needed_length;
1280 if (source_error == NULL)
1281 source_error = xmalloc (source_error_allocated);
1283 source_error = xrealloc (source_error, source_error_allocated);
1286 read_command_file (stream);
1288 do_cleanups (old_cleanups);
1292 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1294 register struct command_line *cmdlines;
1296 cmdlines = c->user_commands;
1299 fputs_filtered ("User command ", stream);
1300 fputs_filtered (c->name, stream);
1301 fputs_filtered (":\n", stream);
1304 print_command_lines (uiout, cmdlines, 1);
1305 fputs_filtered ("\n", stream);
1309 print_command_line (cmdlines, 4, stream);
1310 cmdlines = cmdlines->next;
1312 fputs_filtered ("\n", stream);