]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.termcap.c
Use POSIX tty semantics.
[bsdgames-darwin.git] / hack / hack.termcap.c
1 /*
2 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
3 */
4
5 #ifndef lint
6 static char rcsid[] = "$NetBSD: hack.termcap.c,v 1.4 1995/04/24 12:23:34 cgd Exp $";
7 #endif /* not lint */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include "config.h" /* for ROWNO and COLNO */
12 #include "def.flag.h" /* for flags.nonull */
13 extern char *tgetstr(), *tgoto(), *getenv();
14 extern long *alloc();
15
16 #ifndef lint
17 extern /* it is defined in libtermlib (libtermcap) */
18 #endif lint
19 short ospeed; /* terminal baudrate; used by tputs */
20 static char tbuf[512];
21 static char *HO, *CL, *CE, *UP, *CM, *ND, *XD, *BC, *SO, *SE, *TI, *TE;
22 static char *VS, *VE;
23 static int SG;
24 static char PC = '\0';
25 char *CD; /* tested in pri.c: docorner() */
26 int CO, LI; /* used in pri.c and whatis.c */
27
28 startup()
29 {
30 register char *term;
31 register char *tptr;
32 char *tbufptr, *pc;
33
34 tptr = (char *) alloc(1024);
35
36 tbufptr = tbuf;
37 if(!(term = getenv("TERM")))
38 error("Can't get TERM.");
39 if(!strncmp(term, "5620", 4))
40 flags.nonull = 1; /* this should be a termcap flag */
41 if(tgetent(tptr, term) < 1)
42 error("Unknown terminal type: %s.", term);
43 if(pc = tgetstr("pc", &tbufptr))
44 PC = *pc;
45 if(!(BC = tgetstr("bc", &tbufptr))) {
46 if(!tgetflag("bs"))
47 error("Terminal must backspace.");
48 BC = tbufptr;
49 tbufptr += 2;
50 *BC = '\b';
51 }
52 HO = tgetstr("ho", &tbufptr);
53 CO = tgetnum("co");
54 LI = tgetnum("li");
55 if(CO < COLNO || LI < ROWNO+2)
56 setclipped();
57 if(!(CL = tgetstr("cl", &tbufptr)))
58 error("Hack needs CL.");
59 ND = tgetstr("nd", &tbufptr);
60 if(tgetflag("os"))
61 error("Hack can't have OS.");
62 CE = tgetstr("ce", &tbufptr);
63 UP = tgetstr("up", &tbufptr);
64 /* It seems that xd is no longer supported, and we should use
65 a linefeed instead; unfortunately this requires resetting
66 CRMOD, and many output routines will have to be modified
67 slightly. Let's leave that till the next release. */
68 XD = tgetstr("xd", &tbufptr);
69 /* not: XD = tgetstr("do", &tbufptr); */
70 if(!(CM = tgetstr("cm", &tbufptr))) {
71 if(!UP && !HO)
72 error("Hack needs CM or UP or HO.");
73 printf("Playing hack on terminals without cm is suspect...\n");
74 getret();
75 }
76 SO = tgetstr("so", &tbufptr);
77 SE = tgetstr("se", &tbufptr);
78 SG = tgetnum("sg"); /* -1: not fnd; else # of spaces left by so */
79 if(!SO || !SE || (SG > 0)) SO = SE = 0;
80 CD = tgetstr("cd", &tbufptr);
81 set_whole_screen(); /* uses LI and CD */
82 if(tbufptr-tbuf > sizeof(tbuf)) error("TERMCAP entry too big...\n");
83 free(tptr);
84 }
85
86 start_screen()
87 {
88 xputs(TI);
89 xputs(VS);
90 }
91
92 end_screen()
93 {
94 xputs(VE);
95 xputs(TE);
96 }
97
98 /* Cursor movements */
99 extern xchar curx, cury;
100
101 curs(x, y)
102 register int x, y; /* not xchar: perhaps xchar is unsigned and
103 curx-x would be unsigned as well */
104 {
105
106 if (y == cury && x == curx)
107 return;
108 if(!ND && (curx != x || x <= 3)) { /* Extremely primitive */
109 cmov(x, y); /* bunker!wtm */
110 return;
111 }
112 if(abs(cury-y) <= 3 && abs(curx-x) <= 3)
113 nocmov(x, y);
114 else if((x <= 3 && abs(cury-y)<= 3) || (!CM && x<abs(curx-x))) {
115 (void) putchar('\r');
116 curx = 1;
117 nocmov(x, y);
118 } else if(!CM) {
119 nocmov(x, y);
120 } else
121 cmov(x, y);
122 }
123
124 nocmov(x, y)
125 {
126 if (cury > y) {
127 if(UP) {
128 while (cury > y) { /* Go up. */
129 xputs(UP);
130 cury--;
131 }
132 } else if(CM) {
133 cmov(x, y);
134 } else if(HO) {
135 home();
136 curs(x, y);
137 } /* else impossible("..."); */
138 } else if (cury < y) {
139 if(XD) {
140 while(cury < y) {
141 xputs(XD);
142 cury++;
143 }
144 } else if(CM) {
145 cmov(x, y);
146 } else {
147 while(cury < y) {
148 xputc('\n');
149 curx = 1;
150 cury++;
151 }
152 }
153 }
154 if (curx < x) { /* Go to the right. */
155 if(!ND) cmov(x, y); else /* bah */
156 /* should instead print what is there already */
157 while (curx < x) {
158 xputs(ND);
159 curx++;
160 }
161 } else if (curx > x) {
162 while (curx > x) { /* Go to the left. */
163 xputs(BC);
164 curx--;
165 }
166 }
167 }
168
169 cmov(x, y)
170 register x, y;
171 {
172 xputs(tgoto(CM, x-1, y-1));
173 cury = y;
174 curx = x;
175 }
176
177 xputc(c) char c; {
178 (void) fputc(c, stdout);
179 }
180
181 xputs(s) char *s; {
182 tputs(s, 1, xputc);
183 }
184
185 cl_end() {
186 if(CE)
187 xputs(CE);
188 else { /* no-CE fix - free after Harold Rynes */
189 /* this looks terrible, especially on a slow terminal
190 but is better than nothing */
191 register cx = curx, cy = cury;
192
193 while(curx < COLNO) {
194 xputc(' ');
195 curx++;
196 }
197 curs(cx, cy);
198 }
199 }
200
201 clear_screen() {
202 xputs(CL);
203 curx = cury = 1;
204 }
205
206 home()
207 {
208 if(HO)
209 xputs(HO);
210 else if(CM)
211 xputs(tgoto(CM, 0, 0));
212 else
213 curs(1, 1); /* using UP ... */
214 curx = cury = 1;
215 }
216
217 standoutbeg()
218 {
219 if(SO) xputs(SO);
220 }
221
222 standoutend()
223 {
224 if(SE) xputs(SE);
225 }
226
227 backsp()
228 {
229 xputs(BC);
230 curx--;
231 }
232
233 bell()
234 {
235 (void) putchar('\007'); /* curx does not change */
236 (void) fflush(stdout);
237 }
238
239 static short tmspc10[] = { /* from termcap */
240 0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5
241 };
242
243 delay_output() {
244 /* delay 50 ms - could also use a 'nap'-system call */
245 /* BUG: if the padding character is visible, as it is on the 5620
246 then this looks terrible. */
247 if(!flags.nonull)
248 tputs("50", 1, xputc);
249
250 /* cbosgd!cbcephus!pds for SYS V R2 */
251 /* is this terminfo, or what? */
252 /* tputs("$<50>", 1, xputc); */
253
254 else if(ospeed > 0 || ospeed < SIZE(tmspc10)) if(CM) {
255 /* delay by sending cm(here) an appropriate number of times */
256 register int cmlen = strlen(tgoto(CM, curx-1, cury-1));
257 register int i = 500 + tmspc10[ospeed]/2;
258
259 while(i > 0) {
260 cmov(curx, cury);
261 i -= cmlen*tmspc10[ospeed];
262 }
263 }
264 }
265
266 cl_eos() /* free after Robert Viduya */
267 { /* must only be called with curx = 1 */
268
269 if(CD)
270 xputs(CD);
271 else {
272 register int cx = curx, cy = cury;
273 while(cury <= LI-2) {
274 cl_end();
275 xputc('\n');
276 curx = 1;
277 cury++;
278 }
279 cl_end();
280 curs(cx, cy);
281 }
282 }