From d5aaacda5a4b6cbea654c0d0cca3c901b8dda3d3 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Sat, 9 May 2009 16:52:07 +1000 Subject: introduce libbb/termios.c for terminal-related functions --- libbb/Kbuild | 1 + libbb/lineedit.c | 68 ++++------------------------------------------------ libbb/termios.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ libbb/xfuncs.c | 21 +--------------- 4 files changed, 80 insertions(+), 83 deletions(-) create mode 100644 libbb/termios.c (limited to 'libbb') diff --git a/libbb/Kbuild b/libbb/Kbuild index e335a95dc..b3560bd0b 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild @@ -138,6 +138,7 @@ lib-$(CONFIG_MINGW32) += setenv.o lib-$(CONFIG_MINGW32) += strbuf.o lib-$(CONFIG_MINGW32) += strbuf_file.o lib-$(CONFIG_MINGW32) += strlcpy.o +lib-$(CONFIG_MINGW32) += termios.o lib-$(CONFIG_MINGW32) += trace.o lib-$(CONFIG_MINGW32) += usage.o lib-$(CONFIG_MINGW32) += winansi.o diff --git a/libbb/lineedit.c b/libbb/lineedit.c index b349bc6f2..e56dd20b0 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -59,28 +59,13 @@ #define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \ (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT) - -static line_input_t *state; - #ifdef __MINGW32__ -struct termios { - int c_lflag; - int c_cc[3]; -}; -#define safe_read(fd,buf,n) wincon_read(fd,buf,n) -/* True value does not matter because it's emulated */ -#define ICANON 1 -#define ECHO 2 -#define ECHONL 4 -#define ISIG 8 - -#define VMIN 0 -#define VTIME 1 -#define VINTR 2 - -static int wincon_read(int fd, char *buf, int size); +#include "cygwin_termios.h" +#define safe_read(fd,buf,size) wincon_read(fd,buf,size) #endif +static line_input_t *state; + static struct termios initial_settings, new_settings; static volatile unsigned cmdedit_termw = 80; /* actual terminal width */ @@ -1227,13 +1212,10 @@ static void parse_prompt(const char *prmt_ptr) } #endif -#ifdef __MINGW32__ -#define setTermSettings(fd, argp) -#define getTermSettings(fd, argp) ((struct termios *)argp)->c_lflag = ECHO; -#else #define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp) #define getTermSettings(fd, argp) tcgetattr(fd, argp); +#ifndef __MINGW32__ static sighandler_t previous_SIGWINCH_handler; #endif @@ -1770,46 +1752,6 @@ line_input_t *new_line_input_t(int flags) return n; } -#ifdef __MINGW32__ -#include -#include -#include "strbuf.h" - -#undef safe_read -static int wincon_read(int fd, char *buf, int size) -{ - static struct strbuf input = STRBUF_INIT; - HANDLE cin = GetStdHandle(STD_INPUT_HANDLE); - static int initialized = 0; - - if (fd != 0) - die("wincon_read is for stdin only"); - if (cin == INVALID_HANDLE_VALUE) - return safe_read(fd, buf, size); - if (!initialized) { - SetConsoleMode(cin, ENABLE_ECHO_INPUT); - initialized = 1; - } - while (input.len < size) { - INPUT_RECORD record; - DWORD nevent = 0, nevent_out; - int ch; - - if (!ReadConsoleInput(cin, &record, 1, &nevent_out)) - return -1; - if (record.EventType != KEY_EVENT || !record.Event.KeyEvent.bKeyDown) - continue; - ch = record.Event.KeyEvent.uChar.AsciiChar; - /* Ctrl-X is handled by ReadConsoleInput, Alt-X is not needed anyway */ - strbuf_addch(&input, ch); - } - memcpy(buf, input.buf, size); - memcpy(input.buf, input.buf+size, input.len-size+1); - strbuf_setlen(&input, input.len-size); - return size; -} -#endif - #else #undef read_line_input diff --git a/libbb/termios.c b/libbb/termios.c new file mode 100644 index 000000000..f47593a49 --- /dev/null +++ b/libbb/termios.c @@ -0,0 +1,73 @@ +#include "libbb.h" +#include +#include +#include "strbuf.h" +#include "cygwin_termios.h" + +int tcgetattr(int fd, struct termios *t) +{ + t->c_lflag = ECHO; + return 0; +} + + +int tcsetattr(int fd, int mode, const struct termios *t) +{ + return 0; +} + +static int get_wincon_width_height(const int fd, int *width, int *height) +{ + HANDLE console; + CONSOLE_SCREEN_BUFFER_INFO sbi; + + console = GetStdHandle(STD_OUTPUT_HANDLE); + if (console == INVALID_HANDLE_VALUE || !console) + return -1; + + GetConsoleScreenBufferInfo(console, &sbi); + + if (width) + *width = sbi.srWindow.Right - sbi.srWindow.Left; + if (height) + *height = sbi.srWindow.Bottom - sbi.srWindow.Top; + return 0; +} + +int get_terminal_width_height(const int fd, int *width, int *height) +{ + return get_wincon_width_height(fd, width, height); +} + +int wincon_read(int fd, char *buf, int size) +{ + static struct strbuf input = STRBUF_INIT; + HANDLE cin = GetStdHandle(STD_INPUT_HANDLE); + static int initialized = 0; + + if (fd != 0) + die("wincon_read is for stdin only"); + if (cin == INVALID_HANDLE_VALUE || is_cygwin_tty(fd)) + return safe_read(fd, buf, size); + if (!initialized) { + SetConsoleMode(cin, ENABLE_ECHO_INPUT); + initialized = 1; + } + while (input.len < size) { + INPUT_RECORD record; + DWORD nevent_out; + int ch; + + if (!ReadConsoleInput(cin, &record, 1, &nevent_out)) + return -1; + if (record.EventType != KEY_EVENT || !record.Event.KeyEvent.bKeyDown) + continue; + ch = record.Event.KeyEvent.uChar.AsciiChar; + /* Ctrl-X is handled by ReadConsoleInput, Alt-X is not needed anyway */ + strbuf_addch(&input, ch); + } + memcpy(buf, input.buf, size); + memcpy(input.buf, input.buf+size, input.len-size+1); + strbuf_setlen(&input, input.len-size); + return size; +} diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 06bba0fe3..3d0bec7ac 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -625,26 +625,7 @@ void selinux_or_die(void) /* It is perfectly ok to pass in a NULL for either width or for * height, in which case that value will not be set. */ -#ifdef __MINGW32__ -#include -int get_terminal_width_height(const int fd, int *width, int *height) -{ - HANDLE console; - CONSOLE_SCREEN_BUFFER_INFO sbi; - - console = GetStdHandle(STD_OUTPUT_HANDLE); - if (console == INVALID_HANDLE_VALUE || !console) - return -1; - - GetConsoleScreenBufferInfo(console, &sbi); - - if (width) - *width = sbi.srWindow.Right - sbi.srWindow.Left; - if (height) - *height = sbi.srWindow.Bottom - sbi.srWindow.Top; - return 0; -} -#else +#ifndef __MINGW32__ int get_terminal_width_height(const int fd, int *width, int *height) { struct winsize win = { 0, 0, 0, 0 }; -- cgit v1.2.3-55-g6feb