-
Notifications
You must be signed in to change notification settings - Fork 3
/
ansiterm.c
319 lines (197 loc) · 3.61 KB
/
ansiterm.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* this is hackjob that translates the ANSI escape sequences used by larn
* to Curses API calls
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "ansiterm.h"
#define ANSITERM_ESC 27
static void ansiterm_command(int, const char *, const char *);
static void ansiterm_putchar(int);
/*
* writes to the terminal
*/
void ansiterm_out(const char *output_buffer, int n_chars)
{
int i;
i = 0;
while (i < n_chars) {
if (output_buffer[i] == ANSITERM_ESC) {
char ansi_param[10];
char param1[5];
char param2[5];
int ansi_cmd;
int j;
*param1 = '\0';
*param2 = '\0';
++i;
++i;
j = 0;
while (!isalpha(output_buffer[i])) {
ansi_param[j] = output_buffer[i];
++i;
++j;
}
ansi_param[j] = '\0';
ansi_cmd = output_buffer[i];
++i;
if (*ansi_param != '\0') {
char *p;
int k;
p = ansi_param;
k = 0;
while (*p != ';' && *p != '\0') {
param1[k] = *p;
++p;
++k;
}
param1[k] = '\0';
if (*p == ';') ++p;
k = 0;
while (*p != ';' && *p != '\0') {
param2[k] = *p;
++p;
++k;
}
param2[k] = '\0';
}
ansiterm_command(ansi_cmd, param1, param2);
} else {
ansiterm_putchar(output_buffer[i]);
++i;
}
}
/*
ESC[;H
ESC[y;xH ESC[24;01H
ESC[2J
ESC[1m standout on
ESC[m standout off
*/
}
/********************************************
* CURSES BACK-END *
********************************************/
#include <curses.h>
static int llgetch(void);
void ansiterm_init(void)
{
initscr();
notimeout(stdscr, 1);
noecho();
cbreak();
nonl();
keypad(stdscr, 1);
}
void ansiterm_clean_up(void)
{
nocbreak();
nl();
echo();
endwin();
}
/*
* get char
*/
int ansiterm_getch(void)
{
return llgetch();
}
/*
* get char (with echo)
*/
int ansiterm_getche(void)
{
int key;
echo();
key = llgetch();
noecho();
return key;
}
static int llgetch(void)
{
int key;
key = getch();
switch (key) {
case KEY_UP: return 'k';
case KEY_DOWN: return 'j';
case KEY_LEFT: return 'h';
case KEY_RIGHT: return 'l';
#if !defined(NCURSES_VERSION)
case KEY_A2: return 'k';
case KEY_B1: return 'h';
case KEY_B3: return 'l';
case KEY_C2: return 'j';
case PADENTER: return 13;
#endif
case KEY_A1: return 'y';
case KEY_A3: return 'u';
case KEY_C1: return 'b';
case KEY_C3: return 'n';
case KEY_ENTER: return 13;
default: return key;
}
}
static void ansiterm_command(int ansi_cmd, const char *param1,
const char *param2
)
{
if (ansi_cmd == 'H') {
int y, x;
if (*param1 == '\0') {
y = 0;
} else {
y = atoi(param1) - 1;
}
if (*param2 == '\0') {
x = 0;
} else {
x = atoi(param2) - 1;
}
move(y, x);
} else if (ansi_cmd == 'J') {
clear();
} else if (ansi_cmd == 'M') {
int i, n_lines;
if (*param1 != '\0') {
n_lines = 1;
} else {
n_lines = atoi(param1);
}
for (i = 0; i < n_lines; i++) {
move(0, 0);
clrtoeol();
}
} else if (ansi_cmd == 'K') {
clrtoeol();
} else if (ansi_cmd == 'm') {
int attribute;
if (*param1 == '\0') {
attribute = 0;
} else {
attribute = atoi(param1);
}
if (attribute == 0) {
attrset(A_NORMAL);
} else if (attribute == 1) {
attrset(A_BOLD);
}
} else {
exit(EXIT_FAILURE);
}
}
static void ansiterm_putchar(int c)
{
if (c == '\n') {
int y, x;
getyx(stdscr, y, x);
move(y + 1, 0);
return;
}
if (c == '\t') {
addstr(" ");
return;
}
addch(c);
}