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 Free Software
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"
33 #include "cli/cli-cmds.h"
34 #include "cli/cli-decode.h"
35 #include "cli/cli-script.h"
39 extern void dont_repeat (void);
41 extern void do_restore_instream_cleanup (void *stream
);
43 /* Prototypes for local functions */
45 static struct cleanup
*
46 make_cleanup_free_command_lines (struct command_line
**arg
);
48 static enum command_control_type
49 recurse_read_control_structure (struct command_line
*current_cmd
);
51 static char *insert_args (char *line
);
53 static struct cleanup
* setup_user_args (char *p
);
55 static void validate_comname (char *);
57 /* Level of control structure. */
58 static int control_level
;
60 /* Source command state variable. */
61 static int source_error_allocated
;
63 /* Structure for arguments to user defined functions. */
64 #define MAXUSERARGS 10
67 struct user_args
*next
;
79 /* Allocate, initialize a new command line structure for one of the
80 control commands (if/while). */
82 static struct command_line
*
83 build_command_line (enum command_control_type type
, char *args
)
85 struct command_line
*cmd
;
88 error ("if/while commands require arguments.\n");
90 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
92 cmd
->control_type
= type
;
96 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
98 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
99 cmd
->line
= savestring (args
, strlen (args
));
103 /* Build and return a new command structure for the control commands
104 such as "if" and "while". */
106 static struct command_line
*
107 get_command_line (enum command_control_type type
, char *arg
)
109 struct command_line
*cmd
;
110 struct cleanup
*old_chain
= NULL
;
112 /* Allocate and build a new command line structure. */
113 cmd
= build_command_line (type
, arg
);
115 old_chain
= make_cleanup_free_command_lines (&cmd
);
117 /* Read in the body of this command. */
118 if (recurse_read_control_structure (cmd
) == invalid_control
)
120 warning ("error reading in control structure\n");
121 do_cleanups (old_chain
);
125 discard_cleanups (old_chain
);
129 /* Recursively print a command (including full control structures). */
132 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
135 struct command_line
*list
;
142 ui_out_spaces (uiout
, 2 * depth
);
144 /* A simple command, print it and continue. */
145 if (list
->control_type
== simple_control
)
147 ui_out_field_string (uiout
, NULL
, list
->line
);
148 ui_out_text (uiout
, "\n");
153 /* loop_continue to jump to the start of a while loop, print it
155 if (list
->control_type
== continue_control
)
157 ui_out_field_string (uiout
, NULL
, "loop_continue");
158 ui_out_text (uiout
, "\n");
163 /* loop_break to break out of a while loop, print it and continue. */
164 if (list
->control_type
== break_control
)
166 ui_out_field_string (uiout
, NULL
, "loop_break");
167 ui_out_text (uiout
, "\n");
172 /* A while command. Recursively print its subcommands and continue. */
173 if (list
->control_type
== while_control
)
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);
179 ui_out_spaces (uiout
, 2 * depth
);
180 ui_out_field_string (uiout
, NULL
, "end");
181 ui_out_text (uiout
, "\n");
186 /* An if command. Recursively print both arms before continueing. */
187 if (list
->control_type
== if_control
)
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
, "\n");
201 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
205 ui_out_spaces (uiout
, 2 * depth
);
206 ui_out_field_string (uiout
, NULL
, "end");
207 ui_out_text (uiout
, "\n");
212 /* ignore illegal command type and try next */
217 /* Handle pre-post hooks. */
220 clear_hook_in_cleanup (void *data
)
222 struct cmd_list_element
*c
= data
;
223 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
227 execute_cmd_pre_hook (struct cmd_list_element
*c
)
229 if ((c
->hook_pre
) && (!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_pre
, (char *) 0);
234 do_cleanups (cleanups
);
239 execute_cmd_post_hook (struct cmd_list_element
*c
)
241 if ((c
->hook_post
) && (!c
->hook_in
))
243 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
244 c
->hook_in
= 1; /* Prevent recursive hooking */
245 execute_user_command (c
->hook_post
, (char *) 0);
246 do_cleanups (cleanups
);
250 /* Execute the command in CMD. */
252 do_restore_user_call_depth (void * call_depth
)
254 int * depth
= call_depth
;
255 /* We will be returning_to_top_level() at this point, so we want to
262 execute_user_command (struct cmd_list_element
*c
, char *args
)
264 register struct command_line
*cmdlines
;
265 struct cleanup
*old_chain
;
266 enum command_control_type ret
;
267 static int user_call_depth
= 0;
268 extern int max_user_call_depth
;
270 old_chain
= setup_user_args (args
);
272 cmdlines
= c
->user_commands
;
277 if (++user_call_depth
> max_user_call_depth
)
278 error ("Max user call depth exceeded -- command aborted\n");
280 old_chain
= make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
282 /* Set the instream to 0, indicating execution of a
283 user-defined function. */
284 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
285 instream
= (FILE *) 0;
288 ret
= execute_control_command (cmdlines
);
289 if (ret
!= simple_control
&& ret
!= break_control
)
291 warning ("Error in control structure.\n");
294 cmdlines
= cmdlines
->next
;
296 do_cleanups (old_chain
);
301 enum command_control_type
302 execute_control_command (struct command_line
*cmd
)
304 struct expression
*expr
;
305 struct command_line
*current
;
306 struct cleanup
*old_chain
= 0;
308 struct value
*val_mark
;
310 enum command_control_type ret
;
313 switch (cmd
->control_type
)
316 /* A simple command, execute it and return. */
317 new_line
= insert_args (cmd
->line
);
319 return invalid_control
;
320 old_chain
= make_cleanup (free_current_contents
, &new_line
);
321 execute_command (new_line
, 0);
322 ret
= cmd
->control_type
;
325 case continue_control
:
327 /* Return for "continue", and "break" so we can either
328 continue the loop at the top, or break out. */
329 ret
= cmd
->control_type
;
334 /* Parse the loop control expression for the while statement. */
335 new_line
= insert_args (cmd
->line
);
337 return invalid_control
;
338 old_chain
= make_cleanup (free_current_contents
, &new_line
);
339 expr
= parse_expression (new_line
);
340 make_cleanup (free_current_contents
, &expr
);
342 ret
= simple_control
;
345 /* Keep iterating so long as the expression is true. */
352 /* Evaluate the expression. */
353 val_mark
= value_mark ();
354 val
= evaluate_expression (expr
);
355 cond_result
= value_true (val
);
356 value_free_to_mark (val_mark
);
358 /* If the value is false, then break out of the loop. */
362 /* Execute the body of the while statement. */
363 current
= *cmd
->body_list
;
366 ret
= execute_control_command (current
);
368 /* If we got an error, or a "break" command, then stop
370 if (ret
== invalid_control
|| ret
== break_control
)
376 /* If we got a "continue" command, then restart the loop
378 if (ret
== continue_control
)
381 /* Get the next statement. */
382 current
= current
->next
;
386 /* Reset RET so that we don't recurse the break all the way down. */
387 if (ret
== break_control
)
388 ret
= simple_control
;
395 new_line
= insert_args (cmd
->line
);
397 return invalid_control
;
398 old_chain
= make_cleanup (free_current_contents
, &new_line
);
399 /* Parse the conditional for the if statement. */
400 expr
= parse_expression (new_line
);
401 make_cleanup (free_current_contents
, &expr
);
404 ret
= simple_control
;
406 /* Evaluate the conditional. */
407 val_mark
= value_mark ();
408 val
= evaluate_expression (expr
);
410 /* Choose which arm to take commands from based on the value of the
411 conditional expression. */
412 if (value_true (val
))
413 current
= *cmd
->body_list
;
414 else if (cmd
->body_count
== 2)
415 current
= *(cmd
->body_list
+ 1);
416 value_free_to_mark (val_mark
);
418 /* Execute commands in the given arm. */
421 ret
= execute_control_command (current
);
423 /* If we got an error, get out. */
424 if (ret
!= simple_control
)
427 /* Get the next statement in the body. */
428 current
= current
->next
;
435 warning ("Invalid control type in command structure.");
436 return invalid_control
;
440 do_cleanups (old_chain
);
445 /* "while" command support. Executes a body of statements while the
446 loop condition is nonzero. */
449 while_command (char *arg
, int from_tty
)
451 struct command_line
*command
= NULL
;
454 command
= get_command_line (while_control
, arg
);
459 execute_control_command (command
);
460 free_command_lines (&command
);
463 /* "if" command support. Execute either the true or false arm depending
464 on the value of the if conditional. */
467 if_command (char *arg
, int from_tty
)
469 struct command_line
*command
= NULL
;
472 command
= get_command_line (if_control
, arg
);
477 execute_control_command (command
);
478 free_command_lines (&command
);
483 arg_cleanup (void *ignore
)
485 struct user_args
*oargs
= user_args
;
487 internal_error (__FILE__
, __LINE__
,
488 "arg_cleanup called with no user args.\n");
490 user_args
= user_args
->next
;
494 /* Bind the incomming arguments for a user defined command to
495 $arg0, $arg1 ... $argMAXUSERARGS. */
497 static struct cleanup
*
498 setup_user_args (char *p
)
500 struct user_args
*args
;
501 struct cleanup
*old_chain
;
502 unsigned int arg_count
= 0;
504 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
505 memset (args
, 0, sizeof (struct user_args
));
507 args
->next
= user_args
;
510 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
522 if (arg_count
>= MAXUSERARGS
)
524 error ("user defined function may only have %d arguments.\n",
529 /* Strip whitespace. */
530 while (*p
== ' ' || *p
== '\t')
533 /* P now points to an argument. */
535 user_args
->a
[arg_count
].arg
= p
;
537 /* Get to the end of this argument. */
540 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
569 user_args
->a
[arg_count
].len
= p
- start_arg
;
576 /* Given character string P, return a point to the first argument ($arg),
577 or NULL if P contains no arguments. */
582 while ((p
= strchr (p
, '$')))
584 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
591 /* Insert the user defined arguments stored in user_arg into the $arg
592 arguments found in line, with the updated copy being placed into nline. */
595 insert_args (char *line
)
597 char *p
, *save_line
, *new_line
;
600 /* First we need to know how much memory to allocate for the new line. */
603 while ((p
= locate_arg (line
)))
608 if (i
>= user_args
->count
)
610 error ("Missing argument %d in user function.\n", i
);
613 len
+= user_args
->a
[i
].len
;
617 /* Don't forget the tail. */
618 len
+= strlen (line
);
620 /* Allocate space for the new line and fill it in. */
621 new_line
= (char *) xmalloc (len
+ 1);
622 if (new_line
== NULL
)
625 /* Restore pointer to beginning of old line. */
628 /* Save pointer to beginning of new line. */
629 save_line
= new_line
;
631 while ((p
= locate_arg (line
)))
635 memcpy (new_line
, line
, p
- line
);
636 new_line
+= p
- line
;
639 len
= user_args
->a
[i
].len
;
642 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
647 /* Don't forget the tail. */
648 strcpy (new_line
, line
);
650 /* Return a pointer to the beginning of the new line. */
655 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
656 code bodies. This is typically used when we encounter an "else"
657 clause for an "if" command. */
660 realloc_body_list (struct command_line
*command
, int new_length
)
663 struct command_line
**body_list
;
665 n
= command
->body_count
;
671 body_list
= (struct command_line
**)
672 xmalloc (sizeof (struct command_line
*) * new_length
);
674 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
676 xfree (command
->body_list
);
677 command
->body_list
= body_list
;
678 command
->body_count
= new_length
;
681 /* Read one line from the input stream. If the command is an "else" or
682 "end", return such an indication to the caller. */
684 static enum misc_command_type
685 read_next_line (struct command_line
**command
)
687 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
690 if (control_level
>= 254)
691 error ("Control nesting too deep!\n");
693 /* Set a prompt based on the nesting of the control commands. */
694 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
696 for (i
= 0; i
< control_level
; i
++)
697 control_prompt
[i
] = ' ';
698 control_prompt
[i
] = '>';
699 control_prompt
[i
+ 1] = '\0';
700 prompt_ptr
= (char *) &control_prompt
[0];
705 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
707 /* Not sure what to do here. */
711 /* Strip leading and trailing whitespace. */
712 while (*p
== ' ' || *p
== '\t')
716 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
719 /* Blanks and comments don't really do anything, but we need to
720 distinguish them from else, end and other commands which can be
722 if (p1
== p
|| p
[0] == '#')
725 /* Is this the end of a simple, while, or if control structure? */
726 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
729 /* Is the else clause of an if control structure? */
730 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
733 /* Check for while, if, break, continue, etc and build a new command
734 line structure for them. */
735 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
736 *command
= build_command_line (while_control
, p
+ 6);
737 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
738 *command
= build_command_line (if_control
, p
+ 3);
739 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
741 *command
= (struct command_line
*)
742 xmalloc (sizeof (struct command_line
));
743 (*command
)->next
= NULL
;
744 (*command
)->line
= NULL
;
745 (*command
)->control_type
= break_control
;
746 (*command
)->body_count
= 0;
747 (*command
)->body_list
= NULL
;
749 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
751 *command
= (struct command_line
*)
752 xmalloc (sizeof (struct command_line
));
753 (*command
)->next
= NULL
;
754 (*command
)->line
= NULL
;
755 (*command
)->control_type
= continue_control
;
756 (*command
)->body_count
= 0;
757 (*command
)->body_list
= NULL
;
761 /* A normal command. */
762 *command
= (struct command_line
*)
763 xmalloc (sizeof (struct command_line
));
764 (*command
)->next
= NULL
;
765 (*command
)->line
= savestring (p
, p1
- p
);
766 (*command
)->control_type
= simple_control
;
767 (*command
)->body_count
= 0;
768 (*command
)->body_list
= NULL
;
771 /* Nothing special. */
775 /* Recursively read in the control structures and create a command_line
778 The parent_control parameter is the control structure in which the
779 following commands are nested. */
781 static enum command_control_type
782 recurse_read_control_structure (struct command_line
*current_cmd
)
785 enum misc_command_type val
;
786 enum command_control_type ret
;
787 struct command_line
**body_ptr
, *child_tail
, *next
;
793 if (current_cmd
->control_type
== simple_control
)
795 error ("Recursed on a simple control type\n");
796 return invalid_control
;
799 if (current_body
> current_cmd
->body_count
)
801 error ("Allocated body is smaller than this command type needs\n");
802 return invalid_control
;
805 /* Read lines from the input stream and build control structures. */
811 val
= read_next_line (&next
);
813 /* Just skip blanks and comments. */
814 if (val
== nop_command
)
817 if (val
== end_command
)
819 if (current_cmd
->control_type
== while_control
820 || current_cmd
->control_type
== if_control
)
822 /* Success reading an entire control structure. */
823 ret
= simple_control
;
828 ret
= invalid_control
;
833 /* Not the end of a control structure. */
834 if (val
== else_command
)
836 if (current_cmd
->control_type
== if_control
837 && current_body
== 1)
839 realloc_body_list (current_cmd
, 2);
846 ret
= invalid_control
;
853 child_tail
->next
= next
;
857 body_ptr
= current_cmd
->body_list
;
858 for (i
= 1; i
< current_body
; i
++)
867 /* If the latest line is another control structure, then recurse
869 if (next
->control_type
== while_control
870 || next
->control_type
== if_control
)
873 ret
= recurse_read_control_structure (next
);
876 if (ret
!= simple_control
)
886 /* Read lines from the input stream and accumulate them in a chain of
887 struct command_line's, which is then returned. For input from a
888 terminal, the special command "end" is used to mark the end of the
889 input, and is not included in the returned chain of commands. */
891 #define END_MESSAGE "End with a line saying just \"end\"."
893 struct command_line
*
894 read_command_lines (char *prompt_arg
, int from_tty
)
896 struct command_line
*head
, *tail
, *next
;
897 struct cleanup
*old_chain
;
898 enum command_control_type ret
;
899 enum misc_command_type val
;
902 if (readline_begin_hook
)
904 /* Note - intentional to merge messages with no newline */
905 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
907 else if (from_tty
&& input_from_terminal_p ())
909 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
910 gdb_flush (gdb_stdout
);
918 val
= read_next_line (&next
);
920 /* Ignore blank lines or comments. */
921 if (val
== nop_command
)
924 if (val
== end_command
)
926 ret
= simple_control
;
930 if (val
!= ok_command
)
932 ret
= invalid_control
;
936 if (next
->control_type
== while_control
937 || next
->control_type
== if_control
)
940 ret
= recurse_read_control_structure (next
);
943 if (ret
== invalid_control
)
954 old_chain
= make_cleanup_free_command_lines (&head
);
963 if (ret
!= invalid_control
)
965 discard_cleanups (old_chain
);
968 do_cleanups (old_chain
);
971 if (readline_end_hook
)
973 (*readline_end_hook
) ();
978 /* Free a chain of struct command_line's. */
981 free_command_lines (struct command_line
**lptr
)
983 register struct command_line
*l
= *lptr
;
984 register struct command_line
*next
;
985 struct command_line
**blist
;
990 if (l
->body_count
> 0)
992 blist
= l
->body_list
;
993 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
994 free_command_lines (blist
);
1005 do_free_command_lines_cleanup (void *arg
)
1007 free_command_lines (arg
);
1010 static struct cleanup
*
1011 make_cleanup_free_command_lines (struct command_line
**arg
)
1013 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1016 struct command_line
*
1017 copy_command_lines (struct command_line
*cmds
)
1019 struct command_line
*result
= NULL
;
1023 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1025 result
->next
= copy_command_lines (cmds
->next
);
1026 result
->line
= xstrdup (cmds
->line
);
1027 result
->control_type
= cmds
->control_type
;
1028 result
->body_count
= cmds
->body_count
;
1029 if (cmds
->body_count
> 0)
1033 result
->body_list
= (struct command_line
**)
1034 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1036 for (i
= 0; i
< cmds
->body_count
; i
++)
1037 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1040 result
->body_list
= NULL
;
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 (void *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
);
1307 print_command_lines (uiout
, cmdlines
, 1);
1308 fputs_filtered ("\n", stream
);