summary refs log tree commit diff
path: root/term.c
blob: 5d7a9181421e6ef428b6179467941e6618ca535e (plain) (blame)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <wchar.h>

#include "stream.h"

enum {
	NUL, SOH, STX, ETX, EOT, ENQ, ACK, BEL,
	BS, HT, NL, VT, NP, CR, SO, SI,
	DLE, DC1, DC2, DC3, DC4, NAK, SYN, ETB,
	CAN, EM, SUB, ESC, FS, GS, RS, US,
	DEL = 0x7F,
};

static const struct Style Default = { .bg = -1, .fg = -1 };

static uint rows, cols;
static uint y, x;

static bool insert;
static struct {
	uint top, bot;
} scroll;

static struct Style style;
static struct Cell *cells;

static struct Cell *cell(uint y, uint x) {
	return &cells[y * cols + x];
}

static void clear(struct Cell *a, struct Cell *b) {
	for (; a <= b; ++a) {
		a->style = style;
		a->ch = ' ';
	}
}

static void move(struct Cell *dst, struct Cell *src, uint len) {
	memmove(dst, src, sizeof(*dst) * len);
}

static char updateNUL(wchar_t ch) {
	switch (ch) {
		break; case ESC: return ESC;

		break; case BS: if (x) x--;
		break; case CR: x = 0;

		break; case NL: {
			if (y == scroll.bot) {
				move(
					cell(scroll.top, 0), cell(scroll.top + 1, 0),
					cols * (scroll.bot - scroll.top)
				);
				clear(cell(scroll.bot, 0), cell(scroll.bot, cols - 1));
			} else {
				y = MIN(y + 1, rows - 1);
			}
		}

		break; default: {
			// FIXME: Nowhere for these warnings to go.
			if (ch < ' ') {
				warnx("unhandled \\x%02X", ch);
				return NUL;
			}

			int width = wcwidth(ch);
			if (width < 0) {
				warnx("unhandled \\u%X", ch);
				return NUL;
			}
			if (x + width > cols) {
				warnx("cannot fit '%lc'", ch);
				return NUL;
			}

			if (insert) {
				move(cell(y, x + width), cell(y, x), cols - x - width);
			}
			cell(y, x)->style = style;
			cell(y, x)->ch = ch;

			for (int i = 1; i < width; ++i) {
				cell(y, x + i)->style = style;
				cell(y, x + i)->ch = '\0';
			}
			x = MIN(x + width, cols - 1);
		}
	}
	return NUL;
}

enum {
	CSI = '[',
	OSC = ']',
	ST  = '\\',
};

static char updateESC(wchar_t ch) {
	static bool discard;
	if (discard) {
		discard = false;
		return NUL;
	}
	switch (ch) {
		case '(': discard = true; return ESC;
		case '=': return NUL;
		case '>': return NUL;
		case CSI: return CSI;
		case OSC: return OSC;
		// FIXME: Nowhere for this warning to go.
		default: warnx("unhandled ESC %lc", ch); return NUL;
	}
}

enum {
	Reset,
	Bold,
	Italic = 3,
	Underline,
	Reverse = 7,
	NotBold = 22,
	NotItalic,
	NotUnderline,
	NotReverse,
	Fg0 = 30,
	Fg7 = 37,
	Fg,
	FgDefault,
	Bg0,
	Bg7 = 47,
	Bg,
	BgDefault,
	Fg8 = 90,
	Fg15 = 97,
	Bg8 = 100,
	Bg15 = 107,
	Color256 = 5,
};

static void updateSGR(uint ps[], uint n) {
	for (uint i = 0; i < n; ++i) {
		switch (ps[i]) {
			break; case Reset: style = Default;

			break; case Bold:      style.bold = true;
			break; case Italic:    style.italic = true;
			break; case Underline: style.underline = true;
			break; case Reverse:   style.reverse = true;

			break; case NotBold:      style.bold = false;
			break; case NotItalic:    style.italic = false;
			break; case NotUnderline: style.underline = false;
			break; case NotReverse:   style.reverse = false;

			break; case Fg: {
				if (++i < n && ps[i] == Color256) {
					if (++i < n) style.fg = ps[i];
				}
			}
			break; case Bg: {
				if (++i < n && ps[i] == Color256) {
					if (++i < n) style.bg = ps[i];
				}
			}

			break; case FgDefault: style.fg = -1;
			break; case BgDefault: style.bg = -1;

			break; default: {
				if (ps[i] >= Fg0 && ps[i] <= Fg7) {
					style.fg = ps[i] - Fg0;
				} else if (ps[i] >= Bg0 && ps[i] <= Bg7) {
					style.bg = ps[i] - Bg0;
				} else if (ps[i] >= Fg8 && ps[i] <= Fg15) {
					style.fg = 8 + ps[i] - Fg8;
				} else if (ps[i] >= Bg8 && ps[i] <= Bg15) {
					style.bg = 8 + ps[i] - Bg8;
				}
			}
		}
	}
}

// SGR toggling all attributes and setting both colors in 256 palette.
enum { PSCap = 4 + 2 * 3 };

enum {
	DEC = '?',
	CUU = 'A',
	CUD,
	CUF,
	CUB,
	CNL,
	CPL,
	CHA,
	CUP,
	ED  = 'J',
	EL,
	DL  = 'M',
	DCH = 'P',
	ECH = 'X',
	VPA = 'd',
	SM  = 'h',
	RM  = 'l',
	SGR,
	DECSTBM = 'r',
};

enum {
	Insert = 4,
};

static char updateCSI(wchar_t ch) {
	static bool dec;
	if (ch == DEC) {
		dec = true;
		return CSI;
	}

	static uint n, p, ps[PSCap];
	if (ch == ';') {
		n++;
		p++;
		ps[p %= PSCap] = 0;
		return CSI;
	}
	if (ch >= '0' && ch <= '9') {
		ps[p] *= 10;
		ps[p] += ch - '0';
		if (!n) n++;
		return CSI;
	}

	switch (ch) {
		break; case CUU: y -= MIN((n ? ps[0] : 1), y);
		break; case CUD: y  = MIN(y + (n ? ps[0] : 1), rows - 1);
		break; case CUF: x  = MIN(x + (n ? ps[0] : 1), cols - 1);
		break; case CUB: x -= MIN((n ? ps[0] : 1), x);
		break; case CNL: y  = MIN(y + (n ? ps[0] : 1), rows - 1); x = 0;
		break; case CPL: y -= MIN((n ? ps[0] : 1), y); x = 0;
		break; case CHA: x  = MIN((n ? ps[0] - 1 : 0), cols - 1);
		break; case VPA: y  = MIN((n ? ps[0] - 1 : 0), rows - 1);
		break; case CUP: {
			y = MIN((n > 0 ? ps[0] - 1 : 0), rows - 1);
			x = MIN((n > 1 ? ps[1] - 1 : 0), cols - 1);
		}

		break; case ED: {
			struct Cell *a = cell(0, 0);
			struct Cell *b = cell(rows - 1, cols - 1);
			if (ps[0] == 0) a = cell(y, x);
			if (ps[0] == 1) b = cell(y, x);
			clear(a, b);
		}
		break; case EL: {
			struct Cell *a = cell(y, 0);
			struct Cell *b = cell(y, cols - 1);
			if (ps[0] == 0) a = cell(y, x);
			if (ps[0] == 1) b = cell(y, x);
			clear(a, b);
		}
		break; case ECH: {
			struct Cell *a = cell(y, x);
			struct Cell *b = cell(y, MIN(x + (n > 0 ? ps[0] - 1 : 0), cols - 1));
			clear(a, b);
		}

		break; case DL: {
			uint i = MIN((n ? ps[0] : 1), rows - y);
			move(cell(y, 0), cell(y + i, 0), cols * (rows - y - i));
			clear(cell(rows - i, 0), cell(rows - 1, cols - 1));
		}
		break; case DCH: {
			uint i = MIN((n ? ps[0] : 1), cols - x);
			move(cell(y, x), cell(y, x + i), cols - x - i);
			clear(cell(y, cols - i), cell(y, cols - 1));
		}

		// FIXME: Nowhere for these warnings to go.
		break; case SM: {
			if (dec) break;
			switch (ps[0]) {
				break; case Insert: insert = true;
				break; default: warnx("unhandled SM %u", ps[0]);
			}
		}
		break; case RM: {
			if (dec) break;
			switch (ps[0]) {
				break; case Insert: insert = false;
				break; default: warnx("unhandled RM %u", ps[0]);
			}
		}

		break; case SGR: updateSGR(ps, p + 1);

		break; case DECSTBM: {
			scroll.top = (n > 0 ? ps[0] - 1 : 0);
			scroll.bot = (n > 1 ? ps[1] - 1 : rows - 1);
		}

		break; case 't': // ignore
		// FIXME: Nowhere for this warning to go.
		break; default: warnx("unhandled CSI %lc", ch);
	}

	dec = false;
	ps[n = p = 0] = 0;
	return NUL;
}

static char updateOSC(wchar_t ch) {
	static bool esc;
	switch (ch) {
		break; case BEL: return NUL;
		break; case ESC: esc = true;
		break; case ST: {
			if (!esc) break;
			esc = false;
			return NUL;
		}
	}
	return OSC;
}

void termUpdate(wchar_t ch) {
	static char seq;
	switch (seq) {
		break; case NUL: seq = updateNUL(ch);
		break; case ESC: seq = updateESC(ch);
		break; case CSI: seq = updateCSI(ch);
		break; case OSC: seq = updateOSC(ch);
	}
}

void termInit(uint _rows, uint _cols) {
	rows = _rows;
	cols = _cols;
	cells = calloc(rows * cols, sizeof(*cells));
	if (!cells) err(EX_OSERR, "calloc");

	style = Default;
	clear(cell(0, 0), cell(rows - 1, cols - 1));
	scroll.bot = rows - 1;
}

static int
styleDiff(FILE *file, const struct Style *prev, const struct Style *next) {
	if (!memcmp(prev, next, sizeof(*prev))) return 0;
	if (!memcmp(next, &Default, sizeof(*next))) {
		return fprintf(file, "%c%c%c", ESC, CSI, SGR);
	}

	uint ps[PSCap];
	uint n = 0;
	if (next->bold != prev->bold) {
		ps[n++] = (next->bold ? Bold : NotBold);
	}
	if (next->italic != prev->italic) {
		ps[n++] = (next->italic ? Italic : NotItalic);
	}
	if (next->underline != prev->underline) {
		ps[n++] = (next->underline ? Underline : NotUnderline);
	}
	if (next->reverse != prev->reverse) {
		ps[n++] = (next->reverse ? Reverse : NotReverse);
	}
	if (next->bg != prev->bg) {
		if (next->bg == -1) {
			ps[n++] = BgDefault;
		} else if (next->bg < 8) {
			ps[n++] = Bg0 + next->bg;
		} else if (next->bg < 16) {
			ps[n++] = Bg8 + next->bg - 8;
		} else {
			ps[n++] = Bg;
			ps[n++] = Color256;
			ps[n++] = next->bg;
		}
	}
	if (next->fg != prev->fg) {
		if (next->fg == -1) {
			ps[n++] = FgDefault;
		} else if (next->fg < 8) {
			ps[n++] = Fg0 + next->fg;
		} else if (next->fg < 16) {
			ps[n++] = Fg8 + next->fg - 8;
		} else {
			ps[n++] = Fg;
			ps[n++] = Color256;
			ps[n++] = next->fg;
		}
	}

	if (0 > fprintf(file, "%c%c%u", ESC, CSI, ps[0])) return -1;
	for (uint i = 1; i < n; ++i) {
		if (0 > fprintf(file, ";%u", ps[i])) return -1;
	}
	return fprintf(file, "%c", SGR);
}

int termSnapshot(int _fd) {
	int fd = dup(_fd);
	if (fd < 0) return -1;

	FILE *file = fdopen(fd, "w");
	if (!file) return -1;

	struct Style prev = Default;
	for (uint y = 0; y < rows; ++y) {
		if (y && 0 > fprintf(file, "\r\n")) goto fail;
		for (uint x = 0; x < cols; ++x) {
			if (!cell(y, x)->ch) continue;
			if (0 > styleDiff(file, &prev, &cell(y, x)->style)) goto fail;
			if (0 > fprintf(file, "%lc", cell(y, x)->ch)) goto fail;
			prev = cell(y, x)->style;
		}
	}
	if (0 > styleDiff(file, &prev, &style)) goto fail;

	int n = fprintf(
		file,
		"%c%c%d%c"
		"%c%c%u;%u%c"
		"%c%c%u;%u%c",
		ESC, CSI, Insert, (insert ? SM : RM),
		ESC, CSI, scroll.top, scroll.bot, DECSTBM,
		ESC, CSI, y, x, CUP
	);
	if (n < 0) goto fail;

	return fclose(file);

fail:
	fclose(file);
	return -1;
}

struct Display termDisplay(void) {
	return (struct Display) {
		.rows = rows,
		.cols = cols,
		.y = y,
		.x = x,
		.cells = cells,
	};
}