Make tui_default_win_height static
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
index e336c58ad0ce7f5587a0e103e3ec8abd0f501c43..4fc9c70259ca537fc1c4cbd38ebee3f68ca8105f 100644 (file)
@@ -1,6 +1,6 @@
 /* GDB CLI command scripting.
 
-   Copyright (C) 1986-2018 Free Software Foundation, Inc.
+   Copyright (C) 1986-2019 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -25,6 +25,7 @@
 #include "ui-out.h"
 #include "top.h"
 #include "breakpoint.h"
+#include "tracepoint.h"
 #include "cli/cli-cmds.h"
 #include "cli/cli-decode.h"
 #include "cli/cli-script.h"
@@ -32,7 +33,9 @@
 #include "extension.h"
 #include "interps.h"
 #include "compile/compile.h"
-#include "common/gdb_string_view.h"
+#include "gdbsupport/gdb_string_view.h"
+#include "python/python.h"
+#include "guile/guile.h"
 
 #include <vector>
 
@@ -58,6 +61,15 @@ static int command_nest_depth = 1;
 /* This is to prevent certain commands being printed twice.  */
 static int suppress_next_print_command_trace = 0;
 
+/* Command element for the 'while' command.  */
+static cmd_list_element *while_cmd_element = nullptr;
+
+/* Command element for the 'if' command.  */
+static cmd_list_element *if_cmd_element = nullptr;
+
+/* Command element for the 'define' command.  */
+static cmd_list_element *define_cmd_element = nullptr;
+
 /* Structure for arguments to user defined functions.  */
 
 class user_args
@@ -374,12 +386,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
@@ -395,25 +464,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
@@ -465,7 +516,7 @@ print_command_trace (const char *fmt, ...)
 /* 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;
@@ -483,7 +534,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;
       }
@@ -538,7 +589,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.  */
@@ -593,7 +644,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)
@@ -644,15 +695,15 @@ 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.  */
   interp *console = interp_lookup (current_ui, INTERP_CONSOLE);
   scoped_restore save_uiout
-    = make_scoped_restore (&current_uiout, interp_ui_out (console));
+    = make_scoped_restore (&current_uiout, console->interp_ui_out ());
 
-  return execute_control_command_1 (cmd);
+  return execute_control_command_1 (cmd, from_tty);
 }
 
 /* Like execute_control_command, but first set
@@ -864,17 +915,7 @@ read_next_line (void)
   else
     prompt_ptr = NULL;
 
-  return command_line_input (prompt_ptr, from_tty, "commands");
-}
-
-/* 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);
+  return command_line_input (prompt_ptr, "commands");
 }
 
 /* Given an input line P, skip the command and return a pointer to the
@@ -951,7 +992,7 @@ process_next_line (const char *p, struct command_line **command,
 
       /* Check for while, if, break, continue, etc and build a new
         command line structure for them.  */
-      if (command_name_equals (cmd, "while-stepping"))
+      if (cmd == while_stepping_cmd_element)
        {
          /* Because validate_actionline and encode_action lookup
             command's line as command, we need the line to
@@ -966,34 +1007,28 @@ process_next_line (const char *p, struct command_line **command,
             not.  */
          *command = build_command_line (while_stepping_control, p);
        }
-      else if (command_name_equals (cmd, "while"))
-       {
-         *command = build_command_line (while_control, line_first_arg (p));
-       }
-      else if (command_name_equals (cmd, "if"))
-       {
-         *command = build_command_line (if_control, line_first_arg (p));
-       }
-      else if (command_name_equals (cmd, "commands"))
-       {
-         *command = build_command_line (commands_control, line_first_arg (p));
-       }
-      else if (command_name_equals (cmd, "define"))
+      else if (cmd == while_cmd_element)
+       *command = build_command_line (while_control, line_first_arg (p));
+      else if (cmd == if_cmd_element)
+       *command = build_command_line (if_control, line_first_arg (p));
+      else if (cmd == commands_cmd_element)
+       *command = build_command_line (commands_control, line_first_arg (p));
+      else if (cmd == define_cmd_element)
        *command = build_command_line (define_control, line_first_arg (p));
-      else if (command_name_equals (cmd, "python") && !inline_cmd)
+      else if (cmd == python_cmd_element && !inline_cmd)
        {
          /* Note that we ignore the inline "python command" form
             here.  */
          *command = build_command_line (python_control, "");
        }
-      else if (command_name_equals (cmd, "compile") && !inline_cmd)
+      else if (cmd == compile_cmd_element && !inline_cmd)
        {
          /* 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)
+      else if (cmd == guile_cmd_element && !inline_cmd)
        {
          /* Note that we ignore the inline "guile command" form here.  */
          *command = build_command_line (guile_control, "");
@@ -1015,16 +1050,15 @@ process_next_line (const char *p, struct command_line **command,
 
   if (validator)
     {
-      TRY
+      try
        {
          validator ((*command)->line);
        }
-      CATCH (ex, RETURN_MASK_ALL)
+      catch (const gdb_exception &ex)
        {
          free_command_lines (command);
-         throw_exception (ex);
+         throw;
        }
-      END_CATCH
     }
 
   /* Nothing special.  */
@@ -1042,7 +1076,7 @@ recurse_read_control_structure (gdb::function_view<const char * ()> read_next_li
 {
   enum misc_command_type val;
   enum command_control_type ret;
-  struct command_line **body_ptr, *child_tail, *next;
+  struct command_line *child_tail, *next;
   counted_command_line *current_body = &current_cmd->body_list_0;
 
   child_tail = NULL;
@@ -1151,10 +1185,7 @@ read_command_lines (const char *prompt_arg, int from_tty, int parse_commands,
                                             END_MESSAGE);
        }
       else
-       {
-         printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
-         gdb_flush (gdb_stdout);
-       }
+       printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
     }
 
 
@@ -1502,24 +1533,25 @@ script_from_file (FILE *stream, const char *file)
 
   scoped_restore restore_line_number
     = make_scoped_restore (&source_line_number, 0);
-  scoped_restore resotre_file
-    = make_scoped_restore (&source_file_name, file);
+  scoped_restore restore_file
+    = make_scoped_restore<std::string, const std::string &> (&source_file_name,
+                                                            file);
 
   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
-  TRY
+  try
     {
       read_command_file (stream);
     }
-  CATCH (e, RETURN_MASK_ERROR)
+  catch (const gdb_exception_error &e)
     {
       /* 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);
+                  source_file_name.c_str (), source_line_number,
+                  e.what ());
     }
-  END_CATCH
 }
 
 /* Print the definition of user command C to STREAM.  Or, if C is a
@@ -1558,20 +1590,22 @@ _initialize_cli_script (void)
 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_cmd_element = 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."));
+Commands defined in this way may accept an unlimited number of arguments\n\
+accessed via $arg0 .. $argN.  $argc tells how many arguments have\n\
+been passed."));
 
-  add_com ("while", class_support, while_command, _("\
+  while_cmd_element = 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, _("\
+  if_cmd_element = 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\
This page took 0.031697 seconds and 4 git commands to generate.