1*9319Smckusick static char sccsid[] = "	fancy.c	4.2	82/11/22	";
26758Srrh 
36758Srrh #include "back.h"
46758Srrh 
56758Srrh char	PC;			/* padding character */
66758Srrh char	*BC;			/* backspace sequence */
76758Srrh char	*CD;			/* clear to end of screen sequence */
86758Srrh char	*CE;			/* clear to end of line sequence */
96758Srrh char	*CL;			/* clear screen sequence */
106758Srrh char	*CM;			/* cursor movement instructions */
116758Srrh char	*HO;			/* home cursor sequence */
126758Srrh char	*MC;			/* column cursor movement map */
136758Srrh char	*ML;			/* row cursor movement map */
146758Srrh char	*ND;			/* forward cursor sequence */
156758Srrh char	*UP;			/* up cursor sequence */
166758Srrh 
176758Srrh int	lHO;			/* length of HO */
186758Srrh int	lBC;			/* length of BC */
196758Srrh int	lND;			/* length of ND */
206758Srrh int	lUP;			/* length of UP */
216758Srrh int	CO;			/* number of columns */
226758Srrh int	LI;			/* number of lines */
236758Srrh int	*linect;		/* array of lengths of lines on screen
246758Srrh 				   (the actual screen is not stored) */
256758Srrh 
266758Srrh 				/* two letter codes */
276758Srrh char	tcap[] = "bccdceclcmhomcmlndup";
286758Srrh 				/* corresponding strings */
296758Srrh char	**tstr[] = { &BC, &CD, &CE, &CL, &CM, &HO, &MC, &ML, &ND, &UP };
306758Srrh 
316758Srrh int	buffnum;		/* pointer to output buffer */
326758Srrh 
336758Srrh char	tbuf[1024];		/* buffer for decoded termcap entries */
346758Srrh 
356758Srrh 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};
366758Srrh 
376758Srrh int	oldr;
386758Srrh int	oldw;
396758Srrh 						/* "real" cursor positions, so
406758Srrh 						 * it knows when to reposition.
416758Srrh 						 * These are -1 if curr and curc
426758Srrh 						 * are accurate */
436758Srrh int	realr;
446758Srrh int	realc;
456758Srrh 
466758Srrh fboard ()  {
476758Srrh 	register int	i, j, l;
486758Srrh 
496758Srrh 	curmove (0,0);				/* do top line */
506758Srrh 	for (i = 0; i < 53; i++)
516758Srrh 		fancyc ('_');
526758Srrh 
536758Srrh 	curmove (15,0);				/* do botttom line */
546758Srrh 	for (i = 0; i < 53; i++)
556758Srrh 		fancyc ('_');
566758Srrh 
576758Srrh 	l = 1;					/* do vertical lines */
586758Srrh 	for (i = 52; i > -1; i -= 28)  {
596758Srrh 		curmove ( (l == 1? 1: 15) ,i);
606758Srrh 		fancyc ('|');
616758Srrh 		for (j = 0; j < 14; j++)  {
626758Srrh 			curmove (curr+l,curc-1);
636758Srrh 			fancyc ('|');
646758Srrh 		}
656758Srrh 		if (i == 24)
666758Srrh 			i += 32;
676758Srrh 		l = -l;				/* alternate directions */
686758Srrh 	}
696758Srrh 
706758Srrh 	curmove (2,1);				/* label positions 13-18 */
716758Srrh 	for (i = 13; i < 18; i++)  {
726758Srrh 		fancyc ('1');
736758Srrh 		fancyc ((i % 10)+'0');
746758Srrh 		curmove (curr,curc+2);
756758Srrh 	}
766758Srrh 	fancyc ('1');
776758Srrh 	fancyc ('8');
786758Srrh 
796758Srrh 	curmove (2,29);				/* label positions 19-24 */
806758Srrh 	fancyc ('1');
816758Srrh 	fancyc ('9');
826758Srrh 	for (i = 20; i < 25; i++)  {
836758Srrh 		curmove (curr,curc+2);
846758Srrh 		fancyc ('2');
856758Srrh 		fancyc ((i % 10)+'0');
866758Srrh 	}
876758Srrh 
886758Srrh 	curmove (14,1);				/* label positions 12-7 */
896758Srrh 	fancyc ('1');
906758Srrh 	fancyc ('2');
916758Srrh 	for (i = 11; i > 6; i--)  {
926758Srrh 		curmove (curr,curc+2);
936758Srrh 		fancyc (i > 9? '1': ' ');
946758Srrh 		fancyc ((i % 10)+'0');
956758Srrh 	}
966758Srrh 
976758Srrh 	curmove (14,30);			/* label positions 6-1 */
986758Srrh 	fancyc ('6');
996758Srrh 	for (i = 5; i > 0; i--) {
1006758Srrh 		curmove (curr,curc+3);
1016758Srrh 		fancyc (i+'0');
1026758Srrh 	}
1036758Srrh 
1046758Srrh 	for (i = 12; i > 6; i--)		/* print positions 12-7 */
1056758Srrh 		if (board[i])
1066758Srrh 			bsect (board[i],13,1+4*(12-i),-1);
1076758Srrh 
1086758Srrh 	if (board[0])				/* print red men on bar */
1096758Srrh 		bsect (board[0],13,25,-1);
1106758Srrh 
1116758Srrh 	for (i = 6; i > 0; i--)			/* print positions 6-1 */
1126758Srrh 		if (board[i])
1136758Srrh 			bsect (board[i],13,29+4*(6-i),-1);
1146758Srrh 
1156758Srrh 	l = (off[1] < 0? off[1]+15: off[1]);	/* print white's home */
1166758Srrh 	bsect (l,3,54,1);
1176758Srrh 
1186758Srrh 	curmove (8,25);				/* print the word BAR */
1196758Srrh 	fancyc ('B');
1206758Srrh 	fancyc ('A');
1216758Srrh 	fancyc ('R');
1226758Srrh 
1236758Srrh 	for (i = 13; i < 19; i++)		/* print positions 13-18 */
1246758Srrh 		if (board[i])
1256758Srrh 			bsect (board[i],3,1+4*(i-13),1);
1266758Srrh 
1276758Srrh 	if (board[25])				/* print white's men on bar */
1286758Srrh 		bsect (board[25],3,25,1);
1296758Srrh 
1306758Srrh 	for (i = 19; i < 25; i++)		/* print positions 19-24 */
1316758Srrh 		if (board[i])
1326758Srrh 			bsect (board[i],3,29+4*(i-19),1);
1336758Srrh 
1346758Srrh 	l = (off[0] < 0? off[0]+15: off[0]);	/* print red's home */
1356758Srrh 	bsect (-l,13,54,-1);
1366758Srrh 
1376758Srrh 	for (i = 0; i < 26; i++)		/* save board position
1386758Srrh 						 * for refresh later */
1396758Srrh 		oldb[i] = board[i];
1406758Srrh 	oldr = (off[1] < 0? off[1]+15: off[1]);
1416758Srrh 	oldw = -(off[0] < 0? off[0]+15: off[0]);
1426758Srrh }
1436758Srrh 
1446758Srrh /*
1456758Srrh  * bsect (b,rpos,cpos,cnext)
1466758Srrh  *	Print the contents of a board position.  "b" has the value of the
1476758Srrh  * position, "rpos" is the row to start printing, "cpos" is the column to
1486758Srrh  * start printing, and "cnext" is positive if the position starts at the top
1496758Srrh  * and negative if it starts at the bottom.  The value of "cpos" is checked
1506758Srrh  * to see if the position is a player's home, since those are printed
1516758Srrh  * differently.
1526758Srrh  */
1536758Srrh 
1546758Srrh bsect (b,rpos,cpos,cnext)
1556758Srrh int	b;					/* contents of position */
1566758Srrh int	rpos;					/* row of position */
1576758Srrh int	cpos;					/* column of position */
1586758Srrh int	cnext;					/* direction of position */
1596758Srrh 
1606758Srrh {
1616758Srrh 	register int	j;			/* index */
1626758Srrh 	register int	n;			/* number of men on position */
1636758Srrh 	register int	bct;			/* counter */
1646758Srrh 	int		k;			/* index */
1656758Srrh 	char		pc;			/* color of men on position */
1666758Srrh 
1676758Srrh 	n = abs(b);				/* initialize n and pc */
1686758Srrh 	pc = (b > 0? 'r': 'w');
1696758Srrh 
1706758Srrh 	if (n < 6 && cpos < 54)			/* position cursor at start */
1716758Srrh 		curmove (rpos,cpos+1);
1726758Srrh 	else
1736758Srrh 		curmove (rpos,cpos);
1746758Srrh 
1756758Srrh 	for (j = 0; j < 5; j++)  {		/* print position row by row */
1766758Srrh 
1776758Srrh 		for (k = 0; k < 15; k += 5)		/* print men */
1786758Srrh 			if (n > j+k)
1796758Srrh 				fancyc (pc);
1806758Srrh 
1816758Srrh 		if (j < 4)  {				/* figure how far to
1826758Srrh 							 * back up for next
1836758Srrh 							 * row */
1846758Srrh 			if (n < 6)  {			/* stop if none left */
1856758Srrh 				if (j+1 == n)
1866758Srrh 					break;
1876758Srrh 				bct = 1;		/* single column */
1886758Srrh 			} else  {
1896758Srrh 				if (n < 11)  {		/* two columns */
1906758Srrh 					if (cpos == 54)  {	/* home pos */
1916758Srrh 						if (j+5 >= n)
1926758Srrh 							bct = 1;
1936758Srrh 						else
1946758Srrh 							bct = 2;
1956758Srrh 					}
1966758Srrh 					if (cpos < 54)  {	/* not home */
1976758Srrh 						if (j+6 >= n)
1986758Srrh 							bct = 1;
1996758Srrh 						else
2006758Srrh 							bct = 2;
2016758Srrh 					}
2026758Srrh 				} else  {		/* three columns */
2036758Srrh 					if (j+10 >= n)
2046758Srrh 						bct = 2;
2056758Srrh 					else
2066758Srrh 						bct = 3;
2076758Srrh 				}
2086758Srrh 			}
2096758Srrh 			curmove (curr+cnext,curc-bct);	/* reposition cursor */
2106758Srrh 		}
2116758Srrh 	}
2126758Srrh }
2136758Srrh 
2146758Srrh refresh()  {
2156758Srrh 	register int	i, r, c;
2166758Srrh 
2176758Srrh 	r = curr;				/* save current position */
2186758Srrh 	c = curc;
2196758Srrh 
2206758Srrh 	for (i = 12; i > 6; i--)		/* fix positions 12-7 */
2216758Srrh 		if (board[i] != oldb[i])  {
2226758Srrh 			fixpos (oldb[i],board[i],13,1+(12-i)*4,-1);
2236758Srrh 			oldb[i] = board[i];
2246758Srrh 		}
2256758Srrh 
2266758Srrh 	if (board[0] != oldb[0])  {		/* fix red men on bar */
2276758Srrh 		fixpos (oldb[0],board[0],13,25,-1);
2286758Srrh 		oldb[0] = board[0];
2296758Srrh 	}
2306758Srrh 
2316758Srrh 	for (i = 6; i > 0; i--)			/* fix positions 6-1 */
2326758Srrh 		if (board[i] != oldb[i])  {
2336758Srrh 			fixpos (oldb[i],board[i],13,29+(6-i)*4,-1);
2346758Srrh 			oldb[i] = board[i];
2356758Srrh 		}
2366758Srrh 
2376758Srrh 	i = -(off[0] < 0? off[0]+15: off[0]);	/* fix white's home */
2386758Srrh 	if (oldw != i)  {
2396758Srrh 		fixpos (oldw,i,13,54,-1);
2406758Srrh 		oldw = i;
2416758Srrh 	}
2426758Srrh 
2436758Srrh 	for (i = 13; i < 19; i++)		/* fix positions 13-18 */
2446758Srrh 		if (board[i] != oldb[i])  {
2456758Srrh 			fixpos (oldb[i],board[i],3,1+(i-13)*4,1);
2466758Srrh 			oldb[i] = board[i];
2476758Srrh 		}
2486758Srrh 
2496758Srrh 	if (board[25] != oldb[25])  {		/* fix white men on bar */
2506758Srrh 		fixpos (oldb[25],board[25],3,25,1);
2516758Srrh 		oldb[25] = board[25];
2526758Srrh 	}
2536758Srrh 
2546758Srrh 	for (i = 19; i < 25; i++)		/* fix positions 19-24 */
2556758Srrh 		if (board[i] != oldb[i])  {
2566758Srrh 			fixpos (oldb[i],board[i],3,29+(i-19)*4,1);
2576758Srrh 			oldb[i] = board[i];
2586758Srrh 		}
2596758Srrh 
2606758Srrh 	i = (off[1] < 0? off[1]+15: off[1]);	/* fix red's home */
2616758Srrh 	if (oldr != i)  {
2626758Srrh 		fixpos (oldr,i,3,54,1);
2636758Srrh 		oldr = i;
2646758Srrh 	}
2656758Srrh 
2666758Srrh 	curmove (r,c);				/* return to saved position */
2676758Srrh 	newpos();
2686758Srrh 	buflush();
2696758Srrh }
2706758Srrh 
2716758Srrh fixpos (old,new,r,c,inc)
2726758Srrh int	old, new, r, c, inc;
2736758Srrh 
2746758Srrh {
2756758Srrh 	register int	o, n, nv;
2766758Srrh 	int		ov, nc;
2776758Srrh 	char		col;
2786758Srrh 
2796758Srrh 	if (old*new >= 0)  {
2806758Srrh 		ov = abs(old);
2816758Srrh 		nv = abs(new);
2826758Srrh 		col = (old+new > 0? 'r': 'w');
2836758Srrh 		o = (ov-1)/5;
2846758Srrh 		n = (nv-1)/5;
2856758Srrh 		if (o == n)  {
2866758Srrh 			if (o == 2)
2876758Srrh 				nc = c+2;
2886758Srrh 			if (o == 1)
2896758Srrh 				nc = c < 54? c: c+1;
2906758Srrh 			if (o == 0)
2916758Srrh 				nc = c < 54? c+1: c;
2926758Srrh 			if (ov > nv)
2936758Srrh 				fixcol (r+inc*(nv-n*5),nc,abs(ov-nv),' ',inc);
2946758Srrh 			else
2956758Srrh 				fixcol (r+inc*(ov-o*5),nc,abs(ov-nv),col,inc);
2966758Srrh 			return;
2976758Srrh 		} else  {
2986758Srrh 			if (c < 54)  {
2996758Srrh 				if (o+n == 1)  {
3006758Srrh 					if (n)  {
3016758Srrh 						fixcol (r,c,abs(nv-5),col,inc);
3026758Srrh 						if (ov != 5)
3036758Srrh 							fixcol (r+inc*ov,c+1,abs(ov-5),col,inc);
3046758Srrh 					} else  {
3056758Srrh 						fixcol (r,c,abs(ov-5),' ',inc);
3066758Srrh 						if (nv != 5)
3076758Srrh 							fixcol (r+inc*nv,c+1,abs(nv-5),' ',inc);
3086758Srrh 					}
3096758Srrh 					return;
3106758Srrh 				}
3116758Srrh 				if (n == 2)  {
3126758Srrh 					if (ov != 10)
3136758Srrh 						fixcol (r+inc*(ov-5),c,abs(ov-10),col,inc);
3146758Srrh 					fixcol (r,c+2,abs(nv-10),col,inc);
3156758Srrh 				} else  {
3166758Srrh 					if (nv != 10)
3176758Srrh 						fixcol (r+inc*(nv-5),c,abs(nv-10),' ',inc);
3186758Srrh 					fixcol (r,c+2,abs(ov-10),' ',inc);
3196758Srrh 				}
3206758Srrh 				return;
3216758Srrh 			}
3226758Srrh 			if (n > o)  {
3236758Srrh 				fixcol (r+inc*(ov%5),c+o,abs(5*n-ov),col,inc);
3246758Srrh 				if (nv != 5*n)
3256758Srrh 					fixcol (r,c+n,abs(5*n-nv),col,inc);
3266758Srrh 			} else  {
3276758Srrh 				fixcol (r+inc*(nv%5),c+n,abs(5*n-nv),' ',inc);
3286758Srrh 				if (ov != 5*o)
3296758Srrh 					fixcol (r,c+o,abs(5*o-ov),' ',inc);
3306758Srrh 			}
3316758Srrh 			return;
3326758Srrh 		}
3336758Srrh 	}
3346758Srrh 	nv = abs(new);
3356758Srrh 	fixcol (r,c+1,nv,new > 0? 'r': 'w',inc);
3366758Srrh 	if (abs(old) <= abs(new))
3376758Srrh 		return;
3386758Srrh 	fixcol (r+inc*new,c+1,abs(old+new),' ',inc);
3396758Srrh }
3406758Srrh 
3416758Srrh fixcol (r,c,l,ch,inc)
3426758Srrh register int	l, ch;
3436758Srrh int		r, c, inc;
3446758Srrh 
3456758Srrh {
3466758Srrh 	register int	i;
3476758Srrh 
3486758Srrh 	curmove (r,c);
3496758Srrh 	fancyc (ch);
3506758Srrh 	for (i = 1; i < l; i++)  {
3516758Srrh 		curmove (curr+inc,curc-1);
3526758Srrh 		fancyc (ch);
3536758Srrh 	}
3546758Srrh }
3556758Srrh 
3566758Srrh curmove (r,c)
3576758Srrh register int	r, c;
3586758Srrh 
3596758Srrh {
3606758Srrh 	if (curr == r && curc == c)
3616758Srrh 		return;
3626758Srrh 	if (realr == -1)  {
3636758Srrh 		realr = curr;
3646758Srrh 		realc = curc;
3656758Srrh 	}
3666758Srrh 	curr = r;
3676758Srrh 	curc = c;
3686758Srrh }
3696758Srrh 
3706758Srrh newpos ()  {
3716758Srrh 	register int	r;		/* destination row */
3726758Srrh 	register int	c;		/* destination column */
3736758Srrh 	register int	mode = -1;	/* mode of movement */
3746758Srrh 
3756758Srrh 	int	count = 1000;		/* character count */
3766758Srrh 	int	i;			/* index */
3776758Srrh 	int	j;			/* index */
3786758Srrh 	int	n;			/* temporary variable */
3796758Srrh 	char	*m;			/* string containing CM movement */
3806758Srrh 	int	addbuf();		/* add a char to the output buffer */
3816758Srrh 
3826758Srrh 
3836758Srrh 	if (realr == -1)		/* see if already there */
3846758Srrh 		return;
3856758Srrh 
3866758Srrh 	r = curr;			/* set current and dest. positions */
3876758Srrh 	c = curc;
3886758Srrh 	curr = realr;
3896758Srrh 	curc = realc;
3906758Srrh 
3916758Srrh 					/* double check position */
3926758Srrh 	if (curr == r && curc == c)  {
3936758Srrh 		realr = realc = -1;
3946758Srrh 		return;
3956758Srrh 	}
3966758Srrh 
3976758Srrh 	if (CM)  {			/* try CM to get there */
3986758Srrh 		mode = 0;
3996758Srrh 		m = tgoto (CM,c,r);
4006758Srrh 		count = strlen (m);
4016758Srrh 	}
4026758Srrh 
4036758Srrh 					/* try HO and local movement */
4046758Srrh 	if (HO && (n = r+c*lND+lHO) < count)  {
4056758Srrh 		mode = 1;
4066758Srrh 		count = n;
4076758Srrh 	}
4086758Srrh 
4096758Srrh 					/* try various LF combinations */
4106758Srrh 	if (r >= curr)  {
4116758Srrh 						/* CR, LF, and ND */
4126758Srrh 		if ((n = (r-curr)+c*lND+1) < count)  {
4136758Srrh 			mode = 2;
4146758Srrh 			count = n;
4156758Srrh 		}
4166758Srrh 						/* LF, ND */
4176758Srrh 		if (c >= curc && (n = (r-curr)+(c-curc)*lND) < count)  {
4186758Srrh 			mode = 3;
4196758Srrh 			count = n;
4206758Srrh 		}
4216758Srrh 						/* LF, BS */
4226758Srrh 		if (c < curc && (n = (r-curr)+(curc-c)*lBC) < count)  {
4236758Srrh 			mode = 4;
4246758Srrh 			count = n;
4256758Srrh 		}
4266758Srrh 	}
4276758Srrh 
4286758Srrh 					/* try corresponding UP combinations */
4296758Srrh 	if (r < curr)  {
4306758Srrh 						/* CR, UP, and ND */
4316758Srrh 		if ((n = (curr-r)*lUP+c*lND+1) < count)  {
4326758Srrh 			mode = 5;
4336758Srrh 			count = n;
4346758Srrh 		}
4356758Srrh 						/* UP and ND */
4366758Srrh 		if (c >= curc && (n = (curr-r)*lUP+(c-curc)*lND) < count)  {
4376758Srrh 			mode = 6;
4386758Srrh 			count = n;
4396758Srrh 		}
4406758Srrh 						/* UP and BS */
4416758Srrh 		if (c < curc && (n = (curr-r)*lUP+(curc-c)*lBC) < count)  {
4426758Srrh 			mode = 7;
4436758Srrh 			count = n;
4446758Srrh 		}
4456758Srrh 	}
4466758Srrh 
4476758Srrh 						/* space over */
4486758Srrh 	if (curr == r && c > curc && linect[r] < curc && c-curc < count)
4496758Srrh 		mode = 8;
4506758Srrh 
4516758Srrh 	switch (mode)  {
4526758Srrh 
4536758Srrh 	case -1:				/* error! */
4546758Srrh 		write (2,"\r\nInternal cursor error.\r\n",26);
4556758Srrh 		getout();
4566758Srrh 
4576758Srrh 						/* direct cursor motion */
4586758Srrh 	case  0:
4596758Srrh 		tputs (m,abs(curr-r),addbuf);
4606758Srrh 		break;
4616758Srrh 
4626758Srrh 						/* relative to "home" */
4636758Srrh 	case  1:
4646758Srrh 		tputs (HO,r,addbuf);
4656758Srrh 		for (i = 0; i < r; i++)
4666758Srrh 			addbuf ('\012');
4676758Srrh 		for (i = 0; i < c; i++)
4686758Srrh 			tputs (ND,1,addbuf);
4696758Srrh 		break;
4706758Srrh 
4716758Srrh 						/* CR and down and over */
4726758Srrh 	case  2:
4736758Srrh 		addbuf ('\015');
4746758Srrh 		for (i = 0; i < r-curr; i++)
4756758Srrh 			addbuf ('\012');
4766758Srrh 		for (i = 0; i < c; i++)
4776758Srrh 			tputs (ND,1,addbuf);
4786758Srrh 		break;
4796758Srrh 
4806758Srrh 						/* down and over */
4816758Srrh 	case  3:
4826758Srrh 		for (i = 0; i < r-curr; i++)
4836758Srrh 			addbuf ('\012');
4846758Srrh 		for (i = 0; i < c-curc; i++)
4856758Srrh 			tputs (ND,1,addbuf);
4866758Srrh 		break;
4876758Srrh 
4886758Srrh 						/* down and back */
4896758Srrh 	case  4:
4906758Srrh 		for (i = 0; i < r-curr; i++)
4916758Srrh 			addbuf ('\012');
4926758Srrh 		for (i = 0; i < curc-c; i++)
4936758Srrh 			addbuf ('\010');
4946758Srrh 		break;
4956758Srrh 
4966758Srrh 						/* CR and up and over */
4976758Srrh 	case  5:
4986758Srrh 		addbuf ('\015');
4996758Srrh 		for (i = 0; i < curr-r; i++)
5006758Srrh 			tputs (UP,1,addbuf);
5016758Srrh 		for (i = 0; i < c; i++)
5026758Srrh 			tputs (ND,1,addbuf);
5036758Srrh 		break;
5046758Srrh 
5056758Srrh 						/* up and over */
5066758Srrh 	case  6:
5076758Srrh 		for (i = 0; i < curr-r; i++)
5086758Srrh 			tputs (UP,1,addbuf);
5096758Srrh 		for (i = 0; i < c-curc; i++)
5106758Srrh 			tputs (ND,1,addbuf);
5116758Srrh 		break;
5126758Srrh 
5136758Srrh 						/* up and back */
5146758Srrh 	case  7:
5156758Srrh 		for (i = 0; i < curr-r; i++)
5166758Srrh 			tputs (UP,1,addbuf);
5176758Srrh 		for (i = 0; i < curc-c; i++)  {
5186758Srrh 			if (BC)
5196758Srrh 				tputs (BC,1,addbuf);
5206758Srrh 			else
5216758Srrh 				addbuf ('\010');
5226758Srrh 		}
5236758Srrh 		break;
5246758Srrh 
5256758Srrh 						/* safe space */
5266758Srrh 	case  8:
5276758Srrh 		for (i = 0; i < c-curc; i++)
5286758Srrh 			addbuf (' ');
5296758Srrh 	}
5306758Srrh 
5316758Srrh 						/* fix positions */
5326758Srrh 	curr = r;
5336758Srrh 	curc = c;
5346758Srrh 	realr = -1;
5356758Srrh 	realc = -1;
5366758Srrh }
5376758Srrh 
5386758Srrh clear ()  {
5396758Srrh 	register int	i;
5406758Srrh 	int		addbuff();
5416758Srrh 
5426758Srrh 					/* double space if can't clear */
5436758Srrh 	if (CL == 0)  {
5446758Srrh 		writel ("\n\n");
5456758Srrh 		return;
5466758Srrh 	}
5476758Srrh 
5486758Srrh 	curr = curc = 0;		/* fix position markers */
5496758Srrh 	realr = realc = -1;
5506758Srrh 	for (i = 0; i < 24; i++)	/* clear line counts */
5516758Srrh 		linect[i] = -1;
5526758Srrh 	buffnum = -1;			/* ignore leftover buffer contents */
5536758Srrh 	tputs (CL,CO,addbuf);		/* put CL in buffer */
5546758Srrh }
5556758Srrh 
5566758Srrh tos ()  {				/* home cursor */
5576758Srrh 	curmove (0,0);
5586758Srrh }
5596758Srrh 
5606758Srrh fancyc (c)
5616758Srrh register char	c;			/* character to output */
5626758Srrh {
5636758Srrh 	register int	sp;		/* counts spaces in a tab */
5646758Srrh 
5656758Srrh 	if (c == '\007')  {		/* bells go in blindly */
5666758Srrh 		addbuf (c);
5676758Srrh 		return;
5686758Srrh 	}
5696758Srrh 
5706758Srrh 					/* process tabs, use spaces if the
5716758Srrh 					 * the tab should be erasing things,
5726758Srrh 					 * otherwise use cursor movement
5736758Srrh 					 * routines.  Note this does not use
5746758Srrh 					 * hardware tabs at all. */
5756758Srrh 	if (c == '\t')  {
5766758Srrh 		sp = (curc+8) & (~ 7);		/* compute spaces */
5776758Srrh 						/* check line length */
5786758Srrh 		if (linect[curr] >= curc || sp < 4)  {
5796758Srrh 			for (; sp > curc; sp--)
5806758Srrh 				addbuf (' ');
5816758Srrh 			curc = sp;		/* fix curc */
5826758Srrh 		} else
5836758Srrh 			curmove (curr,sp);
5846758Srrh 		return;
5856758Srrh 	}
5866758Srrh 
5876758Srrh 					/* do newline be calling newline */
5886758Srrh 	if (c == '\n')  {
5896758Srrh 		newline();
5906758Srrh 		return;
5916758Srrh 	}
5926758Srrh 
5936758Srrh 					/* ignore any other control chars */
5946758Srrh 	if (c < ' ')
5956758Srrh 		return;
5966758Srrh 
5976758Srrh 					/* if an erasing space or non-space,
5986758Srrh 					 * just add it to buffer.  Otherwise
5996758Srrh 					 * use cursor movement routine, so that
6006758Srrh 					 * multiple spaces will be grouped
6016758Srrh 					 * together */
6026758Srrh 	if (c > ' ' || linect[curr] >= curc)  {
6036758Srrh 		newpos ();			/* make sure position correct */
6046758Srrh 		addbuf (c);			/* add character to buffer */
6056758Srrh 						/* fix line length */
6066758Srrh 		if (c == ' ' && linect[curr] == curc)
6076758Srrh 			linect[curr]--;
6086758Srrh 		else if (linect[curr] < curc)
6096758Srrh 			linect[curr] = curc;
6106758Srrh 		curc++;				/* fix curc */
6116758Srrh 	} else
6126758Srrh 					/* use cursor movement routine */
6136758Srrh 		curmove (curr,curc+1);
6146758Srrh }
6156758Srrh 
6166758Srrh clend()  {
6176758Srrh 	register int	i;
6186758Srrh 	register char	*s;
6196758Srrh 	int		addbuf();
6206758Srrh 
6216758Srrh 
6226758Srrh 	if (CD)  {
6236758Srrh 		tputs (CD,CO-curr,addbuf);
6246758Srrh 		for (i = curr; i < LI; i++)
6256758Srrh 			linect[i] = -1;
6266758Srrh 		return;
6276758Srrh 	}
6286758Srrh 
6296758Srrh 	curmove (i = curr,0);
6306758Srrh 	cline();
6316758Srrh 	while (curr < LI-1)  {
6326758Srrh 		curmove (curr+1,0);
6336758Srrh 		if (linect[curr] > -1)
6346758Srrh 			cline ();
6356758Srrh 	}
6366758Srrh 	curmove (i,0);
6376758Srrh }
6386758Srrh 
6396758Srrh cline ()  {
6406758Srrh 	register int	i;
6416758Srrh 	register int	c;
6426758Srrh 	register char	*s;
6436758Srrh 	int		addbuf();
6446758Srrh 
6456758Srrh 	if (curc > linect[curr])
6466758Srrh 		return;
6476758Srrh 	newpos ();
6486758Srrh 	if (CE)  {
6496758Srrh 		tputs (CE,1,addbuf);
6506758Srrh 		linect[curr] = curc-1;
6516758Srrh 	} else  {
6526758Srrh 		c = curc-1;
6536758Srrh 		while (linect[curr] > c)  {
6546758Srrh 			addbuf (' ');
6556758Srrh 			curc++;
6566758Srrh 			linect[curr]--;
6576758Srrh 		}
6586758Srrh 		curmove (curr,c+1);
6596758Srrh 	}
6606758Srrh }
6616758Srrh 
6626758Srrh newline ()  {
6636758Srrh 	cline();
6646758Srrh 	if (curr == LI-1)
6656758Srrh 		curmove (begscr,0);
6666758Srrh 	else
6676758Srrh 		curmove (curr+1,0);
6686758Srrh }
6696758Srrh 
6706758Srrh getcaps (s)
6716758Srrh register char	*s;
6726758Srrh 
6736758Srrh {
6746758Srrh 	register char	*code;		/* two letter code */
6756758Srrh 	register char	***cap;		/* pointer to cap string */
6766758Srrh 	char		*bufp;		/* pointer to cap buffer */
6776758Srrh 	char		tentry[1024];	/* temporary uncoded caps buffer */
6786758Srrh 
6796758Srrh 	tgetent (tentry,s);		/* get uncoded termcap entry */
6806758Srrh 
6816758Srrh 	LI = tgetnum ("li");		/* get number of lines */
6826758Srrh 	if (LI == -1)
6836758Srrh 		LI = 12;
6846758Srrh 	CO = tgetnum ("co");		/* get number of columns */
6856758Srrh 	if (CO == -1)
6866758Srrh 		CO = 65;
6876758Srrh 
6886758Srrh 	bufp = tbuf;			/* get padding character */
6896758Srrh 	tgetstr ("pc",&bufp);
6906758Srrh 	if (bufp != tbuf)
6916758Srrh 		PC = *tbuf;
6926758Srrh 	else
6936758Srrh 		PC = 0;
6946758Srrh 
6956758Srrh 	bufp = tbuf;			/* get string entries */
6966758Srrh 	cap = tstr;
6976758Srrh 	for (code = tcap; *code; code += 2)
6986758Srrh 		**cap++ = tgetstr (code,&bufp);
6996758Srrh 
7006758Srrh 					/* get pertinent lengths */
7016758Srrh 	if (HO)
7026758Srrh 		lHO = strlen (HO);
7036758Srrh 	if (BC)
7046758Srrh 		lBC = strlen (BC);
7056758Srrh 	else
7066758Srrh 		lBC = 1;
7076758Srrh 	if (UP)
7086758Srrh 		lUP = strlen (UP);
7096758Srrh 	if (ND)
7106758Srrh 		lND = strlen (ND);
7116758Srrh 	if (LI < 24 || CO < 72 || !(CL && UP && ND))
7126758Srrh 		return (0);
713*9319Smckusick 	linect = calloc (LI+1,sizeof(int));
7146758Srrh 	return (1);
7156758Srrh }
716