client: Use readline only when standard input is a TTY

Readline is for interactive use.  For non-interactive use, it merely
complicates things.  Case in point: it slows down "make check" by almost
10% for me.

Interactive use should always involve a TTY, so use readline only when
standard input is a TTY.  This supresses readline in "make check".

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2017-07-09 17:05:51 +02:00
parent a0220e864f
commit de638fd779

View file

@ -421,6 +421,7 @@ recv_output(int sock)
} }
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
static int use_readline;
static char *input_from_rl; static char *input_from_rl;
static int has_rl_input; static int has_rl_input;
@ -471,7 +472,7 @@ recv_input(int fd, struct ring *inbuf)
int res = 1; int res = 1;
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
if (fd == 0) { if (fd == 0 && use_readline) {
if (!has_rl_input) if (!has_rl_input)
rl_callback_read_char(); rl_callback_read_char();
if (!has_rl_input) if (!has_rl_input)
@ -519,7 +520,7 @@ send_input(int fd, struct ring *inbuf)
} }
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
if (fd == 0 && has_rl_input && input_from_rl) if (fd == 0 && use_readline && has_rl_input && input_from_rl)
ring_from_rl(inbuf); ring_from_rl(inbuf);
#endif #endif
@ -563,11 +564,14 @@ play(int sock, char *history_file)
sa.sa_handler = SIG_IGN; sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL); sigaction(SIGPIPE, &sa, NULL);
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
rl_already_prompted = 1; if (isatty(0)) {
if (history_file) use_readline = 1;
read_history(history_file); rl_already_prompted = 1;
rl_bind_key('\t', rl_insert); /* Disable tab completion */ if (history_file)
rl_callback_handler_install("", input_handler); read_history(history_file);
rl_bind_key('\t', rl_insert); /* Disable tab completion */
rl_callback_handler_install("", input_handler);
}
#endif /* HAVE_LIBREADLINE */ #endif /* HAVE_LIBREADLINE */
ring_init(&inbuf); ring_init(&inbuf);
@ -670,9 +674,11 @@ play(int sock, char *history_file)
} }
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
rl_callback_handler_remove(); if (use_readline) {
if (history_file) rl_callback_handler_remove();
write_history(history_file); if (history_file)
write_history(history_file);
}
#endif #endif
return ret; return ret;
} }
@ -684,12 +690,15 @@ prompt(int code, char *prompt, char *teles)
snprintf(pr, sizeof(pr), "%s%s", teles, prompt); snprintf(pr, sizeof(pr), "%s%s", teles, prompt);
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
rl_set_prompt(pr); if (use_readline) {
rl_forced_update_display(); rl_set_prompt(pr);
#else /* !HAVE_LIBREADLINE */ rl_forced_update_display();
printf("%s", pr); } else
fflush(stdout); #endif /* HAVE_LIBREADLINE */
#endif /* !HAVE_LIBREADLINE */ {
printf("%s", pr);
fflush(stdout);
}
if (auxfp) { if (auxfp) {
fprintf(auxfp, "%s%s", teles, prompt); fprintf(auxfp, "%s%s", teles, prompt);
fflush(auxfp); fflush(auxfp);