diff --git a/src/Makefile.am b/src/Makefile.am index 2352ec7..7808425 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,7 +6,7 @@ libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 4:1:0 libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fparseln.c \ fsendfile.c ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c \ progress.c reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c \ - tempfile.c tree.c which.c + tempfile.c tree.c which.c conio.c ## Distribute these header files in the name space pkgincludedir = $(includedir)/lite diff --git a/src/conio.c b/src/conio.c new file mode 100644 index 0000000..d618ad7 --- /dev/null +++ b/src/conio.c @@ -0,0 +1,83 @@ +/* A conio.h like implementation for VTANSI displays. + * + * Copyright (c) 2009-2017 Joachim Nilsson + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +void initscr(int *row, int *col) +{ + if (!row || !col) + return; + +#if defined(TCSANOW) && defined(POLLIN) + struct termios tc, saved; + struct pollfd fd = { STDIN_FILENO, POLLIN, 0 }; + + if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) { + *row = 24; + *col = 80; + return; + } + + /* Disable echo to terminal while probing */ + tcgetattr(STDOUT_FILENO, &tc); + saved = tc; + tc.c_cflag |= (CLOCAL | CREAD); + tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + tcsetattr(STDOUT_FILENO, TCSANOW, &tc); + + /* + * Save cursor pos+attr + * Diable top+bottom margins + * Set cursor at the far bottom,right pos + * Query term for resulting pos + */ + fprintf(stdout, "\e7" "\e[r" "\e[999;999H" "\e[6n"); + fflush(stdout); + + /* + * Wait here for terminal to echo back \e[row,lineR ... + */ + if (poll(&fd, 1, 300) <= 0 || scanf("\e[%d;%dR", row, col) != 2) { + *row = 24; + *col = 80; + } + + /* + * Restore above saved cursor pos+attr + */ + fprintf(stdout, "\e8"); + fflush(stdout); + + /* Restore terminal */ + tcsetattr(STDOUT_FILENO, TCSANOW, &saved); + + return; +#else + *row = 24; + *col = 80; +#endif +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ diff --git a/src/conio.h b/src/conio.h index 33fbcb0..f24295c 100644 --- a/src/conio.h +++ b/src/conio.h @@ -19,8 +19,6 @@ #define CONIO_H_ #include -#include -#include /* Attributes */ #define RESETATTR 0 @@ -86,62 +84,6 @@ static inline void printheader(FILE *fp, const char *line, int nl) line, SCREEN_WIDTH - (int)strlen(line), ""); } -/* Requries: #inlude and #include */ -static inline void initscr(int *row, int *col) -{ - if (!row || !col) - return; - -#if defined(TCSANOW) && defined(POLLIN) - struct termios tc, saved; - struct pollfd fd = { STDIN_FILENO, POLLIN, 0 }; - - if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) { - *row = 24; - *col = 80; - return; - } - - /* Disable echo to terminal while probing */ - tcgetattr(STDOUT_FILENO, &tc); - saved = tc; - tc.c_cflag |= (CLOCAL | CREAD); - tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); - tcsetattr(STDOUT_FILENO, TCSANOW, &tc); - - /* - * Save cursor pos+attr - * Diable top+bottom margins - * Set cursor at the far bottom,right pos - * Query term for resulting pos - */ - fprintf(stdout, "\e7" "\e[r" "\e[999;999H" "\e[6n"); - fflush(stdout); - - /* - * Wait here for terminal to echo back \e[row,lineR ... - */ - if (poll(&fd, 1, 300) <= 0 || scanf("\e[%d;%dR", row, col) != 2) { - *row = 24; - *col = 80; - } - - /* - * Restore above saved cursor pos+attr - */ - fprintf(stdout, "\e8"); - fflush(stdout); - - /* Restore terminal */ - tcsetattr(STDOUT_FILENO, TCSANOW, &saved); - - return; -#else - *row = 24; - *col = 80; -#endif -} - #endif /* CONIO_H_ */ /**