xref: /dragonfly/contrib/gdb-7/gdb/cli/cli-script.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* GDB CLI command scripting.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "value.h"
225796c8dcSSimon Schubert #include "language.h"		/* For value_true */
235796c8dcSSimon Schubert #include <ctype.h>
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert #include "ui-out.h"
265796c8dcSSimon Schubert #include "gdb_string.h"
275796c8dcSSimon Schubert #include "exceptions.h"
285796c8dcSSimon Schubert #include "top.h"
295796c8dcSSimon Schubert #include "breakpoint.h"
305796c8dcSSimon Schubert #include "cli/cli-cmds.h"
315796c8dcSSimon Schubert #include "cli/cli-decode.h"
325796c8dcSSimon Schubert #include "cli/cli-script.h"
335796c8dcSSimon Schubert #include "gdb_assert.h"
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert #include "python/python.h"
36a45ae5f8SJohn Marino #include "interps.h"
375796c8dcSSimon Schubert 
38c50c785cSJohn Marino /* Prototypes for local functions.  */
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert static enum command_control_type
41cf7f2e2dSJohn Marino recurse_read_control_structure (char * (*read_next_line_func) (void),
42cf7f2e2dSJohn Marino 				struct command_line *current_cmd,
43cf7f2e2dSJohn Marino 				void (*validator)(char *, void *),
44cf7f2e2dSJohn Marino 				void *closure);
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert static char *insert_args (char *line);
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert static struct cleanup * setup_user_args (char *p);
495796c8dcSSimon Schubert 
50cf7f2e2dSJohn Marino static char *read_next_line (void);
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert /* Level of control structure when reading.  */
535796c8dcSSimon Schubert static int control_level;
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert /* Level of control structure when executing.  */
565796c8dcSSimon Schubert static int command_nest_depth = 1;
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert /* This is to prevent certain commands being printed twice.  */
595796c8dcSSimon Schubert static int suppress_next_print_command_trace = 0;
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert /* Structure for arguments to user defined functions.  */
625796c8dcSSimon Schubert #define MAXUSERARGS 10
635796c8dcSSimon Schubert struct user_args
645796c8dcSSimon Schubert   {
655796c8dcSSimon Schubert     struct user_args *next;
665796c8dcSSimon Schubert     /* It is necessary to store a malloced copy of the command line to
67c50c785cSJohn Marino        ensure that the arguments are not overwritten before they are
68c50c785cSJohn Marino        used.  */
695796c8dcSSimon Schubert     char *command;
705796c8dcSSimon Schubert     struct
715796c8dcSSimon Schubert       {
725796c8dcSSimon Schubert 	char *arg;
735796c8dcSSimon Schubert 	int len;
745796c8dcSSimon Schubert       }
755796c8dcSSimon Schubert     a[MAXUSERARGS];
765796c8dcSSimon Schubert     int count;
775796c8dcSSimon Schubert   }
785796c8dcSSimon Schubert  *user_args;
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert /* Allocate, initialize a new command line structure for one of the
825796c8dcSSimon Schubert    control commands (if/while).  */
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert static struct command_line *
build_command_line(enum command_control_type type,char * args)855796c8dcSSimon Schubert build_command_line (enum command_control_type type, char *args)
865796c8dcSSimon Schubert {
875796c8dcSSimon Schubert   struct command_line *cmd;
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert   if (args == NULL && (type == if_control || type == while_control))
905796c8dcSSimon Schubert     error (_("if/while commands require arguments."));
915796c8dcSSimon Schubert   gdb_assert (args != NULL);
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert   cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
945796c8dcSSimon Schubert   cmd->next = NULL;
955796c8dcSSimon Schubert   cmd->control_type = type;
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert   cmd->body_count = 1;
985796c8dcSSimon Schubert   cmd->body_list
995796c8dcSSimon Schubert     = (struct command_line **) xmalloc (sizeof (struct command_line *)
1005796c8dcSSimon Schubert 					* cmd->body_count);
1015796c8dcSSimon Schubert   memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
1025796c8dcSSimon Schubert   cmd->line = xstrdup (args);
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert   return cmd;
1055796c8dcSSimon Schubert }
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert /* Build and return a new command structure for the control commands
1085796c8dcSSimon Schubert    such as "if" and "while".  */
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert struct command_line *
get_command_line(enum command_control_type type,char * arg)1115796c8dcSSimon Schubert get_command_line (enum command_control_type type, char *arg)
1125796c8dcSSimon Schubert {
1135796c8dcSSimon Schubert   struct command_line *cmd;
1145796c8dcSSimon Schubert   struct cleanup *old_chain = NULL;
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert   /* Allocate and build a new command line structure.  */
1175796c8dcSSimon Schubert   cmd = build_command_line (type, arg);
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert   old_chain = make_cleanup_free_command_lines (&cmd);
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert   /* Read in the body of this command.  */
122cf7f2e2dSJohn Marino   if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
123cf7f2e2dSJohn Marino       == invalid_control)
1245796c8dcSSimon Schubert     {
1255796c8dcSSimon Schubert       warning (_("Error reading in canned sequence of commands."));
1265796c8dcSSimon Schubert       do_cleanups (old_chain);
1275796c8dcSSimon Schubert       return NULL;
1285796c8dcSSimon Schubert     }
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   discard_cleanups (old_chain);
1315796c8dcSSimon Schubert   return cmd;
1325796c8dcSSimon Schubert }
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert /* Recursively print a command (including full control structures).  */
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert void
print_command_lines(struct ui_out * uiout,struct command_line * cmd,unsigned int depth)1375796c8dcSSimon Schubert print_command_lines (struct ui_out *uiout, struct command_line *cmd,
1385796c8dcSSimon Schubert 		     unsigned int depth)
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert   struct command_line *list;
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert   list = cmd;
1435796c8dcSSimon Schubert   while (list)
1445796c8dcSSimon Schubert     {
1455796c8dcSSimon Schubert       if (depth)
1465796c8dcSSimon Schubert 	ui_out_spaces (uiout, 2 * depth);
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert       /* A simple command, print it and continue.  */
1495796c8dcSSimon Schubert       if (list->control_type == simple_control)
1505796c8dcSSimon Schubert 	{
1515796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, list->line);
1525796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
1535796c8dcSSimon Schubert 	  list = list->next;
1545796c8dcSSimon Schubert 	  continue;
1555796c8dcSSimon Schubert 	}
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert       /* loop_continue to jump to the start of a while loop, print it
1585796c8dcSSimon Schubert          and continue. */
1595796c8dcSSimon Schubert       if (list->control_type == continue_control)
1605796c8dcSSimon Schubert 	{
1615796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, "loop_continue");
1625796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
1635796c8dcSSimon Schubert 	  list = list->next;
1645796c8dcSSimon Schubert 	  continue;
1655796c8dcSSimon Schubert 	}
1665796c8dcSSimon Schubert 
167c50c785cSJohn Marino       /* loop_break to break out of a while loop, print it and
168c50c785cSJohn Marino 	 continue.  */
1695796c8dcSSimon Schubert       if (list->control_type == break_control)
1705796c8dcSSimon Schubert 	{
1715796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, "loop_break");
1725796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
1735796c8dcSSimon Schubert 	  list = list->next;
1745796c8dcSSimon Schubert 	  continue;
1755796c8dcSSimon Schubert 	}
1765796c8dcSSimon Schubert 
177c50c785cSJohn Marino       /* A while command.  Recursively print its subcommands and
178c50c785cSJohn Marino 	 continue.  */
179cf7f2e2dSJohn Marino       if (list->control_type == while_control
180cf7f2e2dSJohn Marino 	  || list->control_type == while_stepping_control)
1815796c8dcSSimon Schubert 	{
182c50c785cSJohn Marino 	  /* For while-stepping, the line includes the 'while-stepping'
183c50c785cSJohn Marino 	     token.  See comment in process_next_line for explanation.
184c50c785cSJohn Marino 	     Here, take care not print 'while-stepping' twice.  */
185cf7f2e2dSJohn Marino 	  if (list->control_type == while_control)
1865796c8dcSSimon Schubert 	    ui_out_field_fmt (uiout, NULL, "while %s", list->line);
187cf7f2e2dSJohn Marino 	  else
188cf7f2e2dSJohn Marino 	    ui_out_field_string (uiout, NULL, list->line);
1895796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
1905796c8dcSSimon Schubert 	  print_command_lines (uiout, *list->body_list, depth + 1);
1915796c8dcSSimon Schubert 	  if (depth)
1925796c8dcSSimon Schubert 	    ui_out_spaces (uiout, 2 * depth);
1935796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, "end");
1945796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
1955796c8dcSSimon Schubert 	  list = list->next;
1965796c8dcSSimon Schubert 	  continue;
1975796c8dcSSimon Schubert 	}
1985796c8dcSSimon Schubert 
199c50c785cSJohn Marino       /* An if command.  Recursively print both arms before
200c50c785cSJohn Marino 	 continueing.  */
2015796c8dcSSimon Schubert       if (list->control_type == if_control)
2025796c8dcSSimon Schubert 	{
2035796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, NULL, "if %s", list->line);
2045796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
2055796c8dcSSimon Schubert 	  /* The true arm.  */
2065796c8dcSSimon Schubert 	  print_command_lines (uiout, list->body_list[0], depth + 1);
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert 	  /* Show the false arm if it exists.  */
2095796c8dcSSimon Schubert 	  if (list->body_count == 2)
2105796c8dcSSimon Schubert 	    {
2115796c8dcSSimon Schubert 	      if (depth)
2125796c8dcSSimon Schubert 		ui_out_spaces (uiout, 2 * depth);
2135796c8dcSSimon Schubert 	      ui_out_field_string (uiout, NULL, "else");
2145796c8dcSSimon Schubert 	      ui_out_text (uiout, "\n");
2155796c8dcSSimon Schubert 	      print_command_lines (uiout, list->body_list[1], depth + 1);
2165796c8dcSSimon Schubert 	    }
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert 	  if (depth)
2195796c8dcSSimon Schubert 	    ui_out_spaces (uiout, 2 * depth);
2205796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, "end");
2215796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
2225796c8dcSSimon Schubert 	  list = list->next;
2235796c8dcSSimon Schubert 	  continue;
2245796c8dcSSimon Schubert 	}
2255796c8dcSSimon Schubert 
226c50c785cSJohn Marino       /* A commands command.  Print the breakpoint commands and
227c50c785cSJohn Marino 	 continue.  */
2285796c8dcSSimon Schubert       if (list->control_type == commands_control)
2295796c8dcSSimon Schubert 	{
2305796c8dcSSimon Schubert 	  if (*(list->line))
2315796c8dcSSimon Schubert 	    ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
2325796c8dcSSimon Schubert 	  else
2335796c8dcSSimon Schubert 	    ui_out_field_string (uiout, NULL, "commands");
2345796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
2355796c8dcSSimon Schubert 	  print_command_lines (uiout, *list->body_list, depth + 1);
2365796c8dcSSimon Schubert 	  if (depth)
2375796c8dcSSimon Schubert 	    ui_out_spaces (uiout, 2 * depth);
2385796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, "end");
2395796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
2405796c8dcSSimon Schubert 	  list = list->next;
2415796c8dcSSimon Schubert 	  continue;
2425796c8dcSSimon Schubert 	}
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert       if (list->control_type == python_control)
2455796c8dcSSimon Schubert 	{
2465796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, "python");
2475796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
2485796c8dcSSimon Schubert 	  /* Don't indent python code at all.  */
2495796c8dcSSimon Schubert 	  print_command_lines (uiout, *list->body_list, 0);
2505796c8dcSSimon Schubert 	  if (depth)
2515796c8dcSSimon Schubert 	    ui_out_spaces (uiout, 2 * depth);
2525796c8dcSSimon Schubert 	  ui_out_field_string (uiout, NULL, "end");
2535796c8dcSSimon Schubert 	  ui_out_text (uiout, "\n");
2545796c8dcSSimon Schubert 	  list = list->next;
2555796c8dcSSimon Schubert 	  continue;
2565796c8dcSSimon Schubert 	}
2575796c8dcSSimon Schubert 
258c50c785cSJohn Marino       /* Ignore illegal command type and try next.  */
2595796c8dcSSimon Schubert       list = list->next;
2605796c8dcSSimon Schubert     }				/* while (list) */
2615796c8dcSSimon Schubert }
2625796c8dcSSimon Schubert 
2635796c8dcSSimon Schubert /* Handle pre-post hooks.  */
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert static void
clear_hook_in_cleanup(void * data)2665796c8dcSSimon Schubert clear_hook_in_cleanup (void *data)
2675796c8dcSSimon Schubert {
2685796c8dcSSimon Schubert   struct cmd_list_element *c = data;
269cf7f2e2dSJohn Marino 
270c50c785cSJohn Marino   c->hook_in = 0; /* Allow hook to work again once it is complete.  */
2715796c8dcSSimon Schubert }
2725796c8dcSSimon Schubert 
2735796c8dcSSimon Schubert void
execute_cmd_pre_hook(struct cmd_list_element * c)2745796c8dcSSimon Schubert execute_cmd_pre_hook (struct cmd_list_element *c)
2755796c8dcSSimon Schubert {
2765796c8dcSSimon Schubert   if ((c->hook_pre) && (!c->hook_in))
2775796c8dcSSimon Schubert     {
2785796c8dcSSimon Schubert       struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
279c50c785cSJohn Marino       c->hook_in = 1; /* Prevent recursive hooking.  */
2805796c8dcSSimon Schubert       execute_user_command (c->hook_pre, (char *) 0);
2815796c8dcSSimon Schubert       do_cleanups (cleanups);
2825796c8dcSSimon Schubert     }
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert void
execute_cmd_post_hook(struct cmd_list_element * c)2865796c8dcSSimon Schubert execute_cmd_post_hook (struct cmd_list_element *c)
2875796c8dcSSimon Schubert {
2885796c8dcSSimon Schubert   if ((c->hook_post) && (!c->hook_in))
2895796c8dcSSimon Schubert     {
2905796c8dcSSimon Schubert       struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
291cf7f2e2dSJohn Marino 
292c50c785cSJohn Marino       c->hook_in = 1; /* Prevent recursive hooking.  */
2935796c8dcSSimon Schubert       execute_user_command (c->hook_post, (char *) 0);
2945796c8dcSSimon Schubert       do_cleanups (cleanups);
2955796c8dcSSimon Schubert     }
2965796c8dcSSimon Schubert }
2975796c8dcSSimon Schubert 
2985796c8dcSSimon Schubert /* Execute the command in CMD.  */
2995796c8dcSSimon Schubert static void
do_restore_user_call_depth(void * call_depth)3005796c8dcSSimon Schubert do_restore_user_call_depth (void * call_depth)
3015796c8dcSSimon Schubert {
3025796c8dcSSimon Schubert   int *depth = call_depth;
303cf7f2e2dSJohn Marino 
3045796c8dcSSimon Schubert   (*depth)--;
3055796c8dcSSimon Schubert   if ((*depth) == 0)
3065796c8dcSSimon Schubert     in_user_command = 0;
3075796c8dcSSimon Schubert }
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert 
3105796c8dcSSimon Schubert void
execute_user_command(struct cmd_list_element * c,char * args)3115796c8dcSSimon Schubert execute_user_command (struct cmd_list_element *c, char *args)
3125796c8dcSSimon Schubert {
3135796c8dcSSimon Schubert   struct command_line *cmdlines;
3145796c8dcSSimon Schubert   struct cleanup *old_chain;
3155796c8dcSSimon Schubert   enum command_control_type ret;
3165796c8dcSSimon Schubert   static int user_call_depth = 0;
317*ef5ccd6cSJohn Marino   extern unsigned int max_user_call_depth;
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert   cmdlines = c->user_commands;
3205796c8dcSSimon Schubert   if (cmdlines == 0)
3215796c8dcSSimon Schubert     /* Null command */
3225796c8dcSSimon Schubert     return;
3235796c8dcSSimon Schubert 
324a45ae5f8SJohn Marino   old_chain = setup_user_args (args);
325a45ae5f8SJohn Marino 
3265796c8dcSSimon Schubert   if (++user_call_depth > max_user_call_depth)
3275796c8dcSSimon Schubert     error (_("Max user call depth exceeded -- command aborted."));
3285796c8dcSSimon Schubert 
3295796c8dcSSimon Schubert   make_cleanup (do_restore_user_call_depth, &user_call_depth);
3305796c8dcSSimon Schubert 
3315796c8dcSSimon Schubert   /* Set the instream to 0, indicating execution of a
3325796c8dcSSimon Schubert      user-defined function.  */
3335796c8dcSSimon Schubert   make_cleanup (do_restore_instream_cleanup, instream);
3345796c8dcSSimon Schubert   instream = (FILE *) 0;
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert   /* Also set the global in_user_command, so that NULL instream is
3375796c8dcSSimon Schubert      not confused with Insight.  */
3385796c8dcSSimon Schubert   in_user_command = 1;
3395796c8dcSSimon Schubert 
340a45ae5f8SJohn Marino   make_cleanup_restore_integer (&interpreter_async);
341a45ae5f8SJohn Marino   interpreter_async = 0;
342a45ae5f8SJohn Marino 
3435796c8dcSSimon Schubert   command_nest_depth++;
3445796c8dcSSimon Schubert   while (cmdlines)
3455796c8dcSSimon Schubert     {
3465796c8dcSSimon Schubert       ret = execute_control_command (cmdlines);
3475796c8dcSSimon Schubert       if (ret != simple_control && ret != break_control)
3485796c8dcSSimon Schubert 	{
3495796c8dcSSimon Schubert 	  warning (_("Error executing canned sequence of commands."));
3505796c8dcSSimon Schubert 	  break;
3515796c8dcSSimon Schubert 	}
3525796c8dcSSimon Schubert       cmdlines = cmdlines->next;
3535796c8dcSSimon Schubert     }
3545796c8dcSSimon Schubert   command_nest_depth--;
3555796c8dcSSimon Schubert   do_cleanups (old_chain);
3565796c8dcSSimon Schubert }
3575796c8dcSSimon Schubert 
358c50c785cSJohn Marino /* This function is called every time GDB prints a prompt.  It ensures
359c50c785cSJohn Marino    that errors and the like do not confuse the command tracing.  */
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert void
reset_command_nest_depth(void)3625796c8dcSSimon Schubert reset_command_nest_depth (void)
3635796c8dcSSimon Schubert {
3645796c8dcSSimon Schubert   command_nest_depth = 1;
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert   /* Just in case.  */
3675796c8dcSSimon Schubert   suppress_next_print_command_trace = 0;
3685796c8dcSSimon Schubert }
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert /* Print the command, prefixed with '+' to represent the call depth.
3715796c8dcSSimon Schubert    This is slightly complicated because this function may be called
3725796c8dcSSimon Schubert    from execute_command and execute_control_command.  Unfortunately
3735796c8dcSSimon Schubert    execute_command also prints the top level control commands.
3745796c8dcSSimon Schubert    In these cases execute_command will call execute_control_command
3755796c8dcSSimon Schubert    via while_command or if_command.  Inner levels of 'if' and 'while'
3765796c8dcSSimon Schubert    are dealt with directly.  Therefore we can use these functions
3775796c8dcSSimon Schubert    to determine whether the command has been printed already or not.  */
3785796c8dcSSimon Schubert void
print_command_trace(const char * cmd)3795796c8dcSSimon Schubert print_command_trace (const char *cmd)
3805796c8dcSSimon Schubert {
3815796c8dcSSimon Schubert   int i;
3825796c8dcSSimon Schubert 
3835796c8dcSSimon Schubert   if (suppress_next_print_command_trace)
3845796c8dcSSimon Schubert     {
3855796c8dcSSimon Schubert       suppress_next_print_command_trace = 0;
3865796c8dcSSimon Schubert       return;
3875796c8dcSSimon Schubert     }
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert   if (!source_verbose && !trace_commands)
3905796c8dcSSimon Schubert     return;
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert   for (i=0; i < command_nest_depth; i++)
3935796c8dcSSimon Schubert     printf_filtered ("+");
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert   printf_filtered ("%s\n", cmd);
3965796c8dcSSimon Schubert }
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert enum command_control_type
execute_control_command(struct command_line * cmd)3995796c8dcSSimon Schubert execute_control_command (struct command_line *cmd)
4005796c8dcSSimon Schubert {
4015796c8dcSSimon Schubert   struct expression *expr;
4025796c8dcSSimon Schubert   struct command_line *current;
4035796c8dcSSimon Schubert   struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
4045796c8dcSSimon Schubert   struct value *val;
4055796c8dcSSimon Schubert   struct value *val_mark;
4065796c8dcSSimon Schubert   int loop;
4075796c8dcSSimon Schubert   enum command_control_type ret;
4085796c8dcSSimon Schubert   char *new_line;
4095796c8dcSSimon Schubert 
4105796c8dcSSimon Schubert   /* Start by assuming failure, if a problem is detected, the code
4115796c8dcSSimon Schubert      below will simply "break" out of the switch.  */
4125796c8dcSSimon Schubert   ret = invalid_control;
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert   switch (cmd->control_type)
4155796c8dcSSimon Schubert     {
4165796c8dcSSimon Schubert     case simple_control:
4175796c8dcSSimon Schubert       /* A simple command, execute it and return.  */
4185796c8dcSSimon Schubert       new_line = insert_args (cmd->line);
4195796c8dcSSimon Schubert       if (!new_line)
4205796c8dcSSimon Schubert 	break;
4215796c8dcSSimon Schubert       make_cleanup (free_current_contents, &new_line);
4225796c8dcSSimon Schubert       execute_command (new_line, 0);
4235796c8dcSSimon Schubert       ret = cmd->control_type;
4245796c8dcSSimon Schubert       break;
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert     case continue_control:
4275796c8dcSSimon Schubert       print_command_trace ("loop_continue");
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert       /* Return for "continue", and "break" so we can either
4305796c8dcSSimon Schubert          continue the loop at the top, or break out.  */
4315796c8dcSSimon Schubert       ret = cmd->control_type;
4325796c8dcSSimon Schubert       break;
4335796c8dcSSimon Schubert 
4345796c8dcSSimon Schubert     case break_control:
4355796c8dcSSimon Schubert       print_command_trace ("loop_break");
4365796c8dcSSimon Schubert 
4375796c8dcSSimon Schubert       /* Return for "continue", and "break" so we can either
4385796c8dcSSimon Schubert          continue the loop at the top, or break out.  */
4395796c8dcSSimon Schubert       ret = cmd->control_type;
4405796c8dcSSimon Schubert       break;
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert     case while_control:
4435796c8dcSSimon Schubert       {
444*ef5ccd6cSJohn Marino 	int len = strlen (cmd->line) + 7;
445*ef5ccd6cSJohn Marino 	char *buffer = alloca (len);
446cf7f2e2dSJohn Marino 
447*ef5ccd6cSJohn Marino 	xsnprintf (buffer, len, "while %s", cmd->line);
4485796c8dcSSimon Schubert 	print_command_trace (buffer);
4495796c8dcSSimon Schubert 
4505796c8dcSSimon Schubert 	/* Parse the loop control expression for the while statement.  */
4515796c8dcSSimon Schubert 	new_line = insert_args (cmd->line);
4525796c8dcSSimon Schubert 	if (!new_line)
4535796c8dcSSimon Schubert 	  break;
4545796c8dcSSimon Schubert 	make_cleanup (free_current_contents, &new_line);
4555796c8dcSSimon Schubert 	expr = parse_expression (new_line);
4565796c8dcSSimon Schubert 	make_cleanup (free_current_contents, &expr);
4575796c8dcSSimon Schubert 
4585796c8dcSSimon Schubert 	ret = simple_control;
4595796c8dcSSimon Schubert 	loop = 1;
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert 	/* Keep iterating so long as the expression is true.  */
4625796c8dcSSimon Schubert 	while (loop == 1)
4635796c8dcSSimon Schubert 	  {
4645796c8dcSSimon Schubert 	    int cond_result;
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert 	    QUIT;
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert 	    /* Evaluate the expression.  */
4695796c8dcSSimon Schubert 	    val_mark = value_mark ();
4705796c8dcSSimon Schubert 	    val = evaluate_expression (expr);
4715796c8dcSSimon Schubert 	    cond_result = value_true (val);
4725796c8dcSSimon Schubert 	    value_free_to_mark (val_mark);
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert 	    /* If the value is false, then break out of the loop.  */
4755796c8dcSSimon Schubert 	    if (!cond_result)
4765796c8dcSSimon Schubert 	      break;
4775796c8dcSSimon Schubert 
4785796c8dcSSimon Schubert 	    /* Execute the body of the while statement.  */
4795796c8dcSSimon Schubert 	    current = *cmd->body_list;
4805796c8dcSSimon Schubert 	    while (current)
4815796c8dcSSimon Schubert 	      {
4825796c8dcSSimon Schubert 		command_nest_depth++;
4835796c8dcSSimon Schubert 		ret = execute_control_command (current);
4845796c8dcSSimon Schubert 		command_nest_depth--;
4855796c8dcSSimon Schubert 
4865796c8dcSSimon Schubert 		/* If we got an error, or a "break" command, then stop
4875796c8dcSSimon Schubert 		   looping.  */
4885796c8dcSSimon Schubert 		if (ret == invalid_control || ret == break_control)
4895796c8dcSSimon Schubert 		  {
4905796c8dcSSimon Schubert 		    loop = 0;
4915796c8dcSSimon Schubert 		    break;
4925796c8dcSSimon Schubert 		  }
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert 		/* If we got a "continue" command, then restart the loop
4955796c8dcSSimon Schubert 		   at this point.  */
4965796c8dcSSimon Schubert 		if (ret == continue_control)
4975796c8dcSSimon Schubert 		  break;
4985796c8dcSSimon Schubert 
4995796c8dcSSimon Schubert 		/* Get the next statement.  */
5005796c8dcSSimon Schubert 		current = current->next;
5015796c8dcSSimon Schubert 	      }
5025796c8dcSSimon Schubert 	  }
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert 	/* Reset RET so that we don't recurse the break all the way down.  */
5055796c8dcSSimon Schubert 	if (ret == break_control)
5065796c8dcSSimon Schubert 	  ret = simple_control;
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert 	break;
5095796c8dcSSimon Schubert       }
5105796c8dcSSimon Schubert 
5115796c8dcSSimon Schubert     case if_control:
5125796c8dcSSimon Schubert       {
513*ef5ccd6cSJohn Marino 	int len = strlen (cmd->line) + 4;
514*ef5ccd6cSJohn Marino 	char *buffer = alloca (len);
515cf7f2e2dSJohn Marino 
516*ef5ccd6cSJohn Marino 	xsnprintf (buffer, len, "if %s", cmd->line);
5175796c8dcSSimon Schubert 	print_command_trace (buffer);
5185796c8dcSSimon Schubert 
5195796c8dcSSimon Schubert 	new_line = insert_args (cmd->line);
5205796c8dcSSimon Schubert 	if (!new_line)
5215796c8dcSSimon Schubert 	  break;
5225796c8dcSSimon Schubert 	make_cleanup (free_current_contents, &new_line);
5235796c8dcSSimon Schubert 	/* Parse the conditional for the if statement.  */
5245796c8dcSSimon Schubert 	expr = parse_expression (new_line);
5255796c8dcSSimon Schubert 	make_cleanup (free_current_contents, &expr);
5265796c8dcSSimon Schubert 
5275796c8dcSSimon Schubert 	current = NULL;
5285796c8dcSSimon Schubert 	ret = simple_control;
5295796c8dcSSimon Schubert 
5305796c8dcSSimon Schubert 	/* Evaluate the conditional.  */
5315796c8dcSSimon Schubert 	val_mark = value_mark ();
5325796c8dcSSimon Schubert 	val = evaluate_expression (expr);
5335796c8dcSSimon Schubert 
534c50c785cSJohn Marino 	/* Choose which arm to take commands from based on the value
535c50c785cSJohn Marino 	   of the conditional expression.  */
5365796c8dcSSimon Schubert 	if (value_true (val))
5375796c8dcSSimon Schubert 	  current = *cmd->body_list;
5385796c8dcSSimon Schubert 	else if (cmd->body_count == 2)
5395796c8dcSSimon Schubert 	  current = *(cmd->body_list + 1);
5405796c8dcSSimon Schubert 	value_free_to_mark (val_mark);
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert 	/* Execute commands in the given arm.  */
5435796c8dcSSimon Schubert 	while (current)
5445796c8dcSSimon Schubert 	  {
5455796c8dcSSimon Schubert 	    command_nest_depth++;
5465796c8dcSSimon Schubert 	    ret = execute_control_command (current);
5475796c8dcSSimon Schubert 	    command_nest_depth--;
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert 	    /* If we got an error, get out.  */
5505796c8dcSSimon Schubert 	    if (ret != simple_control)
5515796c8dcSSimon Schubert 	      break;
5525796c8dcSSimon Schubert 
5535796c8dcSSimon Schubert 	    /* Get the next statement in the body.  */
5545796c8dcSSimon Schubert 	    current = current->next;
5555796c8dcSSimon Schubert 	  }
5565796c8dcSSimon Schubert 
5575796c8dcSSimon Schubert 	break;
5585796c8dcSSimon Schubert       }
5595796c8dcSSimon Schubert     case commands_control:
5605796c8dcSSimon Schubert       {
561c50c785cSJohn Marino 	/* Breakpoint commands list, record the commands in the
562c50c785cSJohn Marino 	   breakpoint's command list and return.  */
5635796c8dcSSimon Schubert 	new_line = insert_args (cmd->line);
5645796c8dcSSimon Schubert 	if (!new_line)
5655796c8dcSSimon Schubert 	  break;
5665796c8dcSSimon Schubert 	make_cleanup (free_current_contents, &new_line);
5675796c8dcSSimon Schubert 	ret = commands_from_control_command (new_line, cmd);
5685796c8dcSSimon Schubert 	break;
5695796c8dcSSimon Schubert       }
5705796c8dcSSimon Schubert     case python_control:
5715796c8dcSSimon Schubert       {
5725796c8dcSSimon Schubert 	eval_python_from_control_command (cmd);
5735796c8dcSSimon Schubert 	ret = simple_control;
5745796c8dcSSimon Schubert 	break;
5755796c8dcSSimon Schubert       }
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert     default:
5785796c8dcSSimon Schubert       warning (_("Invalid control type in canned commands structure."));
5795796c8dcSSimon Schubert       break;
5805796c8dcSSimon Schubert     }
5815796c8dcSSimon Schubert 
5825796c8dcSSimon Schubert   do_cleanups (old_chain);
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert   return ret;
5855796c8dcSSimon Schubert }
5865796c8dcSSimon Schubert 
5875796c8dcSSimon Schubert /* Like execute_control_command, but first set
5885796c8dcSSimon Schubert    suppress_next_print_command_trace.  */
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert enum command_control_type
execute_control_command_untraced(struct command_line * cmd)5915796c8dcSSimon Schubert execute_control_command_untraced (struct command_line *cmd)
5925796c8dcSSimon Schubert {
5935796c8dcSSimon Schubert   suppress_next_print_command_trace = 1;
5945796c8dcSSimon Schubert   return execute_control_command (cmd);
5955796c8dcSSimon Schubert }
5965796c8dcSSimon Schubert 
5975796c8dcSSimon Schubert 
5985796c8dcSSimon Schubert /* "while" command support.  Executes a body of statements while the
5995796c8dcSSimon Schubert    loop condition is nonzero.  */
6005796c8dcSSimon Schubert 
601*ef5ccd6cSJohn Marino static void
while_command(char * arg,int from_tty)6025796c8dcSSimon Schubert while_command (char *arg, int from_tty)
6035796c8dcSSimon Schubert {
6045796c8dcSSimon Schubert   struct command_line *command = NULL;
605a45ae5f8SJohn Marino   struct cleanup *old_chain;
6065796c8dcSSimon Schubert 
6075796c8dcSSimon Schubert   control_level = 1;
6085796c8dcSSimon Schubert   command = get_command_line (while_control, arg);
6095796c8dcSSimon Schubert 
6105796c8dcSSimon Schubert   if (command == NULL)
6115796c8dcSSimon Schubert     return;
6125796c8dcSSimon Schubert 
613a45ae5f8SJohn Marino   old_chain = make_cleanup_restore_integer (&interpreter_async);
614a45ae5f8SJohn Marino   interpreter_async = 0;
615a45ae5f8SJohn Marino 
6165796c8dcSSimon Schubert   execute_control_command_untraced (command);
6175796c8dcSSimon Schubert   free_command_lines (&command);
618a45ae5f8SJohn Marino 
619a45ae5f8SJohn Marino   do_cleanups (old_chain);
6205796c8dcSSimon Schubert }
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert /* "if" command support.  Execute either the true or false arm depending
6235796c8dcSSimon Schubert    on the value of the if conditional.  */
6245796c8dcSSimon Schubert 
625*ef5ccd6cSJohn Marino static void
if_command(char * arg,int from_tty)6265796c8dcSSimon Schubert if_command (char *arg, int from_tty)
6275796c8dcSSimon Schubert {
6285796c8dcSSimon Schubert   struct command_line *command = NULL;
629a45ae5f8SJohn Marino   struct cleanup *old_chain;
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert   control_level = 1;
6325796c8dcSSimon Schubert   command = get_command_line (if_control, arg);
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert   if (command == NULL)
6355796c8dcSSimon Schubert     return;
6365796c8dcSSimon Schubert 
637a45ae5f8SJohn Marino   old_chain = make_cleanup_restore_integer (&interpreter_async);
638a45ae5f8SJohn Marino   interpreter_async = 0;
639a45ae5f8SJohn Marino 
6405796c8dcSSimon Schubert   execute_control_command_untraced (command);
6415796c8dcSSimon Schubert   free_command_lines (&command);
642a45ae5f8SJohn Marino 
643a45ae5f8SJohn Marino   do_cleanups (old_chain);
6445796c8dcSSimon Schubert }
6455796c8dcSSimon Schubert 
6465796c8dcSSimon Schubert /* Cleanup */
6475796c8dcSSimon Schubert static void
arg_cleanup(void * ignore)6485796c8dcSSimon Schubert arg_cleanup (void *ignore)
6495796c8dcSSimon Schubert {
6505796c8dcSSimon Schubert   struct user_args *oargs = user_args;
651cf7f2e2dSJohn Marino 
6525796c8dcSSimon Schubert   if (!user_args)
6535796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
6545796c8dcSSimon Schubert 		    _("arg_cleanup called with no user args.\n"));
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert   user_args = user_args->next;
6575796c8dcSSimon Schubert   xfree (oargs->command);
6585796c8dcSSimon Schubert   xfree (oargs);
6595796c8dcSSimon Schubert }
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert /* Bind the incomming arguments for a user defined command to
6625796c8dcSSimon Schubert    $arg0, $arg1 ... $argMAXUSERARGS.  */
6635796c8dcSSimon Schubert 
6645796c8dcSSimon Schubert static struct cleanup *
setup_user_args(char * p)6655796c8dcSSimon Schubert setup_user_args (char *p)
6665796c8dcSSimon Schubert {
6675796c8dcSSimon Schubert   struct user_args *args;
6685796c8dcSSimon Schubert   struct cleanup *old_chain;
6695796c8dcSSimon Schubert   unsigned int arg_count = 0;
6705796c8dcSSimon Schubert 
6715796c8dcSSimon Schubert   args = (struct user_args *) xmalloc (sizeof (struct user_args));
6725796c8dcSSimon Schubert   memset (args, 0, sizeof (struct user_args));
6735796c8dcSSimon Schubert 
6745796c8dcSSimon Schubert   args->next = user_args;
6755796c8dcSSimon Schubert   user_args = args;
6765796c8dcSSimon Schubert 
6775796c8dcSSimon Schubert   old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
6785796c8dcSSimon Schubert 
6795796c8dcSSimon Schubert   if (p == NULL)
6805796c8dcSSimon Schubert     return old_chain;
6815796c8dcSSimon Schubert 
6825796c8dcSSimon Schubert   user_args->command = p = xstrdup (p);
6835796c8dcSSimon Schubert 
6845796c8dcSSimon Schubert   while (*p)
6855796c8dcSSimon Schubert     {
6865796c8dcSSimon Schubert       char *start_arg;
6875796c8dcSSimon Schubert       int squote = 0;
6885796c8dcSSimon Schubert       int dquote = 0;
6895796c8dcSSimon Schubert       int bsquote = 0;
6905796c8dcSSimon Schubert 
6915796c8dcSSimon Schubert       if (arg_count >= MAXUSERARGS)
6925796c8dcSSimon Schubert 	{
6935796c8dcSSimon Schubert 	  error (_("user defined function may only have %d arguments."),
6945796c8dcSSimon Schubert 		 MAXUSERARGS);
6955796c8dcSSimon Schubert 	  return old_chain;
6965796c8dcSSimon Schubert 	}
6975796c8dcSSimon Schubert 
6985796c8dcSSimon Schubert       /* Strip whitespace.  */
6995796c8dcSSimon Schubert       while (*p == ' ' || *p == '\t')
7005796c8dcSSimon Schubert 	p++;
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert       /* P now points to an argument.  */
7035796c8dcSSimon Schubert       start_arg = p;
7045796c8dcSSimon Schubert       user_args->a[arg_count].arg = p;
7055796c8dcSSimon Schubert 
7065796c8dcSSimon Schubert       /* Get to the end of this argument.  */
7075796c8dcSSimon Schubert       while (*p)
7085796c8dcSSimon Schubert 	{
7095796c8dcSSimon Schubert 	  if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
7105796c8dcSSimon Schubert 	    break;
7115796c8dcSSimon Schubert 	  else
7125796c8dcSSimon Schubert 	    {
7135796c8dcSSimon Schubert 	      if (bsquote)
7145796c8dcSSimon Schubert 		bsquote = 0;
7155796c8dcSSimon Schubert 	      else if (*p == '\\')
7165796c8dcSSimon Schubert 		bsquote = 1;
7175796c8dcSSimon Schubert 	      else if (squote)
7185796c8dcSSimon Schubert 		{
7195796c8dcSSimon Schubert 		  if (*p == '\'')
7205796c8dcSSimon Schubert 		    squote = 0;
7215796c8dcSSimon Schubert 		}
7225796c8dcSSimon Schubert 	      else if (dquote)
7235796c8dcSSimon Schubert 		{
7245796c8dcSSimon Schubert 		  if (*p == '"')
7255796c8dcSSimon Schubert 		    dquote = 0;
7265796c8dcSSimon Schubert 		}
7275796c8dcSSimon Schubert 	      else
7285796c8dcSSimon Schubert 		{
7295796c8dcSSimon Schubert 		  if (*p == '\'')
7305796c8dcSSimon Schubert 		    squote = 1;
7315796c8dcSSimon Schubert 		  else if (*p == '"')
7325796c8dcSSimon Schubert 		    dquote = 1;
7335796c8dcSSimon Schubert 		}
7345796c8dcSSimon Schubert 	      p++;
7355796c8dcSSimon Schubert 	    }
7365796c8dcSSimon Schubert 	}
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert       user_args->a[arg_count].len = p - start_arg;
7395796c8dcSSimon Schubert       arg_count++;
7405796c8dcSSimon Schubert       user_args->count++;
7415796c8dcSSimon Schubert     }
7425796c8dcSSimon Schubert   return old_chain;
7435796c8dcSSimon Schubert }
7445796c8dcSSimon Schubert 
745c50c785cSJohn Marino /* Given character string P, return a point to the first argument
746c50c785cSJohn Marino    ($arg), or NULL if P contains no arguments.  */
7475796c8dcSSimon Schubert 
7485796c8dcSSimon Schubert static char *
locate_arg(char * p)7495796c8dcSSimon Schubert locate_arg (char *p)
7505796c8dcSSimon Schubert {
7515796c8dcSSimon Schubert   while ((p = strchr (p, '$')))
7525796c8dcSSimon Schubert     {
7535796c8dcSSimon Schubert       if (strncmp (p, "$arg", 4) == 0
7545796c8dcSSimon Schubert 	  && (isdigit (p[4]) || p[4] == 'c'))
7555796c8dcSSimon Schubert 	return p;
7565796c8dcSSimon Schubert       p++;
7575796c8dcSSimon Schubert     }
7585796c8dcSSimon Schubert   return NULL;
7595796c8dcSSimon Schubert }
7605796c8dcSSimon Schubert 
7615796c8dcSSimon Schubert /* Insert the user defined arguments stored in user_arg into the $arg
762c50c785cSJohn Marino    arguments found in line, with the updated copy being placed into
763c50c785cSJohn Marino    nline.  */
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert static char *
insert_args(char * line)7665796c8dcSSimon Schubert insert_args (char *line)
7675796c8dcSSimon Schubert {
7685796c8dcSSimon Schubert   char *p, *save_line, *new_line;
7695796c8dcSSimon Schubert   unsigned len, i;
7705796c8dcSSimon Schubert 
7715796c8dcSSimon Schubert   /* If we are not in a user-defined function, treat $argc, $arg0, et
7725796c8dcSSimon Schubert      cetera as normal convenience variables.  */
7735796c8dcSSimon Schubert   if (user_args == NULL)
7745796c8dcSSimon Schubert     return xstrdup (line);
7755796c8dcSSimon Schubert 
776c50c785cSJohn Marino   /* First we need to know how much memory to allocate for the new
777c50c785cSJohn Marino      line.  */
7785796c8dcSSimon Schubert   save_line = line;
7795796c8dcSSimon Schubert   len = 0;
7805796c8dcSSimon Schubert   while ((p = locate_arg (line)))
7815796c8dcSSimon Schubert     {
7825796c8dcSSimon Schubert       len += p - line;
7835796c8dcSSimon Schubert       i = p[4] - '0';
7845796c8dcSSimon Schubert 
7855796c8dcSSimon Schubert       if (p[4] == 'c')
7865796c8dcSSimon Schubert 	{
7875796c8dcSSimon Schubert 	  /* $argc.  Number will be <=10.  */
7885796c8dcSSimon Schubert 	  len += user_args->count == 10 ? 2 : 1;
7895796c8dcSSimon Schubert 	}
7905796c8dcSSimon Schubert       else if (i >= user_args->count)
7915796c8dcSSimon Schubert 	{
7925796c8dcSSimon Schubert 	  error (_("Missing argument %d in user function."), i);
7935796c8dcSSimon Schubert 	  return NULL;
7945796c8dcSSimon Schubert 	}
7955796c8dcSSimon Schubert       else
7965796c8dcSSimon Schubert 	{
7975796c8dcSSimon Schubert 	  len += user_args->a[i].len;
7985796c8dcSSimon Schubert 	}
7995796c8dcSSimon Schubert       line = p + 5;
8005796c8dcSSimon Schubert     }
8015796c8dcSSimon Schubert 
8025796c8dcSSimon Schubert   /* Don't forget the tail.  */
8035796c8dcSSimon Schubert   len += strlen (line);
8045796c8dcSSimon Schubert 
8055796c8dcSSimon Schubert   /* Allocate space for the new line and fill it in.  */
8065796c8dcSSimon Schubert   new_line = (char *) xmalloc (len + 1);
8075796c8dcSSimon Schubert   if (new_line == NULL)
8085796c8dcSSimon Schubert     return NULL;
8095796c8dcSSimon Schubert 
8105796c8dcSSimon Schubert   /* Restore pointer to beginning of old line.  */
8115796c8dcSSimon Schubert   line = save_line;
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert   /* Save pointer to beginning of new line.  */
8145796c8dcSSimon Schubert   save_line = new_line;
8155796c8dcSSimon Schubert 
8165796c8dcSSimon Schubert   while ((p = locate_arg (line)))
8175796c8dcSSimon Schubert     {
8185796c8dcSSimon Schubert       int i, len;
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert       memcpy (new_line, line, p - line);
8215796c8dcSSimon Schubert       new_line += p - line;
8225796c8dcSSimon Schubert 
8235796c8dcSSimon Schubert       if (p[4] == 'c')
8245796c8dcSSimon Schubert 	{
8255796c8dcSSimon Schubert 	  gdb_assert (user_args->count >= 0 && user_args->count <= 10);
8265796c8dcSSimon Schubert 	  if (user_args->count == 10)
8275796c8dcSSimon Schubert 	    {
8285796c8dcSSimon Schubert 	      *(new_line++) = '1';
8295796c8dcSSimon Schubert 	      *(new_line++) = '0';
8305796c8dcSSimon Schubert 	    }
8315796c8dcSSimon Schubert 	  else
8325796c8dcSSimon Schubert 	    *(new_line++) = user_args->count + '0';
8335796c8dcSSimon Schubert 	}
8345796c8dcSSimon Schubert       else
8355796c8dcSSimon Schubert 	{
8365796c8dcSSimon Schubert 	  i = p[4] - '0';
8375796c8dcSSimon Schubert 	  len = user_args->a[i].len;
8385796c8dcSSimon Schubert 	  if (len)
8395796c8dcSSimon Schubert 	    {
8405796c8dcSSimon Schubert 	      memcpy (new_line, user_args->a[i].arg, len);
8415796c8dcSSimon Schubert 	      new_line += len;
8425796c8dcSSimon Schubert 	    }
8435796c8dcSSimon Schubert 	}
8445796c8dcSSimon Schubert       line = p + 5;
8455796c8dcSSimon Schubert     }
8465796c8dcSSimon Schubert   /* Don't forget the tail.  */
8475796c8dcSSimon Schubert   strcpy (new_line, line);
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert   /* Return a pointer to the beginning of the new line.  */
8505796c8dcSSimon Schubert   return save_line;
8515796c8dcSSimon Schubert }
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert 
8545796c8dcSSimon Schubert /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
8555796c8dcSSimon Schubert    code bodies.  This is typically used when we encounter an "else"
8565796c8dcSSimon Schubert    clause for an "if" command.  */
8575796c8dcSSimon Schubert 
8585796c8dcSSimon Schubert static void
realloc_body_list(struct command_line * command,int new_length)8595796c8dcSSimon Schubert realloc_body_list (struct command_line *command, int new_length)
8605796c8dcSSimon Schubert {
8615796c8dcSSimon Schubert   int n;
8625796c8dcSSimon Schubert   struct command_line **body_list;
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert   n = command->body_count;
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert   /* Nothing to do?  */
8675796c8dcSSimon Schubert   if (new_length <= n)
8685796c8dcSSimon Schubert     return;
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert   body_list = (struct command_line **)
8715796c8dcSSimon Schubert     xmalloc (sizeof (struct command_line *) * new_length);
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
8745796c8dcSSimon Schubert   memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert   xfree (command->body_list);
8775796c8dcSSimon Schubert   command->body_list = body_list;
8785796c8dcSSimon Schubert   command->body_count = new_length;
8795796c8dcSSimon Schubert }
8805796c8dcSSimon Schubert 
8815796c8dcSSimon Schubert /* Read next line from stdout.  Passed to read_command_line_1 and
8825796c8dcSSimon Schubert    recurse_read_control_structure whenever we need to read commands
8835796c8dcSSimon Schubert    from stdout.  */
8845796c8dcSSimon Schubert 
8855796c8dcSSimon Schubert static char *
read_next_line(void)886cf7f2e2dSJohn Marino read_next_line (void)
8875796c8dcSSimon Schubert {
8885796c8dcSSimon Schubert   char *prompt_ptr, control_prompt[256];
8895796c8dcSSimon Schubert   int i = 0;
8905796c8dcSSimon Schubert 
8915796c8dcSSimon Schubert   if (control_level >= 254)
8925796c8dcSSimon Schubert     error (_("Control nesting too deep!"));
8935796c8dcSSimon Schubert 
8945796c8dcSSimon Schubert   /* Set a prompt based on the nesting of the control commands.  */
8955796c8dcSSimon Schubert   if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
8965796c8dcSSimon Schubert     {
8975796c8dcSSimon Schubert       for (i = 0; i < control_level; i++)
8985796c8dcSSimon Schubert 	control_prompt[i] = ' ';
8995796c8dcSSimon Schubert       control_prompt[i] = '>';
9005796c8dcSSimon Schubert       control_prompt[i + 1] = '\0';
9015796c8dcSSimon Schubert       prompt_ptr = (char *) &control_prompt[0];
9025796c8dcSSimon Schubert     }
9035796c8dcSSimon Schubert   else
9045796c8dcSSimon Schubert     prompt_ptr = NULL;
9055796c8dcSSimon Schubert 
9065796c8dcSSimon Schubert   return command_line_input (prompt_ptr, instream == stdin, "commands");
9075796c8dcSSimon Schubert }
9085796c8dcSSimon Schubert 
909c50c785cSJohn Marino /* Process one input line.  If the command is an "end", return such an
910c50c785cSJohn Marino    indication to the caller.  If PARSE_COMMANDS is true, strip leading
911c50c785cSJohn Marino    whitespace (trailing whitespace is always stripped) in the line,
912c50c785cSJohn Marino    attempt to recognize GDB control commands, and also return an
913c50c785cSJohn Marino    indication if the command is an "else" or a nop.
914c50c785cSJohn Marino 
9155796c8dcSSimon Schubert    Otherwise, only "end" is recognized.  */
9165796c8dcSSimon Schubert 
9175796c8dcSSimon Schubert static enum misc_command_type
process_next_line(char * p,struct command_line ** command,int parse_commands,void (* validator)(char *,void *),void * closure)918cf7f2e2dSJohn Marino process_next_line (char *p, struct command_line **command, int parse_commands,
919cf7f2e2dSJohn Marino 		   void (*validator)(char *, void *), void *closure)
9205796c8dcSSimon Schubert {
921cf7f2e2dSJohn Marino   char *p_end;
922cf7f2e2dSJohn Marino   char *p_start;
9235796c8dcSSimon Schubert   int not_handled = 0;
9245796c8dcSSimon Schubert 
9255796c8dcSSimon Schubert   /* Not sure what to do here.  */
9265796c8dcSSimon Schubert   if (p == NULL)
9275796c8dcSSimon Schubert     return end_command;
9285796c8dcSSimon Schubert 
9295796c8dcSSimon Schubert   /* Strip trailing whitespace.  */
930cf7f2e2dSJohn Marino   p_end = p + strlen (p);
931cf7f2e2dSJohn Marino   while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
932cf7f2e2dSJohn Marino     p_end--;
9335796c8dcSSimon Schubert 
934cf7f2e2dSJohn Marino   p_start = p;
935cf7f2e2dSJohn Marino   /* Strip leading whitespace.  */
936cf7f2e2dSJohn Marino   while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
937cf7f2e2dSJohn Marino     p_start++;
938cf7f2e2dSJohn Marino 
939cf7f2e2dSJohn Marino   /* 'end' is always recognized, regardless of parse_commands value.
940cf7f2e2dSJohn Marino      We also permit whitespace before end and after.  */
941cf7f2e2dSJohn Marino   if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
9425796c8dcSSimon Schubert     return end_command;
9435796c8dcSSimon Schubert 
9445796c8dcSSimon Schubert   if (parse_commands)
9455796c8dcSSimon Schubert     {
946cf7f2e2dSJohn Marino       /* If commands are parsed, we skip initial spaces.  Otherwise,
947cf7f2e2dSJohn Marino 	 which is the case for Python commands and documentation
948cf7f2e2dSJohn Marino 	 (see the 'document' command), spaces are preserved.  */
949cf7f2e2dSJohn Marino       p = p_start;
950cf7f2e2dSJohn Marino 
9515796c8dcSSimon Schubert       /* Blanks and comments don't really do anything, but we need to
952c50c785cSJohn Marino 	 distinguish them from else, end and other commands which can
953c50c785cSJohn Marino 	 be executed.  */
954cf7f2e2dSJohn Marino       if (p_end == p || p[0] == '#')
9555796c8dcSSimon Schubert 	return nop_command;
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert       /* Is the else clause of an if control structure?  */
958cf7f2e2dSJohn Marino       if (p_end - p == 4 && !strncmp (p, "else", 4))
9595796c8dcSSimon Schubert 	return else_command;
9605796c8dcSSimon Schubert 
961c50c785cSJohn Marino       /* Check for while, if, break, continue, etc and build a new
962c50c785cSJohn Marino 	 command line structure for them.  */
963cf7f2e2dSJohn Marino       if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
964cf7f2e2dSJohn Marino 	  || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
965cf7f2e2dSJohn Marino 	  || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
966cf7f2e2dSJohn Marino 	{
967cf7f2e2dSJohn Marino 	  /* Because validate_actionline and encode_action lookup
968cf7f2e2dSJohn Marino 	     command's line as command, we need the line to
969cf7f2e2dSJohn Marino 	     include 'while-stepping'.
970cf7f2e2dSJohn Marino 
971cf7f2e2dSJohn Marino 	     For 'ws' alias, the command will have 'ws', not expanded
972cf7f2e2dSJohn Marino 	     to 'while-stepping'.  This is intentional -- we don't
973cf7f2e2dSJohn Marino 	     really want frontend to send a command list with 'ws',
974c50c785cSJohn Marino 	     and next break-info returning command line with
975c50c785cSJohn Marino 	     'while-stepping'.  This should work, but might cause the
976c50c785cSJohn Marino 	     breakpoint to be marked as changed while it's actually
977c50c785cSJohn Marino 	     not.  */
978cf7f2e2dSJohn Marino 	  *command = build_command_line (while_stepping_control, p);
979cf7f2e2dSJohn Marino 	}
980cf7f2e2dSJohn Marino       else if (p_end - p > 5 && !strncmp (p, "while", 5))
9815796c8dcSSimon Schubert 	{
9825796c8dcSSimon Schubert 	  char *first_arg;
983cf7f2e2dSJohn Marino 
9845796c8dcSSimon Schubert 	  first_arg = p + 5;
985cf7f2e2dSJohn Marino 	  while (first_arg < p_end && isspace (*first_arg))
9865796c8dcSSimon Schubert 	    first_arg++;
9875796c8dcSSimon Schubert 	  *command = build_command_line (while_control, first_arg);
9885796c8dcSSimon Schubert 	}
989cf7f2e2dSJohn Marino       else if (p_end - p > 2 && !strncmp (p, "if", 2))
9905796c8dcSSimon Schubert 	{
9915796c8dcSSimon Schubert 	  char *first_arg;
992cf7f2e2dSJohn Marino 
9935796c8dcSSimon Schubert 	  first_arg = p + 2;
994cf7f2e2dSJohn Marino 	  while (first_arg < p_end && isspace (*first_arg))
9955796c8dcSSimon Schubert 	    first_arg++;
9965796c8dcSSimon Schubert 	  *command = build_command_line (if_control, first_arg);
9975796c8dcSSimon Schubert 	}
998cf7f2e2dSJohn Marino       else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
9995796c8dcSSimon Schubert 	{
10005796c8dcSSimon Schubert 	  char *first_arg;
1001cf7f2e2dSJohn Marino 
10025796c8dcSSimon Schubert 	  first_arg = p + 8;
1003cf7f2e2dSJohn Marino 	  while (first_arg < p_end && isspace (*first_arg))
10045796c8dcSSimon Schubert 	    first_arg++;
10055796c8dcSSimon Schubert 	  *command = build_command_line (commands_control, first_arg);
10065796c8dcSSimon Schubert 	}
1007cf7f2e2dSJohn Marino       else if (p_end - p == 6 && !strncmp (p, "python", 6))
10085796c8dcSSimon Schubert 	{
10095796c8dcSSimon Schubert 	  /* Note that we ignore the inline "python command" form
10105796c8dcSSimon Schubert 	     here.  */
10115796c8dcSSimon Schubert 	  *command = build_command_line (python_control, "");
10125796c8dcSSimon Schubert 	}
1013cf7f2e2dSJohn Marino       else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
10145796c8dcSSimon Schubert 	{
10155796c8dcSSimon Schubert 	  *command = (struct command_line *)
10165796c8dcSSimon Schubert 	    xmalloc (sizeof (struct command_line));
10175796c8dcSSimon Schubert 	  (*command)->next = NULL;
10185796c8dcSSimon Schubert 	  (*command)->line = NULL;
10195796c8dcSSimon Schubert 	  (*command)->control_type = break_control;
10205796c8dcSSimon Schubert 	  (*command)->body_count = 0;
10215796c8dcSSimon Schubert 	  (*command)->body_list = NULL;
10225796c8dcSSimon Schubert 	}
1023cf7f2e2dSJohn Marino       else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
10245796c8dcSSimon Schubert 	{
10255796c8dcSSimon Schubert 	  *command = (struct command_line *)
10265796c8dcSSimon Schubert 	    xmalloc (sizeof (struct command_line));
10275796c8dcSSimon Schubert 	  (*command)->next = NULL;
10285796c8dcSSimon Schubert 	  (*command)->line = NULL;
10295796c8dcSSimon Schubert 	  (*command)->control_type = continue_control;
10305796c8dcSSimon Schubert 	  (*command)->body_count = 0;
10315796c8dcSSimon Schubert 	  (*command)->body_list = NULL;
10325796c8dcSSimon Schubert 	}
10335796c8dcSSimon Schubert       else
10345796c8dcSSimon Schubert 	not_handled = 1;
10355796c8dcSSimon Schubert     }
10365796c8dcSSimon Schubert 
10375796c8dcSSimon Schubert   if (!parse_commands || not_handled)
10385796c8dcSSimon Schubert     {
10395796c8dcSSimon Schubert       /* A normal command.  */
10405796c8dcSSimon Schubert       *command = (struct command_line *)
10415796c8dcSSimon Schubert 	xmalloc (sizeof (struct command_line));
10425796c8dcSSimon Schubert       (*command)->next = NULL;
1043cf7f2e2dSJohn Marino       (*command)->line = savestring (p, p_end - p);
10445796c8dcSSimon Schubert       (*command)->control_type = simple_control;
10455796c8dcSSimon Schubert       (*command)->body_count = 0;
10465796c8dcSSimon Schubert       (*command)->body_list = NULL;
10475796c8dcSSimon Schubert     }
10485796c8dcSSimon Schubert 
1049cf7f2e2dSJohn Marino   if (validator)
1050cf7f2e2dSJohn Marino     {
1051cf7f2e2dSJohn Marino       volatile struct gdb_exception ex;
1052cf7f2e2dSJohn Marino 
1053cf7f2e2dSJohn Marino       TRY_CATCH (ex, RETURN_MASK_ALL)
1054cf7f2e2dSJohn Marino 	{
1055cf7f2e2dSJohn Marino 	  validator ((*command)->line, closure);
1056cf7f2e2dSJohn Marino 	}
1057cf7f2e2dSJohn Marino       if (ex.reason < 0)
1058cf7f2e2dSJohn Marino 	{
1059cf7f2e2dSJohn Marino 	  xfree (*command);
1060cf7f2e2dSJohn Marino 	  throw_exception (ex);
1061cf7f2e2dSJohn Marino 	}
1062cf7f2e2dSJohn Marino     }
1063cf7f2e2dSJohn Marino 
10645796c8dcSSimon Schubert   /* Nothing special.  */
10655796c8dcSSimon Schubert   return ok_command;
10665796c8dcSSimon Schubert }
10675796c8dcSSimon Schubert 
1068c50c785cSJohn Marino /* Recursively read in the control structures and create a
1069c50c785cSJohn Marino    command_line structure from them.  Use read_next_line_func to
1070c50c785cSJohn Marino    obtain lines of the command.  */
10715796c8dcSSimon Schubert 
10725796c8dcSSimon Schubert static enum command_control_type
recurse_read_control_structure(char * (* read_next_line_func)(void),struct command_line * current_cmd,void (* validator)(char *,void *),void * closure)1073cf7f2e2dSJohn Marino recurse_read_control_structure (char * (*read_next_line_func) (void),
1074cf7f2e2dSJohn Marino 				struct command_line *current_cmd,
1075cf7f2e2dSJohn Marino 				void (*validator)(char *, void *),
1076cf7f2e2dSJohn Marino 				void *closure)
10775796c8dcSSimon Schubert {
10785796c8dcSSimon Schubert   int current_body, i;
10795796c8dcSSimon Schubert   enum misc_command_type val;
10805796c8dcSSimon Schubert   enum command_control_type ret;
10815796c8dcSSimon Schubert   struct command_line **body_ptr, *child_tail, *next;
10825796c8dcSSimon Schubert 
10835796c8dcSSimon Schubert   child_tail = NULL;
10845796c8dcSSimon Schubert   current_body = 1;
10855796c8dcSSimon Schubert 
10865796c8dcSSimon Schubert   /* Sanity checks.  */
10875796c8dcSSimon Schubert   if (current_cmd->control_type == simple_control)
10885796c8dcSSimon Schubert     error (_("Recursed on a simple control type."));
10895796c8dcSSimon Schubert 
10905796c8dcSSimon Schubert   if (current_body > current_cmd->body_count)
10915796c8dcSSimon Schubert     error (_("Allocated body is smaller than this command type needs."));
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert   /* Read lines from the input stream and build control structures.  */
10945796c8dcSSimon Schubert   while (1)
10955796c8dcSSimon Schubert     {
10965796c8dcSSimon Schubert       dont_repeat ();
10975796c8dcSSimon Schubert 
10985796c8dcSSimon Schubert       next = NULL;
10995796c8dcSSimon Schubert       val = process_next_line (read_next_line_func (), &next,
1100cf7f2e2dSJohn Marino 			       current_cmd->control_type != python_control,
1101cf7f2e2dSJohn Marino 			       validator, closure);
11025796c8dcSSimon Schubert 
11035796c8dcSSimon Schubert       /* Just skip blanks and comments.  */
11045796c8dcSSimon Schubert       if (val == nop_command)
11055796c8dcSSimon Schubert 	continue;
11065796c8dcSSimon Schubert 
11075796c8dcSSimon Schubert       if (val == end_command)
11085796c8dcSSimon Schubert 	{
11095796c8dcSSimon Schubert 	  if (current_cmd->control_type == while_control
1110cf7f2e2dSJohn Marino 	      || current_cmd->control_type == while_stepping_control
11115796c8dcSSimon Schubert 	      || current_cmd->control_type == if_control
11125796c8dcSSimon Schubert 	      || current_cmd->control_type == python_control
11135796c8dcSSimon Schubert 	      || current_cmd->control_type == commands_control)
11145796c8dcSSimon Schubert 	    {
11155796c8dcSSimon Schubert 	      /* Success reading an entire canned sequence of commands.  */
11165796c8dcSSimon Schubert 	      ret = simple_control;
11175796c8dcSSimon Schubert 	      break;
11185796c8dcSSimon Schubert 	    }
11195796c8dcSSimon Schubert 	  else
11205796c8dcSSimon Schubert 	    {
11215796c8dcSSimon Schubert 	      ret = invalid_control;
11225796c8dcSSimon Schubert 	      break;
11235796c8dcSSimon Schubert 	    }
11245796c8dcSSimon Schubert 	}
11255796c8dcSSimon Schubert 
11265796c8dcSSimon Schubert       /* Not the end of a control structure.  */
11275796c8dcSSimon Schubert       if (val == else_command)
11285796c8dcSSimon Schubert 	{
11295796c8dcSSimon Schubert 	  if (current_cmd->control_type == if_control
11305796c8dcSSimon Schubert 	      && current_body == 1)
11315796c8dcSSimon Schubert 	    {
11325796c8dcSSimon Schubert 	      realloc_body_list (current_cmd, 2);
11335796c8dcSSimon Schubert 	      current_body = 2;
11345796c8dcSSimon Schubert 	      child_tail = NULL;
11355796c8dcSSimon Schubert 	      continue;
11365796c8dcSSimon Schubert 	    }
11375796c8dcSSimon Schubert 	  else
11385796c8dcSSimon Schubert 	    {
11395796c8dcSSimon Schubert 	      ret = invalid_control;
11405796c8dcSSimon Schubert 	      break;
11415796c8dcSSimon Schubert 	    }
11425796c8dcSSimon Schubert 	}
11435796c8dcSSimon Schubert 
11445796c8dcSSimon Schubert       if (child_tail)
11455796c8dcSSimon Schubert 	{
11465796c8dcSSimon Schubert 	  child_tail->next = next;
11475796c8dcSSimon Schubert 	}
11485796c8dcSSimon Schubert       else
11495796c8dcSSimon Schubert 	{
11505796c8dcSSimon Schubert 	  body_ptr = current_cmd->body_list;
11515796c8dcSSimon Schubert 	  for (i = 1; i < current_body; i++)
11525796c8dcSSimon Schubert 	    body_ptr++;
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert 	  *body_ptr = next;
11555796c8dcSSimon Schubert 
11565796c8dcSSimon Schubert 	}
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert       child_tail = next;
11595796c8dcSSimon Schubert 
11605796c8dcSSimon Schubert       /* If the latest line is another control structure, then recurse
11615796c8dcSSimon Schubert          on it.  */
11625796c8dcSSimon Schubert       if (next->control_type == while_control
1163cf7f2e2dSJohn Marino 	  || next->control_type == while_stepping_control
11645796c8dcSSimon Schubert 	  || next->control_type == if_control
11655796c8dcSSimon Schubert 	  || next->control_type == python_control
11665796c8dcSSimon Schubert 	  || next->control_type == commands_control)
11675796c8dcSSimon Schubert 	{
11685796c8dcSSimon Schubert 	  control_level++;
1169cf7f2e2dSJohn Marino 	  ret = recurse_read_control_structure (read_next_line_func, next,
1170cf7f2e2dSJohn Marino 						validator, closure);
11715796c8dcSSimon Schubert 	  control_level--;
11725796c8dcSSimon Schubert 
11735796c8dcSSimon Schubert 	  if (ret != simple_control)
11745796c8dcSSimon Schubert 	    break;
11755796c8dcSSimon Schubert 	}
11765796c8dcSSimon Schubert     }
11775796c8dcSSimon Schubert 
11785796c8dcSSimon Schubert   dont_repeat ();
11795796c8dcSSimon Schubert 
11805796c8dcSSimon Schubert   return ret;
11815796c8dcSSimon Schubert }
11825796c8dcSSimon Schubert 
1183*ef5ccd6cSJohn Marino static void
restore_interp(void * arg)1184*ef5ccd6cSJohn Marino restore_interp (void *arg)
1185*ef5ccd6cSJohn Marino {
1186*ef5ccd6cSJohn Marino   interp_set_temp (interp_name ((struct interp *)arg));
1187*ef5ccd6cSJohn Marino }
1188*ef5ccd6cSJohn Marino 
11895796c8dcSSimon Schubert /* Read lines from the input stream and accumulate them in a chain of
11905796c8dcSSimon Schubert    struct command_line's, which is then returned.  For input from a
11915796c8dcSSimon Schubert    terminal, the special command "end" is used to mark the end of the
11925796c8dcSSimon Schubert    input, and is not included in the returned chain of commands.
11935796c8dcSSimon Schubert 
11945796c8dcSSimon Schubert    If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
11955796c8dcSSimon Schubert    is always stripped) in the line and attempt to recognize GDB control
11965796c8dcSSimon Schubert    commands.  Otherwise, only "end" is recognized.  */
11975796c8dcSSimon Schubert 
11985796c8dcSSimon Schubert #define END_MESSAGE "End with a line saying just \"end\"."
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert struct command_line *
read_command_lines(char * prompt_arg,int from_tty,int parse_commands,void (* validator)(char *,void *),void * closure)1201cf7f2e2dSJohn Marino read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1202cf7f2e2dSJohn Marino 		    void (*validator)(char *, void *), void *closure)
12035796c8dcSSimon Schubert {
12045796c8dcSSimon Schubert   struct command_line *head;
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert   if (from_tty && input_from_terminal_p ())
12075796c8dcSSimon Schubert     {
12085796c8dcSSimon Schubert       if (deprecated_readline_begin_hook)
12095796c8dcSSimon Schubert 	{
1210c50c785cSJohn Marino 	  /* Note - intentional to merge messages with no newline.  */
1211c50c785cSJohn Marino 	  (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg,
1212c50c785cSJohn Marino 					     END_MESSAGE);
12135796c8dcSSimon Schubert 	}
12145796c8dcSSimon Schubert       else
12155796c8dcSSimon Schubert 	{
12165796c8dcSSimon Schubert 	  printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
12175796c8dcSSimon Schubert 	  gdb_flush (gdb_stdout);
12185796c8dcSSimon Schubert 	}
12195796c8dcSSimon Schubert     }
12205796c8dcSSimon Schubert 
1221*ef5ccd6cSJohn Marino 
1222*ef5ccd6cSJohn Marino   /* Reading commands assumes the CLI behavior, so temporarily
1223*ef5ccd6cSJohn Marino      override the current interpreter with CLI.  */
1224*ef5ccd6cSJohn Marino   if (current_interp_named_p (INTERP_CONSOLE))
1225cf7f2e2dSJohn Marino     head = read_command_lines_1 (read_next_line, parse_commands,
1226cf7f2e2dSJohn Marino 				 validator, closure);
1227*ef5ccd6cSJohn Marino   else
1228*ef5ccd6cSJohn Marino     {
1229*ef5ccd6cSJohn Marino       struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
1230*ef5ccd6cSJohn Marino       struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
1231*ef5ccd6cSJohn Marino 
1232*ef5ccd6cSJohn Marino       head = read_command_lines_1 (read_next_line, parse_commands,
1233*ef5ccd6cSJohn Marino 				   validator, closure);
1234*ef5ccd6cSJohn Marino       do_cleanups (old_chain);
1235*ef5ccd6cSJohn Marino     }
12365796c8dcSSimon Schubert 
12375796c8dcSSimon Schubert   if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
12385796c8dcSSimon Schubert     {
12395796c8dcSSimon Schubert       (*deprecated_readline_end_hook) ();
12405796c8dcSSimon Schubert     }
12415796c8dcSSimon Schubert   return (head);
12425796c8dcSSimon Schubert }
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert /* Act the same way as read_command_lines, except that each new line is
12455796c8dcSSimon Schubert    obtained using READ_NEXT_LINE_FUNC.  */
12465796c8dcSSimon Schubert 
12475796c8dcSSimon Schubert struct command_line *
read_command_lines_1(char * (* read_next_line_func)(void),int parse_commands,void (* validator)(char *,void *),void * closure)1248cf7f2e2dSJohn Marino read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1249cf7f2e2dSJohn Marino 		      void (*validator)(char *, void *), void *closure)
12505796c8dcSSimon Schubert {
12515796c8dcSSimon Schubert   struct command_line *head, *tail, *next;
12525796c8dcSSimon Schubert   struct cleanup *old_chain;
12535796c8dcSSimon Schubert   enum command_control_type ret;
12545796c8dcSSimon Schubert   enum misc_command_type val;
12555796c8dcSSimon Schubert 
12565796c8dcSSimon Schubert   control_level = 0;
12575796c8dcSSimon Schubert   head = tail = NULL;
12585796c8dcSSimon Schubert   old_chain = NULL;
12595796c8dcSSimon Schubert 
12605796c8dcSSimon Schubert   while (1)
12615796c8dcSSimon Schubert     {
12625796c8dcSSimon Schubert       dont_repeat ();
1263cf7f2e2dSJohn Marino       val = process_next_line (read_next_line_func (), &next, parse_commands,
1264cf7f2e2dSJohn Marino 			       validator, closure);
12655796c8dcSSimon Schubert 
12665796c8dcSSimon Schubert       /* Ignore blank lines or comments.  */
12675796c8dcSSimon Schubert       if (val == nop_command)
12685796c8dcSSimon Schubert 	continue;
12695796c8dcSSimon Schubert 
12705796c8dcSSimon Schubert       if (val == end_command)
12715796c8dcSSimon Schubert 	{
12725796c8dcSSimon Schubert 	  ret = simple_control;
12735796c8dcSSimon Schubert 	  break;
12745796c8dcSSimon Schubert 	}
12755796c8dcSSimon Schubert 
12765796c8dcSSimon Schubert       if (val != ok_command)
12775796c8dcSSimon Schubert 	{
12785796c8dcSSimon Schubert 	  ret = invalid_control;
12795796c8dcSSimon Schubert 	  break;
12805796c8dcSSimon Schubert 	}
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert       if (next->control_type == while_control
12835796c8dcSSimon Schubert 	  || next->control_type == if_control
12845796c8dcSSimon Schubert 	  || next->control_type == python_control
1285cf7f2e2dSJohn Marino 	  || next->control_type == commands_control
1286cf7f2e2dSJohn Marino 	  || next->control_type == while_stepping_control)
12875796c8dcSSimon Schubert 	{
12885796c8dcSSimon Schubert 	  control_level++;
1289cf7f2e2dSJohn Marino 	  ret = recurse_read_control_structure (read_next_line_func, next,
1290cf7f2e2dSJohn Marino 						validator, closure);
12915796c8dcSSimon Schubert 	  control_level--;
12925796c8dcSSimon Schubert 
12935796c8dcSSimon Schubert 	  if (ret == invalid_control)
12945796c8dcSSimon Schubert 	    break;
12955796c8dcSSimon Schubert 	}
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert       if (tail)
12985796c8dcSSimon Schubert 	{
12995796c8dcSSimon Schubert 	  tail->next = next;
13005796c8dcSSimon Schubert 	}
13015796c8dcSSimon Schubert       else
13025796c8dcSSimon Schubert 	{
13035796c8dcSSimon Schubert 	  head = next;
13045796c8dcSSimon Schubert 	  old_chain = make_cleanup_free_command_lines (&head);
13055796c8dcSSimon Schubert 	}
13065796c8dcSSimon Schubert       tail = next;
13075796c8dcSSimon Schubert     }
13085796c8dcSSimon Schubert 
13095796c8dcSSimon Schubert   dont_repeat ();
13105796c8dcSSimon Schubert 
13115796c8dcSSimon Schubert   if (head)
13125796c8dcSSimon Schubert     {
13135796c8dcSSimon Schubert       if (ret != invalid_control)
13145796c8dcSSimon Schubert 	{
13155796c8dcSSimon Schubert 	  discard_cleanups (old_chain);
13165796c8dcSSimon Schubert 	}
13175796c8dcSSimon Schubert       else
13185796c8dcSSimon Schubert 	do_cleanups (old_chain);
13195796c8dcSSimon Schubert     }
13205796c8dcSSimon Schubert 
13215796c8dcSSimon Schubert   return head;
13225796c8dcSSimon Schubert }
13235796c8dcSSimon Schubert 
13245796c8dcSSimon Schubert /* Free a chain of struct command_line's.  */
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert void
free_command_lines(struct command_line ** lptr)13275796c8dcSSimon Schubert free_command_lines (struct command_line **lptr)
13285796c8dcSSimon Schubert {
13295796c8dcSSimon Schubert   struct command_line *l = *lptr;
13305796c8dcSSimon Schubert   struct command_line *next;
13315796c8dcSSimon Schubert   struct command_line **blist;
13325796c8dcSSimon Schubert   int i;
13335796c8dcSSimon Schubert 
13345796c8dcSSimon Schubert   while (l)
13355796c8dcSSimon Schubert     {
13365796c8dcSSimon Schubert       if (l->body_count > 0)
13375796c8dcSSimon Schubert 	{
13385796c8dcSSimon Schubert 	  blist = l->body_list;
13395796c8dcSSimon Schubert 	  for (i = 0; i < l->body_count; i++, blist++)
13405796c8dcSSimon Schubert 	    free_command_lines (blist);
13415796c8dcSSimon Schubert 	}
13425796c8dcSSimon Schubert       next = l->next;
13435796c8dcSSimon Schubert       xfree (l->line);
13445796c8dcSSimon Schubert       xfree (l);
13455796c8dcSSimon Schubert       l = next;
13465796c8dcSSimon Schubert     }
13475796c8dcSSimon Schubert   *lptr = NULL;
13485796c8dcSSimon Schubert }
13495796c8dcSSimon Schubert 
13505796c8dcSSimon Schubert static void
do_free_command_lines_cleanup(void * arg)13515796c8dcSSimon Schubert do_free_command_lines_cleanup (void *arg)
13525796c8dcSSimon Schubert {
13535796c8dcSSimon Schubert   free_command_lines (arg);
13545796c8dcSSimon Schubert }
13555796c8dcSSimon Schubert 
13565796c8dcSSimon Schubert struct cleanup *
make_cleanup_free_command_lines(struct command_line ** arg)13575796c8dcSSimon Schubert make_cleanup_free_command_lines (struct command_line **arg)
13585796c8dcSSimon Schubert {
13595796c8dcSSimon Schubert   return make_cleanup (do_free_command_lines_cleanup, arg);
13605796c8dcSSimon Schubert }
13615796c8dcSSimon Schubert 
13625796c8dcSSimon Schubert struct command_line *
copy_command_lines(struct command_line * cmds)13635796c8dcSSimon Schubert copy_command_lines (struct command_line *cmds)
13645796c8dcSSimon Schubert {
13655796c8dcSSimon Schubert   struct command_line *result = NULL;
13665796c8dcSSimon Schubert 
13675796c8dcSSimon Schubert   if (cmds)
13685796c8dcSSimon Schubert     {
13695796c8dcSSimon Schubert       result = (struct command_line *) xmalloc (sizeof (struct command_line));
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert       result->next = copy_command_lines (cmds->next);
13725796c8dcSSimon Schubert       result->line = xstrdup (cmds->line);
13735796c8dcSSimon Schubert       result->control_type = cmds->control_type;
13745796c8dcSSimon Schubert       result->body_count = cmds->body_count;
13755796c8dcSSimon Schubert       if (cmds->body_count > 0)
13765796c8dcSSimon Schubert         {
13775796c8dcSSimon Schubert           int i;
13785796c8dcSSimon Schubert 
13795796c8dcSSimon Schubert           result->body_list = (struct command_line **)
13805796c8dcSSimon Schubert             xmalloc (sizeof (struct command_line *) * cmds->body_count);
13815796c8dcSSimon Schubert 
13825796c8dcSSimon Schubert           for (i = 0; i < cmds->body_count; i++)
13835796c8dcSSimon Schubert             result->body_list[i] = copy_command_lines (cmds->body_list[i]);
13845796c8dcSSimon Schubert         }
13855796c8dcSSimon Schubert       else
13865796c8dcSSimon Schubert         result->body_list = NULL;
13875796c8dcSSimon Schubert     }
13885796c8dcSSimon Schubert 
13895796c8dcSSimon Schubert   return result;
13905796c8dcSSimon Schubert }
13915796c8dcSSimon Schubert 
13925796c8dcSSimon Schubert /* Validate that *COMNAME is a valid name for a command.  Return the
13935796c8dcSSimon Schubert    containing command list, in case it starts with a prefix command.
13945796c8dcSSimon Schubert    The prefix must already exist.  *COMNAME is advanced to point after
13955796c8dcSSimon Schubert    any prefix, and a NUL character overwrites the space after the
13965796c8dcSSimon Schubert    prefix.  */
13975796c8dcSSimon Schubert 
13985796c8dcSSimon Schubert static struct cmd_list_element **
validate_comname(char ** comname)13995796c8dcSSimon Schubert validate_comname (char **comname)
14005796c8dcSSimon Schubert {
14015796c8dcSSimon Schubert   struct cmd_list_element **list = &cmdlist;
14025796c8dcSSimon Schubert   char *p, *last_word;
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert   if (*comname == 0)
14055796c8dcSSimon Schubert     error_no_arg (_("name of command to define"));
14065796c8dcSSimon Schubert 
14075796c8dcSSimon Schubert   /* Find the last word of the argument.  */
14085796c8dcSSimon Schubert   p = *comname + strlen (*comname);
14095796c8dcSSimon Schubert   while (p > *comname && isspace (p[-1]))
14105796c8dcSSimon Schubert     p--;
14115796c8dcSSimon Schubert   while (p > *comname && !isspace (p[-1]))
14125796c8dcSSimon Schubert     p--;
14135796c8dcSSimon Schubert   last_word = p;
14145796c8dcSSimon Schubert 
14155796c8dcSSimon Schubert   /* Find the corresponding command list.  */
14165796c8dcSSimon Schubert   if (last_word != *comname)
14175796c8dcSSimon Schubert     {
14185796c8dcSSimon Schubert       struct cmd_list_element *c;
14195796c8dcSSimon Schubert       char saved_char, *tem = *comname;
14205796c8dcSSimon Schubert 
14215796c8dcSSimon Schubert       /* Separate the prefix and the command.  */
14225796c8dcSSimon Schubert       saved_char = last_word[-1];
14235796c8dcSSimon Schubert       last_word[-1] = '\0';
14245796c8dcSSimon Schubert 
14255796c8dcSSimon Schubert       c = lookup_cmd (&tem, cmdlist, "", 0, 1);
14265796c8dcSSimon Schubert       if (c->prefixlist == NULL)
14275796c8dcSSimon Schubert 	error (_("\"%s\" is not a prefix command."), *comname);
14285796c8dcSSimon Schubert 
14295796c8dcSSimon Schubert       list = c->prefixlist;
14305796c8dcSSimon Schubert       last_word[-1] = saved_char;
14315796c8dcSSimon Schubert       *comname = last_word;
14325796c8dcSSimon Schubert     }
14335796c8dcSSimon Schubert 
14345796c8dcSSimon Schubert   p = *comname;
14355796c8dcSSimon Schubert   while (*p)
14365796c8dcSSimon Schubert     {
14375796c8dcSSimon Schubert       if (!isalnum (*p) && *p != '-' && *p != '_')
14385796c8dcSSimon Schubert 	error (_("Junk in argument list: \"%s\""), p);
14395796c8dcSSimon Schubert       p++;
14405796c8dcSSimon Schubert     }
14415796c8dcSSimon Schubert 
14425796c8dcSSimon Schubert   return list;
14435796c8dcSSimon Schubert }
14445796c8dcSSimon Schubert 
14455796c8dcSSimon Schubert /* This is just a placeholder in the command data structures.  */
14465796c8dcSSimon Schubert static void
user_defined_command(char * ignore,int from_tty)14475796c8dcSSimon Schubert user_defined_command (char *ignore, int from_tty)
14485796c8dcSSimon Schubert {
14495796c8dcSSimon Schubert }
14505796c8dcSSimon Schubert 
1451*ef5ccd6cSJohn Marino static void
define_command(char * comname,int from_tty)14525796c8dcSSimon Schubert define_command (char *comname, int from_tty)
14535796c8dcSSimon Schubert {
14545796c8dcSSimon Schubert #define MAX_TMPBUF 128
14555796c8dcSSimon Schubert   enum cmd_hook_type
14565796c8dcSSimon Schubert     {
14575796c8dcSSimon Schubert       CMD_NO_HOOK = 0,
14585796c8dcSSimon Schubert       CMD_PRE_HOOK,
14595796c8dcSSimon Schubert       CMD_POST_HOOK
14605796c8dcSSimon Schubert     };
14615796c8dcSSimon Schubert   struct command_line *cmds;
1462cf7f2e2dSJohn Marino   struct cmd_list_element *c, *newc, *hookc = 0, **list;
1463cf7f2e2dSJohn Marino   char *tem, *comfull;
14645796c8dcSSimon Schubert   char tmpbuf[MAX_TMPBUF];
14655796c8dcSSimon Schubert   int  hook_type      = CMD_NO_HOOK;
14665796c8dcSSimon Schubert   int  hook_name_size = 0;
14675796c8dcSSimon Schubert 
14685796c8dcSSimon Schubert #define	HOOK_STRING	"hook-"
14695796c8dcSSimon Schubert #define	HOOK_LEN 5
14705796c8dcSSimon Schubert #define HOOK_POST_STRING "hookpost-"
14715796c8dcSSimon Schubert #define HOOK_POST_LEN    9
14725796c8dcSSimon Schubert 
14735796c8dcSSimon Schubert   comfull = comname;
14745796c8dcSSimon Schubert   list = validate_comname (&comname);
14755796c8dcSSimon Schubert 
14765796c8dcSSimon Schubert   /* Look it up, and verify that we got an exact match.  */
14775796c8dcSSimon Schubert   tem = comname;
14785796c8dcSSimon Schubert   c = lookup_cmd (&tem, *list, "", -1, 1);
14795796c8dcSSimon Schubert   if (c && strcmp (comname, c->name) != 0)
14805796c8dcSSimon Schubert     c = 0;
14815796c8dcSSimon Schubert 
14825796c8dcSSimon Schubert   if (c)
14835796c8dcSSimon Schubert     {
14845796c8dcSSimon Schubert       int q;
1485cf7f2e2dSJohn Marino 
14865796c8dcSSimon Schubert       if (c->class == class_user || c->class == class_alias)
14875796c8dcSSimon Schubert 	q = query (_("Redefine command \"%s\"? "), c->name);
14885796c8dcSSimon Schubert       else
14895796c8dcSSimon Schubert 	q = query (_("Really redefine built-in command \"%s\"? "), c->name);
14905796c8dcSSimon Schubert       if (!q)
14915796c8dcSSimon Schubert 	error (_("Command \"%s\" not redefined."), c->name);
14925796c8dcSSimon Schubert     }
14935796c8dcSSimon Schubert 
14945796c8dcSSimon Schubert   /* If this new command is a hook, then mark the command which it
14955796c8dcSSimon Schubert      is hooking.  Note that we allow hooking `help' commands, so that
14965796c8dcSSimon Schubert      we can hook the `stop' pseudo-command.  */
14975796c8dcSSimon Schubert 
14985796c8dcSSimon Schubert   if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
14995796c8dcSSimon Schubert     {
15005796c8dcSSimon Schubert        hook_type      = CMD_PRE_HOOK;
15015796c8dcSSimon Schubert        hook_name_size = HOOK_LEN;
15025796c8dcSSimon Schubert     }
15035796c8dcSSimon Schubert   else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
15045796c8dcSSimon Schubert     {
15055796c8dcSSimon Schubert       hook_type      = CMD_POST_HOOK;
15065796c8dcSSimon Schubert       hook_name_size = HOOK_POST_LEN;
15075796c8dcSSimon Schubert     }
15085796c8dcSSimon Schubert 
15095796c8dcSSimon Schubert   if (hook_type != CMD_NO_HOOK)
15105796c8dcSSimon Schubert     {
15115796c8dcSSimon Schubert       /* Look up cmd it hooks, and verify that we got an exact match.  */
15125796c8dcSSimon Schubert       tem = comname + hook_name_size;
15135796c8dcSSimon Schubert       hookc = lookup_cmd (&tem, *list, "", -1, 0);
15145796c8dcSSimon Schubert       if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
15155796c8dcSSimon Schubert 	hookc = 0;
15165796c8dcSSimon Schubert       if (!hookc)
15175796c8dcSSimon Schubert 	{
1518c50c785cSJohn Marino 	  warning (_("Your new `%s' command does not "
1519c50c785cSJohn Marino 		     "hook any existing command."),
15205796c8dcSSimon Schubert 		   comfull);
15215796c8dcSSimon Schubert 	  if (!query (_("Proceed? ")))
15225796c8dcSSimon Schubert 	    error (_("Not confirmed."));
15235796c8dcSSimon Schubert 	}
15245796c8dcSSimon Schubert     }
15255796c8dcSSimon Schubert 
15265796c8dcSSimon Schubert   comname = xstrdup (comname);
15275796c8dcSSimon Schubert 
15285796c8dcSSimon Schubert   /* If the rest of the commands will be case insensitive, this one
15295796c8dcSSimon Schubert      should behave in the same manner.  */
15305796c8dcSSimon Schubert   for (tem = comname; *tem; tem++)
15315796c8dcSSimon Schubert     if (isupper (*tem))
15325796c8dcSSimon Schubert       *tem = tolower (*tem);
15335796c8dcSSimon Schubert 
1534*ef5ccd6cSJohn Marino   xsnprintf (tmpbuf, sizeof (tmpbuf),
1535*ef5ccd6cSJohn Marino 	     "Type commands for definition of \"%s\".", comfull);
1536cf7f2e2dSJohn Marino   cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
15375796c8dcSSimon Schubert 
15385796c8dcSSimon Schubert   if (c && c->class == class_user)
15395796c8dcSSimon Schubert     free_command_lines (&c->user_commands);
15405796c8dcSSimon Schubert 
15415796c8dcSSimon Schubert   newc = add_cmd (comname, class_user, user_defined_command,
15425796c8dcSSimon Schubert 		  (c && c->class == class_user)
15435796c8dcSSimon Schubert 		  ? c->doc : xstrdup ("User-defined."), list);
15445796c8dcSSimon Schubert   newc->user_commands = cmds;
15455796c8dcSSimon Schubert 
15465796c8dcSSimon Schubert   /* If this new command is a hook, then mark both commands as being
15475796c8dcSSimon Schubert      tied.  */
15485796c8dcSSimon Schubert   if (hookc)
15495796c8dcSSimon Schubert     {
15505796c8dcSSimon Schubert       switch (hook_type)
15515796c8dcSSimon Schubert         {
15525796c8dcSSimon Schubert         case CMD_PRE_HOOK:
15535796c8dcSSimon Schubert           hookc->hook_pre  = newc;  /* Target gets hooked.  */
15545796c8dcSSimon Schubert           newc->hookee_pre = hookc; /* We are marked as hooking target cmd.  */
15555796c8dcSSimon Schubert           break;
15565796c8dcSSimon Schubert         case CMD_POST_HOOK:
15575796c8dcSSimon Schubert           hookc->hook_post  = newc;  /* Target gets hooked.  */
1558c50c785cSJohn Marino           newc->hookee_post = hookc; /* We are marked as hooking
1559c50c785cSJohn Marino 					target cmd.  */
15605796c8dcSSimon Schubert           break;
15615796c8dcSSimon Schubert         default:
15625796c8dcSSimon Schubert           /* Should never come here as hookc would be 0.  */
15635796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__, _("bad switch"));
15645796c8dcSSimon Schubert         }
15655796c8dcSSimon Schubert     }
15665796c8dcSSimon Schubert }
15675796c8dcSSimon Schubert 
1568*ef5ccd6cSJohn Marino static void
document_command(char * comname,int from_tty)15695796c8dcSSimon Schubert document_command (char *comname, int from_tty)
15705796c8dcSSimon Schubert {
15715796c8dcSSimon Schubert   struct command_line *doclines;
15725796c8dcSSimon Schubert   struct cmd_list_element *c, **list;
15735796c8dcSSimon Schubert   char *tem, *comfull;
15745796c8dcSSimon Schubert   char tmpbuf[128];
15755796c8dcSSimon Schubert 
15765796c8dcSSimon Schubert   comfull = comname;
15775796c8dcSSimon Schubert   list = validate_comname (&comname);
15785796c8dcSSimon Schubert 
15795796c8dcSSimon Schubert   tem = comname;
15805796c8dcSSimon Schubert   c = lookup_cmd (&tem, *list, "", 0, 1);
15815796c8dcSSimon Schubert 
15825796c8dcSSimon Schubert   if (c->class != class_user)
15835796c8dcSSimon Schubert     error (_("Command \"%s\" is built-in."), comfull);
15845796c8dcSSimon Schubert 
1585*ef5ccd6cSJohn Marino   xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
1586*ef5ccd6cSJohn Marino 	     comfull);
1587cf7f2e2dSJohn Marino   doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
15885796c8dcSSimon Schubert 
15895796c8dcSSimon Schubert   if (c->doc)
15905796c8dcSSimon Schubert     xfree (c->doc);
15915796c8dcSSimon Schubert 
15925796c8dcSSimon Schubert   {
15935796c8dcSSimon Schubert     struct command_line *cl1;
15945796c8dcSSimon Schubert     int len = 0;
15955796c8dcSSimon Schubert 
15965796c8dcSSimon Schubert     for (cl1 = doclines; cl1; cl1 = cl1->next)
15975796c8dcSSimon Schubert       len += strlen (cl1->line) + 1;
15985796c8dcSSimon Schubert 
15995796c8dcSSimon Schubert     c->doc = (char *) xmalloc (len + 1);
16005796c8dcSSimon Schubert     *c->doc = 0;
16015796c8dcSSimon Schubert 
16025796c8dcSSimon Schubert     for (cl1 = doclines; cl1; cl1 = cl1->next)
16035796c8dcSSimon Schubert       {
16045796c8dcSSimon Schubert 	strcat (c->doc, cl1->line);
16055796c8dcSSimon Schubert 	if (cl1->next)
16065796c8dcSSimon Schubert 	  strcat (c->doc, "\n");
16075796c8dcSSimon Schubert       }
16085796c8dcSSimon Schubert   }
16095796c8dcSSimon Schubert 
16105796c8dcSSimon Schubert   free_command_lines (&doclines);
16115796c8dcSSimon Schubert }
16125796c8dcSSimon Schubert 
16135796c8dcSSimon Schubert struct source_cleanup_lines_args
16145796c8dcSSimon Schubert {
16155796c8dcSSimon Schubert   int old_line;
1616cf7f2e2dSJohn Marino   const char *old_file;
16175796c8dcSSimon Schubert };
16185796c8dcSSimon Schubert 
16195796c8dcSSimon Schubert static void
source_cleanup_lines(void * args)16205796c8dcSSimon Schubert source_cleanup_lines (void *args)
16215796c8dcSSimon Schubert {
16225796c8dcSSimon Schubert   struct source_cleanup_lines_args *p =
16235796c8dcSSimon Schubert     (struct source_cleanup_lines_args *) args;
1624cf7f2e2dSJohn Marino 
16255796c8dcSSimon Schubert   source_line_number = p->old_line;
16265796c8dcSSimon Schubert   source_file_name = p->old_file;
16275796c8dcSSimon Schubert }
16285796c8dcSSimon Schubert 
1629c50c785cSJohn Marino /* Used to implement source_command.  */
16305796c8dcSSimon Schubert 
16315796c8dcSSimon Schubert void
script_from_file(FILE * stream,const char * file)1632cf7f2e2dSJohn Marino script_from_file (FILE *stream, const char *file)
16335796c8dcSSimon Schubert {
16345796c8dcSSimon Schubert   struct cleanup *old_cleanups;
16355796c8dcSSimon Schubert   struct source_cleanup_lines_args old_lines;
16365796c8dcSSimon Schubert 
16375796c8dcSSimon Schubert   if (stream == NULL)
16385796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
16395796c8dcSSimon Schubert 
16405796c8dcSSimon Schubert   old_lines.old_line = source_line_number;
16415796c8dcSSimon Schubert   old_lines.old_file = source_file_name;
1642*ef5ccd6cSJohn Marino   old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
16435796c8dcSSimon Schubert   source_line_number = 0;
16445796c8dcSSimon Schubert   source_file_name = file;
1645c50c785cSJohn Marino   /* This will get set every time we read a line.  So it won't stay ""
1646c50c785cSJohn Marino      for long.  */
16475796c8dcSSimon Schubert   error_pre_print = "";
16485796c8dcSSimon Schubert 
16495796c8dcSSimon Schubert   {
1650a45ae5f8SJohn Marino     volatile struct gdb_exception e;
1651cf7f2e2dSJohn Marino 
1652a45ae5f8SJohn Marino     TRY_CATCH (e, RETURN_MASK_ERROR)
1653a45ae5f8SJohn Marino       {
1654a45ae5f8SJohn Marino 	read_command_file (stream);
1655a45ae5f8SJohn Marino       }
16565796c8dcSSimon Schubert     switch (e.reason)
16575796c8dcSSimon Schubert       {
16585796c8dcSSimon Schubert       case 0:
16595796c8dcSSimon Schubert 	break;
16605796c8dcSSimon Schubert       case RETURN_ERROR:
16615796c8dcSSimon Schubert 	/* Re-throw the error, but with the file name information
16625796c8dcSSimon Schubert 	   prepended.  */
16635796c8dcSSimon Schubert 	throw_error (e.error,
16645796c8dcSSimon Schubert 		     _("%s:%d: Error in sourced command file:\n%s"),
16655796c8dcSSimon Schubert 		     source_file_name, source_line_number, e.message);
16665796c8dcSSimon Schubert       default:
16675796c8dcSSimon Schubert 	internal_error (__FILE__, __LINE__, _("bad reason"));
16685796c8dcSSimon Schubert       }
16695796c8dcSSimon Schubert   }
16705796c8dcSSimon Schubert 
16715796c8dcSSimon Schubert   do_cleanups (old_cleanups);
16725796c8dcSSimon Schubert }
16735796c8dcSSimon Schubert 
16745796c8dcSSimon Schubert /* Print the definition of user command C to STREAM.  Or, if C is a
16755796c8dcSSimon Schubert    prefix command, show the definitions of all user commands under C
16765796c8dcSSimon Schubert    (recursively).  PREFIX and NAME combined are the name of the
16775796c8dcSSimon Schubert    current command.  */
16785796c8dcSSimon Schubert void
show_user_1(struct cmd_list_element * c,char * prefix,char * name,struct ui_file * stream)16795796c8dcSSimon Schubert show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
16805796c8dcSSimon Schubert 	     struct ui_file *stream)
16815796c8dcSSimon Schubert {
16825796c8dcSSimon Schubert   struct command_line *cmdlines;
16835796c8dcSSimon Schubert 
16845796c8dcSSimon Schubert   if (c->prefixlist != NULL)
16855796c8dcSSimon Schubert     {
16865796c8dcSSimon Schubert       char *prefixname = c->prefixname;
1687cf7f2e2dSJohn Marino 
16885796c8dcSSimon Schubert       for (c = *c->prefixlist; c != NULL; c = c->next)
16895796c8dcSSimon Schubert 	if (c->class == class_user || c->prefixlist != NULL)
16905796c8dcSSimon Schubert 	  show_user_1 (c, prefixname, c->name, gdb_stdout);
16915796c8dcSSimon Schubert       return;
16925796c8dcSSimon Schubert     }
16935796c8dcSSimon Schubert 
16945796c8dcSSimon Schubert   cmdlines = c->user_commands;
16955796c8dcSSimon Schubert   if (!cmdlines)
16965796c8dcSSimon Schubert     return;
16975796c8dcSSimon Schubert   fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
16985796c8dcSSimon Schubert 
1699a45ae5f8SJohn Marino   print_command_lines (current_uiout, cmdlines, 1);
17005796c8dcSSimon Schubert   fputs_filtered ("\n", stream);
17015796c8dcSSimon Schubert }
17025796c8dcSSimon Schubert 
1703*ef5ccd6cSJohn Marino 
1704*ef5ccd6cSJohn Marino 
1705*ef5ccd6cSJohn Marino initialize_file_ftype _initialize_cli_script;
1706*ef5ccd6cSJohn Marino 
1707*ef5ccd6cSJohn Marino void
_initialize_cli_script(void)1708*ef5ccd6cSJohn Marino _initialize_cli_script (void)
1709*ef5ccd6cSJohn Marino {
1710*ef5ccd6cSJohn Marino   add_com ("document", class_support, document_command, _("\
1711*ef5ccd6cSJohn Marino Document a user-defined command.\n\
1712*ef5ccd6cSJohn Marino Give command name as argument.  Give documentation on following lines.\n\
1713*ef5ccd6cSJohn Marino End with a line of just \"end\"."));
1714*ef5ccd6cSJohn Marino   add_com ("define", class_support, define_command, _("\
1715*ef5ccd6cSJohn Marino Define a new command name.  Command name is argument.\n\
1716*ef5ccd6cSJohn Marino Definition appears on following lines, one command per line.\n\
1717*ef5ccd6cSJohn Marino End with a line of just \"end\".\n\
1718*ef5ccd6cSJohn Marino Use the \"document\" command to give documentation for the new command.\n\
1719*ef5ccd6cSJohn Marino Commands defined in this way may have up to ten arguments."));
1720*ef5ccd6cSJohn Marino 
1721*ef5ccd6cSJohn Marino   add_com ("while", class_support, while_command, _("\
1722*ef5ccd6cSJohn Marino Execute nested commands WHILE the conditional expression is non zero.\n\
1723*ef5ccd6cSJohn Marino The conditional expression must follow the word `while' and must in turn be\n\
1724*ef5ccd6cSJohn Marino followed by a new line.  The nested commands must be entered one per line,\n\
1725*ef5ccd6cSJohn Marino and should be terminated by the word `end'."));
1726*ef5ccd6cSJohn Marino 
1727*ef5ccd6cSJohn Marino   add_com ("if", class_support, if_command, _("\
1728*ef5ccd6cSJohn Marino Execute nested commands once IF the conditional expression is non zero.\n\
1729*ef5ccd6cSJohn Marino The conditional expression must follow the word `if' and must in turn be\n\
1730*ef5ccd6cSJohn Marino followed by a new line.  The nested commands must be entered one per line,\n\
1731*ef5ccd6cSJohn Marino and should be terminated by the word 'else' or `end'.  If an else clause\n\
1732*ef5ccd6cSJohn Marino is used, the same rules apply to its nested commands as to the first ones."));
1733*ef5ccd6cSJohn Marino }
1734