From 14403375798d1d836f81a538acaefdef8e596cd8 Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Wed, 24 Oct 2018 22:20:33 -0400 Subject: [PATCH] Move STDIN association to termio module modified: Makefile modified: src/nmseffect.c modified: src/nmstermio.c modified: src/nmstermio_ncurses.c --- Makefile | 2 +- src/nmseffect.c | 6 ------ src/nmstermio.c | 4 ++++ src/nmstermio_ncurses.c | 5 +++++ 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index ef09b34..c96efa1 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ all: nms sneakers all-ncurses: nms-ncurses sneakers-ncurses -nms-ncurses: $(OBJ)/nmscharset.o $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN) +nms-ncurses: $(OBJ)/input.o $(OBJ)/error.o $(OBJ)/nmscharset.o $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/nms.o | $(BIN) $(CC) $(CFLAGS) -o $(BIN)/nms $^ -lncurses sneakers-ncurses: $(OBJ)/nmscharset.o $(OBJ)/nmstermio_ncurses.o $(OBJ)/nmseffect.o $(OBJ)/sneakers.o | $(BIN) diff --git a/src/nmseffect.c b/src/nmseffect.c index 1883d91..9abcfdf 100644 --- a/src/nmseffect.c +++ b/src/nmseffect.c @@ -63,12 +63,6 @@ char nmseffect_exec(unsigned char *string, int string_len) { int maxRows, maxCols, curRow, curCol, origRow = 0, origCol = 0; char ret = 0; - // Reassociate STDIN to the terminal if needed - if (!isatty(STDIN_FILENO) && !freopen ("/dev/tty", "r", stdin)) { - fprintf(stderr, "Error. Can't associate STDIN with terminal.\n"); - return 0; - } - // Needed for UTF-8 support setlocale(LC_ALL, ""); diff --git a/src/nmstermio.c b/src/nmstermio.c index bad9695..f4f082c 100644 --- a/src/nmstermio.c +++ b/src/nmstermio.c @@ -294,6 +294,10 @@ static void nmstermio_set_terminal(int s) { struct termios tp; static struct termios save; static int state = 1; + + if (!isatty(STDIN_FILENO)) { + stdin = freopen("/dev/tty", "r", stdin); + } if (s == 0) { if (tcgetattr(STDIN_FILENO, &tp) == -1) { diff --git a/src/nmstermio_ncurses.c b/src/nmstermio_ncurses.c index 1cdfd8a..928ede7 100644 --- a/src/nmstermio_ncurses.c +++ b/src/nmstermio_ncurses.c @@ -11,6 +11,8 @@ * functionality is defined and implemented here. */ +#include +#include #include #include @@ -25,6 +27,9 @@ static int foregroundColor = COLOR_BLUE; // Foreground color settin * otherwise. */ void nmstermio_init_terminal(void) { + if (!isatty(STDIN_FILENO)) { + stdin = freopen("/dev/tty", "r", stdin); + } initscr(); cbreak(); noecho();