]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - adventure/io.c
PR/55693: Andreas Gustafsson: factor(6) lists factors in wrong order
[bsdgames-darwin.git] / adventure / io.c
index 6343d43416ad7db3b484b4dada55c6bd2a21c949..eae7695c5a379e0f3ab64b87c3ae02f2af015eff 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.6 1997/10/11 01:53:29 lukem Exp $     */
+/*     $NetBSD: io.c,v 1.22 2009/08/25 06:56:52 dholland Exp $ */
 
 /*-
  * Copyright (c) 1991, 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.
  *
 #if 0
 static char sccsid[] = "@(#)io.c       8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: io.c,v 1.6 1997/10/11 01:53:29 lukem Exp $");
+__RCSID("$NetBSD: io.c,v 1.22 2009/08/25 06:56:52 dholland Exp $");
 #endif
 #endif /* not lint */
 
 /*      Re-coding of advent in C: file i/o and user i/o                 */
 
+#include <err.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include "hdr.h"
 #include "extern.h"
 
-
+static int next(void);
+static void rdesc(int);
+static void rdefault(void);
+static void rhints(void);
+static void rliq(void);
+static void rlocs(void);
+static int rnum(void);
+static void rtrav(void);
+static void rvoc(void);
+
+/* get command from user        */
+/* no prompt, usually           */
 void
-getin(wrd1, wrd2)              /* get command from user        */
-       char  **wrd1, **wrd2;   /* no prompt, usually           */
+getin(char **wrd1, char **wrd2)
 {
        char   *s;
        static char wd1buf[MAXSTR], wd2buf[MAXSTR];
-       int     first, numch;
+       int     first, numch, c;
 
        *wrd1 = wd1buf;                         /* return ptr to internal str */
        *wrd2 = wd2buf;
        wd2buf[0] = 0;                          /* in case it isn't set here */
        for (s = wd1buf, first = 1, numch = 0;;) {
-               if ((*s = getchar()) >= 'A' && *s <= 'Z')
+               c = getchar();
+               if ((*s = (char)c) >= 'A' && *s <= 'Z')
                        *s = *s - ('A' - 'a');
                /* convert to upper case */
-               switch (*s) {                   /* start reading from user */
+               switch (c) {                    /* start reading from user */
                case '\n':
                        *s = 0;
                        return;
@@ -88,6 +96,9 @@ getin(wrd1, wrd2)             /* get command from user        */
                                *s = 0;
                                return;
                        }
+               case EOF:
+                       printf("user closed input stream, quitting...\n");
+                       exit(0);
                default:
                        if (++numch >= MAXSTR) {        /* string too long */
                                printf("Give me a break!!\n");
@@ -100,33 +111,22 @@ getin(wrd1, wrd2)         /* get command from user        */
        }
 }
 
+/* confirm with rspeak          */
 int
-confirm(mesg)                  /* confirm irreversible action  */
-       char   *mesg;
-{
-       int     result;
-       printf("%s", mesg);     /* tell him what he did         */
-       if (getchar() == 'y')   /* was his first letter a 'y'?  */
-               result = 1;
-       else
-               result = 0;
-       FLUSHLINE;
-       return (result);
-}
-
-int
-yes(x, y, z)                   /* confirm with rspeak          */
-       int     x, y, z;
+yes(int x, int y, int z)
 {
        int     result = TRUE;  /* pacify gcc */
-       char    ch;
+       int    ch;
        for (;;) {
                rspeak(x);      /* tell him what we want */
                if ((ch = getchar()) == 'y')
                        result = TRUE;
-               else
-                       if (ch == 'n')
-                               result = FALSE;
+               else if (ch == 'n')
+                       result = FALSE;
+               else if (ch == EOF) {
+                       printf("user closed input stream, quitting...\n");
+                       exit(0);
+               }
                FLUSHLINE;
                if (ch == 'y' || ch == 'n')
                        break;
@@ -139,19 +139,22 @@ yes(x, y, z)                      /* confirm with rspeak          */
        return (result);
 }
 
+/* confirm with mspeak          */
 int
-yesm(x, y, z)                  /* confirm with mspeak          */
-       int     x, y, z;
+yesm(int x, int y, int z)
 {
        int     result = TRUE;  /* pacify gcc */
-       char    ch;
+       int    ch;
        for (;;) {
                mspeak(x);      /* tell him what we want */
                if ((ch = getchar()) == 'y')
                        result = TRUE;
-               else
-                       if (ch == 'n')
-                               result = FALSE;
+               else if (ch == 'n')
+                       result = FALSE;
+               else if (ch == EOF) {
+                       printf("user closed input stream, quitting...\n");
+                       exit(0);
+               }
                FLUSHLINE;
                if (ch == 'y' || ch == 'n')
                        break;
@@ -165,19 +168,20 @@ yesm(x, y, z)                     /* confirm with mspeak          */
 }
 /* FILE *inbuf,*outbuf; */
 
-char   *inptr;                 /* Pointer into virtual disk    */
+static char *inptr;            /* Pointer into virtual disk    */
 
-int     outsw = 0;             /* putting stuff to data file?  */
+static int outsw = 0;          /* putting stuff to data file?  */
 
-char    iotape[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
-char   *tape = iotape;         /* pointer to encryption tape   */
+static const char    iotape[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
+static const char   *tape = iotape;    /* pointer to encryption tape   */
 
-int
-next()
-{                              /* next virtual char, bump adr  */
+/* next virtual char, bump adr  */
+static int
+next(void)
+{      
        int     ch;
 
-       ch = (*inptr ^ random()) & 0xFF;        /* Decrypt input data           */
+       ch = (*inptr ^ random()) & 0xFF;        /* Decrypt input data  */
        if (outsw) {            /* putting data in tmp file     */
                if (*tape == 0)
                        tape = iotape;  /* rewind encryption tape       */
@@ -187,18 +191,19 @@ next()
        return (ch);
 }
 
-char    breakch;               /* tell which char ended rnum   */
+static char breakch;           /* tell which char ended rnum   */
 
+/* "read" data from virtual file */
 void
-rdata()
-{                              /* "read" data from virtual file */
+rdata(void)
+{      
        int     sect;
        char    ch;
 
        inptr = data_file;      /* Pointer to virtual data file */
        srandom(SEED);          /* which is lightly encrypted.  */
 
-       clsses = 1;
+       classes = 1;
        for (;;) {              /* read data sections           */
                sect = next() - '0';    /* 1st digit of section number  */
 #ifdef VERBOSE
@@ -239,7 +244,7 @@ rdata()
                        rlocs();
                        break;
                case 8: /* action defaults              */
-                       rdflt();
+                       rdefault();
                        break;
                case 9: /* liquid assets                */
                        rliq();
@@ -263,12 +268,12 @@ rdata()
        }
 }
 
-char    nbf[12];
-
+static char nbf[12];
 
-int
-rnum()
-{                              /* read initial location num    */
+/* read initial location num    */
+static int
+rnum(void)
+{      
        char   *s;
        tape = iotape;          /* restart encryption tape      */
        for (s = nbf, *s = 0;; s++)
@@ -281,11 +286,11 @@ rnum()
        return (atoi(nbf));     /* convert it to integer        */
 }
 
-char   *seekhere;
+static char *seekhere;
 
-void
-rdesc(sect)                    /* read description-format msgs */
-       int     sect;
+/* read description-format msgs */
+static void
+rdesc(int sect)
 {
        int     locc;
        char   *seekstart, *maystart;
@@ -294,8 +299,9 @@ rdesc(sect)                 /* read description-format msgs */
        outsw = 1;              /* these msgs go into tmp file  */
        for (oldloc = -1, seekstart = seekhere;;) {
                maystart = inptr;       /* maybe starting new entry     */
-               if ((locc = rnum()) != oldloc && oldloc >= 0    /* finished msg */
-                   && !(sect == 5 && (locc == 0 || locc >= 100))) {    /* unless sect 5 */
+               if ((locc = rnum()) != oldloc && oldloc >= 0 /* finished msg */
+                   /* unless sect 5 */
+                   && !(sect == 5 && (locc == 0 || locc >= 100))) {
                        switch (sect) { /* now put it into right table  */
                        case 1:/* long descriptions            */
                                ltext[oldloc].seekadr = seekhere;
@@ -310,29 +316,24 @@ rdesc(sect)                       /* read description-format msgs */
                                ptext[oldloc].txtlen = maystart - seekstart;
                                break;
                        case 6:/* random messages              */
-                               if (oldloc > RTXSIZ) {
-                                       printf("Too many random msgs\n");
-                                       exit(0);
-                               }
+                               if (oldloc >= RTXSIZE) 
+                                       errx(1,"Too many random msgs");
                                rtext[oldloc].seekadr = seekhere;
                                rtext[oldloc].txtlen = maystart - seekstart;
                                break;
                        case 10:        /* class messages               */
-                               ctext[clsses].seekadr = seekhere;
-                               ctext[clsses].txtlen = maystart - seekstart;
-                               cval[clsses++] = oldloc;
+                               ctext[classes].seekadr = seekhere;
+                               ctext[classes].txtlen = maystart - seekstart;
+                               cval[classes++] = oldloc;
                                break;
                        case 12:        /* magic messages               */
-                               if (oldloc > MAGSIZ) {
-                                       printf("Too many magic msgs\n");
-                                       exit(0);
-                               }
+                               if (oldloc >= MAGSIZE)
+                                       errx(1,"Too many magic msgs");
                                mtext[oldloc].seekadr = seekhere;
                                mtext[oldloc].txtlen = maystart - seekstart;
                                break;
                        default:
-                               printf("rdesc called with bad section\n");
-                               exit(0);
+                               errx(1,"rdesc called with bad section");
                        }
                        seekhere += maystart - seekstart;
                }
@@ -342,7 +343,7 @@ rdesc(sect)                 /* read description-format msgs */
                        return;
                }
                if (sect != 5 || (locc > 0 && locc < 100)) {
-                       if (oldloc != locc)     /* starting a new message       */
+                       if (oldloc != locc)     /* starting a new message */
                                seekstart = maystart;
                        oldloc = locc;
                }
@@ -350,9 +351,10 @@ rdesc(sect)                        /* read description-format msgs */
        }
 }
 
-void
-rtrav()
-{                              /* read travel table            */
+/* read travel table            */
+static void
+rtrav(void)
+{      
        int     locc;
        struct travlist *t = NULL;
        char   *s;
@@ -360,7 +362,8 @@ rtrav()
        int     len, m, n, entries = 0;
 
        for (oldloc = -1;;) {   /* get another line             */
-               if ((locc = rnum()) != oldloc && oldloc >= 0) { /* end of entry */
+               /* end of entry */              
+               if ((locc = rnum()) != oldloc && oldloc >= 0 && t) {
                        t->next = 0;    /* terminate the old entry      */
                        /* printf("%d:%d entries\n",oldloc,entries);       */
                        /* twrite(oldloc);                                 */
@@ -368,7 +371,9 @@ rtrav()
                if (locc == -1)
                        return;
                if (locc != oldloc) {   /* getting a new entry         */
-                       t = travel[locc] = (struct travlist *) malloc(sizeof(struct travlist));
+                       t = travel[locc] = calloc(1, sizeof(*t));
+                       if (t == NULL)
+                               err(1, NULL);
                        /* printf("New travel list for %d\n",locc);        */
                        entries = 0;
                        oldloc = locc;
@@ -384,24 +389,30 @@ rtrav()
                        n = atoi(buf);  /* newloc mod 1000 = newloc     */
                } else {        /* a long integer               */
                        n = atoi(buf + len - 3);
-                       buf[len - 3] = 0;       /* terminate newloc/1000        */
+                       buf[len - 3] = 0;       /* terminate newloc/1000  */
                        m = atoi(buf);
                }
                while (breakch != LF) { /* only do one line at a time   */
-                       if (entries++)
-                               t = t->next = (struct travlist *) malloc(sizeof(struct travlist));
-                       t->tverb = rnum();      /* get verb from the file       */
+                       if (t == NULL)
+                               abort();
+                       if (entries++) {
+                               t->next = calloc(1, sizeof(*t));
+                               if (t->next == NULL)
+                                       err(1, NULL);
+                               t = t->next;
+                       }
+                       t->tverb = rnum();      /* get verb from the file */
                        t->tloc = n;    /* table entry mod 1000         */
-                       t->conditions = m;      /* table entry / 1000           */
-                       /* printf("entry %d for %d\n",entries,locc);       */
+                       t->conditions = m;      /* table entry / 1000   */
+                       /* printf("entry %d for %d\n",entries,locc);    */
                }
        }
 }
 #ifdef DEBUG
 
+/* travel options from this loc */
 void
-twrite(loq)                    /* travel options from this loc */
-       int     loq;
+twrite(int loq)
 {
        struct travlist *t;
        printf("If");
@@ -421,17 +432,18 @@ twrite(loq)                       /* travel options from this loc */
 }
 #endif                         /* DEBUG */
 
-void
-rvoc()
+/* read the vocabulary          */
+static void
+rvoc(void)
 {
-       char   *s;              /* read the vocabulary          */
-       int     index;
+       char   *s;
+       int     idx;
        char    buf[6];
        for (;;) {
-               index = rnum();
-               if (index < 0)
+               idx = rnum();
+               if (idx < 0)
                        break;
-               for (s = buf, *s = 0;; s++)     /* get the word                 */
+               for (s = buf, *s = 0;; s++)     /* get the word  */
                        if ((*s = next()) == TAB || *s == '\n' || *s == LF
                            || *s == ' ')
                                break;
@@ -439,16 +451,16 @@ rvoc()
                if (*s != '\n' && *s != LF)
                        FLUSHLF;/* can be comments    */
                *s = 0;
-               /* printf("\"%s\"=%d\n",buf,index); */
-               vocab(buf, -2, index);
+               /* printf("\"%s\"=%d\n",buf,idx); */
+               vocab(buf, -2, idx);
        }
 /*     prht(); */
 }
 
-
-void
-rlocs()
-{                              /* initial object locations     */
+/* initial object locations     */
+static void
+rlocs(void)
+{      
        for (;;) {
                if ((obj = rnum()) < 0)
                        break;
@@ -460,50 +472,54 @@ rlocs()
        }
 }
 
-void
-rdflt()
-{                              /* default verb messages        */
+/* default verb messages        */
+static void
+rdefault(void)
+{      
        for (;;) {
                if ((verb = rnum()) < 0)
                        break;
-               actspk[verb] = rnum();
+               actspeak[verb] = rnum();
        }
 }
 
-void
-rliq()
-{                              /* liquid assets &c: cond bits  */
+/* liquid assets &c: cond bits  */
+static void
+rliq(void)
+{      
        int     bitnum;
        for (;;) {              /* read new bit list            */
                if ((bitnum = rnum()) < 0)
                        break;
                for (;;) {      /* read locs for bits           */
-                       cond[rnum()] |= setbit[bitnum];
+                       int n = rnum();
+                       if (n < 0)
+                               break;
+                       cond[n] |= setbit[bitnum];
                        if (breakch == LF)
                                break;
                }
        }
 }
 
-void
-rhints()
+static void
+rhints(void)
 {
        int     hintnum, i;
-       hntmax = 0;
+       hintmax = 0;
        for (;;) {
                if ((hintnum = rnum()) < 0)
                        break;
                for (i = 1; i < 5; i++)
                        hints[hintnum][i] = rnum();
-               if (hintnum > hntmax)
-                       hntmax = hintnum;
+               if (hintnum > hintmax)
+                       hintmax = hintnum;
        }
 }
 
 
 void
-rspeak(msg)
-       int     msg;
+rspeak(int msg)
 {
        if (msg != 0)
                speak(&rtext[msg]);
@@ -511,27 +527,25 @@ rspeak(msg)
 
 
 void
-mspeak(msg)
-       int     msg;
+mspeak(int msg)
 {
        if (msg != 0)
                speak(&mtext[msg]);
 }
 
 
+/* read, decrypt, and print a message (not ptext)      */
+/* msg is a pointer to seek address and length of mess */
 void
-speak(msg)                     /* read, decrypt, and print a message (not
-                                * ptext)      */
-       struct text *msg;       /* msg is a pointer to seek address and length
-                                * of mess */
+speak(const struct text *msg)
 {
        char   *s, nonfirst;
 
        s = msg->seekadr;
        nonfirst = 0;
-       while (s - msg->seekadr < msg->txtlen) {        /* read a line at a time */
+       while (s - msg->seekadr < msg->txtlen) { /* read a line at a time */
                tape = iotape;  /* restart decryption tape      */
-               while ((*s++ ^ *tape++) != TAB);        /* read past loc num       */
+               while ((*s++ ^ *tape++) != TAB); /* read past loc num       */
                /* assume tape is longer than location number           */
                /* plus the lookahead put together                    */
                if ((*s ^ *tape) == '>' &&
@@ -544,36 +558,34 @@ speak(msg)                        /* read, decrypt, and print a message (not
                        if (*tape == 0)
                                tape = iotape;  /* rewind decryp tape */
                        putchar(*s ^ *tape);
-               } while ((*s++ ^ *tape++) != LF);       /* better end with LF   */
+               } while ((*s++ ^ *tape++) != LF); /* better end with LF   */
        }
 }
 
-
+/* read, decrypt and print a ptext message  */
+/* msg is the number of all the p msgs for this place  */
+/* assumes object 1 doesn't have prop 1, obj 2 no prop 2 &c */
 void
-pspeak(m, skip)                        /* read, decrypt an print a ptext message              */
-       int     m;              /* msg is the number of all the p msgs for
-                                * this place  */
-       int     skip;           /* assumes object 1 doesn't have prop 1, obj 2
-                                * no prop 2 &c */
+pspeak(int m, int skip)
 {
        char   *s, nonfirst;
-       char   *numst, save;
+       char   *numst;
        struct text *msg;
        char   *tbuf;
 
        msg = &ptext[m];
-       if ((tbuf = (char *) malloc(msg->txtlen + 1)) == 0)
-               bug(108);
+       if ((tbuf = (char *) malloc(msg->txtlen + 1)) == NULL)
+               err(1, NULL);
        memcpy(tbuf, msg->seekadr, msg->txtlen + 1);    /* Room to null */
        s = tbuf;
 
        nonfirst = 0;
        while (s - tbuf < msg->txtlen) {        /* read line at a time */
                tape = iotape;  /* restart decryption tape      */
-               for (numst = s; (*s ^= *tape++) != TAB; s++);   /* get number  */
+               for (numst = s; (*s ^= *tape++) != TAB; s++); /* get number  */
 
-               save = *s;      /* Temporarily trash the string (cringe) */
-               *s++ = 0;       /* decrypting number within the string          */
+                               /* Temporarily trash the string (cringe) */
+               *s++ = 0;       /* decrypting number within the string   */
 
                if (atoi(numst) != 100 * skip && skip >= 0) {
                        while ((*s++ ^ *tape++) != LF)  /* flush the line    */
@@ -590,7 +602,7 @@ pspeak(m, skip)                     /* read, decrypt an print a ptext message              */
                        if (*tape == 0)
                                tape = iotape;
                        putchar(*s ^ *tape);
-               } while ((*s++ ^ *tape++) != LF);       /* better end with LF   */
+               } while ((*s++ ^ *tape++) != LF); /* better end with LF   */
                if (skip < 0)
                        break;
        }