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
|
/*-
* Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/ioctl.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ncurses.h>
#if defined(__FreeBSD__)
#include <libutil.h>
#elif defined(__linux__)
#include <pty.h>
#else
#include <util.h>
#endif
#include "teken.h"
static tf_bell_t test_bell;
static tf_cursor_t test_cursor;
static tf_putchar_t test_putchar;
static tf_fill_t test_fill;
static tf_copy_t test_copy;
static tf_param_t test_param;
static tf_respond_t test_respond;
static teken_funcs_t tf = {
.tf_bell = test_bell,
.tf_cursor = test_cursor,
.tf_putchar = test_putchar,
.tf_fill = test_fill,
.tf_copy = test_copy,
.tf_param = test_param,
.tf_respond = test_respond,
};
struct pixel {
teken_char_t c;
teken_attr_t a;
};
#define NCOLS 80
#define NROWS 25
struct pixel buffer[NCOLS][NROWS];
static int ptfd;
static void
printchar(const teken_pos_t *p)
{
int y, x, attr = 0;
struct pixel *px, pt;
assert(p->tp_row < NROWS);
assert(p->tp_col < NCOLS);
getyx(stdscr, y, x);
px = &buffer[p->tp_col][p->tp_row];
if (px->c >= 0x80) {
/* Mark UTF-8 chars (we don't support it). */
px = &pt;
px->c = '?';
px->a.ta_format = TF_BOLD;
px->a.ta_fgcolor = TC_BROWN;
px->a.ta_bgcolor = TC_RED;
}
if (px->a.ta_format & TF_BOLD)
attr |= A_BOLD;
if (px->a.ta_format & TF_UNDERLINE)
attr |= A_UNDERLINE;
if (px->a.ta_format & TF_BLINK)
attr |= A_BLINK;
bkgdset(attr | COLOR_PAIR(px->a.ta_fgcolor + 8 * px->a.ta_bgcolor + 1));
mvaddch(p->tp_row, p->tp_col, px->c);
move(y, x);
}
static void
test_bell(void *s __unused)
{
beep();
}
static void
test_cursor(void *s __unused, const teken_pos_t *p)
{
move(p->tp_row, p->tp_col);
}
static void
test_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c,
const teken_attr_t *a)
{
buffer[p->tp_col][p->tp_row].c = c;
buffer[p->tp_col][p->tp_row].a = *a;
printchar(p);
}
static void
test_fill(void *s, const teken_rect_t *r, teken_char_t c,
const teken_attr_t *a)
{
teken_pos_t p;
/* Braindead implementation of fill() - just call putchar(). */
for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++)
for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++)
test_putchar(s, &p, c, a);
}
static void
test_copy(void *s __unused, const teken_rect_t *r, const teken_pos_t *p)
{
int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
teken_pos_t d;
/*
* Copying is a little tricky. We must make sure we do it in
* correct order, to make sure we don't overwrite our own data.
*/
nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
if (p->tp_row < r->tr_begin.tp_row) {
/* Copy from top to bottom. */
if (p->tp_col < r->tr_begin.tp_col) {
/* Copy from left to right. */
for (y = 0; y < nrow; y++) {
d.tp_row = p->tp_row + y;
for (x = 0; x < ncol; x++) {
d.tp_col = p->tp_col + x;
buffer[d.tp_col][d.tp_row] =
buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
printchar(&d);
}
}
} else {
/* Copy from right to left. */
for (y = 0; y < nrow; y++) {
d.tp_row = p->tp_row + y;
for (x = ncol - 1; x >= 0; x--) {
d.tp_col = p->tp_col + x;
buffer[d.tp_col][d.tp_row] =
buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
printchar(&d);
}
}
}
} else {
/* Copy from bottom to top. */
if (p->tp_col < r->tr_begin.tp_col) {
/* Copy from left to right. */
for (y = nrow - 1; y >= 0; y--) {
d.tp_row = p->tp_row + y;
for (x = 0; x < ncol; x++) {
d.tp_col = p->tp_col + x;
buffer[d.tp_col][d.tp_row] =
buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
printchar(&d);
}
}
} else {
/* Copy from right to left. */
for (y = nrow - 1; y >= 0; y--) {
d.tp_row = p->tp_row + y;
for (x = ncol - 1; x >= 0; x--) {
d.tp_col = p->tp_col + x;
buffer[d.tp_col][d.tp_row] =
buffer[r->tr_begin.tp_col + x][r->tr_begin.tp_row + y];
printchar(&d);
}
}
}
}
}
static void
test_param(void *s __unused, int cmd, int value)
{
switch (cmd) {
case TP_SHOWCURSOR:
curs_set(value);
break;
case TP_KEYPADAPP:
keypad(stdscr, value ? TRUE : FALSE);
break;
}
}
static void
test_respond(void *s __unused, const void *buf, size_t len)
{
write(ptfd, buf, len);
}
static void
redraw_border(void)
{
unsigned int i;
for (i = 0; i < NROWS; i++)
mvaddch(i, NCOLS, '|');
for (i = 0; i < NCOLS; i++)
mvaddch(NROWS, i, '-');
mvaddch(NROWS, NCOLS, '+');
}
static void
redraw_all(void)
{
teken_pos_t tp;
for (tp.tp_row = 0; tp.tp_row < NROWS; tp.tp_row++)
for (tp.tp_col = 0; tp.tp_col < NCOLS; tp.tp_col++)
printchar(&tp);
redraw_border();
}
int
main(int argc __unused, char *argv[] __unused)
{
struct winsize ws;
teken_t t;
teken_pos_t tp;
fd_set rfds;
char b[256];
ssize_t bl;
const int ccolors[8] = { COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE };
int i, j;
tp.tp_row = ws.ws_row = NROWS;
tp.tp_col = ws.ws_col = NCOLS;
switch (forkpty(&ptfd, NULL, NULL, &ws)) {
case -1:
perror("forkpty");
exit(1);
case 0:
#ifdef TEKEN_XTERM
setenv("TERM", "xterm", 1);
#else /* !TEKEN_XTERM */
setenv("TERM", "cons25", 1);
#endif /* TEKEN_XTERM */
#ifdef TEKEN_UTF8
setenv("LC_CTYPE", "UTF-8", 0);
#endif /* TEKEN_UTF8 */
execlp("zsh", "-zsh", NULL);
execlp("bash", "-bash", NULL);
execlp("sh", "-sh", NULL);
_exit(1);
}
teken_init(&t, &tf, NULL);
teken_set_winsize(&t, &tp);
initscr();
raw();
start_color();
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
init_pair(i + 8 * j + 1, ccolors[i], ccolors[j]);
redraw_border();
FD_ZERO(&rfds);
for (;;) {
FD_SET(STDIN_FILENO, &rfds);
FD_SET(ptfd, &rfds);
if (select(ptfd + 1, &rfds, NULL, NULL, NULL) < 0) {
if (errno == EINTR) {
redraw_all();
refresh();
continue;
}
break;
}
if (FD_ISSET(STDIN_FILENO, &rfds)) {
bl = read(STDIN_FILENO, b, sizeof b);
if (bl <= 0)
break;
write(ptfd, b, bl);
}
if (FD_ISSET(ptfd, &rfds)) {
bl = read(ptfd, b, sizeof b);
if (bl <= 0)
break;
teken_input(&t, b, bl);
refresh();
}
}
endwin();
return (0);
}
|