xref: /netbsd-src/games/hack/hack.topl.c (revision 8e73b3ad763e92a803658f492d224264534c3d17)
1*8e73b3adSdholland /*	$NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $	*/
23ea4a95cSchristos 
3210cab45Smycroft /*
41c7f94e5Sjsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
51c7f94e5Sjsm  * Amsterdam
61c7f94e5Sjsm  * All rights reserved.
71c7f94e5Sjsm  *
81c7f94e5Sjsm  * Redistribution and use in source and binary forms, with or without
91c7f94e5Sjsm  * modification, are permitted provided that the following conditions are
101c7f94e5Sjsm  * met:
111c7f94e5Sjsm  *
121c7f94e5Sjsm  * - Redistributions of source code must retain the above copyright notice,
131c7f94e5Sjsm  * this list of conditions and the following disclaimer.
141c7f94e5Sjsm  *
151c7f94e5Sjsm  * - Redistributions in binary form must reproduce the above copyright
161c7f94e5Sjsm  * notice, this list of conditions and the following disclaimer in the
171c7f94e5Sjsm  * documentation and/or other materials provided with the distribution.
181c7f94e5Sjsm  *
191c7f94e5Sjsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
201c7f94e5Sjsm  * Informatica, nor the names of its contributors may be used to endorse or
211c7f94e5Sjsm  * promote products derived from this software without specific prior
221c7f94e5Sjsm  * written permission.
231c7f94e5Sjsm  *
241c7f94e5Sjsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
251c7f94e5Sjsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
261c7f94e5Sjsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
271c7f94e5Sjsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
281c7f94e5Sjsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
291c7f94e5Sjsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
301c7f94e5Sjsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
311c7f94e5Sjsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
321c7f94e5Sjsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
331c7f94e5Sjsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
341c7f94e5Sjsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
351c7f94e5Sjsm  */
361c7f94e5Sjsm 
371c7f94e5Sjsm /*
381c7f94e5Sjsm  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
391c7f94e5Sjsm  * All rights reserved.
401c7f94e5Sjsm  *
411c7f94e5Sjsm  * Redistribution and use in source and binary forms, with or without
421c7f94e5Sjsm  * modification, are permitted provided that the following conditions
431c7f94e5Sjsm  * are met:
441c7f94e5Sjsm  * 1. Redistributions of source code must retain the above copyright
451c7f94e5Sjsm  *    notice, this list of conditions and the following disclaimer.
461c7f94e5Sjsm  * 2. Redistributions in binary form must reproduce the above copyright
471c7f94e5Sjsm  *    notice, this list of conditions and the following disclaimer in the
481c7f94e5Sjsm  *    documentation and/or other materials provided with the distribution.
491c7f94e5Sjsm  * 3. The name of the author may not be used to endorse or promote products
501c7f94e5Sjsm  *    derived from this software without specific prior written permission.
511c7f94e5Sjsm  *
521c7f94e5Sjsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
531c7f94e5Sjsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
541c7f94e5Sjsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
551c7f94e5Sjsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
561c7f94e5Sjsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
571c7f94e5Sjsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
581c7f94e5Sjsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
591c7f94e5Sjsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
601c7f94e5Sjsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
611c7f94e5Sjsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62210cab45Smycroft  */
63210cab45Smycroft 
643ea4a95cSchristos #include <sys/cdefs.h>
65210cab45Smycroft #ifndef lint
66*8e73b3adSdholland __RCSID("$NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $");
67210cab45Smycroft #endif				/* not lint */
6861f28255Scgd 
693ea4a95cSchristos #include <stdlib.h>
7061f28255Scgd #include "hack.h"
713ea4a95cSchristos #include "extern.h"
7261f28255Scgd 
739b92b189Sdholland static char toplines[BUFSZ];
749b92b189Sdholland static xchar tlx, tly;		/* set by pline; used by addtopl */
7561f28255Scgd 
769b92b189Sdholland static struct topl {
7761f28255Scgd 	struct topl    *next_topl;
7861f28255Scgd 	char           *topl_text;
7961f28255Scgd }              *old_toplines, *last_redone_topl;
8061f28255Scgd #define	OTLMAX	20		/* max nr of old toplines remembered */
8161f28255Scgd 
829b92b189Sdholland static void redotoplin(void);
839b92b189Sdholland static void xmore(const char *);
849b92b189Sdholland 
853ea4a95cSchristos int
doredotopl(void)861fa8a9a6Sdholland doredotopl(void)
873ea4a95cSchristos {
8861f28255Scgd 	if (last_redone_topl)
8961f28255Scgd 		last_redone_topl = last_redone_topl->next_topl;
9061f28255Scgd 	if (!last_redone_topl)
9161f28255Scgd 		last_redone_topl = old_toplines;
9261f28255Scgd 	if (last_redone_topl) {
9361f28255Scgd 		(void) strcpy(toplines, last_redone_topl->topl_text);
9461f28255Scgd 	}
9561f28255Scgd 	redotoplin();
9661f28255Scgd 	return (0);
9761f28255Scgd }
9861f28255Scgd 
999b92b189Sdholland static void
redotoplin(void)1001fa8a9a6Sdholland redotoplin(void)
1013ea4a95cSchristos {
10261f28255Scgd 	home();
1033ea4a95cSchristos 	if (strchr(toplines, '\n'))
1043ea4a95cSchristos 		cl_end();
10561f28255Scgd 	putstr(toplines);
10661f28255Scgd 	cl_end();
10761f28255Scgd 	tlx = curx;
10861f28255Scgd 	tly = cury;
10961f28255Scgd 	flags.toplin = 1;
11061f28255Scgd 	if (tly > 1)
11161f28255Scgd 		more();
11261f28255Scgd }
11361f28255Scgd 
1143ea4a95cSchristos void
remember_topl(void)1151fa8a9a6Sdholland remember_topl(void)
1163ea4a95cSchristos {
1173ea4a95cSchristos 	struct topl    *tl;
1183ea4a95cSchristos 	int             cnt = OTLMAX;
11961f28255Scgd 	if (last_redone_topl &&
1203ea4a95cSchristos 	    !strcmp(toplines, last_redone_topl->topl_text))
1213ea4a95cSchristos 		return;
12261f28255Scgd 	if (old_toplines &&
1233ea4a95cSchristos 	    !strcmp(toplines, old_toplines->topl_text))
1243ea4a95cSchristos 		return;
12561f28255Scgd 	last_redone_topl = 0;
126434d266eSdholland 	tl = alloc(strlen(toplines) + sizeof(*tl) + 1);
12761f28255Scgd 	tl->next_topl = old_toplines;
12861f28255Scgd 	tl->topl_text = (char *) (tl + 1);
12961f28255Scgd 	(void) strcpy(tl->topl_text, toplines);
13061f28255Scgd 	old_toplines = tl;
13161f28255Scgd 	while (cnt && tl) {
13261f28255Scgd 		cnt--;
13361f28255Scgd 		tl = tl->next_topl;
13461f28255Scgd 	}
13561f28255Scgd 	if (tl && tl->next_topl) {
136*8e73b3adSdholland 		free(tl->next_topl);
13761f28255Scgd 		tl->next_topl = 0;
13861f28255Scgd 	}
13961f28255Scgd }
14061f28255Scgd 
1413ea4a95cSchristos void
addtopl(const char * s)1421fa8a9a6Sdholland addtopl(const char *s)
1433ea4a95cSchristos {
14461f28255Scgd 	curs(tlx, tly);
1453c439f43Sdholland 	if (tlx + (int)strlen(s) > CO)
1463ea4a95cSchristos 		putsym('\n');
14761f28255Scgd 	putstr(s);
14861f28255Scgd 	tlx = curx;
14961f28255Scgd 	tly = cury;
15061f28255Scgd 	flags.toplin = 1;
15161f28255Scgd }
15261f28255Scgd 
1531fa8a9a6Sdholland /* s = allowed chars besides space/return */
1549b92b189Sdholland static void
xmore(const char * s)1551fa8a9a6Sdholland xmore(const char *s)
15661f28255Scgd {
15761f28255Scgd 	if (flags.toplin) {
15861f28255Scgd 		curs(tlx, tly);
1593ea4a95cSchristos 		if (tlx + 8 > CO)
1603ea4a95cSchristos 			putsym('\n'), tly++;
16161f28255Scgd 	}
16261f28255Scgd 	if (flags.standout)
16361f28255Scgd 		standoutbeg();
16461f28255Scgd 	putstr("--More--");
16561f28255Scgd 	if (flags.standout)
16661f28255Scgd 		standoutend();
16761f28255Scgd 
16861f28255Scgd 	xwaitforspace(s);
16961f28255Scgd 	if (flags.toplin && tly > 1) {
17061f28255Scgd 		home();
17161f28255Scgd 		cl_end();
17261f28255Scgd 		docorner(1, tly - 1);
17361f28255Scgd 	}
17461f28255Scgd 	flags.toplin = 0;
17561f28255Scgd }
17661f28255Scgd 
1773ea4a95cSchristos void
more(void)1781fa8a9a6Sdholland more(void)
1793ea4a95cSchristos {
18061f28255Scgd 	xmore("");
18161f28255Scgd }
18261f28255Scgd 
1833ea4a95cSchristos void
cmore(const char * s)1841fa8a9a6Sdholland cmore(const char *s)
18561f28255Scgd {
18661f28255Scgd 	xmore(s);
18761f28255Scgd }
18861f28255Scgd 
1893ea4a95cSchristos void
clrlin(void)1901fa8a9a6Sdholland clrlin(void)
1913ea4a95cSchristos {
19261f28255Scgd 	if (flags.toplin) {
19361f28255Scgd 		home();
19461f28255Scgd 		cl_end();
1953ea4a95cSchristos 		if (tly > 1)
1963ea4a95cSchristos 			docorner(1, tly - 1);
19761f28255Scgd 		remember_topl();
19861f28255Scgd 	}
19961f28255Scgd 	flags.toplin = 0;
20061f28255Scgd }
20161f28255Scgd 
2023ea4a95cSchristos void
pline(const char * fmt,...)2033ea4a95cSchristos pline(const char *fmt, ...)
2043ea4a95cSchristos {
2053ea4a95cSchristos 	va_list ap;
2067fd8f3dfSwiz 
2073ea4a95cSchristos 	va_start(ap, fmt);
2083ea4a95cSchristos 	vpline(fmt, ap);
2093ea4a95cSchristos 	va_end(ap);
2103ea4a95cSchristos }
2113ea4a95cSchristos 
2123ea4a95cSchristos void
vpline(const char * line,va_list ap)2131fa8a9a6Sdholland vpline(const char *line, va_list ap)
21461f28255Scgd {
21561f28255Scgd 	char            pbuf[BUFSZ];
2163ea4a95cSchristos 	char           *bp = pbuf, *tl;
217165c915bSdholland 	int             n, n0, tlpos, dead;
21861f28255Scgd 
2193ea4a95cSchristos 	if (!line || !*line)
2203ea4a95cSchristos 		return;
2213ea4a95cSchristos 	if (!strchr(line, '%'))
222907fca1bSdholland 		(void) strlcpy(pbuf, line, sizeof(pbuf));
2233ea4a95cSchristos 	else
224907fca1bSdholland 		(void) vsnprintf(pbuf, sizeof(pbuf), line, ap);
2253ea4a95cSchristos 	if (flags.toplin == 1 && !strcmp(pbuf, toplines))
2263ea4a95cSchristos 		return;
22761f28255Scgd 	nscr();			/* %% */
22861f28255Scgd 
22961f28255Scgd 	/* If there is room on the line, print message on same line */
23061f28255Scgd 	/* But messages like "You die..." deserve their own line */
23161f28255Scgd 	n0 = strlen(bp);
23261f28255Scgd 	if (flags.toplin == 1 && tly == 1 &&
2333c439f43Sdholland 	    n0 + (int)strlen(toplines) + 3 < CO - 8 &&	/* leave room for
2343ea4a95cSchristos 							 * --More-- */
23561f28255Scgd 	    strncmp(bp, "You ", 4)) {
23661f28255Scgd 		(void) strcat(toplines, "  ");
23761f28255Scgd 		(void) strcat(toplines, bp);
23861f28255Scgd 		tlx += 2;
23961f28255Scgd 		addtopl(bp);
24061f28255Scgd 		return;
24161f28255Scgd 	}
2423ea4a95cSchristos 	if (flags.toplin == 1)
2433ea4a95cSchristos 		more();
24461f28255Scgd 	remember_topl();
245165c915bSdholland 	dead = 0;
24661f28255Scgd 	toplines[0] = 0;
247165c915bSdholland 	while (n0 && !dead) {
24861f28255Scgd 		if (n0 >= CO) {
24961f28255Scgd 			/* look for appropriate cut point */
25061f28255Scgd 			n0 = 0;
2513ea4a95cSchristos 			for (n = 0; n < CO; n++)
2523ea4a95cSchristos 				if (bp[n] == ' ')
25361f28255Scgd 					n0 = n;
2543ea4a95cSchristos 			if (!n0)
2553ea4a95cSchristos 				for (n = 0; n < CO - 1; n++)
2563ea4a95cSchristos 					if (!letter(bp[n]))
2573ea4a95cSchristos 						n0 = n;
2583ea4a95cSchristos 			if (!n0)
2593ea4a95cSchristos 				n0 = CO - 2;
26061f28255Scgd 		}
261165c915bSdholland 		tlpos = strlen(toplines);
262165c915bSdholland 		tl = toplines + tlpos;
263165c915bSdholland 		/* avoid overflow */
264165c915bSdholland 		if (tlpos + n0 > (int)sizeof(toplines) - 1) {
265165c915bSdholland 			n0 = sizeof(toplines) - 1 - tlpos;
266165c915bSdholland 			dead = 1;
267165c915bSdholland 		}
268165c915bSdholland 		(void) memcpy(tl, bp, n0);
26961f28255Scgd 		tl[n0] = 0;
27061f28255Scgd 		bp += n0;
27161f28255Scgd 
27261f28255Scgd 		/* remove trailing spaces, but leave one */
27361f28255Scgd 		while (n0 > 1 && tl[n0 - 1] == ' ' && tl[n0 - 2] == ' ')
27461f28255Scgd 			tl[--n0] = 0;
27561f28255Scgd 
27661f28255Scgd 		n0 = strlen(bp);
2773ea4a95cSchristos 		if (n0 && tl[0])
278165c915bSdholland 			(void) strlcat(toplines, "\n", sizeof(toplines));
27961f28255Scgd 	}
28061f28255Scgd 	redotoplin();
28161f28255Scgd }
28261f28255Scgd 
2833ea4a95cSchristos void
putsym(int c1)2841fa8a9a6Sdholland putsym(int c1)
2853ea4a95cSchristos {
2861fa8a9a6Sdholland 	char c = c1; /* XXX this hack prevents .o diffs -- remove later */
2871fa8a9a6Sdholland 
28861f28255Scgd 	switch (c) {
28961f28255Scgd 	case '\b':
29061f28255Scgd 		backsp();
29161f28255Scgd 		return;
29261f28255Scgd 	case '\n':
29361f28255Scgd 		curx = 1;
29461f28255Scgd 		cury++;
2953ea4a95cSchristos 		if (cury > tly)
2963ea4a95cSchristos 			tly = cury;
29761f28255Scgd 		break;
29861f28255Scgd 	default:
29961f28255Scgd 		if (curx == CO)
30061f28255Scgd 			putsym('\n');	/* 1 <= curx <= CO; avoid CO */
30161f28255Scgd 		else
30261f28255Scgd 			curx++;
30361f28255Scgd 	}
30461f28255Scgd 	(void) putchar(c);
30561f28255Scgd }
30661f28255Scgd 
3073ea4a95cSchristos void
putstr(const char * s)3081fa8a9a6Sdholland putstr(const char *s)
3093ea4a95cSchristos {
3103ea4a95cSchristos 	while (*s)
3113ea4a95cSchristos 		putsym(*s++);
31261f28255Scgd }
313