1*6758Srrh static char sccsid[] = "	fancy.c	1.1	82/05/11	";
2*6758Srrh 
3*6758Srrh #include "back.h"
4*6758Srrh 
5*6758Srrh char	PC;			/* padding character */
6*6758Srrh char	*BC;			/* backspace sequence */
7*6758Srrh char	*CD;			/* clear to end of screen sequence */
8*6758Srrh char	*CE;			/* clear to end of line sequence */
9*6758Srrh char	*CL;			/* clear screen sequence */
10*6758Srrh char	*CM;			/* cursor movement instructions */
11*6758Srrh char	*HO;			/* home cursor sequence */
12*6758Srrh char	*MC;			/* column cursor movement map */
13*6758Srrh char	*ML;			/* row cursor movement map */
14*6758Srrh char	*ND;			/* forward cursor sequence */
15*6758Srrh char	*UP;			/* up cursor sequence */
16*6758Srrh 
17*6758Srrh int	lHO;			/* length of HO */
18*6758Srrh int	lBC;			/* length of BC */
19*6758Srrh int	lND;			/* length of ND */
20*6758Srrh int	lUP;			/* length of UP */
21*6758Srrh int	CO;			/* number of columns */
22*6758Srrh int	LI;			/* number of lines */
23*6758Srrh int	*linect;		/* array of lengths of lines on screen
24*6758Srrh 				   (the actual screen is not stored) */
25*6758Srrh 
26*6758Srrh 				/* two letter codes */
27*6758Srrh char	tcap[] = "bccdceclcmhomcmlndup";
28*6758Srrh 				/* corresponding strings */
29*6758Srrh char	**tstr[] = { &BC, &CD, &CE, &CL, &CM, &HO, &MC, &ML, &ND, &UP };
30*6758Srrh 
31*6758Srrh int	buffnum;		/* pointer to output buffer */
32*6758Srrh 
33*6758Srrh char	tbuf[1024];		/* buffer for decoded termcap entries */
34*6758Srrh 
35*6758Srrh int	oldb[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
36*6758Srrh 
37*6758Srrh int	oldr;
38*6758Srrh int	oldw;
39*6758Srrh 						/* "real" cursor positions, so
40*6758Srrh 						 * it knows when to reposition.
41*6758Srrh 						 * These are -1 if curr and curc
42*6758Srrh 						 * are accurate */
43*6758Srrh int	realr;
44*6758Srrh int	realc;
45*6758Srrh 
46*6758Srrh fboard ()  {
47*6758Srrh 	register int	i, j, l;
48*6758Srrh 
49*6758Srrh 	curmove (0,0);				/* do top line */
50*6758Srrh 	for (i = 0; i < 53; i++)
51*6758Srrh 		fancyc ('_');
52*6758Srrh 
53*6758Srrh 	curmove (15,0);				/* do botttom line */
54*6758Srrh 	for (i = 0; i < 53; i++)
55*6758Srrh 		fancyc ('_');
56*6758Srrh 
57*6758Srrh 	l = 1;					/* do vertical lines */
58*6758Srrh 	for (i = 52; i > -1; i -= 28)  {
59*6758Srrh 		curmove ( (l == 1? 1: 15) ,i);
60*6758Srrh 		fancyc ('|');
61*6758Srrh 		for (j = 0; j < 14; j++)  {
62*6758Srrh 			curmove (curr+l,curc-1);
63*6758Srrh 			fancyc ('|');
64*6758Srrh 		}
65*6758Srrh 		if (i == 24)
66*6758Srrh 			i += 32;
67*6758Srrh 		l = -l;				/* alternate directions */
68*6758Srrh 	}
69*6758Srrh 
70*6758Srrh 	curmove (2,1);				/* label positions 13-18 */
71*6758Srrh 	for (i = 13; i < 18; i++)  {
72*6758Srrh 		fancyc ('1');
73*6758Srrh 		fancyc ((i % 10)+'0');
74*6758Srrh 		curmove (curr,curc+2);
75*6758Srrh 	}
76*6758Srrh 	fancyc ('1');
77*6758Srrh 	fancyc ('8');
78*6758Srrh 
79*6758Srrh 	curmove (2,29);				/* label positions 19-24 */
80*6758Srrh 	fancyc ('1');
81*6758Srrh 	fancyc ('9');
82*6758Srrh 	for (i = 20; i < 25; i++)  {
83*6758Srrh 		curmove (curr,curc+2);
84*6758Srrh 		fancyc ('2');
85*6758Srrh 		fancyc ((i % 10)+'0');
86*6758Srrh 	}
87*6758Srrh 
88*6758Srrh 	curmove (14,1);				/* label positions 12-7 */
89*6758Srrh 	fancyc ('1');
90*6758Srrh 	fancyc ('2');
91*6758Srrh 	for (i = 11; i > 6; i--)  {
92*6758Srrh 		curmove (curr,curc+2);
93*6758Srrh 		fancyc (i > 9? '1': ' ');
94*6758Srrh 		fancyc ((i % 10)+'0');
95*6758Srrh 	}
96*6758Srrh 
97*6758Srrh 	curmove (14,30);			/* label positions 6-1 */
98*6758Srrh 	fancyc ('6');
99*6758Srrh 	for (i = 5; i > 0; i--) {
100*6758Srrh 		curmove (curr,curc+3);
101*6758Srrh 		fancyc (i+'0');
102*6758Srrh 	}
103*6758Srrh 
104*6758Srrh 	for (i = 12; i > 6; i--)		/* print positions 12-7 */
105*6758Srrh 		if (board[i])
106*6758Srrh 			bsect (board[i],13,1+4*(12-i),-1);
107*6758Srrh 
108*6758Srrh 	if (board[0])				/* print red men on bar */
109*6758Srrh 		bsect (board[0],13,25,-1);
110*6758Srrh 
111*6758Srrh 	for (i = 6; i > 0; i--)			/* print positions 6-1 */
112*6758Srrh 		if (board[i])
113*6758Srrh 			bsect (board[i],13,29+4*(6-i),-1);
114*6758Srrh 
115*6758Srrh 	l = (off[1] < 0? off[1]+15: off[1]);	/* print white's home */
116*6758Srrh 	bsect (l,3,54,1);
117*6758Srrh 
118*6758Srrh 	curmove (8,25);				/* print the word BAR */
119*6758Srrh 	fancyc ('B');
120*6758Srrh 	fancyc ('A');
121*6758Srrh 	fancyc ('R');
122*6758Srrh 
123*6758Srrh 	for (i = 13; i < 19; i++)		/* print positions 13-18 */
124*6758Srrh 		if (board[i])
125*6758Srrh 			bsect (board[i],3,1+4*(i-13),1);
126*6758Srrh 
127*6758Srrh 	if (board[25])				/* print white's men on bar */
128*6758Srrh 		bsect (board[25],3,25,1);
129*6758Srrh 
130*6758Srrh 	for (i = 19; i < 25; i++)		/* print positions 19-24 */
131*6758Srrh 		if (board[i])
132*6758Srrh 			bsect (board[i],3,29+4*(i-19),1);
133*6758Srrh 
134*6758Srrh 	l = (off[0] < 0? off[0]+15: off[0]);	/* print red's home */
135*6758Srrh 	bsect (-l,13,54,-1);
136*6758Srrh 
137*6758Srrh 	for (i = 0; i < 26; i++)		/* save board position
138*6758Srrh 						 * for refresh later */
139*6758Srrh 		oldb[i] = board[i];
140*6758Srrh 	oldr = (off[1] < 0? off[1]+15: off[1]);
141*6758Srrh 	oldw = -(off[0] < 0? off[0]+15: off[0]);
142*6758Srrh }
143*6758Srrh 
144*6758Srrh /*
145*6758Srrh  * bsect (b,rpos,cpos,cnext)
146*6758Srrh  *	Print the contents of a board position.  "b" has the value of the
147*6758Srrh  * position, "rpos" is the row to start printing, "cpos" is the column to
148*6758Srrh  * start printing, and "cnext" is positive if the position starts at the top
149*6758Srrh  * and negative if it starts at the bottom.  The value of "cpos" is checked
150*6758Srrh  * to see if the position is a player's home, since those are printed
151*6758Srrh  * differently.
152*6758Srrh  */
153*6758Srrh 
154*6758Srrh bsect (b,rpos,cpos,cnext)
155*6758Srrh int	b;					/* contents of position */
156*6758Srrh int	rpos;					/* row of position */
157*6758Srrh int	cpos;					/* column of position */
158*6758Srrh int	cnext;					/* direction of position */
159*6758Srrh 
160*6758Srrh {
161*6758Srrh 	register int	j;			/* index */
162*6758Srrh 	register int	n;			/* number of men on position */
163*6758Srrh 	register int	bct;			/* counter */
164*6758Srrh 	int		k;			/* index */
165*6758Srrh 	char		pc;			/* color of men on position */
166*6758Srrh 
167*6758Srrh 	n = abs(b);				/* initialize n and pc */
168*6758Srrh 	pc = (b > 0? 'r': 'w');
169*6758Srrh 
170*6758Srrh 	if (n < 6 && cpos < 54)			/* position cursor at start */
171*6758Srrh 		curmove (rpos,cpos+1);
172*6758Srrh 	else
173*6758Srrh 		curmove (rpos,cpos);
174*6758Srrh 
175*6758Srrh 	for (j = 0; j < 5; j++)  {		/* print position row by row */
176*6758Srrh 
177*6758Srrh 		for (k = 0; k < 15; k += 5)		/* print men */
178*6758Srrh 			if (n > j+k)
179*6758Srrh 				fancyc (pc);
180*6758Srrh 
181*6758Srrh 		if (j < 4)  {				/* figure how far to
182*6758Srrh 							 * back up for next
183*6758Srrh 							 * row */
184*6758Srrh 			if (n < 6)  {			/* stop if none left */
185*6758Srrh 				if (j+1 == n)
186*6758Srrh 					break;
187*6758Srrh 				bct = 1;		/* single column */
188*6758Srrh 			} else  {
189*6758Srrh 				if (n < 11)  {		/* two columns */
190*6758Srrh 					if (cpos == 54)  {	/* home pos */
191*6758Srrh 						if (j+5 >= n)
192*6758Srrh 							bct = 1;
193*6758Srrh 						else
194*6758Srrh 							bct = 2;
195*6758Srrh 					}
196*6758Srrh 					if (cpos < 54)  {	/* not home */
197*6758Srrh 						if (j+6 >= n)
198*6758Srrh 							bct = 1;
199*6758Srrh 						else
200*6758Srrh 							bct = 2;
201*6758Srrh 					}
202*6758Srrh 				} else  {		/* three columns */
203*6758Srrh 					if (j+10 >= n)
204*6758Srrh 						bct = 2;
205*6758Srrh 					else
206*6758Srrh 						bct = 3;
207*6758Srrh 				}
208*6758Srrh 			}
209*6758Srrh 			curmove (curr+cnext,curc-bct);	/* reposition cursor */
210*6758Srrh 		}
211*6758Srrh 	}
212*6758Srrh }
213*6758Srrh 
214*6758Srrh refresh()  {
215*6758Srrh 	register int	i, r, c;
216*6758Srrh 
217*6758Srrh 	r = curr;				/* save current position */
218*6758Srrh 	c = curc;
219*6758Srrh 
220*6758Srrh 	for (i = 12; i > 6; i--)		/* fix positions 12-7 */
221*6758Srrh 		if (board[i] != oldb[i])  {
222*6758Srrh 			fixpos (oldb[i],board[i],13,1+(12-i)*4,-1);
223*6758Srrh 			oldb[i] = board[i];
224*6758Srrh 		}
225*6758Srrh 
226*6758Srrh 	if (board[0] != oldb[0])  {		/* fix red men on bar */
227*6758Srrh 		fixpos (oldb[0],board[0],13,25,-1);
228*6758Srrh 		oldb[0] = board[0];
229*6758Srrh 	}
230*6758Srrh 
231*6758Srrh 	for (i = 6; i > 0; i--)			/* fix positions 6-1 */
232*6758Srrh 		if (board[i] != oldb[i])  {
233*6758Srrh 			fixpos (oldb[i],board[i],13,29+(6-i)*4,-1);
234*6758Srrh 			oldb[i] = board[i];
235*6758Srrh 		}
236*6758Srrh 
237*6758Srrh 	i = -(off[0] < 0? off[0]+15: off[0]);	/* fix white's home */
238*6758Srrh 	if (oldw != i)  {
239*6758Srrh 		fixpos (oldw,i,13,54,-1);
240*6758Srrh 		oldw = i;
241*6758Srrh 	}
242*6758Srrh 
243*6758Srrh 	for (i = 13; i < 19; i++)		/* fix positions 13-18 */
244*6758Srrh 		if (board[i] != oldb[i])  {
245*6758Srrh 			fixpos (oldb[i],board[i],3,1+(i-13)*4,1);
246*6758Srrh 			oldb[i] = board[i];
247*6758Srrh 		}
248*6758Srrh 
249*6758Srrh 	if (board[25] != oldb[25])  {		/* fix white men on bar */
250*6758Srrh 		fixpos (oldb[25],board[25],3,25,1);
251*6758Srrh 		oldb[25] = board[25];
252*6758Srrh 	}
253*6758Srrh 
254*6758Srrh 	for (i = 19; i < 25; i++)		/* fix positions 19-24 */
255*6758Srrh 		if (board[i] != oldb[i])  {
256*6758Srrh 			fixpos (oldb[i],board[i],3,29+(i-19)*4,1);
257*6758Srrh 			oldb[i] = board[i];
258*6758Srrh 		}
259*6758Srrh 
260*6758Srrh 	i = (off[1] < 0? off[1]+15: off[1]);	/* fix red's home */
261*6758Srrh 	if (oldr != i)  {
262*6758Srrh 		fixpos (oldr,i,3,54,1);
263*6758Srrh 		oldr = i;
264*6758Srrh 	}
265*6758Srrh 
266*6758Srrh 	curmove (r,c);				/* return to saved position */
267*6758Srrh 	newpos();
268*6758Srrh 	buflush();
269*6758Srrh }
270*6758Srrh 
271*6758Srrh fixpos (old,new,r,c,inc)
272*6758Srrh int	old, new, r, c, inc;
273*6758Srrh 
274*6758Srrh {
275*6758Srrh 	register int	o, n, nv;
276*6758Srrh 	int		ov, nc;
277*6758Srrh 	char		col;
278*6758Srrh 
279*6758Srrh 	if (old*new >= 0)  {
280*6758Srrh 		ov = abs(old);
281*6758Srrh 		nv = abs(new);
282*6758Srrh 		col = (old+new > 0? 'r': 'w');
283*6758Srrh 		o = (ov-1)/5;
284*6758Srrh 		n = (nv-1)/5;
285*6758Srrh 		if (o == n)  {
286*6758Srrh 			if (o == 2)
287*6758Srrh 				nc = c+2;
288*6758Srrh 			if (o == 1)
289*6758Srrh 				nc = c < 54? c: c+1;
290*6758Srrh 			if (o == 0)
291*6758Srrh 				nc = c < 54? c+1: c;
292*6758Srrh 			if (ov > nv)
293*6758Srrh 				fixcol (r+inc*(nv-n*5),nc,abs(ov-nv),' ',inc);
294*6758Srrh 			else
295*6758Srrh 				fixcol (r+inc*(ov-o*5),nc,abs(ov-nv),col,inc);
296*6758Srrh 			return;
297*6758Srrh 		} else  {
298*6758Srrh 			if (c < 54)  {
299*6758Srrh 				if (o+n == 1)  {
300*6758Srrh 					if (n)  {
301*6758Srrh 						fixcol (r,c,abs(nv-5),col,inc);
302*6758Srrh 						if (ov != 5)
303*6758Srrh 							fixcol (r+inc*ov,c+1,abs(ov-5),col,inc);
304*6758Srrh 					} else  {
305*6758Srrh 						fixcol (r,c,abs(ov-5),' ',inc);
306*6758Srrh 						if (nv != 5)
307*6758Srrh 							fixcol (r+inc*nv,c+1,abs(nv-5),' ',inc);
308*6758Srrh 					}
309*6758Srrh 					return;
310*6758Srrh 				}
311*6758Srrh 				if (n == 2)  {
312*6758Srrh 					if (ov != 10)
313*6758Srrh 						fixcol (r+inc*(ov-5),c,abs(ov-10),col,inc);
314*6758Srrh 					fixcol (r,c+2,abs(nv-10),col,inc);
315*6758Srrh 				} else  {
316*6758Srrh 					if (nv != 10)
317*6758Srrh 						fixcol (r+inc*(nv-5),c,abs(nv-10),' ',inc);
318*6758Srrh 					fixcol (r,c+2,abs(ov-10),' ',inc);
319*6758Srrh 				}
320*6758Srrh 				return;
321*6758Srrh 			}
322*6758Srrh 			if (n > o)  {
323*6758Srrh 				fixcol (r+inc*(ov%5),c+o,abs(5*n-ov),col,inc);
324*6758Srrh 				if (nv != 5*n)
325*6758Srrh 					fixcol (r,c+n,abs(5*n-nv),col,inc);
326*6758Srrh 			} else  {
327*6758Srrh 				fixcol (r+inc*(nv%5),c+n,abs(5*n-nv),' ',inc);
328*6758Srrh 				if (ov != 5*o)
329*6758Srrh 					fixcol (r,c+o,abs(5*o-ov),' ',inc);
330*6758Srrh 			}
331*6758Srrh 			return;
332*6758Srrh 		}
333*6758Srrh 	}
334*6758Srrh 	nv = abs(new);
335*6758Srrh 	fixcol (r,c+1,nv,new > 0? 'r': 'w',inc);
336*6758Srrh 	if (abs(old) <= abs(new))
337*6758Srrh 		return;
338*6758Srrh 	fixcol (r+inc*new,c+1,abs(old+new),' ',inc);
339*6758Srrh }
340*6758Srrh 
341*6758Srrh fixcol (r,c,l,ch,inc)
342*6758Srrh register int	l, ch;
343*6758Srrh int		r, c, inc;
344*6758Srrh 
345*6758Srrh {
346*6758Srrh 	register int	i;
347*6758Srrh 
348*6758Srrh 	curmove (r,c);
349*6758Srrh 	fancyc (ch);
350*6758Srrh 	for (i = 1; i < l; i++)  {
351*6758Srrh 		curmove (curr+inc,curc-1);
352*6758Srrh 		fancyc (ch);
353*6758Srrh 	}
354*6758Srrh }
355*6758Srrh 
356*6758Srrh curmove (r,c)
357*6758Srrh register int	r, c;
358*6758Srrh 
359*6758Srrh {
360*6758Srrh 	if (curr == r && curc == c)
361*6758Srrh 		return;
362*6758Srrh 	if (realr == -1)  {
363*6758Srrh 		realr = curr;
364*6758Srrh 		realc = curc;
365*6758Srrh 	}
366*6758Srrh 	curr = r;
367*6758Srrh 	curc = c;
368*6758Srrh }
369*6758Srrh 
370*6758Srrh newpos ()  {
371*6758Srrh 	register int	r;		/* destination row */
372*6758Srrh 	register int	c;		/* destination column */
373*6758Srrh 	register int	mode = -1;	/* mode of movement */
374*6758Srrh 
375*6758Srrh 	int	count = 1000;		/* character count */
376*6758Srrh 	int	i;			/* index */
377*6758Srrh 	int	j;			/* index */
378*6758Srrh 	int	n;			/* temporary variable */
379*6758Srrh 	char	*m;			/* string containing CM movement */
380*6758Srrh 	int	addbuf();		/* add a char to the output buffer */
381*6758Srrh 
382*6758Srrh 
383*6758Srrh 	if (realr == -1)		/* see if already there */
384*6758Srrh 		return;
385*6758Srrh 
386*6758Srrh 	r = curr;			/* set current and dest. positions */
387*6758Srrh 	c = curc;
388*6758Srrh 	curr = realr;
389*6758Srrh 	curc = realc;
390*6758Srrh 
391*6758Srrh 					/* double check position */
392*6758Srrh 	if (curr == r && curc == c)  {
393*6758Srrh 		realr = realc = -1;
394*6758Srrh 		return;
395*6758Srrh 	}
396*6758Srrh 
397*6758Srrh 	if (CM)  {			/* try CM to get there */
398*6758Srrh 		mode = 0;
399*6758Srrh 		m = tgoto (CM,c,r);
400*6758Srrh 		count = strlen (m);
401*6758Srrh 	}
402*6758Srrh 
403*6758Srrh 					/* try HO and local movement */
404*6758Srrh 	if (HO && (n = r+c*lND+lHO) < count)  {
405*6758Srrh 		mode = 1;
406*6758Srrh 		count = n;
407*6758Srrh 	}
408*6758Srrh 
409*6758Srrh 					/* try various LF combinations */
410*6758Srrh 	if (r >= curr)  {
411*6758Srrh 						/* CR, LF, and ND */
412*6758Srrh 		if ((n = (r-curr)+c*lND+1) < count)  {
413*6758Srrh 			mode = 2;
414*6758Srrh 			count = n;
415*6758Srrh 		}
416*6758Srrh 						/* LF, ND */
417*6758Srrh 		if (c >= curc && (n = (r-curr)+(c-curc)*lND) < count)  {
418*6758Srrh 			mode = 3;
419*6758Srrh 			count = n;
420*6758Srrh 		}
421*6758Srrh 						/* LF, BS */
422*6758Srrh 		if (c < curc && (n = (r-curr)+(curc-c)*lBC) < count)  {
423*6758Srrh 			mode = 4;
424*6758Srrh 			count = n;
425*6758Srrh 		}
426*6758Srrh 	}
427*6758Srrh 
428*6758Srrh 					/* try corresponding UP combinations */
429*6758Srrh 	if (r < curr)  {
430*6758Srrh 						/* CR, UP, and ND */
431*6758Srrh 		if ((n = (curr-r)*lUP+c*lND+1) < count)  {
432*6758Srrh 			mode = 5;
433*6758Srrh 			count = n;
434*6758Srrh 		}
435*6758Srrh 						/* UP and ND */
436*6758Srrh 		if (c >= curc && (n = (curr-r)*lUP+(c-curc)*lND) < count)  {
437*6758Srrh 			mode = 6;
438*6758Srrh 			count = n;
439*6758Srrh 		}
440*6758Srrh 						/* UP and BS */
441*6758Srrh 		if (c < curc && (n = (curr-r)*lUP+(curc-c)*lBC) < count)  {
442*6758Srrh 			mode = 7;
443*6758Srrh 			count = n;
444*6758Srrh 		}
445*6758Srrh 	}
446*6758Srrh 
447*6758Srrh 						/* space over */
448*6758Srrh 	if (curr == r && c > curc && linect[r] < curc && c-curc < count)
449*6758Srrh 		mode = 8;
450*6758Srrh 
451*6758Srrh 	switch (mode)  {
452*6758Srrh 
453*6758Srrh 	case -1:				/* error! */
454*6758Srrh 		write (2,"\r\nInternal cursor error.\r\n",26);
455*6758Srrh 		getout();
456*6758Srrh 
457*6758Srrh 						/* direct cursor motion */
458*6758Srrh 	case  0:
459*6758Srrh 		tputs (m,abs(curr-r),addbuf);
460*6758Srrh 		break;
461*6758Srrh 
462*6758Srrh 						/* relative to "home" */
463*6758Srrh 	case  1:
464*6758Srrh 		tputs (HO,r,addbuf);
465*6758Srrh 		for (i = 0; i < r; i++)
466*6758Srrh 			addbuf ('\012');
467*6758Srrh 		for (i = 0; i < c; i++)
468*6758Srrh 			tputs (ND,1,addbuf);
469*6758Srrh 		break;
470*6758Srrh 
471*6758Srrh 						/* CR and down and over */
472*6758Srrh 	case  2:
473*6758Srrh 		addbuf ('\015');
474*6758Srrh 		for (i = 0; i < r-curr; i++)
475*6758Srrh 			addbuf ('\012');
476*6758Srrh 		for (i = 0; i < c; i++)
477*6758Srrh 			tputs (ND,1,addbuf);
478*6758Srrh 		break;
479*6758Srrh 
480*6758Srrh 						/* down and over */
481*6758Srrh 	case  3:
482*6758Srrh 		for (i = 0; i < r-curr; i++)
483*6758Srrh 			addbuf ('\012');
484*6758Srrh 		for (i = 0; i < c-curc; i++)
485*6758Srrh 			tputs (ND,1,addbuf);
486*6758Srrh 		break;
487*6758Srrh 
488*6758Srrh 						/* down and back */
489*6758Srrh 	case  4:
490*6758Srrh 		for (i = 0; i < r-curr; i++)
491*6758Srrh 			addbuf ('\012');
492*6758Srrh 		for (i = 0; i < curc-c; i++)
493*6758Srrh 			addbuf ('\010');
494*6758Srrh 		break;
495*6758Srrh 
496*6758Srrh 						/* CR and up and over */
497*6758Srrh 	case  5:
498*6758Srrh 		addbuf ('\015');
499*6758Srrh 		for (i = 0; i < curr-r; i++)
500*6758Srrh 			tputs (UP,1,addbuf);
501*6758Srrh 		for (i = 0; i < c; i++)
502*6758Srrh 			tputs (ND,1,addbuf);
503*6758Srrh 		break;
504*6758Srrh 
505*6758Srrh 						/* up and over */
506*6758Srrh 	case  6:
507*6758Srrh 		for (i = 0; i < curr-r; i++)
508*6758Srrh 			tputs (UP,1,addbuf);
509*6758Srrh 		for (i = 0; i < c-curc; i++)
510*6758Srrh 			tputs (ND,1,addbuf);
511*6758Srrh 		break;
512*6758Srrh 
513*6758Srrh 						/* up and back */
514*6758Srrh 	case  7:
515*6758Srrh 		for (i = 0; i < curr-r; i++)
516*6758Srrh 			tputs (UP,1,addbuf);
517*6758Srrh 		for (i = 0; i < curc-c; i++)  {
518*6758Srrh 			if (BC)
519*6758Srrh 				tputs (BC,1,addbuf);
520*6758Srrh 			else
521*6758Srrh 				addbuf ('\010');
522*6758Srrh 		}
523*6758Srrh 		break;
524*6758Srrh 
525*6758Srrh 						/* safe space */
526*6758Srrh 	case  8:
527*6758Srrh 		for (i = 0; i < c-curc; i++)
528*6758Srrh 			addbuf (' ');
529*6758Srrh 	}
530*6758Srrh 
531*6758Srrh 						/* fix positions */
532*6758Srrh 	curr = r;
533*6758Srrh 	curc = c;
534*6758Srrh 	realr = -1;
535*6758Srrh 	realc = -1;
536*6758Srrh }
537*6758Srrh 
538*6758Srrh clear ()  {
539*6758Srrh 	register int	i;
540*6758Srrh 	int		addbuff();
541*6758Srrh 
542*6758Srrh 					/* double space if can't clear */
543*6758Srrh 	if (CL == 0)  {
544*6758Srrh 		writel ("\n\n");
545*6758Srrh 		return;
546*6758Srrh 	}
547*6758Srrh 
548*6758Srrh 	curr = curc = 0;		/* fix position markers */
549*6758Srrh 	realr = realc = -1;
550*6758Srrh 	for (i = 0; i < 24; i++)	/* clear line counts */
551*6758Srrh 		linect[i] = -1;
552*6758Srrh 	buffnum = -1;			/* ignore leftover buffer contents */
553*6758Srrh 	tputs (CL,CO,addbuf);		/* put CL in buffer */
554*6758Srrh }
555*6758Srrh 
556*6758Srrh tos ()  {				/* home cursor */
557*6758Srrh 	curmove (0,0);
558*6758Srrh }
559*6758Srrh 
560*6758Srrh fancyc (c)
561*6758Srrh register char	c;			/* character to output */
562*6758Srrh {
563*6758Srrh 	register int	sp;		/* counts spaces in a tab */
564*6758Srrh 
565*6758Srrh 	if (c == '\007')  {		/* bells go in blindly */
566*6758Srrh 		addbuf (c);
567*6758Srrh 		return;
568*6758Srrh 	}
569*6758Srrh 
570*6758Srrh 					/* process tabs, use spaces if the
571*6758Srrh 					 * the tab should be erasing things,
572*6758Srrh 					 * otherwise use cursor movement
573*6758Srrh 					 * routines.  Note this does not use
574*6758Srrh 					 * hardware tabs at all. */
575*6758Srrh 	if (c == '\t')  {
576*6758Srrh 		sp = (curc+8) & (~ 7);		/* compute spaces */
577*6758Srrh 						/* check line length */
578*6758Srrh 		if (linect[curr] >= curc || sp < 4)  {
579*6758Srrh 			for (; sp > curc; sp--)
580*6758Srrh 				addbuf (' ');
581*6758Srrh 			curc = sp;		/* fix curc */
582*6758Srrh 		} else
583*6758Srrh 			curmove (curr,sp);
584*6758Srrh 		return;
585*6758Srrh 	}
586*6758Srrh 
587*6758Srrh 					/* do newline be calling newline */
588*6758Srrh 	if (c == '\n')  {
589*6758Srrh 		newline();
590*6758Srrh 		return;
591*6758Srrh 	}
592*6758Srrh 
593*6758Srrh 					/* ignore any other control chars */
594*6758Srrh 	if (c < ' ')
595*6758Srrh 		return;
596*6758Srrh 
597*6758Srrh 					/* if an erasing space or non-space,
598*6758Srrh 					 * just add it to buffer.  Otherwise
599*6758Srrh 					 * use cursor movement routine, so that
600*6758Srrh 					 * multiple spaces will be grouped
601*6758Srrh 					 * together */
602*6758Srrh 	if (c > ' ' || linect[curr] >= curc)  {
603*6758Srrh 		newpos ();			/* make sure position correct */
604*6758Srrh 		addbuf (c);			/* add character to buffer */
605*6758Srrh 						/* fix line length */
606*6758Srrh 		if (c == ' ' && linect[curr] == curc)
607*6758Srrh 			linect[curr]--;
608*6758Srrh 		else if (linect[curr] < curc)
609*6758Srrh 			linect[curr] = curc;
610*6758Srrh 		curc++;				/* fix curc */
611*6758Srrh 	} else
612*6758Srrh 					/* use cursor movement routine */
613*6758Srrh 		curmove (curr,curc+1);
614*6758Srrh }
615*6758Srrh 
616*6758Srrh clend()  {
617*6758Srrh 	register int	i;
618*6758Srrh 	register char	*s;
619*6758Srrh 	int		addbuf();
620*6758Srrh 
621*6758Srrh 
622*6758Srrh 	if (CD)  {
623*6758Srrh 		tputs (CD,CO-curr,addbuf);
624*6758Srrh 		for (i = curr; i < LI; i++)
625*6758Srrh 			linect[i] = -1;
626*6758Srrh 		return;
627*6758Srrh 	}
628*6758Srrh 
629*6758Srrh 	curmove (i = curr,0);
630*6758Srrh 	cline();
631*6758Srrh 	while (curr < LI-1)  {
632*6758Srrh 		curmove (curr+1,0);
633*6758Srrh 		if (linect[curr] > -1)
634*6758Srrh 			cline ();
635*6758Srrh 	}
636*6758Srrh 	curmove (i,0);
637*6758Srrh }
638*6758Srrh 
639*6758Srrh cline ()  {
640*6758Srrh 	register int	i;
641*6758Srrh 	register int	c;
642*6758Srrh 	register char	*s;
643*6758Srrh 	int		addbuf();
644*6758Srrh 
645*6758Srrh 	if (curc > linect[curr])
646*6758Srrh 		return;
647*6758Srrh 	newpos ();
648*6758Srrh 	if (CE)  {
649*6758Srrh 		tputs (CE,1,addbuf);
650*6758Srrh 		linect[curr] = curc-1;
651*6758Srrh 	} else  {
652*6758Srrh 		c = curc-1;
653*6758Srrh 		while (linect[curr] > c)  {
654*6758Srrh 			addbuf (' ');
655*6758Srrh 			curc++;
656*6758Srrh 			linect[curr]--;
657*6758Srrh 		}
658*6758Srrh 		curmove (curr,c+1);
659*6758Srrh 	}
660*6758Srrh }
661*6758Srrh 
662*6758Srrh newline ()  {
663*6758Srrh 	cline();
664*6758Srrh 	if (curr == LI-1)
665*6758Srrh 		curmove (begscr,0);
666*6758Srrh 	else
667*6758Srrh 		curmove (curr+1,0);
668*6758Srrh }
669*6758Srrh 
670*6758Srrh getcaps (s)
671*6758Srrh register char	*s;
672*6758Srrh 
673*6758Srrh {
674*6758Srrh 	register char	*code;		/* two letter code */
675*6758Srrh 	register char	***cap;		/* pointer to cap string */
676*6758Srrh 	char		*bufp;		/* pointer to cap buffer */
677*6758Srrh 	char		tentry[1024];	/* temporary uncoded caps buffer */
678*6758Srrh 
679*6758Srrh 	tgetent (tentry,s);		/* get uncoded termcap entry */
680*6758Srrh 
681*6758Srrh 	LI = tgetnum ("li");		/* get number of lines */
682*6758Srrh 	if (LI == -1)
683*6758Srrh 		LI = 12;
684*6758Srrh 	CO = tgetnum ("co");		/* get number of columns */
685*6758Srrh 	if (CO == -1)
686*6758Srrh 		CO = 65;
687*6758Srrh 
688*6758Srrh 	bufp = tbuf;			/* get padding character */
689*6758Srrh 	tgetstr ("pc",&bufp);
690*6758Srrh 	if (bufp != tbuf)
691*6758Srrh 		PC = *tbuf;
692*6758Srrh 	else
693*6758Srrh 		PC = 0;
694*6758Srrh 
695*6758Srrh 	bufp = tbuf;			/* get string entries */
696*6758Srrh 	cap = tstr;
697*6758Srrh 	for (code = tcap; *code; code += 2)
698*6758Srrh 		**cap++ = tgetstr (code,&bufp);
699*6758Srrh 
700*6758Srrh 					/* get pertinent lengths */
701*6758Srrh 	if (HO)
702*6758Srrh 		lHO = strlen (HO);
703*6758Srrh 	if (BC)
704*6758Srrh 		lBC = strlen (BC);
705*6758Srrh 	else
706*6758Srrh 		lBC = 1;
707*6758Srrh 	if (UP)
708*6758Srrh 		lUP = strlen (UP);
709*6758Srrh 	if (ND)
710*6758Srrh 		lND = strlen (ND);
711*6758Srrh 	if (LI < 24 || CO < 72 || !(CL && UP && ND))
712*6758Srrh 		return (0);
713*6758Srrh 	linect = calloc (LI+1,2);
714*6758Srrh 	return (1);
715*6758Srrh }
716