Class-ify ui_out
[deliverable/binutils-gdb.git] / gdb / tui / tui-io.c
index 831705cfa7f0f46035e52cf6c3c43e43363e2f59..cde679cc947506ef5b5bd2a1af72759bc14ca75e 100644 (file)
@@ -1,6 +1,6 @@
 /* TUI support I/O functions.
 
-   Copyright (C) 1998-2015 Free Software Foundation, Inc.
+   Copyright (C) 1998-2016 Free Software Foundation, Inc.
 
    Contributed by Hewlett-Packard Company.
 
@@ -32,6 +32,7 @@
 #include "tui/tui-win.h"
 #include "tui/tui-wingeneral.h"
 #include "tui/tui-file.h"
+#include "tui/tui-out.h"
 #include "ui-out.h"
 #include "cli-out.h"
 #include <fcntl.h>
@@ -63,17 +64,6 @@ key_is_backspace (int ch)
   return (ch == 8);
 }
 
-int
-key_is_command_char (int ch)
-{
-  return ((ch == KEY_NPAGE) || (ch == KEY_PPAGE)
-         || (ch == KEY_LEFT) || (ch == KEY_RIGHT)
-         || (ch == KEY_UP) || (ch == KEY_DOWN)
-         || (ch == KEY_SF) || (ch == KEY_SR)
-         || (ch == (int)'\f') 
-         || key_is_start_sequence (ch));
-}
-
 /* Use definition from readline 4.3.  */
 #undef CTRL_CHAR
 #define CTRL_CHAR(c) \
@@ -125,13 +115,14 @@ struct ui_out *tui_out;
 /* GDB output files in non-curses mode.  */
 static struct ui_file *tui_old_stdout;
 static struct ui_file *tui_old_stderr;
-struct ui_out *tui_old_uiout;
+cli_ui_out *tui_old_uiout;
 
 /* Readline previous hooks.  */
 static rl_getc_func_t *tui_old_rl_getc_function;
 static rl_voidfunc_t *tui_old_rl_redisplay_function;
 static rl_vintfunc_t *tui_old_rl_prep_terminal;
 static rl_voidfunc_t *tui_old_rl_deprep_terminal;
+static rl_compdisp_func_t *tui_old_rl_display_matches_hook;
 static int tui_old_rl_echoing_p;
 
 /* Readline output stream.
@@ -146,8 +137,6 @@ static int tui_readline_pipe[2];
    This may be the main gdb prompt or a secondary prompt.  */
 static char *tui_rl_saved_prompt;
 
-static int tui_handle_resize_during_io (int, int);
-
 static void
 tui_putc (char c)
 {
@@ -158,7 +147,10 @@ tui_putc (char c)
   tui_puts (buf);
 }
 
-/* Print the string in the curses command window.  */
+/* Print the string in the curses command window.
+   The output is buffered.  It is up to the caller to refresh the screen
+   if necessary.  */
+
 void
 tui_puts (const char *string)
 {
@@ -181,9 +173,9 @@ tui_puts (const char *string)
          /* Expand TABs, since ncurses on MS-Windows doesn't.  */
          if (c == '\t')
            {
-             int line, col;
+             int col;
 
-             getyx (w, line, col);
+             col = getcurx (w);
              do
                {
                  waddch (w, ' ');
@@ -196,14 +188,7 @@ tui_puts (const char *string)
       else if (c == '\n')
         tui_skip_line = -1;
     }
-  getyx (w, TUI_CMD_WIN->detail.command_info.cur_line,
-         TUI_CMD_WIN->detail.command_info.curch);
-  TUI_CMD_WIN->detail.command_info.start_line
-    = TUI_CMD_WIN->detail.command_info.cur_line;
-
-  /* We could defer the following.  */
-  wrefresh (w);
-  fflush (stdout);
+  TUI_CMD_WIN->detail.command_info.start_line = getcury (w);
 }
 
 /* Readline callback.
@@ -214,12 +199,12 @@ tui_redisplay_readline (void)
 {
   int prev_col;
   int height;
-  int col, line;
+  int col;
   int c_pos;
   int c_line;
   int in;
   WINDOW *w;
-  char *prompt;
+  const char *prompt;
   int start_line;
 
   /* Detect when we temporarily left SingleKey and now the readline
@@ -228,7 +213,7 @@ tui_redisplay_readline (void)
      The command could call prompt_for_continue and we must not
      restore SingleKey so that the prompt and normal keymap are used.  */
   if (tui_current_key_mode == TUI_ONE_COMMAND_MODE && rl_end == 0
-      && immediate_quit == 0)
+      && !gdb_in_secondary_prompt_p (current_ui))
     tui_set_key_mode (TUI_SINGLE_KEY_MODE);
 
   if (tui_current_key_mode == TUI_SINGLE_KEY_MODE)
@@ -246,7 +231,7 @@ tui_redisplay_readline (void)
   for (in = 0; prompt && prompt[in]; in++)
     {
       waddch (w, prompt[in]);
-      getyx (w, line, col);
+      col = getcurx (w);
       if (col <= prev_col)
         height++;
       prev_col = col;
@@ -272,7 +257,7 @@ tui_redisplay_readline (void)
       else if (c == '\t')
        {
          /* Expand TABs, since ncurses on MS-Windows doesn't.  */
-         getyx (w, line, col);
+         col = getcurx (w);
          do
            {
              waddch (w, ' ');
@@ -284,24 +269,16 @@ tui_redisplay_readline (void)
           waddch (w, c);
        }
       if (c == '\n')
-        {
-          getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
-                 TUI_CMD_WIN->detail.command_info.curch);
-        }
-      getyx (w, line, col);
+       TUI_CMD_WIN->detail.command_info.start_line = getcury (w);
+      col = getcurx (w);
       if (col < prev_col)
         height++;
       prev_col = col;
     }
   wclrtobot (w);
-  getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
-         TUI_CMD_WIN->detail.command_info.curch);
+  TUI_CMD_WIN->detail.command_info.start_line = getcury (w);
   if (c_line >= 0)
-    {
-      wmove (w, c_line, c_pos);
-      TUI_CMD_WIN->detail.command_info.cur_line = c_line;
-      TUI_CMD_WIN->detail.command_info.curch = c_pos;
-    }
+    wmove (w, c_line, c_pos);
   TUI_CMD_WIN->detail.command_info.start_line -= height - 1;
 
   wrefresh (w);
@@ -384,10 +361,11 @@ static void
 tui_mld_erase_entire_line (const struct match_list_displayer *displayer)
 {
   WINDOW *w = TUI_CMD_WIN->generic.handle;
+  int cur_y = getcury (w);
 
-  wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
+  wmove (w, cur_y, 0);
   wclrtoeol (w);
-  wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
+  wmove (w, cur_y, 0);
 }
 
 /* TUI version of displayer.beep.  */
@@ -408,8 +386,6 @@ tui_mld_getc (FILE *fp)
   WINDOW *w = TUI_CMD_WIN->generic.handle;
   int c = wgetch (w);
 
-  c = tui_handle_resize_during_io (c, 1);
-
   return c;
 }
 
@@ -468,6 +444,7 @@ tui_setup_io (int mode)
       tui_old_rl_deprep_terminal = rl_deprep_term_function;
       tui_old_rl_prep_terminal = rl_prep_term_function;
       tui_old_rl_getc_function = rl_getc_function;
+      tui_old_rl_display_matches_hook = rl_completion_display_matches_hook;
       tui_old_rl_outstream = rl_outstream;
       tui_old_rl_echoing_p = _rl_echoing_p;
       rl_redisplay_function = tui_redisplay_readline;
@@ -483,7 +460,8 @@ tui_setup_io (int mode)
       /* Keep track of previous gdb output.  */
       tui_old_stdout = gdb_stdout;
       tui_old_stderr = gdb_stderr;
-      tui_old_uiout = current_uiout;
+      tui_old_uiout = dynamic_cast<cli_ui_out *> (current_uiout);
+      gdb_assert (tui_old_uiout != nullptr);
 
       /* Reconfigure gdb output.  */
       gdb_stdout = tui_stdout;
@@ -511,8 +489,8 @@ tui_setup_io (int mode)
       rl_deprep_term_function = tui_old_rl_deprep_terminal;
       rl_prep_term_function = tui_old_rl_prep_terminal;
       rl_getc_function = tui_old_rl_getc_function;
+      rl_completion_display_matches_hook = tui_old_rl_display_matches_hook;
       rl_outstream = tui_old_rl_outstream;
-      rl_completion_display_matches_hook = 0;
       _rl_echoing_p = tui_old_rl_echoing_p;
       rl_already_prompted = 0;
 
@@ -535,10 +513,6 @@ tui_cont_sig (int sig)
       /* Force a refresh of the screen.  */
       tui_refresh_all_win ();
 
-      /* Update cursor position on the screen.  */
-      wmove (TUI_CMD_WIN->generic.handle,
-             TUI_CMD_WIN->detail.command_info.start_line,
-             TUI_CMD_WIN->detail.command_info.curch);
       wrefresh (TUI_CMD_WIN->generic.handle);
     }
   signal (sig, tui_cont_sig);
@@ -603,7 +577,6 @@ tui_getc (FILE *fp)
 #endif
 
   ch = wgetch (w);
-  ch = tui_handle_resize_during_io (ch, 0);
 
   /* The \n must be echoed because it will not be printed by
      readline.  */
@@ -614,9 +587,9 @@ tui_getc (FILE *fp)
          with empty lines with gdb prompt at beginning.  Instead of that,
          stay on the same line but provide a visual effect to show the
          user we recognized the command.  */
-      if (rl_end == 0)
+      if (rl_end == 0 && !gdb_in_secondary_prompt_p (current_ui))
         {
-          wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
+         wmove (w, getcury (w), 0);
 
           /* Clear the line.  This will blink the gdb prompt since
              it will be redrawn at the same line.  */
@@ -626,23 +599,26 @@ tui_getc (FILE *fp)
         }
       else
         {
-          wmove (w, TUI_CMD_WIN->detail.command_info.cur_line,
-                 TUI_CMD_WIN->detail.command_info.curch);
-          waddch (w, ch);
+         /* Move cursor to the end of the command line before emitting the
+            newline.  We need to do so because when ncurses outputs a newline
+            it truncates any text that appears past the end of the cursor.  */
+         int px, py;
+         getyx (w, py, px);
+         px += rl_end - rl_point;
+         py += px / TUI_CMD_WIN->generic.width;
+         px %= TUI_CMD_WIN->generic.width;
+         wmove (w, py, px);
+         tui_putc ('\n');
         }
     }
   
-  if (key_is_command_char (ch))
-    {                          /* Handle prev/next/up/down here.  */
-      ch = tui_dispatch_ctrl_char (ch);
-    }
+  /* Handle prev/next/up/down here.  */
+  ch = tui_dispatch_ctrl_char (ch);
   
-  if (ch == '\n' || ch == '\r' || ch == '\f')
-    TUI_CMD_WIN->detail.command_info.curch = 0;
   if (ch == KEY_BACKSPACE)
     return '\b';
 
-  if (async_command_editing_p && key_is_start_sequence (ch))
+  if (current_ui->command_editing && key_is_start_sequence (ch))
     {
       int ch_pending;
 
@@ -678,43 +654,43 @@ tui_getc (FILE *fp)
 char *
 tui_expand_tabs (const char *string, int col)
 {
-  int n_adjust;
+  int n_adjust, ncol;
   const char *s;
   char *ret, *q;
 
   /* 1. How many additional characters do we need?  */
-  for (n_adjust = 0, s = string; s; )
+  for (ncol = col, n_adjust = 0, s = string; s; )
     {
       s = strpbrk (s, "\t");
       if (s)
        {
-         col += (s - string) + n_adjust;
+         ncol += (s - string) + n_adjust;
          /* Adjustment for the next tab stop, minus one for the TAB
             we replace with spaces.  */
-         n_adjust += 8 - (col % 8) - 1;
+         n_adjust += 8 - (ncol % 8) - 1;
          s++;
        }
     }
 
   /* Allocate the copy.  */
-  ret = q = xmalloc (strlen (string) + n_adjust + 1);
+  ret = q = (char *) xmalloc (strlen (string) + n_adjust + 1);
 
   /* 2. Copy the original string while replacing TABs with spaces.  */
-  for (s = string; s; )
+  for (ncol = col, s = string; s; )
     {
-      char *s1 = strpbrk (s, "\t");
+      const char *s1 = strpbrk (s, "\t");
       if (s1)
        {
          if (s1 > s)
            {
              strncpy (q, s, s1 - s);
              q += s1 - s;
-             col += s1 - s;
+             ncol += s1 - s;
            }
          do {
            *q++ = ' ';
-           col++;
-         } while ((col % 8) != 0);
+           ncol++;
+         } while ((ncol % 8) != 0);
          s1++;
        }
       else
@@ -724,24 +700,3 @@ tui_expand_tabs (const char *string, int col)
 
   return ret;
 }
-
-/* Cleanup when a resize has occured.
-   Returns the character that must be processed.  */
-
-static int
-tui_handle_resize_during_io (int original_ch, int for_completion)
-{
-  if (tui_win_resized ())
-    {
-      tui_resize_all ();
-      tui_refresh_all_win ();
-      tui_set_win_resized_to (FALSE);
-      if (!for_completion)
-       {
-         dont_repeat ();
-         return '\n';
-       }
-    }
-
-  return original_ch;
-}
This page took 0.030425 seconds and 4 git commands to generate.