* Known contributors to this file:
* Markus Armbruster, 2007-2017
* Ron Koenderink, 2007-2009
+ * Martin Haukeli, 2015
*/
#include <config.h>
#include "ringbuf.h"
#include "secure.h"
+#ifdef HAVE_LIBREADLINE
+#include <readline/readline.h>
+#include <readline/history.h>
+#endif
+
#define EOF_COOKIE "ctld\n"
#define INTR_COOKIE "aborted\n"
return n;
}
+#ifdef HAVE_LIBREADLINE
+static char *input_from_rl;
+static int has_rl_input;
+
+static void
+input_handler(char *line)
+{
+ input_from_rl = line;
+ has_rl_input = 1;
+ if (line && *line)
+ add_history(line);
+}
+
+static int
+ring_from_rl(struct ring *inbuf)
+{
+ size_t len;
+ int n;
+
+ assert(has_rl_input && input_from_rl);
+
+ len = strlen(input_from_rl);
+ n = ring_space(inbuf);
+ assert(n);
+
+ if (len >= (size_t)n) {
+ ring_putm(inbuf, input_from_rl, n);
+ memmove(input_from_rl, input_from_rl + n, len - n + 1);
+ } else {
+ ring_putm(inbuf, input_from_rl, len);
+ ring_putc(inbuf, '\n');
+ free(input_from_rl);
+ has_rl_input = 0;
+ n = len + 1;
+ }
+
+ return n;
+}
+#endif /* HAVE_LIBREADLINE */
+
/*
* Receive player input from @fd into @inbuf.
* Return 1 on receipt of input, zero on EOF, -1 on error.
static int
recv_input(int fd, struct ring *inbuf)
{
- int n, i, ch;
+ int n;
int res = 1;
- n = ring_from_file(inbuf, fd);
+#ifdef HAVE_LIBREADLINE
+ if (fd == 0) {
+ if (!has_rl_input)
+ rl_callback_read_char();
+ if (!has_rl_input)
+ return 1;
+ if (input_from_rl) {
+ n = ring_from_rl(inbuf);
+ } else
+ n = 0;
+ } else
+#endif
+ n = ring_from_file(inbuf, fd);
if (n < 0)
return -1;
if (n == 0) {
res = 0;
}
- /* copy input to AUXFP etc. */
- for (i = -n; i < 0; i++) {
- ch = ring_peek(inbuf, i);
- assert(ch != EOF);
- if (ch != '\r')
- save_input(ch);
- if (auxfp)
- putc(ch, auxfp);
- }
-
return res;
}
send_input(int fd, struct ring *inbuf)
{
struct iovec iov[2];
- int cnt;
+ int cnt, i, ch;
ssize_t res;
cnt = ring_to_iovec(inbuf, iov);
res = writev(fd, iov, cnt);
if (res < 0)
return res;
- ring_discard(inbuf, res);
+
+ /* Copy input to @auxfp etc. */
+ for (i = 0; i < res; i++) {
+ ch = ring_getc(inbuf);
+ assert(ch != EOF);
+ if (ch != '\r')
+ save_input(ch);
+ if (auxfp)
+ putc(ch, auxfp);
+ }
+
+#ifdef HAVE_LIBREADLINE
+ if (fd == 0 && has_rl_input && input_from_rl)
+ ring_from_rl(inbuf);
+#endif
+
return res;
}
/*
* Play on @sock.
+ * @history_file is the name of the history file, or null.
* The session must be in the playing phase.
* Return 0 when the session ended, -1 on error.
*/
int
-play(int sock)
+play(int sock, char *history_file)
{
/*
* Player input flows from INPUT_FD through recv_input() into ring
int send_eof; /* need to send EOF_COOKIE */
fd_set rdfd, wrfd;
int n;
+ int ret = -1;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
+#ifdef HAVE_LIBREADLINE
+ rl_already_prompted = 1;
+ if (history_file)
+ read_history(history_file);
+ rl_bind_key('\t', rl_insert); /* Disable tab completion */
+ rl_callback_handler_install("", input_handler);
+#endif /* HAVE_LIBREADLINE */
ring_init(&inbuf);
eof_fd0 = partial_line_sent = send_eof = send_intr = 0;
if (n < 0) {
if (errno != EINTR) {
perror("select");
- return -1;
+ break;
}
}
input_fd = 0;
}
}
-
if (n < 0)
continue;
sigaction(SIGINT, &sa, NULL);
send_intr = 0;
}
- } else
+ } else if (ring_len(&inbuf) > 0)
partial_line_sent = ring_peek(&inbuf, -1) != '\n';
}
n = send_input(sock, &inbuf);
if (n < 0) {
perror("write socket");
- return -1;
+ break;
}
}
n = recv_output(sock);
if (n < 0) {
perror("read socket");
- return -1;
+ break;
+ }
+ if (n == 0) {
+ ret = 0;
+ break;
}
- if (n == 0)
- return 0;
}
}
+
+#ifdef HAVE_LIBREADLINE
+ rl_callback_handler_remove();
+ if (history_file)
+ write_history(history_file);
+#endif
+ return ret;
+}
+
+void
+prompt(int code, char *prompt, char *teles)
+{
+ char pr[1024];
+
+ snprintf(pr, sizeof(pr), "%s%s", teles, prompt);
+#ifdef HAVE_LIBREADLINE
+ rl_set_prompt(pr);
+ rl_forced_update_display();
+#else /* !HAVE_LIBREADLINE */
+ printf("%s", pr);
+ fflush(stdout);
+#endif /* !HAVE_LIBREADLINE */
+ if (auxfp) {
+ fprintf(auxfp, "%s%s", teles, prompt);
+ fflush(auxfp);
+ }
}