From 63e971fc4cfc4d7a5638da032326eaf016c62719 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sat, 10 Jun 2017 05:11:29 -0400 Subject: [PATCH] Enable input editing with the linenoise library. --- NEWS | 1 + advent.adoc | 10 +++++++++- advent.h | 2 +- main.c | 3 +++ misc.c | 21 ++++++++++++++++----- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 46acfa3..0614e59 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,7 @@ Repository head:: Include tests directory in generated tarball. + Support command-line editing with arrow keys and Emacs keystrokes. 1.0: 2016-06-05:: Forward port of Crowther & Woods's 430-point Adventure 2.5. diff --git a/advent.adoc b/advent.adoc index a53ccd3..1005809 100644 --- a/advent.adoc +++ b/advent.adoc @@ -25,11 +25,19 @@ PDP-10 on which it originally ran limited filenames to 6 characters. This version is released as open source with the permission and encouragement of the original authors. +Unlike the original, this version supports use of your arrow keys to edit +your command line in place. Basic Emacs keystrokes are supported, and +your up/down arrows access a command history. + +To exit the game, type Ctrl-D (EOF). + +There have been no gameplay changes. + == OPTIONS == -l:: Log commands to specified file. --o:: Old-style. Restores original interface, no prompt. +-o:: Old-style. Restores original interface, no prompt or command history. == BUGS == diff --git a/advent.h b/advent.h index 51e7317..9e50d9c 100644 --- a/advent.h +++ b/advent.h @@ -118,7 +118,7 @@ extern long SETBIT(long); extern bool TSTBIT(long,int); extern long RNDVOC(long,long); extern void BUG(long) __attribute__((noreturn)); -extern void MAPLIN(FILE *); +extern bool MAPLIN(FILE *); extern void TYPE(void); extern void fSAVEIO(long,long,long*); diff --git a/main.c b/main.c index 4a5d3c5..31eaf46 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ #include #include "advent.h" #include "database.h" +#include "linenoise/linenoise.h" struct game_t game; @@ -82,6 +83,8 @@ case 'l': } } + linenoiseHistorySetMaxLen(350); + /* Logical variables: * * game.closed says whether we're all the way closed diff --git a/misc.c b/misc.c index b991117..0eae162 100644 --- a/misc.c +++ b/misc.c @@ -703,6 +703,7 @@ void BUG(long num) bool MAPLIN(FILE *fp) { long i, val; + bool eof; /* Read a line of input, from the specified input source, * translate the chars to integers in the range 0-126 and store @@ -729,13 +730,23 @@ bool MAPLIN(FILE *fp) * and is not changed thereafter unless the routines on this page choose * to do so. */ - if (!oldstyle && fp == stdin) - fputs("> ", stdout); do { - IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp)); + if (oldstyle) { + IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp)); + eof = (feof(fp)); + } else { + char *cp = linenoise("> "); + eof = (cp == NULL); + if (!eof) { + strncpy(rawbuf, cp, sizeof(rawbuf)-1); + linenoiseHistoryAdd(rawbuf); + strncat(rawbuf, "\n", sizeof(rawbuf)-1); + linenoiseFree(cp); + } + } } while - (!feof(fp) && rawbuf[0] == '#'); - if (feof(fp)) { + (!eof && rawbuf[0] == '#'); + if (eof) { if (logfp && fp == stdin) fclose(logfp); return false; -- 2.31.1