NetBSD/games/hack/hack.topl.c

250 lines
4.2 KiB
C
Raw Normal View History

2002-05-26 04:12:11 +04:00
/* $NetBSD: hack.topl.c,v 1.6 2002/05/26 00:12:12 wiz Exp $ */
1997-10-19 20:56:41 +04:00
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
#ifndef lint
2002-05-26 04:12:11 +04:00
__RCSID("$NetBSD: hack.topl.c,v 1.6 2002/05/26 00:12:12 wiz 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
1997-10-19 20:56:41 +04:00
char toplines[BUFSZ];
xchar tlx, tly; /* set by pline; used by addtopl */
1993-03-21 12:45:37 +03:00
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 */
1997-10-19 20:56:41 +04:00
int
doredotopl()
{
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
}
1997-10-19 20:56:41 +04:00
void
redotoplin()
{
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()
{
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 = (struct topl *)
1997-10-19 20:56:41 +04:00
alloc((unsigned) (strlen(toplines) + sizeof(struct topl) + 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) {
1993-03-21 12:45:37 +03:00
free((char *) tl->next_topl);
tl->next_topl = 0;
}
}
1997-10-19 20:56:41 +04:00
void
addtopl(s)
const char *s;
1997-10-19 20:56:41 +04:00
{
curs(tlx, tly);
if (tlx + strlen(s) > CO)
putsym('\n');
1993-03-21 12:45:37 +03:00
putstr(s);
tlx = curx;
tly = cury;
flags.toplin = 1;
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
xmore(s)
const char *s; /* allowed chars besides space/return */
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()
{
1993-03-21 12:45:37 +03:00
xmore("");
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
cmore(s)
const char *s;
1993-03-21 12:45:37 +03:00
{
xmore(s);
}
1997-10-19 20:56:41 +04:00
void
clrlin()
{
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(line, ap)
const char *line;
va_list ap;
{
char pbuf[BUFSZ];
char *bp = pbuf, *tl;
int n, n0;
if (!line || !*line)
return;
if (!strchr(line, '%'))
(void) strcpy(pbuf, line);
else
(void) vsprintf(pbuf, line, ap);
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 &&
n0 + strlen(toplines) + 3 < CO - 8 && /* leave room for
* --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();
toplines[0] = 0;
1997-10-19 20:56:41 +04:00
while (n0) {
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
}
(void) strncpy((tl = eos(toplines)), bp, n0);
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) strcat(tl, "\n");
1993-03-21 12:45:37 +03:00
}
redotoplin();
}
1997-10-19 20:56:41 +04:00
void
putsym(c)
char c;
{
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(s)
const char *s;
1997-10-19 20:56:41 +04:00
{
while (*s)
putsym(*s++);
1993-03-21 12:45:37 +03:00
}