aboutsummaryrefslogtreecommitdiff
path: root/libbb/termios.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/termios.c')
-rw-r--r--libbb/termios.c73
1 files changed, 73 insertions, 0 deletions
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 @@
1#include "libbb.h"
2#include <windef.h>
3#include <wincon.h>
4#include "strbuf.h"
5#include "cygwin_termios.h"
6
7int tcgetattr(int fd, struct termios *t)
8{
9 t->c_lflag = ECHO;
10 return 0;
11}
12
13
14int tcsetattr(int fd, int mode, const struct termios *t)
15{
16 return 0;
17}
18
19static int get_wincon_width_height(const int fd, int *width, int *height)
20{
21 HANDLE console;
22 CONSOLE_SCREEN_BUFFER_INFO sbi;
23
24 console = GetStdHandle(STD_OUTPUT_HANDLE);
25 if (console == INVALID_HANDLE_VALUE || !console)
26 return -1;
27
28 GetConsoleScreenBufferInfo(console, &sbi);
29
30 if (width)
31 *width = sbi.srWindow.Right - sbi.srWindow.Left;
32 if (height)
33 *height = sbi.srWindow.Bottom - sbi.srWindow.Top;
34 return 0;
35}
36
37int get_terminal_width_height(const int fd, int *width, int *height)
38{
39 return get_wincon_width_height(fd, width, height);
40}
41
42int wincon_read(int fd, char *buf, int size)
43{
44 static struct strbuf input = STRBUF_INIT;
45 HANDLE cin = GetStdHandle(STD_INPUT_HANDLE);
46 static int initialized = 0;
47
48 if (fd != 0)
49 die("wincon_read is for stdin only");
50 if (cin == INVALID_HANDLE_VALUE || is_cygwin_tty(fd))
51 return safe_read(fd, buf, size);
52 if (!initialized) {
53 SetConsoleMode(cin, ENABLE_ECHO_INPUT);
54 initialized = 1;
55 }
56 while (input.len < size) {
57 INPUT_RECORD record;
58 DWORD nevent_out;
59 int ch;
60
61 if (!ReadConsoleInput(cin, &record, 1, &nevent_out))
62 return -1;
63 if (record.EventType != KEY_EVENT || !record.Event.KeyEvent.bKeyDown)
64 continue;
65 ch = record.Event.KeyEvent.uChar.AsciiChar;
66 /* Ctrl-X is handled by ReadConsoleInput, Alt-X is not needed anyway */
67 strbuf_addch(&input, ch);
68 }
69 memcpy(buf, input.buf, size);
70 memcpy(input.buf, input.buf+size, input.len-size+1);
71 strbuf_setlen(&input, input.len-size);
72 return size;
73}