lexer: Add support for DEFINE...!ENDDEFINE.
[pspp] / src / ui / terminal / terminal-reader.c
index ad3ced4960a8903396c0127cfdf9a4af67462157..e0f219826dfc4820f55e519dbc9c3c8c4d737929 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 
+#include "libpspp/str.h"
 
 #if HAVE_READLINE
 #include <readline/readline.h>
@@ -33,12 +34,6 @@ static char *history_file;
 static char **complete_command_name (const char *, int, int);
 static char **dont_complete (const char *, int, int);
 static char *command_generator (const char *text, int state);
-
-static const bool have_readline = true;
-
-#else
-static const bool have_readline = false;
-static int rl_end;
 #endif
 
 
@@ -47,7 +42,6 @@ static int rl_end;
 #include <sys/time.h>
 #include <sys/types.h>
 
-#include <assert.h>
 #include <errno.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -63,7 +57,6 @@ static int rl_end;
 #include "libpspp/version.h"
 #include "output/driver.h"
 #include "output/journal.h"
-#include "ui/terminal/terminal.h"
 
 #include "gl/minmax.h"
 #include "gl/xalloc.h"
@@ -165,15 +158,6 @@ terminal_reader_read (struct lex_reader *r_, char *buf, size_t n,
        }
       r->offset = 0;
       r->eof = ss_is_empty (r->s);
-
-      /* Check whether the size of the window has changed, so that
-         the output drivers can adjust their settings as needed.  We
-         only do this for the first line of a command, as it's
-         possible that the output drivers are actually in use
-         afterward, and we don't want to confuse them in the middle
-         of output. */
-      if (prompt_style == PROMPT_FIRST)
-        terminal_check_size ();
     }
 
   chunk = MIN (n, r->s.length - r->offset);
@@ -245,17 +229,20 @@ readline_prompt (enum prompt_style style)
 
     case PROMPT_DO_REPEAT:
       return "DO REPEAT> ";
+
+    case PROMPT_DEFINE:
+      return "DEFINE> ";
     }
 
   NOT_REACHED ();
 }
 
 
+#if HAVE_READLINE
 
 static int pfd[2];
 static bool sigint_received ;
 
-
 /*
    A function similar to getc from stdio.
    However this one may be interrupted by SIGINT.
@@ -281,13 +268,13 @@ interruptible_getc (FILE *fp)
       max_fd = (max_fd > fd) ? max_fd : fd;
       FD_SET (fd, &what);
       ret = select (max_fd + 1, &what, NULL, NULL, &timeout);
-      if ( ret == -1 && errno != EINTR)
+      if (ret == -1 && errno != EINTR)
        {
          perror ("Select failed");
          continue;
        }
 
-      if (ret > 0 )
+      if (ret > 0)
        {
          if (FD_ISSET (pfd[0], &what))
            {
@@ -306,10 +293,6 @@ interruptible_getc (FILE *fp)
   return c;
 }
 
-
-
-#if HAVE_READLINE
-
 static void
 handler (int sig)
 {
@@ -323,10 +306,10 @@ handler (int sig)
 static void
 readline_init (void)
 {
-  if ( 0 != pipe2 (pfd, O_NONBLOCK))
+  if (0 != pipe2 (pfd, O_NONBLOCK))
     perror ("Cannot create pipe");
 
-  if ( SIG_ERR == signal (SIGINT, handler))
+  if (SIG_ERR == signal (SIGINT, handler))
     perror ("Cannot add signal handler");
 
   rl_catch_signals = 0;
@@ -348,7 +331,7 @@ readline_init (void)
 static void
 readline_done (void)
 {
-  if (history_file != NULL && false == settings_get_testing_mode () )
+  if (history_file != NULL && false == settings_get_testing_mode ())
     write_history (history_file);
   clear_history ();
   free (history_file);
@@ -429,14 +412,26 @@ command_generator (const char *text, int state)
   if (state == 0)
     cmd = NULL;
   name = cmd_complete (text, &cmd);
-  return name ? xstrdup (name) : NULL;
+  return xstrdup_if_nonnull (name);
 }
 
 #else  /* !HAVE_READLINE */
 
+static const char * the_prompt;
+
+static void
+handler (int sig)
+{
+  if (the_prompt)
+    fputs (the_prompt, stdout);
+  fflush (stdout);
+}
+
 static void
 readline_init (void)
 {
+  if (SIG_ERR == signal (SIGINT, handler))
+    perror ("Cannot add signal handler");
 }
 
 static void
@@ -444,19 +439,22 @@ readline_done (void)
 {
 }
 
+/* Prompt the user for a line of input and return it in LINE.
+   Returns true if the LINE should be considered valid, false otherwise.
+ */
 static bool
 readline_read (struct substring *line, enum prompt_style style)
 {
   struct string string;
-  const char *prompt = readline_prompt (style);
+  the_prompt = readline_prompt (style);
 
-  fputs (prompt, stdout);
+  fputs (the_prompt, stdout);
   fflush (stdout);
   ds_init_empty (&string);
   ds_read_line (&string, stdin, SIZE_MAX);
 
   *line = string.ss;
 
-  return false;
+  return true;
 }
 #endif /* !HAVE_READLINE */