From 4b3082a34d7200d4b8ac980949bae355fc982c6b Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Sun, 9 Apr 2017 01:20:56 +0200 Subject: Minor cleanup and pretty printing --- common.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 common.c (limited to 'common.c') diff --git a/common.c b/common.c new file mode 100644 index 0000000..467a92c --- /dev/null +++ b/common.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + +int toggle_nonblocking_input() +{ + static struct termios *saved_termios = NULL; + int ret = 0; + + if (saved_termios) { + /* restore old settings */ + if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, saved_termios)) < 0) { + fprintf(stderr, "Failed restoring termios settings: %s\n", strerror(errno)); + } + free(saved_termios); + saved_termios = NULL; + + /* restore cursor */ + fprintf(stderr, "\033[?25h"); + } else { + struct termios new_termios; + + /* get and backup current settings */ + saved_termios = malloc(sizeof(struct termios)); + if (tcgetattr(STDIN_FILENO, saved_termios) < 0) { + fprintf(stderr, "Failed retrieving current termios setings: %s\n", strerror(errno)); + free(saved_termios); + saved_termios = NULL; + return -1; + } + memcpy(&new_termios, saved_termios, sizeof(struct termios)); + + /* disable echo and canonical mode (line by line input; line editing) */ + new_termios.c_lflag &= ~(ICANON | ECHO); + + if ((ret = tcsetattr(STDIN_FILENO, TCSANOW, &new_termios)) < 0) { + fprintf(stderr, "Failed setting to changed termios: %s\n", strerror(errno)); + } + + /* disable cursor */ + fprintf(stderr, "\033[?25l"); + } + + return ret; +} -- cgit v1.2.3