BSD 4_3_Reno release
[unix-history] / usr / src / games / cribbage / io.c
index 2a3abed..4bb8061 100644 (file)
@@ -1,17 +1,44 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that: (1) source distributions retain this entire copyright
+ * notice and comment, and (2) distributions including binaries display
+ * the following acknowledgement:  ``This product includes software
+ * developed by the University of California, Berkeley and its contributors''
+ * in the documentation or other materials provided with the distribution
+ * and in all advertising materials mentioning features or use of this
+ * software. Neither the name of the University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)io.c       5.6 (Berkeley) 6/1/90";
+#endif /* not lint */
+
 # include      <curses.h>
 # include      <ctype.h>
 # include      <curses.h>
 # include      <ctype.h>
-# include      <unctrl.h>
+# include      <signal.h>
 # include      "deck.h"
 # include      "cribbage.h"
 # include      "deck.h"
 # include      "cribbage.h"
+# include      "cribcur.h"
 
 # define       LINESIZE                128
 
 
 # define       LINESIZE                128
 
+# ifdef CTRL
+# undef CTRL
+# endif
 # define       CTRL(X)                 ('X' - 'A' + 1)
 
 # define       CTRL(X)                 ('X' - 'A' + 1)
 
-# ifndef       attron
+# ifdef        notdef                          /* defined in curses.h */
 #      define  erasechar()     _tty.sg_erase
 #      define  killchar()      _tty.sg_kill
 #      define  erasechar()     _tty.sg_erase
 #      define  killchar()      _tty.sg_kill
-# endif                attron
+# endif
 
 char           linebuf[ LINESIZE ];
 
 
 char           linebuf[ LINESIZE ];
 
@@ -74,22 +101,24 @@ BOOLEAN            brfrank,  brfsuit;
  * printcard:
  *     Print out a card.
  */
  * printcard:
  *     Print out a card.
  */
-printcard(win, cardno, c)
+printcard(win, cardno, c, blank)
 WINDOW         *win;
 int            cardno;
 CARD           c;
 WINDOW         *win;
 int            cardno;
 CARD           c;
+BOOLEAN                blank;
 {
 {
-       prcard(win, cardno * 2, cardno, c);
+       prcard(win, cardno * 2, cardno, c, blank);
 }
 
 /*
  * prcard:
  *     Print out a card on the window at the specified location
  */
 }
 
 /*
  * prcard:
  *     Print out a card on the window at the specified location
  */
-prcard(win, y, x, c)
+prcard(win, y, x, c, blank)
 WINDOW         *win;
 int            y, x;
 CARD           c;
 WINDOW         *win;
 int            y, x;
 CARD           c;
+BOOLEAN                blank;
 {
        if (c.rank == EMPTY)
            return;
 {
        if (c.rank == EMPTY)
            return;
@@ -98,26 +127,29 @@ CARD               c;
        mvwaddstr(win, y + 2, x, "|     |");
        mvwaddstr(win, y + 3, x, "|     |");
        mvwaddstr(win, y + 4, x, "+-----+");
        mvwaddstr(win, y + 2, x, "|     |");
        mvwaddstr(win, y + 3, x, "|     |");
        mvwaddstr(win, y + 4, x, "+-----+");
-       mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
-       waddch(win, suitchar[c.suit][0]);
-       mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
-       waddch(win, suitchar[c.suit][0]);
+       if (!blank) {
+               mvwaddch(win, y + 1, x + 1, rankchar[c.rank][0]);
+               waddch(win, suitchar[c.suit][0]);
+               mvwaddch(win, y + 3, x + 4, rankchar[c.rank][0]);
+               waddch(win, suitchar[c.suit][0]);
+       }
 }
 
 /*
  * prhand:
  *     Print a hand of n cards
  */
 }
 
 /*
  * prhand:
  *     Print a hand of n cards
  */
-prhand(h, n, win)
+prhand(h, n, win, blank)
 CARD           h[];
 int            n;
 WINDOW         *win;
 CARD           h[];
 int            n;
 WINDOW         *win;
+BOOLEAN                blank;
 {
        register int    i;
 
        werase(win);
        for (i = 0; i < n; i++)
 {
        register int    i;
 
        werase(win);
        for (i = 0; i < n; i++)
-           printcard(win, i, h[i]);
+           printcard(win, i, *h++, blank);
        wrefresh(win);
 }
 
        wrefresh(win);
 }
 
@@ -141,6 +173,7 @@ char                *prompt;
            exit(74);
        }
        for (;;) {
            exit(74);
        }
        for (;;) {
+           msg(prompt);
            if (incard(&crd)) {                 /* if card is full card */
                if (!isone(crd, hand, n))
                    msg("That's not in your hand");
            if (incard(&crd)) {                 /* if card is full card */
                if (!isone(crd, hand, n))
                    msg("That's not in your hand");
@@ -175,7 +208,6 @@ char                *prompt;
                }
                else
                    msg("Sorry, I missed that");
                }
                else
                    msg("Sorry, I missed that");
-           msg(prompt);
        }
        /* NOTREACHED */
 }
        }
        /* NOTREACHED */
 }
@@ -270,7 +302,7 @@ getuchar()
        c = readchar();
        if (islower(c))
            c = toupper(c);
        c = readchar();
        if (islower(c))
            c = toupper(c);
-       addch(c);
+       waddch(Msgwin, c);
        return c;
 }
 
        return c;
 }
 
@@ -320,7 +352,7 @@ char                *prompt;
  * msg:
  *     Display a message at the top of the screen.
  */
  * msg:
  *     Display a message at the top of the screen.
  */
-char           Msgbuf[BUFSIZ] = "";
+char           Msgbuf[BUFSIZ] = { '\0' };
 
 int            Mpos = 0;
 
 
 int            Mpos = 0;
 
@@ -331,19 +363,6 @@ msg(fmt, args)
 char   *fmt;
 int    args;
 {
 char   *fmt;
 int    args;
 {
-    /*
-     * if the string is "", just clear the line
-     */
-    if (*fmt == '\0') {
-       move(LINES - 1, 0);
-       clrtoeol();
-       Mpos = 0;
-       Hasread = TRUE;
-       return;
-    }
-    /*
-     * otherwise add to the message and flush it out
-     */
     doadd(fmt, &args);
     endmsg();
 }
     doadd(fmt, &args);
     endmsg();
 }
@@ -362,29 +381,55 @@ int       args;
 
 /*
  * endmsg:
 
 /*
  * endmsg:
- *     Display a new msg (giving him a chance to see the previous one
- *     if it is up there with the --More--)
+ *     Display a new msg.
  */
  */
+
+int    Lineno = 0;
+
 endmsg()
 {
 endmsg()
 {
-    if (!Hasread) {
-       move(LINES - 1, Mpos);
-       addstr("--More--");
-       refresh();
-       wait_for(' ');
-    }
+    register int       len;
+    register char      *mp, *omp;
+    static int         lastline = 0;
+
     /*
     /*
-     * All messages should start with uppercase, except ones that
-     * start with a pack addressing character
+     * All messages should start with uppercase
      */
      */
+    mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
     if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
        Msgbuf[0] = toupper(Msgbuf[0]);
     if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
        Msgbuf[0] = toupper(Msgbuf[0]);
-    mvaddstr(LINES - 1, 0, Msgbuf);
-    clrtoeol();
-    Mpos = Newpos;
+    mp = Msgbuf;
+    len = strlen(mp);
+    if (len / MSG_X + Lineno >= MSG_Y) {
+       while (Lineno < MSG_Y) {
+           wmove(Msgwin, Lineno++, 0);
+           wclrtoeol(Msgwin);
+       }
+       Lineno = 0;
+    }
+    mvaddch(Lineno + Y_MSG_START, SCORE_X, '*');
+    lastline = Lineno;
+    do {
+       mvwaddstr(Msgwin, Lineno, 0, mp);
+       if ((len = strlen(mp)) > MSG_X) {
+           omp = mp;
+           for (mp = &mp[MSG_X-1]; *mp != ' '; mp--)
+               continue;
+           while (*mp == ' ')
+               mp--;
+           mp++;
+           wmove(Msgwin, Lineno, mp - omp);
+           wclrtoeol(Msgwin);
+       }
+       if (++Lineno >= MSG_Y)
+           Lineno = 0;
+    } while (len > MSG_X);
+    wclrtoeol(Msgwin);
+    Mpos = len;
     Newpos = 0;
     Newpos = 0;
+    wrefresh(Msgwin);
     refresh();
     refresh();
-    Hasread = FALSE;
+    wrefresh(Msgwin);
 }
 
 /*
 }
 
 /*
@@ -408,6 +453,28 @@ int        *args;
     Newpos = strlen(Msgbuf);
 }
 
     Newpos = strlen(Msgbuf);
 }
 
+/*
+ * do_wait:
+ *     Wait for the user to type ' ' before doing anything else
+ */
+do_wait()
+{
+    register int line;
+    static char prompt[] = { '-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0' };
+
+    if (Mpos + sizeof prompt < MSG_X)
+       wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
+    else {
+       mvwaddch(Msgwin, Lineno, 0, ' ');
+       wclrtoeol(Msgwin);
+       if (++Lineno >= MSG_Y)
+           Lineno = 0;
+    }
+    waddstr(Msgwin, prompt);
+    wrefresh(Msgwin);
+    wait_for(' ');
+}
+
 /*
  * wait_for
  *     Sit around until the guy types the right key
 /*
  * wait_for
  *     Sit around until the guy types the right key
@@ -437,13 +504,14 @@ readchar()
 over:
     cnt = 0;
     while (read(0, &c, 1) <= 0)
 over:
     cnt = 0;
     while (read(0, &c, 1) <= 0)
-       if (cnt++ > 100)        /* if we are getting infinite EOFs */
-           bye();              /* quit the game */
+       if (cnt++ > 100) {      /* if we are getting infinite EOFs */
+               bye();          /* quit the game */
+               exit(1);
+       }
     if (c == CTRL(L)) {
        wrefresh(curscr);
        goto over;
     }
     if (c == CTRL(L)) {
        wrefresh(curscr);
        goto over;
     }
-    Hasread = TRUE;
     if (c == '\r')
        return '\n';
     else
     if (c == '\r')
        return '\n';
     else
@@ -460,7 +528,10 @@ getline()
 {
     register char      *sp;
     register int       c, oy, ox;
 {
     register char      *sp;
     register int       c, oy, ox;
+    register WINDOW    *oscr;
 
 
+    oscr = stdscr;
+    stdscr = Msgwin;
     getyx(stdscr, oy, ox);
     refresh();
     /*
     getyx(stdscr, oy, ox);
     refresh();
     /*
@@ -493,10 +564,29 @@ getline()
                c = toupper(c);
            *sp++ = c;
            addstr(unctrl(c));
                c = toupper(c);
            *sp++ = c;
            addstr(unctrl(c));
-/*###366 [cc] Mpos undefined %%%*/
            Mpos++;
        }
     }
     *sp = '\0';
            Mpos++;
        }
     }
     *sp = '\0';
+    stdscr = oscr;
     return linebuf;
 }
     return linebuf;
 }
+
+rint()
+{
+       bye();
+       exit(1);
+}
+
+/*
+ * bye:
+ *     Leave the program, cleaning things up as we go.
+ */
+bye()
+{
+       signal(SIGINT, SIG_IGN);
+       mvcur(0, COLS - 1, LINES - 1, 0);
+       fflush(stdout);
+       endwin();
+       putchar('\n');
+}