-
Notifications
You must be signed in to change notification settings - Fork 2
/
kraw.c
159 lines (145 loc) · 3.8 KB
/
kraw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
module : kraw.c
version : 1.5
date : 09/17/24
*/
#include "globals.h"
#ifdef _MSC_VER
static int size_x, size_y;
static DWORD input_mode, output_mode;
#else
static struct termios orig_mode;
static void die(const char *s)
{
printf("\033[2J\033[H"); /* assume VT100 escape codes are supported */
perror(s);
abort();
}
static int sizeScreen(int *rows, int *cols)
{
int num;
char buf[MAXNUM];
struct winsize ws;
ws.ws_row = ws.ws_col = 0;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 || !ws.ws_row) {
printf("\033[999C\033[999B\033[6n\r");
fflush(stdout);
if ((num = fread(buf, 1, sizeof(buf), stdin)) <= 0)
return -1;
if (buf[0] == '\033' && buf[1] == '[' && buf[num - 1] == 'R') {
sscanf(&buf[2], "%d;%d", rows, cols);
return 0;
}
}
*rows = ws.ws_row;
*cols = ws.ws_col;
return 0;
}
#endif
/*
* initScreen - make rows and cols available on the stack.
*/
static void initScreen(pEnv env)
{
Node node;
int rows, cols;
#ifdef _MSC_VER
rows = size_y;
cols = size_x;
#else
if (sizeScreen(&rows, &cols) < 0)
die("sizeScreen");
#endif
node.u.num = rows;
node.op = INTEGER_;
vec_push(env->stck, node);
node.u.num = cols;
vec_push(env->stck, node);
}
/*
* Undo the machinations of SetRaw.
*/
static void SetNormal(void)
{
#ifdef _MSC_VER
HANDLE input, output;
input = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(input, input_mode);
output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleMode(output, output_mode);
#else
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_mode) < 0)
die("tcsetattr");
#endif
}
/*
* Set the terminal in raw mode.
*
#define ENABLE_PROCESSED_INPUT 0x1
#define ENABLE_LINE_INPUT 0x2
#define ENABLE_ECHO_INPUT 0x4
#define ENABLE_WINDOW_INPUT 0x8
#define ENABLE_MOUSE_INPUT 0x10
#define ENABLE_INSERT_MODE 0x20
#define ENABLE_QUICK_EDIT_MODE 0x40
#define ENABLE_EXTENDED_FLAGS 0x80
#define ENABLE_AUTO_POSITION 0x100
#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x200
*
#define ENABLE_PROCESSED_OUTPUT 0x1
#define ENABLE_WRAP_AT_EOL_OUTPUT 0x2
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
#define DISABLE_NEWLINE_AUTO_RETURN 0x8
#define ENABLE_LVB_GRID_WORLDWIDE 0x10
*/
void SetRaw(pEnv env)
{
#ifdef _MSC_VER
DWORD mode;
HANDLE input, output;
CONSOLE_SCREEN_BUFFER_INFO info;
input = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(input, &input_mode);
mode = ENABLE_VIRTUAL_TERMINAL_INPUT;
SetConsoleMode(input, mode);
SetConsoleCP(65001);
output = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(output, &output_mode);
mode = ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT;
SetConsoleMode(output, mode);
SetConsoleOutputCP(65001);
GetConsoleScreenBufferInfo(output, &info);
size_x = info.srWindow.Right - info.srWindow.Left + 1;
size_y = info.srWindow.Bottom - info.srWindow.Top + 1;
#else
struct termios raw;
if (tcgetattr(STDIN_FILENO, &orig_mode) < 0)
die("tcgetattr");
raw = orig_mode;
/*
* control chars: set return condition: min number of bytes and timer.
*/
raw.c_cc[VMIN] = 1; /* return each byte. */
raw.c_cc[VTIME] = 0; /* no timeout. */
/*
* input modes: no break, no CR to NL, no parity check, no strip char,
* no start/stop output control.
*/
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*
* local modes: echoing off, canonical off, no extended functions,
* no signal chars (^Z,^C)
*/
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
#if 0
/* output modes: disable post processing */
raw.c_oflag &= ~OPOST;
#endif
/* control modes: set 8 bit chars */
raw.c_cflag |= CS8;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) < 0)
die("tcsetattr");
#endif
initScreen(env);
atexit(SetNormal);
}