Home | History | Annotate | Line # | Download | only in cli
cli-script.c revision 1.1
      1  1.1  christos /* GDB CLI command scripting.
      2  1.1  christos 
      3  1.1  christos    Copyright (C) 1986-2014 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "value.h"
     22  1.1  christos #include "language.h"		/* For value_true */
     23  1.1  christos #include <ctype.h>
     24  1.1  christos 
     25  1.1  christos #include "ui-out.h"
     26  1.1  christos #include <string.h>
     27  1.1  christos #include "exceptions.h"
     28  1.1  christos #include "top.h"
     29  1.1  christos #include "breakpoint.h"
     30  1.1  christos #include "cli/cli-cmds.h"
     31  1.1  christos #include "cli/cli-decode.h"
     32  1.1  christos #include "cli/cli-script.h"
     33  1.1  christos #include "gdb_assert.h"
     34  1.1  christos 
     35  1.1  christos #include "python/python.h"
     36  1.1  christos #include "interps.h"
     37  1.1  christos 
     38  1.1  christos /* Prototypes for local functions.  */
     39  1.1  christos 
     40  1.1  christos static enum command_control_type
     41  1.1  christos recurse_read_control_structure (char * (*read_next_line_func) (void),
     42  1.1  christos 				struct command_line *current_cmd,
     43  1.1  christos 				void (*validator)(char *, void *),
     44  1.1  christos 				void *closure);
     45  1.1  christos 
     46  1.1  christos static char *insert_args (char *line);
     47  1.1  christos 
     48  1.1  christos static struct cleanup * setup_user_args (char *p);
     49  1.1  christos 
     50  1.1  christos static char *read_next_line (void);
     51  1.1  christos 
     52  1.1  christos /* Level of control structure when reading.  */
     53  1.1  christos static int control_level;
     54  1.1  christos 
     55  1.1  christos /* Level of control structure when executing.  */
     56  1.1  christos static int command_nest_depth = 1;
     57  1.1  christos 
     58  1.1  christos /* This is to prevent certain commands being printed twice.  */
     59  1.1  christos static int suppress_next_print_command_trace = 0;
     60  1.1  christos 
     61  1.1  christos /* Structure for arguments to user defined functions.  */
     62  1.1  christos #define MAXUSERARGS 10
     63  1.1  christos struct user_args
     64  1.1  christos   {
     65  1.1  christos     struct user_args *next;
     66  1.1  christos     /* It is necessary to store a malloced copy of the command line to
     67  1.1  christos        ensure that the arguments are not overwritten before they are
     68  1.1  christos        used.  */
     69  1.1  christos     char *command;
     70  1.1  christos     struct
     71  1.1  christos       {
     72  1.1  christos 	char *arg;
     73  1.1  christos 	int len;
     74  1.1  christos       }
     75  1.1  christos     a[MAXUSERARGS];
     76  1.1  christos     int count;
     77  1.1  christos   }
     78  1.1  christos  *user_args;
     79  1.1  christos 
     80  1.1  christos 
     81  1.1  christos /* Return non-zero if TYPE is a multi-line command (i.e., is terminated
     83  1.1  christos    by "end").  */
     84  1.1  christos 
     85  1.1  christos static int
     86  1.1  christos multi_line_command_p (enum command_control_type type)
     87  1.1  christos {
     88  1.1  christos   switch (type)
     89  1.1  christos     {
     90  1.1  christos     case if_control:
     91  1.1  christos     case while_control:
     92  1.1  christos     case while_stepping_control:
     93  1.1  christos     case commands_control:
     94  1.1  christos     case python_control:
     95  1.1  christos       return 1;
     96  1.1  christos     default:
     97  1.1  christos       return 0;
     98  1.1  christos     }
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos /* Allocate, initialize a new command line structure for one of the
    102  1.1  christos    control commands (if/while).  */
    103  1.1  christos 
    104  1.1  christos static struct command_line *
    105  1.1  christos build_command_line (enum command_control_type type, char *args)
    106  1.1  christos {
    107  1.1  christos   struct command_line *cmd;
    108  1.1  christos 
    109  1.1  christos   if (args == NULL && (type == if_control || type == while_control))
    110  1.1  christos     error (_("if/while commands require arguments."));
    111  1.1  christos   gdb_assert (args != NULL);
    112  1.1  christos 
    113  1.1  christos   cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
    114  1.1  christos   cmd->next = NULL;
    115  1.1  christos   cmd->control_type = type;
    116  1.1  christos 
    117  1.1  christos   cmd->body_count = 1;
    118  1.1  christos   cmd->body_list
    119  1.1  christos     = (struct command_line **) xmalloc (sizeof (struct command_line *)
    120  1.1  christos 					* cmd->body_count);
    121  1.1  christos   memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
    122  1.1  christos   cmd->line = xstrdup (args);
    123  1.1  christos 
    124  1.1  christos   return cmd;
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos /* Build and return a new command structure for the control commands
    128  1.1  christos    such as "if" and "while".  */
    129  1.1  christos 
    130  1.1  christos struct command_line *
    131  1.1  christos get_command_line (enum command_control_type type, char *arg)
    132  1.1  christos {
    133  1.1  christos   struct command_line *cmd;
    134  1.1  christos   struct cleanup *old_chain = NULL;
    135  1.1  christos 
    136  1.1  christos   /* Allocate and build a new command line structure.  */
    137  1.1  christos   cmd = build_command_line (type, arg);
    138  1.1  christos 
    139  1.1  christos   old_chain = make_cleanup_free_command_lines (&cmd);
    140  1.1  christos 
    141  1.1  christos   /* Read in the body of this command.  */
    142  1.1  christos   if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
    143  1.1  christos       == invalid_control)
    144  1.1  christos     {
    145  1.1  christos       warning (_("Error reading in canned sequence of commands."));
    146  1.1  christos       do_cleanups (old_chain);
    147  1.1  christos       return NULL;
    148  1.1  christos     }
    149  1.1  christos 
    150  1.1  christos   discard_cleanups (old_chain);
    151  1.1  christos   return cmd;
    152  1.1  christos }
    153  1.1  christos 
    154  1.1  christos /* Recursively print a command (including full control structures).  */
    155  1.1  christos 
    156  1.1  christos void
    157  1.1  christos print_command_lines (struct ui_out *uiout, struct command_line *cmd,
    158  1.1  christos 		     unsigned int depth)
    159  1.1  christos {
    160  1.1  christos   struct command_line *list;
    161  1.1  christos 
    162  1.1  christos   list = cmd;
    163  1.1  christos   while (list)
    164  1.1  christos     {
    165  1.1  christos       if (depth)
    166  1.1  christos 	ui_out_spaces (uiout, 2 * depth);
    167  1.1  christos 
    168  1.1  christos       /* A simple command, print it and continue.  */
    169  1.1  christos       if (list->control_type == simple_control)
    170  1.1  christos 	{
    171  1.1  christos 	  ui_out_field_string (uiout, NULL, list->line);
    172  1.1  christos 	  ui_out_text (uiout, "\n");
    173  1.1  christos 	  list = list->next;
    174  1.1  christos 	  continue;
    175  1.1  christos 	}
    176  1.1  christos 
    177  1.1  christos       /* loop_continue to jump to the start of a while loop, print it
    178  1.1  christos          and continue. */
    179  1.1  christos       if (list->control_type == continue_control)
    180  1.1  christos 	{
    181  1.1  christos 	  ui_out_field_string (uiout, NULL, "loop_continue");
    182  1.1  christos 	  ui_out_text (uiout, "\n");
    183  1.1  christos 	  list = list->next;
    184  1.1  christos 	  continue;
    185  1.1  christos 	}
    186  1.1  christos 
    187  1.1  christos       /* loop_break to break out of a while loop, print it and
    188  1.1  christos 	 continue.  */
    189  1.1  christos       if (list->control_type == break_control)
    190  1.1  christos 	{
    191  1.1  christos 	  ui_out_field_string (uiout, NULL, "loop_break");
    192  1.1  christos 	  ui_out_text (uiout, "\n");
    193  1.1  christos 	  list = list->next;
    194  1.1  christos 	  continue;
    195  1.1  christos 	}
    196  1.1  christos 
    197  1.1  christos       /* A while command.  Recursively print its subcommands and
    198  1.1  christos 	 continue.  */
    199  1.1  christos       if (list->control_type == while_control
    200  1.1  christos 	  || list->control_type == while_stepping_control)
    201  1.1  christos 	{
    202  1.1  christos 	  /* For while-stepping, the line includes the 'while-stepping'
    203  1.1  christos 	     token.  See comment in process_next_line for explanation.
    204  1.1  christos 	     Here, take care not print 'while-stepping' twice.  */
    205  1.1  christos 	  if (list->control_type == while_control)
    206  1.1  christos 	    ui_out_field_fmt (uiout, NULL, "while %s", list->line);
    207  1.1  christos 	  else
    208  1.1  christos 	    ui_out_field_string (uiout, NULL, list->line);
    209  1.1  christos 	  ui_out_text (uiout, "\n");
    210  1.1  christos 	  print_command_lines (uiout, *list->body_list, depth + 1);
    211  1.1  christos 	  if (depth)
    212  1.1  christos 	    ui_out_spaces (uiout, 2 * depth);
    213  1.1  christos 	  ui_out_field_string (uiout, NULL, "end");
    214  1.1  christos 	  ui_out_text (uiout, "\n");
    215  1.1  christos 	  list = list->next;
    216  1.1  christos 	  continue;
    217  1.1  christos 	}
    218  1.1  christos 
    219  1.1  christos       /* An if command.  Recursively print both arms before
    220  1.1  christos 	 continueing.  */
    221  1.1  christos       if (list->control_type == if_control)
    222  1.1  christos 	{
    223  1.1  christos 	  ui_out_field_fmt (uiout, NULL, "if %s", list->line);
    224  1.1  christos 	  ui_out_text (uiout, "\n");
    225  1.1  christos 	  /* The true arm.  */
    226  1.1  christos 	  print_command_lines (uiout, list->body_list[0], depth + 1);
    227  1.1  christos 
    228  1.1  christos 	  /* Show the false arm if it exists.  */
    229  1.1  christos 	  if (list->body_count == 2)
    230  1.1  christos 	    {
    231  1.1  christos 	      if (depth)
    232  1.1  christos 		ui_out_spaces (uiout, 2 * depth);
    233  1.1  christos 	      ui_out_field_string (uiout, NULL, "else");
    234  1.1  christos 	      ui_out_text (uiout, "\n");
    235  1.1  christos 	      print_command_lines (uiout, list->body_list[1], depth + 1);
    236  1.1  christos 	    }
    237  1.1  christos 
    238  1.1  christos 	  if (depth)
    239  1.1  christos 	    ui_out_spaces (uiout, 2 * depth);
    240  1.1  christos 	  ui_out_field_string (uiout, NULL, "end");
    241  1.1  christos 	  ui_out_text (uiout, "\n");
    242  1.1  christos 	  list = list->next;
    243  1.1  christos 	  continue;
    244  1.1  christos 	}
    245  1.1  christos 
    246  1.1  christos       /* A commands command.  Print the breakpoint commands and
    247  1.1  christos 	 continue.  */
    248  1.1  christos       if (list->control_type == commands_control)
    249  1.1  christos 	{
    250  1.1  christos 	  if (*(list->line))
    251  1.1  christos 	    ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
    252  1.1  christos 	  else
    253  1.1  christos 	    ui_out_field_string (uiout, NULL, "commands");
    254  1.1  christos 	  ui_out_text (uiout, "\n");
    255  1.1  christos 	  print_command_lines (uiout, *list->body_list, depth + 1);
    256  1.1  christos 	  if (depth)
    257  1.1  christos 	    ui_out_spaces (uiout, 2 * depth);
    258  1.1  christos 	  ui_out_field_string (uiout, NULL, "end");
    259  1.1  christos 	  ui_out_text (uiout, "\n");
    260  1.1  christos 	  list = list->next;
    261  1.1  christos 	  continue;
    262  1.1  christos 	}
    263  1.1  christos 
    264  1.1  christos       if (list->control_type == python_control)
    265  1.1  christos 	{
    266  1.1  christos 	  ui_out_field_string (uiout, NULL, "python");
    267  1.1  christos 	  ui_out_text (uiout, "\n");
    268  1.1  christos 	  /* Don't indent python code at all.  */
    269  1.1  christos 	  print_command_lines (uiout, *list->body_list, 0);
    270  1.1  christos 	  if (depth)
    271  1.1  christos 	    ui_out_spaces (uiout, 2 * depth);
    272  1.1  christos 	  ui_out_field_string (uiout, NULL, "end");
    273  1.1  christos 	  ui_out_text (uiout, "\n");
    274  1.1  christos 	  list = list->next;
    275  1.1  christos 	  continue;
    276  1.1  christos 	}
    277  1.1  christos 
    278  1.1  christos       /* Ignore illegal command type and try next.  */
    279  1.1  christos       list = list->next;
    280  1.1  christos     }				/* while (list) */
    281  1.1  christos }
    282  1.1  christos 
    283  1.1  christos /* Handle pre-post hooks.  */
    284  1.1  christos 
    285  1.1  christos static void
    286  1.1  christos clear_hook_in_cleanup (void *data)
    287  1.1  christos {
    288  1.1  christos   struct cmd_list_element *c = data;
    289  1.1  christos 
    290  1.1  christos   c->hook_in = 0; /* Allow hook to work again once it is complete.  */
    291  1.1  christos }
    292  1.1  christos 
    293  1.1  christos void
    294  1.1  christos execute_cmd_pre_hook (struct cmd_list_element *c)
    295  1.1  christos {
    296  1.1  christos   if ((c->hook_pre) && (!c->hook_in))
    297  1.1  christos     {
    298  1.1  christos       struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
    299  1.1  christos       c->hook_in = 1; /* Prevent recursive hooking.  */
    300  1.1  christos       execute_user_command (c->hook_pre, (char *) 0);
    301  1.1  christos       do_cleanups (cleanups);
    302  1.1  christos     }
    303  1.1  christos }
    304  1.1  christos 
    305  1.1  christos void
    306  1.1  christos execute_cmd_post_hook (struct cmd_list_element *c)
    307  1.1  christos {
    308  1.1  christos   if ((c->hook_post) && (!c->hook_in))
    309  1.1  christos     {
    310  1.1  christos       struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
    311  1.1  christos 
    312  1.1  christos       c->hook_in = 1; /* Prevent recursive hooking.  */
    313  1.1  christos       execute_user_command (c->hook_post, (char *) 0);
    314  1.1  christos       do_cleanups (cleanups);
    315  1.1  christos     }
    316  1.1  christos }
    317  1.1  christos 
    318  1.1  christos /* Execute the command in CMD.  */
    319  1.1  christos static void
    320  1.1  christos do_restore_user_call_depth (void * call_depth)
    321  1.1  christos {
    322  1.1  christos   int *depth = call_depth;
    323  1.1  christos 
    324  1.1  christos   (*depth)--;
    325  1.1  christos   if ((*depth) == 0)
    326  1.1  christos     in_user_command = 0;
    327  1.1  christos }
    328  1.1  christos 
    329  1.1  christos 
    330  1.1  christos void
    331  1.1  christos execute_user_command (struct cmd_list_element *c, char *args)
    332  1.1  christos {
    333  1.1  christos   struct command_line *cmdlines;
    334  1.1  christos   struct cleanup *old_chain;
    335  1.1  christos   enum command_control_type ret;
    336  1.1  christos   static int user_call_depth = 0;
    337  1.1  christos   extern unsigned int max_user_call_depth;
    338  1.1  christos 
    339  1.1  christos   cmdlines = c->user_commands;
    340  1.1  christos   if (cmdlines == 0)
    341  1.1  christos     /* Null command */
    342  1.1  christos     return;
    343  1.1  christos 
    344  1.1  christos   old_chain = setup_user_args (args);
    345  1.1  christos 
    346  1.1  christos   if (++user_call_depth > max_user_call_depth)
    347  1.1  christos     error (_("Max user call depth exceeded -- command aborted."));
    348  1.1  christos 
    349  1.1  christos   make_cleanup (do_restore_user_call_depth, &user_call_depth);
    350  1.1  christos 
    351  1.1  christos   /* Set the instream to 0, indicating execution of a
    352  1.1  christos      user-defined function.  */
    353  1.1  christos   make_cleanup (do_restore_instream_cleanup, instream);
    354  1.1  christos   instream = (FILE *) 0;
    355  1.1  christos 
    356  1.1  christos   /* Also set the global in_user_command, so that NULL instream is
    357  1.1  christos      not confused with Insight.  */
    358  1.1  christos   in_user_command = 1;
    359  1.1  christos 
    360  1.1  christos   make_cleanup_restore_integer (&interpreter_async);
    361  1.1  christos   interpreter_async = 0;
    362  1.1  christos 
    363  1.1  christos   command_nest_depth++;
    364  1.1  christos   while (cmdlines)
    365  1.1  christos     {
    366  1.1  christos       ret = execute_control_command (cmdlines);
    367  1.1  christos       if (ret != simple_control && ret != break_control)
    368  1.1  christos 	{
    369  1.1  christos 	  warning (_("Error executing canned sequence of commands."));
    370  1.1  christos 	  break;
    371  1.1  christos 	}
    372  1.1  christos       cmdlines = cmdlines->next;
    373  1.1  christos     }
    374  1.1  christos   command_nest_depth--;
    375  1.1  christos   do_cleanups (old_chain);
    376  1.1  christos }
    377  1.1  christos 
    378  1.1  christos /* This function is called every time GDB prints a prompt.  It ensures
    379  1.1  christos    that errors and the like do not confuse the command tracing.  */
    380  1.1  christos 
    381  1.1  christos void
    382  1.1  christos reset_command_nest_depth (void)
    383  1.1  christos {
    384  1.1  christos   command_nest_depth = 1;
    385  1.1  christos 
    386  1.1  christos   /* Just in case.  */
    387  1.1  christos   suppress_next_print_command_trace = 0;
    388  1.1  christos }
    389  1.1  christos 
    390  1.1  christos /* Print the command, prefixed with '+' to represent the call depth.
    391  1.1  christos    This is slightly complicated because this function may be called
    392  1.1  christos    from execute_command and execute_control_command.  Unfortunately
    393  1.1  christos    execute_command also prints the top level control commands.
    394  1.1  christos    In these cases execute_command will call execute_control_command
    395  1.1  christos    via while_command or if_command.  Inner levels of 'if' and 'while'
    396  1.1  christos    are dealt with directly.  Therefore we can use these functions
    397  1.1  christos    to determine whether the command has been printed already or not.  */
    398  1.1  christos void
    399  1.1  christos print_command_trace (const char *cmd)
    400  1.1  christos {
    401  1.1  christos   int i;
    402  1.1  christos 
    403  1.1  christos   if (suppress_next_print_command_trace)
    404  1.1  christos     {
    405  1.1  christos       suppress_next_print_command_trace = 0;
    406  1.1  christos       return;
    407  1.1  christos     }
    408  1.1  christos 
    409  1.1  christos   if (!source_verbose && !trace_commands)
    410  1.1  christos     return;
    411  1.1  christos 
    412  1.1  christos   for (i=0; i < command_nest_depth; i++)
    413  1.1  christos     printf_filtered ("+");
    414  1.1  christos 
    415  1.1  christos   printf_filtered ("%s\n", cmd);
    416  1.1  christos }
    417  1.1  christos 
    418  1.1  christos enum command_control_type
    419  1.1  christos execute_control_command (struct command_line *cmd)
    420  1.1  christos {
    421  1.1  christos   struct expression *expr;
    422  1.1  christos   struct command_line *current;
    423  1.1  christos   struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
    424  1.1  christos   struct value *val;
    425  1.1  christos   struct value *val_mark;
    426  1.1  christos   int loop;
    427  1.1  christos   enum command_control_type ret;
    428  1.1  christos   char *new_line;
    429  1.1  christos 
    430  1.1  christos   /* Start by assuming failure, if a problem is detected, the code
    431  1.1  christos      below will simply "break" out of the switch.  */
    432  1.1  christos   ret = invalid_control;
    433  1.1  christos 
    434  1.1  christos   switch (cmd->control_type)
    435  1.1  christos     {
    436  1.1  christos     case simple_control:
    437  1.1  christos       /* A simple command, execute it and return.  */
    438  1.1  christos       new_line = insert_args (cmd->line);
    439  1.1  christos       if (!new_line)
    440  1.1  christos 	break;
    441  1.1  christos       make_cleanup (free_current_contents, &new_line);
    442  1.1  christos       execute_command (new_line, 0);
    443  1.1  christos       ret = cmd->control_type;
    444  1.1  christos       break;
    445  1.1  christos 
    446  1.1  christos     case continue_control:
    447  1.1  christos       print_command_trace ("loop_continue");
    448  1.1  christos 
    449  1.1  christos       /* Return for "continue", and "break" so we can either
    450  1.1  christos          continue the loop at the top, or break out.  */
    451  1.1  christos       ret = cmd->control_type;
    452  1.1  christos       break;
    453  1.1  christos 
    454  1.1  christos     case break_control:
    455  1.1  christos       print_command_trace ("loop_break");
    456  1.1  christos 
    457  1.1  christos       /* Return for "continue", and "break" so we can either
    458  1.1  christos          continue the loop at the top, or break out.  */
    459  1.1  christos       ret = cmd->control_type;
    460  1.1  christos       break;
    461  1.1  christos 
    462  1.1  christos     case while_control:
    463  1.1  christos       {
    464  1.1  christos 	int len = strlen (cmd->line) + 7;
    465  1.1  christos 	char *buffer = alloca (len);
    466  1.1  christos 
    467  1.1  christos 	xsnprintf (buffer, len, "while %s", cmd->line);
    468  1.1  christos 	print_command_trace (buffer);
    469  1.1  christos 
    470  1.1  christos 	/* Parse the loop control expression for the while statement.  */
    471  1.1  christos 	new_line = insert_args (cmd->line);
    472  1.1  christos 	if (!new_line)
    473  1.1  christos 	  break;
    474  1.1  christos 	make_cleanup (free_current_contents, &new_line);
    475  1.1  christos 	expr = parse_expression (new_line);
    476  1.1  christos 	make_cleanup (free_current_contents, &expr);
    477  1.1  christos 
    478  1.1  christos 	ret = simple_control;
    479  1.1  christos 	loop = 1;
    480  1.1  christos 
    481  1.1  christos 	/* Keep iterating so long as the expression is true.  */
    482  1.1  christos 	while (loop == 1)
    483  1.1  christos 	  {
    484  1.1  christos 	    int cond_result;
    485  1.1  christos 
    486  1.1  christos 	    QUIT;
    487  1.1  christos 
    488  1.1  christos 	    /* Evaluate the expression.  */
    489  1.1  christos 	    val_mark = value_mark ();
    490  1.1  christos 	    val = evaluate_expression (expr);
    491  1.1  christos 	    cond_result = value_true (val);
    492  1.1  christos 	    value_free_to_mark (val_mark);
    493  1.1  christos 
    494  1.1  christos 	    /* If the value is false, then break out of the loop.  */
    495  1.1  christos 	    if (!cond_result)
    496  1.1  christos 	      break;
    497  1.1  christos 
    498  1.1  christos 	    /* Execute the body of the while statement.  */
    499  1.1  christos 	    current = *cmd->body_list;
    500  1.1  christos 	    while (current)
    501  1.1  christos 	      {
    502  1.1  christos 		command_nest_depth++;
    503  1.1  christos 		ret = execute_control_command (current);
    504  1.1  christos 		command_nest_depth--;
    505  1.1  christos 
    506  1.1  christos 		/* If we got an error, or a "break" command, then stop
    507  1.1  christos 		   looping.  */
    508  1.1  christos 		if (ret == invalid_control || ret == break_control)
    509  1.1  christos 		  {
    510  1.1  christos 		    loop = 0;
    511  1.1  christos 		    break;
    512  1.1  christos 		  }
    513  1.1  christos 
    514  1.1  christos 		/* If we got a "continue" command, then restart the loop
    515  1.1  christos 		   at this point.  */
    516  1.1  christos 		if (ret == continue_control)
    517  1.1  christos 		  break;
    518  1.1  christos 
    519  1.1  christos 		/* Get the next statement.  */
    520  1.1  christos 		current = current->next;
    521  1.1  christos 	      }
    522  1.1  christos 	  }
    523  1.1  christos 
    524  1.1  christos 	/* Reset RET so that we don't recurse the break all the way down.  */
    525  1.1  christos 	if (ret == break_control)
    526  1.1  christos 	  ret = simple_control;
    527  1.1  christos 
    528  1.1  christos 	break;
    529  1.1  christos       }
    530  1.1  christos 
    531  1.1  christos     case if_control:
    532  1.1  christos       {
    533  1.1  christos 	int len = strlen (cmd->line) + 4;
    534  1.1  christos 	char *buffer = alloca (len);
    535  1.1  christos 
    536  1.1  christos 	xsnprintf (buffer, len, "if %s", cmd->line);
    537  1.1  christos 	print_command_trace (buffer);
    538  1.1  christos 
    539  1.1  christos 	new_line = insert_args (cmd->line);
    540  1.1  christos 	if (!new_line)
    541  1.1  christos 	  break;
    542  1.1  christos 	make_cleanup (free_current_contents, &new_line);
    543  1.1  christos 	/* Parse the conditional for the if statement.  */
    544  1.1  christos 	expr = parse_expression (new_line);
    545  1.1  christos 	make_cleanup (free_current_contents, &expr);
    546  1.1  christos 
    547  1.1  christos 	current = NULL;
    548  1.1  christos 	ret = simple_control;
    549  1.1  christos 
    550  1.1  christos 	/* Evaluate the conditional.  */
    551  1.1  christos 	val_mark = value_mark ();
    552  1.1  christos 	val = evaluate_expression (expr);
    553  1.1  christos 
    554  1.1  christos 	/* Choose which arm to take commands from based on the value
    555  1.1  christos 	   of the conditional expression.  */
    556  1.1  christos 	if (value_true (val))
    557  1.1  christos 	  current = *cmd->body_list;
    558  1.1  christos 	else if (cmd->body_count == 2)
    559  1.1  christos 	  current = *(cmd->body_list + 1);
    560  1.1  christos 	value_free_to_mark (val_mark);
    561  1.1  christos 
    562  1.1  christos 	/* Execute commands in the given arm.  */
    563  1.1  christos 	while (current)
    564  1.1  christos 	  {
    565  1.1  christos 	    command_nest_depth++;
    566  1.1  christos 	    ret = execute_control_command (current);
    567  1.1  christos 	    command_nest_depth--;
    568  1.1  christos 
    569  1.1  christos 	    /* If we got an error, get out.  */
    570  1.1  christos 	    if (ret != simple_control)
    571  1.1  christos 	      break;
    572  1.1  christos 
    573  1.1  christos 	    /* Get the next statement in the body.  */
    574  1.1  christos 	    current = current->next;
    575  1.1  christos 	  }
    576  1.1  christos 
    577  1.1  christos 	break;
    578  1.1  christos       }
    579  1.1  christos 
    580  1.1  christos     case commands_control:
    581  1.1  christos       {
    582  1.1  christos 	/* Breakpoint commands list, record the commands in the
    583  1.1  christos 	   breakpoint's command list and return.  */
    584  1.1  christos 	new_line = insert_args (cmd->line);
    585  1.1  christos 	if (!new_line)
    586  1.1  christos 	  break;
    587  1.1  christos 	make_cleanup (free_current_contents, &new_line);
    588  1.1  christos 	ret = commands_from_control_command (new_line, cmd);
    589  1.1  christos 	break;
    590  1.1  christos       }
    591  1.1  christos 
    592  1.1  christos     case python_control:
    593  1.1  christos       {
    594  1.1  christos 	eval_python_from_control_command (cmd);
    595  1.1  christos 	ret = simple_control;
    596  1.1  christos 	break;
    597  1.1  christos       }
    598  1.1  christos 
    599  1.1  christos     default:
    600  1.1  christos       warning (_("Invalid control type in canned commands structure."));
    601  1.1  christos       break;
    602  1.1  christos     }
    603  1.1  christos 
    604  1.1  christos   do_cleanups (old_chain);
    605  1.1  christos 
    606  1.1  christos   return ret;
    607  1.1  christos }
    608  1.1  christos 
    609  1.1  christos /* Like execute_control_command, but first set
    610  1.1  christos    suppress_next_print_command_trace.  */
    611  1.1  christos 
    612  1.1  christos enum command_control_type
    613  1.1  christos execute_control_command_untraced (struct command_line *cmd)
    614  1.1  christos {
    615  1.1  christos   suppress_next_print_command_trace = 1;
    616  1.1  christos   return execute_control_command (cmd);
    617  1.1  christos }
    618  1.1  christos 
    619  1.1  christos 
    620  1.1  christos /* "while" command support.  Executes a body of statements while the
    621  1.1  christos    loop condition is nonzero.  */
    622  1.1  christos 
    623  1.1  christos static void
    624  1.1  christos while_command (char *arg, int from_tty)
    625  1.1  christos {
    626  1.1  christos   struct command_line *command = NULL;
    627  1.1  christos   struct cleanup *old_chain;
    628  1.1  christos 
    629  1.1  christos   control_level = 1;
    630  1.1  christos   command = get_command_line (while_control, arg);
    631  1.1  christos 
    632  1.1  christos   if (command == NULL)
    633  1.1  christos     return;
    634  1.1  christos 
    635  1.1  christos   old_chain = make_cleanup_restore_integer (&interpreter_async);
    636  1.1  christos   interpreter_async = 0;
    637  1.1  christos 
    638  1.1  christos   execute_control_command_untraced (command);
    639  1.1  christos   free_command_lines (&command);
    640  1.1  christos 
    641  1.1  christos   do_cleanups (old_chain);
    642  1.1  christos }
    643  1.1  christos 
    644  1.1  christos /* "if" command support.  Execute either the true or false arm depending
    645  1.1  christos    on the value of the if conditional.  */
    646  1.1  christos 
    647  1.1  christos static void
    648  1.1  christos if_command (char *arg, int from_tty)
    649  1.1  christos {
    650  1.1  christos   struct command_line *command = NULL;
    651  1.1  christos   struct cleanup *old_chain;
    652  1.1  christos 
    653  1.1  christos   control_level = 1;
    654  1.1  christos   command = get_command_line (if_control, arg);
    655  1.1  christos 
    656  1.1  christos   if (command == NULL)
    657  1.1  christos     return;
    658  1.1  christos 
    659  1.1  christos   old_chain = make_cleanup_restore_integer (&interpreter_async);
    660  1.1  christos   interpreter_async = 0;
    661  1.1  christos 
    662  1.1  christos   execute_control_command_untraced (command);
    663  1.1  christos   free_command_lines (&command);
    664  1.1  christos 
    665  1.1  christos   do_cleanups (old_chain);
    666  1.1  christos }
    667  1.1  christos 
    668  1.1  christos /* Cleanup */
    669  1.1  christos static void
    670  1.1  christos arg_cleanup (void *ignore)
    671  1.1  christos {
    672  1.1  christos   struct user_args *oargs = user_args;
    673  1.1  christos 
    674  1.1  christos   if (!user_args)
    675  1.1  christos     internal_error (__FILE__, __LINE__,
    676  1.1  christos 		    _("arg_cleanup called with no user args.\n"));
    677  1.1  christos 
    678  1.1  christos   user_args = user_args->next;
    679  1.1  christos   xfree (oargs->command);
    680  1.1  christos   xfree (oargs);
    681  1.1  christos }
    682  1.1  christos 
    683  1.1  christos /* Bind the incomming arguments for a user defined command to
    684  1.1  christos    $arg0, $arg1 ... $argMAXUSERARGS.  */
    685  1.1  christos 
    686  1.1  christos static struct cleanup *
    687  1.1  christos setup_user_args (char *p)
    688  1.1  christos {
    689  1.1  christos   struct user_args *args;
    690  1.1  christos   struct cleanup *old_chain;
    691  1.1  christos   unsigned int arg_count = 0;
    692  1.1  christos 
    693  1.1  christos   args = (struct user_args *) xmalloc (sizeof (struct user_args));
    694  1.1  christos   memset (args, 0, sizeof (struct user_args));
    695  1.1  christos 
    696  1.1  christos   args->next = user_args;
    697  1.1  christos   user_args = args;
    698  1.1  christos 
    699  1.1  christos   old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
    700  1.1  christos 
    701  1.1  christos   if (p == NULL)
    702  1.1  christos     return old_chain;
    703  1.1  christos 
    704  1.1  christos   user_args->command = p = xstrdup (p);
    705  1.1  christos 
    706  1.1  christos   while (*p)
    707  1.1  christos     {
    708  1.1  christos       char *start_arg;
    709  1.1  christos       int squote = 0;
    710  1.1  christos       int dquote = 0;
    711  1.1  christos       int bsquote = 0;
    712  1.1  christos 
    713  1.1  christos       if (arg_count >= MAXUSERARGS)
    714  1.1  christos 	error (_("user defined function may only have %d arguments."),
    715  1.1  christos 	       MAXUSERARGS);
    716  1.1  christos 
    717  1.1  christos       /* Strip whitespace.  */
    718  1.1  christos       while (*p == ' ' || *p == '\t')
    719  1.1  christos 	p++;
    720  1.1  christos 
    721  1.1  christos       /* P now points to an argument.  */
    722  1.1  christos       start_arg = p;
    723  1.1  christos       user_args->a[arg_count].arg = p;
    724  1.1  christos 
    725  1.1  christos       /* Get to the end of this argument.  */
    726  1.1  christos       while (*p)
    727  1.1  christos 	{
    728  1.1  christos 	  if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
    729  1.1  christos 	    break;
    730  1.1  christos 	  else
    731  1.1  christos 	    {
    732  1.1  christos 	      if (bsquote)
    733  1.1  christos 		bsquote = 0;
    734  1.1  christos 	      else if (*p == '\\')
    735  1.1  christos 		bsquote = 1;
    736  1.1  christos 	      else if (squote)
    737  1.1  christos 		{
    738  1.1  christos 		  if (*p == '\'')
    739  1.1  christos 		    squote = 0;
    740  1.1  christos 		}
    741  1.1  christos 	      else if (dquote)
    742  1.1  christos 		{
    743  1.1  christos 		  if (*p == '"')
    744  1.1  christos 		    dquote = 0;
    745  1.1  christos 		}
    746  1.1  christos 	      else
    747  1.1  christos 		{
    748  1.1  christos 		  if (*p == '\'')
    749  1.1  christos 		    squote = 1;
    750  1.1  christos 		  else if (*p == '"')
    751  1.1  christos 		    dquote = 1;
    752  1.1  christos 		}
    753  1.1  christos 	      p++;
    754  1.1  christos 	    }
    755  1.1  christos 	}
    756  1.1  christos 
    757  1.1  christos       user_args->a[arg_count].len = p - start_arg;
    758  1.1  christos       arg_count++;
    759  1.1  christos       user_args->count++;
    760  1.1  christos     }
    761  1.1  christos   return old_chain;
    762  1.1  christos }
    763  1.1  christos 
    764  1.1  christos /* Given character string P, return a point to the first argument
    765  1.1  christos    ($arg), or NULL if P contains no arguments.  */
    766  1.1  christos 
    767  1.1  christos static char *
    768  1.1  christos locate_arg (char *p)
    769  1.1  christos {
    770  1.1  christos   while ((p = strchr (p, '$')))
    771  1.1  christos     {
    772  1.1  christos       if (strncmp (p, "$arg", 4) == 0
    773  1.1  christos 	  && (isdigit (p[4]) || p[4] == 'c'))
    774  1.1  christos 	return p;
    775  1.1  christos       p++;
    776  1.1  christos     }
    777  1.1  christos   return NULL;
    778  1.1  christos }
    779  1.1  christos 
    780  1.1  christos /* Insert the user defined arguments stored in user_arg into the $arg
    781  1.1  christos    arguments found in line, with the updated copy being placed into
    782  1.1  christos    nline.  */
    783  1.1  christos 
    784  1.1  christos static char *
    785  1.1  christos insert_args (char *line)
    786  1.1  christos {
    787  1.1  christos   char *p, *save_line, *new_line;
    788  1.1  christos   unsigned len, i;
    789  1.1  christos 
    790  1.1  christos   /* If we are not in a user-defined function, treat $argc, $arg0, et
    791  1.1  christos      cetera as normal convenience variables.  */
    792  1.1  christos   if (user_args == NULL)
    793  1.1  christos     return xstrdup (line);
    794  1.1  christos 
    795  1.1  christos   /* First we need to know how much memory to allocate for the new
    796  1.1  christos      line.  */
    797  1.1  christos   save_line = line;
    798  1.1  christos   len = 0;
    799  1.1  christos   while ((p = locate_arg (line)))
    800  1.1  christos     {
    801  1.1  christos       len += p - line;
    802  1.1  christos       i = p[4] - '0';
    803  1.1  christos 
    804  1.1  christos       if (p[4] == 'c')
    805  1.1  christos 	{
    806  1.1  christos 	  /* $argc.  Number will be <=10.  */
    807  1.1  christos 	  len += user_args->count == 10 ? 2 : 1;
    808  1.1  christos 	}
    809  1.1  christos       else if (i >= user_args->count)
    810  1.1  christos 	{
    811  1.1  christos 	  error (_("Missing argument %d in user function."), i);
    812  1.1  christos 	  return NULL;
    813  1.1  christos 	}
    814  1.1  christos       else
    815  1.1  christos 	{
    816  1.1  christos 	  len += user_args->a[i].len;
    817  1.1  christos 	}
    818  1.1  christos       line = p + 5;
    819  1.1  christos     }
    820  1.1  christos 
    821  1.1  christos   /* Don't forget the tail.  */
    822  1.1  christos   len += strlen (line);
    823  1.1  christos 
    824  1.1  christos   /* Allocate space for the new line and fill it in.  */
    825  1.1  christos   new_line = (char *) xmalloc (len + 1);
    826  1.1  christos   if (new_line == NULL)
    827  1.1  christos     return NULL;
    828  1.1  christos 
    829  1.1  christos   /* Restore pointer to beginning of old line.  */
    830  1.1  christos   line = save_line;
    831  1.1  christos 
    832  1.1  christos   /* Save pointer to beginning of new line.  */
    833  1.1  christos   save_line = new_line;
    834  1.1  christos 
    835  1.1  christos   while ((p = locate_arg (line)))
    836  1.1  christos     {
    837  1.1  christos       int i, len;
    838  1.1  christos 
    839  1.1  christos       memcpy (new_line, line, p - line);
    840  1.1  christos       new_line += p - line;
    841  1.1  christos 
    842  1.1  christos       if (p[4] == 'c')
    843  1.1  christos 	{
    844  1.1  christos 	  gdb_assert (user_args->count >= 0 && user_args->count <= 10);
    845  1.1  christos 	  if (user_args->count == 10)
    846  1.1  christos 	    {
    847  1.1  christos 	      *(new_line++) = '1';
    848  1.1  christos 	      *(new_line++) = '0';
    849  1.1  christos 	    }
    850  1.1  christos 	  else
    851  1.1  christos 	    *(new_line++) = user_args->count + '0';
    852  1.1  christos 	}
    853  1.1  christos       else
    854  1.1  christos 	{
    855  1.1  christos 	  i = p[4] - '0';
    856  1.1  christos 	  len = user_args->a[i].len;
    857  1.1  christos 	  if (len)
    858  1.1  christos 	    {
    859  1.1  christos 	      memcpy (new_line, user_args->a[i].arg, len);
    860  1.1  christos 	      new_line += len;
    861  1.1  christos 	    }
    862  1.1  christos 	}
    863  1.1  christos       line = p + 5;
    864  1.1  christos     }
    865  1.1  christos   /* Don't forget the tail.  */
    866  1.1  christos   strcpy (new_line, line);
    867  1.1  christos 
    868  1.1  christos   /* Return a pointer to the beginning of the new line.  */
    869  1.1  christos   return save_line;
    870  1.1  christos }
    871  1.1  christos 
    872  1.1  christos 
    873  1.1  christos /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
    875  1.1  christos    code bodies.  This is typically used when we encounter an "else"
    876  1.1  christos    clause for an "if" command.  */
    877  1.1  christos 
    878  1.1  christos static void
    879  1.1  christos realloc_body_list (struct command_line *command, int new_length)
    880  1.1  christos {
    881  1.1  christos   int n;
    882  1.1  christos   struct command_line **body_list;
    883  1.1  christos 
    884  1.1  christos   n = command->body_count;
    885  1.1  christos 
    886  1.1  christos   /* Nothing to do?  */
    887  1.1  christos   if (new_length <= n)
    888  1.1  christos     return;
    889  1.1  christos 
    890  1.1  christos   body_list = (struct command_line **)
    891  1.1  christos     xmalloc (sizeof (struct command_line *) * new_length);
    892  1.1  christos 
    893  1.1  christos   memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
    894  1.1  christos   memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
    895  1.1  christos 
    896  1.1  christos   xfree (command->body_list);
    897  1.1  christos   command->body_list = body_list;
    898  1.1  christos   command->body_count = new_length;
    899  1.1  christos }
    900  1.1  christos 
    901  1.1  christos /* Read next line from stdout.  Passed to read_command_line_1 and
    902  1.1  christos    recurse_read_control_structure whenever we need to read commands
    903  1.1  christos    from stdout.  */
    904  1.1  christos 
    905  1.1  christos static char *
    906  1.1  christos read_next_line (void)
    907  1.1  christos {
    908  1.1  christos   char *prompt_ptr, control_prompt[256];
    909  1.1  christos   int i = 0;
    910  1.1  christos 
    911  1.1  christos   if (control_level >= 254)
    912  1.1  christos     error (_("Control nesting too deep!"));
    913  1.1  christos 
    914  1.1  christos   /* Set a prompt based on the nesting of the control commands.  */
    915  1.1  christos   if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
    916  1.1  christos     {
    917  1.1  christos       for (i = 0; i < control_level; i++)
    918  1.1  christos 	control_prompt[i] = ' ';
    919  1.1  christos       control_prompt[i] = '>';
    920  1.1  christos       control_prompt[i + 1] = '\0';
    921  1.1  christos       prompt_ptr = (char *) &control_prompt[0];
    922  1.1  christos     }
    923  1.1  christos   else
    924  1.1  christos     prompt_ptr = NULL;
    925  1.1  christos 
    926  1.1  christos   return command_line_input (prompt_ptr, instream == stdin, "commands");
    927  1.1  christos }
    928  1.1  christos 
    929  1.1  christos /* Process one input line.  If the command is an "end", return such an
    930  1.1  christos    indication to the caller.  If PARSE_COMMANDS is true, strip leading
    931  1.1  christos    whitespace (trailing whitespace is always stripped) in the line,
    932  1.1  christos    attempt to recognize GDB control commands, and also return an
    933  1.1  christos    indication if the command is an "else" or a nop.
    934  1.1  christos 
    935  1.1  christos    Otherwise, only "end" is recognized.  */
    936  1.1  christos 
    937  1.1  christos static enum misc_command_type
    938  1.1  christos process_next_line (char *p, struct command_line **command, int parse_commands,
    939  1.1  christos 		   void (*validator)(char *, void *), void *closure)
    940  1.1  christos {
    941  1.1  christos   char *p_end;
    942  1.1  christos   char *p_start;
    943  1.1  christos   int not_handled = 0;
    944  1.1  christos 
    945  1.1  christos   /* Not sure what to do here.  */
    946  1.1  christos   if (p == NULL)
    947  1.1  christos     return end_command;
    948  1.1  christos 
    949  1.1  christos   /* Strip trailing whitespace.  */
    950  1.1  christos   p_end = p + strlen (p);
    951  1.1  christos   while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
    952  1.1  christos     p_end--;
    953  1.1  christos 
    954  1.1  christos   p_start = p;
    955  1.1  christos   /* Strip leading whitespace.  */
    956  1.1  christos   while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
    957  1.1  christos     p_start++;
    958  1.1  christos 
    959  1.1  christos   /* 'end' is always recognized, regardless of parse_commands value.
    960  1.1  christos      We also permit whitespace before end and after.  */
    961  1.1  christos   if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
    962  1.1  christos     return end_command;
    963  1.1  christos 
    964  1.1  christos   if (parse_commands)
    965  1.1  christos     {
    966  1.1  christos       /* If commands are parsed, we skip initial spaces.  Otherwise,
    967  1.1  christos 	 which is the case for Python commands and documentation
    968  1.1  christos 	 (see the 'document' command), spaces are preserved.  */
    969  1.1  christos       p = p_start;
    970  1.1  christos 
    971  1.1  christos       /* Blanks and comments don't really do anything, but we need to
    972  1.1  christos 	 distinguish them from else, end and other commands which can
    973  1.1  christos 	 be executed.  */
    974  1.1  christos       if (p_end == p || p[0] == '#')
    975  1.1  christos 	return nop_command;
    976  1.1  christos 
    977  1.1  christos       /* Is the else clause of an if control structure?  */
    978  1.1  christos       if (p_end - p == 4 && !strncmp (p, "else", 4))
    979  1.1  christos 	return else_command;
    980  1.1  christos 
    981  1.1  christos       /* Check for while, if, break, continue, etc and build a new
    982  1.1  christos 	 command line structure for them.  */
    983  1.1  christos       if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
    984  1.1  christos 	  || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
    985  1.1  christos 	  || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
    986  1.1  christos 	{
    987  1.1  christos 	  /* Because validate_actionline and encode_action lookup
    988  1.1  christos 	     command's line as command, we need the line to
    989  1.1  christos 	     include 'while-stepping'.
    990  1.1  christos 
    991  1.1  christos 	     For 'ws' alias, the command will have 'ws', not expanded
    992  1.1  christos 	     to 'while-stepping'.  This is intentional -- we don't
    993  1.1  christos 	     really want frontend to send a command list with 'ws',
    994  1.1  christos 	     and next break-info returning command line with
    995  1.1  christos 	     'while-stepping'.  This should work, but might cause the
    996  1.1  christos 	     breakpoint to be marked as changed while it's actually
    997  1.1  christos 	     not.  */
    998  1.1  christos 	  *command = build_command_line (while_stepping_control, p);
    999  1.1  christos 	}
   1000  1.1  christos       else if (p_end - p > 5 && !strncmp (p, "while", 5))
   1001  1.1  christos 	{
   1002  1.1  christos 	  char *first_arg;
   1003  1.1  christos 
   1004  1.1  christos 	  first_arg = p + 5;
   1005  1.1  christos 	  while (first_arg < p_end && isspace (*first_arg))
   1006  1.1  christos 	    first_arg++;
   1007  1.1  christos 	  *command = build_command_line (while_control, first_arg);
   1008  1.1  christos 	}
   1009  1.1  christos       else if (p_end - p > 2 && !strncmp (p, "if", 2))
   1010  1.1  christos 	{
   1011  1.1  christos 	  char *first_arg;
   1012  1.1  christos 
   1013  1.1  christos 	  first_arg = p + 2;
   1014  1.1  christos 	  while (first_arg < p_end && isspace (*first_arg))
   1015  1.1  christos 	    first_arg++;
   1016  1.1  christos 	  *command = build_command_line (if_control, first_arg);
   1017  1.1  christos 	}
   1018  1.1  christos       else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
   1019  1.1  christos 	{
   1020  1.1  christos 	  char *first_arg;
   1021  1.1  christos 
   1022  1.1  christos 	  first_arg = p + 8;
   1023  1.1  christos 	  while (first_arg < p_end && isspace (*first_arg))
   1024  1.1  christos 	    first_arg++;
   1025  1.1  christos 	  *command = build_command_line (commands_control, first_arg);
   1026  1.1  christos 	}
   1027  1.1  christos       else if (p_end - p == 6 && !strncmp (p, "python", 6))
   1028  1.1  christos 	{
   1029  1.1  christos 	  /* Note that we ignore the inline "python command" form
   1030  1.1  christos 	     here.  */
   1031  1.1  christos 	  *command = build_command_line (python_control, "");
   1032  1.1  christos 	}
   1033  1.1  christos       else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
   1034  1.1  christos 	{
   1035  1.1  christos 	  *command = (struct command_line *)
   1036  1.1  christos 	    xmalloc (sizeof (struct command_line));
   1037  1.1  christos 	  (*command)->next = NULL;
   1038  1.1  christos 	  (*command)->line = NULL;
   1039  1.1  christos 	  (*command)->control_type = break_control;
   1040  1.1  christos 	  (*command)->body_count = 0;
   1041  1.1  christos 	  (*command)->body_list = NULL;
   1042  1.1  christos 	}
   1043  1.1  christos       else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
   1044  1.1  christos 	{
   1045  1.1  christos 	  *command = (struct command_line *)
   1046  1.1  christos 	    xmalloc (sizeof (struct command_line));
   1047  1.1  christos 	  (*command)->next = NULL;
   1048  1.1  christos 	  (*command)->line = NULL;
   1049  1.1  christos 	  (*command)->control_type = continue_control;
   1050  1.1  christos 	  (*command)->body_count = 0;
   1051  1.1  christos 	  (*command)->body_list = NULL;
   1052  1.1  christos 	}
   1053  1.1  christos       else
   1054  1.1  christos 	not_handled = 1;
   1055  1.1  christos     }
   1056  1.1  christos 
   1057  1.1  christos   if (!parse_commands || not_handled)
   1058  1.1  christos     {
   1059  1.1  christos       /* A normal command.  */
   1060  1.1  christos       *command = (struct command_line *)
   1061  1.1  christos 	xmalloc (sizeof (struct command_line));
   1062  1.1  christos       (*command)->next = NULL;
   1063  1.1  christos       (*command)->line = savestring (p, p_end - p);
   1064  1.1  christos       (*command)->control_type = simple_control;
   1065  1.1  christos       (*command)->body_count = 0;
   1066  1.1  christos       (*command)->body_list = NULL;
   1067  1.1  christos     }
   1068  1.1  christos 
   1069  1.1  christos   if (validator)
   1070  1.1  christos     {
   1071  1.1  christos       volatile struct gdb_exception ex;
   1072  1.1  christos 
   1073  1.1  christos       TRY_CATCH (ex, RETURN_MASK_ALL)
   1074  1.1  christos 	{
   1075  1.1  christos 	  validator ((*command)->line, closure);
   1076  1.1  christos 	}
   1077  1.1  christos       if (ex.reason < 0)
   1078  1.1  christos 	{
   1079  1.1  christos 	  xfree (*command);
   1080  1.1  christos 	  throw_exception (ex);
   1081  1.1  christos 	}
   1082  1.1  christos     }
   1083  1.1  christos 
   1084  1.1  christos   /* Nothing special.  */
   1085  1.1  christos   return ok_command;
   1086  1.1  christos }
   1087  1.1  christos 
   1088  1.1  christos /* Recursively read in the control structures and create a
   1089  1.1  christos    command_line structure from them.  Use read_next_line_func to
   1090  1.1  christos    obtain lines of the command.  */
   1091  1.1  christos 
   1092  1.1  christos static enum command_control_type
   1093  1.1  christos recurse_read_control_structure (char * (*read_next_line_func) (void),
   1094  1.1  christos 				struct command_line *current_cmd,
   1095  1.1  christos 				void (*validator)(char *, void *),
   1096  1.1  christos 				void *closure)
   1097  1.1  christos {
   1098  1.1  christos   int current_body, i;
   1099  1.1  christos   enum misc_command_type val;
   1100  1.1  christos   enum command_control_type ret;
   1101  1.1  christos   struct command_line **body_ptr, *child_tail, *next;
   1102  1.1  christos 
   1103  1.1  christos   child_tail = NULL;
   1104  1.1  christos   current_body = 1;
   1105  1.1  christos 
   1106  1.1  christos   /* Sanity checks.  */
   1107  1.1  christos   if (current_cmd->control_type == simple_control)
   1108  1.1  christos     error (_("Recursed on a simple control type."));
   1109  1.1  christos 
   1110  1.1  christos   if (current_body > current_cmd->body_count)
   1111  1.1  christos     error (_("Allocated body is smaller than this command type needs."));
   1112  1.1  christos 
   1113  1.1  christos   /* Read lines from the input stream and build control structures.  */
   1114  1.1  christos   while (1)
   1115  1.1  christos     {
   1116  1.1  christos       dont_repeat ();
   1117  1.1  christos 
   1118  1.1  christos       next = NULL;
   1119  1.1  christos       val = process_next_line (read_next_line_func (), &next,
   1120  1.1  christos 			       current_cmd->control_type != python_control,
   1121  1.1  christos 			       validator, closure);
   1122  1.1  christos 
   1123  1.1  christos       /* Just skip blanks and comments.  */
   1124  1.1  christos       if (val == nop_command)
   1125  1.1  christos 	continue;
   1126  1.1  christos 
   1127  1.1  christos       if (val == end_command)
   1128  1.1  christos 	{
   1129  1.1  christos 	  if (multi_line_command_p (current_cmd->control_type))
   1130  1.1  christos 	    {
   1131  1.1  christos 	      /* Success reading an entire canned sequence of commands.  */
   1132  1.1  christos 	      ret = simple_control;
   1133  1.1  christos 	      break;
   1134  1.1  christos 	    }
   1135  1.1  christos 	  else
   1136  1.1  christos 	    {
   1137  1.1  christos 	      ret = invalid_control;
   1138  1.1  christos 	      break;
   1139  1.1  christos 	    }
   1140  1.1  christos 	}
   1141  1.1  christos 
   1142  1.1  christos       /* Not the end of a control structure.  */
   1143  1.1  christos       if (val == else_command)
   1144  1.1  christos 	{
   1145  1.1  christos 	  if (current_cmd->control_type == if_control
   1146  1.1  christos 	      && current_body == 1)
   1147  1.1  christos 	    {
   1148  1.1  christos 	      realloc_body_list (current_cmd, 2);
   1149  1.1  christos 	      current_body = 2;
   1150  1.1  christos 	      child_tail = NULL;
   1151  1.1  christos 	      continue;
   1152  1.1  christos 	    }
   1153  1.1  christos 	  else
   1154  1.1  christos 	    {
   1155  1.1  christos 	      ret = invalid_control;
   1156  1.1  christos 	      break;
   1157  1.1  christos 	    }
   1158  1.1  christos 	}
   1159  1.1  christos 
   1160  1.1  christos       if (child_tail)
   1161  1.1  christos 	{
   1162  1.1  christos 	  child_tail->next = next;
   1163  1.1  christos 	}
   1164  1.1  christos       else
   1165  1.1  christos 	{
   1166  1.1  christos 	  body_ptr = current_cmd->body_list;
   1167  1.1  christos 	  for (i = 1; i < current_body; i++)
   1168  1.1  christos 	    body_ptr++;
   1169  1.1  christos 
   1170  1.1  christos 	  *body_ptr = next;
   1171  1.1  christos 
   1172  1.1  christos 	}
   1173  1.1  christos 
   1174  1.1  christos       child_tail = next;
   1175  1.1  christos 
   1176  1.1  christos       /* If the latest line is another control structure, then recurse
   1177  1.1  christos          on it.  */
   1178  1.1  christos       if (multi_line_command_p (next->control_type))
   1179  1.1  christos 	{
   1180  1.1  christos 	  control_level++;
   1181  1.1  christos 	  ret = recurse_read_control_structure (read_next_line_func, next,
   1182  1.1  christos 						validator, closure);
   1183  1.1  christos 	  control_level--;
   1184  1.1  christos 
   1185  1.1  christos 	  if (ret != simple_control)
   1186  1.1  christos 	    break;
   1187  1.1  christos 	}
   1188  1.1  christos     }
   1189  1.1  christos 
   1190  1.1  christos   dont_repeat ();
   1191  1.1  christos 
   1192  1.1  christos   return ret;
   1193  1.1  christos }
   1194  1.1  christos 
   1195  1.1  christos static void
   1196  1.1  christos restore_interp (void *arg)
   1197  1.1  christos {
   1198  1.1  christos   interp_set_temp (interp_name ((struct interp *)arg));
   1199  1.1  christos }
   1200  1.1  christos 
   1201  1.1  christos /* Read lines from the input stream and accumulate them in a chain of
   1202  1.1  christos    struct command_line's, which is then returned.  For input from a
   1203  1.1  christos    terminal, the special command "end" is used to mark the end of the
   1204  1.1  christos    input, and is not included in the returned chain of commands.
   1205  1.1  christos 
   1206  1.1  christos    If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
   1207  1.1  christos    is always stripped) in the line and attempt to recognize GDB control
   1208  1.1  christos    commands.  Otherwise, only "end" is recognized.  */
   1209  1.1  christos 
   1210  1.1  christos #define END_MESSAGE "End with a line saying just \"end\"."
   1211  1.1  christos 
   1212  1.1  christos struct command_line *
   1213  1.1  christos read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
   1214  1.1  christos 		    void (*validator)(char *, void *), void *closure)
   1215  1.1  christos {
   1216  1.1  christos   struct command_line *head;
   1217  1.1  christos 
   1218  1.1  christos   if (from_tty && input_from_terminal_p ())
   1219  1.1  christos     {
   1220  1.1  christos       if (deprecated_readline_begin_hook)
   1221  1.1  christos 	{
   1222  1.1  christos 	  /* Note - intentional to merge messages with no newline.  */
   1223  1.1  christos 	  (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg,
   1224  1.1  christos 					     END_MESSAGE);
   1225  1.1  christos 	}
   1226  1.1  christos       else
   1227  1.1  christos 	{
   1228  1.1  christos 	  printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
   1229  1.1  christos 	  gdb_flush (gdb_stdout);
   1230  1.1  christos 	}
   1231  1.1  christos     }
   1232  1.1  christos 
   1233  1.1  christos 
   1234  1.1  christos   /* Reading commands assumes the CLI behavior, so temporarily
   1235  1.1  christos      override the current interpreter with CLI.  */
   1236  1.1  christos   if (current_interp_named_p (INTERP_CONSOLE))
   1237  1.1  christos     head = read_command_lines_1 (read_next_line, parse_commands,
   1238  1.1  christos 				 validator, closure);
   1239  1.1  christos   else
   1240  1.1  christos     {
   1241  1.1  christos       struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
   1242  1.1  christos       struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
   1243  1.1  christos 
   1244  1.1  christos       head = read_command_lines_1 (read_next_line, parse_commands,
   1245  1.1  christos 				   validator, closure);
   1246  1.1  christos       do_cleanups (old_chain);
   1247  1.1  christos     }
   1248  1.1  christos 
   1249  1.1  christos   if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
   1250  1.1  christos     {
   1251  1.1  christos       (*deprecated_readline_end_hook) ();
   1252  1.1  christos     }
   1253  1.1  christos   return (head);
   1254  1.1  christos }
   1255  1.1  christos 
   1256  1.1  christos /* Act the same way as read_command_lines, except that each new line is
   1257  1.1  christos    obtained using READ_NEXT_LINE_FUNC.  */
   1258  1.1  christos 
   1259  1.1  christos struct command_line *
   1260  1.1  christos read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
   1261  1.1  christos 		      void (*validator)(char *, void *), void *closure)
   1262  1.1  christos {
   1263  1.1  christos   struct command_line *head, *tail, *next;
   1264  1.1  christos   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
   1265  1.1  christos   enum command_control_type ret;
   1266  1.1  christos   enum misc_command_type val;
   1267  1.1  christos 
   1268  1.1  christos   control_level = 0;
   1269  1.1  christos   head = tail = NULL;
   1270  1.1  christos 
   1271  1.1  christos   while (1)
   1272  1.1  christos     {
   1273  1.1  christos       dont_repeat ();
   1274  1.1  christos       val = process_next_line (read_next_line_func (), &next, parse_commands,
   1275  1.1  christos 			       validator, closure);
   1276  1.1  christos 
   1277  1.1  christos       /* Ignore blank lines or comments.  */
   1278  1.1  christos       if (val == nop_command)
   1279  1.1  christos 	continue;
   1280  1.1  christos 
   1281  1.1  christos       if (val == end_command)
   1282  1.1  christos 	{
   1283  1.1  christos 	  ret = simple_control;
   1284  1.1  christos 	  break;
   1285  1.1  christos 	}
   1286  1.1  christos 
   1287  1.1  christos       if (val != ok_command)
   1288  1.1  christos 	{
   1289  1.1  christos 	  ret = invalid_control;
   1290  1.1  christos 	  break;
   1291  1.1  christos 	}
   1292  1.1  christos 
   1293  1.1  christos       if (multi_line_command_p (next->control_type))
   1294  1.1  christos 	{
   1295  1.1  christos 	  control_level++;
   1296  1.1  christos 	  ret = recurse_read_control_structure (read_next_line_func, next,
   1297  1.1  christos 						validator, closure);
   1298  1.1  christos 	  control_level--;
   1299  1.1  christos 
   1300  1.1  christos 	  if (ret == invalid_control)
   1301  1.1  christos 	    break;
   1302  1.1  christos 	}
   1303  1.1  christos 
   1304  1.1  christos       if (tail)
   1305  1.1  christos 	{
   1306  1.1  christos 	  tail->next = next;
   1307  1.1  christos 	}
   1308  1.1  christos       else
   1309  1.1  christos 	{
   1310  1.1  christos 	  head = next;
   1311  1.1  christos 	  make_cleanup_free_command_lines (&head);
   1312  1.1  christos 	}
   1313  1.1  christos       tail = next;
   1314  1.1  christos     }
   1315  1.1  christos 
   1316  1.1  christos   dont_repeat ();
   1317  1.1  christos 
   1318  1.1  christos   if (ret != invalid_control)
   1319  1.1  christos     discard_cleanups (old_chain);
   1320  1.1  christos   else
   1321  1.1  christos     do_cleanups (old_chain);
   1322  1.1  christos 
   1323  1.1  christos   return head;
   1324  1.1  christos }
   1325  1.1  christos 
   1326  1.1  christos /* Free a chain of struct command_line's.  */
   1327  1.1  christos 
   1328  1.1  christos void
   1329  1.1  christos free_command_lines (struct command_line **lptr)
   1330  1.1  christos {
   1331  1.1  christos   struct command_line *l = *lptr;
   1332  1.1  christos   struct command_line *next;
   1333  1.1  christos   struct command_line **blist;
   1334  1.1  christos   int i;
   1335  1.1  christos 
   1336  1.1  christos   while (l)
   1337  1.1  christos     {
   1338  1.1  christos       if (l->body_count > 0)
   1339  1.1  christos 	{
   1340  1.1  christos 	  blist = l->body_list;
   1341  1.1  christos 	  for (i = 0; i < l->body_count; i++, blist++)
   1342  1.1  christos 	    free_command_lines (blist);
   1343  1.1  christos 	}
   1344  1.1  christos       next = l->next;
   1345  1.1  christos       xfree (l->line);
   1346  1.1  christos       xfree (l);
   1347  1.1  christos       l = next;
   1348  1.1  christos     }
   1349  1.1  christos   *lptr = NULL;
   1350  1.1  christos }
   1351  1.1  christos 
   1352  1.1  christos static void
   1353  1.1  christos do_free_command_lines_cleanup (void *arg)
   1354  1.1  christos {
   1355  1.1  christos   free_command_lines (arg);
   1356  1.1  christos }
   1357  1.1  christos 
   1358  1.1  christos struct cleanup *
   1359  1.1  christos make_cleanup_free_command_lines (struct command_line **arg)
   1360  1.1  christos {
   1361  1.1  christos   return make_cleanup (do_free_command_lines_cleanup, arg);
   1362  1.1  christos }
   1363  1.1  christos 
   1364  1.1  christos struct command_line *
   1365  1.1  christos copy_command_lines (struct command_line *cmds)
   1366  1.1  christos {
   1367  1.1  christos   struct command_line *result = NULL;
   1368  1.1  christos 
   1369  1.1  christos   if (cmds)
   1370  1.1  christos     {
   1371  1.1  christos       result = (struct command_line *) xmalloc (sizeof (struct command_line));
   1372  1.1  christos 
   1373  1.1  christos       result->next = copy_command_lines (cmds->next);
   1374  1.1  christos       result->line = xstrdup (cmds->line);
   1375  1.1  christos       result->control_type = cmds->control_type;
   1376  1.1  christos       result->body_count = cmds->body_count;
   1377  1.1  christos       if (cmds->body_count > 0)
   1378  1.1  christos         {
   1379  1.1  christos           int i;
   1380  1.1  christos 
   1381  1.1  christos           result->body_list = (struct command_line **)
   1382  1.1  christos             xmalloc (sizeof (struct command_line *) * cmds->body_count);
   1383  1.1  christos 
   1384  1.1  christos           for (i = 0; i < cmds->body_count; i++)
   1385  1.1  christos             result->body_list[i] = copy_command_lines (cmds->body_list[i]);
   1386  1.1  christos         }
   1387  1.1  christos       else
   1388  1.1  christos         result->body_list = NULL;
   1389  1.1  christos     }
   1390  1.1  christos 
   1391  1.1  christos   return result;
   1392  1.1  christos }
   1393  1.1  christos 
   1394  1.1  christos /* Validate that *COMNAME is a valid name for a command.  Return the
   1396  1.1  christos    containing command list, in case it starts with a prefix command.
   1397  1.1  christos    The prefix must already exist.  *COMNAME is advanced to point after
   1398  1.1  christos    any prefix, and a NUL character overwrites the space after the
   1399  1.1  christos    prefix.  */
   1400  1.1  christos 
   1401  1.1  christos static struct cmd_list_element **
   1402  1.1  christos validate_comname (char **comname)
   1403  1.1  christos {
   1404  1.1  christos   struct cmd_list_element **list = &cmdlist;
   1405  1.1  christos   char *p, *last_word;
   1406  1.1  christos 
   1407  1.1  christos   if (*comname == 0)
   1408  1.1  christos     error_no_arg (_("name of command to define"));
   1409  1.1  christos 
   1410  1.1  christos   /* Find the last word of the argument.  */
   1411  1.1  christos   p = *comname + strlen (*comname);
   1412  1.1  christos   while (p > *comname && isspace (p[-1]))
   1413  1.1  christos     p--;
   1414  1.1  christos   while (p > *comname && !isspace (p[-1]))
   1415  1.1  christos     p--;
   1416  1.1  christos   last_word = p;
   1417  1.1  christos 
   1418  1.1  christos   /* Find the corresponding command list.  */
   1419  1.1  christos   if (last_word != *comname)
   1420  1.1  christos     {
   1421  1.1  christos       struct cmd_list_element *c;
   1422  1.1  christos       char saved_char;
   1423  1.1  christos       const char *tem = *comname;
   1424  1.1  christos 
   1425  1.1  christos       /* Separate the prefix and the command.  */
   1426  1.1  christos       saved_char = last_word[-1];
   1427  1.1  christos       last_word[-1] = '\0';
   1428  1.1  christos 
   1429  1.1  christos       c = lookup_cmd (&tem, cmdlist, "", 0, 1);
   1430  1.1  christos       if (c->prefixlist == NULL)
   1431  1.1  christos 	error (_("\"%s\" is not a prefix command."), *comname);
   1432  1.1  christos 
   1433  1.1  christos       list = c->prefixlist;
   1434  1.1  christos       last_word[-1] = saved_char;
   1435  1.1  christos       *comname = last_word;
   1436  1.1  christos     }
   1437  1.1  christos 
   1438  1.1  christos   p = *comname;
   1439  1.1  christos   while (*p)
   1440  1.1  christos     {
   1441  1.1  christos       if (!isalnum (*p) && *p != '-' && *p != '_')
   1442  1.1  christos 	error (_("Junk in argument list: \"%s\""), p);
   1443  1.1  christos       p++;
   1444  1.1  christos     }
   1445  1.1  christos 
   1446  1.1  christos   return list;
   1447  1.1  christos }
   1448  1.1  christos 
   1449  1.1  christos /* This is just a placeholder in the command data structures.  */
   1450  1.1  christos static void
   1451  1.1  christos user_defined_command (char *ignore, int from_tty)
   1452  1.1  christos {
   1453  1.1  christos }
   1454  1.1  christos 
   1455  1.1  christos static void
   1456  1.1  christos define_command (char *comname, int from_tty)
   1457  1.1  christos {
   1458  1.1  christos #define MAX_TMPBUF 128
   1459  1.1  christos   enum cmd_hook_type
   1460  1.1  christos     {
   1461  1.1  christos       CMD_NO_HOOK = 0,
   1462  1.1  christos       CMD_PRE_HOOK,
   1463  1.1  christos       CMD_POST_HOOK
   1464  1.1  christos     };
   1465  1.1  christos   struct command_line *cmds;
   1466  1.1  christos   struct cmd_list_element *c, *newc, *hookc = 0, **list;
   1467  1.1  christos   char *tem, *comfull;
   1468  1.1  christos   const char *tem_c;
   1469  1.1  christos   char tmpbuf[MAX_TMPBUF];
   1470  1.1  christos   int  hook_type      = CMD_NO_HOOK;
   1471  1.1  christos   int  hook_name_size = 0;
   1472  1.1  christos 
   1473  1.1  christos #define	HOOK_STRING	"hook-"
   1474  1.1  christos #define	HOOK_LEN 5
   1475  1.1  christos #define HOOK_POST_STRING "hookpost-"
   1476  1.1  christos #define HOOK_POST_LEN    9
   1477  1.1  christos 
   1478  1.1  christos   comfull = comname;
   1479  1.1  christos   list = validate_comname (&comname);
   1480  1.1  christos 
   1481  1.1  christos   /* Look it up, and verify that we got an exact match.  */
   1482  1.1  christos   tem_c = comname;
   1483  1.1  christos   c = lookup_cmd (&tem_c, *list, "", -1, 1);
   1484  1.1  christos   if (c && strcmp (comname, c->name) != 0)
   1485  1.1  christos     c = 0;
   1486  1.1  christos 
   1487  1.1  christos   if (c)
   1488  1.1  christos     {
   1489  1.1  christos       int q;
   1490  1.1  christos 
   1491  1.1  christos       if (c->class == class_user || c->class == class_alias)
   1492  1.1  christos 	q = query (_("Redefine command \"%s\"? "), c->name);
   1493  1.1  christos       else
   1494  1.1  christos 	q = query (_("Really redefine built-in command \"%s\"? "), c->name);
   1495  1.1  christos       if (!q)
   1496  1.1  christos 	error (_("Command \"%s\" not redefined."), c->name);
   1497  1.1  christos     }
   1498  1.1  christos 
   1499  1.1  christos   /* If this new command is a hook, then mark the command which it
   1500  1.1  christos      is hooking.  Note that we allow hooking `help' commands, so that
   1501  1.1  christos      we can hook the `stop' pseudo-command.  */
   1502  1.1  christos 
   1503  1.1  christos   if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
   1504  1.1  christos     {
   1505  1.1  christos        hook_type      = CMD_PRE_HOOK;
   1506  1.1  christos        hook_name_size = HOOK_LEN;
   1507  1.1  christos     }
   1508  1.1  christos   else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
   1509  1.1  christos     {
   1510  1.1  christos       hook_type      = CMD_POST_HOOK;
   1511  1.1  christos       hook_name_size = HOOK_POST_LEN;
   1512  1.1  christos     }
   1513  1.1  christos 
   1514  1.1  christos   if (hook_type != CMD_NO_HOOK)
   1515  1.1  christos     {
   1516  1.1  christos       /* Look up cmd it hooks, and verify that we got an exact match.  */
   1517  1.1  christos       tem_c = comname + hook_name_size;
   1518  1.1  christos       hookc = lookup_cmd (&tem_c, *list, "", -1, 0);
   1519  1.1  christos       if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
   1520  1.1  christos 	hookc = 0;
   1521  1.1  christos       if (!hookc)
   1522  1.1  christos 	{
   1523  1.1  christos 	  warning (_("Your new `%s' command does not "
   1524  1.1  christos 		     "hook any existing command."),
   1525  1.1  christos 		   comfull);
   1526  1.1  christos 	  if (!query (_("Proceed? ")))
   1527  1.1  christos 	    error (_("Not confirmed."));
   1528  1.1  christos 	}
   1529  1.1  christos     }
   1530  1.1  christos 
   1531  1.1  christos   comname = xstrdup (comname);
   1532  1.1  christos 
   1533  1.1  christos   /* If the rest of the commands will be case insensitive, this one
   1534  1.1  christos      should behave in the same manner.  */
   1535  1.1  christos   for (tem = comname; *tem; tem++)
   1536  1.1  christos     if (isupper (*tem))
   1537  1.1  christos       *tem = tolower (*tem);
   1538  1.1  christos 
   1539  1.1  christos   xsnprintf (tmpbuf, sizeof (tmpbuf),
   1540  1.1  christos 	     "Type commands for definition of \"%s\".", comfull);
   1541  1.1  christos   cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
   1542  1.1  christos 
   1543  1.1  christos   if (c && c->class == class_user)
   1544  1.1  christos     free_command_lines (&c->user_commands);
   1545  1.1  christos 
   1546  1.1  christos   newc = add_cmd (comname, class_user, user_defined_command,
   1547  1.1  christos 		  (c && c->class == class_user)
   1548  1.1  christos 		  ? c->doc : xstrdup ("User-defined."), list);
   1549  1.1  christos   newc->user_commands = cmds;
   1550  1.1  christos 
   1551  1.1  christos   /* If this new command is a hook, then mark both commands as being
   1552  1.1  christos      tied.  */
   1553  1.1  christos   if (hookc)
   1554  1.1  christos     {
   1555  1.1  christos       switch (hook_type)
   1556  1.1  christos         {
   1557  1.1  christos         case CMD_PRE_HOOK:
   1558  1.1  christos           hookc->hook_pre  = newc;  /* Target gets hooked.  */
   1559  1.1  christos           newc->hookee_pre = hookc; /* We are marked as hooking target cmd.  */
   1560  1.1  christos           break;
   1561  1.1  christos         case CMD_POST_HOOK:
   1562  1.1  christos           hookc->hook_post  = newc;  /* Target gets hooked.  */
   1563  1.1  christos           newc->hookee_post = hookc; /* We are marked as hooking
   1564  1.1  christos 					target cmd.  */
   1565  1.1  christos           break;
   1566  1.1  christos         default:
   1567  1.1  christos           /* Should never come here as hookc would be 0.  */
   1568  1.1  christos 	  internal_error (__FILE__, __LINE__, _("bad switch"));
   1569  1.1  christos         }
   1570  1.1  christos     }
   1571  1.1  christos }
   1572  1.1  christos 
   1573  1.1  christos static void
   1574  1.1  christos document_command (char *comname, int from_tty)
   1575  1.1  christos {
   1576  1.1  christos   struct command_line *doclines;
   1577  1.1  christos   struct cmd_list_element *c, **list;
   1578  1.1  christos   const char *tem;
   1579  1.1  christos   char *comfull;
   1580  1.1  christos   char tmpbuf[128];
   1581  1.1  christos 
   1582  1.1  christos   comfull = comname;
   1583  1.1  christos   list = validate_comname (&comname);
   1584  1.1  christos 
   1585  1.1  christos   tem = comname;
   1586  1.1  christos   c = lookup_cmd (&tem, *list, "", 0, 1);
   1587  1.1  christos 
   1588  1.1  christos   if (c->class != class_user)
   1589  1.1  christos     error (_("Command \"%s\" is built-in."), comfull);
   1590  1.1  christos 
   1591  1.1  christos   xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
   1592  1.1  christos 	     comfull);
   1593  1.1  christos   doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
   1594  1.1  christos 
   1595  1.1  christos   if (c->doc)
   1596  1.1  christos     xfree (c->doc);
   1597  1.1  christos 
   1598  1.1  christos   {
   1599  1.1  christos     struct command_line *cl1;
   1600  1.1  christos     int len = 0;
   1601  1.1  christos 
   1602  1.1  christos     for (cl1 = doclines; cl1; cl1 = cl1->next)
   1603  1.1  christos       len += strlen (cl1->line) + 1;
   1604  1.1  christos 
   1605  1.1  christos     c->doc = (char *) xmalloc (len + 1);
   1606  1.1  christos     *c->doc = 0;
   1607  1.1  christos 
   1608  1.1  christos     for (cl1 = doclines; cl1; cl1 = cl1->next)
   1609  1.1  christos       {
   1610  1.1  christos 	strcat (c->doc, cl1->line);
   1611  1.1  christos 	if (cl1->next)
   1612  1.1  christos 	  strcat (c->doc, "\n");
   1613  1.1  christos       }
   1614  1.1  christos   }
   1615  1.1  christos 
   1616  1.1  christos   free_command_lines (&doclines);
   1617  1.1  christos }
   1618  1.1  christos 
   1619  1.1  christos struct source_cleanup_lines_args
   1621  1.1  christos {
   1622  1.1  christos   int old_line;
   1623  1.1  christos   const char *old_file;
   1624  1.1  christos };
   1625  1.1  christos 
   1626  1.1  christos static void
   1627  1.1  christos source_cleanup_lines (void *args)
   1628  1.1  christos {
   1629  1.1  christos   struct source_cleanup_lines_args *p =
   1630  1.1  christos     (struct source_cleanup_lines_args *) args;
   1631  1.1  christos 
   1632  1.1  christos   source_line_number = p->old_line;
   1633  1.1  christos   source_file_name = p->old_file;
   1634  1.1  christos }
   1635  1.1  christos 
   1636  1.1  christos /* Used to implement source_command.  */
   1637  1.1  christos 
   1638  1.1  christos void
   1639  1.1  christos script_from_file (FILE *stream, const char *file)
   1640  1.1  christos {
   1641  1.1  christos   struct cleanup *old_cleanups;
   1642  1.1  christos   struct source_cleanup_lines_args old_lines;
   1643  1.1  christos 
   1644  1.1  christos   if (stream == NULL)
   1645  1.1  christos     internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
   1646  1.1  christos 
   1647  1.1  christos   old_lines.old_line = source_line_number;
   1648  1.1  christos   old_lines.old_file = source_file_name;
   1649  1.1  christos   old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
   1650  1.1  christos   source_line_number = 0;
   1651  1.1  christos   source_file_name = file;
   1652  1.1  christos 
   1653  1.1  christos   {
   1654  1.1  christos     volatile struct gdb_exception e;
   1655  1.1  christos 
   1656  1.1  christos     TRY_CATCH (e, RETURN_MASK_ERROR)
   1657  1.1  christos       {
   1658  1.1  christos 	read_command_file (stream);
   1659  1.1  christos       }
   1660  1.1  christos     switch (e.reason)
   1661  1.1  christos       {
   1662  1.1  christos       case 0:
   1663  1.1  christos 	break;
   1664  1.1  christos       case RETURN_ERROR:
   1665  1.1  christos 	/* Re-throw the error, but with the file name information
   1666  1.1  christos 	   prepended.  */
   1667  1.1  christos 	throw_error (e.error,
   1668  1.1  christos 		     _("%s:%d: Error in sourced command file:\n%s"),
   1669  1.1  christos 		     source_file_name, source_line_number, e.message);
   1670  1.1  christos       default:
   1671  1.1  christos 	internal_error (__FILE__, __LINE__, _("bad reason"));
   1672  1.1  christos       }
   1673  1.1  christos   }
   1674  1.1  christos 
   1675  1.1  christos   do_cleanups (old_cleanups);
   1676  1.1  christos }
   1677  1.1  christos 
   1678  1.1  christos /* Print the definition of user command C to STREAM.  Or, if C is a
   1679  1.1  christos    prefix command, show the definitions of all user commands under C
   1680  1.1  christos    (recursively).  PREFIX and NAME combined are the name of the
   1681  1.1  christos    current command.  */
   1682  1.1  christos void
   1683  1.1  christos show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
   1684  1.1  christos 	     struct ui_file *stream)
   1685  1.1  christos {
   1686  1.1  christos   struct command_line *cmdlines;
   1687  1.1  christos 
   1688  1.1  christos   if (c->prefixlist != NULL)
   1689  1.1  christos     {
   1690  1.1  christos       char *prefixname = c->prefixname;
   1691  1.1  christos 
   1692  1.1  christos       for (c = *c->prefixlist; c != NULL; c = c->next)
   1693  1.1  christos 	if (c->class == class_user || c->prefixlist != NULL)
   1694  1.1  christos 	  show_user_1 (c, prefixname, c->name, gdb_stdout);
   1695  1.1  christos       return;
   1696  1.1  christos     }
   1697  1.1  christos 
   1698  1.1  christos   cmdlines = c->user_commands;
   1699  1.1  christos   if (!cmdlines)
   1700  1.1  christos     return;
   1701  1.1  christos   fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
   1702  1.1  christos 
   1703  1.1  christos   print_command_lines (current_uiout, cmdlines, 1);
   1704  1.1  christos   fputs_filtered ("\n", stream);
   1705  1.1  christos }
   1706  1.1  christos 
   1707  1.1  christos 
   1708  1.1  christos 
   1710  1.1  christos initialize_file_ftype _initialize_cli_script;
   1711  1.1  christos 
   1712  1.1  christos void
   1713  1.1  christos _initialize_cli_script (void)
   1714  1.1  christos {
   1715  1.1  christos   add_com ("document", class_support, document_command, _("\
   1716  1.1  christos Document a user-defined command.\n\
   1717  1.1  christos Give command name as argument.  Give documentation on following lines.\n\
   1718  1.1  christos End with a line of just \"end\"."));
   1719  1.1  christos   add_com ("define", class_support, define_command, _("\
   1720  1.1  christos Define a new command name.  Command name is argument.\n\
   1721  1.1  christos Definition appears on following lines, one command per line.\n\
   1722  1.1  christos End with a line of just \"end\".\n\
   1723  1.1  christos Use the \"document\" command to give documentation for the new command.\n\
   1724  1.1  christos Commands defined in this way may have up to ten arguments."));
   1725  1.1  christos 
   1726  1.1  christos   add_com ("while", class_support, while_command, _("\
   1727  1.1  christos Execute nested commands WHILE the conditional expression is non zero.\n\
   1728  1.1  christos The conditional expression must follow the word `while' and must in turn be\n\
   1729  1.1  christos followed by a new line.  The nested commands must be entered one per line,\n\
   1730  1.1  christos and should be terminated by the word `end'."));
   1731  1.1  christos 
   1732  1.1  christos   add_com ("if", class_support, if_command, _("\
   1733  1.1  christos Execute nested commands once IF the conditional expression is non zero.\n\
   1734                The conditional expression must follow the word `if' and must in turn be\n\
   1735                followed by a new line.  The nested commands must be entered one per line,\n\
   1736                and should be terminated by the word 'else' or `end'.  If an else clause\n\
   1737                is used, the same rules apply to its nested commands as to the first ones."));
   1738                }
   1739