/
tcap.c
273 lines (233 loc) · 5.34 KB
/
tcap.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
/*
* Conway's game of life.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*
* tcap.c:
* terminal capabilities
*
*/
#include <curses.h>
#include <stdio.h>
#include <term.h>
#include <stdlib.h>
#include "tcap.h"
#include "cgol.h"
#include "defs.h"
#include "func_defs.h"
#define MARGIN 8
#define SCRSIZ 64
#define NPAUSE 10 /* # times thru update to pause. */
#define BEL 0x07
#define ESC 0x1B
static void tcapkopen(void);
static void tcapkclose(void);
static void tcapmove(int, int);
static void tcapeeol(void);
static void tcapeeop(void);
static void tcapbeep(void);
static void tcaprev(int);
static int tcapcres(char *);
static void tcapscrollregion(int top, int bot);
static void putpad(char *str);
static void tcapopen(void);
static void tcapclose(void);
static void tcapfcol(void);
static void tcapbcol(void);
static void tcapscroll_reg(int from, int to, int linestoscroll);
static void tcapscroll_delins(int from, int to, int linestoscroll);
#define TCAPSLEN 315
static char tcapbuf[TCAPSLEN];
static char *UP, PC, *CM, *CE, *CL, *SO, *SE;
static char *TI, *TE;
static char *CS, *DL, *AL, *SF, *SR;
struct terminal term = {
0,
0,
0,
0,
MARGIN,
SCRSIZ,
NPAUSE,
tcapopen,
tcapclose,
tcapkopen,
tcapkclose,
tiogetc,
tioputc,
tioflush,
tcapmove,
tcapeeol,
tcapeeop,
tcapbeep,
tcaprev,
tcapcres,
tcapfcol,
tcapbcol,
NULL
};
static void tcapopen(void)
{
char *t, *p;
char tcbuf[1024];
char *tv_stype;
char err_str[72];
int int_col, int_row;
if ((tv_stype = getenv("TERM")) == NULL) {
puts("Environment variable TERM not defined!");
exit(EXIT_FAILURE);
}
if ((tgetent(tcbuf, tv_stype)) !=1) {
sprintf(err_str, "Unknown terminal type %s!", tv_stype);
puts(err_str);
exit(EXIT_FAILURE);
}
/* Get screen size */
getscreensize(&int_col, &int_row);
term.t_nrow = int_row -1;
term.t_ncol = int_col;
if ((term.t_nrow <= 0)
&& (term.t_nrow = (short) tgetnum("li") == -1)) {
puts("termcap entry incomplete (lines)");
exit(EXIT_FAILURE);
}
if ((term.t_ncol <= 0)
&& (term.t_ncol = (short) tgetnum("co") == -1)) {
puts("termcap entry incomplete (columns)");
exit(EXIT_FAILURE);
}
term.t_mrow = term.t_nrow > MAXROW ? MAXROW : term.t_nrow;
term.t_mcol = term.t_ncol > MAXCOL ? MAXCOL : term.t_ncol;
p = tcapbuf;
t = tgetstr("pc", &p);
if (t)
PC = *t;
else
PC = 0;
CL = tgetstr("cl", &p);
CM = tgetstr("cm", &p);
CE = tgetstr("ce", &p);
if (CE != NULL)
eolexists = TRUE;
UP = tgetstr("up", &p);
SE = tgetstr("se", &p);
SO = tgetstr("so", &p);
if (SO != NULL)
revexists = TRUE;
tioinit();
}
static void tcapclose(void)
{
putpad(tgoto(CM, 0, term.t_nrow));
putpad(TE);
tioflush();
tiorestore();
}
static void tcapkopen(void)
{
putpad(TI);
tioflush();
ttrow = 999;
ttcol = 999;
sgarbf = TRUE;
}
static void tcapkclose(void)
{
putpad(TE);
tioflush();
}
static void tcapmove(int row, int col)
{
putpad(tgoto(CM, col, row));
}
static void tcapeeol(void)
{
putpad(CE);
}
static void tcapeeop(void)
{
putpad(CL);
}
static void tcaprev(int state)
{
if (state) {
if (SO != NULL)
putpad(SO);
} else if (SE != NULL)
putpad(SE);
}
static int tcapcres(char *res)
{
return TRUE;
}
static void tcapscroll_reg(int from, int to, int numlines)
{
int i;
if (to == from)
return;
if (to < from) {
tcapscrollregion(to, from + numlines - 1);
tcapmove(from + numlines - 1, 0);
for (i = from - to; i > 0; i--)
putpad(SF);
} else { /* from < to */
tcapscrollregion(from, to + numlines - 1);
tcapmove(from, 0);
for (i = to - from; i > 0; i--)
putpad(SR);
}
tcapscrollregion(0, term.t_nrow);
}
static void tcapscroll_delins(int from, int to, int numlines)
{
int i;
if (to == from)
return;
if (to < from) {
tcapmove(to, 0);
for (i = from - to; i > 0; i--)
putpad(DL);
tcapmove(to + numlines, 0);
for (i = from - to; i > 0; i--)
putpad(AL);
} else {
tcapmove(from + numlines, 0);
for (i = to - from; i > 0; i--)
putpad(DL);
tcapmove(from, 0);
for (i = to - from; i > 0; i--)
putpad(AL);
}
}
static void tcapscrollregion(int top, int bot)
{
tioputc(PC);
putpad(tgoto(CS, bot, top));
}
static void tcapfcol(void)
{
}
static void tcapbcol(void)
{
}
static void tcapbeep(void)
{
tioputc(BEL);
}
static void putpad(char *str)
{
tputs(str, 1, tioputc);
}