gdb/riscv: Remove 'Contributed by....' comments
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
index 6e90bf222bf14c1d404cd23011b736fe95667e99..366c42227c620b73f5497b18fe0372a2c04d2b3c 100644 (file)
@@ -1,8 +1,6 @@
 /* GDB CLI command scripting.
 
-   Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
-   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008,
-   2009 Free Software Foundation, Inc.
+   Copyright (C) 1986-2018 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include <ctype.h>
 
 #include "ui-out.h"
-#include "gdb_string.h"
-#include "exceptions.h"
 #include "top.h"
 #include "breakpoint.h"
 #include "cli/cli-cmds.h"
 #include "cli/cli-decode.h"
 #include "cli/cli-script.h"
-#include "gdb_assert.h"
 
-#include "python/python.h"
+#include "extension.h"
+#include "interps.h"
+#include "compile/compile.h"
 
-/* Prototypes for local functions */
+#include <vector>
 
-static enum command_control_type
-recurse_read_control_structure (char * (*read_next_line_func) (), 
-                               struct command_line *current_cmd);
-
-static char *insert_args (char *line);
+/* Prototypes for local functions.  */
 
-static struct cleanup * setup_user_args (char *p);
+static enum command_control_type
+recurse_read_control_structure (char * (*read_next_line_func) (void),
+                               struct command_line *current_cmd,
+                               void (*validator)(char *, void *),
+                               void *closure);
 
-static char *read_next_line ();
+static char *read_next_line (void);
 
 /* Level of control structure when reading.  */
 static int control_level;
@@ -57,46 +54,110 @@ static int command_nest_depth = 1;
 /* This is to prevent certain commands being printed twice.  */
 static int suppress_next_print_command_trace = 0;
 
+/* A non-owning slice of a string.  */
+
+struct string_view
+{
+  string_view (const char *str_, size_t len_)
+    : str (str_), len (len_)
+  {}
+
+  const char *str;
+  size_t len;
+};
+
 /* Structure for arguments to user defined functions.  */
-#define MAXUSERARGS 10
-struct user_args
+
+class user_args
+{
+public:
+  /* Save the command line and store the locations of arguments passed
+     to the user defined function.  */
+  explicit user_args (const char *line);
+
+  /* Insert the stored user defined arguments into the $arg arguments
+     found in LINE.  */
+  std::string insert_args (const char *line) const;
+
+private:
+  /* Disable copy/assignment.  (Since the elements of A point inside
+     COMMAND, copying would need to reconstruct the A vector in the
+     new copy.)  */
+  user_args (const user_args &) =delete;
+  user_args &operator= (const user_args &) =delete;
+
+  /* It is necessary to store a copy of the command line to ensure
+     that the arguments are not overwritten before they are used.  */
+  std::string m_command_line;
+
+  /* The arguments.  Each element points inside M_COMMAND_LINE.  */
+  std::vector<string_view> m_args;
+};
+
+/* The stack of arguments passed to user defined functions.  We need a
+   stack because user-defined functions can call other user-defined
+   functions.  */
+static std::vector<std::unique_ptr<user_args>> user_args_stack;
+
+/* An RAII-base class used to push/pop args on the user args
+   stack.  */
+struct scoped_user_args_level
+{
+  /* Parse the command line and push the arguments in the user args
+     stack.  */
+  explicit scoped_user_args_level (const char *line)
   {
-    struct user_args *next;
-    /* It is necessary to store a malloced copy of the command line to
-       ensure that the arguments are not overwritten before they are used.  */
-    char *command;
-    struct
-      {
-       char *arg;
-       int len;
-      }
-    a[MAXUSERARGS];
-    int count;
+    user_args_stack.emplace_back (new user_args (line));
+  }
+
+  /* Pop the current user arguments from the stack.  */
+  ~scoped_user_args_level ()
+  {
+    user_args_stack.pop_back ();
   }
- *user_args;
+};
 
 \f
+/* Return non-zero if TYPE is a multi-line command (i.e., is terminated
+   by "end").  */
+
+static int
+multi_line_command_p (enum command_control_type type)
+{
+  switch (type)
+    {
+    case if_control:
+    case while_control:
+    case while_stepping_control:
+    case commands_control:
+    case compile_control:
+    case python_control:
+    case guile_control:
+      return 1;
+    default:
+      return 0;
+    }
+}
+
 /* Allocate, initialize a new command line structure for one of the
    control commands (if/while).  */
 
 static struct command_line *
-build_command_line (enum command_control_type type, char *args)
+build_command_line (enum command_control_type type, const char *args)
 {
   struct command_line *cmd;
 
-  if (args == NULL && (type == if_control || type == while_control))
+  if ((args == NULL || *args == '\0')
+      && (type == if_control || type == while_control))
     error (_("if/while commands require arguments."));
   gdb_assert (args != NULL);
 
-  cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
+  cmd = XNEW (struct command_line);
   cmd->next = NULL;
   cmd->control_type = type;
 
   cmd->body_count = 1;
-  cmd->body_list
-    = (struct command_line **) xmalloc (sizeof (struct command_line *)
-                                       * cmd->body_count);
-  memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
+  cmd->body_list = XCNEWVEC (struct command_line *, cmd->body_count);
   cmd->line = xstrdup (args);
 
   return cmd;
@@ -105,26 +166,20 @@ build_command_line (enum command_control_type type, char *args)
 /* Build and return a new command structure for the control commands
    such as "if" and "while".  */
 
-struct command_line *
-get_command_line (enum command_control_type type, char *arg)
+command_line_up
+get_command_line (enum command_control_type type, const char *arg)
 {
-  struct command_line *cmd;
-  struct cleanup *old_chain = NULL;
-
   /* Allocate and build a new command line structure.  */
-  cmd = build_command_line (type, arg);
-
-  old_chain = make_cleanup_free_command_lines (&cmd);
+  command_line_up cmd (build_command_line (type, arg));
 
   /* 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.get (), 0, 0)
+      == invalid_control)
     {
       warning (_("Error reading in canned sequence of commands."));
-      do_cleanups (old_chain);
       return NULL;
     }
 
-  discard_cleanups (old_chain);
   return cmd;
 }
 
@@ -139,15 +194,14 @@ print_command_lines (struct ui_out *uiout, struct command_line *cmd,
   list = cmd;
   while (list)
     {
-
       if (depth)
-       ui_out_spaces (uiout, 2 * depth);
+       uiout->spaces (2 * depth);
 
       /* A simple command, print it and continue.  */
       if (list->control_type == simple_control)
        {
-         ui_out_field_string (uiout, NULL, list->line);
-         ui_out_text (uiout, "\n");
+         uiout->field_string (NULL, list->line);
+         uiout->text ("\n");
          list = list->next;
          continue;
        }
@@ -156,115 +210,166 @@ print_command_lines (struct ui_out *uiout, struct command_line *cmd,
          and continue. */
       if (list->control_type == continue_control)
        {
-         ui_out_field_string (uiout, NULL, "loop_continue");
-         ui_out_text (uiout, "\n");
+         uiout->field_string (NULL, "loop_continue");
+         uiout->text ("\n");
          list = list->next;
          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)
        {
-         ui_out_field_string (uiout, NULL, "loop_break");
-         ui_out_text (uiout, "\n");
+         uiout->field_string (NULL, "loop_break");
+         uiout->text ("\n");
          list = list->next;
          continue;
        }
 
-      /* A while command.  Recursively print its subcommands and continue.  */
-      if (list->control_type == while_control)
+      /* A while command.  Recursively print its subcommands and
+        continue.  */
+      if (list->control_type == while_control
+         || list->control_type == while_stepping_control)
        {
-         ui_out_field_fmt (uiout, NULL, "while %s", list->line);
-         ui_out_text (uiout, "\n");
+         /* For while-stepping, the line includes the 'while-stepping'
+            token.  See comment in process_next_line for explanation.
+            Here, take care not print 'while-stepping' twice.  */
+         if (list->control_type == while_control)
+           uiout->field_fmt (NULL, "while %s", list->line);
+         else
+           uiout->field_string (NULL, list->line);
+         uiout->text ("\n");
          print_command_lines (uiout, *list->body_list, depth + 1);
          if (depth)
-           ui_out_spaces (uiout, 2 * depth);
-         ui_out_field_string (uiout, NULL, "end");
-         ui_out_text (uiout, "\n");
+           uiout->spaces (2 * depth);
+         uiout->field_string (NULL, "end");
+         uiout->text ("\n");
          list = list->next;
          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)
        {
-         ui_out_field_fmt (uiout, NULL, "if %s", list->line);
-         ui_out_text (uiout, "\n");
-         /* The true arm. */
+         uiout->field_fmt (NULL, "if %s", list->line);
+         uiout->text ("\n");
+         /* The true arm.  */
          print_command_lines (uiout, list->body_list[0], depth + 1);
 
          /* Show the false arm if it exists.  */
          if (list->body_count == 2)
            {
              if (depth)
-               ui_out_spaces (uiout, 2 * depth);
-             ui_out_field_string (uiout, NULL, "else");
-             ui_out_text (uiout, "\n");
+               uiout->spaces (2 * depth);
+             uiout->field_string (NULL, "else");
+             uiout->text ("\n");
              print_command_lines (uiout, list->body_list[1], depth + 1);
            }
 
          if (depth)
-           ui_out_spaces (uiout, 2 * depth);
-         ui_out_field_string (uiout, NULL, "end");
-         ui_out_text (uiout, "\n");
+           uiout->spaces (2 * depth);
+         uiout->field_string (NULL, "end");
+         uiout->text ("\n");
          list = list->next;
          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->line))
-           ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
+           uiout->field_fmt (NULL, "commands %s", list->line);
          else
-           ui_out_field_string (uiout, NULL, "commands");
-         ui_out_text (uiout, "\n");
+           uiout->field_string (NULL, "commands");
+         uiout->text ("\n");
          print_command_lines (uiout, *list->body_list, depth + 1);
          if (depth)
-           ui_out_spaces (uiout, 2 * depth);
-         ui_out_field_string (uiout, NULL, "end");
-         ui_out_text (uiout, "\n");
+           uiout->spaces (2 * depth);
+         uiout->field_string (NULL, "end");
+         uiout->text ("\n");
          list = list->next;
          continue;
        }
 
       if (list->control_type == python_control)
        {
-         ui_out_field_string (uiout, NULL, "python");
-         ui_out_text (uiout, "\n");
+         uiout->field_string (NULL, "python");
+         uiout->text ("\n");
          /* Don't indent python code at all.  */
          print_command_lines (uiout, *list->body_list, 0);
          if (depth)
-           ui_out_spaces (uiout, 2 * depth);
-         ui_out_field_string (uiout, NULL, "end");
-         ui_out_text (uiout, "\n");
+           uiout->spaces (2 * depth);
+         uiout->field_string (NULL, "end");
+         uiout->text ("\n");
          list = list->next;
          continue;
        }
 
-      /* ignore illegal command type and try next */
+      if (list->control_type == compile_control)
+       {
+         uiout->field_string (NULL, "compile expression");
+         uiout->text ("\n");
+         print_command_lines (uiout, *list->body_list, 0);
+         if (depth)
+           uiout->spaces (2 * depth);
+         uiout->field_string (NULL, "end");
+         uiout->text ("\n");
+         list = list->next;
+         continue;
+       }
+
+      if (list->control_type == guile_control)
+       {
+         uiout->field_string (NULL, "guile");
+         uiout->text ("\n");
+         print_command_lines (uiout, *list->body_list, depth + 1);
+         if (depth)
+           uiout->spaces (2 * depth);
+         uiout->field_string (NULL, "end");
+         uiout->text ("\n");
+         list = list->next;
+         continue;
+       }
+
+      /* Ignore illegal command type and try next.  */
       list = list->next;
     }                          /* while (list) */
 }
 
 /* Handle pre-post hooks.  */
 
-static void
-clear_hook_in_cleanup (void *data)
+class scoped_restore_hook_in
 {
-  struct cmd_list_element *c = data;
-  c->hook_in = 0; /* Allow hook to work again once it is complete */
-}
+public:
+
+  scoped_restore_hook_in (struct cmd_list_element *c)
+    : m_cmd (c)
+  {
+  }
+
+  ~scoped_restore_hook_in ()
+  {
+    m_cmd->hook_in = 0;
+  }
+
+  scoped_restore_hook_in (const scoped_restore_hook_in &) = delete;
+  scoped_restore_hook_in &operator= (const scoped_restore_hook_in &) = delete;
+
+private:
+
+  struct cmd_list_element *m_cmd;
+};
 
 void
 execute_cmd_pre_hook (struct cmd_list_element *c)
 {
   if ((c->hook_pre) && (!c->hook_in))
     {
-      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
-      c->hook_in = 1; /* Prevent recursive hooking */
-      execute_user_command (c->hook_pre, (char *) 0);
-      do_cleanups (cleanups);
+      scoped_restore_hook_in restore_hook (c);
+      c->hook_in = 1; /* Prevent recursive hooking.  */
+      execute_user_command (c->hook_pre, nullptr);
     }
 }
 
@@ -273,55 +378,39 @@ execute_cmd_post_hook (struct cmd_list_element *c)
 {
   if ((c->hook_post) && (!c->hook_in))
     {
-      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
-      c->hook_in = 1; /* Prevent recursive hooking */
-      execute_user_command (c->hook_post, (char *) 0);
-      do_cleanups (cleanups);
+      scoped_restore_hook_in restore_hook (c);
+      c->hook_in = 1; /* Prevent recursive hooking.  */
+      execute_user_command (c->hook_post, nullptr);
     }
 }
 
-/* Execute the command in CMD.  */
-static void
-do_restore_user_call_depth (void * call_depth)
-{      
-  int * depth = call_depth;
-  (*depth)--;
-  if ((*depth) == 0)
-    in_user_command = 0;
-}
-
-
 void
-execute_user_command (struct cmd_list_element *c, char *args)
+execute_user_command (struct cmd_list_element *c, const char *args)
 {
+  struct ui *ui = current_ui;
   struct command_line *cmdlines;
-  struct cleanup *old_chain;
   enum command_control_type ret;
-  static int user_call_depth = 0;
-  extern int max_user_call_depth;
-
-  old_chain = setup_user_args (args);
+  extern unsigned int max_user_call_depth;
 
   cmdlines = c->user_commands;
   if (cmdlines == 0)
     /* Null command */
     return;
 
-  if (++user_call_depth > max_user_call_depth)
-    error (_("Max user call depth exceeded -- command aborted."));
+  scoped_user_args_level push_user_args (args);
 
-  make_cleanup (do_restore_user_call_depth, &user_call_depth);
+  if (user_args_stack.size () > max_user_call_depth)
+    error (_("Max user call depth exceeded -- command aborted."));
 
   /* Set the instream to 0, indicating execution of a
      user-defined function.  */
-  make_cleanup (do_restore_instream_cleanup, instream);
-  instream = (FILE *) 0;
+  scoped_restore restore_instream
+    = make_scoped_restore (&ui->instream, nullptr);
 
-  /* Also set the global in_user_command, so that NULL instream is
-     not confused with Insight.  */
-  in_user_command = 1;
+  scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
-  command_nest_depth++;
+  scoped_restore save_nesting
+    = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
   while (cmdlines)
     {
       ret = execute_control_command (cmdlines);
@@ -332,12 +421,10 @@ execute_user_command (struct cmd_list_element *c, char *args)
        }
       cmdlines = cmdlines->next;
     }
-  command_nest_depth--;
-  do_cleanups (old_chain);
 }
 
-/* This function is called every time GDB prints a prompt.
-   It ensures that errors and the like to not confuse the command tracing.  */
+/* This function is called every time GDB prints a prompt.  It ensures
+   that errors and the like do not confuse the command tracing.  */
 
 void
 reset_command_nest_depth (void)
@@ -376,17 +463,16 @@ print_command_trace (const char *cmd)
   printf_filtered ("%s\n", cmd);
 }
 
-enum command_control_type
-execute_control_command (struct command_line *cmd)
+/* Helper for execute_control_command.  */
+
+static enum command_control_type
+execute_control_command_1 (struct command_line *cmd)
 {
-  struct expression *expr;
   struct command_line *current;
-  struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
   struct value *val;
   struct value *val_mark;
   int loop;
   enum command_control_type ret;
-  char *new_line;
 
   /* Start by assuming failure, if a problem is detected, the code
      below will simply "break" out of the switch.  */
@@ -395,14 +481,13 @@ execute_control_command (struct command_line *cmd)
   switch (cmd->control_type)
     {
     case simple_control:
-      /* A simple command, execute it and return.  */
-      new_line = insert_args (cmd->line);
-      if (!new_line)
+      {
+       /* A simple command, execute it and return.  */
+       std::string new_line = insert_user_defined_cmd_args (cmd->line);
+       execute_command (new_line.c_str (), 0);
+       ret = cmd->control_type;
        break;
-      make_cleanup (free_current_contents, &new_line);
-      execute_command (new_line, 0);
-      ret = cmd->control_type;
-      break;
+      }
 
     case continue_control:
       print_command_trace ("loop_continue");
@@ -422,17 +507,15 @@ execute_control_command (struct command_line *cmd)
 
     case while_control:
       {
-       char *buffer = alloca (strlen (cmd->line) + 7);
-       sprintf (buffer, "while %s", cmd->line);
+       int len = strlen (cmd->line) + 7;
+       char *buffer = (char *) alloca (len);
+
+       xsnprintf (buffer, len, "while %s", cmd->line);
        print_command_trace (buffer);
 
        /* Parse the loop control expression for the while statement.  */
-       new_line = insert_args (cmd->line);
-       if (!new_line)
-         break;
-       make_cleanup (free_current_contents, &new_line);
-       expr = parse_expression (new_line);
-       make_cleanup (free_current_contents, &expr);
+       std::string new_line = insert_user_defined_cmd_args (cmd->line);
+       expression_up expr = parse_expression (new_line.c_str ());
 
        ret = simple_control;
        loop = 1;
@@ -446,7 +529,7 @@ execute_control_command (struct command_line *cmd)
 
            /* Evaluate the expression.  */
            val_mark = value_mark ();
-           val = evaluate_expression (expr);
+           val = evaluate_expression (expr.get ());
            cond_result = value_true (val);
            value_free_to_mark (val_mark);
 
@@ -458,9 +541,9 @@ execute_control_command (struct command_line *cmd)
            current = *cmd->body_list;
            while (current)
              {
-               command_nest_depth++;
-               ret = execute_control_command (current);
-               command_nest_depth--;
+               scoped_restore save_nesting
+                 = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
+               ret = execute_control_command_1 (current);
 
                /* If we got an error, or a "break" command, then stop
                   looping.  */
@@ -489,27 +572,25 @@ execute_control_command (struct command_line *cmd)
 
     case if_control:
       {
-       char *buffer = alloca (strlen (cmd->line) + 4);
-       sprintf (buffer, "if %s", cmd->line);
+       int len = strlen (cmd->line) + 4;
+       char *buffer = (char *) alloca (len);
+
+       xsnprintf (buffer, len, "if %s", cmd->line);
        print_command_trace (buffer);
 
-       new_line = insert_args (cmd->line);
-       if (!new_line)
-         break;
-       make_cleanup (free_current_contents, &new_line);
        /* Parse the conditional for the if statement.  */
-       expr = parse_expression (new_line);
-       make_cleanup (free_current_contents, &expr);
+       std::string new_line = insert_user_defined_cmd_args (cmd->line);
+       expression_up expr = parse_expression (new_line.c_str ());
 
        current = NULL;
        ret = simple_control;
 
        /* Evaluate the conditional.  */
        val_mark = value_mark ();
-       val = evaluate_expression (expr);
+       val = evaluate_expression (expr.get ());
 
-       /* Choose which arm to take commands from based on the value of the
-          conditional expression.  */
+       /* Choose which arm to take commands from based on the value
+          of the conditional expression.  */
        if (value_true (val))
          current = *cmd->body_list;
        else if (cmd->body_count == 2)
@@ -519,9 +600,9 @@ execute_control_command (struct command_line *cmd)
        /* Execute commands in the given arm.  */
        while (current)
          {
-           command_nest_depth++;
-           ret = execute_control_command (current);
-           command_nest_depth--;
+           scoped_restore save_nesting
+             = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
+           ret = execute_control_command_1 (current);
 
            /* If we got an error, get out.  */
            if (ret != simple_control)
@@ -533,20 +614,26 @@ execute_control_command (struct command_line *cmd)
 
        break;
       }
+
     case commands_control:
       {
-       /* Breakpoint commands list, record the commands in the breakpoint's
-          command list and return.  */
-       new_line = insert_args (cmd->line);
-       if (!new_line)
-         break;
-       make_cleanup (free_current_contents, &new_line);
-       ret = commands_from_control_command (new_line, cmd);
+       /* Breakpoint commands list, record the commands in the
+          breakpoint's command list and return.  */
+       std::string new_line = insert_user_defined_cmd_args (cmd->line);
+       ret = commands_from_control_command (new_line.c_str (), cmd);
        break;
       }
+
+    case compile_control:
+      eval_compile_command (cmd, NULL, cmd->control_u.compile.scope,
+                           cmd->control_u.compile.scope_data);
+      ret = simple_control;
+      break;
+
     case python_control:
+    case guile_control:
       {
-       eval_python_from_control_command (cmd);
+       eval_ext_lang_from_control_command (cmd);
        ret = simple_control;
        break;
       }
@@ -556,13 +643,23 @@ execute_control_command (struct command_line *cmd)
       break;
     }
 
-  do_cleanups (old_chain);
-
   return ret;
 }
 
+enum command_control_type
+execute_control_command (struct command_line *cmd)
+{
+  /* Make sure we use the console uiout.  It's possible that we are executing
+     breakpoint commands while running the MI interpreter.  */
+  interp *console = interp_lookup (current_ui, INTERP_CONSOLE);
+  scoped_restore save_uiout
+    = make_scoped_restore (&current_uiout, interp_ui_out (console));
+
+  return execute_control_command_1 (cmd);
+}
+
 /* Like execute_control_command, but first set
-   suppress_next_print_command_trace.   */
+   suppress_next_print_command_trace.  */
 
 enum command_control_type
 execute_control_command_untraced (struct command_line *cmd)
@@ -575,97 +672,63 @@ execute_control_command_untraced (struct command_line *cmd)
 /* "while" command support.  Executes a body of statements while the
    loop condition is nonzero.  */
 
-void
-while_command (char *arg, int from_tty)
+static void
+while_command (const char *arg, int from_tty)
 {
-  struct command_line *command = NULL;
-
   control_level = 1;
-  command = get_command_line (while_control, arg);
+  command_line_up command = get_command_line (while_control, arg);
 
   if (command == NULL)
     return;
 
-  execute_control_command_untraced (command);
-  free_command_lines (&command);
+  scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
+
+  execute_control_command_untraced (command.get ());
 }
 
 /* "if" command support.  Execute either the true or false arm depending
    on the value of the if conditional.  */
 
-void
-if_command (char *arg, int from_tty)
+static void
+if_command (const char *arg, int from_tty)
 {
-  struct command_line *command = NULL;
-
   control_level = 1;
-  command = get_command_line (if_control, arg);
+  command_line_up command = get_command_line (if_control, arg);
 
   if (command == NULL)
     return;
 
-  execute_control_command_untraced (command);
-  free_command_lines (&command);
-}
+  scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
-/* Cleanup */
-static void
-arg_cleanup (void *ignore)
-{
-  struct user_args *oargs = user_args;
-  if (!user_args)
-    internal_error (__FILE__, __LINE__,
-                   _("arg_cleanup called with no user args.\n"));
-
-  user_args = user_args->next;
-  xfree (oargs->command);
-  xfree (oargs);
+  execute_control_command_untraced (command.get ());
 }
 
-/* Bind the incomming arguments for a user defined command to
-   $arg0, $arg1 ... $argMAXUSERARGS.  */
+/* Bind the incoming arguments for a user defined command to $arg0,
+   $arg1 ... $argN.  */
 
-static struct cleanup *
-setup_user_args (char *p)
+user_args::user_args (const char *command_line)
 {
-  struct user_args *args;
-  struct cleanup *old_chain;
-  unsigned int arg_count = 0;
-
-  args = (struct user_args *) xmalloc (sizeof (struct user_args));
-  memset (args, 0, sizeof (struct user_args));
+  const char *p;
 
-  args->next = user_args;
-  user_args = args;
-
-  old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
-
-  if (p == NULL)
-    return old_chain;
+  if (command_line == NULL)
+    return;
 
-  user_args->command = p = xstrdup (p);
+  m_command_line = command_line;
+  p = m_command_line.c_str ();
 
   while (*p)
     {
-      char *start_arg;
+      const char *start_arg;
       int squote = 0;
       int dquote = 0;
       int bsquote = 0;
 
-      if (arg_count >= MAXUSERARGS)
-       {
-         error (_("user defined function may only have %d arguments."),
-                MAXUSERARGS);
-         return old_chain;
-       }
-
       /* Strip whitespace.  */
       while (*p == ' ' || *p == '\t')
        p++;
 
       /* P now points to an argument.  */
       start_arg = p;
-      user_args->a[arg_count].arg = p;
 
       /* Get to the end of this argument.  */
       while (*p)
@@ -699,22 +762,19 @@ setup_user_args (char *p)
            }
        }
 
-      user_args->a[arg_count].len = p - start_arg;
-      arg_count++;
-      user_args->count++;
+      m_args.emplace_back (start_arg, p - start_arg);
     }
-  return old_chain;
 }
 
-/* Given character string P, return a point to the first argument ($arg),
-   or NULL if P contains no arguments.  */
+/* Given character string P, return a point to the first argument
+   ($arg), or NULL if P contains no arguments.  */
 
-static char *
-locate_arg (char *p)
+static const char *
+locate_arg (const char *p)
 {
   while ((p = strchr (p, '$')))
     {
-      if (strncmp (p, "$arg", 4) == 0
+      if (startswith (p, "$arg")
          && (isdigit (p[4]) || p[4] == 'c'))
        return p;
       p++;
@@ -722,94 +782,60 @@ locate_arg (char *p)
   return NULL;
 }
 
-/* Insert the user defined arguments stored in user_arg into the $arg
-   arguments found in line, with the updated copy being placed into nline.  */
+/* See cli-script.h.  */
 
-static char *
-insert_args (char *line)
+std::string
+insert_user_defined_cmd_args (const char *line)
 {
-  char *p, *save_line, *new_line;
-  unsigned len, i;
-
-  /* If we are not in a user-defined function, treat $argc, $arg0, et
+  /* If we are not in a user-defined command, treat $argc, $arg0, et
      cetera as normal convenience variables.  */
-  if (user_args == NULL)
-    return xstrdup (line);
+  if (user_args_stack.empty ())
+    return line;
 
-  /* First we need to know how much memory to allocate for the new line.  */
-  save_line = line;
-  len = 0;
-  while ((p = locate_arg (line)))
-    {
-      len += p - line;
-      i = p[4] - '0';
-
-      if (p[4] == 'c')
-       {
-         /* $argc.  Number will be <=10.  */
-         len += user_args->count == 10 ? 2 : 1;
-       }
-      else if (i >= user_args->count)
-       {
-         error (_("Missing argument %d in user function."), i);
-         return NULL;
-       }
-      else
-       {
-         len += user_args->a[i].len;
-       }
-      line = p + 5;
-    }
-
-  /* Don't forget the tail.  */
-  len += strlen (line);
-
-  /* Allocate space for the new line and fill it in.  */
-  new_line = (char *) xmalloc (len + 1);
-  if (new_line == NULL)
-    return NULL;
+  const std::unique_ptr<user_args> &args = user_args_stack.back ();
+  return args->insert_args (line);
+}
 
-  /* Restore pointer to beginning of old line.  */
-  line = save_line;
+/* Insert the user defined arguments stored in user_args into the $arg
+   arguments found in line.  */
 
-  /* Save pointer to beginning of new line.  */
-  save_line = new_line;
+std::string
+user_args::insert_args (const char *line) const
+{
+  std::string new_line;
+  const char *p;
 
   while ((p = locate_arg (line)))
     {
-      int i, len;
-
-      memcpy (new_line, line, p - line);
-      new_line += p - line;
+      new_line.append (line, p - line);
 
       if (p[4] == 'c')
        {
-         gdb_assert (user_args->count >= 0 && user_args->count <= 10);
-         if (user_args->count == 10)
-           {
-             *(new_line++) = '1';
-             *(new_line++) = '0';
-           }
-         else
-           *(new_line++) = user_args->count + '0';
+         new_line += std::to_string (m_args.size ());
+         line = p + 5;
        }
       else
        {
-         i = p[4] - '0';
-         len = user_args->a[i].len;
-         if (len)
-         {
-           memcpy (new_line, user_args->a[i].arg, len);
-           new_line += len;
-         }
+         char *tmp;
+         unsigned long i;
+
+         errno = 0;
+         i = strtoul (p + 4, &tmp, 10);
+         if ((i == 0 && tmp == p + 4) || errno != 0)
+           line = p + 4;
+         else if (i >= m_args.size ())
+           error (_("Missing argument %ld in user function."), i);
+         else
+           {
+             new_line.append (m_args[i].str, m_args[i].len);
+             line = tmp;
+           }
        }
-      line = p + 5;
     }
   /* Don't forget the tail.  */
-  strcpy (new_line, line);
+  new_line.append (line);
 
-  /* Return a pointer to the beginning of the new line.  */
-  return save_line;
+  return new_line;
 }
 
 \f
@@ -829,32 +855,33 @@ realloc_body_list (struct command_line *command, int new_length)
   if (new_length <= n)
     return;
 
-  body_list = (struct command_line **)
-    xmalloc (sizeof (struct command_line *) * new_length);
+  body_list = XCNEWVEC (struct command_line *, new_length);
 
   memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
-  memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
 
   xfree (command->body_list);
   command->body_list = body_list;
   command->body_count = new_length;
 }
 
-/* Read next line from stdout.  Passed to read_command_line_1 and
+/* Read next line from stdin.  Passed to read_command_line_1 and
    recurse_read_control_structure whenever we need to read commands
-   from stdout.  */
+   from stdin.  */
 
 static char *
-read_next_line ()
+read_next_line (void)
 {
+  struct ui *ui = current_ui;
   char *prompt_ptr, control_prompt[256];
   int i = 0;
+  int from_tty = ui->instream == ui->stdin_stream;
 
   if (control_level >= 254)
     error (_("Control nesting too deep!"));
 
   /* Set a prompt based on the nesting of the control commands.  */
-  if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
+  if (from_tty
+      || (ui->instream == 0 && deprecated_readline_hook != NULL))
     {
       for (i = 0; i < control_level; i++)
        control_prompt[i] = ' ';
@@ -865,100 +892,148 @@ read_next_line ()
   else
     prompt_ptr = NULL;
 
-  return command_line_input (prompt_ptr, instream == stdin, "commands");
+  return command_line_input (prompt_ptr, from_tty, "commands");
 }
 
-/* Process one input line.  If the command is an "end",
-   return such an indication to the caller.  If PARSE_COMMANDS is true,
-   strip leading whitespace (trailing whitespace is always stripped)
-   in the line, attempt to recognize GDB control commands, and also
-   return an indication if the command is an "else" or a nop.
+/* Return true if CMD's name is NAME.  */
+
+static bool
+command_name_equals (struct cmd_list_element *cmd, const char *name)
+{
+  return (cmd != NULL
+         && cmd != CMD_LIST_AMBIGUOUS
+         && strcmp (cmd->name, name) == 0);
+}
+
+/* Given an input line P, skip the command and return a pointer to the
+   first argument.  */
+
+static const char *
+line_first_arg (const char *p)
+{
+  const char *first_arg = p + find_command_name_length (p);
+
+  return skip_spaces (first_arg); 
+}
+
+/* Process one input line.  If the command is an "end", return such an
+   indication to the caller.  If PARSE_COMMANDS is true, strip leading
+   whitespace (trailing whitespace is always stripped) in the line,
+   attempt to recognize GDB control commands, and also return an
+   indication if the command is an "else" or a nop.
+
    Otherwise, only "end" is recognized.  */
 
 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,
+                  void (*validator)(char *, void *), void *closure)
 {
-  char *p1;
+  char *p_end;
+  char *p_start;
   int not_handled = 0;
 
   /* Not sure what to do here.  */
   if (p == NULL)
     return end_command;
 
-  if (parse_commands)
-    {
-      /* Strip leading whitespace.  */
-      while (*p == ' ' || *p == '\t')
-       p++;
-    }
-
   /* Strip trailing whitespace.  */
-  p1 = p + strlen (p);
-  while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
-    p1--;
-
-  /* Is this the end of a simple, while, or if control structure?  */
-  if (p1 - p == 3 && !strncmp (p, "end", 3))
+  p_end = p + strlen (p);
+  while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
+    p_end--;
+
+  p_start = p;
+  /* Strip leading whitespace.  */
+  while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
+    p_start++;
+
+  /* 'end' is always recognized, regardless of parse_commands value.
+     We also permit whitespace before end and after.  */
+  if (p_end - p_start == 3 && startswith (p_start, "end"))
     return end_command;
 
   if (parse_commands)
     {
+      /* Resolve command abbreviations (e.g. 'ws' for 'while-stepping').  */
+      const char *cmd_name = p;
+      struct cmd_list_element *cmd
+       = lookup_cmd_1 (&cmd_name, cmdlist, NULL, 1);
+      cmd_name = skip_spaces (cmd_name);
+      bool inline_cmd = *cmd_name != '\0';
+
+      /* If commands are parsed, we skip initial spaces.  Otherwise,
+        which is the case for Python commands and documentation
+        (see the 'document' command), spaces are preserved.  */
+      p = p_start;
+
       /* Blanks and comments don't really do anything, but we need to
-        distinguish them from else, end and other commands which can be
-        executed.  */
-      if (p1 == p || p[0] == '#')
+        distinguish them from else, end and other commands which can
+        be executed.  */
+      if (p_end == p || p[0] == '#')
        return nop_command;
 
       /* Is the else clause of an if control structure?  */
-      if (p1 - p == 4 && !strncmp (p, "else", 4))
+      if (p_end - p == 4 && startswith (p, "else"))
        return else_command;
 
-      /* Check for while, if, break, continue, etc and build a new command
-        line structure for them.  */
-      if (p1 - p > 5 && !strncmp (p, "while", 5))
+      /* Check for while, if, break, continue, etc and build a new
+        command line structure for them.  */
+      if (command_name_equals (cmd, "while-stepping"))
        {
-         char *first_arg;
-         first_arg = p + 5;
-         while (first_arg < p1 && isspace (*first_arg))
-           first_arg++;
-         *command = build_command_line (while_control, first_arg);
+         /* Because validate_actionline and encode_action lookup
+            command's line as command, we need the line to
+            include 'while-stepping'.
+
+            For 'ws' alias, the command will have 'ws', not expanded
+            to 'while-stepping'.  This is intentional -- we don't
+            really want frontend to send a command list with 'ws',
+            and next break-info returning command line with
+            'while-stepping'.  This should work, but might cause the
+            breakpoint to be marked as changed while it's actually
+            not.  */
+         *command = build_command_line (while_stepping_control, p);
        }
-      else if (p1 - p > 2 && !strncmp (p, "if", 2))
+      else if (command_name_equals (cmd, "while"))
        {
-         char *first_arg;
-         first_arg = p + 2;
-         while (first_arg < p1 && isspace (*first_arg))
-           first_arg++;
-         *command = build_command_line (if_control, first_arg);
+         *command = build_command_line (while_control, line_first_arg (p));
        }
-      else if (p1 - p >= 8 && !strncmp (p, "commands", 8))
+      else if (command_name_equals (cmd, "if"))
        {
-         char *first_arg;
-         first_arg = p + 8;
-         while (first_arg < p1 && isspace (*first_arg))
-           first_arg++;
-         *command = build_command_line (commands_control, first_arg);
+         *command = build_command_line (if_control, line_first_arg (p));
        }
-      else if (p1 - p == 6 && !strncmp (p, "python", 6))
+      else if (command_name_equals (cmd, "commands"))
+       {
+         *command = build_command_line (commands_control, line_first_arg (p));
+       }
+      else if (command_name_equals (cmd, "python") && !inline_cmd)
        {
          /* Note that we ignore the inline "python command" form
             here.  */
          *command = build_command_line (python_control, "");
        }
-      else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
+      else if (command_name_equals (cmd, "compile") && !inline_cmd)
        {
-         *command = (struct command_line *)
-           xmalloc (sizeof (struct command_line));
+         /* Note that we ignore the inline "compile command" form
+            here.  */
+         *command = build_command_line (compile_control, "");
+         (*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
+       }
+      else if (command_name_equals (cmd, "guile") && !inline_cmd)
+       {
+         /* Note that we ignore the inline "guile command" form here.  */
+         *command = build_command_line (guile_control, "");
+       }
+      else if (p_end - p == 10 && startswith (p, "loop_break"))
+       {
+         *command = XNEW (struct command_line);
          (*command)->next = NULL;
          (*command)->line = NULL;
          (*command)->control_type = break_control;
          (*command)->body_count = 0;
          (*command)->body_list = NULL;
        }
-      else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
+      else if (p_end - p == 13 && startswith (p, "loop_continue"))
        {
-         *command = (struct command_line *)
-           xmalloc (sizeof (struct command_line));
+         *command = XNEW (struct command_line);
          (*command)->next = NULL;
          (*command)->line = NULL;
          (*command)->control_type = continue_control;
@@ -972,34 +1047,47 @@ process_next_line (char *p, struct command_line **command, int parse_commands)
   if (!parse_commands || not_handled)
     {
       /* A normal command.  */
-      *command = (struct command_line *)
-       xmalloc (sizeof (struct command_line));
+      *command = XNEW (struct command_line);
       (*command)->next = NULL;
-      (*command)->line = savestring (p, p1 - p);
+      (*command)->line = savestring (p, p_end - p);
       (*command)->control_type = simple_control;
       (*command)->body_count = 0;
       (*command)->body_list = NULL;
     }
 
+  if (validator)
+    {
+
+      TRY
+       {
+         validator ((*command)->line, closure);
+       }
+      CATCH (ex, RETURN_MASK_ALL)
+       {
+         xfree (*command);
+         throw_exception (ex);
+       }
+      END_CATCH
+    }
+
   /* Nothing special.  */
   return ok_command;
 }
 
-/* Recursively read in the control structures and create a command_line 
-   structure from them.  Use read_next_line_func to obtain lines of
-   the command.
-
-*/
+/* Recursively read in the control structures and create a
+   command_line structure from them.  Use read_next_line_func to
+   obtain lines of the command.  */
 
 static enum command_control_type
-recurse_read_control_structure (char * (*read_next_line_func) (), 
-                               struct command_line *current_cmd)
+recurse_read_control_structure (char * (*read_next_line_func) (void),
+                               struct command_line *current_cmd,
+                               void (*validator)(char *, void *),
+                               void *closure)
 {
   int current_body, i;
   enum misc_command_type val;
   enum command_control_type ret;
   struct command_line **body_ptr, *child_tail, *next;
-  char *p;
 
   child_tail = NULL;
   current_body = 1;
@@ -1018,7 +1106,10 @@ recurse_read_control_structure (char * (*read_next_line_func) (),
 
       next = NULL;
       val = process_next_line (read_next_line_func (), &next, 
-                              current_cmd->control_type != python_control);
+                              current_cmd->control_type != python_control
+                              && current_cmd->control_type != guile_control
+                              && current_cmd->control_type != compile_control,
+                              validator, closure);
 
       /* Just skip blanks and comments.  */
       if (val == nop_command)
@@ -1026,10 +1117,7 @@ recurse_read_control_structure (char * (*read_next_line_func) (),
 
       if (val == end_command)
        {
-         if (current_cmd->control_type == while_control
-             || current_cmd->control_type == if_control
-             || current_cmd->control_type == python_control
-             || current_cmd->control_type == commands_control)
+         if (multi_line_command_p (current_cmd->control_type))
            {
              /* Success reading an entire canned sequence of commands.  */
              ret = simple_control;
@@ -1078,13 +1166,11 @@ recurse_read_control_structure (char * (*read_next_line_func) (),
 
       /* If the latest line is another control structure, then recurse
          on it.  */
-      if (next->control_type == while_control
-         || next->control_type == if_control
-         || next->control_type == python_control
-         || next->control_type == commands_control)
+      if (multi_line_command_p (next->control_type))
        {
          control_level++;
-         ret = recurse_read_control_structure (read_next_line_func, next);
+         ret = recurse_read_control_structure (read_next_line_func, next,
+                                               validator, closure);
          control_level--;
 
          if (ret != simple_control)
@@ -1108,17 +1194,17 @@ recurse_read_control_structure (char * (*read_next_line_func) (),
 
 #define END_MESSAGE "End with a line saying just \"end\"."
 
-struct command_line *
-read_command_lines (char *prompt_arg, int from_tty, int parse_commands)
+command_line_up
+read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
+                   void (*validator)(char *, void *), void *closure)
 {
-  struct command_line *head;
-
-  if (from_tty && input_from_terminal_p ())
+  if (from_tty && input_interactive_p (current_ui))
     {
       if (deprecated_readline_begin_hook)
        {
-         /* Note - intentional to merge messages with no newline */
-         (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg, END_MESSAGE);
+         /* Note - intentional to merge messages with no newline.  */
+         (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg,
+                                            END_MESSAGE);
        }
       else
        {
@@ -1127,9 +1213,23 @@ read_command_lines (char *prompt_arg, int from_tty, int parse_commands)
        }
     }
 
-  head = read_command_lines_1 (read_next_line, parse_commands);
 
-  if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
+  /* Reading commands assumes the CLI behavior, so temporarily
+     override the current interpreter with CLI.  */
+  command_line_up head;
+  if (current_interp_named_p (INTERP_CONSOLE))
+    head = read_command_lines_1 (read_next_line, parse_commands,
+                                validator, closure);
+  else
+    {
+      scoped_restore_interp interp_restorer (INTERP_CONSOLE);
+
+      head = read_command_lines_1 (read_next_line, parse_commands,
+                                  validator, closure);
+    }
+
+  if (from_tty && input_interactive_p (current_ui)
+      && deprecated_readline_end_hook)
     {
       (*deprecated_readline_end_hook) ();
     }
@@ -1139,22 +1239,23 @@ read_command_lines (char *prompt_arg, int from_tty, int parse_commands)
 /* Act the same way as read_command_lines, except that each new line is
    obtained using READ_NEXT_LINE_FUNC.  */
 
-struct command_line *
-read_command_lines_1 (char * (*read_next_line_func) (), int parse_commands)
+command_line_up
+read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
+                     void (*validator)(char *, void *), void *closure)
 {
-  struct command_line *head, *tail, *next;
-  struct cleanup *old_chain;
+  struct command_line *tail, *next;
+  command_line_up head;
   enum command_control_type ret;
   enum misc_command_type val;
 
   control_level = 0;
-  head = tail = NULL;
-  old_chain = NULL;
+  tail = NULL;
 
   while (1)
     {
       dont_repeat ();
-      val = process_next_line (read_next_line_func (), &next, parse_commands);
+      val = process_next_line (read_next_line_func (), &next, parse_commands,
+                              validator, closure);
 
       /* Ignore blank lines or comments.  */
       if (val == nop_command)
@@ -1172,13 +1273,11 @@ read_command_lines_1 (char * (*read_next_line_func) (), int parse_commands)
          break;
        }
 
-      if (next->control_type == while_control
-         || next->control_type == if_control
-         || next->control_type == python_control
-         || next->control_type == commands_control)
+      if (multi_line_command_p (next->control_type))
        {
          control_level++;
-         ret = recurse_read_control_structure (read_next_line_func, next);
+         ret = recurse_read_control_structure (read_next_line_func, next,
+                                               validator, closure);
          control_level--;
 
          if (ret == invalid_control)
@@ -1191,23 +1290,15 @@ read_command_lines_1 (char * (*read_next_line_func) (), int parse_commands)
        }
       else
        {
-         head = next;
-         old_chain = make_cleanup_free_command_lines (&head);
+         head.reset (next);
        }
       tail = next;
     }
 
   dont_repeat ();
 
-  if (head)
-    {
-      if (ret != invalid_control)
-       {
-         discard_cleanups (old_chain);
-       }
-      else
-       do_cleanups (old_chain);
-    }
+  if (ret == invalid_control)
+    return NULL;
 
   return head;
 }
@@ -1238,28 +1329,16 @@ free_command_lines (struct command_line **lptr)
   *lptr = NULL;
 }
 
-static void
-do_free_command_lines_cleanup (void *arg)
-{
-  free_command_lines (arg);
-}
-
-struct cleanup *
-make_cleanup_free_command_lines (struct command_line **arg)
-{
-  return make_cleanup (do_free_command_lines_cleanup, arg);
-}
-
-struct command_line *
+command_line_up
 copy_command_lines (struct command_line *cmds)
 {
   struct command_line *result = NULL;
 
   if (cmds)
     {
-      result = (struct command_line *) xmalloc (sizeof (struct command_line));
+      result = XNEW (struct command_line);
 
-      result->next = copy_command_lines (cmds->next);
+      result->next = copy_command_lines (cmds->next).release ();
       result->line = xstrdup (cmds->line);
       result->control_type = cmds->control_type;
       result->body_count = cmds->body_count;
@@ -1267,17 +1346,17 @@ copy_command_lines (struct command_line *cmds)
         {
           int i;
 
-          result->body_list = (struct command_line **)
-            xmalloc (sizeof (struct command_line *) * cmds->body_count);
+          result->body_list = XNEWVEC (struct command_line *, cmds->body_count);
 
           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]).release ();
         }
       else
         result->body_list = NULL;
     }
 
-  return result;
+  return command_line_up (result);
 }
 \f
 /* Validate that *COMNAME is a valid name for a command.  Return the
@@ -1287,10 +1366,10 @@ copy_command_lines (struct command_line *cmds)
    prefix.  */
 
 static struct cmd_list_element **
-validate_comname (char **comname)
+validate_comname (const char **comname)
 {
   struct cmd_list_element **list = &cmdlist;
-  char *p, *last_word;
+  const char *p, *last_word;
 
   if (*comname == 0)
     error_no_arg (_("name of command to define"));
@@ -1307,18 +1386,16 @@ validate_comname (char **comname)
   if (last_word != *comname)
     {
       struct cmd_list_element *c;
-      char saved_char, *tem = *comname;
 
       /* Separate the prefix and the command.  */
-      saved_char = last_word[-1];
-      last_word[-1] = '\0';
+      std::string prefix (*comname, last_word - 1);
+      const char *tem = prefix.c_str ();
 
       c = lookup_cmd (&tem, cmdlist, "", 0, 1);
       if (c->prefixlist == NULL)
-       error (_("\"%s\" is not a prefix command."), *comname);
+       error (_("\"%s\" is not a prefix command."), prefix.c_str ());
 
       list = c->prefixlist;
-      last_word[-1] = saved_char;
       *comname = last_word;
     }
 
@@ -1335,12 +1412,12 @@ validate_comname (char **comname)
 
 /* This is just a placeholder in the command data structures.  */
 static void
-user_defined_command (char *ignore, int from_tty)
+user_defined_command (const char *ignore, int from_tty)
 {
 }
 
-void
-define_command (char *comname, int from_tty)
+static void
+define_command (const char *comname, int from_tty)
 {
 #define MAX_TMPBUF 128   
   enum cmd_hook_type
@@ -1349,9 +1426,8 @@ define_command (char *comname, int from_tty)
       CMD_PRE_HOOK,
       CMD_POST_HOOK
     };
-  struct command_line *cmds;
-  struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
-  char *tem, *tem2, *comfull;
+  struct cmd_list_element *c, *newc, *hookc = 0, **list;
+  const char *tem, *comfull;
   char tmpbuf[MAX_TMPBUF];
   int  hook_type      = CMD_NO_HOOK;
   int  hook_name_size = 0;
@@ -1373,7 +1449,8 @@ define_command (char *comname, int from_tty)
   if (c)
     {
       int q;
-      if (c->class == class_user || c->class == class_alias)
+
+      if (c->theclass == class_user || c->theclass == class_alias)
        q = query (_("Redefine command \"%s\"? "), c->name);
       else
        q = query (_("Really redefine built-in command \"%s\"? "), c->name);
@@ -1405,7 +1482,8 @@ define_command (char *comname, int from_tty)
        hookc = 0;
       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);
          if (!query (_("Proceed? ")))
            error (_("Not confirmed."));
@@ -1414,22 +1492,17 @@ define_command (char *comname, int from_tty)
 
   comname = xstrdup (comname);
 
-  /* If the rest of the commands will be case insensitive, this one
-     should behave in the same manner. */
-  for (tem = comname; *tem; tem++)
-    if (isupper (*tem))
-      *tem = tolower (*tem);
-
-  sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
-  cmds = read_command_lines (tmpbuf, from_tty, 1);
+  xsnprintf (tmpbuf, sizeof (tmpbuf),
+            "Type commands for definition of \"%s\".", comfull);
+  command_line_up cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
 
-  if (c && c->class == class_user)
+  if (c && c->theclass == class_user)
     free_command_lines (&c->user_commands);
 
   newc = add_cmd (comname, class_user, user_defined_command,
-                 (c && c->class == class_user)
+                 (c && c->theclass == class_user)
                  ? c->doc : xstrdup ("User-defined."), list);
-  newc->user_commands = cmds;
+  newc->user_commands = cmds.release ();
 
   /* If this new command is a hook, then mark both commands as being
      tied.  */
@@ -1439,25 +1512,26 @@ define_command (char *comname, int from_tty)
         {
         case CMD_PRE_HOOK:
           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;
         case CMD_POST_HOOK:
           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;
         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"));
         }
     }
 }
 
-void
-document_command (char *comname, int from_tty)
+static void
+document_command (const char *comname, int from_tty)
 {
-  struct command_line *doclines;
   struct cmd_list_element *c, **list;
-  char *tem, *comfull;
+  const char *tem;
+  const char *comfull;
   char tmpbuf[128];
 
   comfull = comname;
@@ -1466,108 +1540,66 @@ document_command (char *comname, int from_tty)
   tem = comname;
   c = lookup_cmd (&tem, *list, "", 0, 1);
 
-  if (c->class != class_user)
+  if (c->theclass != class_user)
     error (_("Command \"%s\" is built-in."), comfull);
 
-  sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
-  doclines = read_command_lines (tmpbuf, from_tty, 0);
+  xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
+            comfull);
+  command_line_up doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
 
   if (c->doc)
-    xfree (c->doc);
+    xfree ((char *) c->doc);
 
   {
     struct command_line *cl1;
     int len = 0;
+    char *doc;
 
-    for (cl1 = doclines; cl1; cl1 = cl1->next)
+    for (cl1 = doclines.get (); cl1; cl1 = cl1->next)
       len += strlen (cl1->line) + 1;
 
-    c->doc = (char *) xmalloc (len + 1);
-    *c->doc = 0;
+    doc = (char *) xmalloc (len + 1);
+    *doc = 0;
 
-    for (cl1 = doclines; cl1; cl1 = cl1->next)
+    for (cl1 = doclines.get (); cl1; cl1 = cl1->next)
       {
-       strcat (c->doc, cl1->line);
+       strcat (doc, cl1->line);
        if (cl1->next)
-         strcat (c->doc, "\n");
+         strcat (doc, "\n");
       }
-  }
 
-  free_command_lines (&doclines);
+    c->doc = doc;
+  }
 }
 \f
-struct source_cleanup_lines_args
-{
-  int old_line;
-  char *old_file;
-};
-
-static void
-source_cleanup_lines (void *args)
-{
-  struct source_cleanup_lines_args *p =
-  (struct source_cleanup_lines_args *) args;
-  source_line_number = p->old_line;
-  source_file_name = p->old_file;
-}
-
-struct wrapped_read_command_file_args
-{
-  FILE *stream;
-};
-
-static void
-wrapped_read_command_file (struct ui_out *uiout, void *data)
-{
-  struct wrapped_read_command_file_args *args = data;
-  read_command_file (args->stream);
-}
-
-/* Used to implement source_command */
+/* Used to implement source_command.  */
 
 void
-script_from_file (FILE *stream, char *file)
+script_from_file (FILE *stream, const char *file)
 {
-  struct cleanup *old_cleanups;
-  struct source_cleanup_lines_args old_lines;
-  int needed_length;
-
   if (stream == NULL)
     internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
 
-  old_cleanups = make_cleanup_fclose (stream);
+  scoped_restore restore_line_number
+    = make_scoped_restore (&source_line_number, 0);
+  scoped_restore resotre_file
+    = make_scoped_restore (&source_file_name, file);
 
-  old_lines.old_line = source_line_number;
-  old_lines.old_file = source_file_name;
-  make_cleanup (source_cleanup_lines, &old_lines);
-  source_line_number = 0;
-  source_file_name = file;
-  /* This will get set every time we read a line.  So it won't stay "" for
-     long.  */
-  error_pre_print = "";
+  scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
-  {
-    struct gdb_exception e;
-    struct wrapped_read_command_file_args args;
-    args.stream = stream;
-    e = catch_exception (uiout, wrapped_read_command_file, &args,
-                        RETURN_MASK_ERROR);
-    switch (e.reason)
-      {
-      case 0:
-       break;
-      case RETURN_ERROR:
-       /* Re-throw the error, but with the file name information
-          prepended.  */
-       throw_error (e.error,
-                    _("%s:%d: Error in sourced command file:\n%s"),
-                    source_file_name, source_line_number, e.message);
-      default:
-       internal_error (__FILE__, __LINE__, _("bad reason"));
-      }
-  }
-
-  do_cleanups (old_cleanups);
+  TRY
+    {
+      read_command_file (stream);
+    }
+  CATCH (e, RETURN_MASK_ERROR)
+    {
+      /* Re-throw the error, but with the file name information
+        prepended.  */
+      throw_error (e.error,
+                  _("%s:%d: Error in sourced command file:\n%s"),
+                  source_file_name, source_line_number, e.message);
+    }
+  END_CATCH
 }
 
 /* Print the definition of user command C to STREAM.  Or, if C is a
@@ -1575,26 +1607,54 @@ script_from_file (FILE *stream, char *file)
    (recursively).  PREFIX and NAME combined are the name of the
    current command.  */
 void
-show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
+show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
             struct ui_file *stream)
 {
   struct command_line *cmdlines;
 
   if (c->prefixlist != NULL)
     {
-      char *prefixname = c->prefixname;
+      const char *prefixname = c->prefixname;
+
       for (c = *c->prefixlist; c != NULL; c = c->next)
-       if (c->class == class_user || c->prefixlist != NULL)
+       if (c->theclass == class_user || c->prefixlist != NULL)
          show_user_1 (c, prefixname, c->name, gdb_stdout);
       return;
     }
 
   cmdlines = c->user_commands;
-  if (!cmdlines)
-    return;
   fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
 
-  print_command_lines (uiout, cmdlines, 1);
+  if (!cmdlines)
+    return;
+  print_command_lines (current_uiout, cmdlines, 1);
   fputs_filtered ("\n", stream);
 }
 
+void
+_initialize_cli_script (void)
+{
+  add_com ("document", class_support, document_command, _("\
+Document a user-defined command.\n\
+Give command name as argument.  Give documentation on following lines.\n\
+End with a line of just \"end\"."));
+  add_com ("define", class_support, define_command, _("\
+Define a new command name.  Command name is argument.\n\
+Definition appears on following lines, one command per line.\n\
+End with a line of just \"end\".\n\
+Use the \"document\" command to give documentation for the new command.\n\
+Commands defined in this way may have up to ten arguments."));
+
+  add_com ("while", class_support, while_command, _("\
+Execute nested commands WHILE the conditional expression is non zero.\n\
+The conditional expression must follow the word `while' and must in turn be\n\
+followed by a new line.  The nested commands must be entered one per line,\n\
+and should be terminated by the word `end'."));
+
+  add_com ("if", class_support, if_command, _("\
+Execute nested commands once IF the conditional expression is non zero.\n\
+The conditional expression must follow the word `if' and must in turn be\n\
+followed by a new line.  The nested commands must be entered one per line,\n\
+and should be terminated by the word 'else' or `end'.  If an else clause\n\
+is used, the same rules apply to its nested commands as to the first ones."));
+}
This page took 0.057638 seconds and 4 git commands to generate.