Enable input editing with the linenoise library.
[open-adventure.git] / misc.c
diff --git a/misc.c b/misc.c
index 4d5dd31cc4eb50d29abde887c9e20d67b56864fa..0eae1623a25bc4c95539df1825435ff628087f5d 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -194,8 +194,7 @@ bool GETIN(FILE *input,
     for (;;) {
        if (game.blklin)
            TYPE0();
-       MAPLIN(input);
-       if (feof(input))
+       if (!MAPLIN(input))
            return false;
        *pword1=GETTXT(true,true,true);
        if (game.blklin && *pword1 < 0)
@@ -701,9 +700,10 @@ void BUG(long num)
 
 /*  Machine dependent routines (MAPLIN, TYPE, SAVEIO) */
 
-void MAPLIN(FILE *fp)
+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
@@ -730,15 +730,26 @@ void 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;
     } else {
        if (logfp && fp == stdin)
            IGNORE(fputs(rawbuf, logfp));
@@ -753,6 +764,7 @@ void MAPLIN(FILE *fp)
                LNLENG=i;
        }
        LNPOSN=1;
+       return true;
     }
 }