-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.c
162 lines (150 loc) · 2.75 KB
/
term.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
160
161
162
#include <stdbool.h>
#include <stdio.h>
#include <termios.h>
struct termios orig_tio;
void init_term(void) {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
struct termios tio;
tcgetattr(fileno(stdin), &tio);
orig_tio = tio;
cfmakeraw(&tio);
tcsetattr(fileno(stdin), TCSANOW, &tio);
printf("\e[H\e[J\e[29r\e[29;1H\e[s");
}
void fini_term(void) {
tcsetattr(fileno(stdin), TCSANOW, &orig_tio);
printf("\e[0m\e[1r\e[u");
}
int int_to_chars(int d, int * res) {
if (d == 0)
return 0;
if (d < 0)
d += 19683;
int pos = 0;
if (d < 0x80) {
res[pos++] = d;
return 1;
} else if (d < 0x800) {
res[pos++] = 0xc0 | d >> 6;
res[pos++] = 0x80 | d & 0x3f;
return 2;
} else {
res[pos++] = 0xe0 | d >> 12;
res[pos++] = 0x80 | d >> 6 & 0x3f;
res[pos++] = 0x80 | d & 0x3f;
return 3;
}
}
void hypercall_log(const char * buffer, int level) {
static const char * const loglevels[] = {
"\e[0;34m[DEBUG]",
"\e[0m[INFO]",
"\e[0;33m[WARNING]",
"\e[0;31m[ERROR]",
"\e[0;31;1m[FATAL]",
};
printf("\e[u%s %s\e[0m\r\n\e[s", loglevels[1 + level], buffer);
}
void termcall_beep(void) {
printf("\a");
}
void termcall_putc(int x, int y, int c, int fg, int bg, int bold) {
x += 40;
y += 13;
if (x < 0 || x > 80)
return;
if (y < 0 || y > 27)
return;
x++;
y++;
printf("\e[%d;%dH", y, x);
printf("\e[0m");
if (bold)
printf("\e[1m");
static const char *const fgs[] = {
"\e[30m",
"\e[31m",
"\e[32m",
"\e[33m",
"",
"\e[34m",
"\e[35m",
"\e[36m",
"\e[37m",
};
static const char *const bgs[] = {
"\e[40m",
"\e[41m",
"\e[42m",
"\e[43m",
"",
"\e[44m",
"\e[45m",
"\e[46m",
"\e[47m",
};
if (fg)
printf(fgs[4+fg]);
if (bg)
printf(bgs[4+bg]);
if (c < 0)
c += 9841;
if (c < 0x80)
putchar(c);
else if (c < 0x800) {
putchar(0xc0 | c >> 6);
putchar(0x80 | c & 0x3f);
} else {
putchar(0xe0 | c >> 12);
putchar(0x80 | c >> 6 & 0x3f);
putchar(0x80 | c & 0x3f);
}
}
int termcall_getc(int x, int y) {
x += 40;
y += 13;
if (x >= 0 && x < 81 && y >= 0 && y < 28) {
x++;
y++;
printf("\e[%d;%dH", y, x);
} else {
printf("\e[u");
}
int res;
while (1) {
int c = getchar();
if (c == EOF) {
return -1;
}
if (c < 0x80) {
res = c;
break;
} else if ((c & 0xe0) == 0xc0) {
int x1 = getchar();
if ((x1 & 0xc0) != 0x80) {
printf("\a");
continue;
}
res = (c & 0x1f) << 6 | (x1 & 0x3f);
break;
} else if ((c & 0xf0) == 0xe0) {
int x1 = getchar();
if ((x1 & 0xc0) != 0x80) {
printf("\a");
continue;
}
int x2 = getchar();
if ((x2 & 0xc0) != 0x80) {
printf("\a");
continue;
}
res = (c & 0xf) << 12 | (x1 & 0x3f) << 6 | (x2 & 0x3f);
break;
} else {
printf("\a");
continue;
}
}
return res % 19683;
}