Make the interpreters be per UI
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
index d3ca9ece00441bfd9a45f34be5fec1ba8ffab9fb..5fc01b3950538643b23fe317f9e141877395e232 100644 (file)
@@ -1,6 +1,6 @@
 /* GDB CLI command scripting.
 
-   Copyright (C) 1986-2014 Free Software Foundation, Inc.
+   Copyright (C) 1986-2016 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -23,8 +23,6 @@
 #include <ctype.h>
 
 #include "ui-out.h"
-#include <string.h>
-#include "exceptions.h"
 #include "top.h"
 #include "breakpoint.h"
 #include "cli/cli-cmds.h"
@@ -33,6 +31,7 @@
 
 #include "extension.h"
 #include "interps.h"
+#include "compile/compile.h"
 
 /* Prototypes for local functions.  */
 
@@ -89,6 +88,7 @@ multi_line_command_p (enum command_control_type type)
     case while_control:
     case while_stepping_control:
     case commands_control:
+    case compile_control:
     case python_control:
     case guile_control:
       return 1;
@@ -109,15 +109,12 @@ build_command_line (enum command_control_type type, char *args)
     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;
@@ -274,6 +271,19 @@ print_command_lines (struct ui_out *uiout, struct command_line *cmd,
          continue;
        }
 
+      if (list->control_type == compile_control)
+       {
+         ui_out_field_string (uiout, NULL, "compile expression");
+         ui_out_text (uiout, "\n");
+         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");
+         list = list->next;
+         continue;
+       }
+
       if (list->control_type == guile_control)
        {
          ui_out_field_string (uiout, NULL, "guile");
@@ -297,7 +307,7 @@ print_command_lines (struct ui_out *uiout, struct command_line *cmd,
 static void
 clear_hook_in_cleanup (void *data)
 {
-  struct cmd_list_element *c = data;
+  struct cmd_list_element *c = (struct cmd_list_element *) data;
 
   c->hook_in = 0; /* Allow hook to work again once it is complete.  */
 }
@@ -331,7 +341,7 @@ execute_cmd_post_hook (struct cmd_list_element *c)
 static void
 do_restore_user_call_depth (void * call_depth)
 {      
-  int *depth = call_depth;
+  int *depth = (int *) call_depth;
 
   (*depth)--;
   if ((*depth) == 0)
@@ -369,8 +379,8 @@ execute_user_command (struct cmd_list_element *c, char *args)
      not confused with Insight.  */
   in_user_command = 1;
 
-  make_cleanup_restore_integer (&interpreter_async);
-  interpreter_async = 0;
+  make_cleanup_restore_integer (&current_ui->async);
+  current_ui->async = 0;
 
   command_nest_depth++;
   while (cmdlines)
@@ -474,7 +484,7 @@ execute_control_command (struct command_line *cmd)
     case while_control:
       {
        int len = strlen (cmd->line) + 7;
-       char *buffer = alloca (len);
+       char *buffer = (char *) alloca (len);
 
        xsnprintf (buffer, len, "while %s", cmd->line);
        print_command_trace (buffer);
@@ -543,7 +553,7 @@ execute_control_command (struct command_line *cmd)
     case if_control:
       {
        int len = strlen (cmd->line) + 4;
-       char *buffer = alloca (len);
+       char *buffer = (char *) alloca (len);
 
        xsnprintf (buffer, len, "if %s", cmd->line);
        print_command_trace (buffer);
@@ -601,6 +611,12 @@ execute_control_command (struct command_line *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:
       {
@@ -645,8 +661,8 @@ while_command (char *arg, int from_tty)
   if (command == NULL)
     return;
 
-  old_chain = make_cleanup_restore_integer (&interpreter_async);
-  interpreter_async = 0;
+  old_chain = make_cleanup_restore_integer (&current_ui->async);
+  current_ui->async = 0;
 
   execute_control_command_untraced (command);
   free_command_lines (&command);
@@ -669,8 +685,8 @@ if_command (char *arg, int from_tty)
   if (command == NULL)
     return;
 
-  old_chain = make_cleanup_restore_integer (&interpreter_async);
-  interpreter_async = 0;
+  old_chain = make_cleanup_restore_integer (&current_ui->async);
+  current_ui->async = 0;
 
   execute_control_command_untraced (command);
   free_command_lines (&command);
@@ -703,7 +719,7 @@ setup_user_args (char *p)
   struct cleanup *old_chain;
   unsigned int arg_count = 0;
 
-  args = (struct user_args *) xmalloc (sizeof (struct user_args));
+  args = XNEW (struct user_args);
   memset (args, 0, sizeof (struct user_args));
 
   args->next = user_args;
@@ -782,7 +798,7 @@ locate_arg (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++;
@@ -899,20 +915,18 @@ 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 (void)
@@ -970,7 +984,7 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
 
   /* 'end' is always recognized, regardless of parse_commands value.
      We also permit whitespace before end and after.  */
-  if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
+  if (p_end - p_start == 3 && startswith (p_start, "end"))
     return end_command;
   
   if (parse_commands)
@@ -987,14 +1001,14 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
        return nop_command;
 
       /* Is the else clause of an if control structure?  */
-      if (p_end - 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 ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
-         || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
-         || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
+      if ((p_end - p >= 14 && startswith (p, "while-stepping"))
+         || (p_end - p >= 8 && startswith (p, "stepping"))
+         || (p_end - p >= 2 && startswith (p, "ws")))
        {
          /* Because validate_actionline and encode_action lookup
             command's line as command, we need the line to
@@ -1009,7 +1023,7 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
             not.  */
          *command = build_command_line (while_stepping_control, p);
        }
-      else if (p_end - p > 5 && !strncmp (p, "while", 5))
+      else if (p_end - p > 5 && startswith (p, "while"))
        {
          char *first_arg;
 
@@ -1018,7 +1032,7 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
            first_arg++;
          *command = build_command_line (while_control, first_arg);
        }
-      else if (p_end - p > 2 && !strncmp (p, "if", 2))
+      else if (p_end - p > 2 && startswith (p, "if"))
        {
          char *first_arg;
 
@@ -1027,7 +1041,7 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
            first_arg++;
          *command = build_command_line (if_control, first_arg);
        }
-      else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
+      else if (p_end - p >= 8 && startswith (p, "commands"))
        {
          char *first_arg;
 
@@ -1036,31 +1050,37 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
            first_arg++;
          *command = build_command_line (commands_control, first_arg);
        }
-      else if (p_end - p == 6 && !strncmp (p, "python", 6))
+      else if (p_end - p == 6 && startswith (p, "python"))
        {
          /* Note that we ignore the inline "python command" form
             here.  */
          *command = build_command_line (python_control, "");
        }
-      else if (p_end - p == 5 && !strncmp (p, "guile", 5))
+      else if (p_end - p == 6 && startswith (p, "compile"))
+       {
+         /* 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 (p_end - p == 5 && startswith (p, "guile"))
        {
          /* Note that we ignore the inline "guile command" form here.  */
          *command = build_command_line (guile_control, "");
        }
-      else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
+      else if (p_end - p == 10 && startswith (p, "loop_break"))
        {
-         *command = (struct command_line *)
-           xmalloc (sizeof (struct command_line));
+         *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 (p_end - 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;
@@ -1074,8 +1094,7 @@ 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, p_end - p);
       (*command)->control_type = simple_control;
@@ -1085,17 +1104,17 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
 
   if (validator)
     {
-      volatile struct gdb_exception ex;
 
-      TRY_CATCH (ex, RETURN_MASK_ALL)
+      TRY
        {
          validator ((*command)->line, closure);
        }
-      if (ex.reason < 0)
+      CATCH (ex, RETURN_MASK_ALL)
        {
          xfree (*command);
          throw_exception (ex);
        }
+      END_CATCH
     }
 
   /* Nothing special.  */
@@ -1135,7 +1154,8 @@ recurse_read_control_structure (char * (*read_next_line_func) (void),
       next = NULL;
       val = process_next_line (read_next_line_func (), &next, 
                               current_cmd->control_type != python_control
-                              && current_cmd->control_type != guile_control,
+                              && current_cmd->control_type != guile_control
+                              && current_cmd->control_type != compile_control,
                               validator, closure);
 
       /* Just skip blanks and comments.  */
@@ -1370,7 +1390,7 @@ free_command_lines (struct command_line **lptr)
 static void
 do_free_command_lines_cleanup (void *arg)
 {
-  free_command_lines (arg);
+  free_command_lines ((struct command_line **) arg);
 }
 
 struct cleanup *
@@ -1386,7 +1406,7 @@ copy_command_lines (struct command_line *cmds)
 
   if (cmds)
     {
-      result = (struct command_line *) xmalloc (sizeof (struct command_line));
+      result = XNEW (struct command_line);
 
       result->next = copy_command_lines (cmds->next);
       result->line = xstrdup (cmds->line);
@@ -1396,8 +1416,7 @@ 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]);
@@ -1505,7 +1524,7 @@ define_command (char *comname, int from_tty)
     {
       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);
@@ -1557,11 +1576,11 @@ define_command (char *comname, int from_tty)
             "Type commands for definition of \"%s\".", comfull);
   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;
 
@@ -1602,7 +1621,7 @@ 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);
 
   xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
@@ -1669,29 +1688,24 @@ script_from_file (FILE *stream, const char *file)
   source_line_number = 0;
   source_file_name = file;
 
-  make_cleanup_restore_integer (&interpreter_async);
-  interpreter_async = 0;
+  make_cleanup_restore_integer (&current_ui->async);
+  current_ui->async = 0;
 
   {
-    volatile struct gdb_exception e;
 
-    TRY_CATCH (e, RETURN_MASK_ERROR)
+    TRY
       {
        read_command_file (stream);
       }
-    switch (e.reason)
+    CATCH (e, RETURN_MASK_ERROR)
       {
-      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"));
       }
+    END_CATCH
   }
 
   do_cleanups (old_cleanups);
@@ -1712,16 +1726,16 @@ show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
       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);
 
+  if (!cmdlines)
+    return;
   print_command_lines (current_uiout, cmdlines, 1);
   fputs_filtered ("\n", stream);
 }
This page took 0.03275 seconds and 4 git commands to generate.