xref: /csrg-svn/games/hack/hack.topl.c (revision 41265)
1*41265Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2*41265Sbostic /* hack.topl.c - version 1.0.2 */
3*41265Sbostic 
4*41265Sbostic #include "hack.h"
5*41265Sbostic #include <stdio.h>
6*41265Sbostic extern char *eos();
7*41265Sbostic extern int CO;
8*41265Sbostic 
9*41265Sbostic char toplines[BUFSZ];
10*41265Sbostic xchar tlx, tly;			/* set by pline; used by addtopl */
11*41265Sbostic 
12*41265Sbostic struct topl {
13*41265Sbostic 	struct topl *next_topl;
14*41265Sbostic 	char *topl_text;
15*41265Sbostic } *old_toplines, *last_redone_topl;
16*41265Sbostic #define	OTLMAX	20		/* max nr of old toplines remembered */
17*41265Sbostic 
doredotopl()18*41265Sbostic doredotopl(){
19*41265Sbostic 	if(last_redone_topl)
20*41265Sbostic 		last_redone_topl = last_redone_topl->next_topl;
21*41265Sbostic 	if(!last_redone_topl)
22*41265Sbostic 		last_redone_topl = old_toplines;
23*41265Sbostic 	if(last_redone_topl){
24*41265Sbostic 		(void) strcpy(toplines, last_redone_topl->topl_text);
25*41265Sbostic 	}
26*41265Sbostic 	redotoplin();
27*41265Sbostic 	return(0);
28*41265Sbostic }
29*41265Sbostic 
redotoplin()30*41265Sbostic redotoplin() {
31*41265Sbostic 	home();
32*41265Sbostic 	if(index(toplines, '\n')) cl_end();
33*41265Sbostic 	putstr(toplines);
34*41265Sbostic 	cl_end();
35*41265Sbostic 	tlx = curx;
36*41265Sbostic 	tly = cury;
37*41265Sbostic 	flags.toplin = 1;
38*41265Sbostic 	if(tly > 1)
39*41265Sbostic 		more();
40*41265Sbostic }
41*41265Sbostic 
remember_topl()42*41265Sbostic remember_topl() {
43*41265Sbostic register struct topl *tl;
44*41265Sbostic register int cnt = OTLMAX;
45*41265Sbostic 	if(last_redone_topl &&
46*41265Sbostic 	   !strcmp(toplines, last_redone_topl->topl_text)) return;
47*41265Sbostic 	if(old_toplines &&
48*41265Sbostic 	   !strcmp(toplines, old_toplines->topl_text)) return;
49*41265Sbostic 	last_redone_topl = 0;
50*41265Sbostic 	tl = (struct topl *)
51*41265Sbostic 		alloc((unsigned)(strlen(toplines) + sizeof(struct topl) + 1));
52*41265Sbostic 	tl->next_topl = old_toplines;
53*41265Sbostic 	tl->topl_text = (char *)(tl + 1);
54*41265Sbostic 	(void) strcpy(tl->topl_text, toplines);
55*41265Sbostic 	old_toplines = tl;
56*41265Sbostic 	while(cnt && tl){
57*41265Sbostic 		cnt--;
58*41265Sbostic 		tl = tl->next_topl;
59*41265Sbostic 	}
60*41265Sbostic 	if(tl && tl->next_topl){
61*41265Sbostic 		free((char *) tl->next_topl);
62*41265Sbostic 		tl->next_topl = 0;
63*41265Sbostic 	}
64*41265Sbostic }
65*41265Sbostic 
addtopl(s)66*41265Sbostic addtopl(s) char *s; {
67*41265Sbostic 	curs(tlx,tly);
68*41265Sbostic 	if(tlx + strlen(s) > CO) putsym('\n');
69*41265Sbostic 	putstr(s);
70*41265Sbostic 	tlx = curx;
71*41265Sbostic 	tly = cury;
72*41265Sbostic 	flags.toplin = 1;
73*41265Sbostic }
74*41265Sbostic 
xmore(s)75*41265Sbostic xmore(s)
76*41265Sbostic char *s;	/* allowed chars besides space/return */
77*41265Sbostic {
78*41265Sbostic 	if(flags.toplin) {
79*41265Sbostic 		curs(tlx, tly);
80*41265Sbostic 		if(tlx + 8 > CO) putsym('\n'), tly++;
81*41265Sbostic 	}
82*41265Sbostic 
83*41265Sbostic 	if(flags.standout)
84*41265Sbostic 		standoutbeg();
85*41265Sbostic 	putstr("--More--");
86*41265Sbostic 	if(flags.standout)
87*41265Sbostic 		standoutend();
88*41265Sbostic 
89*41265Sbostic 	xwaitforspace(s);
90*41265Sbostic 	if(flags.toplin && tly > 1) {
91*41265Sbostic 		home();
92*41265Sbostic 		cl_end();
93*41265Sbostic 		docorner(1, tly-1);
94*41265Sbostic 	}
95*41265Sbostic 	flags.toplin = 0;
96*41265Sbostic }
97*41265Sbostic 
more()98*41265Sbostic more(){
99*41265Sbostic 	xmore("");
100*41265Sbostic }
101*41265Sbostic 
cmore(s)102*41265Sbostic cmore(s)
103*41265Sbostic register char *s;
104*41265Sbostic {
105*41265Sbostic 	xmore(s);
106*41265Sbostic }
107*41265Sbostic 
clrlin()108*41265Sbostic clrlin(){
109*41265Sbostic 	if(flags.toplin) {
110*41265Sbostic 		home();
111*41265Sbostic 		cl_end();
112*41265Sbostic 		if(tly > 1) docorner(1, tly-1);
113*41265Sbostic 		remember_topl();
114*41265Sbostic 	}
115*41265Sbostic 	flags.toplin = 0;
116*41265Sbostic }
117*41265Sbostic 
118*41265Sbostic /*VARARGS1*/
pline(line,arg1,arg2,arg3,arg4,arg5,arg6)119*41265Sbostic pline(line,arg1,arg2,arg3,arg4,arg5,arg6)
120*41265Sbostic register char *line,*arg1,*arg2,*arg3,*arg4,*arg5,*arg6;
121*41265Sbostic {
122*41265Sbostic 	char pbuf[BUFSZ];
123*41265Sbostic 	register char *bp = pbuf, *tl;
124*41265Sbostic 	register int n,n0;
125*41265Sbostic 
126*41265Sbostic 	if(!line || !*line) return;
127*41265Sbostic 	if(!index(line, '%')) (void) strcpy(pbuf,line); else
128*41265Sbostic 	(void) sprintf(pbuf,line,arg1,arg2,arg3,arg4,arg5,arg6);
129*41265Sbostic 	if(flags.toplin == 1 && !strcmp(pbuf, toplines)) return;
130*41265Sbostic 	nscr();		/* %% */
131*41265Sbostic 
132*41265Sbostic 	/* If there is room on the line, print message on same line */
133*41265Sbostic 	/* But messages like "You die..." deserve their own line */
134*41265Sbostic 	n0 = strlen(bp);
135*41265Sbostic 	if(flags.toplin == 1 && tly == 1 &&
136*41265Sbostic 	    n0 + strlen(toplines) + 3 < CO-8 &&  /* leave room for --More-- */
137*41265Sbostic 	    strncmp(bp, "You ", 4)) {
138*41265Sbostic 		(void) strcat(toplines, "  ");
139*41265Sbostic 		(void) strcat(toplines, bp);
140*41265Sbostic 		tlx += 2;
141*41265Sbostic 		addtopl(bp);
142*41265Sbostic 		return;
143*41265Sbostic 	}
144*41265Sbostic 	if(flags.toplin == 1) more();
145*41265Sbostic 	remember_topl();
146*41265Sbostic 	toplines[0] = 0;
147*41265Sbostic 	while(n0){
148*41265Sbostic 		if(n0 >= CO){
149*41265Sbostic 			/* look for appropriate cut point */
150*41265Sbostic 			n0 = 0;
151*41265Sbostic 			for(n = 0; n < CO; n++) if(bp[n] == ' ')
152*41265Sbostic 				n0 = n;
153*41265Sbostic 			if(!n0) for(n = 0; n < CO-1; n++)
154*41265Sbostic 				if(!letter(bp[n])) n0 = n;
155*41265Sbostic 			if(!n0) n0 = CO-2;
156*41265Sbostic 		}
157*41265Sbostic 		(void) strncpy((tl = eos(toplines)), bp, n0);
158*41265Sbostic 		tl[n0] = 0;
159*41265Sbostic 		bp += n0;
160*41265Sbostic 
161*41265Sbostic 		/* remove trailing spaces, but leave one */
162*41265Sbostic 		while(n0 > 1 && tl[n0-1] == ' ' && tl[n0-2] == ' ')
163*41265Sbostic 			tl[--n0] = 0;
164*41265Sbostic 
165*41265Sbostic 		n0 = strlen(bp);
166*41265Sbostic 		if(n0 && tl[0]) (void) strcat(tl, "\n");
167*41265Sbostic 	}
168*41265Sbostic 	redotoplin();
169*41265Sbostic }
170*41265Sbostic 
putsym(c)171*41265Sbostic putsym(c) char c; {
172*41265Sbostic 	switch(c) {
173*41265Sbostic 	case '\b':
174*41265Sbostic 		backsp();
175*41265Sbostic 		return;
176*41265Sbostic 	case '\n':
177*41265Sbostic 		curx = 1;
178*41265Sbostic 		cury++;
179*41265Sbostic 		if(cury > tly) tly = cury;
180*41265Sbostic 		break;
181*41265Sbostic 	default:
182*41265Sbostic 		if(curx == CO)
183*41265Sbostic 			putsym('\n');	/* 1 <= curx <= CO; avoid CO */
184*41265Sbostic 		else
185*41265Sbostic 			curx++;
186*41265Sbostic 	}
187*41265Sbostic 	(void) putchar(c);
188*41265Sbostic }
189*41265Sbostic 
putstr(s)190*41265Sbostic putstr(s) register char *s; {
191*41265Sbostic 	while(*s) putsym(*s++);
192*41265Sbostic }
193