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"
37 /* Prototypes for local functions */
39 static enum command_control_type
40 recurse_read_control_structure (struct command_line
*current_cmd
);
42 static char *insert_args (char *line
);
44 static struct cleanup
* setup_user_args (char *p
);
46 static void validate_comname (char *);
48 /* Level of control structure. */
49 static int control_level
;
51 /* Source command state variable. */
52 static int source_error_allocated
;
54 /* Structure for arguments to user defined functions. */
55 #define MAXUSERARGS 10
58 struct user_args
*next
;
70 /* Allocate, initialize a new command line structure for one of the
71 control commands (if/while). */
73 static struct command_line
*
74 build_command_line (enum command_control_type type
, char *args
)
76 struct command_line
*cmd
;
79 error ("if/while commands require arguments.\n");
81 cmd
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
83 cmd
->control_type
= type
;
87 = (struct command_line
**) xmalloc (sizeof (struct command_line
*)
89 memset (cmd
->body_list
, 0, sizeof (struct command_line
*) * cmd
->body_count
);
90 cmd
->line
= savestring (args
, strlen (args
));
94 /* Build and return a new command structure for the control commands
95 such as "if" and "while". */
97 static struct command_line
*
98 get_command_line (enum command_control_type type
, char *arg
)
100 struct command_line
*cmd
;
101 struct cleanup
*old_chain
= NULL
;
103 /* Allocate and build a new command line structure. */
104 cmd
= build_command_line (type
, arg
);
106 old_chain
= make_cleanup_free_command_lines (&cmd
);
108 /* Read in the body of this command. */
109 if (recurse_read_control_structure (cmd
) == invalid_control
)
111 warning ("error reading in control structure\n");
112 do_cleanups (old_chain
);
116 discard_cleanups (old_chain
);
120 /* Recursively print a command (including full control structures). */
123 print_command_lines (struct ui_out
*uiout
, struct command_line
*cmd
,
126 struct command_line
*list
;
133 ui_out_spaces (uiout
, 2 * depth
);
135 /* A simple command, print it and continue. */
136 if (list
->control_type
== simple_control
)
138 ui_out_field_string (uiout
, NULL
, list
->line
);
139 ui_out_text (uiout
, "\n");
144 /* loop_continue to jump to the start of a while loop, print it
146 if (list
->control_type
== continue_control
)
148 ui_out_field_string (uiout
, NULL
, "loop_continue");
149 ui_out_text (uiout
, "\n");
154 /* loop_break to break out of a while loop, print it and continue. */
155 if (list
->control_type
== break_control
)
157 ui_out_field_string (uiout
, NULL
, "loop_break");
158 ui_out_text (uiout
, "\n");
163 /* A while command. Recursively print its subcommands and continue. */
164 if (list
->control_type
== while_control
)
166 ui_out_field_fmt (uiout
, NULL
, "while %s", list
->line
);
167 ui_out_text (uiout
, "\n");
168 print_command_lines (uiout
, *list
->body_list
, depth
+ 1);
170 ui_out_spaces (uiout
, 2 * depth
);
171 ui_out_field_string (uiout
, NULL
, "end");
172 ui_out_text (uiout
, "\n");
177 /* An if command. Recursively print both arms before continueing. */
178 if (list
->control_type
== if_control
)
180 ui_out_field_fmt (uiout
, NULL
, "if %s", list
->line
);
181 ui_out_text (uiout
, "\n");
183 print_command_lines (uiout
, list
->body_list
[0], depth
+ 1);
185 /* Show the false arm if it exists. */
186 if (list
->body_count
== 2)
189 ui_out_spaces (uiout
, 2 * depth
);
190 ui_out_field_string (uiout
, NULL
, "else");
191 ui_out_text (uiout
, "\n");
192 print_command_lines (uiout
, list
->body_list
[1], depth
+ 1);
196 ui_out_spaces (uiout
, 2 * depth
);
197 ui_out_field_string (uiout
, NULL
, "end");
198 ui_out_text (uiout
, "\n");
203 /* ignore illegal command type and try next */
208 /* Handle pre-post hooks. */
211 clear_hook_in_cleanup (void *data
)
213 struct cmd_list_element
*c
= data
;
214 c
->hook_in
= 0; /* Allow hook to work again once it is complete */
218 execute_cmd_pre_hook (struct cmd_list_element
*c
)
220 if ((c
->hook_pre
) && (!c
->hook_in
))
222 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
223 c
->hook_in
= 1; /* Prevent recursive hooking */
224 execute_user_command (c
->hook_pre
, (char *) 0);
225 do_cleanups (cleanups
);
230 execute_cmd_post_hook (struct cmd_list_element
*c
)
232 if ((c
->hook_post
) && (!c
->hook_in
))
234 struct cleanup
*cleanups
= make_cleanup (clear_hook_in_cleanup
, c
);
235 c
->hook_in
= 1; /* Prevent recursive hooking */
236 execute_user_command (c
->hook_post
, (char *) 0);
237 do_cleanups (cleanups
);
241 /* Execute the command in CMD. */
243 do_restore_user_call_depth (void * call_depth
)
245 int * depth
= call_depth
;
246 /* We will be returning_to_top_level() at this point, so we want to
253 execute_user_command (struct cmd_list_element
*c
, char *args
)
255 struct command_line
*cmdlines
;
256 struct cleanup
*old_chain
;
257 enum command_control_type ret
;
258 static int user_call_depth
= 0;
259 extern int max_user_call_depth
;
261 old_chain
= setup_user_args (args
);
263 cmdlines
= c
->user_commands
;
268 if (++user_call_depth
> max_user_call_depth
)
269 error ("Max user call depth exceeded -- command aborted\n");
271 old_chain
= make_cleanup (do_restore_user_call_depth
, &user_call_depth
);
273 /* Set the instream to 0, indicating execution of a
274 user-defined function. */
275 old_chain
= make_cleanup (do_restore_instream_cleanup
, instream
);
276 instream
= (FILE *) 0;
279 ret
= execute_control_command (cmdlines
);
280 if (ret
!= simple_control
&& ret
!= break_control
)
282 warning ("Error in control structure.\n");
285 cmdlines
= cmdlines
->next
;
287 do_cleanups (old_chain
);
292 enum command_control_type
293 execute_control_command (struct command_line
*cmd
)
295 struct expression
*expr
;
296 struct command_line
*current
;
297 struct cleanup
*old_chain
= 0;
299 struct value
*val_mark
;
301 enum command_control_type ret
;
304 switch (cmd
->control_type
)
307 /* A simple command, execute it and return. */
308 new_line
= insert_args (cmd
->line
);
310 return invalid_control
;
311 old_chain
= make_cleanup (free_current_contents
, &new_line
);
312 execute_command (new_line
, 0);
313 ret
= cmd
->control_type
;
316 case continue_control
:
318 /* Return for "continue", and "break" so we can either
319 continue the loop at the top, or break out. */
320 ret
= cmd
->control_type
;
325 /* Parse the loop control expression for the while statement. */
326 new_line
= insert_args (cmd
->line
);
328 return invalid_control
;
329 old_chain
= make_cleanup (free_current_contents
, &new_line
);
330 expr
= parse_expression (new_line
);
331 make_cleanup (free_current_contents
, &expr
);
333 ret
= simple_control
;
336 /* Keep iterating so long as the expression is true. */
343 /* Evaluate the expression. */
344 val_mark
= value_mark ();
345 val
= evaluate_expression (expr
);
346 cond_result
= value_true (val
);
347 value_free_to_mark (val_mark
);
349 /* If the value is false, then break out of the loop. */
353 /* Execute the body of the while statement. */
354 current
= *cmd
->body_list
;
357 ret
= execute_control_command (current
);
359 /* If we got an error, or a "break" command, then stop
361 if (ret
== invalid_control
|| ret
== break_control
)
367 /* If we got a "continue" command, then restart the loop
369 if (ret
== continue_control
)
372 /* Get the next statement. */
373 current
= current
->next
;
377 /* Reset RET so that we don't recurse the break all the way down. */
378 if (ret
== break_control
)
379 ret
= simple_control
;
386 new_line
= insert_args (cmd
->line
);
388 return invalid_control
;
389 old_chain
= make_cleanup (free_current_contents
, &new_line
);
390 /* Parse the conditional for the if statement. */
391 expr
= parse_expression (new_line
);
392 make_cleanup (free_current_contents
, &expr
);
395 ret
= simple_control
;
397 /* Evaluate the conditional. */
398 val_mark
= value_mark ();
399 val
= evaluate_expression (expr
);
401 /* Choose which arm to take commands from based on the value of the
402 conditional expression. */
403 if (value_true (val
))
404 current
= *cmd
->body_list
;
405 else if (cmd
->body_count
== 2)
406 current
= *(cmd
->body_list
+ 1);
407 value_free_to_mark (val_mark
);
409 /* Execute commands in the given arm. */
412 ret
= execute_control_command (current
);
414 /* If we got an error, get out. */
415 if (ret
!= simple_control
)
418 /* Get the next statement in the body. */
419 current
= current
->next
;
426 warning ("Invalid control type in command structure.");
427 return invalid_control
;
431 do_cleanups (old_chain
);
436 /* "while" command support. Executes a body of statements while the
437 loop condition is nonzero. */
440 while_command (char *arg
, int from_tty
)
442 struct command_line
*command
= NULL
;
445 command
= get_command_line (while_control
, arg
);
450 execute_control_command (command
);
451 free_command_lines (&command
);
454 /* "if" command support. Execute either the true or false arm depending
455 on the value of the if conditional. */
458 if_command (char *arg
, int from_tty
)
460 struct command_line
*command
= NULL
;
463 command
= get_command_line (if_control
, arg
);
468 execute_control_command (command
);
469 free_command_lines (&command
);
474 arg_cleanup (void *ignore
)
476 struct user_args
*oargs
= user_args
;
478 internal_error (__FILE__
, __LINE__
,
479 "arg_cleanup called with no user args.\n");
481 user_args
= user_args
->next
;
485 /* Bind the incomming arguments for a user defined command to
486 $arg0, $arg1 ... $argMAXUSERARGS. */
488 static struct cleanup
*
489 setup_user_args (char *p
)
491 struct user_args
*args
;
492 struct cleanup
*old_chain
;
493 unsigned int arg_count
= 0;
495 args
= (struct user_args
*) xmalloc (sizeof (struct user_args
));
496 memset (args
, 0, sizeof (struct user_args
));
498 args
->next
= user_args
;
501 old_chain
= make_cleanup (arg_cleanup
, 0/*ignored*/);
513 if (arg_count
>= MAXUSERARGS
)
515 error ("user defined function may only have %d arguments.\n",
520 /* Strip whitespace. */
521 while (*p
== ' ' || *p
== '\t')
524 /* P now points to an argument. */
526 user_args
->a
[arg_count
].arg
= p
;
528 /* Get to the end of this argument. */
531 if (((*p
== ' ' || *p
== '\t')) && !squote
&& !dquote
&& !bsquote
)
560 user_args
->a
[arg_count
].len
= p
- start_arg
;
567 /* Given character string P, return a point to the first argument ($arg),
568 or NULL if P contains no arguments. */
573 while ((p
= strchr (p
, '$')))
575 if (strncmp (p
, "$arg", 4) == 0 && isdigit (p
[4]))
582 /* Insert the user defined arguments stored in user_arg into the $arg
583 arguments found in line, with the updated copy being placed into nline. */
586 insert_args (char *line
)
588 char *p
, *save_line
, *new_line
;
591 /* First we need to know how much memory to allocate for the new line. */
594 while ((p
= locate_arg (line
)))
599 if (i
>= user_args
->count
)
601 error ("Missing argument %d in user function.\n", i
);
604 len
+= user_args
->a
[i
].len
;
608 /* Don't forget the tail. */
609 len
+= strlen (line
);
611 /* Allocate space for the new line and fill it in. */
612 new_line
= (char *) xmalloc (len
+ 1);
613 if (new_line
== NULL
)
616 /* Restore pointer to beginning of old line. */
619 /* Save pointer to beginning of new line. */
620 save_line
= new_line
;
622 while ((p
= locate_arg (line
)))
626 memcpy (new_line
, line
, p
- line
);
627 new_line
+= p
- line
;
630 len
= user_args
->a
[i
].len
;
633 memcpy (new_line
, user_args
->a
[i
].arg
, len
);
638 /* Don't forget the tail. */
639 strcpy (new_line
, line
);
641 /* Return a pointer to the beginning of the new line. */
646 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
647 code bodies. This is typically used when we encounter an "else"
648 clause for an "if" command. */
651 realloc_body_list (struct command_line
*command
, int new_length
)
654 struct command_line
**body_list
;
656 n
= command
->body_count
;
662 body_list
= (struct command_line
**)
663 xmalloc (sizeof (struct command_line
*) * new_length
);
665 memcpy (body_list
, command
->body_list
, sizeof (struct command_line
*) * n
);
667 xfree (command
->body_list
);
668 command
->body_list
= body_list
;
669 command
->body_count
= new_length
;
672 /* Read one line from the input stream. If the command is an "else" or
673 "end", return such an indication to the caller. */
675 static enum misc_command_type
676 read_next_line (struct command_line
**command
)
678 char *p
, *p1
, *prompt_ptr
, control_prompt
[256];
681 if (control_level
>= 254)
682 error ("Control nesting too deep!\n");
684 /* Set a prompt based on the nesting of the control commands. */
685 if (instream
== stdin
|| (instream
== 0 && readline_hook
!= NULL
))
687 for (i
= 0; i
< control_level
; i
++)
688 control_prompt
[i
] = ' ';
689 control_prompt
[i
] = '>';
690 control_prompt
[i
+ 1] = '\0';
691 prompt_ptr
= (char *) &control_prompt
[0];
696 p
= command_line_input (prompt_ptr
, instream
== stdin
, "commands");
698 /* Not sure what to do here. */
702 /* Strip leading and trailing whitespace. */
703 while (*p
== ' ' || *p
== '\t')
707 while (p1
!= p
&& (p1
[-1] == ' ' || p1
[-1] == '\t'))
710 /* Blanks and comments don't really do anything, but we need to
711 distinguish them from else, end and other commands which can be
713 if (p1
== p
|| p
[0] == '#')
716 /* Is this the end of a simple, while, or if control structure? */
717 if (p1
- p
== 3 && !strncmp (p
, "end", 3))
720 /* Is the else clause of an if control structure? */
721 if (p1
- p
== 4 && !strncmp (p
, "else", 4))
724 /* Check for while, if, break, continue, etc and build a new command
725 line structure for them. */
726 if (p1
- p
> 5 && !strncmp (p
, "while", 5))
727 *command
= build_command_line (while_control
, p
+ 6);
728 else if (p1
- p
> 2 && !strncmp (p
, "if", 2))
729 *command
= build_command_line (if_control
, p
+ 3);
730 else if (p1
- p
== 10 && !strncmp (p
, "loop_break", 10))
732 *command
= (struct command_line
*)
733 xmalloc (sizeof (struct command_line
));
734 (*command
)->next
= NULL
;
735 (*command
)->line
= NULL
;
736 (*command
)->control_type
= break_control
;
737 (*command
)->body_count
= 0;
738 (*command
)->body_list
= NULL
;
740 else if (p1
- p
== 13 && !strncmp (p
, "loop_continue", 13))
742 *command
= (struct command_line
*)
743 xmalloc (sizeof (struct command_line
));
744 (*command
)->next
= NULL
;
745 (*command
)->line
= NULL
;
746 (*command
)->control_type
= continue_control
;
747 (*command
)->body_count
= 0;
748 (*command
)->body_list
= NULL
;
752 /* A normal command. */
753 *command
= (struct command_line
*)
754 xmalloc (sizeof (struct command_line
));
755 (*command
)->next
= NULL
;
756 (*command
)->line
= savestring (p
, p1
- p
);
757 (*command
)->control_type
= simple_control
;
758 (*command
)->body_count
= 0;
759 (*command
)->body_list
= NULL
;
762 /* Nothing special. */
766 /* Recursively read in the control structures and create a command_line
769 The parent_control parameter is the control structure in which the
770 following commands are nested. */
772 static enum command_control_type
773 recurse_read_control_structure (struct command_line
*current_cmd
)
776 enum misc_command_type val
;
777 enum command_control_type ret
;
778 struct command_line
**body_ptr
, *child_tail
, *next
;
784 if (current_cmd
->control_type
== simple_control
)
786 error ("Recursed on a simple control type\n");
787 return invalid_control
;
790 if (current_body
> current_cmd
->body_count
)
792 error ("Allocated body is smaller than this command type needs\n");
793 return invalid_control
;
796 /* Read lines from the input stream and build control structures. */
802 val
= read_next_line (&next
);
804 /* Just skip blanks and comments. */
805 if (val
== nop_command
)
808 if (val
== end_command
)
810 if (current_cmd
->control_type
== while_control
811 || current_cmd
->control_type
== if_control
)
813 /* Success reading an entire control structure. */
814 ret
= simple_control
;
819 ret
= invalid_control
;
824 /* Not the end of a control structure. */
825 if (val
== else_command
)
827 if (current_cmd
->control_type
== if_control
828 && current_body
== 1)
830 realloc_body_list (current_cmd
, 2);
837 ret
= invalid_control
;
844 child_tail
->next
= next
;
848 body_ptr
= current_cmd
->body_list
;
849 for (i
= 1; i
< current_body
; i
++)
858 /* If the latest line is another control structure, then recurse
860 if (next
->control_type
== while_control
861 || next
->control_type
== if_control
)
864 ret
= recurse_read_control_structure (next
);
867 if (ret
!= simple_control
)
877 /* Read lines from the input stream and accumulate them in a chain of
878 struct command_line's, which is then returned. For input from a
879 terminal, the special command "end" is used to mark the end of the
880 input, and is not included in the returned chain of commands. */
882 #define END_MESSAGE "End with a line saying just \"end\"."
884 struct command_line
*
885 read_command_lines (char *prompt_arg
, int from_tty
)
887 struct command_line
*head
, *tail
, *next
;
888 struct cleanup
*old_chain
;
889 enum command_control_type ret
;
890 enum misc_command_type val
;
893 if (readline_begin_hook
)
895 /* Note - intentional to merge messages with no newline */
896 (*readline_begin_hook
) ("%s %s\n", prompt_arg
, END_MESSAGE
);
898 else if (from_tty
&& input_from_terminal_p ())
900 printf_unfiltered ("%s\n%s\n", prompt_arg
, END_MESSAGE
);
901 gdb_flush (gdb_stdout
);
909 val
= read_next_line (&next
);
911 /* Ignore blank lines or comments. */
912 if (val
== nop_command
)
915 if (val
== end_command
)
917 ret
= simple_control
;
921 if (val
!= ok_command
)
923 ret
= invalid_control
;
927 if (next
->control_type
== while_control
928 || next
->control_type
== if_control
)
931 ret
= recurse_read_control_structure (next
);
934 if (ret
== invalid_control
)
945 old_chain
= make_cleanup_free_command_lines (&head
);
954 if (ret
!= invalid_control
)
956 discard_cleanups (old_chain
);
959 do_cleanups (old_chain
);
962 if (readline_end_hook
)
964 (*readline_end_hook
) ();
969 /* Free a chain of struct command_line's. */
972 free_command_lines (struct command_line
**lptr
)
974 struct command_line
*l
= *lptr
;
975 struct command_line
*next
;
976 struct command_line
**blist
;
981 if (l
->body_count
> 0)
983 blist
= l
->body_list
;
984 for (i
= 0; i
< l
->body_count
; i
++, blist
++)
985 free_command_lines (blist
);
996 do_free_command_lines_cleanup (void *arg
)
998 free_command_lines (arg
);
1002 make_cleanup_free_command_lines (struct command_line
**arg
)
1004 return make_cleanup (do_free_command_lines_cleanup
, arg
);
1007 struct command_line
*
1008 copy_command_lines (struct command_line
*cmds
)
1010 struct command_line
*result
= NULL
;
1014 result
= (struct command_line
*) xmalloc (sizeof (struct command_line
));
1016 result
->next
= copy_command_lines (cmds
->next
);
1017 result
->line
= xstrdup (cmds
->line
);
1018 result
->control_type
= cmds
->control_type
;
1019 result
->body_count
= cmds
->body_count
;
1020 if (cmds
->body_count
> 0)
1024 result
->body_list
= (struct command_line
**)
1025 xmalloc (sizeof (struct command_line
*) * cmds
->body_count
);
1027 for (i
= 0; i
< cmds
->body_count
; i
++)
1028 result
->body_list
[i
] = copy_command_lines (cmds
->body_list
[i
]);
1031 result
->body_list
= NULL
;
1038 validate_comname (char *comname
)
1043 error_no_arg ("name of command to define");
1048 if (!isalnum (*p
) && *p
!= '-' && *p
!= '_')
1049 error ("Junk in argument list: \"%s\"", p
);
1054 /* This is just a placeholder in the command data structures. */
1056 user_defined_command (char *ignore
, int from_tty
)
1061 define_command (char *comname
, int from_tty
)
1063 #define MAX_TMPBUF 128
1070 struct command_line
*cmds
;
1071 struct cmd_list_element
*c
, *newc
, *oldc
, *hookc
= 0;
1072 char *tem
= comname
;
1074 char tmpbuf
[MAX_TMPBUF
];
1075 int hook_type
= CMD_NO_HOOK
;
1076 int hook_name_size
= 0;
1078 #define HOOK_STRING "hook-"
1080 #define HOOK_POST_STRING "hookpost-"
1081 #define HOOK_POST_LEN 9
1083 validate_comname (comname
);
1085 /* Look it up, and verify that we got an exact match. */
1086 c
= lookup_cmd (&tem
, cmdlist
, "", -1, 1);
1087 if (c
&& strcmp (comname
, c
->name
) != 0)
1093 if (c
->class == class_user
|| c
->class == class_alias
)
1094 q
= query ("Redefine command \"%s\"? ", c
->name
);
1096 q
= query ("Really redefine built-in command \"%s\"? ", c
->name
);
1098 error ("Command \"%s\" not redefined.", c
->name
);
1101 /* If this new command is a hook, then mark the command which it
1102 is hooking. Note that we allow hooking `help' commands, so that
1103 we can hook the `stop' pseudo-command. */
1105 if (!strncmp (comname
, HOOK_STRING
, HOOK_LEN
))
1107 hook_type
= CMD_PRE_HOOK
;
1108 hook_name_size
= HOOK_LEN
;
1110 else if (!strncmp (comname
, HOOK_POST_STRING
, HOOK_POST_LEN
))
1112 hook_type
= CMD_POST_HOOK
;
1113 hook_name_size
= HOOK_POST_LEN
;
1116 if (hook_type
!= CMD_NO_HOOK
)
1118 /* Look up cmd it hooks, and verify that we got an exact match. */
1119 tem
= comname
+ hook_name_size
;
1120 hookc
= lookup_cmd (&tem
, cmdlist
, "", -1, 0);
1121 if (hookc
&& strcmp (comname
+ hook_name_size
, hookc
->name
) != 0)
1125 warning ("Your new `%s' command does not hook any existing command.",
1127 if (!query ("Proceed? "))
1128 error ("Not confirmed.");
1132 comname
= savestring (comname
, strlen (comname
));
1134 /* If the rest of the commands will be case insensitive, this one
1135 should behave in the same manner. */
1136 for (tem
= comname
; *tem
; tem
++)
1138 *tem
= tolower (*tem
);
1140 sprintf (tmpbuf
, "Type commands for definition of \"%s\".", comname
);
1141 cmds
= read_command_lines (tmpbuf
, from_tty
);
1143 if (c
&& c
->class == class_user
)
1144 free_command_lines (&c
->user_commands
);
1146 newc
= add_cmd (comname
, class_user
, user_defined_command
,
1147 (c
&& c
->class == class_user
)
1148 ? c
->doc
: savestring ("User-defined.", 13), &cmdlist
);
1149 newc
->user_commands
= cmds
;
1151 /* If this new command is a hook, then mark both commands as being
1158 hookc
->hook_pre
= newc
; /* Target gets hooked. */
1159 newc
->hookee_pre
= hookc
; /* We are marked as hooking target cmd. */
1162 hookc
->hook_post
= newc
; /* Target gets hooked. */
1163 newc
->hookee_post
= hookc
; /* We are marked as hooking target cmd. */
1166 /* Should never come here as hookc would be 0. */
1167 internal_error (__FILE__
, __LINE__
, "bad switch");
1173 document_command (char *comname
, int from_tty
)
1175 struct command_line
*doclines
;
1176 struct cmd_list_element
*c
;
1177 char *tem
= comname
;
1180 validate_comname (comname
);
1182 c
= lookup_cmd (&tem
, cmdlist
, "", 0, 1);
1184 if (c
->class != class_user
)
1185 error ("Command \"%s\" is built-in.", comname
);
1187 sprintf (tmpbuf
, "Type documentation for \"%s\".", comname
);
1188 doclines
= read_command_lines (tmpbuf
, from_tty
);
1194 struct command_line
*cl1
;
1197 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1198 len
+= strlen (cl1
->line
) + 1;
1200 c
->doc
= (char *) xmalloc (len
+ 1);
1203 for (cl1
= doclines
; cl1
; cl1
= cl1
->next
)
1205 strcat (c
->doc
, cl1
->line
);
1207 strcat (c
->doc
, "\n");
1211 free_command_lines (&doclines
);
1214 struct source_cleanup_lines_args
1218 char *old_pre_error
;
1219 char *old_error_pre_print
;
1223 source_cleanup_lines (void *args
)
1225 struct source_cleanup_lines_args
*p
=
1226 (struct source_cleanup_lines_args
*) args
;
1227 source_line_number
= p
->old_line
;
1228 source_file_name
= p
->old_file
;
1229 source_pre_error
= p
->old_pre_error
;
1230 error_pre_print
= p
->old_error_pre_print
;
1234 do_fclose_cleanup (void *stream
)
1239 /* Used to implement source_command */
1242 script_from_file (FILE *stream
, char *file
)
1244 struct cleanup
*old_cleanups
;
1245 struct source_cleanup_lines_args old_lines
;
1250 internal_error (__FILE__
, __LINE__
, "called with NULL file pointer!");
1253 old_cleanups
= make_cleanup (do_fclose_cleanup
, stream
);
1255 old_lines
.old_line
= source_line_number
;
1256 old_lines
.old_file
= source_file_name
;
1257 old_lines
.old_pre_error
= source_pre_error
;
1258 old_lines
.old_error_pre_print
= error_pre_print
;
1259 make_cleanup (source_cleanup_lines
, &old_lines
);
1260 source_line_number
= 0;
1261 source_file_name
= file
;
1262 source_pre_error
= error_pre_print
== NULL
? "" : error_pre_print
;
1263 source_pre_error
= savestring (source_pre_error
, strlen (source_pre_error
));
1264 make_cleanup (xfree
, source_pre_error
);
1265 /* This will get set every time we read a line. So it won't stay "" for
1267 error_pre_print
= "";
1269 needed_length
= strlen (source_file_name
) + strlen (source_pre_error
) + 80;
1270 if (source_error_allocated
< needed_length
)
1272 source_error_allocated
*= 2;
1273 if (source_error_allocated
< needed_length
)
1274 source_error_allocated
= needed_length
;
1275 if (source_error
== NULL
)
1276 source_error
= xmalloc (source_error_allocated
);
1278 source_error
= xrealloc (source_error
, source_error_allocated
);
1281 read_command_file (stream
);
1283 do_cleanups (old_cleanups
);
1287 show_user_1 (struct cmd_list_element
*c
, struct ui_file
*stream
)
1289 struct command_line
*cmdlines
;
1291 cmdlines
= c
->user_commands
;
1294 fputs_filtered ("User command ", stream
);
1295 fputs_filtered (c
->name
, stream
);
1296 fputs_filtered (":\n", stream
);
1298 print_command_lines (uiout
, cmdlines
, 1);
1299 fputs_filtered ("\n", stream
);