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_text (uiout
, "while ");
175 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
176 ui_out_text (uiout
, "\n");
177 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
178 ui_out_field_string (uiout
, NULL
, "end");
180 ui_out_spaces (uiout
, 2 * depth
);
181 ui_out_text (uiout
, "end\n");
186 /* An if command. Recursively print both arms before continueing. */
187 if (list
->control_type
== if_control
)
189 ui_out_text (uiout
, "if ");
190 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
191 ui_out_text (uiout
, "\n");
193 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
195 /* Show the false arm if it exists. */
196 if (list
->body_count
== 2)
199 ui_out_spaces (uiout
, 2 * depth
);
200 ui_out_field_string (uiout
, NULL
, "else");
201 ui_out_text (uiout
, "else\n");
202 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
205 ui_out_field_string (uiout
, NULL
, "end");
207 ui_out_spaces (uiout
, 2 * depth
);
208 ui_out_text (uiout
, "end\n");
213 /* ignore illegal command type and try next */
219 print_command_line (struct command_line
*cmd
, unsigned int depth
,
220 struct ui_file
*stream
)
226 for (i
= 0; i
< depth
; i
++)
227 fputs_filtered (" ", stream
);
230 /* A simple command, print it and return. */
231 if (cmd
->control_type
== simple_control
)
233 fputs_filtered (cmd
->line
, stream
);
234 fputs_filtered ("\n", stream
);
238 /* loop_continue to jump to the start of a while loop, print it
240 if (cmd
->control_type
== continue_control
)
242 fputs_filtered ("loop_continue\n", stream
);
246 /* loop_break to break out of a while loop, print it and return. */
247 if (cmd
->control_type
== break_control
)
249 fputs_filtered ("loop_break\n", stream
);
253 /* A while command. Recursively print its subcommands before returning. */
254 if (cmd
->control_type
== while_control
)
256 struct command_line
*list
;
257 fputs_filtered ("while ", stream
);
258 fputs_filtered (cmd
->line
, stream
);
259 fputs_filtered ("\n", stream
);
260 list
= *cmd
->body_list
;
263 print_command_line (list
, depth
+ 1, stream
);
268 /* An if command. Recursively print both arms before returning. */
269 if (cmd
->control_type
== if_control
)
271 fputs_filtered ("if ", stream
);
272 fputs_filtered (cmd
->line
, stream
);
273 fputs_filtered ("\n", stream
);
275 print_command_line (cmd
->body_list
[0], depth
+ 1, stream
);
277 /* Show the false arm if it exists. */
278 if (cmd
->body_count
== 2)
282 for (i
= 0; i
< depth
; i
++)
283 fputs_filtered (" ", stream
);
285 fputs_filtered ("else\n", stream
);
286 print_command_line (cmd
->body_list
[1], depth
+ 1, stream
);
290 for (i
= 0; i
< depth
; i
++)
291 fputs_filtered (" ", stream
);
293 fputs_filtered ("end\n", stream
);
298 /* Execute the command in CMD. */
301 execute_user_command (struct cmd_list_element
*c
, char *args
)
303 register struct command_line
*cmdlines
;
304 struct cleanup
*old_chain
;
305 enum command_control_type ret
;
307 old_chain
= setup_user_args (args
);
309 cmdlines
= c
->user_commands
;
314 /* Set the instream to 0, indicating execution of a
315 user-defined function. */
316 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
317 instream
= (FILE *) 0;
320 ret
= execute_control_command (cmdlines
);
321 if (ret
!= simple_control
&& ret
!= break_control
)
323 warning ("Error in control structure.\n");
326 cmdlines
= cmdlines
->next
;
328 do_cleanups (old_chain
);
331 enum command_control_type
332 execute_control_command (struct command_line
*cmd
)
334 struct expression
*expr
;
335 struct command_line
*current
;
336 struct cleanup
*old_chain
= 0;
340 enum command_control_type ret
;
343 switch (cmd
->control_type
)
346 /* A simple command, execute it and return. */
347 new_line
= insert_args (cmd
->line
);
349 return invalid_control
;
350 old_chain
= make_cleanup (free_current_contents
, &new_line
);
351 execute_command (new_line
, 0);
352 ret
= cmd
->control_type
;
355 case continue_control
:
357 /* Return for "continue", and "break" so we can either
358 continue the loop at the top, or break out. */
359 ret
= cmd
->control_type
;
364 /* Parse the loop control expression for the while statement. */
365 new_line
= insert_args (cmd
->line
);
367 return invalid_control
;
368 old_chain
= make_cleanup (free_current_contents
, &new_line
);
369 expr
= parse_expression (new_line
);
370 make_cleanup (free_current_contents
, &expr
);
372 ret
= simple_control
;
375 /* Keep iterating so long as the expression is true. */
382 /* Evaluate the expression. */
383 val_mark
= value_mark ();
384 val
= evaluate_expression (expr
);
385 cond_result
= value_true (val
);
386 value_free_to_mark (val_mark
);
388 /* If the value is false, then break out of the loop. */
392 /* Execute the body of the while statement. */
393 current
= *cmd
->body_list
;
396 ret
= execute_control_command (current
);
398 /* If we got an error, or a "break" command, then stop
400 if (ret
== invalid_control
|| ret
== break_control
)
406 /* If we got a "continue" command, then restart the loop
408 if (ret
== continue_control
)
411 /* Get the next statement. */
412 current
= current
->next
;
416 /* Reset RET so that we don't recurse the break all the way down. */
417 if (ret
== break_control
)
418 ret
= simple_control
;
425 new_line
= insert_args (cmd
->line
);
427 return invalid_control
;
428 old_chain
= make_cleanup (free_current_contents
, &new_line
);
429 /* Parse the conditional for the if statement. */
430 expr
= parse_expression (new_line
);
431 make_cleanup (free_current_contents
, &expr
);
434 ret
= simple_control
;
436 /* Evaluate the conditional. */
437 val_mark
= value_mark ();
438 val
= evaluate_expression (expr
);
440 /* Choose which arm to take commands from based on the value of the
441 conditional expression. */
442 if (value_true (val
))
443 current
= *cmd
->body_list
;
444 else if (cmd
->body_count
== 2)
445 current
= *(cmd
->body_list
+ 1);
446 value_free_to_mark (val_mark
);
448 /* Execute commands in the given arm. */
451 ret
= execute_control_command (current
);
453 /* If we got an error, get out. */
454 if (ret
!= simple_control
)
457 /* Get the next statement in the body. */
458 current
= current
->next
;
465 warning ("Invalid control type in command structure.");
466 return invalid_control
;
470 do_cleanups (old_chain
);
475 /* "while" command support. Executes a body of statements while the
476 loop condition is nonzero. */
479 while_command (char *arg
, int from_tty
)
481 struct command_line
*command
= NULL
;
484 command
= get_command_line (while_control
, arg
);
489 execute_control_command (command
);
490 free_command_lines (&command
);
493 /* "if" command support. Execute either the true or false arm depending
494 on the value of the if conditional. */
497 if_command (char *arg
, int from_tty
)
499 struct command_line
*command
= NULL
;
502 command
= get_command_line (if_control
, arg
);
507 execute_control_command (command
);
508 free_command_lines (&command
);
513 arg_cleanup (void *ignore
)
515 struct user_args
*oargs
= user_args
;
517 internal_error (__FILE__
, __LINE__
,
518 "arg_cleanup called with no user args.\n");
520 user_args
= user_args
->next
;
524 /* Bind the incomming arguments for a user defined command to
525 $arg0, $arg1 ... $argMAXUSERARGS. */
527 static struct cleanup
*
528 setup_user_args (char *p
)
530 struct user_args
*args
;
531 struct cleanup
*old_chain
;
532 unsigned int arg_count
= 0;
534 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
535 memset (args
, 0, sizeof (struct user_args
));
537 args
->next
= user_args
;
540 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
552 if (arg_count
>= MAXUSERARGS
)
554 error ("user defined function may only have %d arguments.\n",
559 /* Strip whitespace. */
560 while (*p
== ' ' || *p
== '\t')
563 /* P now points to an argument. */
565 user_args
->a
[arg_count
].arg
= p
;
567 /* Get to the end of this argument. */
570 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
599 user_args
->a
[arg_count
].len
= p
- start_arg
;
606 /* Given character string P, return a point to the first argument ($arg),
607 or NULL if P contains no arguments. */
612 while ((p
= strchr (p
, '$')))
614 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
621 /* Insert the user defined arguments stored in user_arg into the $arg
622 arguments found in line, with the updated copy being placed into nline. */
625 insert_args (char *line
)
627 char *p
, *save_line
, *new_line
;
630 /* First we need to know how much memory to allocate for the new line. */
633 while ((p
= locate_arg (line
)))
638 if (i
>= user_args
->count
)
640 error ("Missing argument %d in user function.\n", i
);
643 len
+= user_args
->a
[i
].len
;
647 /* Don't forget the tail. */
648 len
+= strlen (line
);
650 /* Allocate space for the new line and fill it in. */
651 new_line
= (char *) xmalloc (len
+ 1);
652 if (new_line
== NULL
)
655 /* Restore pointer to beginning of old line. */
658 /* Save pointer to beginning of new line. */
659 save_line
= new_line
;
661 while ((p
= locate_arg (line
)))
665 memcpy (new_line
, line
, p
- line
);
666 new_line
+= p
- line
;
669 len
= user_args
->a
[i
].len
;
672 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
677 /* Don't forget the tail. */
678 strcpy (new_line
, line
);
680 /* Return a pointer to the beginning of the new line. */
685 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
686 code bodies. This is typically used when we encounter an "else"
687 clause for an "if" command. */
690 realloc_body_list (struct command_line
*command
, int new_length
)
693 struct command_line
**body_list
;
695 n
= command
->body_count
;
701 body_list
= (struct command_line
**)
702 xmalloc (sizeof (struct command_line
*) * new_length
);
704 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
706 xfree (command
->body_list
);
707 command
->body_list
= body_list
;
708 command
->body_count
= new_length
;
711 /* Read one line from the input stream. If the command is an "else" or
712 "end", return such an indication to the caller. */
714 static enum misc_command_type
715 read_next_line (struct command_line
**command
)
717 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
720 if (control_level
>= 254)
721 error ("Control nesting too deep!\n");
723 /* Set a prompt based on the nesting of the control commands. */
724 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
726 for (i
= 0; i
< control_level
; i
++)
727 control_prompt
[i
] = ' ';
728 control_prompt
[i
] = '>';
729 control_prompt
[i
+ 1] = '\0';
730 prompt_ptr
= (char *) &control_prompt
[0];
735 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
737 /* Not sure what to do here. */
741 /* Strip leading and trailing whitespace. */
742 while (*p
== ' ' || *p
== '\t')
746 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
749 /* Blanks and comments don't really do anything, but we need to
750 distinguish them from else, end and other commands which can be
752 if (p1
== p
|| p
[0] == '#')
755 /* Is this the end of a simple, while, or if control structure? */
756 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
759 /* Is the else clause of an if control structure? */
760 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
763 /* Check for while, if, break, continue, etc and build a new command
764 line structure for them. */
765 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
766 *command
= build_command_line (while_control
, p
+ 6);
767 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
768 *command
= build_command_line (if_control
, p
+ 3);
769 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
771 *command
= (struct command_line
*)
772 xmalloc (sizeof (struct command_line
));
773 (*command
)->next
= NULL
;
774 (*command
)->line
= NULL
;
775 (*command
)->control_type
= break_control
;
776 (*command
)->body_count
= 0;
777 (*command
)->body_list
= NULL
;
779 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
781 *command
= (struct command_line
*)
782 xmalloc (sizeof (struct command_line
));
783 (*command
)->next
= NULL
;
784 (*command
)->line
= NULL
;
785 (*command
)->control_type
= continue_control
;
786 (*command
)->body_count
= 0;
787 (*command
)->body_list
= NULL
;
791 /* A normal command. */
792 *command
= (struct command_line
*)
793 xmalloc (sizeof (struct command_line
));
794 (*command
)->next
= NULL
;
795 (*command
)->line
= savestring (p
, p1
- p
);
796 (*command
)->control_type
= simple_control
;
797 (*command
)->body_count
= 0;
798 (*command
)->body_list
= NULL
;
801 /* Nothing special. */
805 /* Recursively read in the control structures and create a command_line
808 The parent_control parameter is the control structure in which the
809 following commands are nested. */
811 static enum command_control_type
812 recurse_read_control_structure (struct command_line
*current_cmd
)
815 enum misc_command_type val
;
816 enum command_control_type ret
;
817 struct command_line
**body_ptr
, *child_tail
, *next
;
823 if (current_cmd
->control_type
== simple_control
)
825 error ("Recursed on a simple control type\n");
826 return invalid_control
;
829 if (current_body
> current_cmd
->body_count
)
831 error ("Allocated body is smaller than this command type needs\n");
832 return invalid_control
;
835 /* Read lines from the input stream and build control structures. */
841 val
= read_next_line (&next
);
843 /* Just skip blanks and comments. */
844 if (val
== nop_command
)
847 if (val
== end_command
)
849 if (current_cmd
->control_type
== while_control
850 || current_cmd
->control_type
== if_control
)
852 /* Success reading an entire control structure. */
853 ret
= simple_control
;
858 ret
= invalid_control
;
863 /* Not the end of a control structure. */
864 if (val
== else_command
)
866 if (current_cmd
->control_type
== if_control
867 && current_body
== 1)
869 realloc_body_list (current_cmd
, 2);
876 ret
= invalid_control
;
883 child_tail
->next
= next
;
887 body_ptr
= current_cmd
->body_list
;
888 for (i
= 1; i
< current_body
; i
++)
897 /* If the latest line is another control structure, then recurse
899 if (next
->control_type
== while_control
900 || next
->control_type
== if_control
)
903 ret
= recurse_read_control_structure (next
);
906 if (ret
!= simple_control
)
916 /* Read lines from the input stream and accumulate them in a chain of
917 struct command_line's, which is then returned. For input from a
918 terminal, the special command "end" is used to mark the end of the
919 input, and is not included in the returned chain of commands. */
921 #define END_MESSAGE "End with a line saying just \"end\"."
923 struct command_line
*
924 read_command_lines (char *prompt_arg
, int from_tty
)
926 struct command_line
*head
, *tail
, *next
;
927 struct cleanup
*old_chain
;
928 enum command_control_type ret
;
929 enum misc_command_type val
;
932 if (readline_begin_hook
)
934 /* Note - intentional to merge messages with no newline */
935 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
937 else if (from_tty
&& input_from_terminal_p ())
939 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
940 gdb_flush (gdb_stdout
);
948 val
= read_next_line (&next
);
950 /* Ignore blank lines or comments. */
951 if (val
== nop_command
)
954 if (val
== end_command
)
956 ret
= simple_control
;
960 if (val
!= ok_command
)
962 ret
= invalid_control
;
966 if (next
->control_type
== while_control
967 || next
->control_type
== if_control
)
970 ret
= recurse_read_control_structure (next
);
973 if (ret
== invalid_control
)
984 old_chain
= make_cleanup_free_command_lines (&head
);
993 if (ret
!= invalid_control
)
995 discard_cleanups (old_chain
);
998 do_cleanups (old_chain
);
1001 if (readline_end_hook
)
1003 (*readline_end_hook
) ();
1008 /* Free a chain of struct command_line's. */
1011 free_command_lines (struct command_line
**lptr
)
1013 register struct command_line
*l
= *lptr
;
1014 register struct command_line
*next
;
1015 struct command_line
**blist
;
1020 if (l
->body_count
> 0)
1022 blist
= l
->body_list
;
1023 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
1024 free_command_lines (blist
);
1035 do_free_command_lines_cleanup (void *arg
)
1037 free_command_lines (arg
);
1040 static struct cleanup
*
1041 make_cleanup_free_command_lines (struct command_line
**arg
)
1043 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1047 validate_comname (char *comname
)
1052 error_no_arg ("name of command to define");
1057 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1058 error ("Junk in argument list: \"%s\"", p
);
1063 /* This is just a placeholder in the command data structures. */
1065 user_defined_command (char *ignore
, int from_tty
)
1070 define_command (char *comname
, int from_tty
)
1072 #define MAX_TMPBUF 128
1079 register struct command_line
*cmds
;
1080 register struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1081 char *tem
= comname
;
1083 char tmpbuf
[MAX_TMPBUF
];
1084 int hook_type
= CMD_NO_HOOK
;
1085 int hook_name_size
= 0;
1087 #define HOOK_STRING "hook-"
1089 #define HOOK_POST_STRING "hookpost-"
1090 #define HOOK_POST_LEN 9
1092 validate_comname (comname
);
1094 /* Look it up, and verify that we got an exact match. */
1095 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1096 if (c
&& !STREQ (comname
, c
->name
))
1101 if (c
->class == class_user
|| c
->class == class_alias
)
1102 tem
= "Redefine command \"%s\"? ";
1104 tem
= "Really redefine built-in command \"%s\"? ";
1105 if (!query (tem
, c
->name
))
1106 error ("Command \"%s\" not redefined.", c
->name
);
1109 /* If this new command is a hook, then mark the command which it
1110 is hooking. Note that we allow hooking `help' commands, so that
1111 we can hook the `stop' pseudo-command. */
1113 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1115 hook_type
= CMD_PRE_HOOK
;
1116 hook_name_size
= HOOK_LEN
;
1118 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1120 hook_type
= CMD_POST_HOOK
;
1121 hook_name_size
= HOOK_POST_LEN
;
1124 if (hook_type
!= CMD_NO_HOOK
)
1126 /* Look up cmd it hooks, and verify that we got an exact match. */
1127 tem
= comname
+ hook_name_size
;
1128 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1129 if (hookc
&& !STREQ (comname
+ hook_name_size
, hookc
->name
))
1133 warning ("Your new `%s' command does not hook any existing command.",
1135 if (!query ("Proceed? "))
1136 error ("Not confirmed.");
1140 comname
= savestring (comname
, strlen (comname
));
1142 /* If the rest of the commands will be case insensitive, this one
1143 should behave in the same manner. */
1144 for (tem
= comname
; *tem
; tem
++)
1146 *tem
= tolower (*tem
);
1148 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1149 cmds
= read_command_lines (tmpbuf
, from_tty
);
1151 if (c
&& c
->class == class_user
)
1152 free_command_lines (&c
->user_commands
);
1154 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1155 (c
&& c
->class == class_user
)
1156 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1157 newc
->user_commands
= cmds
;
1159 /* If this new command is a hook, then mark both commands as being
1166 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1167 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1170 hookc
->hook_post
= newc
; /* Target gets hooked. */
1171 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1174 /* Should never come here as hookc would be 0. */
1175 internal_error (__FILE__
, __LINE__
, "bad switch");
1181 document_command (char *comname
, int from_tty
)
1183 struct command_line
*doclines
;
1184 register struct cmd_list_element
*c
;
1185 char *tem
= comname
;
1188 validate_comname (comname
);
1190 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1192 if (c
->class != class_user
)
1193 error ("Command \"%s\" is built-in.", comname
);
1195 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1196 doclines
= read_command_lines (tmpbuf
, from_tty
);
1202 register struct command_line
*cl1
;
1203 register int len
= 0;
1205 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1206 len
+= strlen (cl1
->line
) + 1;
1208 c
->doc
= (char *) xmalloc (len
+ 1);
1211 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1213 strcat (c
->doc
, cl1
->line
);
1215 strcat (c
->doc
, "\n");
1219 free_command_lines (&doclines
);
1222 struct source_cleanup_lines_args
1226 char *old_pre_error
;
1227 char *old_error_pre_print
;
1231 source_cleanup_lines (PTR args
)
1233 struct source_cleanup_lines_args
*p
=
1234 (struct source_cleanup_lines_args
*) args
;
1235 source_line_number
= p
->old_line
;
1236 source_file_name
= p
->old_file
;
1237 source_pre_error
= p
->old_pre_error
;
1238 error_pre_print
= p
->old_error_pre_print
;
1243 do_fclose_cleanup (void *stream
)
1248 /* Used to implement source_command */
1251 script_from_file (FILE *stream
, char *file
)
1253 struct cleanup
*old_cleanups
;
1254 struct source_cleanup_lines_args old_lines
;
1259 internal_error (__FILE__
, __LINE__
, "called with NULL file pointer!");
1262 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1264 old_lines
.old_line
= source_line_number
;
1265 old_lines
.old_file
= source_file_name
;
1266 old_lines
.old_pre_error
= source_pre_error
;
1267 old_lines
.old_error_pre_print
= error_pre_print
;
1268 make_cleanup (source_cleanup_lines
, &old_lines
);
1269 source_line_number
= 0;
1270 source_file_name
= file
;
1271 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
1272 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
1273 make_cleanup (xfree
, source_pre_error
);
1274 /* This will get set every time we read a line. So it won't stay "" for
1276 error_pre_print
= "";
1278 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
1279 if (source_error_allocated
< needed_length
)
1281 source_error_allocated
*= 2;
1282 if (source_error_allocated
< needed_length
)
1283 source_error_allocated
= needed_length
;
1284 if (source_error
== NULL
)
1285 source_error
= xmalloc (source_error_allocated
);
1287 source_error
= xrealloc (source_error
, source_error_allocated
);
1290 read_command_file (stream
);
1292 do_cleanups (old_cleanups
);
1296 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1298 register struct command_line
*cmdlines
;
1300 cmdlines
= c
->user_commands
;
1303 fputs_filtered ("User command ", stream
);
1304 fputs_filtered (c
->name
, stream
);
1305 fputs_filtered (":\n", stream
);
1308 print_command_lines (uiout
, cmdlines
, 1);
1309 fputs_filtered ("\n", stream
);
1313 print_command_line (cmdlines
, 4, stream
);
1314 cmdlines
= cmdlines
->next
;
1316 fputs_filtered ("\n", stream
);