Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ports/posix: Allow building with -static, skipping readline #70

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ports/posix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ include $(SNEK_ROOT)/snek-install.defs

OPT?=-O3

CFLAGS+=-DSNEK_MEM_INCLUDE_NAME $(OPT) -g -I. $(SNEK_CFLAGS) -Werror $(CPPFLAGS) $(CFLAGS_POSIX)
STATIC?=0

LIBS=-lreadline -lm
ifeq ($(STATIC),0)
LIBS_READLINE=-lreadline
CFLAGS_READLINE=-DUSE_READLINE
else
LIBS_STATIC=-static
endif

CFLAGS+=-DSNEK_MEM_INCLUDE_NAME $(CFLAGS_READLINE) $(OPT) -g -I. $(SNEK_CFLAGS) -Werror $(CPPFLAGS) $(CFLAGS_POSIX)

LIBS=$(LIBS_STATIC) $(LIBS_READLINE) -lm

all: snek snek.desktop

Expand Down
25 changes: 22 additions & 3 deletions ports/posix/snek-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

#include "snek.h"
#include <getopt.h>
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include <signal.h>

FILE *snek_posix_input;
Expand All @@ -34,6 +36,23 @@ usage (char *program, int val)
exit(val);
}

#ifdef USE_READLINE
#define snek_readline(p) readline(p)
#define snek_add_history(p) add_history(p)
#define snek_free_line(p) free(p)
#else
static char *
snek_readline(char *prompt)
{
static char line[256];
fputs(prompt, stdout);
fflush(stdout);
return fgets(line, sizeof(line), stdin);
}
#define snek_add_history(p)
#define snek_free_line(p)
#endif

static int
snek_getc_interactive(void)
{
Expand All @@ -45,16 +64,16 @@ snek_getc_interactive(void)
char *prompt = "> ";
if (snek_parse_middle)
prompt = "+ ";
line_base = readline (prompt);
line_base = snek_readline (prompt);
line = line_base;
if (!line)
return EOF;
add_history (line_base);
snek_add_history (line_base);
}
c = (*line++) & 0xff;
if (!c) {
c = '\n';
free (line_base);
snek_free_line (line_base);
line = 0;
}
return c;
Expand Down