-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvt100utils.h
More file actions
381 lines (339 loc) · 7.73 KB
/
Copy pathvt100utils.h
File metadata and controls
381 lines (339 loc) · 7.73 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* vt100utils.h: An ANSI graphics escape sequence encoder/decoder
*/
#ifndef __VT100UTILS_H
#define __VT100UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
/**
* PREPROCESSOR
*/
#define MAX(a, b) (a > b ? a : b)
/**
* STRUCTS and GLOBALS
*/
struct vt100_color_t {
enum {
palette_8,
palette_8_bright,
palette_256,
truecolor
} type;
uint32_t value;
};
struct vt100_node_t {
char *str;
int len;
struct vt100_color_t fg;
struct vt100_color_t bg;
uint8_t mode;
struct vt100_node_t *next;
};
static struct vt100_color_t
default_fg = { palette_8, 7 },
default_bg = { palette_8, 0 };
static struct vt100_color_t
global_fg = { palette_8, 7 },
global_bg = { palette_8, 0 };
static uint8_t global_mode;
static char *empty_str = "";
/**
* LIBRARY FUNCTIONS
*/
/*
* vt100_sgr: Generate an escape sequence
* representing the given node's graphics
* data
*/
char *
vt100_sgr(struct vt100_node_t *node, struct vt100_node_t *prev)
{
char *buf = malloc(128);
int len = sprintf(buf, "\x1b[");
int i;
if (!prev ||
prev->fg.type != node->fg.type ||
prev->fg.value != node->fg.value
) {
switch (node->fg.type) {
case palette_8:
len += sprintf(
buf + len,
"%i;",
node->fg.value + 30
);
break;
case palette_8_bright:
len += sprintf(
buf + len,
"%i;",
node->fg.value + 90
);
break;
case palette_256:
len += sprintf(
buf + len,
"38;5;%i;",
node->fg.value
);
break;
case truecolor:
len += sprintf(
buf + len,
"38;2;%i;%i;%i;",
(node->fg.value >> 16) & 0xff,
(node->fg.value >> 8) & 0xff,
(node->fg.value >> 0) & 0xff
);
break;
}
}
if (!prev ||
prev->bg.type != node->bg.type ||
prev->bg.value != node->bg.value
) {
switch (node->bg.type) {
case palette_8:
len += sprintf(
buf + len,
"%i",
node->bg.value + 40
);
break;
case palette_8_bright:
len += sprintf(
buf + len,
"%i",
node->bg.value + 100
);
break;
case palette_256:
len += sprintf(
buf + len,
"48;5;%i",
node->bg.value
);
break;
case truecolor:
len += sprintf(
buf + len,
"48;2;%i;%i;%i",
(node->bg.value >> 16) & 0xff,
(node->bg.value >> 8) & 0xff,
(node->bg.value >> 0) & 0xff
);
break;
}
}
#ifndef VT100UTILS_SKIP_FORMATTING
if (!prev || prev->mode != node->mode) {
for (i = 0; i < 8; i++) {
len += sprintf(
buf + len,
";%i",
i + 1
+ (20 * ((node->mode & (1 << i)) == 0)) /* Disable format */
+ (1 * (i == 0 && (node->mode & 1) == 0)) /* \x1b[21m is nonstandard */
);
}
}
#endif
sprintf(buf + len, "m");
return buf;
}
/*
* vt100_encode: Encode a chain of nodes as
* a continuous string, for printing to
* the terminal
*/
char *
vt100_encode(struct vt100_node_t *node)
{
int len = 0;
int size;
char *out = malloc((size = MAX(node->len, 32)));
char *buf;
struct vt100_node_t *tmp = node,
*prev = NULL;
while (tmp != NULL) {
while (len + tmp->len + 64 > size) {
out = realloc(out, (size *= 2));
}
buf = vt100_sgr(tmp, prev);
len += sprintf(
out + len,
"%s%s",
buf,
tmp->str
);
free(buf);
prev = tmp;
tmp = tmp->next;
}
return out;
}
/*
* vt100_parse: Parses a string beginning with
* "\x1b[" as a graphics/SGR escape sequence
*/
char *
vt100_parse(struct vt100_node_t *node, char *str)
{
char *start = str + 2;
char *end = start;
int args[256];
int i = 0;
struct vt100_color_t *to_modify;
int j, state = 0;
node->fg = global_fg;
node->bg = global_bg;
node->mode = global_mode;
if (str[0] != '\x1b' || str[1] != '[')
goto abort;
for (;;) {
switch (*end) {
case 'm':
args[i++] = atoi(start);
for (j = 0; j < i; j++) {
switch (state) {
case 0:
switch (args[j]) {
/* Reset */
case 0:
node->fg = default_fg;
node->bg = default_bg;
node->mode = 0;
break;
/* Formatting (bold, dim, italic, etc.) */
case 1 ... 9: /* GCC case range extension (supported by Clang) */
node->mode |= (1 << (args[j] - 1));
break;
/* 8-color standard palette */
case 30 ... 37:
node->fg.type = palette_8;
node->fg.value = args[j] - 30;
break;
case 40 ... 47:
node->bg.type = palette_8;
node->bg.value = args[j] - 40;
break;
/* 8-color bright palette */
case 90 ... 97:
node->fg.type = palette_8_bright;
node->fg.value = args[j] - 90;
break;
case 100 ... 107:
node->bg.type = palette_8_bright;
node->bg.value = args[j] - 100;
break;
default:
state = args[j];
break;
}
break;
case 38:
case 48:
if (state == 38)
to_modify = &(node->fg);
else
to_modify = &(node->bg);
if (args[j] == 5) {
/* 256-color palette */
if (j + 1 >= i || args[j + 1] < 0 || args[j + 1] > 255)
goto abort;
to_modify->type = palette_256;
to_modify->value = args[++j];
} else if (args[j] == 2) {
/* Truecolor */
if (j + 3 >= i)
goto abort;
to_modify->type = truecolor;
to_modify->value = args[++j];
to_modify->value <<= 8;
to_modify->value |= args[++j];
to_modify->value <<= 8;
to_modify->value |= args[++j];
} else {
goto abort;
}
state = 0;
break;
}
}
global_fg = node->fg;
global_bg = node->bg;
global_mode = node->mode;
return end + 1;
case ';':
args[i++] = atoi(start);
start = ++end;
break;
default:
if (*end >= '0' && *end <= '9')
end++;
else
goto abort;
break;
}
}
abort:;
node->fg = global_fg;
node->bg = global_bg;
node->mode = global_mode;
return str + 1;
}
/*
* vt100_decode: Decodes an input string
* into a chain of nodes
*/
struct vt100_node_t *
vt100_decode(char *str)
{
struct vt100_node_t *head = malloc(sizeof(struct vt100_node_t)),
*cur = head;
char *start = str;
char *end = str;
cur->str = empty_str;
cur->fg = global_fg;
cur->bg = global_bg;
for (;;) {
switch (*end) {
case '\0': /* Fall through */
case '\x1b':
if (end != start) {
cur->str = malloc(end - start + 1);
strncpy(cur->str, start, end - start);
cur->str[end - start] = '\0';
cur->len = end - start + 1;
}
if (*end == '\0')
return head;
cur->next = malloc(sizeof(struct vt100_node_t));
cur = cur->next;
cur->next = NULL;
start = vt100_parse(cur, end);
/* Fall through */
default:
end++;
break;
}
}
}
void
vt100_free(struct vt100_node_t *head)
{
struct vt100_node_t *tmp = head->next,
*prev = head;
while (tmp != NULL) {
if (prev->str != empty_str && prev->str != NULL)
free(prev->str);
free(prev);
prev = tmp;
tmp = tmp->next;
}
free(prev->str);
free(prev);
}
#endif