Replace linenoise with libedit in code and build.

This commit is contained in:
Jason S. Ninneman 2017-07-01 06:51:00 -07:00
parent f47f3a4603
commit ad3b097c9e
5 changed files with 24 additions and 65 deletions

View file

@ -1,17 +1,6 @@
# Makefile for the open-source release of adventure 2.5
# Note: If you're building from the repository rather than the source tarball,
# do this to get the linenoise library where you can use it:
#
# git submodule update --recursive --remote --init
#
# Therafter, you can update it like this:
#
# git submodule update --recursive --remote
#
# but this should seldom be necessary as that library is pretty stable.
#
# You will also need Python 3 YAML. Under Debian or ubuntu:
# You will need Python 3 YAML. Under Debian or ubuntu:
#
# apt-get install python3-yaml
#
@ -31,23 +20,23 @@ VERS=$(shell sed -n <NEWS '/^[0-9]/s/:.*//p' | head -1)
CC?=gcc
CCFLAGS+=-std=c99 -D_DEFAULT_SOURCE -DVERSION=\"$(VERS)\" -Wpedantic -O2
LIBS=
LIBS=-ledit
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LIBS=-lrt
LIBS+=-lrt
endif
OBJS=main.o init.o actions.o score.o misc.o saveresume.o
CHEAT_OBJS=cheat.o init.o actions.o score.o misc.o saveresume.o
SOURCES=$(OBJS:.o=.c) advent.h adventure.yaml Makefile control linenoise/linenoise.[ch] make_dungeon.py
SOURCES=$(OBJS:.o=.c) advent.h adventure.yaml Makefile control make_dungeon.py
.c.o:
$(CC) $(CCFLAGS) $(DBX) -c $<
advent: $(OBJS) linenoise.o dungeon.o
$(CC) $(CCFLAGS) $(DBX) -o advent $(OBJS) dungeon.o linenoise.o $(LDFLAGS) $(LIBS)
advent: $(OBJS) dungeon.o
$(CC) $(CCFLAGS) $(DBX) -o advent $(OBJS) dungeon.o $(LDFLAGS) $(LIBS)
main.o: advent.h dungeon.h linenoise.o
main.o: advent.h dungeon.h
init.o: advent.h dungeon.h
@ -57,7 +46,7 @@ score.o: advent.h dungeon.h
misc.o: advent.h dungeon.h
cheat.o: advent.h dungeon.h linenoise.o
cheat.o: advent.h dungeon.h
saveresume.o: advent.h dungeon.h
@ -67,16 +56,6 @@ dungeon.o: dungeon.c dungeon.h
dungeon.c dungeon.h: make_dungeon.py adventure.yaml
python3 make_dungeon.py
linenoise.o: linenoise/linenoise.h
@test -s linenoise/linenoise.h || { echo -e "linenoise not present. Try:\n\
git submodule update --recursive --remote --init"; exit 1; }
$(CC) -c linenoise/linenoise.c
linenoise-dbg: linenoise/linenoise.h
@test -s linenoise/linenoise.h || { echo -e "linenoise not present. Try:\n\
git submodule update --recursive --remote --init"; exit 1; }
$(CC) $(CCFLAGS) -c linenoise/linenoise.c
clean:
rm -f *.o advent cheat *.html *.gcno *.gcda
rm -f dungeon.c dungeon.h
@ -87,8 +66,8 @@ clean:
cd tests; $(MAKE) --quiet clean
cheat: $(CHEAT_OBJS) linenoise.o dungeon.o
$(CC) $(CCFLAGS) $(DBX) -o cheat $(CHEAT_OBJS) linenoise.o dungeon.o $(LDFLAGS) $(LIBS)
cheat: $(CHEAT_OBJS) dungeon.o
$(CC) $(CCFLAGS) $(DBX) -o cheat $(CHEAT_OBJS) dungeon.o $(LDFLAGS) $(LIBS)
check: advent cheat
cd tests; $(MAKE) --quiet
@ -155,4 +134,3 @@ debug: CCFLAGS += -O0 --coverage -ggdb
debug: linty
debug: cheat
debug-ln: linenoise-dbg debug

View file

@ -4,7 +4,6 @@
#include <stdbool.h>
#include <time.h>
#include "advent.h"
#include "linenoise/linenoise.h"
#include "dungeon.h"
FILE *logfp = NULL, *rfp = NULL;

5
main.c
View file

@ -21,7 +21,6 @@
#include <signal.h>
#include <string.h>
#include "advent.h"
#include "linenoise/linenoise.h"
#include "dungeon.h"
#define DIM(a) (sizeof(a)/sizeof(a[0]))
@ -115,8 +114,6 @@ int main(int argc, char *argv[])
}
}
linenoiseHistorySetMaxLen(350);
/* Initialize game variables */
long seedval = initialise();
@ -1036,7 +1033,7 @@ L2600:
}
strncpy(inputbuf, input, LINESIZE - 1);
linenoiseFree(input);
free(input);
long tokens[4];
tokenize(inputbuf, tokens);

31
misc.c
View file

@ -5,9 +5,9 @@
#include <stdarg.h>
#include <sys/time.h>
#include <ctype.h>
#include <editline/readline.h>
#include "advent.h"
#include "linenoise/linenoise.h"
#include "dungeon.h"
char* xstrdup(const char* s)
@ -334,27 +334,12 @@ char* get_input()
char* input;
while (true) {
if (editline)
input = linenoise(input_prompt);
else {
input = NULL;
size_t n = 0;
if (isatty(0))
// LCOV_EXCL_START
// Should be unreachable in tests, as they will use a non-interactive shell.
printf("%s", input_prompt);
// LCOV_EXCL_STOP
ssize_t numread = getline(&input, &n, stdin);
if (numread == -1) { // Got EOF; return with it.
free(input);
return (NULL);
}
}
input = readline(input_prompt);
if (input == NULL) // Got EOF; return with it.
return (input);
else if (input[0] == '#') { // Ignore comments.
linenoiseFree(input);
free(input);
continue;
} else // We have a 'normal' line; leave the loop.
break;
@ -363,7 +348,7 @@ char* get_input()
// Strip trailing newlines from the input
input[strcspn(input, "\n")] = 0;
linenoiseHistoryAdd(input);
add_history(input);
if (!isatty(0))
echo_input(stdout, input_prompt, input);
@ -384,7 +369,7 @@ bool silent_yes()
if (reply == NULL) {
// LCOV_EXCL_START
// Should be unreachable. Reply should never be NULL
linenoiseFree(reply);
free(reply);
exit(EXIT_SUCCESS);
// LCOV_EXCL_STOP
}
@ -392,7 +377,7 @@ bool silent_yes()
char* firstword = (char*) xmalloc(strlen(reply) + 1);
sscanf(reply, "%s", firstword);
linenoiseFree(reply);
free(reply);
for (int i = 0; i < (int)strlen(firstword); ++i)
firstword[i] = tolower(firstword[i]);
@ -431,7 +416,7 @@ bool yes(const char* question, const char* yes_response, const char* no_response
if (reply == NULL) {
// LCOV_EXCL_START
// Should be unreachable. Reply should never be NULL
linenoiseFree(reply);
free(reply);
exit(EXIT_SUCCESS);
// LCOV_EXCL_STOP
}
@ -439,7 +424,7 @@ bool yes(const char* question, const char* yes_response, const char* no_response
char* firstword = (char*) xmalloc(strlen(reply) + 1);
sscanf(reply, "%s", firstword);
linenoiseFree(reply);
free(reply);
for (int i = 0; i < (int)strlen(firstword); ++i)
firstword[i] = tolower(firstword[i]);

View file

@ -1,9 +1,9 @@
#include <stdlib.h>
#include <string.h>
#include <editline/readline.h>
#include "advent.h"
#include "dungeon.h"
#include "linenoise/linenoise.h"
/*
* (ESR) This replaces a bunch of particularly nasty FORTRAN-derived code;
@ -63,13 +63,13 @@ int suspend(void)
game.saved = game.saved + 5;
while (fp == NULL) {
char* name = linenoise("\nFile name: ");
char* name = readline("\nFile name: ");
if (name == NULL)
return GO_TOP;
fp = fopen(name, WRITE_MODE);
if (fp == NULL)
printf("Can't open file %s, try again.\n", name);
linenoiseFree(name);
free(name);
}
savefile(fp, VRSION);
@ -95,13 +95,13 @@ int resume(void)
}
while (fp == NULL) {
char* name = linenoise("\nFile name: ");
char* name = readline("\nFile name: ");
if (name == NULL)
return GO_TOP;
fp = fopen(name, READ_MODE);
if (fp == NULL)
printf("Can't open file %s, try again.\n", name);
linenoiseFree(name);
free(name);
}
return restore(fp);