OSDN Git Service

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