2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)fancy.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/backgammon/common_source/fancy.c,v 1.7 1999/11/30 03:48:25 billf Exp $";
47 char PC; /* padding character */
48 char *BC; /* backspace sequence */
49 char *CD; /* clear to end of screen sequence */
50 char *CE; /* clear to end of line sequence */
51 char *CL; /* clear screen sequence */
52 char *CM; /* cursor movement instructions */
53 char *HO; /* home cursor sequence */
54 char *MC; /* column cursor movement map */
55 char *ML; /* row cursor movement map */
56 char *ND; /* forward cursor sequence */
57 char *UP; /* up cursor sequence */
59 int lHO; /* length of HO */
60 int lBC; /* length of BC */
61 int lND; /* length of ND */
62 int lUP; /* length of UP */
63 int CO; /* number of columns */
64 int LI; /* number of lines */
65 int *linect; /* array of lengths of lines on screen
66 (the actual screen is not stored) */
68 /* two letter codes */
69 char tcap[] = "bccdceclcmhomcmlndup";
70 /* corresponding strings */
71 char **tstr[] = { &BC, &CD, &CE, &CL, &CM, &HO, &MC, &ML, &ND, &UP };
73 int buffnum; /* pointer to output buffer */
75 char tbuf[1024]; /* buffer for decoded termcap entries */
77 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};
81 /* "real" cursor positions, so
82 * it knows when to reposition.
83 * These are -1 if curr and curc
88 int addbuf __P((int));
93 curmove (0,0); /* do top line */
94 for (i = 0; i < 53; i++)
97 curmove (15,0); /* do botttom line */
98 for (i = 0; i < 53; i++)
101 l = 1; /* do vertical lines */
102 for (i = 52; i > -1; i -= 28) {
103 curmove ( (l == 1? 1: 15) ,i);
105 for (j = 0; j < 14; j++) {
106 curmove (curr+l,curc-1);
111 l = -l; /* alternate directions */
114 curmove (2,1); /* label positions 13-18 */
115 for (i = 13; i < 18; i++) {
117 fancyc ((i % 10)+'0');
118 curmove (curr,curc+2);
123 curmove (2,29); /* label positions 19-24 */
126 for (i = 20; i < 25; i++) {
127 curmove (curr,curc+2);
129 fancyc ((i % 10)+'0');
132 curmove (14,1); /* label positions 12-7 */
135 for (i = 11; i > 6; i--) {
136 curmove (curr,curc+2);
137 fancyc (i > 9? '1': ' ');
138 fancyc ((i % 10)+'0');
141 curmove (14,30); /* label positions 6-1 */
143 for (i = 5; i > 0; i--) {
144 curmove (curr,curc+3);
148 for (i = 12; i > 6; i--) /* print positions 12-7 */
150 bsect (board[i],13,1+4*(12-i),-1);
152 if (board[0]) /* print red men on bar */
153 bsect (board[0],13,25,-1);
155 for (i = 6; i > 0; i--) /* print positions 6-1 */
157 bsect (board[i],13,29+4*(6-i),-1);
159 l = (off[1] < 0? off[1]+15: off[1]); /* print white's home */
162 curmove (8,25); /* print the word BAR */
167 for (i = 13; i < 19; i++) /* print positions 13-18 */
169 bsect (board[i],3,1+4*(i-13),1);
171 if (board[25]) /* print white's men on bar */
172 bsect (board[25],3,25,1);
174 for (i = 19; i < 25; i++) /* print positions 19-24 */
176 bsect (board[i],3,29+4*(i-19),1);
178 l = (off[0] < 0? off[0]+15: off[0]); /* print red's home */
181 for (i = 0; i < 26; i++) /* save board position
182 * for refresh later */
184 oldr = (off[1] < 0? off[1]+15: off[1]);
185 oldw = -(off[0] < 0? off[0]+15: off[0]);
189 * bsect (b,rpos,cpos,cnext)
190 * Print the contents of a board position. "b" has the value of the
191 * position, "rpos" is the row to start printing, "cpos" is the column to
192 * start printing, and "cnext" is positive if the position starts at the top
193 * and negative if it starts at the bottom. The value of "cpos" is checked
194 * to see if the position is a player's home, since those are printed
198 bsect (b,rpos,cpos,cnext)
199 int b; /* contents of position */
200 int rpos; /* row of position */
201 int cpos; /* column of position */
202 int cnext; /* direction of position */
206 int n; /* number of men on position */
207 int bct; /* counter */
209 char pc; /* color of men on position */
211 n = abs(b); /* initialize n and pc */
212 pc = (b > 0? 'r': 'w');
214 if (n < 6 && cpos < 54) /* position cursor at start */
215 curmove (rpos,cpos+1);
219 for (j = 0; j < 5; j++) { /* print position row by row */
221 for (k = 0; k < 15; k += 5) /* print men */
225 if (j < 4) { /* figure how far to
228 if (n < 6) { /* stop if none left */
231 bct = 1; /* single column */
233 if (n < 11) { /* two columns */
234 if (cpos == 54) { /* home pos */
240 if (cpos < 54) { /* not home */
246 } else { /* three columns */
253 curmove (curr+cnext,curc-bct); /* reposition cursor */
261 r = curr; /* save current position */
264 for (i = 12; i > 6; i--) /* fix positions 12-7 */
265 if (board[i] != oldb[i]) {
266 fixpos (oldb[i],board[i],13,1+(12-i)*4,-1);
270 if (board[0] != oldb[0]) { /* fix red men on bar */
271 fixpos (oldb[0],board[0],13,25,-1);
275 for (i = 6; i > 0; i--) /* fix positions 6-1 */
276 if (board[i] != oldb[i]) {
277 fixpos (oldb[i],board[i],13,29+(6-i)*4,-1);
281 i = -(off[0] < 0? off[0]+15: off[0]); /* fix white's home */
283 fixpos (oldw,i,13,54,-1);
287 for (i = 13; i < 19; i++) /* fix positions 13-18 */
288 if (board[i] != oldb[i]) {
289 fixpos (oldb[i],board[i],3,1+(i-13)*4,1);
293 if (board[25] != oldb[25]) { /* fix white men on bar */
294 fixpos (oldb[25],board[25],3,25,1);
295 oldb[25] = board[25];
298 for (i = 19; i < 25; i++) /* fix positions 19-24 */
299 if (board[i] != oldb[i]) {
300 fixpos (oldb[i],board[i],3,29+(i-19)*4,1);
304 i = (off[1] < 0? off[1]+15: off[1]); /* fix red's home */
306 fixpos (oldr,i,3,54,1);
310 curmove (r,c); /* return to saved position */
315 fixpos (old,new,r,c,inc)
316 int old, new, r, c, inc;
326 col = (old+new > 0? 'r': 'w');
337 fixcol (r+inc*(nv-n*5),nc,abs(ov-nv),' ',inc);
339 fixcol (r+inc*(ov-o*5),nc,abs(ov-nv),col,inc);
345 fixcol (r,c,abs(nv-5),col,inc);
347 fixcol (r+inc*ov,c+1,abs(ov-5),col,inc);
349 fixcol (r,c,abs(ov-5),' ',inc);
351 fixcol (r+inc*nv,c+1,abs(nv-5),' ',inc);
357 fixcol (r+inc*(ov-5),c,abs(ov-10),col,inc);
358 fixcol (r,c+2,abs(nv-10),col,inc);
361 fixcol (r+inc*(nv-5),c,abs(nv-10),' ',inc);
362 fixcol (r,c+2,abs(ov-10),' ',inc);
367 fixcol (r+inc*(ov%5),c+o,abs(5*n-ov),col,inc);
369 fixcol (r,c+n,abs(5*n-nv),col,inc);
371 fixcol (r+inc*(nv%5),c+n,abs(5*n-nv),' ',inc);
373 fixcol (r,c+o,abs(5*o-ov),' ',inc);
379 fixcol (r,c+1,nv,new > 0? 'r': 'w',inc);
380 if (abs(old) <= abs(new))
382 fixcol (r+inc*new,c+1,abs(old+new),' ',inc);
385 fixcol (r,c,l,ch,inc)
386 int l, ch, r, c, inc;
393 for (i = 1; i < l; i++) {
394 curmove (curr+inc,curc-1);
403 if (curr == r && curc == c)
414 int r; /* destination row */
415 int c; /* destination column */
416 int mode = -1; /* mode of movement */
418 int count = 1000; /* character count */
421 int n; /* temporary variable */
422 char *m; /* string containing CM movement */
425 if (realr == -1) /* see if already there */
428 r = curr; /* set current and dest. positions */
433 /* double check position */
434 if (curr == r && curc == c) {
439 if (CM) { /* try CM to get there */
441 m = (char *)tgoto (CM,c,r);
445 /* try HO and local movement */
446 if (HO && (n = r+c*lND+lHO) < count) {
451 /* try various LF combinations */
454 if ((n = (r-curr)+c*lND+1) < count) {
459 if (c >= curc && (n = (r-curr)+(c-curc)*lND) < count) {
464 if (c < curc && (n = (r-curr)+(curc-c)*lBC) < count) {
470 /* try corresponding UP combinations */
473 if ((n = (curr-r)*lUP+c*lND+1) < count) {
478 if (c >= curc && (n = (curr-r)*lUP+(c-curc)*lND) < count) {
483 if (c < curc && (n = (curr-r)*lUP+(curc-c)*lBC) < count) {
490 if (curr == r && c > curc && linect[r] < curc && c-curc < count)
495 case -1: /* error! */
496 write (2,"\r\nInternal cursor error.\r\n",26);
499 /* direct cursor motion */
501 tputs (m,abs(curr-r),addbuf);
504 /* relative to "home" */
507 for (i = 0; i < r; i++)
509 for (i = 0; i < c; i++)
513 /* CR and down and over */
516 for (i = 0; i < r-curr; i++)
518 for (i = 0; i < c; i++)
524 for (i = 0; i < r-curr; i++)
526 for (i = 0; i < c-curc; i++)
532 for (i = 0; i < r-curr; i++)
534 for (i = 0; i < curc-c; i++)
538 /* CR and up and over */
541 for (i = 0; i < curr-r; i++)
543 for (i = 0; i < c; i++)
549 for (i = 0; i < curr-r; i++)
551 for (i = 0; i < c-curc; i++)
557 for (i = 0; i < curr-r; i++)
559 for (i = 0; i < curc-c; i++) {
569 for (i = 0; i < c-curc; i++)
583 /* double space if can't clear */
589 curr = curc = 0; /* fix position markers */
591 for (i = 0; i < 24; i++) /* clear line counts */
593 buffnum = -1; /* ignore leftover buffer contents */
594 tputs (CL,CO,addbuf); /* put CL in buffer */
599 char c; /* character to output */
601 int sp; /* counts spaces in a tab */
603 if (c == '\007') { /* bells go in blindly */
608 /* process tabs, use spaces if the
609 * the tab should be erasing things,
610 * otherwise use cursor movement
611 * routines. Note this does not use
612 * hardware tabs at all. */
614 sp = (curc+8) & (~ 7); /* compute spaces */
615 /* check line length */
616 if (linect[curr] >= curc || sp < 4) {
617 for (; sp > curc; sp--)
619 curc = sp; /* fix curc */
625 /* do newline be calling newline */
631 /* ignore any other control chars */
635 /* if an erasing space or non-space,
636 * just add it to buffer. Otherwise
637 * use cursor movement routine, so that
638 * multiple spaces will be grouped
640 if (c > ' ' || linect[curr] >= curc) {
641 newpos (); /* make sure position correct */
642 addbuf (c); /* add character to buffer */
643 /* fix line length */
644 if (c == ' ' && linect[curr] == curc)
646 else if (linect[curr] < curc)
648 curc++; /* fix curc */
650 /* use cursor movement routine */
651 curmove (curr,curc+1);
660 tputs (CD,CO-curr,addbuf);
661 for (i = curr; i < LI; i++)
666 curmove (i = curr,0);
668 while (curr < LI-1) {
670 if (linect[curr] > -1)
681 if (curc > linect[curr])
686 linect[curr] = curc-1;
689 while (linect[curr] > c) {
711 char *code; /* two letter code */
712 char ***cap; /* pointer to cap string */
713 char *bufp; /* pointer to cap buffer */
714 char tentry[1024]; /* temporary uncoded caps buffer */
716 tgetent (tentry, (char *)s); /* get uncoded termcap entry */
718 LI = tgetnum ("li"); /* get number of lines */
721 CO = tgetnum ("co"); /* get number of columns */
725 bufp = tbuf; /* get padding character */
726 tgetstr ("pc",&bufp);
732 bufp = tbuf; /* get string entries */
734 for (code = tcap; *code; code += 2)
735 **cap++ = (char *)tgetstr (code,&bufp);
737 /* get pertinent lengths */
748 if (LI < 24 || CO < 72 || !(CL && UP && ND))
750 linect = (int *)calloc (LI+1,sizeof(int));