]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/common_source/fancy.c
1 /* $NetBSD: fancy.c,v 1.14 2009/08/12 05:17:57 dholland Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)fancy.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: fancy.c,v 1.14 2009/08/12 05:17:57 dholland Exp $");
43 static void bsect(int, int, int, int);
44 static void fixpos(int, int, int, int, int);
45 static void fixcol(int, int, int, int, int);
46 static void newline(void);
49 * These need to be declared so they come out as commons, because
50 * termcap might or might not define some of them. Our termcap defines
51 * PC, BC, and UP only. This is gross.
53 * XXX: rewrite this crap using curses.
55 char PC
; /* padding character */
56 char *BC
; /* backspace sequence */
57 char *CD
; /* clear to end of screen sequence */
58 char *CE
; /* clear to end of line sequence */
59 char *CL
; /* clear screen sequence */
60 char *CM
; /* cursor movement instructions */
61 char *HO
; /* home cursor sequence */
62 char *MC
; /* column cursor movement map */
63 char *ML
; /* row cursor movement map */
64 char *ND
; /* forward cursor sequence */
65 char *UP
; /* up cursor sequence */
67 static int lHO
; /* length of HO */
68 static int lBC
; /* length of BC */
69 static int lND
; /* length of ND */
70 static int lUP
; /* length of UP */
71 static int CO
; /* number of columns */
72 static int LI
; /* number of lines */
73 static int *linect
; /* array of lengths of lines on screen (the
74 * actual screen is not stored) */
76 /* two letter codes */
77 static char tcap
[] = "bccdceclcmhomcmlndup";
78 /* corresponding strings */
79 static char **tstr
[] = {&BC
, &CD
, &CE
, &CL
, &CM
, &HO
, &MC
, &ML
, &ND
, &UP
};
81 int buffnum
; /* pointer to output buffer */
83 static char tbuf
[1024]; /* buffer for decoded termcap entries */
85 static int oldb
[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 /* "real" cursor positions, so it knows when to reposition. These are -1 if
91 * curr and curc are accurate */
100 curmove(0, 0); /* do top line */
101 for (i
= 0; i
< 53; i
++)
104 curmove(15, 0); /* do botttom line */
105 for (i
= 0; i
< 53; i
++)
108 l
= 1; /* do vertical lines */
109 for (i
= 52; i
> -1; i
-= 28) {
110 curmove((l
== 1 ? 1 : 15), i
);
112 for (j
= 0; j
< 14; j
++) {
113 curmove(curr
+ l
, curc
- 1);
118 l
= -l
; /* alternate directions */
121 curmove(2, 1); /* label positions 13-18 */
122 for (i
= 13; i
< 18; i
++) {
124 fancyc((i
% 10) + '0');
125 curmove(curr
, curc
+ 2);
130 curmove(2, 29); /* label positions 19-24 */
133 for (i
= 20; i
< 25; i
++) {
134 curmove(curr
, curc
+ 2);
136 fancyc((i
% 10) + '0');
139 curmove(14, 1); /* label positions 12-7 */
142 for (i
= 11; i
> 6; i
--) {
143 curmove(curr
, curc
+ 2);
144 fancyc(i
> 9 ? '1' : ' ');
145 fancyc((i
% 10) + '0');
148 curmove(14, 30); /* label positions 6-1 */
150 for (i
= 5; i
> 0; i
--) {
151 curmove(curr
, curc
+ 3);
155 for (i
= 12; i
> 6; i
--)/* print positions 12-7 */
157 bsect(board
[i
], 13, 1 + 4 * (12 - i
), -1);
159 if (board
[0]) /* print red men on bar */
160 bsect(board
[0], 13, 25, -1);
162 for (i
= 6; i
> 0; i
--) /* print positions 6-1 */
164 bsect(board
[i
], 13, 29 + 4 * (6 - i
), -1);
166 l
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* print white's home */
169 curmove(8, 25); /* print the word BAR */
174 for (i
= 13; i
< 19; i
++) /* print positions 13-18 */
176 bsect(board
[i
], 3, 1 + 4 * (i
- 13), 1);
178 if (board
[25]) /* print white's men on bar */
179 bsect(board
[25], 3, 25, 1);
181 for (i
= 19; i
< 25; i
++) /* print positions 19-24 */
183 bsect(board
[i
], 3, 29 + 4 * (i
- 19), 1);
185 l
= (off
[0] < 0 ? off
[0] + 15 : off
[0]); /* print red's home */
186 bsect(-l
, 13, 54, -1);
188 for (i
= 0; i
< 26; i
++)/* save board position for refresh later */
190 oldr
= (off
[1] < 0 ? off
[1] + 15 : off
[1]);
191 oldw
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]);
194 * bsect (b,rpos,cpos,cnext)
195 * Print the contents of a board position. "b" has the value of the
196 * position, "rpos" is the row to start printing, "cpos" is the column to
197 * start printing, and "cnext" is positive if the position starts at the top
198 * and negative if it starts at the bottom. The value of "cpos" is checked
199 * to see if the position is a player's home, since those are printed
203 bsect(int b
, int rpos
, int cpos
, int cnext
)
206 int n
; /* number of men on position */
207 int bct
; /* counter */
209 char pc
; /* color of men on position */
212 n
= abs(b
); /* initialize n and pc */
213 pc
= (b
> 0 ? 'r' : 'w');
215 if (n
< 6 && cpos
< 54) /* position cursor at start */
216 curmove(rpos
, cpos
+ 1);
220 for (j
= 0; j
< 5; j
++) { /* print position row by row */
222 for (k
= 0; k
< 15; k
+= 5) /* print men */
226 if (j
< 4) { /* figure how far to back up for next row */
227 if (n
< 6) { /* stop if none left */
230 bct
= 1; /* single column */
232 if (n
< 11) { /* two columns */
233 if (cpos
== 54) { /* home pos */
239 if (cpos
< 54) { /* not home */
245 } else { /* three columns */
252 /* reposition cursor */
253 curmove(curr
+ cnext
, curc
- bct
);
263 r
= curr
; /* save current position */
266 for (i
= 12; i
> 6; i
--)/* fix positions 12-7 */
267 if (board
[i
] != oldb
[i
]) {
268 fixpos(oldb
[i
], board
[i
], 13, 1 + (12 - i
) * 4, -1);
271 if (board
[0] != oldb
[0]) { /* fix red men on bar */
272 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);
280 i
= -(off
[0] < 0 ? off
[0] + 15 : off
[0]); /* fix white's home */
282 fixpos(oldw
, i
, 13, 54, -1);
285 for (i
= 13; i
< 19; i
++) /* fix positions 13-18 */
286 if (board
[i
] != oldb
[i
]) {
287 fixpos(oldb
[i
], board
[i
], 3, 1 + (i
- 13) * 4, 1);
290 if (board
[25] != oldb
[25]) { /* fix white men on bar */
291 fixpos(oldb
[25], board
[25], 3, 25, 1);
292 oldb
[25] = board
[25];
294 for (i
= 19; i
< 25; i
++) /* fix positions 19-24 */
295 if (board
[i
] != oldb
[i
]) {
296 fixpos(oldb
[i
], board
[i
], 3, 29 + (i
- 19) * 4, 1);
299 i
= (off
[1] < 0 ? off
[1] + 15 : off
[1]); /* fix red's home */
301 fixpos(oldr
, i
, 3, 54, 1);
304 curmove(r
, c
); /* return to saved position */
310 fixpos(int cur
, int new, int r
, int c
, int inc
)
317 if (cur
* new >= 0) {
320 col
= (cur
+ new > 0 ? 'r' : 'w');
327 nc
= c
< 54 ? c
: c
+ 1;
329 nc
= c
< 54 ? c
+ 1 : c
;
331 fixcol(r
+ inc
* (nv
- n
* 5), nc
,
332 abs(ov
- nv
), ' ', inc
);
334 fixcol(r
+ inc
* (ov
- o
* 5), nc
,
335 abs(ov
- nv
), col
, inc
);
341 fixcol(r
, c
, abs(nv
- 5), col
,
348 fixcol(r
, c
, abs(ov
- 5), ' ',
359 fixcol(r
+ inc
* (ov
- 5), c
,
360 abs(ov
- 10), col
, inc
);
361 fixcol(r
, c
+ 2, abs(nv
- 10), col
,
365 fixcol(r
+ inc
* (nv
- 5), c
,
366 abs(nv
- 10), ' ', inc
);
367 fixcol(r
, c
+ 2, abs(ov
- 10), ' ',
373 fixcol(r
+ inc
* (ov
% 5), c
+ o
,
374 abs(5 * n
- ov
), col
, inc
);
376 fixcol(r
, c
+ n
, abs(5 * n
- nv
),
379 fixcol(r
+ inc
* (nv
% 5), c
+ n
,
380 abs(5 * n
- nv
), ' ', inc
);
382 fixcol(r
, c
+ o
, abs(5 * o
- ov
),
389 fixcol(r
, c
+ 1, nv
, new > 0 ? 'r' : 'w', inc
);
390 if (abs(cur
) <= abs(new))
392 fixcol(r
+ inc
* new, c
+ 1, abs(cur
+ new), ' ', inc
);
396 fixcol(int r
, int c
, int l
, int ch
, int inc
)
402 for (i
= 1; i
< l
; i
++) {
403 curmove(curr
+ inc
, curc
- 1);
409 curmove(int r
, int c
)
411 if (curr
== r
&& curc
== c
)
424 int r
; /* destination row */
425 int c
; /* destination column */
426 int mode
= -1; /* mode of movement */
428 int ccount
= 1000; /* character count */
430 int n
; /* temporary variable */
431 char *m
; /* string containing CM movement */
435 if (realr
== -1) /* see if already there */
438 r
= curr
; /* set current and dest. positions */
443 /* double check position */
444 if (curr
== r
&& curc
== c
) {
448 if (CM
) { /* try CM to get there */
450 m
= (char *) tgoto(CM
, c
, r
);
453 /* try HO and local movement */
454 if (HO
&& (n
= r
+ c
* lND
+ lHO
) < ccount
) {
458 /* try various LF combinations */
461 if ((n
= (r
- curr
) + c
* lND
+ 1) < ccount
) {
466 if (c
>= curc
&& (n
= (r
- curr
) + (c
- curc
) * lND
) < ccount
) {
471 if (c
< curc
&& (n
= (r
- curr
) + (curc
- c
) * lBC
) < ccount
) {
476 /* try corresponding UP combinations */
479 if ((n
= (curr
- r
) * lUP
+ c
* lND
+ 1) < ccount
) {
485 (n
= (curr
- r
) * lUP
+ (c
- curc
) * lND
) < ccount
) {
491 (n
= (curr
- r
) * lUP
+ (curc
- c
) * lBC
) < ccount
) {
497 if (curr
== r
&& c
> curc
&& linect
[r
] < curc
&& c
- curc
< ccount
)
502 case -1: /* error! */
503 write(2, "\r\nInternal cursor error.\r\n", 26);
506 /* direct cursor motion */
508 tputs(m
, abs(curr
- r
), addbuf
);
511 /* relative to "home" */
513 tputs(HO
, r
, addbuf
);
514 for (i
= 0; i
< r
; i
++)
516 for (i
= 0; i
< c
; i
++)
517 tputs(ND
, 1, addbuf
);
520 /* CR and down and over */
523 for (i
= 0; i
< r
- curr
; i
++)
525 for (i
= 0; i
< c
; i
++)
526 tputs(ND
, 1, addbuf
);
531 for (i
= 0; i
< r
- curr
; i
++)
533 for (i
= 0; i
< c
- curc
; i
++)
534 tputs(ND
, 1, addbuf
);
539 for (i
= 0; i
< r
- curr
; i
++)
541 for (i
= 0; i
< curc
- c
; i
++)
545 /* CR and up and over */
548 for (i
= 0; i
< curr
- r
; i
++)
549 tputs(UP
, 1, addbuf
);
550 for (i
= 0; i
< c
; i
++)
551 tputs(ND
, 1, addbuf
);
556 for (i
= 0; i
< curr
- r
; i
++)
557 tputs(UP
, 1, addbuf
);
558 for (i
= 0; i
< c
- curc
; i
++)
559 tputs(ND
, 1, addbuf
);
564 for (i
= 0; i
< curr
- r
; i
++)
565 tputs(UP
, 1, addbuf
);
566 for (i
= 0; i
< curc
- c
; i
++) {
568 tputs(BC
, 1, addbuf
);
576 for (i
= 0; i
< c
- curc
; i
++)
592 /* double space if can't clear */
597 curr
= curc
= 0; /* fix position markers */
599 for (i
= 0; i
< 24; i
++)/* clear line counts */
601 buffnum
= -1; /* ignore leftover buffer contents */
602 tputs(CL
, CO
, addbuf
); /* put CL in buffer */
608 int sp
; /* counts spaces in a tab */
610 if (c
== '\007') { /* bells go in blindly */
614 /* process tabs, use spaces if the tab should be erasing things,
615 * otherwise use cursor movement routines. Note this does not use
616 * hardware tabs at all. */
618 sp
= (curc
+ 8) & (~7); /* compute spaces */
619 /* check line length */
620 if (linect
[curr
] >= curc
|| sp
< 4) {
621 for (; sp
> curc
; sp
--)
623 curc
= sp
; /* fix curc */
628 /* do newline be calling newline */
633 /* ignore any other control chars */
637 /* if an erasing space or non-space, just add it to buffer. Otherwise
638 * use cursor movement routine, so that multiple spaces will be
639 * grouped together */
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
)
647 if (linect
[curr
] < curc
)
649 curc
++; /* fix curc */
651 /* use cursor movement routine */
652 curmove(curr
, curc
+ 1);
661 tputs(CD
, CO
- curr
, addbuf
);
662 for (i
= curr
; i
< LI
; i
++)
666 curmove(i
= curr
, 0);
668 while (curr
< LI
- 1) {
669 curmove(curr
+ 1, 0);
670 if (linect
[curr
] > -1)
681 if (curc
> linect
[curr
])
685 tputs(CE
, 1, addbuf
);
686 linect
[curr
] = curc
- 1;
689 while (linect
[curr
] > c
) {
694 curmove(curr
, c
+ 1);
705 curmove(curr
+ 1, 0);
709 getcaps(const char *s
)
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
, 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));
751 if (linect
== NULL
) {
752 write(2, "\r\nOut of memory!\r\n", 18);