io.c revision 1.3 1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)io.c 5.8 (Berkeley) 2/28/91";
36 #endif /* not lint */
37
38 # include <curses.h>
39 # include <ctype.h>
40 # include <signal.h>
41 # include <stdarg.h>
42 # include "deck.h"
43 # include "cribbage.h"
44 # include "cribcur.h"
45
46 # define LINESIZE 128
47
48 # ifdef CTRL
49 # undef CTRL
50 # endif
51 # define CTRL(X) ('X' - 'A' + 1)
52
53 # ifdef notdef /* defined in curses.h */
54 # define erasechar() _tty.sg_erase
55 # define killchar() _tty.sg_kill
56 # endif
57
58 char linebuf[ LINESIZE ];
59
60 char *rankname[ RANKS ] = { "ACE", "TWO", "THREE", "FOUR",
61 "FIVE", "SIX", "SEVEN", "EIGHT",
62 "NINE", "TEN", "JACK", "QUEEN",
63 "KING" };
64
65 char *rankchar[ RANKS ] = { "A", "2", "3", "4", "5", "6", "7",
66 "8", "9", "T", "J", "Q", "K" };
67
68 char *suitname[ SUITS ] = { "SPADES", "HEARTS", "DIAMONDS",
69 "CLUBS" };
70
71 char *suitchar[ SUITS ] = { "S", "H", "D", "C" };
72
73
74
75 /*
76 * msgcard:
77 * Call msgcrd in one of two forms
78 */
79 msgcard(c, brief)
80 CARD c;
81 BOOLEAN brief;
82 {
83 if (brief)
84 return msgcrd(c, TRUE, (char *) NULL, TRUE);
85 else
86 return msgcrd(c, FALSE, " of ", FALSE);
87 }
88
89
90
91 /*
92 * msgcrd:
93 * Print the value of a card in ascii
94 */
95 msgcrd(c, brfrank, mid, brfsuit)
96 CARD c;
97 char *mid;
98 BOOLEAN brfrank, brfsuit;
99 {
100 if (c.rank == EMPTY || c.suit == EMPTY)
101 return FALSE;
102 if (brfrank)
103 addmsg("%1.1s", rankchar[c.rank]);
104 else
105 addmsg(rankname[c.rank]);
106 if (mid != NULL)
107 addmsg(mid);
108 if (brfsuit)
109 addmsg("%1.1s", suitchar[c.suit]);
110 else
111 addmsg(suitname[c.suit]);
112 return TRUE;
113 }
114
115 /*
116 * printcard:
117 * Print out a card.
118 */
119 printcard(win, cardno, c, blank)
120 WINDOW *win;
121 int cardno;
122 CARD c;
123 BOOLEAN blank;
124 {
125 prcard(win, cardno * 2, cardno, c, blank);
126 }
127
128 /*
129 * prcard:
130 * Print out a card on the window at the specified location
131 */
132 prcard(win, y, x, c, blank)
133 WINDOW *win;
134 int y, x;
135 CARD c;
136 BOOLEAN blank;
137 {
138 if (c.rank == EMPTY)
139 return;
140 mvwaddstr(win, y + 0, x, "+-----+");
141 mvwaddstr(win, y + 1, x, "| |");
142 mvwaddstr(win, y + 2, x, "| |");
143 mvwaddstr(win, y + 3, x, "| |");
144 mvwaddstr(win, y + 4, x, "+-----+");
145 if (!blank) {
146 mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
147 waddch(win, suitchar[c.suit][0]);
148 mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
149 waddch(win, suitchar[c.suit][0]);
150 }
151 }
152
153 /*
154 * prhand:
155 * Print a hand of n cards
156 */
157 prhand(h, n, win, blank)
158 CARD h[];
159 int n;
160 WINDOW *win;
161 BOOLEAN blank;
162 {
163 register int i;
164
165 werase(win);
166 for (i = 0; i < n; i++)
167 printcard(win, i, *h++, blank);
168 wrefresh(win);
169 }
170
171
172
173 /*
174 * infrom:
175 * reads a card, supposedly in hand, accepting unambigous brief
176 * input, returns the index of the card found...
177 */
178 infrom(hand, n, prompt)
179 CARD hand[];
180 int n;
181 char *prompt;
182 {
183 register int i, j;
184 CARD crd;
185
186 if (n < 1) {
187 printf("\nINFROM: %d = n < 1!!\n", n);
188 exit(74);
189 }
190 for (;;) {
191 msg(prompt);
192 if (incard(&crd)) { /* if card is full card */
193 if (!isone(crd, hand, n))
194 msg("That's not in your hand");
195 else {
196 for (i = 0; i < n; i++)
197 if (hand[i].rank == crd.rank &&
198 hand[i].suit == crd.suit)
199 break;
200 if (i >= n) {
201 printf("\nINFROM: isone or something messed up\n");
202 exit(77);
203 }
204 return i;
205 }
206 }
207 else /* if not full card... */
208 if (crd.rank != EMPTY) {
209 for (i = 0; i < n; i++)
210 if (hand[i].rank == crd.rank)
211 break;
212 if (i >= n)
213 msg("No such rank in your hand");
214 else {
215 for (j = i + 1; j < n; j++)
216 if (hand[j].rank == crd.rank)
217 break;
218 if (j < n)
219 msg("Ambiguous rank");
220 else
221 return i;
222 }
223 }
224 else
225 msg("Sorry, I missed that");
226 }
227 /* NOTREACHED */
228 }
229
230
231
232 /*
233 * incard:
234 * Inputs a card in any format. It reads a line ending with a CR
235 * and then parses it.
236 */
237 incard(crd)
238 CARD *crd;
239 {
240 char *getline();
241 register int i;
242 int rnk, sut;
243 char *line, *p, *p1;
244 BOOLEAN retval;
245
246 retval = FALSE;
247 rnk = sut = EMPTY;
248 if (!(line = getline()))
249 goto gotit;
250 p = p1 = line;
251 while( *p1 != ' ' && *p1 != NULL ) ++p1;
252 *p1++ = NULL;
253 if( *p == NULL ) goto gotit;
254 /* IMPORTANT: no real card has 2 char first name */
255 if( strlen(p) == 2 ) { /* check for short form */
256 rnk = EMPTY;
257 for( i = 0; i < RANKS; i++ ) {
258 if( *p == *rankchar[i] ) {
259 rnk = i;
260 break;
261 }
262 }
263 if( rnk == EMPTY ) goto gotit; /* it's nothing... */
264 ++p; /* advance to next char */
265 sut = EMPTY;
266 for( i = 0; i < SUITS; i++ ) {
267 if( *p == *suitchar[i] ) {
268 sut = i;
269 break;
270 }
271 }
272 if( sut != EMPTY ) retval = TRUE;
273 goto gotit;
274 }
275 rnk = EMPTY;
276 for( i = 0; i < RANKS; i++ ) {
277 if( !strcmp( p, rankname[i] ) || !strcmp( p, rankchar[i] ) ) {
278 rnk = i;
279 break;
280 }
281 }
282 if( rnk == EMPTY ) goto gotit;
283 p = p1;
284 while( *p1 != ' ' && *p1 != NULL ) ++p1;
285 *p1++ = NULL;
286 if( *p == NULL ) goto gotit;
287 if( !strcmp( "OF", p ) ) {
288 p = p1;
289 while( *p1 != ' ' && *p1 != NULL ) ++p1;
290 *p1++ = NULL;
291 if( *p == NULL ) goto gotit;
292 }
293 sut = EMPTY;
294 for( i = 0; i < SUITS; i++ ) {
295 if( !strcmp( p, suitname[i] ) || !strcmp( p, suitchar[i] ) ) {
296 sut = i;
297 break;
298 }
299 }
300 if( sut != EMPTY ) retval = TRUE;
301 gotit:
302 (*crd).rank = rnk;
303 (*crd).suit = sut;
304 return( retval );
305 }
306
307
308
309 /*
310 * getuchar:
311 * Reads and converts to upper case
312 */
313 getuchar()
314 {
315 register int c;
316
317 c = readchar();
318 if (islower(c))
319 c = toupper(c);
320 waddch(Msgwin, c);
321 return c;
322 }
323
324 /*
325 * number:
326 * Reads in a decimal number and makes sure it is between "lo" and
327 * "hi" inclusive.
328 */
329 number(lo, hi, prompt)
330 int lo, hi;
331 char *prompt;
332 {
333 char *getline();
334 register char *p;
335 register int sum;
336
337 sum = 0;
338 for (;;) {
339 msg(prompt);
340 if(!(p = getline()) || *p == NULL) {
341 msg(quiet ? "Not a number" : "That doesn't look like a number");
342 continue;
343 }
344 sum = 0;
345
346 if (!isdigit(*p))
347 sum = lo - 1;
348 else
349 while (isdigit(*p)) {
350 sum = 10 * sum + (*p - '0');
351 ++p;
352 }
353
354 if (*p != ' ' && *p != '\t' && *p != NULL)
355 sum = lo - 1;
356 if (sum >= lo && sum <= hi)
357 return sum;
358 if (sum == lo - 1)
359 msg("that doesn't look like a number, try again --> ");
360 else
361 msg("%d is not between %d and %d inclusive, try again --> ",
362 sum, lo, hi);
363 }
364 }
365
366 /*
367 * msg:
368 * Display a message at the top of the screen.
369 */
370 char Msgbuf[BUFSIZ] = { '\0' };
371
372 int Mpos = 0;
373
374 static int Newpos = 0;
375
376 /* VARARGS1 */
377 msg(fmt,ap)
378 char *fmt;
379 va_list ap;
380 {
381 (void)vsprintf(&Msgbuf[Newpos], fmt, &ap);
382 endmsg();
383 }
384
385 /*
386 * addmsg:
387 * Add things to the current message
388 */
389 /* VARARGS1 */
390 addmsg(fmt,ap)
391 char *fmt;
392 va_list ap;
393 {
394 (void)vsprintf(&Msgbuf[Newpos], fmt, &ap);
395 }
396
397 /*
398 * endmsg:
399 * Display a new msg.
400 */
401
402 int Lineno = 0;
403
404 endmsg()
405 {
406 register int len;
407 register char *mp, *omp;
408 static int lastline = 0;
409
410 /*
411 * All messages should start with uppercase
412 */
413 mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
414 if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
415 Msgbuf[0] = toupper(Msgbuf[0]);
416 mp = Msgbuf;
417 len = strlen(mp);
418 if (len / MSG_X + Lineno >= MSG_Y) {
419 while (Lineno < MSG_Y) {
420 wmove(Msgwin, Lineno++, 0);
421 wclrtoeol(Msgwin);
422 }
423 Lineno = 0;
424 }
425 mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
426 lastline = Lineno;
427 do {
428 mvwaddstr(Msgwin, Lineno, 0, mp);
429 if ((len = strlen(mp)) > MSG_X) {
430 omp = mp;
431 for (mp = &mp[MSG_X-1]; *mp != ' '; mp--)
432 continue;
433 while (*mp == ' ')
434 mp--;
435 mp++;
436 wmove(Msgwin, Lineno, mp - omp);
437 wclrtoeol(Msgwin);
438 }
439 if (++Lineno >= MSG_Y)
440 Lineno = 0;
441 } while (len > MSG_X);
442 wclrtoeol(Msgwin);
443 Mpos = len;
444 Newpos = 0;
445 wrefresh(Msgwin);
446 refresh();
447 wrefresh(Msgwin);
448 }
449
450 #ifdef notdef
451 /*
452 * doadd:
453 * Perform an add onto the message buffer
454 */
455 doadd(fmt, args)
456 char *fmt;
457 int *args;
458 {
459 static FILE junk;
460
461 /*
462 * Do the printf into Msgbuf
463 */
464 junk._flag = _IOWRT + _IOSTRG;
465 junk._ptr = &Msgbuf[Newpos];
466 junk._cnt = 32767;
467 _doprnt(fmt, args, &junk);
468 putc('\0', &junk);
469 Newpos = strlen(Msgbuf);
470 }
471 #endif
472
473 /*
474 * do_wait:
475 * Wait for the user to type ' ' before doing anything else
476 */
477 do_wait()
478 {
479 register int line;
480 static char prompt[] = { '-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0' };
481
482 if (Mpos + sizeof prompt < MSG_X)
483 wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
484 else {
485 mvwaddch(Msgwin, Lineno, 0, ' ');
486 wclrtoeol(Msgwin);
487 if (++Lineno >= MSG_Y)
488 Lineno = 0;
489 }
490 waddstr(Msgwin, prompt);
491 wrefresh(Msgwin);
492 wait_for(' ');
493 }
494
495 /*
496 * wait_for
497 * Sit around until the guy types the right key
498 */
499 wait_for(ch)
500 register char ch;
501 {
502 register char c;
503
504 if (ch == '\n')
505 while ((c = readchar()) != '\n')
506 continue;
507 else
508 while (readchar() != ch)
509 continue;
510 }
511
512 /*
513 * readchar:
514 * Reads and returns a character, checking for gross input errors
515 */
516 readchar()
517 {
518 register int cnt, y, x;
519 auto char c;
520
521 over:
522 cnt = 0;
523 while (read(0, &c, 1) <= 0)
524 if (cnt++ > 100) { /* if we are getting infinite EOFs */
525 bye(); /* quit the game */
526 exit(1);
527 }
528 if (c == CTRL(L)) {
529 wrefresh(curscr);
530 goto over;
531 }
532 if (c == '\r')
533 return '\n';
534 else
535 return c;
536 }
537
538 /*
539 * getline:
540 * Reads the next line up to '\n' or EOF. Multiple spaces are
541 * compressed to one space; a space is inserted before a ','
542 */
543 char *
544 getline()
545 {
546 register char *sp;
547 register int c, oy, ox;
548 register WINDOW *oscr;
549
550 oscr = stdscr;
551 stdscr = Msgwin;
552 getyx(stdscr, oy, ox);
553 refresh();
554 /*
555 * loop reading in the string, and put it in a temporary buffer
556 */
557 for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
558 if (c == -1)
559 continue;
560 else if (c == erasechar()) { /* process erase character */
561 if (sp > linebuf) {
562 register int i;
563
564 sp--;
565 for (i = strlen(unctrl(*sp)); i; i--)
566 addch('\b');
567 }
568 continue;
569 }
570 else if (c == killchar()) { /* process kill character */
571 sp = linebuf;
572 move(oy, ox);
573 continue;
574 }
575 else if (sp == linebuf && c == ' ')
576 continue;
577 if (sp >= &linebuf[LINESIZE-1] || !(isprint(c) || c == ' '))
578 putchar(CTRL(G));
579 else {
580 if (islower(c))
581 c = toupper(c);
582 *sp++ = c;
583 addstr(unctrl(c));
584 Mpos++;
585 }
586 }
587 *sp = '\0';
588 stdscr = oscr;
589 return linebuf;
590 }
591
592 rint()
593 {
594 bye();
595 exit(1);
596 }
597
598 /*
599 * bye:
600 * Leave the program, cleaning things up as we go.
601 */
602 bye()
603 {
604 signal(SIGINT, SIG_IGN);
605 mvcur(0, COLS - 1, LINES - 1, 0);
606 fflush(stdout);
607 endwin();
608 putchar('\n');
609 }
610