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