]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.termcap.c
Coverity CID 3382: Fix memory leak.
[bsdgames-darwin.git] / hack / hack.termcap.c
1 /* $NetBSD: hack.termcap.c,v 1.13 2006/05/11 00:18:31 mrg 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.13 2006/05/11 00:18:31 mrg 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 char *HO, *CL, *CE, *CM, *ND, *XD, *SO, *SE, *TI, *TE;
80 static 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()
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 HO = t_agetstr(info, "ho");
98 CO = t_getnum(info, "co");
99 LI = t_getnum(info, "li");
100 if (CO < COLNO || LI < ROWNO + 2)
101 setclipped();
102 if (!(CL = t_agetstr(info, "cl")))
103 error("Hack needs CL.");
104 ND = t_agetstr(info, "nd");
105 if (t_getflag(info, "os"))
106 error("Hack can't have OS.");
107 CE = t_agetstr(info, "ce");
108 /*
109 * It seems that xd is no longer supported, and we should use a
110 * linefeed instead; unfortunately this requires resetting CRMOD, and
111 * many output routines will have to be modified slightly. Let's
112 * leave that till the next release.
113 */
114 XD = t_agetstr(info, "xd");
115 /* not: XD = t_agetstr(info, "do"); */
116 if (!(CM = t_agetstr(info, "cm"))) {
117 if (!UP && !HO)
118 error("Hack needs CM or UP or HO.");
119 printf("Playing hack on terminals without cm is suspect...\n");
120 getret();
121 }
122 SO = t_agetstr(info, "so");
123 SE = t_agetstr(info, "se");
124 SG = t_getnum(info, "sg"); /* -1: not fnd; else # of spaces left by so */
125 if (!SO || !SE || (SG > 0))
126 SO = SE = 0;
127 CD = t_agetstr(info, "cd");
128 set_whole_screen(); /* uses LI and CD */
129 }
130
131 void
132 start_screen()
133 {
134 xputs(TI);
135 xputs(VS);
136 }
137
138 void
139 end_screen()
140 {
141 xputs(VE);
142 xputs(TE);
143 }
144
145 /* Cursor movements */
146 void
147 curs(x, y)
148 int x, y; /* not xchar: perhaps xchar is unsigned and
149 * curx-x would be unsigned as well */
150 {
151
152 if (y == cury && x == curx)
153 return;
154 if (!ND && (curx != x || x <= 3)) { /* Extremely primitive */
155 cmov(x, y); /* bunker!wtm */
156 return;
157 }
158 if (abs(cury - y) <= 3 && abs(curx - x) <= 3)
159 nocmov(x, y);
160 else if ((x <= 3 && abs(cury - y) <= 3) || (!CM && x < abs(curx - x))) {
161 (void) putchar('\r');
162 curx = 1;
163 nocmov(x, y);
164 } else if (!CM) {
165 nocmov(x, y);
166 } else
167 cmov(x, y);
168 }
169
170 void
171 nocmov(x, y)
172 int x, y;
173 {
174 if (cury > y) {
175 if (UP) {
176 while (cury > y) { /* Go up. */
177 xputs(UP);
178 cury--;
179 }
180 } else if (CM) {
181 cmov(x, y);
182 } else if (HO) {
183 home();
184 curs(x, y);
185 } /* else impossible("..."); */
186 } else if (cury < y) {
187 if (XD) {
188 while (cury < y) {
189 xputs(XD);
190 cury++;
191 }
192 } else if (CM) {
193 cmov(x, y);
194 } else {
195 while (cury < y) {
196 xputc('\n');
197 curx = 1;
198 cury++;
199 }
200 }
201 }
202 if (curx < x) { /* Go to the right. */
203 if (!ND)
204 cmov(x, y);
205 else /* bah */
206 /* should instead print what is there already */
207 while (curx < x) {
208 xputs(ND);
209 curx++;
210 }
211 } else if (curx > x) {
212 while (curx > x) { /* Go to the left. */
213 xputs(BC);
214 curx--;
215 }
216 }
217 }
218
219 void
220 cmov(x, y)
221 int x, y;
222 {
223 char buf[256];
224
225 if (t_goto(info, CM, x - 1, y - 1, buf, 255) >= 0) {
226 xputs(buf);
227 cury = y;
228 curx = x;
229 }
230 }
231
232 int
233 xputc(c)
234 char c;
235 {
236 return (fputc(c, stdout));
237 }
238
239 void
240 xputs(s)
241 char *s;
242 {
243 tputs(s, 1, xputc);
244 }
245
246 void
247 cl_end()
248 {
249 if (CE)
250 xputs(CE);
251 else { /* no-CE fix - free after Harold Rynes */
252 /*
253 * this looks terrible, especially on a slow terminal but is
254 * better than nothing
255 */
256 int cx = curx, cy = cury;
257
258 while (curx < COLNO) {
259 xputc(' ');
260 curx++;
261 }
262 curs(cx, cy);
263 }
264 }
265
266 void
267 clear_screen()
268 {
269 xputs(CL);
270 curx = cury = 1;
271 }
272
273 void
274 home()
275 {
276 char buf[256];
277
278 if (HO)
279 xputs(HO);
280 else if ((CM) && (t_goto(info, CM, 0, 0, buf, 255) >= 0))
281 xputs(buf);
282 else
283 curs(1, 1); /* using UP ... */
284 curx = cury = 1;
285 }
286
287 void
288 standoutbeg()
289 {
290 if (SO)
291 xputs(SO);
292 }
293
294 void
295 standoutend()
296 {
297 if (SE)
298 xputs(SE);
299 }
300
301 void
302 backsp()
303 {
304 xputs(BC);
305 curx--;
306 }
307
308 void
309 bell()
310 {
311 (void) putchar('\007'); /* curx does not change */
312 (void) fflush(stdout);
313 }
314
315 void
316 delay_output()
317 {
318
319 /* delay 50 ms - could also use a 'nap'-system call */
320 /* or the usleep call like this :-) */
321 usleep(50000);
322 }
323
324 void
325 cl_eos()
326 { /* free after Robert Viduya *//* must only be
327 * called with curx = 1 */
328
329 if (CD)
330 xputs(CD);
331 else {
332 int cx = curx, cy = cury;
333 while (cury <= LI - 2) {
334 cl_end();
335 xputc('\n');
336 curx = 1;
337 cury++;
338 }
339 cl_end();
340 curs(cx, cy);
341 }
342 }