Deal properly with EOF in the REPL, when built without readline
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 5 Sep 2020 02:31:28 +0000 (04:31 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 5 Sep 2020 02:33:59 +0000 (04:33 +0200)
src/ui/terminal/terminal-reader.c

index c23fad186f86992b164b401792ed3a1300f1706c..071e435eca6f572809331c84d2cd2fe493c1b8c5 100644 (file)
@@ -413,9 +413,21 @@ command_generator (const char *text, int state)
 
 #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
@@ -423,19 +435,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 */