]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.termcap.c
don't use char values for functions that can return -1; chars are not always
[bsdgames-darwin.git] / hack / hack.termcap.c
1 /* $NetBSD: hack.termcap.c,v 1.17 2009/06/07 18:30:39 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5 * Amsterdam
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * - Neither the name of the Stichting Centrum voor Wiskunde en
20 * Informatica, nor the names of its contributors may be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /*
38 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39 * All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
55 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 #include <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.termcap.c,v 1.17 2009/06/07 18:30:39 dholland Exp $");
67 #endif /* not lint */
68
69 #include <string.h>
70 #include <termios.h>
71 #include <termcap.h>
72 #include <stdlib.h>
73 #include <unistd.h>
74 #include "hack.h"
75 #include "extern.h"
76 #include "def.flag.h" /* for flags.nonull */
77
78 static struct tinfo *info;
79 static const char *HO, *CL, *CE, *CM, *ND, *XD, *BC_BS, *SO, *SE, *TI, *TE;
80 static const char *VS, *VE;
81 static int SG;
82 char *CD; /* tested in pri.c: docorner() */
83 int CO, LI; /* used in pri.c and whatis.c */
84
85 void
86 startup(void)
87 {
88 char *term;
89
90 /* UP, BC, PC already set */
91 if (!(term = getenv("TERM")))
92 error("Can't get TERM.");
93 if (!strncmp(term, "5620", 4))
94 flags.nonull = 1; /* this should be a termcap flag */
95 if (t_getent(&info, term) < 1)
96 error("Unknown terminal type: %s.", term);
97 BC_BS = t_agetstr(info, "bc");
98 if (!BC_BS) {
99 if (!t_getflag(info, "bs"))
100 error("Terminal must backspace.");
101 BC_BS = "\b";
102 }
103 HO = t_agetstr(info, "ho");
104 CO = t_getnum(info, "co");
105 LI = t_getnum(info, "li");
106 if (CO < COLNO || LI < ROWNO + 2)
107 setclipped();
108 if (!(CL = t_agetstr(info, "cl")))
109 error("Hack needs CL.");
110 ND = t_agetstr(info, "nd");
111 if (t_getflag(info, "os"))
112 error("Hack can't have OS.");
113 CE = t_agetstr(info, "ce");
114 /*
115 * It seems that xd is no longer supported, and we should use a
116 * linefeed instead; unfortunately this requires resetting CRMOD, and
117 * many output routines will have to be modified slightly. Let's
118 * leave that till the next release.
119 */
120 XD = t_agetstr(info, "xd");
121 /* not: XD = t_agetstr(info, "do"); */
122 if (!(CM = t_agetstr(info, "cm"))) {
123 if (!UP && !HO)
124 error("Hack needs CM or UP or HO.");
125 printf("Playing hack on terminals without cm is suspect...\n");
126 getret();
127 }
128 SO = t_agetstr(info, "so");
129 SE = t_agetstr(info, "se");
130 SG = t_getnum(info, "sg"); /* -1: not fnd; else # of spaces left by so */
131 if (!SO || !SE || (SG > 0))
132 SO = SE = 0;
133 CD = t_agetstr(info, "cd");
134 set_whole_screen(); /* uses LI and CD */
135 }
136
137 void
138 start_screen(void)
139 {
140 xputs(TI);
141 xputs(VS);
142 }
143
144 void
145 end_screen(void)
146 {
147 xputs(VE);
148 xputs(TE);
149 }
150
151 /*
152 * Cursor movements
153 *
154 * x,y not xchar: perhaps xchar is unsigned and
155 * curx-x would be unsigned as well
156 */
157 void
158 curs(int x, int y)
159 {
160
161 if (y == cury && x == curx)
162 return;
163 if (!ND && (curx != x || x <= 3)) { /* Extremely primitive */
164 cmov(x, y); /* bunker!wtm */
165 return;
166 }
167 if (abs(cury - y) <= 3 && abs(curx - x) <= 3)
168 nocmov(x, y);
169 else if ((x <= 3 && abs(cury - y) <= 3) || (!CM && x < abs(curx - x))) {
170 (void) putchar('\r');
171 curx = 1;
172 nocmov(x, y);
173 } else if (!CM) {
174 nocmov(x, y);
175 } else
176 cmov(x, y);
177 }
178
179 void
180 nocmov(int x, int y)
181 {
182 if (cury > y) {
183 if (UP) {
184 while (cury > y) { /* Go up. */
185 xputs(UP);
186 cury--;
187 }
188 } else if (CM) {
189 cmov(x, y);
190 } else if (HO) {
191 home();
192 curs(x, y);
193 } /* else impossible("..."); */
194 } else if (cury < y) {
195 if (XD) {
196 while (cury < y) {
197 xputs(XD);
198 cury++;
199 }
200 } else if (CM) {
201 cmov(x, y);
202 } else {
203 while (cury < y) {
204 xputc('\n');
205 curx = 1;
206 cury++;
207 }
208 }
209 }
210 if (curx < x) { /* Go to the right. */
211 if (!ND)
212 cmov(x, y);
213 else /* bah */
214 /* should instead print what is there already */
215 while (curx < x) {
216 xputs(ND);
217 curx++;
218 }
219 } else if (curx > x) {
220 while (curx > x) { /* Go to the left. */
221 xputs(BC_BS);
222 curx--;
223 }
224 }
225 }
226
227 void
228 cmov(int x, int y)
229 {
230 char buf[256];
231
232 if (t_goto(info, CM, x - 1, y - 1, buf, 255) >= 0) {
233 xputs(buf);
234 cury = y;
235 curx = x;
236 }
237 }
238
239 int
240 xputc(int c)
241 {
242 return (fputc(c, stdout));
243 }
244
245 void
246 xputs(const char *s)
247 {
248 tputs(s, 1, xputc);
249 }
250
251 void
252 cl_end(void)
253 {
254 if (CE)
255 xputs(CE);
256 else { /* no-CE fix - free after Harold Rynes */
257 /*
258 * this looks terrible, especially on a slow terminal but is
259 * better than nothing
260 */
261 int cx = curx, cy = cury;
262
263 while (curx < COLNO) {
264 xputc(' ');
265 curx++;
266 }
267 curs(cx, cy);
268 }
269 }
270
271 void
272 clear_screen(void)
273 {
274 xputs(CL);
275 curx = cury = 1;
276 }
277
278 void
279 home(void)
280 {
281 char buf[256];
282
283 if (HO)
284 xputs(HO);
285 else if ((CM) && (t_goto(info, CM, 0, 0, buf, 255) >= 0))
286 xputs(buf);
287 else
288 curs(1, 1); /* using UP ... */
289 curx = cury = 1;
290 }
291
292 void
293 standoutbeg(void)
294 {
295 if (SO)
296 xputs(SO);
297 }
298
299 void
300 standoutend(void)
301 {
302 if (SE)
303 xputs(SE);
304 }
305
306 void
307 backsp(void)
308 {
309 xputs(BC_BS);
310 curx--;
311 }
312
313 void
314 bell(void)
315 {
316 (void) putchar('\007'); /* curx does not change */
317 (void) fflush(stdout);
318 }
319
320 void
321 delay_output(void)
322 {
323
324 /* delay 50 ms - could also use a 'nap'-system call */
325 /* or the usleep call like this :-) */
326 usleep(50000);
327 }
328
329 void
330 cl_eos(void)
331 { /* free after Robert Viduya *//* must only be
332 * called with curx = 1 */
333
334 if (CD)
335 xputs(CD);
336 else {
337 int cx = curx, cy = cury;
338 while (cury <= LI - 2) {
339 cl_end();
340 xputc('\n');
341 curx = 1;
342 cury++;
343 }
344 cl_end();
345 curs(cx, cy);
346 }
347 }