client: Tie up a few lose readline ends

Document readline in more detail in man/empire.6.

Make @history_file local to main().

main() silently truncates the home directory name to 1000 characters
when constructing the history file name; mark FIXME.

Set @rl_already_prompted just once.

Write history file on unsuccessful exit, too.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
Markus Armbruster 2015-12-13 09:10:08 +01:00
parent f1fc0df03d
commit 0cb6690600
4 changed files with 36 additions and 21 deletions

View file

@ -3,7 +3,7 @@
empire \- Empire client empire \- Empire client
.SH SYNOPSIS .SH SYNOPSIS
.B empire .B empire
[\fB\-hkruv\fP] [\fB\-hHkruv\fP]
[\fB\-2\fP \fIoutfile\fP] [\fB\-2\fP \fIoutfile\fP]
[\fB\-s\fP \fI[host:]port\fP] [\fB\-s\fP \fI[host:]port\fP]
[\fIcountry\fP [\fIcountry\fP
@ -24,6 +24,11 @@ the thin veneer of civilization that hides the maniac within.
.B \-h .B \-h
Help. Print brief usage information and exit. Help. Print brief usage information and exit.
.TP .TP
.B \-H
Save readline command history to file.
.IP
Only available when compiled the GNU \fBreadline\fP library.
.TP
.B \-k .B \-k
If someone else is connected to your country, kill their connection. If someone else is connected to your country, kill their connection.
.TP .TP
@ -39,11 +44,6 @@ Use UTF-8 rather than ASCII character set.
This requires server version 4.2.21 or later, and a terminal that This requires server version 4.2.21 or later, and a terminal that
understands UTF-8. understands UTF-8.
.TP .TP
.B \-H
Save readline command history to file.
.IP
Only available when compiled with readline and history support.
.TP
.B \-v .B \-v
Print version information and exit. Print version information and exit.
.TP .TP
@ -79,8 +79,20 @@ representative.
.TP .TP
.I LOGNAME .I LOGNAME
Your user name. Your user name.
.TP
.I INPUTRC
The filename for the \fBreadline\fP startup file, overriding the
default of \fI~/.inputrc\fP (see \fBREADLINE\fP below).
.SH READLINE
When compiled with the GNU \fBreadline\fP library, the client supports
fancy line editing and, with option \fB-H\fP, persistent history. See
the readline documentation for details.
.SH "SEE ALSO" .SH "SEE ALSO"
\fIemp_server\fR(6). \fIemp_server\fR(6), \fIreadline\fR(3).
.SH FILES
.TP
.I ~/.inputrc
Individual \fBreadline\fP initialization file
.SH AUTHORS .SH AUTHORS
.nf .nf
Primary Author is Dave Pare <mr-frog@scam.berkeley.edu> Primary Author is Dave Pare <mr-frog@scam.berkeley.edu>
@ -90,6 +102,7 @@ Jeff Anton <anton@postgres.berkeley.edu>
Markus Armbruster <armbru@pond.sub.org> Markus Armbruster <armbru@pond.sub.org>
Phill Everson <everson@compsci.bristol.ac.uk> Phill Everson <everson@compsci.bristol.ac.uk>
Steven Grimm <koreth@ucscb.UCSC.EDU> Steven Grimm <koreth@ucscb.UCSC.EDU>
Martin Haukeli <martin.haukeli@gmail.com>
Lewis R. Jansen <lrj@helios.tn.cornell.edu> Lewis R. Jansen <lrj@helios.tn.cornell.edu>
Mike St. Johns <stjohns@edn-vax.arpa> Mike St. Johns <stjohns@edn-vax.arpa>
Ron Koenderink <rkoenderink@yahoo.ca> Ron Koenderink <rkoenderink@yahoo.ca>

View file

@ -30,8 +30,9 @@
* Dave Pare, 1986 * Dave Pare, 1986
* Steve McClure, 1998 * Steve McClure, 1998
* Ron Koenderink, 2004-2007 * Ron Koenderink, 2004-2007
* Markus Armbruster, 2005-2010 * Markus Armbruster, 2005-2015
* Tom Dickson-Hunt, 2010 * Tom Dickson-Hunt, 2010
* Martin Haukeli, 2015
*/ */
#include <config.h> #include <config.h>
@ -99,8 +100,9 @@ main(int argc, char **argv)
char *udir; char *udir;
char *colon; char *colon;
int sock; int sock;
char *history_file;
while ((opt = getopt(argc, argv, "2:krs:uHhv")) != EOF) { while ((opt = getopt(argc, argv, "2:Hkrs:uhv")) != EOF) {
switch (opt) { switch (opt) {
case '2': case '2':
auxfname = optarg; auxfname = optarg;
@ -189,6 +191,7 @@ main(int argc, char **argv)
sock = tcp_connect(host, port); sock = tcp_connect(host, port);
if (use_history_file) { if (use_history_file) {
/* FIXME don't truncate udir */
history_file = malloc(1024); history_file = malloc(1024);
strncpy(history_file, udir, 1000); strncpy(history_file, udir, 1000);
strcat(history_file, "/.empire.history"); strcat(history_file, "/.empire.history");
@ -197,7 +200,7 @@ main(int argc, char **argv)
if (!login(sock, uname, country, passwd, send_kill, utf8)) if (!login(sock, uname, country, passwd, send_kill, utf8))
exit(1); exit(1);
if (play(sock) < 0) if (play(sock, history_file) < 0)
exit(1); exit(1);
return 0; return 0;

View file

@ -44,7 +44,6 @@ extern char empireport[];
extern int eight_bit_clean; extern int eight_bit_clean;
extern FILE *auxfp; extern FILE *auxfp;
extern int restricted; extern int restricted;
extern char *history_file;
#ifdef HAVE_CURSES_TERMINFO #ifdef HAVE_CURSES_TERMINFO
void getsose(void); void getsose(void);
@ -61,7 +60,7 @@ int parseid(char *);
int expect(int s, int match, char *buf); int expect(int s, int match, char *buf);
int tcp_connect(char *, char *); int tcp_connect(char *, char *);
int login(int s, char *uname, char *cname, char *cpass, int kill_proc, int); int login(int s, char *uname, char *cname, char *cpass, int kill_proc, int);
int play(int); int play(int, char *);
void sendcmd(int s, char *cmd, char *arg); void sendcmd(int s, char *cmd, char *arg);
int servercmd(int, char *, int); int servercmd(int, char *, int);
void outch(char); void outch(char);

View file

@ -29,6 +29,7 @@
* Known contributors to this file: * Known contributors to this file:
* Markus Armbruster, 2007-2017 * Markus Armbruster, 2007-2017
* Ron Koenderink, 2007-2009 * Ron Koenderink, 2007-2009
* Martin Haukeli, 2015
*/ */
#include <config.h> #include <config.h>
@ -436,7 +437,6 @@ recv_output(int sock)
return n; return n;
} }
char *history_file = NULL;
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
static char *input_from_rl; static char *input_from_rl;
static int has_rl_input; static int has_rl_input;
@ -446,7 +446,6 @@ input_handler(char *line)
{ {
input_from_rl = line; input_from_rl = line;
has_rl_input = 1; has_rl_input = 1;
rl_already_prompted = 1;
#ifdef HAVE_READLINE_HISTORY #ifdef HAVE_READLINE_HISTORY
if (line && *line) if (line && *line)
add_history(line); add_history(line);
@ -536,11 +535,12 @@ intr(int sig)
/* /*
* Play on @sock. * Play on @sock.
* @history_file is the name of the history file, or null.
* The session must be in the playing phase. * The session must be in the playing phase.
* Return 0 when the session ended, -1 on error. * Return 0 when the session ended, -1 on error.
*/ */
int int
play(int sock) play(int sock, char *history_file)
{ {
/* /*
* Player input flows from INPUT_FD through recv_input() into ring * Player input flows from INPUT_FD through recv_input() into ring
@ -564,6 +564,7 @@ play(int sock)
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;
#ifdef HAVE_READLINE_HISTORY #ifdef HAVE_READLINE_HISTORY
if (history_file) if (history_file)
read_history(history_file); read_history(history_file);
@ -665,19 +666,18 @@ play(int sock)
break; break;
} }
if (n == 0) { if (n == 0) {
#ifdef HAVE_LIBREADLINE
#ifdef HAVE_READLINE_HISTORY
if (history_file)
write_history(history_file);
#endif /* HAVE_READLINE_HISTORY */
#endif /* HAVE_LIBREADLINE */
ret = 0; ret = 0;
break; break;
} }
} }
} }
#ifdef HAVE_LIBREADLINE #ifdef HAVE_LIBREADLINE
rl_callback_handler_remove(); rl_callback_handler_remove();
#ifdef HAVE_READLINE_HISTORY
if (history_file)
write_history(history_file);
#endif /* HAVE_READLINE_HISTORY */
#endif #endif
return ret; return ret;
} }