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