2011-08-04 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / top.c
index c453a71fada9cea0ab8758ab5286809332651e26..da930ca28893f91082256d21d166c1016bb7f8ff 100644 (file)
--- a/gdb/top.c
+++ b/gdb/top.c
 #include "ui-out.h"
 #include "cli-out.h"
 
+#define PROMPT(X) the_prompts.prompt_stack[the_prompts.top + X].prompt
+#define PREFIX(X) the_prompts.prompt_stack[the_prompts.top + X].prefix
+#define SUFFIX(X) the_prompts.prompt_stack[the_prompts.top + X].suffix
+
 /* Default command line prompt.  This is overriden in some configs.  */
 
 #ifndef DEFAULT_PROMPT
@@ -137,8 +141,8 @@ int xgdb_verbose;
 /* Buffer used for reading command lines, and the size
    allocated for it so far.  */
 
-char *line;
-int linesize = 100;
+char *saved_command_line;
+int saved_command_line_size = 100;
 
 /* Nonzero if the current command is modified by "server ".  This
    affects things like recording into the command history, commands
@@ -294,7 +298,7 @@ quit_cover (void *s)
 }
 #endif /* defined SIGHUP */
 \f
-/* Line number we are currently in in a file which is being sourced.  */
+/* Line number we are currently in, in a file which is being sourced.  */
 /* NOTE 1999-04-29: This variable will be static again, once we modify
    gdb to use the event loop as the default command loop and we merge
    event-top.c into this file, top.c.  */
@@ -339,10 +343,14 @@ do_chdir_cleanup (void *old_dir)
 }
 #endif
 
-void
+struct cleanup *
 prepare_execute_command (void)
 {
-  free_all_values ();
+  struct value *mark;
+  struct cleanup *cleanup;
+
+  mark = value_mark ();
+  cleanup = make_cleanup_value_free_to_mark (mark);
 
   /* With multiple threads running while the one we're examining is
      stopped, the dcache can get stale without us being able to detect
@@ -350,6 +358,8 @@ prepare_execute_command (void)
      help things like backtrace.  */
   if (non_stop)
     target_dcache_invalidate ();
+
+  return cleanup;
 }
 
 /* Execute the line P as a command, in the current user context.
@@ -358,12 +368,13 @@ prepare_execute_command (void)
 void
 execute_command (char *p, int from_tty)
 {
+  struct cleanup *cleanup;
   struct cmd_list_element *c;
   enum language flang;
   static int warned = 0;
   char *line;
 
-  prepare_execute_command ();
+  cleanup = prepare_execute_command ();
 
   /* Force cleanup of any alloca areas if using C alloca instead of
      a builtin alloca.  */
@@ -371,7 +382,10 @@ execute_command (char *p, int from_tty)
 
   /* This can happen when command_line_input hits end of file.  */
   if (p == NULL)
-    return;
+    {
+      do_cleanups (cleanup);
+      return;
+    }
 
   target_log_command (p);
 
@@ -462,6 +476,8 @@ execute_command (char *p, int from_tty)
          warned = 1;
        }
     }
+
+    do_cleanups (cleanup);
 }
 
 /* Run execute_command for P and FROM_TTY.  Capture its output into the
@@ -488,10 +504,10 @@ execute_command_to_string (char *p, int from_tty)
   make_cleanup_restore_ui_file (&gdb_stdtarg);
   make_cleanup_restore_ui_file (&gdb_stdtargerr);
 
-  if (ui_out_redirect (uiout, str_file) < 0)
+  if (ui_out_redirect (current_uiout, str_file) < 0)
     warning (_("Current output protocol does not support redirection"));
   else
-    make_cleanup_ui_out_redirect_pop (uiout);
+    make_cleanup_ui_out_redirect_pop (current_uiout);
 
   gdb_stdout = str_file;
   gdb_stderr = str_file;
@@ -521,7 +537,7 @@ command_loop (void)
   while (instream && !feof (instream))
     {
       if (window_hook && instream == stdin)
-       (*window_hook) (instream, get_prompt ());
+       (*window_hook) (instream, get_prompt (0));
 
       quit_flag = 0;
       if (instream == stdin && stdin_is_tty)
@@ -530,10 +546,13 @@ command_loop (void)
 
       /* Get a command-line.  This calls the readline package.  */
       command = command_line_input (instream == stdin ?
-                                   get_prompt () : (char *) NULL,
+                                   get_prompt (0) : (char *) NULL,
                                    instream == stdin, "prompt");
       if (command == 0)
-       return;
+       {
+         do_cleanups (old_chain);
+         return;
+       }
 
       make_command_stats_cleanup (1);
 
@@ -563,7 +582,7 @@ dont_repeat (void)
      thing read from stdin in line and don't want to delete it.  Null
      lines won't repeat here in any case.  */
   if (instream == stdin)
-    *line = 0;
+    *saved_command_line = 0;
 }
 
 /* Prevent dont_repeat from working, and return a cleanup that
@@ -778,8 +797,7 @@ gdb_readline_wrapper (char *prompt)
     (*after_char_processing_hook) ();
   gdb_assert (after_char_processing_hook == NULL);
 
-  /* gdb_do_one_event argument is unused.  */
-  while (gdb_do_one_event (NULL) >= 0)
+  while (gdb_do_one_event () >= 0)
     if (gdb_readline_wrapper_done)
       break;
 
@@ -1025,10 +1043,10 @@ command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
   /* If we just got an empty line, and that is supposed to repeat the
      previous command, return the value in the global buffer.  */
   if (repeat && p == linebuffer)
-    return line;
+    return saved_command_line;
   for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
   if (repeat && !*p1)
-    return line;
+    return saved_command_line;
 
   *p = 0;
 
@@ -1049,13 +1067,13 @@ command_line_input (char *prompt_arg, int repeat, char *annotation_suffix)
   /* Save into global buffer if appropriate.  */
   if (repeat)
     {
-      if (linelength > linesize)
+      if (linelength > saved_command_line_size)
        {
-         line = xrealloc (line, linelength);
-         linesize = linelength;
+         saved_command_line = xrealloc (saved_command_line, linelength);
+         saved_command_line_size = linelength;
        }
-      strcpy (line, linebuffer);
-      return line;
+      strcpy (saved_command_line, linebuffer);
+      return saved_command_line;
     }
 
   return linebuffer;
@@ -1109,23 +1127,98 @@ and \"show warranty\" for details.\n");
     }
 }
 \f
-/* get_prompt: access method for the GDB prompt string.  */
+
+/* get_prefix: access method for the GDB prefix string.  */
 
 char *
-get_prompt (void)
+get_prefix (int level)
 {
-  return PROMPT (0);
+  return PREFIX (level);
 }
 
+/* set_prefix: set method for the GDB prefix string.  */
+
 void
-set_prompt (char *s)
+set_prefix (const char *s, int level)
 {
-/* ??rehrauer: I don't know why this fails, since it looks as though
-   assignments to prompt are wrapped in calls to xstrdup...
-   if (prompt != NULL)
-     xfree (prompt);
- */
-  PROMPT (0) = xstrdup (s);
+  /* If S is NULL, just free the PREFIX at level LEVEL and set to
+     NULL.  */
+  if (s == NULL)
+    {
+      xfree (PREFIX (level));
+      PREFIX (level) = NULL;
+    }
+  else
+    {
+      char *p = xstrdup (s);
+
+      xfree (PREFIX (level));
+      PREFIX (level) =  p;
+    }
+}
+
+/* get_suffix: access method for the GDB suffix string.  */
+
+char *
+get_suffix (int level)
+{
+  return SUFFIX (level);
+}
+
+/* set_suffix: set method for the GDB suffix string.  */
+
+void
+set_suffix (const char *s, int level)
+{
+  /* If S is NULL, just free the SUFFIX at level LEVEL and set to
+     NULL.  */
+  if (s == NULL)
+    {
+      xfree (SUFFIX (level));
+      SUFFIX (level) = NULL;
+    }
+  else
+    {
+      char *p = xstrdup (s);
+
+      xfree (SUFFIX (level));
+      SUFFIX (level) =  p;
+    }
+}
+
+ /* get_prompt: access method for the GDB prompt string.  */
+
+char *
+get_prompt (int level)
+{
+  return PROMPT (level);
+}
+
+void
+set_prompt (const char *s, int level)
+{
+  /* If S is NULL, just free the PROMPT at level LEVEL and set to
+     NULL.  */
+  if (s == NULL)
+    {
+      xfree (PROMPT (level));
+      PROMPT (level) = NULL;
+    }
+  else
+    {
+      char *p = xstrdup (s);
+
+      xfree (PROMPT (0));
+      PROMPT (0) = p;
+
+      if (level == 0)
+       {
+         /* Also, free and set new_async_prompt so prompt changes sync up
+            with set/show prompt.  */
+           xfree (new_async_prompt);
+           new_async_prompt = xstrdup (PROMPT (0));
+         }
+      }
 }
 \f
 
@@ -1305,8 +1398,9 @@ input_from_terminal_p (void)
 static void
 dont_repeat_command (char *ignored, int from_tty)
 {
-  *line = 0;                   /* Can't call dont_repeat here because we're 
-                                  not necessarily reading from stdin.  */
+  /* Can't call dont_repeat here because we're not necessarily reading
+     from stdin.  */
+  *saved_command_line = 0;
 }
 \f
 /* Functions to manipulate command line editing control variables.  */
@@ -1425,8 +1519,7 @@ set_verbose (char *args, int from_tty, struct cmd_list_element *c)
   struct cmd_list_element *showcmd;
 
   showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
-  gdb_assert (showcmd != NULL
-             && showcmd != (struct cmd_list_element *) -1);
+  gdb_assert (showcmd != NULL && showcmd != CMD_LIST_AMBIGUOUS);
 
   if (info_verbose)
     {
@@ -1516,13 +1609,11 @@ init_main (void)
      whatever the DEFAULT_PROMPT is.  */
   the_prompts.top = 0;
   PREFIX (0) = "";
-  PROMPT (0) = xstrdup (DEFAULT_PROMPT);
+  set_prompt (DEFAULT_PROMPT, 0);
   SUFFIX (0) = "";
   /* Set things up for annotation_level > 1, if the user ever decides
      to use it.  */
   async_annotation_suffix = "prompt";
-  /* Set the variable associated with the setshow prompt command.  */
-  new_async_prompt = xstrdup (PROMPT (0));
 
   /* If gdb was started with --annotate=2, this is equivalent to the
      user entering the command 'set annotate 2' at the gdb prompt, so
This page took 0.037831 seconds and 4 git commands to generate.