aboutsummaryrefslogtreecommitdiff
path: root/tea.c
blob: db1239d349c791c3270d4960a3f43d9b67ff5ed5 (plain)
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
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/ttydefaults.h>
#include <termios.h>
#include <unistd.h>

#define ESC "\033"
#define CSI ESC "["

#define prn(...) dprintf(ttyfd, __VA_ARGS__)
#define rerr(...) do { raw(false); err(__VA_ARGS__); } while (0)

char **lines;
int lines_c;
int lines_s;

char *src; /* source text */
int src_l;
int src_s;

int *breaks; /* positions of visual line breaks in source text */
             /* i.e. breaks[src index] = 1 if line broken before, else 0 */

int x, y; /* current cursor position */
int w, h; /* terminal width, height */
int margin = 10;

int current = -1; /* currently selected item */
int xorig, yorig; /* original cursor position in cols, rows */
int ttyfd;

/* enable/disable "raw" mode */
void raw(bool enable) {
	int r;
	static struct termios orig;
	struct termios raw;

	if (enable) {
		r = tcgetattr(ttyfd, &orig);
		if (r == -1) err(1, "tcgetattr");

		raw = orig;
		raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
		raw.c_oflag &= ~OPOST;
		raw.c_cflag |= CS8;
		raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);

		r = tcsetattr(ttyfd, TCSAFLUSH, &raw);
		if (r == -1) err(1, "tcsetattr");

	} else
		tcsetattr(ttyfd, TCSAFLUSH, &orig);
}

/* cursor position report */
int cpr(int *x, int *y) {
	char c, xbuf[4], ybuf[4];
	int i;

	prn(CSI "6n");

	/* get CSI */
	read(ttyfd, &c, 1);
	if (c != '\033') return -1;
	read(ttyfd, &c, 1);
	if (c != '[') return -1;

	/* get x */
	i = 0;
	while (read(ttyfd, &c, 1) && c != ';') {
		if (i > 3 || !isdigit(c)) return -1;
		ybuf[i++] = c;
	}
	if (i == 0) return -1;
	ybuf[i] = '\0';

	/* get y */
	i = 0;
	while (read(ttyfd, &c, 1) && c != 'R') {
		if (i > 3 || !isdigit(c)) return -1;
		xbuf[i++] = c;
	}
	if (i == 0) return -1;
	xbuf[i] = '\0';

	*x = atoi(xbuf);
	*y = atoi(ybuf);

	return 0;
}

int up() { }
int down() { }
int right() {}
int left() {}

void breakline() {
	prn("\r\n");
	breaks[src_l + 1] = 1;
	x = margin;
	if (y == h) yorig--;
	else y++;
	prn(CSI "%d;%dH", y, margin);
}

void addc(char c) {
	char *tmp;
	int i, *tmp2;

	if (src_l + 2 > src_s) {
		src_s += 50;
		tmp = realloc(src, (src_s + 1) * sizeof(char));
		if (tmp == NULL) rerr(1, "realloc");
		src = tmp;

		tmp2 = realloc(breaks, src_s * sizeof(int));
		if (breaks == NULL) err(1, "malloc");
		breaks = tmp2;
		for (i = src_s - 50; i < src_s; i++)
			breaks[i] = 0;
	}
	src[++src_l] = c;
	src[src_l + 1] = '\0';
}

void delc() {
	src[src_l--] = '\0';
}

int main() {
	bool dot;
	char c, *line, *p, *tmp;
	int i, line_s, r;
	struct winsize ws;

	src_s = 100;
	src = malloc((src_s + 1) * sizeof(char));
	if (src == NULL) err(1, "malloc");
	src_l = -1;

	breaks = malloc(src_s * sizeof(int));
	if (breaks == NULL) err(1, "malloc");
	for (i = 0; i < src_s; i++)
		breaks[i] = 0;

	ttyfd = open("/dev/tty", O_RDWR);
	if (ttyfd == -1) rerr(1, "open");

	/* read file */

/*
	line_s = 100;
	line = malloc((line_s + 1) * sizeof(char));
	if (line == NULL) err(1, "malloc");

	lines_c = 0;
	lines_s = 50;
	lines = malloc(lines_s * sizeof(char *));
	if (lines == NULL) err(1, "malloc");

	i = 0;
	while (read(STDIN_FILENO, &c, 1) != 0) {
		if (c == '\n') {
			line[i] = '\0';
			lines[lines_c] = malloc((strlen(line) + 1) * sizeof(char));
			if (lines[lines_c] == NULL) err(1, "malloc");
			strcpy(lines[lines_c], line);
			lines_c++;
			i = 0;
		} else {
			if (i > line_s) {
				line_s += 50;
				tmp = realloc(line, (line_s + 1) * sizeof(char));
				if (tmp == NULL) err(1, "realloc");
				line = tmp;
			}
			line[i++] = c;
		}
	}
*/

	raw(true);

	/* save original cursor position */
	r = cpr(&xorig, &yorig);
	if (r == -1) {
		fprintf(stderr, "could not retrieve cursor position");
		goto quit;
	}

	/* print output */
	for (i = 0; i < lines_c; i++)
		prn("%s\r\n", lines[i]);

	/* get height of terminal */
	r = ioctl(ttyfd, TIOCGWINSZ, &ws);
	if (r == -1) rerr(1, "ioctl");
	w = ws.ws_col;
	h = ws.ws_row;

	/* correct cursor position if original cursor was near bottom */
	r = y + lines_c - h;
	if (r > 0)
		y = y - r;

	/* restore original cursor position (CUP) */
	prn(CSI "%d;%dH", yorig, margin);
	x = margin;
	y = yorig;

	dot = false;
	while (read(ttyfd, &c, 1) != 0) {
		switch (c) {
		case '\033': /* escape */
			read(ttyfd, &c, 1);
			if (c != '[') break;
			read(ttyfd, &c, 1);
			if (c == 'A') up();
			if (c == 'B') down();
			if (c == 'C') right();
			if (c == 'D') left();
			break;
		case 3: /* ctrl-c */
			goto quit;
			break;
		case 13: /* enter */
			addc('\n');
			breakline();
			prn(CSI "0m");
			break;
		case 127: /* backspace */
			if (x == margin || x == 1) break;
			prn("\b");
			delc();
			x--;
			break;
		default:
			if (iscntrl(c)) break;
			if (x == margin && c == '.') {
				prn(CSI "%dD", margin - 1);
				prn(CSI "2m");
			}
			addc(c);
			if (x + 1 > w) breakline();
			prn("%c", c);
			x++;
			break;
		}
	}
quit:
	prn(CSI "%d;%dH", yorig, xorig);
	prn(CSI "J"); /* delete from cursor to end of display (ED) */
	raw(false);
	printf("%s", src);
	return 0;
die:
	raw(false);
	puts("died");
	return 0;
}