Use scoped_restore in a couple of interp-related places
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
index b066da7d6018c4a1007681cce0e964c607ec9c69..a62e4fe1c19e00a85dfcd1a5c5d5408525d2bb07 100644 (file)
 /* Prototypes for local functions.  */
 
 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);
+recurse_read_control_structure
+    (gdb::function_view<const char * ()> read_next_line_func,
+     struct command_line *current_cmd,
+     gdb::function_view<void (const char *)> validator);
+
+static void do_define_command (const char *comname, int from_tty,
+                              const counted_command_line *commands);
 
 static char *read_next_line (void);
 
@@ -122,6 +125,7 @@ multi_line_command_p (enum command_control_type type)
     case compile_control:
     case python_control:
     case guile_control:
+    case define_control:
       return 1;
     default:
       return 0;
@@ -134,9 +138,15 @@ multi_line_command_p (enum command_control_type type)
 static struct command_line *
 build_command_line (enum command_control_type type, const char *args)
 {
-  if ((args == NULL || *args == '\0')
-      && (type == if_control || type == while_control))
-    error (_("if/while commands require arguments."));
+  if (args == NULL || *args == '\0')
+    {
+      if (type == if_control)
+       error (_("if command requires an argument."));
+      else if (type == while_control)
+       error (_("while command requires an argument."));
+      else if (type == define_control)
+       error (_("define command requires an argument."));
+    }
   gdb_assert (args != NULL);
 
   return new struct command_line (type, xstrdup (args));
@@ -153,7 +163,7 @@ get_command_line (enum command_control_type type, const char *arg)
                            command_lines_deleter ());
 
   /* Read in the body of this command.  */
-  if (recurse_read_control_structure (read_next_line, cmd.get (), 0, 0)
+  if (recurse_read_control_structure (read_next_line, cmd.get (), 0)
       == invalid_control)
     {
       warning (_("Error reading in canned sequence of commands."));
@@ -364,12 +374,69 @@ execute_cmd_post_hook (struct cmd_list_element *c)
     }
 }
 
+/* See cli-script.h.  */
+
+void
+execute_control_commands (struct command_line *cmdlines, int from_tty)
+{
+  /* Set the instream to 0, indicating execution of a
+     user-defined function.  */
+  scoped_restore restore_instream
+    = make_scoped_restore (&current_ui->instream, nullptr);
+  scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
+  scoped_restore save_nesting
+    = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
+
+  while (cmdlines)
+    {
+      enum command_control_type ret = execute_control_command (cmdlines,
+                                                              from_tty);
+      if (ret != simple_control && ret != break_control)
+       {
+         warning (_("Error executing canned sequence of commands."));
+         break;
+       }
+      cmdlines = cmdlines->next;
+    }
+}
+
+/* See cli-script.h.  */
+
+std::string
+execute_control_commands_to_string (struct command_line *commands,
+                                   int from_tty)
+{
+  /* GDB_STDOUT should be better already restored during these
+     restoration callbacks.  */
+  set_batch_flag_and_restore_page_info save_page_info;
+
+  string_file str_file;
+
+  {
+    current_uiout->redirect (&str_file);
+    ui_out_redirect_pop redirect_popper (current_uiout);
+
+    scoped_restore save_stdout
+      = make_scoped_restore (&gdb_stdout, &str_file);
+    scoped_restore save_stderr
+      = make_scoped_restore (&gdb_stderr, &str_file);
+    scoped_restore save_stdlog
+      = make_scoped_restore (&gdb_stdlog, &str_file);
+    scoped_restore save_stdtarg
+      = make_scoped_restore (&gdb_stdtarg, &str_file);
+    scoped_restore save_stdtargerr
+      = make_scoped_restore (&gdb_stdtargerr, &str_file);
+
+    execute_control_commands (commands, from_tty);
+  }
+
+  return std::move (str_file.string ());
+}
+
 void
 execute_user_command (struct cmd_list_element *c, const char *args)
 {
-  struct ui *ui = current_ui;
   counted_command_line cmdlines_copy;
-  enum command_control_type ret;
   extern unsigned int max_user_call_depth;
 
   /* Ensure that the user commands can't be deleted while they are
@@ -385,25 +452,7 @@ execute_user_command (struct cmd_list_element *c, const char *args)
   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.  */
-  scoped_restore restore_instream
-    = make_scoped_restore (&ui->instream, nullptr);
-
-  scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
-
-  scoped_restore save_nesting
-    = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
-  while (cmdlines)
-    {
-      ret = execute_control_command (cmdlines);
-      if (ret != simple_control && ret != break_control)
-       {
-         warning (_("Error executing canned sequence of commands."));
-         break;
-       }
-      cmdlines = cmdlines->next;
-    }
+  execute_control_commands (cmdlines, 0);
 }
 
 /* This function is called every time GDB prints a prompt.  It ensures
@@ -426,8 +475,9 @@ reset_command_nest_depth (void)
    via while_command or if_command.  Inner levels of 'if' and 'while'
    are dealt with directly.  Therefore we can use these functions
    to determine whether the command has been printed already or not.  */
+ATTRIBUTE_PRINTF (1, 2)
 void
-print_command_trace (const char *cmd)
+print_command_trace (const char *fmt, ...)
 {
   int i;
 
@@ -443,13 +493,18 @@ print_command_trace (const char *cmd)
   for (i=0; i < command_nest_depth; i++)
     printf_filtered ("+");
 
-  printf_filtered ("%s\n", cmd);
+  va_list args;
+
+  va_start (args, fmt);
+  vprintf_filtered (fmt, args);
+  va_end (args);
+  puts_filtered ("\n");
 }
 
 /* Helper for execute_control_command.  */
 
 static enum command_control_type
-execute_control_command_1 (struct command_line *cmd)
+execute_control_command_1 (struct command_line *cmd, int from_tty)
 {
   struct command_line *current;
   struct value *val;
@@ -467,7 +522,7 @@ execute_control_command_1 (struct command_line *cmd)
       {
        /* 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);
+       execute_command (new_line.c_str (), from_tty);
        ret = cmd->control_type;
        break;
       }
@@ -490,11 +545,7 @@ execute_control_command_1 (struct command_line *cmd)
 
     case while_control:
       {
-       int len = strlen (cmd->line) + 7;
-       char *buffer = (char *) alloca (len);
-
-       xsnprintf (buffer, len, "while %s", cmd->line);
-       print_command_trace (buffer);
+       print_command_trace ("while %s", cmd->line);
 
        /* Parse the loop control expression for the while statement.  */
        std::string new_line = insert_user_defined_cmd_args (cmd->line);
@@ -526,7 +577,7 @@ execute_control_command_1 (struct command_line *cmd)
              {
                scoped_restore save_nesting
                  = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
-               ret = execute_control_command_1 (current);
+               ret = execute_control_command_1 (current, from_tty);
 
                /* If we got an error, or a "break" command, then stop
                   looping.  */
@@ -555,11 +606,7 @@ execute_control_command_1 (struct command_line *cmd)
 
     case if_control:
       {
-       int len = strlen (cmd->line) + 4;
-       char *buffer = (char *) alloca (len);
-
-       xsnprintf (buffer, len, "if %s", cmd->line);
-       print_command_trace (buffer);
+       print_command_trace ("if %s", cmd->line);
 
        /* Parse the conditional for the if statement.  */
        std::string new_line = insert_user_defined_cmd_args (cmd->line);
@@ -585,7 +632,7 @@ execute_control_command_1 (struct command_line *cmd)
          {
            scoped_restore save_nesting
              = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
-           ret = execute_control_command_1 (current);
+           ret = execute_control_command_1 (current, from_tty);
 
            /* If we got an error, get out.  */
            if (ret != simple_control)
@@ -613,6 +660,12 @@ execute_control_command_1 (struct command_line *cmd)
       ret = simple_control;
       break;
 
+    case define_control:
+      print_command_trace ("define %s", cmd->line);
+      do_define_command (cmd->line, 0, &cmd->body_list_0);
+      ret = simple_control;
+      break;
+
     case python_control:
     case guile_control:
       {
@@ -630,7 +683,7 @@ execute_control_command_1 (struct command_line *cmd)
 }
 
 enum command_control_type
-execute_control_command (struct command_line *cmd)
+execute_control_command (struct command_line *cmd, int from_tty)
 {
   /* Make sure we use the console uiout.  It's possible that we are executing
      breakpoint commands while running the MI interpreter.  */
@@ -638,7 +691,7 @@ execute_control_command (struct command_line *cmd)
   scoped_restore save_uiout
     = make_scoped_restore (&current_uiout, interp_ui_out (console));
 
-  return execute_control_command_1 (cmd);
+  return execute_control_command_1 (cmd, from_tty);
 }
 
 /* Like execute_control_command, but first set
@@ -883,11 +936,13 @@ line_first_arg (const char *p)
    Otherwise, only "end" is recognized.  */
 
 static enum misc_command_type
-process_next_line (char *p, struct command_line **command, int parse_commands,
-                  void (*validator)(char *, void *), void *closure)
+process_next_line (const char *p, struct command_line **command,
+                  int parse_commands,
+                  gdb::function_view<void (const char *)> validator)
+
 {
-  char *p_end;
-  char *p_start;
+  const char *p_end;
+  const char *p_start;
   int not_handled = 0;
 
   /* Not sure what to do here.  */
@@ -962,6 +1017,8 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
        {
          *command = build_command_line (commands_control, line_first_arg (p));
        }
+      else if (command_name_equals (cmd, "define"))
+       *command = build_command_line (define_control, line_first_arg (p));
       else if (command_name_equals (cmd, "python") && !inline_cmd)
        {
          /* Note that we ignore the inline "python command" form
@@ -997,10 +1054,9 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
 
   if (validator)
     {
-
       TRY
        {
-         validator ((*command)->line, closure);
+         validator ((*command)->line);
        }
       CATCH (ex, RETURN_MASK_ALL)
        {
@@ -1019,10 +1075,9 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
    obtain lines of the command.  */
 
 static enum command_control_type
-recurse_read_control_structure (char * (*read_next_line_func) (void),
+recurse_read_control_structure (gdb::function_view<const char * ()> read_next_line_func,
                                struct command_line *current_cmd,
-                               void (*validator)(char *, void *),
-                               void *closure)
+                               gdb::function_view<void (const char *)> validator)
 {
   enum misc_command_type val;
   enum command_control_type ret;
@@ -1045,7 +1100,7 @@ recurse_read_control_structure (char * (*read_next_line_func) (void),
                               current_cmd->control_type != python_control
                               && current_cmd->control_type != guile_control
                               && current_cmd->control_type != compile_control,
-                              validator, closure);
+                              validator);
 
       /* Just skip blanks and comments.  */
       if (val == nop_command)
@@ -1098,7 +1153,7 @@ recurse_read_control_structure (char * (*read_next_line_func) (void),
        {
          control_level++;
          ret = recurse_read_control_structure (read_next_line_func, next,
-                                               validator, closure);
+                                               validator);
          control_level--;
 
          if (ret != simple_control)
@@ -1123,8 +1178,8 @@ recurse_read_control_structure (char * (*read_next_line_func) (void),
 #define END_MESSAGE "End with a line saying just \"end\"."
 
 counted_command_line
-read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
-                   void (*validator)(char *, void *), void *closure)
+read_command_lines (const char *prompt_arg, int from_tty, int parse_commands,
+                   gdb::function_view<void (const char *)> validator)
 {
   if (from_tty && input_interactive_p (current_ui))
     {
@@ -1147,13 +1202,13 @@ read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
   counted_command_line head (nullptr, command_lines_deleter ());
   if (current_interp_named_p (INTERP_CONSOLE))
     head = read_command_lines_1 (read_next_line, parse_commands,
-                                validator, closure);
+                                validator);
   else
     {
       scoped_restore_interp interp_restorer (INTERP_CONSOLE);
 
       head = read_command_lines_1 (read_next_line, parse_commands,
-                                  validator, closure);
+                                  validator);
     }
 
   if (from_tty && input_interactive_p (current_ui)
@@ -1168,8 +1223,9 @@ read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
    obtained using READ_NEXT_LINE_FUNC.  */
 
 counted_command_line
-read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
-                     void (*validator)(char *, void *), void *closure)
+read_command_lines_1 (gdb::function_view<const char * ()> read_next_line_func,
+                     int parse_commands,
+                     gdb::function_view<void (const char *)> validator)
 {
   struct command_line *tail, *next;
   counted_command_line head (nullptr, command_lines_deleter ());
@@ -1183,7 +1239,7 @@ read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
     {
       dont_repeat ();
       val = process_next_line (read_next_line_func (), &next, parse_commands,
-                              validator, closure);
+                              validator);
 
       /* Ignore blank lines or comments.  */
       if (val == nop_command)
@@ -1205,7 +1261,7 @@ read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
        {
          control_level++;
          ret = recurse_read_control_structure (read_next_line_func, next,
-                                               validator, closure);
+                                               validator);
          control_level--;
 
          if (ret == invalid_control)
@@ -1305,10 +1361,16 @@ user_defined_command (const char *ignore, int from_tty)
 {
 }
 
+/* Define a user-defined command.  If COMMANDS is NULL, then this is a
+   top-level call and the commands will be read using
+   read_command_lines.  Otherwise, it is a "define" command in an
+   existing command and the commands are provided.  In the
+   non-top-level case, various prompts and warnings are disabled.  */
+
 static void
-define_command (const char *comname, int from_tty)
+do_define_command (const char *comname, int from_tty,
+                  const counted_command_line *commands)
 {
-#define MAX_TMPBUF 128   
   enum cmd_hook_type
     {
       CMD_NO_HOOK = 0,
@@ -1317,7 +1379,6 @@ define_command (const char *comname, int from_tty)
     };
   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;
    
@@ -1335,7 +1396,7 @@ define_command (const char *comname, int from_tty)
   if (c && strcmp (comname, c->name) != 0)
     c = 0;
 
-  if (c)
+  if (c && commands == nullptr)
     {
       int q;
 
@@ -1369,7 +1430,7 @@ define_command (const char *comname, int from_tty)
       hookc = lookup_cmd (&tem, *list, "", -1, 0);
       if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
        hookc = 0;
-      if (!hookc)
+      if (!hookc && commands == nullptr)
        {
          warning (_("Your new `%s' command does not "
                     "hook any existing command."),
@@ -1381,9 +1442,15 @@ define_command (const char *comname, int from_tty)
 
   comname = xstrdup (comname);
 
-  xsnprintf (tmpbuf, sizeof (tmpbuf),
-            "Type commands for definition of \"%s\".", comfull);
-  counted_command_line cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
+  counted_command_line cmds;
+  if (commands == nullptr)
+    {
+      std::string prompt
+       = string_printf ("Type commands for definition of \"%s\".", comfull);
+      cmds = read_command_lines (prompt.c_str (), from_tty, 1, 0);
+    }
+  else
+    cmds = *commands;
 
   newc = add_cmd (comname, class_user, user_defined_command,
                  (c && c->theclass == class_user)
@@ -1412,13 +1479,18 @@ define_command (const char *comname, int from_tty)
     }
 }
 
+static void
+define_command (const char *comname, int from_tty)
+{
+  do_define_command (comname, from_tty, nullptr);
+}
+
 static void
 document_command (const char *comname, int from_tty)
 {
   struct cmd_list_element *c, **list;
   const char *tem;
   const char *comfull;
-  char tmpbuf[128];
 
   comfull = comname;
   list = validate_comname (&comname);
@@ -1429,10 +1501,10 @@ document_command (const char *comname, int from_tty)
   if (c->theclass != class_user)
     error (_("Command \"%s\" is built-in."), comfull);
 
-  xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
-            comfull);
-  counted_command_line doclines = read_command_lines (tmpbuf, from_tty,
-                                                     0, 0, 0);
+  std::string prompt = string_printf ("Type documentation for \"%s\".",
+                                     comfull);
+  counted_command_line doclines = read_command_lines (prompt.c_str (),
+                                                     from_tty, 0, 0);
 
   if (c->doc)
     xfree ((char *) c->doc);
This page took 0.030807 seconds and 4 git commands to generate.