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