#include <stdbool.h>
#include <stdio.h>
+#include "libpspp/str.h"
#if HAVE_READLINE
#include <readline/readline.h>
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))
{
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;
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);
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
{
}
+/* 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 */