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 */
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 */
216 /* Handle pre-post hooks. */
219 clear_hook_in_cleanup (void *data
)
221 struct cmd_list_element
*c
= data
;
222 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
226 execute_cmd_pre_hook (struct cmd_list_element
*c
)
228 if ((c
->hook_pre
) && (!c
->hook_in
))
230 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
231 c
->hook_in
= 1; /* Prevent recursive hooking */
232 execute_user_command (c
->hook_pre
, (char *) 0);
233 do_cleanups (cleanups
);
238 execute_cmd_post_hook (struct cmd_list_element
*c
)
240 if ((c
->hook_post
) && (!c
->hook_in
))
242 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
243 c
->hook_in
= 1; /* Prevent recursive hooking */
244 execute_user_command (c
->hook_post
, (char *) 0);
245 do_cleanups (cleanups
);
249 /* Execute the command in CMD. */
251 do_restore_user_call_depth (void * call_depth
)
253 int * depth
= call_depth
;
254 /* We will be returning_to_top_level() at this point, so we want to
261 execute_user_command (struct cmd_list_element
*c
, char *args
)
263 register struct command_line
*cmdlines
;
264 struct cleanup
*old_chain
;
265 enum command_control_type ret
;
266 static int user_call_depth
= 0;
267 extern int max_user_call_depth
;
269 old_chain
= setup_user_args (args
);
271 cmdlines
= c
->user_commands
;
276 if (++user_call_depth
> max_user_call_depth
)
277 error ("Max user call depth exceeded -- command aborted\n");
279 old_chain
= make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
281 /* Set the instream to 0, indicating execution of a
282 user-defined function. */
283 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
284 instream
= (FILE *) 0;
287 ret
= execute_control_command (cmdlines
);
288 if (ret
!= simple_control
&& ret
!= break_control
)
290 warning ("Error in control structure.\n");
293 cmdlines
= cmdlines
->next
;
295 do_cleanups (old_chain
);
300 enum command_control_type
301 execute_control_command (struct command_line
*cmd
)
303 struct expression
*expr
;
304 struct command_line
*current
;
305 struct cleanup
*old_chain
= 0;
307 struct value
*val_mark
;
309 enum command_control_type ret
;
312 switch (cmd
->control_type
)
315 /* A simple command, execute it and return. */
316 new_line
= insert_args (cmd
->line
);
318 return invalid_control
;
319 old_chain
= make_cleanup (free_current_contents
, &new_line
);
320 execute_command (new_line
, 0);
321 ret
= cmd
->control_type
;
324 case continue_control
:
326 /* Return for "continue", and "break" so we can either
327 continue the loop at the top, or break out. */
328 ret
= cmd
->control_type
;
333 /* Parse the loop control expression for the while statement. */
334 new_line
= insert_args (cmd
->line
);
336 return invalid_control
;
337 old_chain
= make_cleanup (free_current_contents
, &new_line
);
338 expr
= parse_expression (new_line
);
339 make_cleanup (free_current_contents
, &expr
);
341 ret
= simple_control
;
344 /* Keep iterating so long as the expression is true. */
351 /* Evaluate the expression. */
352 val_mark
= value_mark ();
353 val
= evaluate_expression (expr
);
354 cond_result
= value_true (val
);
355 value_free_to_mark (val_mark
);
357 /* If the value is false, then break out of the loop. */
361 /* Execute the body of the while statement. */
362 current
= *cmd
->body_list
;
365 ret
= execute_control_command (current
);
367 /* If we got an error, or a "break" command, then stop
369 if (ret
== invalid_control
|| ret
== break_control
)
375 /* If we got a "continue" command, then restart the loop
377 if (ret
== continue_control
)
380 /* Get the next statement. */
381 current
= current
->next
;
385 /* Reset RET so that we don't recurse the break all the way down. */
386 if (ret
== break_control
)
387 ret
= simple_control
;
394 new_line
= insert_args (cmd
->line
);
396 return invalid_control
;
397 old_chain
= make_cleanup (free_current_contents
, &new_line
);
398 /* Parse the conditional for the if statement. */
399 expr
= parse_expression (new_line
);
400 make_cleanup (free_current_contents
, &expr
);
403 ret
= simple_control
;
405 /* Evaluate the conditional. */
406 val_mark
= value_mark ();
407 val
= evaluate_expression (expr
);
409 /* Choose which arm to take commands from based on the value of the
410 conditional expression. */
411 if (value_true (val
))
412 current
= *cmd
->body_list
;
413 else if (cmd
->body_count
== 2)
414 current
= *(cmd
->body_list
+ 1);
415 value_free_to_mark (val_mark
);
417 /* Execute commands in the given arm. */
420 ret
= execute_control_command (current
);
422 /* If we got an error, get out. */
423 if (ret
!= simple_control
)
426 /* Get the next statement in the body. */
427 current
= current
->next
;
434 warning ("Invalid control type in command structure.");
435 return invalid_control
;
439 do_cleanups (old_chain
);
444 /* "while" command support. Executes a body of statements while the
445 loop condition is nonzero. */
448 while_command (char *arg
, int from_tty
)
450 struct command_line
*command
= NULL
;
453 command
= get_command_line (while_control
, arg
);
458 execute_control_command (command
);
459 free_command_lines (&command
);
462 /* "if" command support. Execute either the true or false arm depending
463 on the value of the if conditional. */
466 if_command (char *arg
, int from_tty
)
468 struct command_line
*command
= NULL
;
471 command
= get_command_line (if_control
, arg
);
476 execute_control_command (command
);
477 free_command_lines (&command
);
482 arg_cleanup (void *ignore
)
484 struct user_args
*oargs
= user_args
;
486 internal_error (__FILE__
, __LINE__
,
487 "arg_cleanup called with no user args.\n");
489 user_args
= user_args
->next
;
493 /* Bind the incomming arguments for a user defined command to
494 $arg0, $arg1 ... $argMAXUSERARGS. */
496 static struct cleanup
*
497 setup_user_args (char *p
)
499 struct user_args
*args
;
500 struct cleanup
*old_chain
;
501 unsigned int arg_count
= 0;
503 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
504 memset (args
, 0, sizeof (struct user_args
));
506 args
->next
= user_args
;
509 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
521 if (arg_count
>= MAXUSERARGS
)
523 error ("user defined function may only have %d arguments.\n",
528 /* Strip whitespace. */
529 while (*p
== ' ' || *p
== '\t')
532 /* P now points to an argument. */
534 user_args
->a
[arg_count
].arg
= p
;
536 /* Get to the end of this argument. */
539 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
568 user_args
->a
[arg_count
].len
= p
- start_arg
;
575 /* Given character string P, return a point to the first argument ($arg),
576 or NULL if P contains no arguments. */
581 while ((p
= strchr (p
, '$')))
583 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
590 /* Insert the user defined arguments stored in user_arg into the $arg
591 arguments found in line, with the updated copy being placed into nline. */
594 insert_args (char *line
)
596 char *p
, *save_line
, *new_line
;
599 /* First we need to know how much memory to allocate for the new line. */
602 while ((p
= locate_arg (line
)))
607 if (i
>= user_args
->count
)
609 error ("Missing argument %d in user function.\n", i
);
612 len
+= user_args
->a
[i
].len
;
616 /* Don't forget the tail. */
617 len
+= strlen (line
);
619 /* Allocate space for the new line and fill it in. */
620 new_line
= (char *) xmalloc (len
+ 1);
621 if (new_line
== NULL
)
624 /* Restore pointer to beginning of old line. */
627 /* Save pointer to beginning of new line. */
628 save_line
= new_line
;
630 while ((p
= locate_arg (line
)))
634 memcpy (new_line
, line
, p
- line
);
635 new_line
+= p
- line
;
638 len
= user_args
->a
[i
].len
;
641 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
646 /* Don't forget the tail. */
647 strcpy (new_line
, line
);
649 /* Return a pointer to the beginning of the new line. */
654 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
655 code bodies. This is typically used when we encounter an "else"
656 clause for an "if" command. */
659 realloc_body_list (struct command_line
*command
, int new_length
)
662 struct command_line
**body_list
;
664 n
= command
->body_count
;
670 body_list
= (struct command_line
**)
671 xmalloc (sizeof (struct command_line
*) * new_length
);
673 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
675 xfree (command
->body_list
);
676 command
->body_list
= body_list
;
677 command
->body_count
= new_length
;
680 /* Read one line from the input stream. If the command is an "else" or
681 "end", return such an indication to the caller. */
683 static enum misc_command_type
684 read_next_line (struct command_line
**command
)
686 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
689 if (control_level
>= 254)
690 error ("Control nesting too deep!\n");
692 /* Set a prompt based on the nesting of the control commands. */
693 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
695 for (i
= 0; i
< control_level
; i
++)
696 control_prompt
[i
] = ' ';
697 control_prompt
[i
] = '>';
698 control_prompt
[i
+ 1] = '\0';
699 prompt_ptr
= (char *) &control_prompt
[0];
704 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
706 /* Not sure what to do here. */
710 /* Strip leading and trailing whitespace. */
711 while (*p
== ' ' || *p
== '\t')
715 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
718 /* Blanks and comments don't really do anything, but we need to
719 distinguish them from else, end and other commands which can be
721 if (p1
== p
|| p
[0] == '#')
724 /* Is this the end of a simple, while, or if control structure? */
725 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
728 /* Is the else clause of an if control structure? */
729 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
732 /* Check for while, if, break, continue, etc and build a new command
733 line structure for them. */
734 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
735 *command
= build_command_line (while_control
, p
+ 6);
736 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
737 *command
= build_command_line (if_control
, p
+ 3);
738 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
740 *command
= (struct command_line
*)
741 xmalloc (sizeof (struct command_line
));
742 (*command
)->next
= NULL
;
743 (*command
)->line
= NULL
;
744 (*command
)->control_type
= break_control
;
745 (*command
)->body_count
= 0;
746 (*command
)->body_list
= NULL
;
748 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
750 *command
= (struct command_line
*)
751 xmalloc (sizeof (struct command_line
));
752 (*command
)->next
= NULL
;
753 (*command
)->line
= NULL
;
754 (*command
)->control_type
= continue_control
;
755 (*command
)->body_count
= 0;
756 (*command
)->body_list
= NULL
;
760 /* A normal command. */
761 *command
= (struct command_line
*)
762 xmalloc (sizeof (struct command_line
));
763 (*command
)->next
= NULL
;
764 (*command
)->line
= savestring (p
, p1
- p
);
765 (*command
)->control_type
= simple_control
;
766 (*command
)->body_count
= 0;
767 (*command
)->body_list
= NULL
;
770 /* Nothing special. */
774 /* Recursively read in the control structures and create a command_line
777 The parent_control parameter is the control structure in which the
778 following commands are nested. */
780 static enum command_control_type
781 recurse_read_control_structure (struct command_line
*current_cmd
)
784 enum misc_command_type val
;
785 enum command_control_type ret
;
786 struct command_line
**body_ptr
, *child_tail
, *next
;
792 if (current_cmd
->control_type
== simple_control
)
794 error ("Recursed on a simple control type\n");
795 return invalid_control
;
798 if (current_body
> current_cmd
->body_count
)
800 error ("Allocated body is smaller than this command type needs\n");
801 return invalid_control
;
804 /* Read lines from the input stream and build control structures. */
810 val
= read_next_line (&next
);
812 /* Just skip blanks and comments. */
813 if (val
== nop_command
)
816 if (val
== end_command
)
818 if (current_cmd
->control_type
== while_control
819 || current_cmd
->control_type
== if_control
)
821 /* Success reading an entire control structure. */
822 ret
= simple_control
;
827 ret
= invalid_control
;
832 /* Not the end of a control structure. */
833 if (val
== else_command
)
835 if (current_cmd
->control_type
== if_control
836 && current_body
== 1)
838 realloc_body_list (current_cmd
, 2);
845 ret
= invalid_control
;
852 child_tail
->next
= next
;
856 body_ptr
= current_cmd
->body_list
;
857 for (i
= 1; i
< current_body
; i
++)
866 /* If the latest line is another control structure, then recurse
868 if (next
->control_type
== while_control
869 || next
->control_type
== if_control
)
872 ret
= recurse_read_control_structure (next
);
875 if (ret
!= simple_control
)
885 /* Read lines from the input stream and accumulate them in a chain of
886 struct command_line's, which is then returned. For input from a
887 terminal, the special command "end" is used to mark the end of the
888 input, and is not included in the returned chain of commands. */
890 #define END_MESSAGE "End with a line saying just \"end\"."
892 struct command_line
*
893 read_command_lines (char *prompt_arg
, int from_tty
)
895 struct command_line
*head
, *tail
, *next
;
896 struct cleanup
*old_chain
;
897 enum command_control_type ret
;
898 enum misc_command_type val
;
901 if (readline_begin_hook
)
903 /* Note - intentional to merge messages with no newline */
904 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
906 else if (from_tty
&& input_from_terminal_p ())
908 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
909 gdb_flush (gdb_stdout
);
917 val
= read_next_line (&next
);
919 /* Ignore blank lines or comments. */
920 if (val
== nop_command
)
923 if (val
== end_command
)
925 ret
= simple_control
;
929 if (val
!= ok_command
)
931 ret
= invalid_control
;
935 if (next
->control_type
== while_control
936 || next
->control_type
== if_control
)
939 ret
= recurse_read_control_structure (next
);
942 if (ret
== invalid_control
)
953 old_chain
= make_cleanup_free_command_lines (&head
);
962 if (ret
!= invalid_control
)
964 discard_cleanups (old_chain
);
967 do_cleanups (old_chain
);
970 if (readline_end_hook
)
972 (*readline_end_hook
) ();
977 /* Free a chain of struct command_line's. */
980 free_command_lines (struct command_line
**lptr
)
982 register struct command_line
*l
= *lptr
;
983 register struct command_line
*next
;
984 struct command_line
**blist
;
989 if (l
->body_count
> 0)
991 blist
= l
->body_list
;
992 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
993 free_command_lines (blist
);
1004 do_free_command_lines_cleanup (void *arg
)
1006 free_command_lines (arg
);
1009 static struct cleanup
*
1010 make_cleanup_free_command_lines (struct command_line
**arg
)
1012 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1016 validate_comname (char *comname
)
1021 error_no_arg ("name of command to define");
1026 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1027 error ("Junk in argument list: \"%s\"", p
);
1032 /* This is just a placeholder in the command data structures. */
1034 user_defined_command (char *ignore
, int from_tty
)
1039 define_command (char *comname
, int from_tty
)
1041 #define MAX_TMPBUF 128
1048 register struct command_line
*cmds
;
1049 register struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1050 char *tem
= comname
;
1052 char tmpbuf
[MAX_TMPBUF
];
1053 int hook_type
= CMD_NO_HOOK
;
1054 int hook_name_size
= 0;
1056 #define HOOK_STRING "hook-"
1058 #define HOOK_POST_STRING "hookpost-"
1059 #define HOOK_POST_LEN 9
1061 validate_comname (comname
);
1063 /* Look it up, and verify that we got an exact match. */
1064 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1065 if (c
&& !STREQ (comname
, c
->name
))
1070 if (c
->class == class_user
|| c
->class == class_alias
)
1071 tem
= "Redefine command \"%s\"? ";
1073 tem
= "Really redefine built-in command \"%s\"? ";
1074 if (!query (tem
, c
->name
))
1075 error ("Command \"%s\" not redefined.", c
->name
);
1078 /* If this new command is a hook, then mark the command which it
1079 is hooking. Note that we allow hooking `help' commands, so that
1080 we can hook the `stop' pseudo-command. */
1082 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1084 hook_type
= CMD_PRE_HOOK
;
1085 hook_name_size
= HOOK_LEN
;
1087 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1089 hook_type
= CMD_POST_HOOK
;
1090 hook_name_size
= HOOK_POST_LEN
;
1093 if (hook_type
!= CMD_NO_HOOK
)
1095 /* Look up cmd it hooks, and verify that we got an exact match. */
1096 tem
= comname
+ hook_name_size
;
1097 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1098 if (hookc
&& !STREQ (comname
+ hook_name_size
, hookc
->name
))
1102 warning ("Your new `%s' command does not hook any existing command.",
1104 if (!query ("Proceed? "))
1105 error ("Not confirmed.");
1109 comname
= savestring (comname
, strlen (comname
));
1111 /* If the rest of the commands will be case insensitive, this one
1112 should behave in the same manner. */
1113 for (tem
= comname
; *tem
; tem
++)
1115 *tem
= tolower (*tem
);
1117 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1118 cmds
= read_command_lines (tmpbuf
, from_tty
);
1120 if (c
&& c
->class == class_user
)
1121 free_command_lines (&c
->user_commands
);
1123 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1124 (c
&& c
->class == class_user
)
1125 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1126 newc
->user_commands
= cmds
;
1128 /* If this new command is a hook, then mark both commands as being
1135 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1136 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1139 hookc
->hook_post
= newc
; /* Target gets hooked. */
1140 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1143 /* Should never come here as hookc would be 0. */
1144 internal_error (__FILE__
, __LINE__
, "bad switch");
1150 document_command (char *comname
, int from_tty
)
1152 struct command_line
*doclines
;
1153 register struct cmd_list_element
*c
;
1154 char *tem
= comname
;
1157 validate_comname (comname
);
1159 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1161 if (c
->class != class_user
)
1162 error ("Command \"%s\" is built-in.", comname
);
1164 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1165 doclines
= read_command_lines (tmpbuf
, from_tty
);
1171 register struct command_line
*cl1
;
1172 register int len
= 0;
1174 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1175 len
+= strlen (cl1
->line
) + 1;
1177 c
->doc
= (char *) xmalloc (len
+ 1);
1180 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1182 strcat (c
->doc
, cl1
->line
);
1184 strcat (c
->doc
, "\n");
1188 free_command_lines (&doclines
);
1191 struct source_cleanup_lines_args
1195 char *old_pre_error
;
1196 char *old_error_pre_print
;
1200 source_cleanup_lines (PTR args
)
1202 struct source_cleanup_lines_args
*p
=
1203 (struct source_cleanup_lines_args
*) args
;
1204 source_line_number
= p
->old_line
;
1205 source_file_name
= p
->old_file
;
1206 source_pre_error
= p
->old_pre_error
;
1207 error_pre_print
= p
->old_error_pre_print
;
1212 do_fclose_cleanup (void *stream
)
1217 /* Used to implement source_command */
1220 script_from_file (FILE *stream
, char *file
)
1222 struct cleanup
*old_cleanups
;
1223 struct source_cleanup_lines_args old_lines
;
1228 internal_error (__FILE__
, __LINE__
, "called with NULL file pointer!");
1231 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1233 old_lines
.old_line
= source_line_number
;
1234 old_lines
.old_file
= source_file_name
;
1235 old_lines
.old_pre_error
= source_pre_error
;
1236 old_lines
.old_error_pre_print
= error_pre_print
;
1237 make_cleanup (source_cleanup_lines
, &old_lines
);
1238 source_line_number
= 0;
1239 source_file_name
= file
;
1240 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
1241 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
1242 make_cleanup (xfree
, source_pre_error
);
1243 /* This will get set every time we read a line. So it won't stay "" for
1245 error_pre_print
= "";
1247 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
1248 if (source_error_allocated
< needed_length
)
1250 source_error_allocated
*= 2;
1251 if (source_error_allocated
< needed_length
)
1252 source_error_allocated
= needed_length
;
1253 if (source_error
== NULL
)
1254 source_error
= xmalloc (source_error_allocated
);
1256 source_error
= xrealloc (source_error
, source_error_allocated
);
1259 read_command_file (stream
);
1261 do_cleanups (old_cleanups
);
1265 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1267 register struct command_line
*cmdlines
;
1269 cmdlines
= c
->user_commands
;
1272 fputs_filtered ("User command ", stream
);
1273 fputs_filtered (c
->name
, stream
);
1274 fputs_filtered (":\n", stream
);
1276 print_command_lines (uiout
, cmdlines
, 1);
1277 fputs_filtered ("\n", stream
);