OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [gdb/] [cli/] [cli-script.c] - Diff between revs 834 and 842

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 834 Rev 842
/* GDB CLI command scripting.
/* GDB CLI command scripting.
 
 
   Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
   Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008,
   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008,
   2009, 2010 Free Software Foundation, Inc.
   2009, 2010 Free Software Foundation, Inc.
 
 
   This file is part of GDB.
   This file is part of GDB.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
#include "defs.h"
#include "defs.h"
#include "value.h"
#include "value.h"
#include "language.h"           /* For value_true */
#include "language.h"           /* For value_true */
#include <ctype.h>
#include <ctype.h>
 
 
#include "ui-out.h"
#include "ui-out.h"
#include "gdb_string.h"
#include "gdb_string.h"
#include "exceptions.h"
#include "exceptions.h"
#include "top.h"
#include "top.h"
#include "breakpoint.h"
#include "breakpoint.h"
#include "cli/cli-cmds.h"
#include "cli/cli-cmds.h"
#include "cli/cli-decode.h"
#include "cli/cli-decode.h"
#include "cli/cli-script.h"
#include "cli/cli-script.h"
#include "gdb_assert.h"
#include "gdb_assert.h"
 
 
#include "python/python.h"
#include "python/python.h"
 
 
/* Prototypes for local functions */
/* Prototypes for local functions */
 
 
static enum command_control_type
static enum command_control_type
recurse_read_control_structure (char * (*read_next_line_func) (),
recurse_read_control_structure (char * (*read_next_line_func) (),
                                struct command_line *current_cmd);
                                struct command_line *current_cmd);
 
 
static char *insert_args (char *line);
static char *insert_args (char *line);
 
 
static struct cleanup * setup_user_args (char *p);
static struct cleanup * setup_user_args (char *p);
 
 
static char *read_next_line ();
static char *read_next_line ();
 
 
/* Level of control structure when reading.  */
/* Level of control structure when reading.  */
static int control_level;
static int control_level;
 
 
/* Level of control structure when executing.  */
/* Level of control structure when executing.  */
static int command_nest_depth = 1;
static int command_nest_depth = 1;
 
 
/* This is to prevent certain commands being printed twice.  */
/* This is to prevent certain commands being printed twice.  */
static int suppress_next_print_command_trace = 0;
static int suppress_next_print_command_trace = 0;
 
 
/* Structure for arguments to user defined functions.  */
/* Structure for arguments to user defined functions.  */
#define MAXUSERARGS 10
#define MAXUSERARGS 10
struct user_args
struct user_args
  {
  {
    struct user_args *next;
    struct user_args *next;
    /* It is necessary to store a malloced copy of the command line to
    /* It is necessary to store a malloced copy of the command line to
       ensure that the arguments are not overwritten before they are used.  */
       ensure that the arguments are not overwritten before they are used.  */
    char *command;
    char *command;
    struct
    struct
      {
      {
        char *arg;
        char *arg;
        int len;
        int len;
      }
      }
    a[MAXUSERARGS];
    a[MAXUSERARGS];
    int count;
    int count;
  }
  }
 *user_args;
 *user_args;
 
 


/* Allocate, initialize a new command line structure for one of the
/* Allocate, initialize a new command line structure for one of the
   control commands (if/while).  */
   control commands (if/while).  */
 
 
static struct command_line *
static struct command_line *
build_command_line (enum command_control_type type, char *args)
build_command_line (enum command_control_type type, char *args)
{
{
  struct command_line *cmd;
  struct command_line *cmd;
 
 
  if (args == NULL && (type == if_control || type == while_control))
  if (args == NULL && (type == if_control || type == while_control))
    error (_("if/while commands require arguments."));
    error (_("if/while commands require arguments."));
  gdb_assert (args != NULL);
  gdb_assert (args != NULL);
 
 
  cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
  cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
  cmd->next = NULL;
  cmd->next = NULL;
  cmd->control_type = type;
  cmd->control_type = type;
 
 
  cmd->body_count = 1;
  cmd->body_count = 1;
  cmd->body_list
  cmd->body_list
    = (struct command_line **) xmalloc (sizeof (struct command_line *)
    = (struct command_line **) xmalloc (sizeof (struct command_line *)
                                        * cmd->body_count);
                                        * cmd->body_count);
  memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
  memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
  cmd->line = xstrdup (args);
  cmd->line = xstrdup (args);
 
 
  return cmd;
  return cmd;
}
}
 
 
/* Build and return a new command structure for the control commands
/* Build and return a new command structure for the control commands
   such as "if" and "while".  */
   such as "if" and "while".  */
 
 
struct command_line *
struct command_line *
get_command_line (enum command_control_type type, char *arg)
get_command_line (enum command_control_type type, char *arg)
{
{
  struct command_line *cmd;
  struct command_line *cmd;
  struct cleanup *old_chain = NULL;
  struct cleanup *old_chain = NULL;
 
 
  /* Allocate and build a new command line structure.  */
  /* Allocate and build a new command line structure.  */
  cmd = build_command_line (type, arg);
  cmd = build_command_line (type, arg);
 
 
  old_chain = make_cleanup_free_command_lines (&cmd);
  old_chain = make_cleanup_free_command_lines (&cmd);
 
 
  /* Read in the body of this command.  */
  /* Read in the body of this command.  */
  if (recurse_read_control_structure (read_next_line, cmd) == invalid_control)
  if (recurse_read_control_structure (read_next_line, cmd) == invalid_control)
    {
    {
      warning (_("Error reading in canned sequence of commands."));
      warning (_("Error reading in canned sequence of commands."));
      do_cleanups (old_chain);
      do_cleanups (old_chain);
      return NULL;
      return NULL;
    }
    }
 
 
  discard_cleanups (old_chain);
  discard_cleanups (old_chain);
  return cmd;
  return cmd;
}
}
 
 
/* Recursively print a command (including full control structures).  */
/* Recursively print a command (including full control structures).  */
 
 
void
void
print_command_lines (struct ui_out *uiout, struct command_line *cmd,
print_command_lines (struct ui_out *uiout, struct command_line *cmd,
                     unsigned int depth)
                     unsigned int depth)
{
{
  struct command_line *list;
  struct command_line *list;
 
 
  list = cmd;
  list = cmd;
  while (list)
  while (list)
    {
    {
 
 
      if (depth)
      if (depth)
        ui_out_spaces (uiout, 2 * depth);
        ui_out_spaces (uiout, 2 * depth);
 
 
      /* A simple command, print it and continue.  */
      /* A simple command, print it and continue.  */
      if (list->control_type == simple_control)
      if (list->control_type == simple_control)
        {
        {
          ui_out_field_string (uiout, NULL, list->line);
          ui_out_field_string (uiout, NULL, list->line);
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          list = list->next;
          list = list->next;
          continue;
          continue;
        }
        }
 
 
      /* loop_continue to jump to the start of a while loop, print it
      /* loop_continue to jump to the start of a while loop, print it
         and continue. */
         and continue. */
      if (list->control_type == continue_control)
      if (list->control_type == continue_control)
        {
        {
          ui_out_field_string (uiout, NULL, "loop_continue");
          ui_out_field_string (uiout, NULL, "loop_continue");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          list = list->next;
          list = list->next;
          continue;
          continue;
        }
        }
 
 
      /* loop_break to break out of a while loop, print it and continue.  */
      /* loop_break to break out of a while loop, print it and continue.  */
      if (list->control_type == break_control)
      if (list->control_type == break_control)
        {
        {
          ui_out_field_string (uiout, NULL, "loop_break");
          ui_out_field_string (uiout, NULL, "loop_break");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          list = list->next;
          list = list->next;
          continue;
          continue;
        }
        }
 
 
      /* A while command.  Recursively print its subcommands and continue.  */
      /* A while command.  Recursively print its subcommands and continue.  */
      if (list->control_type == while_control)
      if (list->control_type == while_control)
        {
        {
          ui_out_field_fmt (uiout, NULL, "while %s", list->line);
          ui_out_field_fmt (uiout, NULL, "while %s", list->line);
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          print_command_lines (uiout, *list->body_list, depth + 1);
          print_command_lines (uiout, *list->body_list, depth + 1);
          if (depth)
          if (depth)
            ui_out_spaces (uiout, 2 * depth);
            ui_out_spaces (uiout, 2 * depth);
          ui_out_field_string (uiout, NULL, "end");
          ui_out_field_string (uiout, NULL, "end");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          list = list->next;
          list = list->next;
          continue;
          continue;
        }
        }
 
 
      /* An if command.  Recursively print both arms before continueing.  */
      /* An if command.  Recursively print both arms before continueing.  */
      if (list->control_type == if_control)
      if (list->control_type == if_control)
        {
        {
          ui_out_field_fmt (uiout, NULL, "if %s", list->line);
          ui_out_field_fmt (uiout, NULL, "if %s", list->line);
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          /* The true arm. */
          /* The true arm. */
          print_command_lines (uiout, list->body_list[0], depth + 1);
          print_command_lines (uiout, list->body_list[0], depth + 1);
 
 
          /* Show the false arm if it exists.  */
          /* Show the false arm if it exists.  */
          if (list->body_count == 2)
          if (list->body_count == 2)
            {
            {
              if (depth)
              if (depth)
                ui_out_spaces (uiout, 2 * depth);
                ui_out_spaces (uiout, 2 * depth);
              ui_out_field_string (uiout, NULL, "else");
              ui_out_field_string (uiout, NULL, "else");
              ui_out_text (uiout, "\n");
              ui_out_text (uiout, "\n");
              print_command_lines (uiout, list->body_list[1], depth + 1);
              print_command_lines (uiout, list->body_list[1], depth + 1);
            }
            }
 
 
          if (depth)
          if (depth)
            ui_out_spaces (uiout, 2 * depth);
            ui_out_spaces (uiout, 2 * depth);
          ui_out_field_string (uiout, NULL, "end");
          ui_out_field_string (uiout, NULL, "end");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          list = list->next;
          list = list->next;
          continue;
          continue;
        }
        }
 
 
      /* A commands command.  Print the breakpoint commands and continue.  */
      /* A commands command.  Print the breakpoint commands and continue.  */
      if (list->control_type == commands_control)
      if (list->control_type == commands_control)
        {
        {
          if (*(list->line))
          if (*(list->line))
            ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
            ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
          else
          else
            ui_out_field_string (uiout, NULL, "commands");
            ui_out_field_string (uiout, NULL, "commands");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          print_command_lines (uiout, *list->body_list, depth + 1);
          print_command_lines (uiout, *list->body_list, depth + 1);
          if (depth)
          if (depth)
            ui_out_spaces (uiout, 2 * depth);
            ui_out_spaces (uiout, 2 * depth);
          ui_out_field_string (uiout, NULL, "end");
          ui_out_field_string (uiout, NULL, "end");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          list = list->next;
          list = list->next;
          continue;
          continue;
        }
        }
 
 
      if (list->control_type == python_control)
      if (list->control_type == python_control)
        {
        {
          ui_out_field_string (uiout, NULL, "python");
          ui_out_field_string (uiout, NULL, "python");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          /* Don't indent python code at all.  */
          /* Don't indent python code at all.  */
          print_command_lines (uiout, *list->body_list, 0);
          print_command_lines (uiout, *list->body_list, 0);
          if (depth)
          if (depth)
            ui_out_spaces (uiout, 2 * depth);
            ui_out_spaces (uiout, 2 * depth);
          ui_out_field_string (uiout, NULL, "end");
          ui_out_field_string (uiout, NULL, "end");
          ui_out_text (uiout, "\n");
          ui_out_text (uiout, "\n");
          list = list->next;
          list = list->next;
          continue;
          continue;
        }
        }
 
 
      /* ignore illegal command type and try next */
      /* ignore illegal command type and try next */
      list = list->next;
      list = list->next;
    }                           /* while (list) */
    }                           /* while (list) */
}
}
 
 
/* Handle pre-post hooks.  */
/* Handle pre-post hooks.  */
 
 
static void
static void
clear_hook_in_cleanup (void *data)
clear_hook_in_cleanup (void *data)
{
{
  struct cmd_list_element *c = data;
  struct cmd_list_element *c = data;
  c->hook_in = 0; /* Allow hook to work again once it is complete */
  c->hook_in = 0; /* Allow hook to work again once it is complete */
}
}
 
 
void
void
execute_cmd_pre_hook (struct cmd_list_element *c)
execute_cmd_pre_hook (struct cmd_list_element *c)
{
{
  if ((c->hook_pre) && (!c->hook_in))
  if ((c->hook_pre) && (!c->hook_in))
    {
    {
      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
      c->hook_in = 1; /* Prevent recursive hooking */
      c->hook_in = 1; /* Prevent recursive hooking */
      execute_user_command (c->hook_pre, (char *) 0);
      execute_user_command (c->hook_pre, (char *) 0);
      do_cleanups (cleanups);
      do_cleanups (cleanups);
    }
    }
}
}
 
 
void
void
execute_cmd_post_hook (struct cmd_list_element *c)
execute_cmd_post_hook (struct cmd_list_element *c)
{
{
  if ((c->hook_post) && (!c->hook_in))
  if ((c->hook_post) && (!c->hook_in))
    {
    {
      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
      c->hook_in = 1; /* Prevent recursive hooking */
      c->hook_in = 1; /* Prevent recursive hooking */
      execute_user_command (c->hook_post, (char *) 0);
      execute_user_command (c->hook_post, (char *) 0);
      do_cleanups (cleanups);
      do_cleanups (cleanups);
    }
    }
}
}
 
 
/* Execute the command in CMD.  */
/* Execute the command in CMD.  */
static void
static void
do_restore_user_call_depth (void * call_depth)
do_restore_user_call_depth (void * call_depth)
{
{
  int * depth = call_depth;
  int * depth = call_depth;
  (*depth)--;
  (*depth)--;
  if ((*depth) == 0)
  if ((*depth) == 0)
    in_user_command = 0;
    in_user_command = 0;
}
}
 
 
 
 
void
void
execute_user_command (struct cmd_list_element *c, char *args)
execute_user_command (struct cmd_list_element *c, char *args)
{
{
  struct command_line *cmdlines;
  struct command_line *cmdlines;
  struct cleanup *old_chain;
  struct cleanup *old_chain;
  enum command_control_type ret;
  enum command_control_type ret;
  static int user_call_depth = 0;
  static int user_call_depth = 0;
  extern int max_user_call_depth;
  extern int max_user_call_depth;
 
 
  old_chain = setup_user_args (args);
  old_chain = setup_user_args (args);
 
 
  cmdlines = c->user_commands;
  cmdlines = c->user_commands;
  if (cmdlines == 0)
  if (cmdlines == 0)
    /* Null command */
    /* Null command */
    return;
    return;
 
 
  if (++user_call_depth > max_user_call_depth)
  if (++user_call_depth > max_user_call_depth)
    error (_("Max user call depth exceeded -- command aborted."));
    error (_("Max user call depth exceeded -- command aborted."));
 
 
  make_cleanup (do_restore_user_call_depth, &user_call_depth);
  make_cleanup (do_restore_user_call_depth, &user_call_depth);
 
 
  /* Set the instream to 0, indicating execution of a
  /* Set the instream to 0, indicating execution of a
     user-defined function.  */
     user-defined function.  */
  make_cleanup (do_restore_instream_cleanup, instream);
  make_cleanup (do_restore_instream_cleanup, instream);
  instream = (FILE *) 0;
  instream = (FILE *) 0;
 
 
  /* Also set the global in_user_command, so that NULL instream is
  /* Also set the global in_user_command, so that NULL instream is
     not confused with Insight.  */
     not confused with Insight.  */
  in_user_command = 1;
  in_user_command = 1;
 
 
  command_nest_depth++;
  command_nest_depth++;
  while (cmdlines)
  while (cmdlines)
    {
    {
      ret = execute_control_command (cmdlines);
      ret = execute_control_command (cmdlines);
      if (ret != simple_control && ret != break_control)
      if (ret != simple_control && ret != break_control)
        {
        {
          warning (_("Error executing canned sequence of commands."));
          warning (_("Error executing canned sequence of commands."));
          break;
          break;
        }
        }
      cmdlines = cmdlines->next;
      cmdlines = cmdlines->next;
    }
    }
  command_nest_depth--;
  command_nest_depth--;
  do_cleanups (old_chain);
  do_cleanups (old_chain);
}
}
 
 
/* This function is called every time GDB prints a prompt.
/* This function is called every time GDB prints a prompt.
   It ensures that errors and the like to not confuse the command tracing.  */
   It ensures that errors and the like to not confuse the command tracing.  */
 
 
void
void
reset_command_nest_depth (void)
reset_command_nest_depth (void)
{
{
  command_nest_depth = 1;
  command_nest_depth = 1;
 
 
  /* Just in case.  */
  /* Just in case.  */
  suppress_next_print_command_trace = 0;
  suppress_next_print_command_trace = 0;
}
}
 
 
/* Print the command, prefixed with '+' to represent the call depth.
/* Print the command, prefixed with '+' to represent the call depth.
   This is slightly complicated because this function may be called
   This is slightly complicated because this function may be called
   from execute_command and execute_control_command.  Unfortunately
   from execute_command and execute_control_command.  Unfortunately
   execute_command also prints the top level control commands.
   execute_command also prints the top level control commands.
   In these cases execute_command will call execute_control_command
   In these cases execute_command will call execute_control_command
   via while_command or if_command.  Inner levels of 'if' and 'while'
   via while_command or if_command.  Inner levels of 'if' and 'while'
   are dealt with directly.  Therefore we can use these functions
   are dealt with directly.  Therefore we can use these functions
   to determine whether the command has been printed already or not.  */
   to determine whether the command has been printed already or not.  */
void
void
print_command_trace (const char *cmd)
print_command_trace (const char *cmd)
{
{
  int i;
  int i;
 
 
  if (suppress_next_print_command_trace)
  if (suppress_next_print_command_trace)
    {
    {
      suppress_next_print_command_trace = 0;
      suppress_next_print_command_trace = 0;
      return;
      return;
    }
    }
 
 
  if (!source_verbose && !trace_commands)
  if (!source_verbose && !trace_commands)
    return;
    return;
 
 
  for (i=0; i < command_nest_depth; i++)
  for (i=0; i < command_nest_depth; i++)
    printf_filtered ("+");
    printf_filtered ("+");
 
 
  printf_filtered ("%s\n", cmd);
  printf_filtered ("%s\n", cmd);
}
}
 
 
enum command_control_type
enum command_control_type
execute_control_command (struct command_line *cmd)
execute_control_command (struct command_line *cmd)
{
{
  struct expression *expr;
  struct expression *expr;
  struct command_line *current;
  struct command_line *current;
  struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
  struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
  struct value *val;
  struct value *val;
  struct value *val_mark;
  struct value *val_mark;
  int loop;
  int loop;
  enum command_control_type ret;
  enum command_control_type ret;
  char *new_line;
  char *new_line;
 
 
  /* Start by assuming failure, if a problem is detected, the code
  /* Start by assuming failure, if a problem is detected, the code
     below will simply "break" out of the switch.  */
     below will simply "break" out of the switch.  */
  ret = invalid_control;
  ret = invalid_control;
 
 
  switch (cmd->control_type)
  switch (cmd->control_type)
    {
    {
    case simple_control:
    case simple_control:
      /* A simple command, execute it and return.  */
      /* A simple command, execute it and return.  */
      new_line = insert_args (cmd->line);
      new_line = insert_args (cmd->line);
      if (!new_line)
      if (!new_line)
        break;
        break;
      make_cleanup (free_current_contents, &new_line);
      make_cleanup (free_current_contents, &new_line);
      execute_command (new_line, 0);
      execute_command (new_line, 0);
      ret = cmd->control_type;
      ret = cmd->control_type;
      break;
      break;
 
 
    case continue_control:
    case continue_control:
      print_command_trace ("loop_continue");
      print_command_trace ("loop_continue");
 
 
      /* Return for "continue", and "break" so we can either
      /* Return for "continue", and "break" so we can either
         continue the loop at the top, or break out.  */
         continue the loop at the top, or break out.  */
      ret = cmd->control_type;
      ret = cmd->control_type;
      break;
      break;
 
 
    case break_control:
    case break_control:
      print_command_trace ("loop_break");
      print_command_trace ("loop_break");
 
 
      /* Return for "continue", and "break" so we can either
      /* Return for "continue", and "break" so we can either
         continue the loop at the top, or break out.  */
         continue the loop at the top, or break out.  */
      ret = cmd->control_type;
      ret = cmd->control_type;
      break;
      break;
 
 
    case while_control:
    case while_control:
      {
      {
        char *buffer = alloca (strlen (cmd->line) + 7);
        char *buffer = alloca (strlen (cmd->line) + 7);
        sprintf (buffer, "while %s", cmd->line);
        sprintf (buffer, "while %s", cmd->line);
        print_command_trace (buffer);
        print_command_trace (buffer);
 
 
        /* Parse the loop control expression for the while statement.  */
        /* Parse the loop control expression for the while statement.  */
        new_line = insert_args (cmd->line);
        new_line = insert_args (cmd->line);
        if (!new_line)
        if (!new_line)
          break;
          break;
        make_cleanup (free_current_contents, &new_line);
        make_cleanup (free_current_contents, &new_line);
        expr = parse_expression (new_line);
        expr = parse_expression (new_line);
        make_cleanup (free_current_contents, &expr);
        make_cleanup (free_current_contents, &expr);
 
 
        ret = simple_control;
        ret = simple_control;
        loop = 1;
        loop = 1;
 
 
        /* Keep iterating so long as the expression is true.  */
        /* Keep iterating so long as the expression is true.  */
        while (loop == 1)
        while (loop == 1)
          {
          {
            int cond_result;
            int cond_result;
 
 
            QUIT;
            QUIT;
 
 
            /* Evaluate the expression.  */
            /* Evaluate the expression.  */
            val_mark = value_mark ();
            val_mark = value_mark ();
            val = evaluate_expression (expr);
            val = evaluate_expression (expr);
            cond_result = value_true (val);
            cond_result = value_true (val);
            value_free_to_mark (val_mark);
            value_free_to_mark (val_mark);
 
 
            /* If the value is false, then break out of the loop.  */
            /* If the value is false, then break out of the loop.  */
            if (!cond_result)
            if (!cond_result)
              break;
              break;
 
 
            /* Execute the body of the while statement.  */
            /* Execute the body of the while statement.  */
            current = *cmd->body_list;
            current = *cmd->body_list;
            while (current)
            while (current)
              {
              {
                command_nest_depth++;
                command_nest_depth++;
                ret = execute_control_command (current);
                ret = execute_control_command (current);
                command_nest_depth--;
                command_nest_depth--;
 
 
                /* If we got an error, or a "break" command, then stop
                /* If we got an error, or a "break" command, then stop
                   looping.  */
                   looping.  */
                if (ret == invalid_control || ret == break_control)
                if (ret == invalid_control || ret == break_control)
                  {
                  {
                    loop = 0;
                    loop = 0;
                    break;
                    break;
                  }
                  }
 
 
                /* If we got a "continue" command, then restart the loop
                /* If we got a "continue" command, then restart the loop
                   at this point.  */
                   at this point.  */
                if (ret == continue_control)
                if (ret == continue_control)
                  break;
                  break;
 
 
                /* Get the next statement.  */
                /* Get the next statement.  */
                current = current->next;
                current = current->next;
              }
              }
          }
          }
 
 
        /* Reset RET so that we don't recurse the break all the way down.  */
        /* Reset RET so that we don't recurse the break all the way down.  */
        if (ret == break_control)
        if (ret == break_control)
          ret = simple_control;
          ret = simple_control;
 
 
        break;
        break;
      }
      }
 
 
    case if_control:
    case if_control:
      {
      {
        char *buffer = alloca (strlen (cmd->line) + 4);
        char *buffer = alloca (strlen (cmd->line) + 4);
        sprintf (buffer, "if %s", cmd->line);
        sprintf (buffer, "if %s", cmd->line);
        print_command_trace (buffer);
        print_command_trace (buffer);
 
 
        new_line = insert_args (cmd->line);
        new_line = insert_args (cmd->line);
        if (!new_line)
        if (!new_line)
          break;
          break;
        make_cleanup (free_current_contents, &new_line);
        make_cleanup (free_current_contents, &new_line);
        /* Parse the conditional for the if statement.  */
        /* Parse the conditional for the if statement.  */
        expr = parse_expression (new_line);
        expr = parse_expression (new_line);
        make_cleanup (free_current_contents, &expr);
        make_cleanup (free_current_contents, &expr);
 
 
        current = NULL;
        current = NULL;
        ret = simple_control;
        ret = simple_control;
 
 
        /* Evaluate the conditional.  */
        /* Evaluate the conditional.  */
        val_mark = value_mark ();
        val_mark = value_mark ();
        val = evaluate_expression (expr);
        val = evaluate_expression (expr);
 
 
        /* Choose which arm to take commands from based on the value of the
        /* Choose which arm to take commands from based on the value of the
           conditional expression.  */
           conditional expression.  */
        if (value_true (val))
        if (value_true (val))
          current = *cmd->body_list;
          current = *cmd->body_list;
        else if (cmd->body_count == 2)
        else if (cmd->body_count == 2)
          current = *(cmd->body_list + 1);
          current = *(cmd->body_list + 1);
        value_free_to_mark (val_mark);
        value_free_to_mark (val_mark);
 
 
        /* Execute commands in the given arm.  */
        /* Execute commands in the given arm.  */
        while (current)
        while (current)
          {
          {
            command_nest_depth++;
            command_nest_depth++;
            ret = execute_control_command (current);
            ret = execute_control_command (current);
            command_nest_depth--;
            command_nest_depth--;
 
 
            /* If we got an error, get out.  */
            /* If we got an error, get out.  */
            if (ret != simple_control)
            if (ret != simple_control)
              break;
              break;
 
 
            /* Get the next statement in the body.  */
            /* Get the next statement in the body.  */
            current = current->next;
            current = current->next;
          }
          }
 
 
        break;
        break;
      }
      }
    case commands_control:
    case commands_control:
      {
      {
        /* Breakpoint commands list, record the commands in the breakpoint's
        /* Breakpoint commands list, record the commands in the breakpoint's
           command list and return.  */
           command list and return.  */
        new_line = insert_args (cmd->line);
        new_line = insert_args (cmd->line);
        if (!new_line)
        if (!new_line)
          break;
          break;
        make_cleanup (free_current_contents, &new_line);
        make_cleanup (free_current_contents, &new_line);
        ret = commands_from_control_command (new_line, cmd);
        ret = commands_from_control_command (new_line, cmd);
        break;
        break;
      }
      }
    case python_control:
    case python_control:
      {
      {
        eval_python_from_control_command (cmd);
        eval_python_from_control_command (cmd);
        ret = simple_control;
        ret = simple_control;
        break;
        break;
      }
      }
 
 
    default:
    default:
      warning (_("Invalid control type in canned commands structure."));
      warning (_("Invalid control type in canned commands structure."));
      break;
      break;
    }
    }
 
 
  do_cleanups (old_chain);
  do_cleanups (old_chain);
 
 
  return ret;
  return ret;
}
}
 
 
/* Like execute_control_command, but first set
/* Like execute_control_command, but first set
   suppress_next_print_command_trace.   */
   suppress_next_print_command_trace.   */
 
 
enum command_control_type
enum command_control_type
execute_control_command_untraced (struct command_line *cmd)
execute_control_command_untraced (struct command_line *cmd)
{
{
  suppress_next_print_command_trace = 1;
  suppress_next_print_command_trace = 1;
  return execute_control_command (cmd);
  return execute_control_command (cmd);
}
}
 
 
 
 
/* "while" command support.  Executes a body of statements while the
/* "while" command support.  Executes a body of statements while the
   loop condition is nonzero.  */
   loop condition is nonzero.  */
 
 
void
void
while_command (char *arg, int from_tty)
while_command (char *arg, int from_tty)
{
{
  struct command_line *command = NULL;
  struct command_line *command = NULL;
 
 
  control_level = 1;
  control_level = 1;
  command = get_command_line (while_control, arg);
  command = get_command_line (while_control, arg);
 
 
  if (command == NULL)
  if (command == NULL)
    return;
    return;
 
 
  execute_control_command_untraced (command);
  execute_control_command_untraced (command);
  free_command_lines (&command);
  free_command_lines (&command);
}
}
 
 
/* "if" command support.  Execute either the true or false arm depending
/* "if" command support.  Execute either the true or false arm depending
   on the value of the if conditional.  */
   on the value of the if conditional.  */
 
 
void
void
if_command (char *arg, int from_tty)
if_command (char *arg, int from_tty)
{
{
  struct command_line *command = NULL;
  struct command_line *command = NULL;
 
 
  control_level = 1;
  control_level = 1;
  command = get_command_line (if_control, arg);
  command = get_command_line (if_control, arg);
 
 
  if (command == NULL)
  if (command == NULL)
    return;
    return;
 
 
  execute_control_command_untraced (command);
  execute_control_command_untraced (command);
  free_command_lines (&command);
  free_command_lines (&command);
}
}
 
 
/* Cleanup */
/* Cleanup */
static void
static void
arg_cleanup (void *ignore)
arg_cleanup (void *ignore)
{
{
  struct user_args *oargs = user_args;
  struct user_args *oargs = user_args;
  if (!user_args)
  if (!user_args)
    internal_error (__FILE__, __LINE__,
    internal_error (__FILE__, __LINE__,
                    _("arg_cleanup called with no user args.\n"));
                    _("arg_cleanup called with no user args.\n"));
 
 
  user_args = user_args->next;
  user_args = user_args->next;
  xfree (oargs->command);
  xfree (oargs->command);
  xfree (oargs);
  xfree (oargs);
}
}
 
 
/* Bind the incomming arguments for a user defined command to
/* Bind the incomming arguments for a user defined command to
   $arg0, $arg1 ... $argMAXUSERARGS.  */
   $arg0, $arg1 ... $argMAXUSERARGS.  */
 
 
static struct cleanup *
static struct cleanup *
setup_user_args (char *p)
setup_user_args (char *p)
{
{
  struct user_args *args;
  struct user_args *args;
  struct cleanup *old_chain;
  struct cleanup *old_chain;
  unsigned int arg_count = 0;
  unsigned int arg_count = 0;
 
 
  args = (struct user_args *) xmalloc (sizeof (struct user_args));
  args = (struct user_args *) xmalloc (sizeof (struct user_args));
  memset (args, 0, sizeof (struct user_args));
  memset (args, 0, sizeof (struct user_args));
 
 
  args->next = user_args;
  args->next = user_args;
  user_args = args;
  user_args = args;
 
 
  old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
  old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
 
 
  if (p == NULL)
  if (p == NULL)
    return old_chain;
    return old_chain;
 
 
  user_args->command = p = xstrdup (p);
  user_args->command = p = xstrdup (p);
 
 
  while (*p)
  while (*p)
    {
    {
      char *start_arg;
      char *start_arg;
      int squote = 0;
      int squote = 0;
      int dquote = 0;
      int dquote = 0;
      int bsquote = 0;
      int bsquote = 0;
 
 
      if (arg_count >= MAXUSERARGS)
      if (arg_count >= MAXUSERARGS)
        {
        {
          error (_("user defined function may only have %d arguments."),
          error (_("user defined function may only have %d arguments."),
                 MAXUSERARGS);
                 MAXUSERARGS);
          return old_chain;
          return old_chain;
        }
        }
 
 
      /* Strip whitespace.  */
      /* Strip whitespace.  */
      while (*p == ' ' || *p == '\t')
      while (*p == ' ' || *p == '\t')
        p++;
        p++;
 
 
      /* P now points to an argument.  */
      /* P now points to an argument.  */
      start_arg = p;
      start_arg = p;
      user_args->a[arg_count].arg = p;
      user_args->a[arg_count].arg = p;
 
 
      /* Get to the end of this argument.  */
      /* Get to the end of this argument.  */
      while (*p)
      while (*p)
        {
        {
          if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
          if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
            break;
            break;
          else
          else
            {
            {
              if (bsquote)
              if (bsquote)
                bsquote = 0;
                bsquote = 0;
              else if (*p == '\\')
              else if (*p == '\\')
                bsquote = 1;
                bsquote = 1;
              else if (squote)
              else if (squote)
                {
                {
                  if (*p == '\'')
                  if (*p == '\'')
                    squote = 0;
                    squote = 0;
                }
                }
              else if (dquote)
              else if (dquote)
                {
                {
                  if (*p == '"')
                  if (*p == '"')
                    dquote = 0;
                    dquote = 0;
                }
                }
              else
              else
                {
                {
                  if (*p == '\'')
                  if (*p == '\'')
                    squote = 1;
                    squote = 1;
                  else if (*p == '"')
                  else if (*p == '"')
                    dquote = 1;
                    dquote = 1;
                }
                }
              p++;
              p++;
            }
            }
        }
        }
 
 
      user_args->a[arg_count].len = p - start_arg;
      user_args->a[arg_count].len = p - start_arg;
      arg_count++;
      arg_count++;
      user_args->count++;
      user_args->count++;
    }
    }
  return old_chain;
  return old_chain;
}
}
 
 
/* Given character string P, return a point to the first argument ($arg),
/* Given character string P, return a point to the first argument ($arg),
   or NULL if P contains no arguments.  */
   or NULL if P contains no arguments.  */
 
 
static char *
static char *
locate_arg (char *p)
locate_arg (char *p)
{
{
  while ((p = strchr (p, '$')))
  while ((p = strchr (p, '$')))
    {
    {
      if (strncmp (p, "$arg", 4) == 0
      if (strncmp (p, "$arg", 4) == 0
          && (isdigit (p[4]) || p[4] == 'c'))
          && (isdigit (p[4]) || p[4] == 'c'))
        return p;
        return p;
      p++;
      p++;
    }
    }
  return NULL;
  return NULL;
}
}
 
 
/* Insert the user defined arguments stored in user_arg into the $arg
/* Insert the user defined arguments stored in user_arg into the $arg
   arguments found in line, with the updated copy being placed into nline.  */
   arguments found in line, with the updated copy being placed into nline.  */
 
 
static char *
static char *
insert_args (char *line)
insert_args (char *line)
{
{
  char *p, *save_line, *new_line;
  char *p, *save_line, *new_line;
  unsigned len, i;
  unsigned len, i;
 
 
  /* If we are not in a user-defined function, treat $argc, $arg0, et
  /* If we are not in a user-defined function, treat $argc, $arg0, et
     cetera as normal convenience variables.  */
     cetera as normal convenience variables.  */
  if (user_args == NULL)
  if (user_args == NULL)
    return xstrdup (line);
    return xstrdup (line);
 
 
  /* First we need to know how much memory to allocate for the new line.  */
  /* First we need to know how much memory to allocate for the new line.  */
  save_line = line;
  save_line = line;
  len = 0;
  len = 0;
  while ((p = locate_arg (line)))
  while ((p = locate_arg (line)))
    {
    {
      len += p - line;
      len += p - line;
      i = p[4] - '0';
      i = p[4] - '0';
 
 
      if (p[4] == 'c')
      if (p[4] == 'c')
        {
        {
          /* $argc.  Number will be <=10.  */
          /* $argc.  Number will be <=10.  */
          len += user_args->count == 10 ? 2 : 1;
          len += user_args->count == 10 ? 2 : 1;
        }
        }
      else if (i >= user_args->count)
      else if (i >= user_args->count)
        {
        {
          error (_("Missing argument %d in user function."), i);
          error (_("Missing argument %d in user function."), i);
          return NULL;
          return NULL;
        }
        }
      else
      else
        {
        {
          len += user_args->a[i].len;
          len += user_args->a[i].len;
        }
        }
      line = p + 5;
      line = p + 5;
    }
    }
 
 
  /* Don't forget the tail.  */
  /* Don't forget the tail.  */
  len += strlen (line);
  len += strlen (line);
 
 
  /* Allocate space for the new line and fill it in.  */
  /* Allocate space for the new line and fill it in.  */
  new_line = (char *) xmalloc (len + 1);
  new_line = (char *) xmalloc (len + 1);
  if (new_line == NULL)
  if (new_line == NULL)
    return NULL;
    return NULL;
 
 
  /* Restore pointer to beginning of old line.  */
  /* Restore pointer to beginning of old line.  */
  line = save_line;
  line = save_line;
 
 
  /* Save pointer to beginning of new line.  */
  /* Save pointer to beginning of new line.  */
  save_line = new_line;
  save_line = new_line;
 
 
  while ((p = locate_arg (line)))
  while ((p = locate_arg (line)))
    {
    {
      int i, len;
      int i, len;
 
 
      memcpy (new_line, line, p - line);
      memcpy (new_line, line, p - line);
      new_line += p - line;
      new_line += p - line;
 
 
      if (p[4] == 'c')
      if (p[4] == 'c')
        {
        {
          gdb_assert (user_args->count >= 0 && user_args->count <= 10);
          gdb_assert (user_args->count >= 0 && user_args->count <= 10);
          if (user_args->count == 10)
          if (user_args->count == 10)
            {
            {
              *(new_line++) = '1';
              *(new_line++) = '1';
              *(new_line++) = '0';
              *(new_line++) = '0';
            }
            }
          else
          else
            *(new_line++) = user_args->count + '0';
            *(new_line++) = user_args->count + '0';
        }
        }
      else
      else
        {
        {
          i = p[4] - '0';
          i = p[4] - '0';
          len = user_args->a[i].len;
          len = user_args->a[i].len;
          if (len)
          if (len)
          {
          {
            memcpy (new_line, user_args->a[i].arg, len);
            memcpy (new_line, user_args->a[i].arg, len);
            new_line += len;
            new_line += len;
          }
          }
        }
        }
      line = p + 5;
      line = p + 5;
    }
    }
  /* Don't forget the tail.  */
  /* Don't forget the tail.  */
  strcpy (new_line, line);
  strcpy (new_line, line);
 
 
  /* Return a pointer to the beginning of the new line.  */
  /* Return a pointer to the beginning of the new line.  */
  return save_line;
  return save_line;
}
}
 
 


/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
   code bodies.  This is typically used when we encounter an "else"
   code bodies.  This is typically used when we encounter an "else"
   clause for an "if" command.  */
   clause for an "if" command.  */
 
 
static void
static void
realloc_body_list (struct command_line *command, int new_length)
realloc_body_list (struct command_line *command, int new_length)
{
{
  int n;
  int n;
  struct command_line **body_list;
  struct command_line **body_list;
 
 
  n = command->body_count;
  n = command->body_count;
 
 
  /* Nothing to do?  */
  /* Nothing to do?  */
  if (new_length <= n)
  if (new_length <= n)
    return;
    return;
 
 
  body_list = (struct command_line **)
  body_list = (struct command_line **)
    xmalloc (sizeof (struct command_line *) * new_length);
    xmalloc (sizeof (struct command_line *) * new_length);
 
 
  memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
  memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
  memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
  memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
 
 
  xfree (command->body_list);
  xfree (command->body_list);
  command->body_list = body_list;
  command->body_list = body_list;
  command->body_count = new_length;
  command->body_count = new_length;
}
}
 
 
/* Read next line from stdout.  Passed to read_command_line_1 and
/* Read next line from stdout.  Passed to read_command_line_1 and
   recurse_read_control_structure whenever we need to read commands
   recurse_read_control_structure whenever we need to read commands
   from stdout.  */
   from stdout.  */
 
 
static char *
static char *
read_next_line ()
read_next_line ()
{
{
  char *prompt_ptr, control_prompt[256];
  char *prompt_ptr, control_prompt[256];
  int i = 0;
  int i = 0;
 
 
  if (control_level >= 254)
  if (control_level >= 254)
    error (_("Control nesting too deep!"));
    error (_("Control nesting too deep!"));
 
 
  /* Set a prompt based on the nesting of the control commands.  */
  /* Set a prompt based on the nesting of the control commands.  */
  if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
  if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
    {
    {
      for (i = 0; i < control_level; i++)
      for (i = 0; i < control_level; i++)
        control_prompt[i] = ' ';
        control_prompt[i] = ' ';
      control_prompt[i] = '>';
      control_prompt[i] = '>';
      control_prompt[i + 1] = '\0';
      control_prompt[i + 1] = '\0';
      prompt_ptr = (char *) &control_prompt[0];
      prompt_ptr = (char *) &control_prompt[0];
    }
    }
  else
  else
    prompt_ptr = NULL;
    prompt_ptr = NULL;
 
 
  return command_line_input (prompt_ptr, instream == stdin, "commands");
  return command_line_input (prompt_ptr, instream == stdin, "commands");
}
}
 
 
/* Process one input line.  If the command is an "end",
/* Process one input line.  If the command is an "end",
   return such an indication to the caller.  If PARSE_COMMANDS is true,
   return such an indication to the caller.  If PARSE_COMMANDS is true,
   strip leading whitespace (trailing whitespace is always stripped)
   strip leading whitespace (trailing whitespace is always stripped)
   in the line, attempt to recognize GDB control commands, and also
   in the line, attempt to recognize GDB control commands, and also
   return an indication if the command is an "else" or a nop.
   return an indication if the command is an "else" or a nop.
   Otherwise, only "end" is recognized.  */
   Otherwise, only "end" is recognized.  */
 
 
static enum misc_command_type
static enum misc_command_type
process_next_line (char *p, struct command_line **command, int parse_commands)
process_next_line (char *p, struct command_line **command, int parse_commands)
{
{
  char *p_end;
  char *p_end;
  char *p_start;
  char *p_start;
  int not_handled = 0;
  int not_handled = 0;
 
 
  /* Not sure what to do here.  */
  /* Not sure what to do here.  */
  if (p == NULL)
  if (p == NULL)
    return end_command;
    return end_command;
 
 
  /* Strip trailing whitespace.  */
  /* Strip trailing whitespace.  */
  p_end = p + strlen (p);
  p_end = p + strlen (p);
  while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
  while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
    p_end--;
    p_end--;
 
 
  p_start = p;
  p_start = p;
  /* Strip leading whitespace.  */
  /* Strip leading whitespace.  */
  while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
  while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
    p_start++;
    p_start++;
 
 
  /* 'end' is always recognized, regardless of parse_commands value.
  /* 'end' is always recognized, regardless of parse_commands value.
     We also permit whitespace before end and after.  */
     We also permit whitespace before end and after.  */
  if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
  if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
    return end_command;
    return end_command;
 
 
  if (parse_commands)
  if (parse_commands)
    {
    {
      /* If commands are parsed, we skip initial spaces. Otherwise,
      /* If commands are parsed, we skip initial spaces. Otherwise,
         which is the case for Python commands and documentation
         which is the case for Python commands and documentation
         (see the 'document' command), spaces are preserved.  */
         (see the 'document' command), spaces are preserved.  */
      p = p_start;
      p = p_start;
 
 
      /* Blanks and comments don't really do anything, but we need to
      /* Blanks and comments don't really do anything, but we need to
         distinguish them from else, end and other commands which can be
         distinguish them from else, end and other commands which can be
         executed.  */
         executed.  */
      if (p_end == p || p[0] == '#')
      if (p_end == p || p[0] == '#')
        return nop_command;
        return nop_command;
 
 
      /* Is the else clause of an if control structure?  */
      /* Is the else clause of an if control structure?  */
      if (p_end - p == 4 && !strncmp (p, "else", 4))
      if (p_end - p == 4 && !strncmp (p, "else", 4))
        return else_command;
        return else_command;
 
 
      /* Check for while, if, break, continue, etc and build a new command
      /* Check for while, if, break, continue, etc and build a new command
         line structure for them.  */
         line structure for them.  */
      if (p_end - p > 5 && !strncmp (p, "while", 5))
      if (p_end - p > 5 && !strncmp (p, "while", 5))
        {
        {
          char *first_arg;
          char *first_arg;
          first_arg = p + 5;
          first_arg = p + 5;
          while (first_arg < p_end && isspace (*first_arg))
          while (first_arg < p_end && isspace (*first_arg))
            first_arg++;
            first_arg++;
          *command = build_command_line (while_control, first_arg);
          *command = build_command_line (while_control, first_arg);
        }
        }
      else if (p_end - p > 2 && !strncmp (p, "if", 2))
      else if (p_end - p > 2 && !strncmp (p, "if", 2))
        {
        {
          char *first_arg;
          char *first_arg;
          first_arg = p + 2;
          first_arg = p + 2;
          while (first_arg < p_end && isspace (*first_arg))
          while (first_arg < p_end && isspace (*first_arg))
            first_arg++;
            first_arg++;
          *command = build_command_line (if_control, first_arg);
          *command = build_command_line (if_control, first_arg);
        }
        }
      else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
      else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
        {
        {
          char *first_arg;
          char *first_arg;
          first_arg = p + 8;
          first_arg = p + 8;
          while (first_arg < p_end && isspace (*first_arg))
          while (first_arg < p_end && isspace (*first_arg))
            first_arg++;
            first_arg++;
          *command = build_command_line (commands_control, first_arg);
          *command = build_command_line (commands_control, first_arg);
        }
        }
      else if (p_end - p == 6 && !strncmp (p, "python", 6))
      else if (p_end - p == 6 && !strncmp (p, "python", 6))
        {
        {
          /* Note that we ignore the inline "python command" form
          /* Note that we ignore the inline "python command" form
             here.  */
             here.  */
          *command = build_command_line (python_control, "");
          *command = build_command_line (python_control, "");
        }
        }
      else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
      else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
        {
        {
          *command = (struct command_line *)
          *command = (struct command_line *)
            xmalloc (sizeof (struct command_line));
            xmalloc (sizeof (struct command_line));
          (*command)->next = NULL;
          (*command)->next = NULL;
          (*command)->line = NULL;
          (*command)->line = NULL;
          (*command)->control_type = break_control;
          (*command)->control_type = break_control;
          (*command)->body_count = 0;
          (*command)->body_count = 0;
          (*command)->body_list = NULL;
          (*command)->body_list = NULL;
        }
        }
      else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
      else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
        {
        {
          *command = (struct command_line *)
          *command = (struct command_line *)
            xmalloc (sizeof (struct command_line));
            xmalloc (sizeof (struct command_line));
          (*command)->next = NULL;
          (*command)->next = NULL;
          (*command)->line = NULL;
          (*command)->line = NULL;
          (*command)->control_type = continue_control;
          (*command)->control_type = continue_control;
          (*command)->body_count = 0;
          (*command)->body_count = 0;
          (*command)->body_list = NULL;
          (*command)->body_list = NULL;
        }
        }
      else
      else
        not_handled = 1;
        not_handled = 1;
    }
    }
 
 
  if (!parse_commands || not_handled)
  if (!parse_commands || not_handled)
    {
    {
      /* A normal command.  */
      /* A normal command.  */
      *command = (struct command_line *)
      *command = (struct command_line *)
        xmalloc (sizeof (struct command_line));
        xmalloc (sizeof (struct command_line));
      (*command)->next = NULL;
      (*command)->next = NULL;
      (*command)->line = savestring (p, p_end - p);
      (*command)->line = savestring (p, p_end - p);
      (*command)->control_type = simple_control;
      (*command)->control_type = simple_control;
      (*command)->body_count = 0;
      (*command)->body_count = 0;
      (*command)->body_list = NULL;
      (*command)->body_list = NULL;
    }
    }
 
 
  /* Nothing special.  */
  /* Nothing special.  */
  return ok_command;
  return ok_command;
}
}
 
 
/* Recursively read in the control structures and create a command_line
/* Recursively read in the control structures and create a command_line
   structure from them.  Use read_next_line_func to obtain lines of
   structure from them.  Use read_next_line_func to obtain lines of
   the command.
   the command.
 
 
*/
*/
 
 
static enum command_control_type
static enum command_control_type
recurse_read_control_structure (char * (*read_next_line_func) (),
recurse_read_control_structure (char * (*read_next_line_func) (),
                                struct command_line *current_cmd)
                                struct command_line *current_cmd)
{
{
  int current_body, i;
  int current_body, i;
  enum misc_command_type val;
  enum misc_command_type val;
  enum command_control_type ret;
  enum command_control_type ret;
  struct command_line **body_ptr, *child_tail, *next;
  struct command_line **body_ptr, *child_tail, *next;
  char *p;
  char *p;
 
 
  child_tail = NULL;
  child_tail = NULL;
  current_body = 1;
  current_body = 1;
 
 
  /* Sanity checks.  */
  /* Sanity checks.  */
  if (current_cmd->control_type == simple_control)
  if (current_cmd->control_type == simple_control)
    error (_("Recursed on a simple control type."));
    error (_("Recursed on a simple control type."));
 
 
  if (current_body > current_cmd->body_count)
  if (current_body > current_cmd->body_count)
    error (_("Allocated body is smaller than this command type needs."));
    error (_("Allocated body is smaller than this command type needs."));
 
 
  /* Read lines from the input stream and build control structures.  */
  /* Read lines from the input stream and build control structures.  */
  while (1)
  while (1)
    {
    {
      dont_repeat ();
      dont_repeat ();
 
 
      next = NULL;
      next = NULL;
      val = process_next_line (read_next_line_func (), &next,
      val = process_next_line (read_next_line_func (), &next,
                               current_cmd->control_type != python_control);
                               current_cmd->control_type != python_control);
 
 
      /* Just skip blanks and comments.  */
      /* Just skip blanks and comments.  */
      if (val == nop_command)
      if (val == nop_command)
        continue;
        continue;
 
 
      if (val == end_command)
      if (val == end_command)
        {
        {
          if (current_cmd->control_type == while_control
          if (current_cmd->control_type == while_control
              || current_cmd->control_type == if_control
              || current_cmd->control_type == if_control
              || current_cmd->control_type == python_control
              || current_cmd->control_type == python_control
              || current_cmd->control_type == commands_control)
              || current_cmd->control_type == commands_control)
            {
            {
              /* Success reading an entire canned sequence of commands.  */
              /* Success reading an entire canned sequence of commands.  */
              ret = simple_control;
              ret = simple_control;
              break;
              break;
            }
            }
          else
          else
            {
            {
              ret = invalid_control;
              ret = invalid_control;
              break;
              break;
            }
            }
        }
        }
 
 
      /* Not the end of a control structure.  */
      /* Not the end of a control structure.  */
      if (val == else_command)
      if (val == else_command)
        {
        {
          if (current_cmd->control_type == if_control
          if (current_cmd->control_type == if_control
              && current_body == 1)
              && current_body == 1)
            {
            {
              realloc_body_list (current_cmd, 2);
              realloc_body_list (current_cmd, 2);
              current_body = 2;
              current_body = 2;
              child_tail = NULL;
              child_tail = NULL;
              continue;
              continue;
            }
            }
          else
          else
            {
            {
              ret = invalid_control;
              ret = invalid_control;
              break;
              break;
            }
            }
        }
        }
 
 
      if (child_tail)
      if (child_tail)
        {
        {
          child_tail->next = next;
          child_tail->next = next;
        }
        }
      else
      else
        {
        {
          body_ptr = current_cmd->body_list;
          body_ptr = current_cmd->body_list;
          for (i = 1; i < current_body; i++)
          for (i = 1; i < current_body; i++)
            body_ptr++;
            body_ptr++;
 
 
          *body_ptr = next;
          *body_ptr = next;
 
 
        }
        }
 
 
      child_tail = next;
      child_tail = next;
 
 
      /* If the latest line is another control structure, then recurse
      /* If the latest line is another control structure, then recurse
         on it.  */
         on it.  */
      if (next->control_type == while_control
      if (next->control_type == while_control
          || next->control_type == if_control
          || next->control_type == if_control
          || next->control_type == python_control
          || next->control_type == python_control
          || next->control_type == commands_control)
          || next->control_type == commands_control)
        {
        {
          control_level++;
          control_level++;
          ret = recurse_read_control_structure (read_next_line_func, next);
          ret = recurse_read_control_structure (read_next_line_func, next);
          control_level--;
          control_level--;
 
 
          if (ret != simple_control)
          if (ret != simple_control)
            break;
            break;
        }
        }
    }
    }
 
 
  dont_repeat ();
  dont_repeat ();
 
 
  return ret;
  return ret;
}
}
 
 
/* Read lines from the input stream and accumulate them in a chain of
/* Read lines from the input stream and accumulate them in a chain of
   struct command_line's, which is then returned.  For input from a
   struct command_line's, which is then returned.  For input from a
   terminal, the special command "end" is used to mark the end of the
   terminal, the special command "end" is used to mark the end of the
   input, and is not included in the returned chain of commands.
   input, and is not included in the returned chain of commands.
 
 
   If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
   If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
   is always stripped) in the line and attempt to recognize GDB control
   is always stripped) in the line and attempt to recognize GDB control
   commands.  Otherwise, only "end" is recognized.  */
   commands.  Otherwise, only "end" is recognized.  */
 
 
#define END_MESSAGE "End with a line saying just \"end\"."
#define END_MESSAGE "End with a line saying just \"end\"."
 
 
struct command_line *
struct command_line *
read_command_lines (char *prompt_arg, int from_tty, int parse_commands)
read_command_lines (char *prompt_arg, int from_tty, int parse_commands)
{
{
  struct command_line *head;
  struct command_line *head;
 
 
  if (from_tty && input_from_terminal_p ())
  if (from_tty && input_from_terminal_p ())
    {
    {
      if (deprecated_readline_begin_hook)
      if (deprecated_readline_begin_hook)
        {
        {
          /* Note - intentional to merge messages with no newline */
          /* Note - intentional to merge messages with no newline */
          (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg, END_MESSAGE);
          (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg, END_MESSAGE);
        }
        }
      else
      else
        {
        {
          printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
          printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
          gdb_flush (gdb_stdout);
          gdb_flush (gdb_stdout);
        }
        }
    }
    }
 
 
  head = read_command_lines_1 (read_next_line, parse_commands);
  head = read_command_lines_1 (read_next_line, parse_commands);
 
 
  if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
  if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
    {
    {
      (*deprecated_readline_end_hook) ();
      (*deprecated_readline_end_hook) ();
    }
    }
  return (head);
  return (head);
}
}
 
 
/* Act the same way as read_command_lines, except that each new line is
/* Act the same way as read_command_lines, except that each new line is
   obtained using READ_NEXT_LINE_FUNC.  */
   obtained using READ_NEXT_LINE_FUNC.  */
 
 
struct command_line *
struct command_line *
read_command_lines_1 (char * (*read_next_line_func) (), int parse_commands)
read_command_lines_1 (char * (*read_next_line_func) (), int parse_commands)
{
{
  struct command_line *head, *tail, *next;
  struct command_line *head, *tail, *next;
  struct cleanup *old_chain;
  struct cleanup *old_chain;
  enum command_control_type ret;
  enum command_control_type ret;
  enum misc_command_type val;
  enum misc_command_type val;
 
 
  control_level = 0;
  control_level = 0;
  head = tail = NULL;
  head = tail = NULL;
  old_chain = NULL;
  old_chain = NULL;
 
 
  while (1)
  while (1)
    {
    {
      dont_repeat ();
      dont_repeat ();
      val = process_next_line (read_next_line_func (), &next, parse_commands);
      val = process_next_line (read_next_line_func (), &next, parse_commands);
 
 
      /* Ignore blank lines or comments.  */
      /* Ignore blank lines or comments.  */
      if (val == nop_command)
      if (val == nop_command)
        continue;
        continue;
 
 
      if (val == end_command)
      if (val == end_command)
        {
        {
          ret = simple_control;
          ret = simple_control;
          break;
          break;
        }
        }
 
 
      if (val != ok_command)
      if (val != ok_command)
        {
        {
          ret = invalid_control;
          ret = invalid_control;
          break;
          break;
        }
        }
 
 
      if (next->control_type == while_control
      if (next->control_type == while_control
          || next->control_type == if_control
          || next->control_type == if_control
          || next->control_type == python_control
          || next->control_type == python_control
          || next->control_type == commands_control)
          || next->control_type == commands_control)
        {
        {
          control_level++;
          control_level++;
          ret = recurse_read_control_structure (read_next_line_func, next);
          ret = recurse_read_control_structure (read_next_line_func, next);
          control_level--;
          control_level--;
 
 
          if (ret == invalid_control)
          if (ret == invalid_control)
            break;
            break;
        }
        }
 
 
      if (tail)
      if (tail)
        {
        {
          tail->next = next;
          tail->next = next;
        }
        }
      else
      else
        {
        {
          head = next;
          head = next;
          old_chain = make_cleanup_free_command_lines (&head);
          old_chain = make_cleanup_free_command_lines (&head);
        }
        }
      tail = next;
      tail = next;
    }
    }
 
 
  dont_repeat ();
  dont_repeat ();
 
 
  if (head)
  if (head)
    {
    {
      if (ret != invalid_control)
      if (ret != invalid_control)
        {
        {
          discard_cleanups (old_chain);
          discard_cleanups (old_chain);
        }
        }
      else
      else
        do_cleanups (old_chain);
        do_cleanups (old_chain);
    }
    }
 
 
  return head;
  return head;
}
}
 
 
/* Free a chain of struct command_line's.  */
/* Free a chain of struct command_line's.  */
 
 
void
void
free_command_lines (struct command_line **lptr)
free_command_lines (struct command_line **lptr)
{
{
  struct command_line *l = *lptr;
  struct command_line *l = *lptr;
  struct command_line *next;
  struct command_line *next;
  struct command_line **blist;
  struct command_line **blist;
  int i;
  int i;
 
 
  while (l)
  while (l)
    {
    {
      if (l->body_count > 0)
      if (l->body_count > 0)
        {
        {
          blist = l->body_list;
          blist = l->body_list;
          for (i = 0; i < l->body_count; i++, blist++)
          for (i = 0; i < l->body_count; i++, blist++)
            free_command_lines (blist);
            free_command_lines (blist);
        }
        }
      next = l->next;
      next = l->next;
      xfree (l->line);
      xfree (l->line);
      xfree (l);
      xfree (l);
      l = next;
      l = next;
    }
    }
  *lptr = NULL;
  *lptr = NULL;
}
}
 
 
static void
static void
do_free_command_lines_cleanup (void *arg)
do_free_command_lines_cleanup (void *arg)
{
{
  free_command_lines (arg);
  free_command_lines (arg);
}
}
 
 
struct cleanup *
struct cleanup *
make_cleanup_free_command_lines (struct command_line **arg)
make_cleanup_free_command_lines (struct command_line **arg)
{
{
  return make_cleanup (do_free_command_lines_cleanup, arg);
  return make_cleanup (do_free_command_lines_cleanup, arg);
}
}
 
 
struct command_line *
struct command_line *
copy_command_lines (struct command_line *cmds)
copy_command_lines (struct command_line *cmds)
{
{
  struct command_line *result = NULL;
  struct command_line *result = NULL;
 
 
  if (cmds)
  if (cmds)
    {
    {
      result = (struct command_line *) xmalloc (sizeof (struct command_line));
      result = (struct command_line *) xmalloc (sizeof (struct command_line));
 
 
      result->next = copy_command_lines (cmds->next);
      result->next = copy_command_lines (cmds->next);
      result->line = xstrdup (cmds->line);
      result->line = xstrdup (cmds->line);
      result->control_type = cmds->control_type;
      result->control_type = cmds->control_type;
      result->body_count = cmds->body_count;
      result->body_count = cmds->body_count;
      if (cmds->body_count > 0)
      if (cmds->body_count > 0)
        {
        {
          int i;
          int i;
 
 
          result->body_list = (struct command_line **)
          result->body_list = (struct command_line **)
            xmalloc (sizeof (struct command_line *) * cmds->body_count);
            xmalloc (sizeof (struct command_line *) * cmds->body_count);
 
 
          for (i = 0; i < cmds->body_count; i++)
          for (i = 0; i < cmds->body_count; i++)
            result->body_list[i] = copy_command_lines (cmds->body_list[i]);
            result->body_list[i] = copy_command_lines (cmds->body_list[i]);
        }
        }
      else
      else
        result->body_list = NULL;
        result->body_list = NULL;
    }
    }
 
 
  return result;
  return result;
}
}


/* Validate that *COMNAME is a valid name for a command.  Return the
/* Validate that *COMNAME is a valid name for a command.  Return the
   containing command list, in case it starts with a prefix command.
   containing command list, in case it starts with a prefix command.
   The prefix must already exist.  *COMNAME is advanced to point after
   The prefix must already exist.  *COMNAME is advanced to point after
   any prefix, and a NUL character overwrites the space after the
   any prefix, and a NUL character overwrites the space after the
   prefix.  */
   prefix.  */
 
 
static struct cmd_list_element **
static struct cmd_list_element **
validate_comname (char **comname)
validate_comname (char **comname)
{
{
  struct cmd_list_element **list = &cmdlist;
  struct cmd_list_element **list = &cmdlist;
  char *p, *last_word;
  char *p, *last_word;
 
 
  if (*comname == 0)
  if (*comname == 0)
    error_no_arg (_("name of command to define"));
    error_no_arg (_("name of command to define"));
 
 
  /* Find the last word of the argument.  */
  /* Find the last word of the argument.  */
  p = *comname + strlen (*comname);
  p = *comname + strlen (*comname);
  while (p > *comname && isspace (p[-1]))
  while (p > *comname && isspace (p[-1]))
    p--;
    p--;
  while (p > *comname && !isspace (p[-1]))
  while (p > *comname && !isspace (p[-1]))
    p--;
    p--;
  last_word = p;
  last_word = p;
 
 
  /* Find the corresponding command list.  */
  /* Find the corresponding command list.  */
  if (last_word != *comname)
  if (last_word != *comname)
    {
    {
      struct cmd_list_element *c;
      struct cmd_list_element *c;
      char saved_char, *tem = *comname;
      char saved_char, *tem = *comname;
 
 
      /* Separate the prefix and the command.  */
      /* Separate the prefix and the command.  */
      saved_char = last_word[-1];
      saved_char = last_word[-1];
      last_word[-1] = '\0';
      last_word[-1] = '\0';
 
 
      c = lookup_cmd (&tem, cmdlist, "", 0, 1);
      c = lookup_cmd (&tem, cmdlist, "", 0, 1);
      if (c->prefixlist == NULL)
      if (c->prefixlist == NULL)
        error (_("\"%s\" is not a prefix command."), *comname);
        error (_("\"%s\" is not a prefix command."), *comname);
 
 
      list = c->prefixlist;
      list = c->prefixlist;
      last_word[-1] = saved_char;
      last_word[-1] = saved_char;
      *comname = last_word;
      *comname = last_word;
    }
    }
 
 
  p = *comname;
  p = *comname;
  while (*p)
  while (*p)
    {
    {
      if (!isalnum (*p) && *p != '-' && *p != '_')
      if (!isalnum (*p) && *p != '-' && *p != '_')
        error (_("Junk in argument list: \"%s\""), p);
        error (_("Junk in argument list: \"%s\""), p);
      p++;
      p++;
    }
    }
 
 
  return list;
  return list;
}
}
 
 
/* This is just a placeholder in the command data structures.  */
/* This is just a placeholder in the command data structures.  */
static void
static void
user_defined_command (char *ignore, int from_tty)
user_defined_command (char *ignore, int from_tty)
{
{
}
}
 
 
void
void
define_command (char *comname, int from_tty)
define_command (char *comname, int from_tty)
{
{
#define MAX_TMPBUF 128   
#define MAX_TMPBUF 128   
  enum cmd_hook_type
  enum cmd_hook_type
    {
    {
      CMD_NO_HOOK = 0,
      CMD_NO_HOOK = 0,
      CMD_PRE_HOOK,
      CMD_PRE_HOOK,
      CMD_POST_HOOK
      CMD_POST_HOOK
    };
    };
  struct command_line *cmds;
  struct command_line *cmds;
  struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
  struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
  char *tem, *tem2, *comfull;
  char *tem, *tem2, *comfull;
  char tmpbuf[MAX_TMPBUF];
  char tmpbuf[MAX_TMPBUF];
  int  hook_type      = CMD_NO_HOOK;
  int  hook_type      = CMD_NO_HOOK;
  int  hook_name_size = 0;
  int  hook_name_size = 0;
 
 
#define HOOK_STRING     "hook-"
#define HOOK_STRING     "hook-"
#define HOOK_LEN 5
#define HOOK_LEN 5
#define HOOK_POST_STRING "hookpost-"
#define HOOK_POST_STRING "hookpost-"
#define HOOK_POST_LEN    9
#define HOOK_POST_LEN    9
 
 
  comfull = comname;
  comfull = comname;
  list = validate_comname (&comname);
  list = validate_comname (&comname);
 
 
  /* Look it up, and verify that we got an exact match.  */
  /* Look it up, and verify that we got an exact match.  */
  tem = comname;
  tem = comname;
  c = lookup_cmd (&tem, *list, "", -1, 1);
  c = lookup_cmd (&tem, *list, "", -1, 1);
  if (c && strcmp (comname, c->name) != 0)
  if (c && strcmp (comname, c->name) != 0)
    c = 0;
    c = 0;
 
 
  if (c)
  if (c)
    {
    {
      int q;
      int q;
      if (c->class == class_user || c->class == class_alias)
      if (c->class == class_user || c->class == class_alias)
        q = query (_("Redefine command \"%s\"? "), c->name);
        q = query (_("Redefine command \"%s\"? "), c->name);
      else
      else
        q = query (_("Really redefine built-in command \"%s\"? "), c->name);
        q = query (_("Really redefine built-in command \"%s\"? "), c->name);
      if (!q)
      if (!q)
        error (_("Command \"%s\" not redefined."), c->name);
        error (_("Command \"%s\" not redefined."), c->name);
    }
    }
 
 
  /* If this new command is a hook, then mark the command which it
  /* If this new command is a hook, then mark the command which it
     is hooking.  Note that we allow hooking `help' commands, so that
     is hooking.  Note that we allow hooking `help' commands, so that
     we can hook the `stop' pseudo-command.  */
     we can hook the `stop' pseudo-command.  */
 
 
  if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
  if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
    {
    {
       hook_type      = CMD_PRE_HOOK;
       hook_type      = CMD_PRE_HOOK;
       hook_name_size = HOOK_LEN;
       hook_name_size = HOOK_LEN;
    }
    }
  else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
  else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
    {
    {
      hook_type      = CMD_POST_HOOK;
      hook_type      = CMD_POST_HOOK;
      hook_name_size = HOOK_POST_LEN;
      hook_name_size = HOOK_POST_LEN;
    }
    }
 
 
  if (hook_type != CMD_NO_HOOK)
  if (hook_type != CMD_NO_HOOK)
    {
    {
      /* Look up cmd it hooks, and verify that we got an exact match.  */
      /* Look up cmd it hooks, and verify that we got an exact match.  */
      tem = comname + hook_name_size;
      tem = comname + hook_name_size;
      hookc = lookup_cmd (&tem, *list, "", -1, 0);
      hookc = lookup_cmd (&tem, *list, "", -1, 0);
      if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
      if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
        hookc = 0;
        hookc = 0;
      if (!hookc)
      if (!hookc)
        {
        {
          warning (_("Your new `%s' command does not hook any existing command."),
          warning (_("Your new `%s' command does not hook any existing command."),
                   comfull);
                   comfull);
          if (!query (_("Proceed? ")))
          if (!query (_("Proceed? ")))
            error (_("Not confirmed."));
            error (_("Not confirmed."));
        }
        }
    }
    }
 
 
  comname = xstrdup (comname);
  comname = xstrdup (comname);
 
 
  /* If the rest of the commands will be case insensitive, this one
  /* If the rest of the commands will be case insensitive, this one
     should behave in the same manner. */
     should behave in the same manner. */
  for (tem = comname; *tem; tem++)
  for (tem = comname; *tem; tem++)
    if (isupper (*tem))
    if (isupper (*tem))
      *tem = tolower (*tem);
      *tem = tolower (*tem);
 
 
  sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
  sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
  cmds = read_command_lines (tmpbuf, from_tty, 1);
  cmds = read_command_lines (tmpbuf, from_tty, 1);
 
 
  if (c && c->class == class_user)
  if (c && c->class == class_user)
    free_command_lines (&c->user_commands);
    free_command_lines (&c->user_commands);
 
 
  newc = add_cmd (comname, class_user, user_defined_command,
  newc = add_cmd (comname, class_user, user_defined_command,
                  (c && c->class == class_user)
                  (c && c->class == class_user)
                  ? c->doc : xstrdup ("User-defined."), list);
                  ? c->doc : xstrdup ("User-defined."), list);
  newc->user_commands = cmds;
  newc->user_commands = cmds;
 
 
  /* If this new command is a hook, then mark both commands as being
  /* If this new command is a hook, then mark both commands as being
     tied.  */
     tied.  */
  if (hookc)
  if (hookc)
    {
    {
      switch (hook_type)
      switch (hook_type)
        {
        {
        case CMD_PRE_HOOK:
        case CMD_PRE_HOOK:
          hookc->hook_pre  = newc;  /* Target gets hooked.  */
          hookc->hook_pre  = newc;  /* Target gets hooked.  */
          newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
          newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
          break;
          break;
        case CMD_POST_HOOK:
        case CMD_POST_HOOK:
          hookc->hook_post  = newc;  /* Target gets hooked.  */
          hookc->hook_post  = newc;  /* Target gets hooked.  */
          newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
          newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
          break;
          break;
        default:
        default:
          /* Should never come here as hookc would be 0. */
          /* Should never come here as hookc would be 0. */
          internal_error (__FILE__, __LINE__, _("bad switch"));
          internal_error (__FILE__, __LINE__, _("bad switch"));
        }
        }
    }
    }
}
}
 
 
void
void
document_command (char *comname, int from_tty)
document_command (char *comname, int from_tty)
{
{
  struct command_line *doclines;
  struct command_line *doclines;
  struct cmd_list_element *c, **list;
  struct cmd_list_element *c, **list;
  char *tem, *comfull;
  char *tem, *comfull;
  char tmpbuf[128];
  char tmpbuf[128];
 
 
  comfull = comname;
  comfull = comname;
  list = validate_comname (&comname);
  list = validate_comname (&comname);
 
 
  tem = comname;
  tem = comname;
  c = lookup_cmd (&tem, *list, "", 0, 1);
  c = lookup_cmd (&tem, *list, "", 0, 1);
 
 
  if (c->class != class_user)
  if (c->class != class_user)
    error (_("Command \"%s\" is built-in."), comfull);
    error (_("Command \"%s\" is built-in."), comfull);
 
 
  sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
  sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
  doclines = read_command_lines (tmpbuf, from_tty, 0);
  doclines = read_command_lines (tmpbuf, from_tty, 0);
 
 
  if (c->doc)
  if (c->doc)
    xfree (c->doc);
    xfree (c->doc);
 
 
  {
  {
    struct command_line *cl1;
    struct command_line *cl1;
    int len = 0;
    int len = 0;
 
 
    for (cl1 = doclines; cl1; cl1 = cl1->next)
    for (cl1 = doclines; cl1; cl1 = cl1->next)
      len += strlen (cl1->line) + 1;
      len += strlen (cl1->line) + 1;
 
 
    c->doc = (char *) xmalloc (len + 1);
    c->doc = (char *) xmalloc (len + 1);
    *c->doc = 0;
    *c->doc = 0;
 
 
    for (cl1 = doclines; cl1; cl1 = cl1->next)
    for (cl1 = doclines; cl1; cl1 = cl1->next)
      {
      {
        strcat (c->doc, cl1->line);
        strcat (c->doc, cl1->line);
        if (cl1->next)
        if (cl1->next)
          strcat (c->doc, "\n");
          strcat (c->doc, "\n");
      }
      }
  }
  }
 
 
  free_command_lines (&doclines);
  free_command_lines (&doclines);
}
}


struct source_cleanup_lines_args
struct source_cleanup_lines_args
{
{
  int old_line;
  int old_line;
  char *old_file;
  char *old_file;
};
};
 
 
static void
static void
source_cleanup_lines (void *args)
source_cleanup_lines (void *args)
{
{
  struct source_cleanup_lines_args *p =
  struct source_cleanup_lines_args *p =
  (struct source_cleanup_lines_args *) args;
  (struct source_cleanup_lines_args *) args;
  source_line_number = p->old_line;
  source_line_number = p->old_line;
  source_file_name = p->old_file;
  source_file_name = p->old_file;
}
}
 
 
struct wrapped_read_command_file_args
struct wrapped_read_command_file_args
{
{
  FILE *stream;
  FILE *stream;
};
};
 
 
static void
static void
wrapped_read_command_file (struct ui_out *uiout, void *data)
wrapped_read_command_file (struct ui_out *uiout, void *data)
{
{
  struct wrapped_read_command_file_args *args = data;
  struct wrapped_read_command_file_args *args = data;
  read_command_file (args->stream);
  read_command_file (args->stream);
}
}
 
 
/* Used to implement source_command */
/* Used to implement source_command */
 
 
void
void
script_from_file (FILE *stream, char *file)
script_from_file (FILE *stream, char *file)
{
{
  struct cleanup *old_cleanups;
  struct cleanup *old_cleanups;
  struct source_cleanup_lines_args old_lines;
  struct source_cleanup_lines_args old_lines;
  int needed_length;
  int needed_length;
 
 
  if (stream == NULL)
  if (stream == NULL)
    internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
    internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
 
 
  old_cleanups = make_cleanup_fclose (stream);
  old_cleanups = make_cleanup_fclose (stream);
 
 
  old_lines.old_line = source_line_number;
  old_lines.old_line = source_line_number;
  old_lines.old_file = source_file_name;
  old_lines.old_file = source_file_name;
  make_cleanup (source_cleanup_lines, &old_lines);
  make_cleanup (source_cleanup_lines, &old_lines);
  source_line_number = 0;
  source_line_number = 0;
  source_file_name = file;
  source_file_name = file;
  /* This will get set every time we read a line.  So it won't stay "" for
  /* This will get set every time we read a line.  So it won't stay "" for
     long.  */
     long.  */
  error_pre_print = "";
  error_pre_print = "";
 
 
  {
  {
    struct gdb_exception e;
    struct gdb_exception e;
    struct wrapped_read_command_file_args args;
    struct wrapped_read_command_file_args args;
    args.stream = stream;
    args.stream = stream;
    e = catch_exception (uiout, wrapped_read_command_file, &args,
    e = catch_exception (uiout, wrapped_read_command_file, &args,
                         RETURN_MASK_ERROR);
                         RETURN_MASK_ERROR);
    switch (e.reason)
    switch (e.reason)
      {
      {
      case 0:
      case 0:
        break;
        break;
      case RETURN_ERROR:
      case RETURN_ERROR:
        /* Re-throw the error, but with the file name information
        /* Re-throw the error, but with the file name information
           prepended.  */
           prepended.  */
        throw_error (e.error,
        throw_error (e.error,
                     _("%s:%d: Error in sourced command file:\n%s"),
                     _("%s:%d: Error in sourced command file:\n%s"),
                     source_file_name, source_line_number, e.message);
                     source_file_name, source_line_number, e.message);
      default:
      default:
        internal_error (__FILE__, __LINE__, _("bad reason"));
        internal_error (__FILE__, __LINE__, _("bad reason"));
      }
      }
  }
  }
 
 
  do_cleanups (old_cleanups);
  do_cleanups (old_cleanups);
}
}
 
 
/* Print the definition of user command C to STREAM.  Or, if C is a
/* Print the definition of user command C to STREAM.  Or, if C is a
   prefix command, show the definitions of all user commands under C
   prefix command, show the definitions of all user commands under C
   (recursively).  PREFIX and NAME combined are the name of the
   (recursively).  PREFIX and NAME combined are the name of the
   current command.  */
   current command.  */
void
void
show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
             struct ui_file *stream)
             struct ui_file *stream)
{
{
  struct command_line *cmdlines;
  struct command_line *cmdlines;
 
 
  if (c->prefixlist != NULL)
  if (c->prefixlist != NULL)
    {
    {
      char *prefixname = c->prefixname;
      char *prefixname = c->prefixname;
      for (c = *c->prefixlist; c != NULL; c = c->next)
      for (c = *c->prefixlist; c != NULL; c = c->next)
        if (c->class == class_user || c->prefixlist != NULL)
        if (c->class == class_user || c->prefixlist != NULL)
          show_user_1 (c, prefixname, c->name, gdb_stdout);
          show_user_1 (c, prefixname, c->name, gdb_stdout);
      return;
      return;
    }
    }
 
 
  cmdlines = c->user_commands;
  cmdlines = c->user_commands;
  if (!cmdlines)
  if (!cmdlines)
    return;
    return;
  fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
  fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
 
 
  print_command_lines (uiout, cmdlines, 1);
  print_command_lines (uiout, cmdlines, 1);
  fputs_filtered ("\n", stream);
  fputs_filtered ("\n", stream);
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.