diff --git a/src/lib/fd.c b/src/lib/fd.c index 04c9caea0..b88c413de 100644 --- a/src/lib/fd.c +++ b/src/lib/fd.c @@ -79,15 +79,25 @@ ncfdplane_thread(void* vncfp){ return NULL; } -int set_fd_nonblocking(int fd){ +int set_fd_nonblocking(int fd, unsigned state, unsigned* oldstate){ int flags = fcntl(fd, F_GETFL, 0); if(flags < 0){ return -1; } - if(flags & O_NONBLOCK){ - return 0; + if(oldstate){ + *oldstate = flags & O_NONBLOCK; + } + if(state){ + if(flags & O_NONBLOCK){ + return 0; + } + flags |= O_NONBLOCK; + }else{ + if(!(flags & O_NONBLOCK)){ + return 0; + } + flags &= ~O_NONBLOCK; } - flags |= O_NONBLOCK; if(fcntl(fd, F_SETFL, flags)){ return -1; } @@ -188,7 +198,7 @@ launch_pipe_process(int* pipe, int* pidfd){ } }else if(p > 0){ // parent *pipe = pipes[0]; - set_fd_nonblocking(*pipe); + set_fd_nonblocking(*pipe, 1, NULL); } return p; } diff --git a/src/lib/internal.h b/src/lib/internal.h index 22e641b66..2a61458c8 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -373,6 +373,7 @@ typedef struct notcurses { int loglevel; palette256 palette; // 256-indexed palette can be used instead of/with RGB bool palette_damage[NCPALETTESIZE]; + unsigned stdio_blocking_save; // was stdio blocking at entry? restore on stop. } notcurses; #include "blitset.h" @@ -1152,7 +1153,7 @@ int ncinputlayer_init(ncinputlayer* nilayer, FILE* infp); // FIXME absorb into ncinputlayer_init() int cbreak_mode(int ttyfd, const struct termios* tpreserved); -int set_fd_nonblocking(int fd); +int set_fd_nonblocking(int fd, unsigned state, unsigned* oldstate); // Given the four channels arguments, verify that: // diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index c5eed9371..8f811aa41 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -777,6 +777,7 @@ void notcurses_stats_reset(notcurses* nc, ncstats* stats){ // the environment / terminal definition. static void init_banner_warnings(const notcurses* nc, FILE* out){ + // might be using stderr, so don't just reuse stdout decision const bool tty = isatty(fileno(out)); if(tty){ term_fg_palindex(nc, out, nc->tcache.colors <= 88 ? 1 % nc->tcache.colors : 0xcb); @@ -1064,7 +1065,7 @@ notcurses* notcurses_core_init(const notcurses_options* opts, FILE* outfp){ if(ncinputlayer_init(&ret->input, stdin)){ goto err; } - if(set_fd_nonblocking(ret->input.ttyinfd)){ + if(set_fd_nonblocking(ret->input.ttyinfd, 1, &ret->stdio_blocking_save)){ goto err; } // Neither of these is supported on e.g. the "linux" virtual console. @@ -1168,6 +1169,7 @@ int notcurses_stop(notcurses* nc){ int ret = 0; if(nc){ ret |= notcurses_stop_minimal(nc); + ret |= set_fd_nonblocking(nc->input.ttyinfd, nc->stdio_blocking_save, NULL); if(nc->stdplane){ notcurses_drop_planes(nc); free_plane(nc->stdplane);