]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - backgammon/common_source/subs.c
Fix merge conflicts
[bsdgames-darwin.git] / backgammon / common_source / subs.c
index 9fb185fe5a395d50d487d616edd2977180aa875a..e362e5adf38c8cb78da8675a4c890881d0cd5cc2 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: subs.c,v 1.5 1995/04/29 00:44:15 mycroft Exp $ */
+/*     $NetBSD: subs.c,v 1.20 2013/09/13 20:46:50 joerg Exp $  */
 
 /*
  * Copyright (c) 1980, 1993
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. 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.
  *
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)subs.c     8.1 (Berkeley) 5/31/93";
 #else
-static char rcsid[] = "$NetBSD: subs.c,v 1.5 1995/04/29 00:44:15 mycroft Exp $";
+__RCSID("$NetBSD: subs.c,v 1.20 2013/09/13 20:46:50 joerg Exp $");
 #endif
 #endif /* not lint */
 
-#include <stdio.h>
 #include "back.h"
 
-int    buffnum;
-char   outbuff[BUFSIZ];
-
-static char    plred[] = "Player is red, computer is white.";
-static char    plwhite[] = "Player is white, computer is red.";
-static char    nocomp[] = "(No computer play.)";
-
-char  *descr[] = {
-       "Usage:  backgammon [-] [n r w b pr pw pb t3a]\n",
-       "\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
-       "\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
-       "\tb\ttwo players, red and white (implies n)",
-       "\tpr\tprint the board before red's turn",
-       "\tpw\tprint the board before white's turn",
-       "\tpb\tprint the board before both player's turn",
-       "\tterm\tterminal is a term",
-       "\tsfile\trecover saved game from file",
-       0
-};
-
-errexit (s)
-register char  *s;
-{
-       write (2,"\n",1);
-       perror (s);
-       getout();
-}
+int     buffnum;
+static char outbuff[BUFSIZ];
+
+static const char plred[] = "Player is red, computer is white.";
+static const char plwhite[] = "Player is white, computer is red.";
+static const char nocomp[] = "(No computer play.)";
 
-strset (s1,s2)
-register char  *s1, *s2;
+void
+errexit(const char *s)
 {
-       while ( (*s1++ = *s2++) != '\0');
+       write(2, "\n", 1);
+       perror(s);
+       getout(0);
 }
 
-addbuf (c)
-register char  c;
-
+int
+addbuf(int c)
 {
        buffnum++;
-       if (buffnum == BUFSIZ)  {
-               if (write(1,outbuff,BUFSIZ) != BUFSIZ)
-                       errexit ("addbuf (write):");
+       if (buffnum == BUFSIZ) {
+               if (write(1, outbuff, BUFSIZ) != BUFSIZ)
+                       errexit("addbuf (write):");
                buffnum = 0;
        }
        outbuff[buffnum] = c;
+       return (0);
 }
 
-buflush ()  {
+void
+buflush(void)
+{
        if (buffnum < 0)
                return;
        buffnum++;
-       if (write (1,outbuff,buffnum) != buffnum)
-               errexit ("buflush (write):");
+       if (write(1, outbuff, buffnum) != buffnum)
+               errexit("buflush (write):");
        buffnum = -1;
 }
 
-readc () {
-       char    c;
+int
+readc(void)
+{
+       char    c;
 
-       if (tflag)  {
+       if (tflag) {
                cline();
                newpos();
        }
        buflush();
-       if (read(0,&c,1) != 1)
-               errexit ("readc");
+       if (read(0, &c, 1) != 1)
+               errexit("readc");
 #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
        if (c == '\177')
-               getout();
+               getout(0);
 #endif
        if (c == '\033' || c == '\015')
                return ('\n');
@@ -125,124 +106,128 @@ readc () {
        return (c);
 }
 
-writec (c)
-char   c;
+void
+writec(int c)
 {
        if (tflag)
-               fancyc (c);
+               fancyc(c);
        else
-               addbuf (c);
+               addbuf(c);
 }
 
-writel (l)
-register char  *l;
+void
+writel(const char *l)
 {
 #ifdef DEBUG
-       register char   *s;
+       const char   *s;
 
        if (trace == NULL)
-               trace = fopen ("bgtrace","w");
-       
-       fprintf (trace,"writel: \"");
+               trace = fopen("bgtrace", "w");
+
+       fprintf(trace, "writel: \"");
        for (s = l; *s; s++) {
                if (*s < ' ' || *s == '\177')
-                       fprintf (trace,"^%c",(*s)^0100);
+                       fprintf(trace, "^%c", (*s) ^ 0100);
                else
-                       putc (*s,trace);
+                       putc(*s, trace);
        }
-       fprintf (trace,"\"\n");
-       fflush (trace);
+       fprintf(trace, "\"\n");
+       fflush(trace);
 #endif
 
        while (*l)
-               writec (*l++);
+               writec(*l++);
 }
 
-proll ()   {
-       if (d0)
-               swap;
+void
+proll(struct move *mm)
+{
+       if (mm->d0)
+               mswap(mm);
        if (cturn == 1)
-               writel ("Red's roll:  ");
+               writel("Red's roll:  ");
        else
-               writel ("White's roll:  ");
-       writec (D0+'0');
-       writec ('\040');
-       writec (D1+'0');
+               writel("White's roll:  ");
+       writec(mm->D0 + '0');
+       writec('\040');
+       writec(mm->D1 + '0');
        if (tflag)
                cline();
 }
 
-wrint (n)
-int    n;
+void
+wrint(int n)
 {
-       register int    i, j, t;
+       int     i, j, t;
 
-       for (i = 4; i > 0; i--)  {
+       for (i = 4; i > 0; i--) {
                t = 1;
-               for (j = 0; j<i; j++)
+               for (j = 0; j < i; j++)
                        t *= 10;
-               if (n > t-1)
-                       writec ((n/t)%10+'0');
+               if (n > t - 1)
+                       writec((n / t) % 10 + '0');
        }
-       writec (n%10+'0');
+       writec(n % 10 + '0');
 }
 
-gwrite()  {
-       register int    r, c;
+void
+gwrite(void)
+{
+       int     r, c;
 
-       if (tflag)  {
+       r = c = 0;
+       if (tflag) {
                r = curr;
                c = curc;
-               curmove (16,0);
+               curmove(16, 0);
        }
-
-       if (gvalue > 1)  {
-               writel ("Game value:  ");
-               wrint (gvalue);
-               writel (".  ");
+       if (gvalue > 1) {
+               writel("Game value:  ");
+               wrint(gvalue);
+               writel(".  ");
                if (dlast == -1)
-                       writel (color[0]);
+                       writel(color[0]);
                else
-                       writel (color[1]);
-               writel (" doubled last.");
-       } else  {
-               switch (pnum)  {
-               case -1:                            /* player is red */
-                       writel (plred);
+                       writel(color[1]);
+               writel(" doubled last.");
+       } else {
+               switch (pnum) {
+               case -1:        /* player is red */
+                       writel(plred);
                        break;
-               case 0:                             /* player is both colors */
-                       writel (nocomp);
+               case 0: /* player is both colors */
+                       writel(nocomp);
                        break;
-               case 1:                             /* player is white */
-                       writel (plwhite);
+               case 1: /* player is white */
+                       writel(plwhite);
                }
        }
 
-       if (rscore || wscore)  {
-               writel ("  ");
+       if (rscore || wscore) {
+               writel("  ");
                wrscore();
        }
-
-       if (tflag)  {
+       if (tflag) {
                cline();
-               curmove (r,c);
+               curmove(r, c);
        }
 }
 
-quit ()  {
-       register int    i;
+int
+quit(struct move *mm)
+{
 
-       if (tflag)  {
-               curmove (20,0);
+       if (tflag) {
+               curmove(20, 0);
                clend();
        } else
-               writec ('\n');
-       writel ("Are you sure you want to quit?");
-       if (yorn (0))  {
-               if (rfl)  {
-                       writel ("Would you like to save this game?");
+               writec('\n');
+       writel("Are you sure you want to quit?");
+       if (yorn(0)) {
+               if (rfl) {
+                       writel("Would you like to save this game?");
                        if (yorn(0))
-                               save(0);
+                               save(mm, 0);
                }
                cturn = 0;
                return (1);
@@ -250,52 +235,54 @@ quit ()  {
        return (0);
 }
 
-yorn (special)
-register char  special;                        /* special response */
+int
+yorn(int special)
 {
-       register char   c;
-       register int    i;
+       char    c;
+       int     i;
 
        i = 1;
-       while ( (c = readc()) != 'Y' && c != 'N')  {
+       while ((c = readc()) != 'Y' && c != 'N') {
                if (special && c == special)
                        return (2);
-               if (i)  {
-                       if (special)  {
-                               writel ("  (Y, N, or ");
-                               writec (special);
-                               writec (')');
+               if (i) {
+                       if (special) {
+                               writel("  (Y, N, or ");
+                               writec(special);
+                               writec(')');
                        } else
-                               writel ("  (Y or N)");
+                               writel("  (Y or N)");
                        i = 0;
                } else
-                       writec ('\007');
+                       writec('\007');
        }
        if (c == 'Y')
-               writel ("  Yes.\n");
+               writel("  Yes.\n");
        else
-               writel ("  No.\n");
+               writel("  No.\n");
        if (tflag)
                buflush();
        return (c == 'Y');
 }
 
-wrhit (i)
-register int   i;
+void
+wrhit(int i)
 {
-       writel ("Blot hit on ");
-       wrint (i);
-       writec ('.');
-       writec ('\n');
+       writel("Blot hit on ");
+       wrint(i);
+       writec('.');
+       writec('\n');
 }
 
-nexturn ()  {
-       register int    c;
+void
+nexturn(void)
+{
+       int     c;
 
        cturn = -cturn;
-       c = cturn/abs(cturn);
+       c = cturn / abs(cturn);
        home = bar;
-       bar = 25-bar;
+       bar = 25 - bar;
        offptr += c;
        offopp -= c;
        inptr += c;
@@ -304,20 +291,19 @@ nexturn ()  {
        colorptr += c;
 }
 
-getarg (arg)
-register char  ***arg;
-
+void
+getarg(struct move *mm, char ***arg)
 {
-       register char   **s;
+       char  **s;
 
-       /* process arguments here.  dashes are ignored, nbrw are ignored
-          if the game is being recovered */
+       /* process arguments here.  dashes are ignored, nbrw are ignored if
+        * the game is being recovered */
 
        s = *arg;
-       while (s[0][0] == '-') {
-               switch (s[0][1])  {
+       while (*s && s[0][0] == '-') {
+               switch (s[0][1]) {
 
-               /* don't ask if rules or instructions needed */
+                       /* don't ask if rules or instructions needed */
                case 'n':
                        if (rflag)
                                break;
@@ -325,7 +311,7 @@ register char       ***arg;
                        args[acnt++] = 'n';
                        break;
 
-               /* player is both read and white */
+                       /* player is both red and white */
                case 'b':
                        if (rflag)
                                break;
@@ -334,7 +320,7 @@ register char       ***arg;
                        args[acnt++] = 'b';
                        break;
 
-               /* player is red */
+                       /* player is red */
                case 'r':
                        if (rflag)
                                break;
@@ -343,7 +329,7 @@ register char       ***arg;
                        args[acnt++] = 'r';
                        break;
 
-               /* player is white */
+                       /* player is white */
                case 'w':
                        if (rflag)
                                break;
@@ -352,7 +338,8 @@ register char       ***arg;
                        args[acnt++] = 'w';
                        break;
 
-               /* print board after move according to following character */
+                       /* print board after move according to following
+                        * character */
                case 'p':
                        if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
                                break;
@@ -369,25 +356,32 @@ register char     ***arg;
                case 't':
                        if (s[0][2] == '\0') {  /* get terminal caps */
                                s++;
-                               tflag = getcaps (*s);
+                               tflag = getcaps(*s);
                        } else
-                               tflag = getcaps (&s[0][2]);
+                               tflag = getcaps(&s[0][2]);
                        break;
 
                case 's':
                        s++;
                        /* recover file */
-                       recover (s[0]);
+                       if (s[0] == NULL) {
+                               writel("No save file named\n");
+                               getout(0);
+                       } else
+                               recover(mm, s[0]);
                        break;
                }
                s++;
        }
        if (s[0] != 0)
-               recover(s[0]);
+               recover(mm, s[0]);
 }
 
-init ()  {
-       register int    i;
+void
+init(void)
+{
+       int     i;
+
        for (i = 0; i < 26;)
                board[i++] = 0;
        board[1] = 2;
@@ -402,81 +396,89 @@ init ()  {
        dlast = 0;
 }
 
-wrscore ()  {
-       writel ("Score:  ");
-       writel (color[1]);
-       writec (' ');
-       wrint (rscore);
-       writel (", ");
-       writel (color[0]);
-       writec (' ');
-       wrint (wscore);
+void
+wrscore(void)
+{
+       writel("Score:  ");
+       writel(color[1]);
+       writec(' ');
+       wrint(rscore);
+       writel(", ");
+       writel(color[0]);
+       writec(' ');
+       wrint(wscore);
 }
 
-fixtty (t)
-struct termios *t;
+void
+fixtty(struct termios *t)
 {
        if (tflag)
                newpos();
        buflush();
-       if (tcsetattr (0, TCSADRAIN, t) < 0)
+       if (tcsetattr(0, TCSADRAIN, t) < 0)
                errexit("fixtty");
 }
 
-getout ()  {
+void
+getout(int dummy __unused)
+{
        /* go to bottom of screen */
-       if (tflag)  {
-               curmove (23,0);
+       if (tflag) {
+               curmove(23, 0);
                cline();
        } else
-               writec ('\n');
+               writec('\n');
 
        /* fix terminal status */
-       fixtty (&old);
+       fixtty(&old);
        exit(0);
 }
-roll ()  {
-       register char   c;
-       register int    row;
-       register int    col;
 
-       if (iroll)  {
-               if (tflag)  {
+void
+roll(struct move *mm)
+{
+       char    c;
+       int     row;
+       int     col;
+
+       row = col = 0;
+       if (iroll) {
+               if (tflag) {
                        row = curr;
                        col = curc;
-                       curmove (17,0);
+                       curmove(17, 0);
                } else
-                       writec ('\n');
-               writel ("ROLL: ");
+                       writec('\n');
+               writel("ROLL: ");
                c = readc();
-               if (c != '\n')  {
+               if (c != '\n') {
                        while (c < '1' || c > '6')
                                c = readc();
-                       D0 = c-'0';
-                       writec (' ');
-                       writec (c);
+                       mm->D0 = c - '0';
+                       writec(' ');
+                       writec(c);
                        c = readc();
                        while (c < '1' || c > '6')
                                c = readc();
-                       D1 = c-'0';
-                       writec (' ');
-                       writec (c);
-                       if (tflag)  {
-                               curmove (17,0);
+                       mm->D1 = c - '0';
+                       writec(' ');
+                       writec(c);
+                       if (tflag) {
+                               curmove(17, 0);
                                cline();
-                               curmove (row,col);
+                               curmove(row, col);
                        } else
-                               writec ('\n');
+                               writec('\n');
                        return;
                }
-               if (tflag)  {
-                       curmove (17,0);
+               if (tflag) {
+                       curmove(17, 0);
                        cline();
-                       curmove (row,col);
+                       curmove(row, col);
                } else
-                       writec ('\n');
+                       writec('\n');
        }
-       D0 = rnum(6)+1;
-       D1 = rnum(6)+1;
-       d0 = 0;
+       mm->D0 = rnum(6) + 1;
+       mm->D1 = rnum(6) + 1;
+       mm->d0 = 0;
 }