Implemented pagination. master
authorJan Kratochvil <jan.kratochvil@redhat.com>
Sat, 11 Aug 2012 21:14:34 +0000 (23:14 +0200)
committerJan Kratochvil <jan.kratochvil@redhat.com>
Sat, 11 Aug 2012 21:14:34 +0000 (23:14 +0200)
ansidecl.h
main.c

index b16f2b2..984e95d 100644 (file)
@@ -6,6 +6,9 @@
 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
 #define LENGTH(x) (sizeof (x) / sizeof (*(x)))
 
+#define max(a, b) ((a) > (b) ? (a) : (b))
+#define min(a, b) ((a) < (b) ? (a) : (b))
+
 #define bool int
 #define true 1
 #define false 0
diff --git a/main.c b/main.c
index b00bc3d..2ac00a7 100644 (file)
--- a/main.c
+++ b/main.c
@@ -83,10 +83,99 @@ xstrprintf (const char *fmt, ...)
   return retval;
 }
 
+static jmp_buf gdb_readline_jmp_buf;
+
+static void
+gdb_readline_sigint_handler (int signum)
+{
+  gdb_assert (signum == SIGINT);
+  longjmp (gdb_readline_jmp_buf, 1);
+}
+
+static char *
+gdb_readline (const char *prompt)
+{
+  char *retval;
+  sighandler_t saved_handler;
+  sigset_t mask;
+  int i;
+
+  i = sigprocmask (SIG_SETMASK, NULL, &mask);
+  assert (i == 0);
+  saved_handler = signal (SIGINT, gdb_readline_sigint_handler);
+  if (setjmp (gdb_readline_jmp_buf) != 0)
+    {
+      rl_free_line_state ();
+      rl_cleanup_after_signal ();
+
+      /* GDB prints this.  */
+      puts ("Quit");
+
+      i = sigprocmask (SIG_SETMASK, &mask, NULL);
+      assert (i == 0);
+    }
+  retval = readline (prompt);
+  saved_handler = signal (SIGINT, saved_handler);
+  gdb_assert (saved_handler == gdb_readline_sigint_handler);
+  return retval;
+}
+
+static int console_cb_rows, console_cb_columns, console_cb_row, console_cb_column;
+static bool console_cb_drop;
+
 static void
 console_cb (const char *str, void *data)
 {
-  fputs (str, stdout);
+  const char *cs;
+
+  if (console_cb_drop)
+    return;
+  if (console_cb_rows == 0)
+    {
+      fputs (str, stdout);
+      return;
+    }
+
+  while (*str != '\0')
+    {
+      int columns;
+      size_t size;
+      char *answer;
+
+      cs = strchr (str, '\n');
+      if (cs == NULL)
+       cs = &str[strlen (str)];
+      columns = min (cs - str, console_cb_columns - console_cb_column);
+      if (columns > 0)
+       {
+         size = fwrite (str, 1, columns, stdout);
+         gdb_assert (size == columns);
+         str += columns;
+         console_cb_column += columns;
+         continue;
+       }
+      if (*str == '\n')
+       str++;
+      else if (console_cb_column < console_cb_columns)
+       gdb_assert_not_reached ("we should not get here");
+      putchar ('\n');
+      console_cb_row++;
+      console_cb_column = 0;
+      if (console_cb_row < console_cb_rows - 1)
+       continue;
+      answer = gdb_readline (_("---Type <return> to continue, "
+                              "or q <return> to quit---"));
+      for (cs = answer; isspace (*cs); cs++);
+      if (*cs == 'q')
+       {
+         xfree (answer);
+         puts ("Quit");
+         console_cb_drop = true;
+         return;
+       }
+      xfree (answer);
+      console_cb_row = 0;
+    }
 }
 
 static int
@@ -199,43 +288,6 @@ gdb_done (mi_h *h, const char *command)
   mi_free_output (res);
 }
 
-static jmp_buf gdb_readline_jmp_buf;
-
-static void
-gdb_readline_sigint_handler (int signum)
-{
-  gdb_assert (signum == SIGINT);
-  longjmp (gdb_readline_jmp_buf, 1);
-}
-
-static char *
-gdb_readline (const char *prompt)
-{
-  char *retval;
-  sighandler_t saved_handler;
-  sigset_t mask;
-  int i;
-
-  i = sigprocmask (SIG_SETMASK, NULL, &mask);
-  assert (i == 0);
-  saved_handler = signal (SIGINT, gdb_readline_sigint_handler);
-  if (setjmp (gdb_readline_jmp_buf) != 0)
-    {
-      rl_free_line_state ();
-      rl_cleanup_after_signal ();
-
-      /* GDB prints this.  */
-      puts ("Quit");
-
-      i = sigprocmask (SIG_SETMASK, &mask, NULL);
-      assert (i == 0);
-    }
-  retval = readline (prompt);
-  saved_handler = signal (SIGINT, saved_handler);
-  gdb_assert (saved_handler == gdb_readline_sigint_handler);
-  return retval;
-}
-
 static void
 commands_command_console_cb (const char *str, void *data)
 {
@@ -566,6 +618,14 @@ main (int argc, char **argv)
       cmd = gdb_readline (prompt);
       xfree (prompt);
 
+      /* FIXME: -ex commands do not have pagination set.  */
+      if (!gdb_show_bool (h, "pagination"))
+       console_cb_rows = 0;
+      else
+       rl_get_screen_size (&console_cb_rows, &console_cb_columns);
+      console_cb_drop = false;
+      console_cb_row = console_cb_column = 0;
+
       if (cmd == NULL)
        cmd = xstrdup ("quit");
       else