* command.h (add_setshow_auto_boolean_cmd): Replace
[binutils-gdb.git] / gdb / cli / cli-script.c
1 /* GDB CLI command scripting.
2
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
5 Foundation, Inc.
6
7 This file is part of GDB.
8
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.
13
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.
18
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. */
23
24 #include "defs.h"
25 #include "value.h"
26 #include "language.h" /* For value_true */
27 #include <ctype.h>
28
29 #include "ui-out.h"
30
31 #include "top.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-script.h"
35
36 /* From gdb/top.c */
37
38 extern void dont_repeat (void);
39
40 extern void do_restore_instream_cleanup (void *stream);
41
42 /* Prototypes for local functions */
43
44 static struct cleanup *
45 make_cleanup_free_command_lines (struct command_line **arg);
46
47 static enum command_control_type
48 recurse_read_control_structure (struct command_line *current_cmd);
49
50 static char *insert_args (char *line);
51
52 static struct cleanup * setup_user_args (char *p);
53
54 static void validate_comname (char *);
55
56 /* Level of control structure. */
57 static int control_level;
58
59 /* Source command state variable. */
60 static int source_error_allocated;
61
62 /* Structure for arguments to user defined functions. */
63 #define MAXUSERARGS 10
64 struct user_args
65 {
66 struct user_args *next;
67 struct
68 {
69 char *arg;
70 int len;
71 }
72 a[MAXUSERARGS];
73 int count;
74 }
75 *user_args;
76
77 \f
78 /* Allocate, initialize a new command line structure for one of the
79 control commands (if/while). */
80
81 static struct command_line *
82 build_command_line (enum command_control_type type, char *args)
83 {
84 struct command_line *cmd;
85
86 if (args == NULL)
87 error ("if/while commands require arguments.\n");
88
89 cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
90 cmd->next = NULL;
91 cmd->control_type = type;
92
93 cmd->body_count = 1;
94 cmd->body_list
95 = (struct command_line **) xmalloc (sizeof (struct command_line *)
96 * cmd->body_count);
97 memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
98 cmd->line = savestring (args, strlen (args));
99 return cmd;
100 }
101
102 /* Build and return a new command structure for the control commands
103 such as "if" and "while". */
104
105 static struct command_line *
106 get_command_line (enum command_control_type type, char *arg)
107 {
108 struct command_line *cmd;
109 struct cleanup *old_chain = NULL;
110
111 /* Allocate and build a new command line structure. */
112 cmd = build_command_line (type, arg);
113
114 old_chain = make_cleanup_free_command_lines (&cmd);
115
116 /* Read in the body of this command. */
117 if (recurse_read_control_structure (cmd) == invalid_control)
118 {
119 warning ("error reading in control structure\n");
120 do_cleanups (old_chain);
121 return NULL;
122 }
123
124 discard_cleanups (old_chain);
125 return cmd;
126 }
127
128 /* Recursively print a command (including full control structures). */
129
130 void
131 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
132 unsigned int depth)
133 {
134 struct command_line *list;
135
136 list = cmd;
137 while (list)
138 {
139
140 if (depth)
141 ui_out_spaces (uiout, 2 * depth);
142
143 /* A simple command, print it and continue. */
144 if (list->control_type == simple_control)
145 {
146 ui_out_field_string (uiout, NULL, list->line);
147 ui_out_text (uiout, "\n");
148 list = list->next;
149 continue;
150 }
151
152 /* loop_continue to jump to the start of a while loop, print it
153 and continue. */
154 if (list->control_type == continue_control)
155 {
156 ui_out_field_string (uiout, NULL, "loop_continue");
157 ui_out_text (uiout, "\n");
158 list = list->next;
159 continue;
160 }
161
162 /* loop_break to break out of a while loop, print it and continue. */
163 if (list->control_type == break_control)
164 {
165 ui_out_field_string (uiout, NULL, "loop_break");
166 ui_out_text (uiout, "\n");
167 list = list->next;
168 continue;
169 }
170
171 /* A while command. Recursively print its subcommands and continue. */
172 if (list->control_type == while_control)
173 {
174 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
175 ui_out_text (uiout, "\n");
176 print_command_lines (uiout, *list->body_list, depth + 1);
177 if (depth)
178 ui_out_spaces (uiout, 2 * depth);
179 ui_out_field_string (uiout, NULL, "end");
180 ui_out_text (uiout, "\n");
181 list = list->next;
182 continue;
183 }
184
185 /* An if command. Recursively print both arms before continueing. */
186 if (list->control_type == if_control)
187 {
188 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
189 ui_out_text (uiout, "\n");
190 /* The true arm. */
191 print_command_lines (uiout, list->body_list[0], depth + 1);
192
193 /* Show the false arm if it exists. */
194 if (list->body_count == 2)
195 {
196 if (depth)
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);
201 }
202
203 if (depth)
204 ui_out_spaces (uiout, 2 * depth);
205 ui_out_field_string (uiout, NULL, "end");
206 ui_out_text (uiout, "\n");
207 list = list->next;
208 continue;
209 }
210
211 /* ignore illegal command type and try next */
212 list = list->next;
213 } /* while (list) */
214 }
215
216 /* Handle pre-post hooks. */
217
218 void
219 clear_hook_in_cleanup (void *data)
220 {
221 struct cmd_list_element *c = data;
222 c->hook_in = 0; /* Allow hook to work again once it is complete */
223 }
224
225 void
226 execute_cmd_pre_hook (struct cmd_list_element *c)
227 {
228 if ((c->hook_pre) && (!c->hook_in))
229 {
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);
234 }
235 }
236
237 void
238 execute_cmd_post_hook (struct cmd_list_element *c)
239 {
240 if ((c->hook_post) && (!c->hook_in))
241 {
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);
246 }
247 }
248
249 /* Execute the command in CMD. */
250 void
251 do_restore_user_call_depth (void * call_depth)
252 {
253 int * depth = call_depth;
254 /* We will be returning_to_top_level() at this point, so we want to
255 reset our depth. */
256 (*depth) = 0;
257 }
258
259
260 void
261 execute_user_command (struct cmd_list_element *c, char *args)
262 {
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;
268
269 old_chain = setup_user_args (args);
270
271 cmdlines = c->user_commands;
272 if (cmdlines == 0)
273 /* Null command */
274 return;
275
276 if (++user_call_depth > max_user_call_depth)
277 error ("Max user call depth exceeded -- command aborted\n");
278
279 old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth);
280
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;
285 while (cmdlines)
286 {
287 ret = execute_control_command (cmdlines);
288 if (ret != simple_control && ret != break_control)
289 {
290 warning ("Error in control structure.\n");
291 break;
292 }
293 cmdlines = cmdlines->next;
294 }
295 do_cleanups (old_chain);
296
297 user_call_depth--;
298 }
299
300 enum command_control_type
301 execute_control_command (struct command_line *cmd)
302 {
303 struct expression *expr;
304 struct command_line *current;
305 struct cleanup *old_chain = 0;
306 struct value *val;
307 struct value *val_mark;
308 int loop;
309 enum command_control_type ret;
310 char *new_line;
311
312 switch (cmd->control_type)
313 {
314 case simple_control:
315 /* A simple command, execute it and return. */
316 new_line = insert_args (cmd->line);
317 if (!new_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;
322 break;
323
324 case continue_control:
325 case break_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;
329 break;
330
331 case while_control:
332 {
333 /* Parse the loop control expression for the while statement. */
334 new_line = insert_args (cmd->line);
335 if (!new_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);
340
341 ret = simple_control;
342 loop = 1;
343
344 /* Keep iterating so long as the expression is true. */
345 while (loop == 1)
346 {
347 int cond_result;
348
349 QUIT;
350
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);
356
357 /* If the value is false, then break out of the loop. */
358 if (!cond_result)
359 break;
360
361 /* Execute the body of the while statement. */
362 current = *cmd->body_list;
363 while (current)
364 {
365 ret = execute_control_command (current);
366
367 /* If we got an error, or a "break" command, then stop
368 looping. */
369 if (ret == invalid_control || ret == break_control)
370 {
371 loop = 0;
372 break;
373 }
374
375 /* If we got a "continue" command, then restart the loop
376 at this point. */
377 if (ret == continue_control)
378 break;
379
380 /* Get the next statement. */
381 current = current->next;
382 }
383 }
384
385 /* Reset RET so that we don't recurse the break all the way down. */
386 if (ret == break_control)
387 ret = simple_control;
388
389 break;
390 }
391
392 case if_control:
393 {
394 new_line = insert_args (cmd->line);
395 if (!new_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);
401
402 current = NULL;
403 ret = simple_control;
404
405 /* Evaluate the conditional. */
406 val_mark = value_mark ();
407 val = evaluate_expression (expr);
408
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);
416
417 /* Execute commands in the given arm. */
418 while (current)
419 {
420 ret = execute_control_command (current);
421
422 /* If we got an error, get out. */
423 if (ret != simple_control)
424 break;
425
426 /* Get the next statement in the body. */
427 current = current->next;
428 }
429
430 break;
431 }
432
433 default:
434 warning ("Invalid control type in command structure.");
435 return invalid_control;
436 }
437
438 if (old_chain)
439 do_cleanups (old_chain);
440
441 return ret;
442 }
443
444 /* "while" command support. Executes a body of statements while the
445 loop condition is nonzero. */
446
447 void
448 while_command (char *arg, int from_tty)
449 {
450 struct command_line *command = NULL;
451
452 control_level = 1;
453 command = get_command_line (while_control, arg);
454
455 if (command == NULL)
456 return;
457
458 execute_control_command (command);
459 free_command_lines (&command);
460 }
461
462 /* "if" command support. Execute either the true or false arm depending
463 on the value of the if conditional. */
464
465 void
466 if_command (char *arg, int from_tty)
467 {
468 struct command_line *command = NULL;
469
470 control_level = 1;
471 command = get_command_line (if_control, arg);
472
473 if (command == NULL)
474 return;
475
476 execute_control_command (command);
477 free_command_lines (&command);
478 }
479
480 /* Cleanup */
481 static void
482 arg_cleanup (void *ignore)
483 {
484 struct user_args *oargs = user_args;
485 if (!user_args)
486 internal_error (__FILE__, __LINE__,
487 "arg_cleanup called with no user args.\n");
488
489 user_args = user_args->next;
490 xfree (oargs);
491 }
492
493 /* Bind the incomming arguments for a user defined command to
494 $arg0, $arg1 ... $argMAXUSERARGS. */
495
496 static struct cleanup *
497 setup_user_args (char *p)
498 {
499 struct user_args *args;
500 struct cleanup *old_chain;
501 unsigned int arg_count = 0;
502
503 args = (struct user_args *) xmalloc (sizeof (struct user_args));
504 memset (args, 0, sizeof (struct user_args));
505
506 args->next = user_args;
507 user_args = args;
508
509 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
510
511 if (p == NULL)
512 return old_chain;
513
514 while (*p)
515 {
516 char *start_arg;
517 int squote = 0;
518 int dquote = 0;
519 int bsquote = 0;
520
521 if (arg_count >= MAXUSERARGS)
522 {
523 error ("user defined function may only have %d arguments.\n",
524 MAXUSERARGS);
525 return old_chain;
526 }
527
528 /* Strip whitespace. */
529 while (*p == ' ' || *p == '\t')
530 p++;
531
532 /* P now points to an argument. */
533 start_arg = p;
534 user_args->a[arg_count].arg = p;
535
536 /* Get to the end of this argument. */
537 while (*p)
538 {
539 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
540 break;
541 else
542 {
543 if (bsquote)
544 bsquote = 0;
545 else if (*p == '\\')
546 bsquote = 1;
547 else if (squote)
548 {
549 if (*p == '\'')
550 squote = 0;
551 }
552 else if (dquote)
553 {
554 if (*p == '"')
555 dquote = 0;
556 }
557 else
558 {
559 if (*p == '\'')
560 squote = 1;
561 else if (*p == '"')
562 dquote = 1;
563 }
564 p++;
565 }
566 }
567
568 user_args->a[arg_count].len = p - start_arg;
569 arg_count++;
570 user_args->count++;
571 }
572 return old_chain;
573 }
574
575 /* Given character string P, return a point to the first argument ($arg),
576 or NULL if P contains no arguments. */
577
578 static char *
579 locate_arg (char *p)
580 {
581 while ((p = strchr (p, '$')))
582 {
583 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
584 return p;
585 p++;
586 }
587 return NULL;
588 }
589
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. */
592
593 static char *
594 insert_args (char *line)
595 {
596 char *p, *save_line, *new_line;
597 unsigned len, i;
598
599 /* First we need to know how much memory to allocate for the new line. */
600 save_line = line;
601 len = 0;
602 while ((p = locate_arg (line)))
603 {
604 len += p - line;
605 i = p[4] - '0';
606
607 if (i >= user_args->count)
608 {
609 error ("Missing argument %d in user function.\n", i);
610 return NULL;
611 }
612 len += user_args->a[i].len;
613 line = p + 5;
614 }
615
616 /* Don't forget the tail. */
617 len += strlen (line);
618
619 /* Allocate space for the new line and fill it in. */
620 new_line = (char *) xmalloc (len + 1);
621 if (new_line == NULL)
622 return NULL;
623
624 /* Restore pointer to beginning of old line. */
625 line = save_line;
626
627 /* Save pointer to beginning of new line. */
628 save_line = new_line;
629
630 while ((p = locate_arg (line)))
631 {
632 int i, len;
633
634 memcpy (new_line, line, p - line);
635 new_line += p - line;
636 i = p[4] - '0';
637
638 len = user_args->a[i].len;
639 if (len)
640 {
641 memcpy (new_line, user_args->a[i].arg, len);
642 new_line += len;
643 }
644 line = p + 5;
645 }
646 /* Don't forget the tail. */
647 strcpy (new_line, line);
648
649 /* Return a pointer to the beginning of the new line. */
650 return save_line;
651 }
652
653 \f
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. */
657
658 static void
659 realloc_body_list (struct command_line *command, int new_length)
660 {
661 int n;
662 struct command_line **body_list;
663
664 n = command->body_count;
665
666 /* Nothing to do? */
667 if (new_length <= n)
668 return;
669
670 body_list = (struct command_line **)
671 xmalloc (sizeof (struct command_line *) * new_length);
672
673 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
674
675 xfree (command->body_list);
676 command->body_list = body_list;
677 command->body_count = new_length;
678 }
679
680 /* Read one line from the input stream. If the command is an "else" or
681 "end", return such an indication to the caller. */
682
683 static enum misc_command_type
684 read_next_line (struct command_line **command)
685 {
686 char *p, *p1, *prompt_ptr, control_prompt[256];
687 int i = 0;
688
689 if (control_level >= 254)
690 error ("Control nesting too deep!\n");
691
692 /* Set a prompt based on the nesting of the control commands. */
693 if (instream == stdin || (instream == 0 && readline_hook != NULL))
694 {
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];
700 }
701 else
702 prompt_ptr = NULL;
703
704 p = command_line_input (prompt_ptr, instream == stdin, "commands");
705
706 /* Not sure what to do here. */
707 if (p == NULL)
708 return end_command;
709
710 /* Strip leading and trailing whitespace. */
711 while (*p == ' ' || *p == '\t')
712 p++;
713
714 p1 = p + strlen (p);
715 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
716 p1--;
717
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
720 executed. */
721 if (p1 == p || p[0] == '#')
722 return nop_command;
723
724 /* Is this the end of a simple, while, or if control structure? */
725 if (p1 - p == 3 && !strncmp (p, "end", 3))
726 return end_command;
727
728 /* Is the else clause of an if control structure? */
729 if (p1 - p == 4 && !strncmp (p, "else", 4))
730 return else_command;
731
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))
739 {
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;
747 }
748 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
749 {
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;
757 }
758 else
759 {
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;
768 }
769
770 /* Nothing special. */
771 return ok_command;
772 }
773
774 /* Recursively read in the control structures and create a command_line
775 structure from them.
776
777 The parent_control parameter is the control structure in which the
778 following commands are nested. */
779
780 static enum command_control_type
781 recurse_read_control_structure (struct command_line *current_cmd)
782 {
783 int current_body, i;
784 enum misc_command_type val;
785 enum command_control_type ret;
786 struct command_line **body_ptr, *child_tail, *next;
787
788 child_tail = NULL;
789 current_body = 1;
790
791 /* Sanity checks. */
792 if (current_cmd->control_type == simple_control)
793 {
794 error ("Recursed on a simple control type\n");
795 return invalid_control;
796 }
797
798 if (current_body > current_cmd->body_count)
799 {
800 error ("Allocated body is smaller than this command type needs\n");
801 return invalid_control;
802 }
803
804 /* Read lines from the input stream and build control structures. */
805 while (1)
806 {
807 dont_repeat ();
808
809 next = NULL;
810 val = read_next_line (&next);
811
812 /* Just skip blanks and comments. */
813 if (val == nop_command)
814 continue;
815
816 if (val == end_command)
817 {
818 if (current_cmd->control_type == while_control
819 || current_cmd->control_type == if_control)
820 {
821 /* Success reading an entire control structure. */
822 ret = simple_control;
823 break;
824 }
825 else
826 {
827 ret = invalid_control;
828 break;
829 }
830 }
831
832 /* Not the end of a control structure. */
833 if (val == else_command)
834 {
835 if (current_cmd->control_type == if_control
836 && current_body == 1)
837 {
838 realloc_body_list (current_cmd, 2);
839 current_body = 2;
840 child_tail = NULL;
841 continue;
842 }
843 else
844 {
845 ret = invalid_control;
846 break;
847 }
848 }
849
850 if (child_tail)
851 {
852 child_tail->next = next;
853 }
854 else
855 {
856 body_ptr = current_cmd->body_list;
857 for (i = 1; i < current_body; i++)
858 body_ptr++;
859
860 *body_ptr = next;
861
862 }
863
864 child_tail = next;
865
866 /* If the latest line is another control structure, then recurse
867 on it. */
868 if (next->control_type == while_control
869 || next->control_type == if_control)
870 {
871 control_level++;
872 ret = recurse_read_control_structure (next);
873 control_level--;
874
875 if (ret != simple_control)
876 break;
877 }
878 }
879
880 dont_repeat ();
881
882 return ret;
883 }
884
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. */
889
890 #define END_MESSAGE "End with a line saying just \"end\"."
891
892 struct command_line *
893 read_command_lines (char *prompt_arg, int from_tty)
894 {
895 struct command_line *head, *tail, *next;
896 struct cleanup *old_chain;
897 enum command_control_type ret;
898 enum misc_command_type val;
899
900 control_level = 0;
901 if (readline_begin_hook)
902 {
903 /* Note - intentional to merge messages with no newline */
904 (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
905 }
906 else if (from_tty && input_from_terminal_p ())
907 {
908 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
909 gdb_flush (gdb_stdout);
910 }
911
912 head = tail = NULL;
913 old_chain = NULL;
914
915 while (1)
916 {
917 val = read_next_line (&next);
918
919 /* Ignore blank lines or comments. */
920 if (val == nop_command)
921 continue;
922
923 if (val == end_command)
924 {
925 ret = simple_control;
926 break;
927 }
928
929 if (val != ok_command)
930 {
931 ret = invalid_control;
932 break;
933 }
934
935 if (next->control_type == while_control
936 || next->control_type == if_control)
937 {
938 control_level++;
939 ret = recurse_read_control_structure (next);
940 control_level--;
941
942 if (ret == invalid_control)
943 break;
944 }
945
946 if (tail)
947 {
948 tail->next = next;
949 }
950 else
951 {
952 head = next;
953 old_chain = make_cleanup_free_command_lines (&head);
954 }
955 tail = next;
956 }
957
958 dont_repeat ();
959
960 if (head)
961 {
962 if (ret != invalid_control)
963 {
964 discard_cleanups (old_chain);
965 }
966 else
967 do_cleanups (old_chain);
968 }
969
970 if (readline_end_hook)
971 {
972 (*readline_end_hook) ();
973 }
974 return (head);
975 }
976
977 /* Free a chain of struct command_line's. */
978
979 void
980 free_command_lines (struct command_line **lptr)
981 {
982 register struct command_line *l = *lptr;
983 register struct command_line *next;
984 struct command_line **blist;
985 int i;
986
987 while (l)
988 {
989 if (l->body_count > 0)
990 {
991 blist = l->body_list;
992 for (i = 0; i < l->body_count; i++, blist++)
993 free_command_lines (blist);
994 }
995 next = l->next;
996 xfree (l->line);
997 xfree (l);
998 l = next;
999 }
1000 *lptr = NULL;
1001 }
1002
1003 static void
1004 do_free_command_lines_cleanup (void *arg)
1005 {
1006 free_command_lines (arg);
1007 }
1008
1009 static struct cleanup *
1010 make_cleanup_free_command_lines (struct command_line **arg)
1011 {
1012 return make_cleanup (do_free_command_lines_cleanup, arg);
1013 }
1014 \f
1015 static void
1016 validate_comname (char *comname)
1017 {
1018 register char *p;
1019
1020 if (comname == 0)
1021 error_no_arg ("name of command to define");
1022
1023 p = comname;
1024 while (*p)
1025 {
1026 if (!isalnum (*p) && *p != '-' && *p != '_')
1027 error ("Junk in argument list: \"%s\"", p);
1028 p++;
1029 }
1030 }
1031
1032 /* This is just a placeholder in the command data structures. */
1033 static void
1034 user_defined_command (char *ignore, int from_tty)
1035 {
1036 }
1037
1038 void
1039 define_command (char *comname, int from_tty)
1040 {
1041 #define MAX_TMPBUF 128
1042 enum cmd_hook_type
1043 {
1044 CMD_NO_HOOK = 0,
1045 CMD_PRE_HOOK,
1046 CMD_POST_HOOK
1047 };
1048 register struct command_line *cmds;
1049 register struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1050 char *tem = comname;
1051 char *tem2;
1052 char tmpbuf[MAX_TMPBUF];
1053 int hook_type = CMD_NO_HOOK;
1054 int hook_name_size = 0;
1055
1056 #define HOOK_STRING "hook-"
1057 #define HOOK_LEN 5
1058 #define HOOK_POST_STRING "hookpost-"
1059 #define HOOK_POST_LEN 9
1060
1061 validate_comname (comname);
1062
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))
1066 c = 0;
1067
1068 if (c)
1069 {
1070 if (c->class == class_user || c->class == class_alias)
1071 tem = "Redefine command \"%s\"? ";
1072 else
1073 tem = "Really redefine built-in command \"%s\"? ";
1074 if (!query (tem, c->name))
1075 error ("Command \"%s\" not redefined.", c->name);
1076 }
1077
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. */
1081
1082 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1083 {
1084 hook_type = CMD_PRE_HOOK;
1085 hook_name_size = HOOK_LEN;
1086 }
1087 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1088 {
1089 hook_type = CMD_POST_HOOK;
1090 hook_name_size = HOOK_POST_LEN;
1091 }
1092
1093 if (hook_type != CMD_NO_HOOK)
1094 {
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))
1099 hookc = 0;
1100 if (!hookc)
1101 {
1102 warning ("Your new `%s' command does not hook any existing command.",
1103 comname);
1104 if (!query ("Proceed? "))
1105 error ("Not confirmed.");
1106 }
1107 }
1108
1109 comname = savestring (comname, strlen (comname));
1110
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++)
1114 if (isupper (*tem))
1115 *tem = tolower (*tem);
1116
1117 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1118 cmds = read_command_lines (tmpbuf, from_tty);
1119
1120 if (c && c->class == class_user)
1121 free_command_lines (&c->user_commands);
1122
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;
1127
1128 /* If this new command is a hook, then mark both commands as being
1129 tied. */
1130 if (hookc)
1131 {
1132 switch (hook_type)
1133 {
1134 case CMD_PRE_HOOK:
1135 hookc->hook_pre = newc; /* Target gets hooked. */
1136 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1137 break;
1138 case CMD_POST_HOOK:
1139 hookc->hook_post = newc; /* Target gets hooked. */
1140 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1141 break;
1142 default:
1143 /* Should never come here as hookc would be 0. */
1144 internal_error (__FILE__, __LINE__, "bad switch");
1145 }
1146 }
1147 }
1148
1149 void
1150 document_command (char *comname, int from_tty)
1151 {
1152 struct command_line *doclines;
1153 register struct cmd_list_element *c;
1154 char *tem = comname;
1155 char tmpbuf[128];
1156
1157 validate_comname (comname);
1158
1159 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1160
1161 if (c->class != class_user)
1162 error ("Command \"%s\" is built-in.", comname);
1163
1164 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1165 doclines = read_command_lines (tmpbuf, from_tty);
1166
1167 if (c->doc)
1168 xfree (c->doc);
1169
1170 {
1171 register struct command_line *cl1;
1172 register int len = 0;
1173
1174 for (cl1 = doclines; cl1; cl1 = cl1->next)
1175 len += strlen (cl1->line) + 1;
1176
1177 c->doc = (char *) xmalloc (len + 1);
1178 *c->doc = 0;
1179
1180 for (cl1 = doclines; cl1; cl1 = cl1->next)
1181 {
1182 strcat (c->doc, cl1->line);
1183 if (cl1->next)
1184 strcat (c->doc, "\n");
1185 }
1186 }
1187
1188 free_command_lines (&doclines);
1189 }
1190 \f
1191 struct source_cleanup_lines_args
1192 {
1193 int old_line;
1194 char *old_file;
1195 char *old_pre_error;
1196 char *old_error_pre_print;
1197 };
1198
1199 static void
1200 source_cleanup_lines (PTR args)
1201 {
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;
1208 }
1209
1210 /* ARGSUSED */
1211 static void
1212 do_fclose_cleanup (void *stream)
1213 {
1214 fclose (stream);
1215 }
1216
1217 /* Used to implement source_command */
1218
1219 void
1220 script_from_file (FILE *stream, char *file)
1221 {
1222 struct cleanup *old_cleanups;
1223 struct source_cleanup_lines_args old_lines;
1224 int needed_length;
1225
1226 if (stream == NULL)
1227 {
1228 internal_error (__FILE__, __LINE__, "called with NULL file pointer!");
1229 }
1230
1231 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1232
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
1244 long. */
1245 error_pre_print = "";
1246
1247 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
1248 if (source_error_allocated < needed_length)
1249 {
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);
1255 else
1256 source_error = xrealloc (source_error, source_error_allocated);
1257 }
1258
1259 read_command_file (stream);
1260
1261 do_cleanups (old_cleanups);
1262 }
1263
1264 void
1265 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1266 {
1267 register struct command_line *cmdlines;
1268
1269 cmdlines = c->user_commands;
1270 if (!cmdlines)
1271 return;
1272 fputs_filtered ("User command ", stream);
1273 fputs_filtered (c->name, stream);
1274 fputs_filtered (":\n", stream);
1275
1276 print_command_lines (uiout, cmdlines, 1);
1277 fputs_filtered ("\n", stream);
1278 }
1279