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, 2004 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 
70 /* Allocate, initialize a new command line structure for one of the
71    control commands (if/while).  */
72 
73 static struct command_line *
build_command_line(enum command_control_type type,char * args)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 *
get_command_line(enum command_control_type type,char * arg)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
print_command_lines(struct ui_out * uiout,struct command_line * cmd,unsigned int depth)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
clear_hook_in_cleanup(void * data)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
execute_cmd_pre_hook(struct cmd_list_element * c)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
execute_cmd_post_hook(struct cmd_list_element * c)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
do_restore_user_call_depth(void * call_depth)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
execute_user_command(struct cmd_list_element * c,char * args)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
execute_control_command(struct command_line * cmd)293 execute_control_command (struct command_line *cmd)
294 {
295   struct expression *expr;
296   struct command_line *current;
297   struct cleanup *old_chain = make_cleanup (null_cleanup, 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   /* Start by assuming failure, if a problem is detected, the code
305      below will simply "break" out of the switch.  */
306   ret = invalid_control;
307 
308   switch (cmd->control_type)
309     {
310     case simple_control:
311       /* A simple command, execute it and return.  */
312       new_line = insert_args (cmd->line);
313       if (!new_line)
314 	break;
315       make_cleanup (free_current_contents, &new_line);
316       execute_command (new_line, 0);
317       ret = cmd->control_type;
318       break;
319 
320     case continue_control:
321     case break_control:
322       /* Return for "continue", and "break" so we can either
323          continue the loop at the top, or break out.  */
324       ret = cmd->control_type;
325       break;
326 
327     case while_control:
328       {
329 	/* Parse the loop control expression for the while statement.  */
330 	new_line = insert_args (cmd->line);
331 	if (!new_line)
332 	  break;
333 	make_cleanup (free_current_contents, &new_line);
334 	expr = parse_expression (new_line);
335 	make_cleanup (free_current_contents, &expr);
336 
337 	ret = simple_control;
338 	loop = 1;
339 
340 	/* Keep iterating so long as the expression is true.  */
341 	while (loop == 1)
342 	  {
343 	    int cond_result;
344 
345 	    QUIT;
346 
347 	    /* Evaluate the expression.  */
348 	    val_mark = value_mark ();
349 	    val = evaluate_expression (expr);
350 	    cond_result = value_true (val);
351 	    value_free_to_mark (val_mark);
352 
353 	    /* If the value is false, then break out of the loop.  */
354 	    if (!cond_result)
355 	      break;
356 
357 	    /* Execute the body of the while statement.  */
358 	    current = *cmd->body_list;
359 	    while (current)
360 	      {
361 		ret = execute_control_command (current);
362 
363 		/* If we got an error, or a "break" command, then stop
364 		   looping.  */
365 		if (ret == invalid_control || ret == break_control)
366 		  {
367 		    loop = 0;
368 		    break;
369 		  }
370 
371 		/* If we got a "continue" command, then restart the loop
372 		   at this point.  */
373 		if (ret == continue_control)
374 		  break;
375 
376 		/* Get the next statement.  */
377 		current = current->next;
378 	      }
379 	  }
380 
381 	/* Reset RET so that we don't recurse the break all the way down.  */
382 	if (ret == break_control)
383 	  ret = simple_control;
384 
385 	break;
386       }
387 
388     case if_control:
389       {
390 	new_line = insert_args (cmd->line);
391 	if (!new_line)
392 	  break;
393 	make_cleanup (free_current_contents, &new_line);
394 	/* Parse the conditional for the if statement.  */
395 	expr = parse_expression (new_line);
396 	make_cleanup (free_current_contents, &expr);
397 
398 	current = NULL;
399 	ret = simple_control;
400 
401 	/* Evaluate the conditional.  */
402 	val_mark = value_mark ();
403 	val = evaluate_expression (expr);
404 
405 	/* Choose which arm to take commands from based on the value of the
406 	   conditional expression.  */
407 	if (value_true (val))
408 	  current = *cmd->body_list;
409 	else if (cmd->body_count == 2)
410 	  current = *(cmd->body_list + 1);
411 	value_free_to_mark (val_mark);
412 
413 	/* Execute commands in the given arm.  */
414 	while (current)
415 	  {
416 	    ret = execute_control_command (current);
417 
418 	    /* If we got an error, get out.  */
419 	    if (ret != simple_control)
420 	      break;
421 
422 	    /* Get the next statement in the body.  */
423 	    current = current->next;
424 	  }
425 
426 	break;
427       }
428 
429     default:
430       warning ("Invalid control type in command structure.");
431       break;
432     }
433 
434   do_cleanups (old_chain);
435 
436   return ret;
437 }
438 
439 /* "while" command support.  Executes a body of statements while the
440    loop condition is nonzero.  */
441 
442 void
while_command(char * arg,int from_tty)443 while_command (char *arg, int from_tty)
444 {
445   struct command_line *command = NULL;
446 
447   control_level = 1;
448   command = get_command_line (while_control, arg);
449 
450   if (command == NULL)
451     return;
452 
453   execute_control_command (command);
454   free_command_lines (&command);
455 }
456 
457 /* "if" command support.  Execute either the true or false arm depending
458    on the value of the if conditional.  */
459 
460 void
if_command(char * arg,int from_tty)461 if_command (char *arg, int from_tty)
462 {
463   struct command_line *command = NULL;
464 
465   control_level = 1;
466   command = get_command_line (if_control, arg);
467 
468   if (command == NULL)
469     return;
470 
471   execute_control_command (command);
472   free_command_lines (&command);
473 }
474 
475 /* Cleanup */
476 static void
arg_cleanup(void * ignore)477 arg_cleanup (void *ignore)
478 {
479   struct user_args *oargs = user_args;
480   if (!user_args)
481     internal_error (__FILE__, __LINE__,
482 		    "arg_cleanup called with no user args.\n");
483 
484   user_args = user_args->next;
485   xfree (oargs);
486 }
487 
488 /* Bind the incomming arguments for a user defined command to
489    $arg0, $arg1 ... $argMAXUSERARGS.  */
490 
491 static struct cleanup *
setup_user_args(char * p)492 setup_user_args (char *p)
493 {
494   struct user_args *args;
495   struct cleanup *old_chain;
496   unsigned int arg_count = 0;
497 
498   args = (struct user_args *) xmalloc (sizeof (struct user_args));
499   memset (args, 0, sizeof (struct user_args));
500 
501   args->next = user_args;
502   user_args = args;
503 
504   old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
505 
506   if (p == NULL)
507     return old_chain;
508 
509   while (*p)
510     {
511       char *start_arg;
512       int squote = 0;
513       int dquote = 0;
514       int bsquote = 0;
515 
516       if (arg_count >= MAXUSERARGS)
517 	{
518 	  error ("user defined function may only have %d arguments.\n",
519 		 MAXUSERARGS);
520 	  return old_chain;
521 	}
522 
523       /* Strip whitespace.  */
524       while (*p == ' ' || *p == '\t')
525 	p++;
526 
527       /* P now points to an argument.  */
528       start_arg = p;
529       user_args->a[arg_count].arg = p;
530 
531       /* Get to the end of this argument.  */
532       while (*p)
533 	{
534 	  if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
535 	    break;
536 	  else
537 	    {
538 	      if (bsquote)
539 		bsquote = 0;
540 	      else if (*p == '\\')
541 		bsquote = 1;
542 	      else if (squote)
543 		{
544 		  if (*p == '\'')
545 		    squote = 0;
546 		}
547 	      else if (dquote)
548 		{
549 		  if (*p == '"')
550 		    dquote = 0;
551 		}
552 	      else
553 		{
554 		  if (*p == '\'')
555 		    squote = 1;
556 		  else if (*p == '"')
557 		    dquote = 1;
558 		}
559 	      p++;
560 	    }
561 	}
562 
563       user_args->a[arg_count].len = p - start_arg;
564       arg_count++;
565       user_args->count++;
566     }
567   return old_chain;
568 }
569 
570 /* Given character string P, return a point to the first argument ($arg),
571    or NULL if P contains no arguments.  */
572 
573 static char *
locate_arg(char * p)574 locate_arg (char *p)
575 {
576   while ((p = strchr (p, '$')))
577     {
578       if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
579 	return p;
580       p++;
581     }
582   return NULL;
583 }
584 
585 /* Insert the user defined arguments stored in user_arg into the $arg
586    arguments found in line, with the updated copy being placed into nline.  */
587 
588 static char *
insert_args(char * line)589 insert_args (char *line)
590 {
591   char *p, *save_line, *new_line;
592   unsigned len, i;
593 
594   /* First we need to know how much memory to allocate for the new line.  */
595   save_line = line;
596   len = 0;
597   while ((p = locate_arg (line)))
598     {
599       len += p - line;
600       i = p[4] - '0';
601 
602       if (i >= user_args->count)
603 	{
604 	  error ("Missing argument %d in user function.\n", i);
605 	  return NULL;
606 	}
607       len += user_args->a[i].len;
608       line = p + 5;
609     }
610 
611   /* Don't forget the tail.  */
612   len += strlen (line);
613 
614   /* Allocate space for the new line and fill it in.  */
615   new_line = (char *) xmalloc (len + 1);
616   if (new_line == NULL)
617     return NULL;
618 
619   /* Restore pointer to beginning of old line.  */
620   line = save_line;
621 
622   /* Save pointer to beginning of new line.  */
623   save_line = new_line;
624 
625   while ((p = locate_arg (line)))
626     {
627       int i, len;
628 
629       memcpy (new_line, line, p - line);
630       new_line += p - line;
631       i = p[4] - '0';
632 
633       len = user_args->a[i].len;
634       if (len)
635 	{
636 	  memcpy (new_line, user_args->a[i].arg, len);
637 	  new_line += len;
638 	}
639       line = p + 5;
640     }
641   /* Don't forget the tail.  */
642   strcpy (new_line, line);
643 
644   /* Return a pointer to the beginning of the new line.  */
645   return save_line;
646 }
647 
648 
649 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
650    code bodies.  This is typically used when we encounter an "else"
651    clause for an "if" command.  */
652 
653 static void
realloc_body_list(struct command_line * command,int new_length)654 realloc_body_list (struct command_line *command, int new_length)
655 {
656   int n;
657   struct command_line **body_list;
658 
659   n = command->body_count;
660 
661   /* Nothing to do?  */
662   if (new_length <= n)
663     return;
664 
665   body_list = (struct command_line **)
666     xmalloc (sizeof (struct command_line *) * new_length);
667 
668   memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
669 
670   xfree (command->body_list);
671   command->body_list = body_list;
672   command->body_count = new_length;
673 }
674 
675 /* Read one line from the input stream.  If the command is an "else" or
676    "end", return such an indication to the caller.  */
677 
678 static enum misc_command_type
read_next_line(struct command_line ** command)679 read_next_line (struct command_line **command)
680 {
681   char *p, *p1, *prompt_ptr, control_prompt[256];
682   int i = 0;
683 
684   if (control_level >= 254)
685     error ("Control nesting too deep!\n");
686 
687   /* Set a prompt based on the nesting of the control commands.  */
688   if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
689     {
690       for (i = 0; i < control_level; i++)
691 	control_prompt[i] = ' ';
692       control_prompt[i] = '>';
693       control_prompt[i + 1] = '\0';
694       prompt_ptr = (char *) &control_prompt[0];
695     }
696   else
697     prompt_ptr = NULL;
698 
699   p = command_line_input (prompt_ptr, instream == stdin, "commands");
700 
701   /* Not sure what to do here.  */
702   if (p == NULL)
703     return end_command;
704 
705   /* Strip leading and trailing whitespace.  */
706   while (*p == ' ' || *p == '\t')
707     p++;
708 
709   p1 = p + strlen (p);
710   while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
711     p1--;
712 
713   /* Blanks and comments don't really do anything, but we need to
714      distinguish them from else, end and other commands which can be
715      executed.  */
716   if (p1 == p || p[0] == '#')
717     return nop_command;
718 
719   /* Is this the end of a simple, while, or if control structure?  */
720   if (p1 - p == 3 && !strncmp (p, "end", 3))
721     return end_command;
722 
723   /* Is the else clause of an if control structure?  */
724   if (p1 - p == 4 && !strncmp (p, "else", 4))
725     return else_command;
726 
727   /* Check for while, if, break, continue, etc and build a new command
728      line structure for them.  */
729   if (p1 - p > 5 && !strncmp (p, "while", 5))
730     {
731       char *first_arg;
732       first_arg = p + 5;
733       while (first_arg < p1 && isspace (*first_arg))
734         first_arg++;
735       *command = build_command_line (while_control, first_arg);
736     }
737   else if (p1 - p > 2 && !strncmp (p, "if", 2))
738     {
739       char *first_arg;
740       first_arg = p + 2;
741       while (first_arg < p1 && isspace (*first_arg))
742         first_arg++;
743       *command = build_command_line (if_control, first_arg);
744     }
745   else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
746     {
747       *command = (struct command_line *)
748 	xmalloc (sizeof (struct command_line));
749       (*command)->next = NULL;
750       (*command)->line = NULL;
751       (*command)->control_type = break_control;
752       (*command)->body_count = 0;
753       (*command)->body_list = NULL;
754     }
755   else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
756     {
757       *command = (struct command_line *)
758 	xmalloc (sizeof (struct command_line));
759       (*command)->next = NULL;
760       (*command)->line = NULL;
761       (*command)->control_type = continue_control;
762       (*command)->body_count = 0;
763       (*command)->body_list = NULL;
764     }
765   else
766     {
767       /* A normal command.  */
768       *command = (struct command_line *)
769 	xmalloc (sizeof (struct command_line));
770       (*command)->next = NULL;
771       (*command)->line = savestring (p, p1 - p);
772       (*command)->control_type = simple_control;
773       (*command)->body_count = 0;
774       (*command)->body_list = NULL;
775     }
776 
777   /* Nothing special.  */
778   return ok_command;
779 }
780 
781 /* Recursively read in the control structures and create a command_line
782    structure from them.
783 
784    The parent_control parameter is the control structure in which the
785    following commands are nested.  */
786 
787 static enum command_control_type
recurse_read_control_structure(struct command_line * current_cmd)788 recurse_read_control_structure (struct command_line *current_cmd)
789 {
790   int current_body, i;
791   enum misc_command_type val;
792   enum command_control_type ret;
793   struct command_line **body_ptr, *child_tail, *next;
794 
795   child_tail = NULL;
796   current_body = 1;
797 
798   /* Sanity checks.  */
799   if (current_cmd->control_type == simple_control)
800     {
801       error ("Recursed on a simple control type\n");
802       return invalid_control;
803     }
804 
805   if (current_body > current_cmd->body_count)
806     {
807       error ("Allocated body is smaller than this command type needs\n");
808       return invalid_control;
809     }
810 
811   /* Read lines from the input stream and build control structures.  */
812   while (1)
813     {
814       dont_repeat ();
815 
816       next = NULL;
817       val = read_next_line (&next);
818 
819       /* Just skip blanks and comments.  */
820       if (val == nop_command)
821 	continue;
822 
823       if (val == end_command)
824 	{
825 	  if (current_cmd->control_type == while_control
826 	      || current_cmd->control_type == if_control)
827 	    {
828 	      /* Success reading an entire control structure.  */
829 	      ret = simple_control;
830 	      break;
831 	    }
832 	  else
833 	    {
834 	      ret = invalid_control;
835 	      break;
836 	    }
837 	}
838 
839       /* Not the end of a control structure.  */
840       if (val == else_command)
841 	{
842 	  if (current_cmd->control_type == if_control
843 	      && current_body == 1)
844 	    {
845 	      realloc_body_list (current_cmd, 2);
846 	      current_body = 2;
847 	      child_tail = NULL;
848 	      continue;
849 	    }
850 	  else
851 	    {
852 	      ret = invalid_control;
853 	      break;
854 	    }
855 	}
856 
857       if (child_tail)
858 	{
859 	  child_tail->next = next;
860 	}
861       else
862 	{
863 	  body_ptr = current_cmd->body_list;
864 	  for (i = 1; i < current_body; i++)
865 	    body_ptr++;
866 
867 	  *body_ptr = next;
868 
869 	}
870 
871       child_tail = next;
872 
873       /* If the latest line is another control structure, then recurse
874          on it.  */
875       if (next->control_type == while_control
876 	  || next->control_type == if_control)
877 	{
878 	  control_level++;
879 	  ret = recurse_read_control_structure (next);
880 	  control_level--;
881 
882 	  if (ret != simple_control)
883 	    break;
884 	}
885     }
886 
887   dont_repeat ();
888 
889   return ret;
890 }
891 
892 /* Read lines from the input stream and accumulate them in a chain of
893    struct command_line's, which is then returned.  For input from a
894    terminal, the special command "end" is used to mark the end of the
895    input, and is not included in the returned chain of commands. */
896 
897 #define END_MESSAGE "End with a line saying just \"end\"."
898 
899 struct command_line *
read_command_lines(char * prompt_arg,int from_tty)900 read_command_lines (char *prompt_arg, int from_tty)
901 {
902   struct command_line *head, *tail, *next;
903   struct cleanup *old_chain;
904   enum command_control_type ret;
905   enum misc_command_type val;
906 
907   control_level = 0;
908   if (deprecated_readline_begin_hook)
909     {
910       /* Note - intentional to merge messages with no newline */
911       (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg, END_MESSAGE);
912     }
913   else if (from_tty && input_from_terminal_p ())
914     {
915       printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
916       gdb_flush (gdb_stdout);
917     }
918 
919   head = tail = NULL;
920   old_chain = NULL;
921 
922   while (1)
923     {
924       val = read_next_line (&next);
925 
926       /* Ignore blank lines or comments.  */
927       if (val == nop_command)
928 	continue;
929 
930       if (val == end_command)
931 	{
932 	  ret = simple_control;
933 	  break;
934 	}
935 
936       if (val != ok_command)
937 	{
938 	  ret = invalid_control;
939 	  break;
940 	}
941 
942       if (next->control_type == while_control
943 	  || next->control_type == if_control)
944 	{
945 	  control_level++;
946 	  ret = recurse_read_control_structure (next);
947 	  control_level--;
948 
949 	  if (ret == invalid_control)
950 	    break;
951 	}
952 
953       if (tail)
954 	{
955 	  tail->next = next;
956 	}
957       else
958 	{
959 	  head = next;
960 	  old_chain = make_cleanup_free_command_lines (&head);
961 	}
962       tail = next;
963     }
964 
965   dont_repeat ();
966 
967   if (head)
968     {
969       if (ret != invalid_control)
970 	{
971 	  discard_cleanups (old_chain);
972 	}
973       else
974 	do_cleanups (old_chain);
975     }
976 
977   if (deprecated_readline_end_hook)
978     {
979       (*deprecated_readline_end_hook) ();
980     }
981   return (head);
982 }
983 
984 /* Free a chain of struct command_line's.  */
985 
986 void
free_command_lines(struct command_line ** lptr)987 free_command_lines (struct command_line **lptr)
988 {
989   struct command_line *l = *lptr;
990   struct command_line *next;
991   struct command_line **blist;
992   int i;
993 
994   while (l)
995     {
996       if (l->body_count > 0)
997 	{
998 	  blist = l->body_list;
999 	  for (i = 0; i < l->body_count; i++, blist++)
1000 	    free_command_lines (blist);
1001 	}
1002       next = l->next;
1003       xfree (l->line);
1004       xfree (l);
1005       l = next;
1006     }
1007   *lptr = NULL;
1008 }
1009 
1010 static void
do_free_command_lines_cleanup(void * arg)1011 do_free_command_lines_cleanup (void *arg)
1012 {
1013   free_command_lines (arg);
1014 }
1015 
1016 struct cleanup *
make_cleanup_free_command_lines(struct command_line ** arg)1017 make_cleanup_free_command_lines (struct command_line **arg)
1018 {
1019   return make_cleanup (do_free_command_lines_cleanup, arg);
1020 }
1021 
1022 struct command_line *
copy_command_lines(struct command_line * cmds)1023 copy_command_lines (struct command_line *cmds)
1024 {
1025   struct command_line *result = NULL;
1026 
1027   if (cmds)
1028     {
1029       result = (struct command_line *) xmalloc (sizeof (struct command_line));
1030 
1031       result->next = copy_command_lines (cmds->next);
1032       result->line = xstrdup (cmds->line);
1033       result->control_type = cmds->control_type;
1034       result->body_count = cmds->body_count;
1035       if (cmds->body_count > 0)
1036         {
1037           int i;
1038 
1039           result->body_list = (struct command_line **)
1040             xmalloc (sizeof (struct command_line *) * cmds->body_count);
1041 
1042           for (i = 0; i < cmds->body_count; i++)
1043             result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1044         }
1045       else
1046         result->body_list = NULL;
1047     }
1048 
1049   return result;
1050 }
1051 
1052 static void
validate_comname(char * comname)1053 validate_comname (char *comname)
1054 {
1055   char *p;
1056 
1057   if (comname == 0)
1058     error_no_arg ("name of command to define");
1059 
1060   p = comname;
1061   while (*p)
1062     {
1063       if (!isalnum (*p) && *p != '-' && *p != '_')
1064 	error ("Junk in argument list: \"%s\"", p);
1065       p++;
1066     }
1067 }
1068 
1069 /* This is just a placeholder in the command data structures.  */
1070 static void
user_defined_command(char * ignore,int from_tty)1071 user_defined_command (char *ignore, int from_tty)
1072 {
1073 }
1074 
1075 void
define_command(char * comname,int from_tty)1076 define_command (char *comname, int from_tty)
1077 {
1078 #define MAX_TMPBUF 128
1079   enum cmd_hook_type
1080     {
1081       CMD_NO_HOOK = 0,
1082       CMD_PRE_HOOK,
1083       CMD_POST_HOOK
1084     };
1085   struct command_line *cmds;
1086   struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1087   char *tem = comname;
1088   char *tem2;
1089   char tmpbuf[MAX_TMPBUF];
1090   int  hook_type      = CMD_NO_HOOK;
1091   int  hook_name_size = 0;
1092 
1093 #define	HOOK_STRING	"hook-"
1094 #define	HOOK_LEN 5
1095 #define HOOK_POST_STRING "hookpost-"
1096 #define HOOK_POST_LEN    9
1097 
1098   validate_comname (comname);
1099 
1100   /* Look it up, and verify that we got an exact match.  */
1101   c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1102   if (c && strcmp (comname, c->name) != 0)
1103     c = 0;
1104 
1105   if (c)
1106     {
1107       int q;
1108       if (c->class == class_user || c->class == class_alias)
1109 	q = query ("Redefine command \"%s\"? ", c->name);
1110       else
1111 	q = query ("Really redefine built-in command \"%s\"? ", c->name);
1112       if (!q)
1113 	error ("Command \"%s\" not redefined.", c->name);
1114     }
1115 
1116   /* If this new command is a hook, then mark the command which it
1117      is hooking.  Note that we allow hooking `help' commands, so that
1118      we can hook the `stop' pseudo-command.  */
1119 
1120   if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1121     {
1122        hook_type      = CMD_PRE_HOOK;
1123        hook_name_size = HOOK_LEN;
1124     }
1125   else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1126     {
1127       hook_type      = CMD_POST_HOOK;
1128       hook_name_size = HOOK_POST_LEN;
1129     }
1130 
1131   if (hook_type != CMD_NO_HOOK)
1132     {
1133       /* Look up cmd it hooks, and verify that we got an exact match.  */
1134       tem = comname + hook_name_size;
1135       hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1136       if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1137 	hookc = 0;
1138       if (!hookc)
1139 	{
1140 	  warning ("Your new `%s' command does not hook any existing command.",
1141 		   comname);
1142 	  if (!query ("Proceed? "))
1143 	    error ("Not confirmed.");
1144 	}
1145     }
1146 
1147   comname = savestring (comname, strlen (comname));
1148 
1149   /* If the rest of the commands will be case insensitive, this one
1150      should behave in the same manner. */
1151   for (tem = comname; *tem; tem++)
1152     if (isupper (*tem))
1153       *tem = tolower (*tem);
1154 
1155   sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1156   cmds = read_command_lines (tmpbuf, from_tty);
1157 
1158   if (c && c->class == class_user)
1159     free_command_lines (&c->user_commands);
1160 
1161   newc = add_cmd (comname, class_user, user_defined_command,
1162 		  (c && c->class == class_user)
1163 		  ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1164   newc->user_commands = cmds;
1165 
1166   /* If this new command is a hook, then mark both commands as being
1167      tied.  */
1168   if (hookc)
1169     {
1170       switch (hook_type)
1171         {
1172         case CMD_PRE_HOOK:
1173           hookc->hook_pre  = newc;  /* Target gets hooked.  */
1174           newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1175           break;
1176         case CMD_POST_HOOK:
1177           hookc->hook_post  = newc;  /* Target gets hooked.  */
1178           newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1179           break;
1180         default:
1181           /* Should never come here as hookc would be 0. */
1182 	  internal_error (__FILE__, __LINE__, "bad switch");
1183         }
1184     }
1185 }
1186 
1187 void
document_command(char * comname,int from_tty)1188 document_command (char *comname, int from_tty)
1189 {
1190   struct command_line *doclines;
1191   struct cmd_list_element *c;
1192   char *tem = comname;
1193   char tmpbuf[128];
1194 
1195   validate_comname (comname);
1196 
1197   c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1198 
1199   if (c->class != class_user)
1200     error ("Command \"%s\" is built-in.", comname);
1201 
1202   sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1203   doclines = read_command_lines (tmpbuf, from_tty);
1204 
1205   if (c->doc)
1206     xfree (c->doc);
1207 
1208   {
1209     struct command_line *cl1;
1210     int len = 0;
1211 
1212     for (cl1 = doclines; cl1; cl1 = cl1->next)
1213       len += strlen (cl1->line) + 1;
1214 
1215     c->doc = (char *) xmalloc (len + 1);
1216     *c->doc = 0;
1217 
1218     for (cl1 = doclines; cl1; cl1 = cl1->next)
1219       {
1220 	strcat (c->doc, cl1->line);
1221 	if (cl1->next)
1222 	  strcat (c->doc, "\n");
1223       }
1224   }
1225 
1226   free_command_lines (&doclines);
1227 }
1228 
1229 struct source_cleanup_lines_args
1230 {
1231   int old_line;
1232   char *old_file;
1233   char *old_pre_error;
1234   char *old_error_pre_print;
1235 };
1236 
1237 static void
source_cleanup_lines(void * args)1238 source_cleanup_lines (void *args)
1239 {
1240   struct source_cleanup_lines_args *p =
1241   (struct source_cleanup_lines_args *) args;
1242   source_line_number = p->old_line;
1243   source_file_name = p->old_file;
1244   source_pre_error = p->old_pre_error;
1245   error_pre_print = p->old_error_pre_print;
1246 }
1247 
1248 static void
do_fclose_cleanup(void * stream)1249 do_fclose_cleanup (void *stream)
1250 {
1251   fclose (stream);
1252 }
1253 
1254 /* Used to implement source_command */
1255 
1256 void
script_from_file(FILE * stream,char * file)1257 script_from_file (FILE *stream, char *file)
1258 {
1259   struct cleanup *old_cleanups;
1260   struct source_cleanup_lines_args old_lines;
1261   int needed_length;
1262 
1263   if (stream == NULL)
1264     {
1265       internal_error (__FILE__, __LINE__, "called with NULL file pointer!");
1266     }
1267 
1268   old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1269 
1270   old_lines.old_line = source_line_number;
1271   old_lines.old_file = source_file_name;
1272   old_lines.old_pre_error = source_pre_error;
1273   old_lines.old_error_pre_print = error_pre_print;
1274   make_cleanup (source_cleanup_lines, &old_lines);
1275   source_line_number = 0;
1276   source_file_name = file;
1277   source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
1278   source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
1279   make_cleanup (xfree, source_pre_error);
1280   /* This will get set every time we read a line.  So it won't stay "" for
1281      long.  */
1282   error_pre_print = "";
1283 
1284   needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
1285   if (source_error_allocated < needed_length)
1286     {
1287       source_error_allocated *= 2;
1288       if (source_error_allocated < needed_length)
1289 	source_error_allocated = needed_length;
1290       if (source_error == NULL)
1291 	source_error = xmalloc (source_error_allocated);
1292       else
1293 	source_error = xrealloc (source_error, source_error_allocated);
1294     }
1295 
1296   read_command_file (stream);
1297 
1298   do_cleanups (old_cleanups);
1299 }
1300 
1301 void
show_user_1(struct cmd_list_element * c,struct ui_file * stream)1302 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1303 {
1304   struct command_line *cmdlines;
1305 
1306   cmdlines = c->user_commands;
1307   if (!cmdlines)
1308     return;
1309   fputs_filtered ("User command ", stream);
1310   fputs_filtered (c->name, stream);
1311   fputs_filtered (":\n", stream);
1312 
1313   print_command_lines (uiout, cmdlines, 1);
1314   fputs_filtered ("\n", stream);
1315 }
1316 
1317