NetBSD/games/hack/hack.topl.c

313 lines
7.5 KiB
C
Raw Normal View History

2011-08-07 00:29:37 +04:00
/* $NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $ */
1997-10-19 20:56:41 +04:00
/*
* Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
* Amsterdam
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Stichting Centrum voor Wiskunde en
* Informatica, nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
#ifndef lint
2011-08-07 00:29:37 +04:00
__RCSID("$NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
#include <stdlib.h>
1993-03-21 12:45:37 +03:00
#include "hack.h"
1997-10-19 20:56:41 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
2009-08-12 11:28:40 +04:00
static char toplines[BUFSZ];
static xchar tlx, tly; /* set by pline; used by addtopl */
1993-03-21 12:45:37 +03:00
2009-08-12 11:28:40 +04:00
static struct topl {
1997-10-19 20:56:41 +04:00
struct topl *next_topl;
char *topl_text;
} *old_toplines, *last_redone_topl;
1993-03-21 12:45:37 +03:00
#define OTLMAX 20 /* max nr of old toplines remembered */
2009-08-12 11:28:40 +04:00
static void redotoplin(void);
static void xmore(const char *);
1997-10-19 20:56:41 +04:00
int
doredotopl(void)
1997-10-19 20:56:41 +04:00
{
if (last_redone_topl)
1993-03-21 12:45:37 +03:00
last_redone_topl = last_redone_topl->next_topl;
1997-10-19 20:56:41 +04:00
if (!last_redone_topl)
1993-03-21 12:45:37 +03:00
last_redone_topl = old_toplines;
1997-10-19 20:56:41 +04:00
if (last_redone_topl) {
1993-03-21 12:45:37 +03:00
(void) strcpy(toplines, last_redone_topl->topl_text);
}
redotoplin();
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
2009-08-12 11:28:40 +04:00
static void
redotoplin(void)
1997-10-19 20:56:41 +04:00
{
1993-03-21 12:45:37 +03:00
home();
1997-10-19 20:56:41 +04:00
if (strchr(toplines, '\n'))
cl_end();
1993-03-21 12:45:37 +03:00
putstr(toplines);
cl_end();
tlx = curx;
tly = cury;
flags.toplin = 1;
1997-10-19 20:56:41 +04:00
if (tly > 1)
1993-03-21 12:45:37 +03:00
more();
}
1997-10-19 20:56:41 +04:00
void
remember_topl(void)
1997-10-19 20:56:41 +04:00
{
struct topl *tl;
int cnt = OTLMAX;
if (last_redone_topl &&
!strcmp(toplines, last_redone_topl->topl_text))
return;
if (old_toplines &&
!strcmp(toplines, old_toplines->topl_text))
return;
1993-03-21 12:45:37 +03:00
last_redone_topl = 0;
tl = alloc(strlen(toplines) + sizeof(*tl) + 1);
1993-03-21 12:45:37 +03:00
tl->next_topl = old_toplines;
1997-10-19 20:56:41 +04:00
tl->topl_text = (char *) (tl + 1);
1993-03-21 12:45:37 +03:00
(void) strcpy(tl->topl_text, toplines);
old_toplines = tl;
1997-10-19 20:56:41 +04:00
while (cnt && tl) {
1993-03-21 12:45:37 +03:00
cnt--;
tl = tl->next_topl;
}
1997-10-19 20:56:41 +04:00
if (tl && tl->next_topl) {
2011-08-07 00:29:37 +04:00
free(tl->next_topl);
1993-03-21 12:45:37 +03:00
tl->next_topl = 0;
}
}
1997-10-19 20:56:41 +04:00
void
addtopl(const char *s)
1997-10-19 20:56:41 +04:00
{
curs(tlx, tly);
2008-01-28 09:55:41 +03:00
if (tlx + (int)strlen(s) > CO)
1997-10-19 20:56:41 +04:00
putsym('\n');
1993-03-21 12:45:37 +03:00
putstr(s);
tlx = curx;
tly = cury;
flags.toplin = 1;
}
/* s = allowed chars besides space/return */
2009-08-12 11:28:40 +04:00
static void
xmore(const char *s)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (flags.toplin) {
1993-03-21 12:45:37 +03:00
curs(tlx, tly);
1997-10-19 20:56:41 +04:00
if (tlx + 8 > CO)
putsym('\n'), tly++;
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (flags.standout)
1993-03-21 12:45:37 +03:00
standoutbeg();
putstr("--More--");
1997-10-19 20:56:41 +04:00
if (flags.standout)
1993-03-21 12:45:37 +03:00
standoutend();
xwaitforspace(s);
1997-10-19 20:56:41 +04:00
if (flags.toplin && tly > 1) {
1993-03-21 12:45:37 +03:00
home();
cl_end();
1997-10-19 20:56:41 +04:00
docorner(1, tly - 1);
1993-03-21 12:45:37 +03:00
}
flags.toplin = 0;
}
1997-10-19 20:56:41 +04:00
void
more(void)
1997-10-19 20:56:41 +04:00
{
1993-03-21 12:45:37 +03:00
xmore("");
}
1997-10-19 20:56:41 +04:00
void
cmore(const char *s)
1993-03-21 12:45:37 +03:00
{
xmore(s);
}
1997-10-19 20:56:41 +04:00
void
clrlin(void)
1997-10-19 20:56:41 +04:00
{
if (flags.toplin) {
1993-03-21 12:45:37 +03:00
home();
cl_end();
1997-10-19 20:56:41 +04:00
if (tly > 1)
docorner(1, tly - 1);
1993-03-21 12:45:37 +03:00
remember_topl();
}
flags.toplin = 0;
}
1997-10-19 20:56:41 +04:00
void
pline(const char *fmt, ...)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
va_list ap;
2002-05-26 04:12:11 +04:00
1997-10-19 20:56:41 +04:00
va_start(ap, fmt);
vpline(fmt, ap);
va_end(ap);
}
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
vpline(const char *line, va_list ap)
1997-10-19 20:56:41 +04:00
{
char pbuf[BUFSZ];
char *bp = pbuf, *tl;
int n, n0, tlpos, dead;
1997-10-19 20:56:41 +04:00
if (!line || !*line)
return;
if (!strchr(line, '%'))
(void) strlcpy(pbuf, line, sizeof(pbuf));
1997-10-19 20:56:41 +04:00
else
(void) vsnprintf(pbuf, sizeof(pbuf), line, ap);
1997-10-19 20:56:41 +04:00
if (flags.toplin == 1 && !strcmp(pbuf, toplines))
return;
nscr(); /* %% */
1993-03-21 12:45:37 +03:00
/* If there is room on the line, print message on same line */
/* But messages like "You die..." deserve their own line */
n0 = strlen(bp);
1997-10-19 20:56:41 +04:00
if (flags.toplin == 1 && tly == 1 &&
2008-01-28 09:55:41 +03:00
n0 + (int)strlen(toplines) + 3 < CO - 8 && /* leave room for
1997-10-19 20:56:41 +04:00
* --More-- */
1993-03-21 12:45:37 +03:00
strncmp(bp, "You ", 4)) {
(void) strcat(toplines, " ");
(void) strcat(toplines, bp);
tlx += 2;
addtopl(bp);
return;
}
1997-10-19 20:56:41 +04:00
if (flags.toplin == 1)
more();
1993-03-21 12:45:37 +03:00
remember_topl();
dead = 0;
1993-03-21 12:45:37 +03:00
toplines[0] = 0;
while (n0 && !dead) {
1997-10-19 20:56:41 +04:00
if (n0 >= CO) {
1993-03-21 12:45:37 +03:00
/* look for appropriate cut point */
n0 = 0;
1997-10-19 20:56:41 +04:00
for (n = 0; n < CO; n++)
if (bp[n] == ' ')
n0 = n;
if (!n0)
for (n = 0; n < CO - 1; n++)
if (!letter(bp[n]))
n0 = n;
if (!n0)
n0 = CO - 2;
1993-03-21 12:45:37 +03:00
}
tlpos = strlen(toplines);
tl = toplines + tlpos;
/* avoid overflow */
if (tlpos + n0 > (int)sizeof(toplines) - 1) {
n0 = sizeof(toplines) - 1 - tlpos;
dead = 1;
}
(void) memcpy(tl, bp, n0);
1993-03-21 12:45:37 +03:00
tl[n0] = 0;
bp += n0;
/* remove trailing spaces, but leave one */
1997-10-19 20:56:41 +04:00
while (n0 > 1 && tl[n0 - 1] == ' ' && tl[n0 - 2] == ' ')
1993-03-21 12:45:37 +03:00
tl[--n0] = 0;
n0 = strlen(bp);
1997-10-19 20:56:41 +04:00
if (n0 && tl[0])
(void) strlcat(toplines, "\n", sizeof(toplines));
1993-03-21 12:45:37 +03:00
}
redotoplin();
}
1997-10-19 20:56:41 +04:00
void
putsym(int c1)
1997-10-19 20:56:41 +04:00
{
char c = c1; /* XXX this hack prevents .o diffs -- remove later */
1997-10-19 20:56:41 +04:00
switch (c) {
1993-03-21 12:45:37 +03:00
case '\b':
backsp();
return;
case '\n':
curx = 1;
cury++;
1997-10-19 20:56:41 +04:00
if (cury > tly)
tly = cury;
1993-03-21 12:45:37 +03:00
break;
default:
1997-10-19 20:56:41 +04:00
if (curx == CO)
1993-03-21 12:45:37 +03:00
putsym('\n'); /* 1 <= curx <= CO; avoid CO */
else
curx++;
}
(void) putchar(c);
}
1997-10-19 20:56:41 +04:00
void
putstr(const char *s)
1997-10-19 20:56:41 +04:00
{
while (*s)
putsym(*s++);
1993-03-21 12:45:37 +03:00
}