]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - larn/io.c
A revision of "The purpose of computing is insight, not numbers" by
[bsdgames-darwin.git] / larn / io.c
index 23ba9969b175f5b77c9d0dc952e561238a4f0310..d803fd81412fb80dffcd184a63824f00e2ade56b 100644 (file)
--- a/larn/io.c
+++ b/larn/io.c
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.19 2008/01/28 05:38:54 dholland Exp $ */
+/*     $NetBSD: io.c,v 1.28 2017/01/10 20:41:40 christos Exp $ */
 
 /*
  * io.c                         Larn is copyrighted 1986 by Noah Morgan.
@@ -7,7 +7,7 @@
  * 
  * setupvt100()        Subroutine to set up terminal in correct mode for game
  * clearvt100()        Subroutine to clean up terminal when the game is over
- * lgetchar()          Routine to read in one character from the terminal
+ * ttgetch()           Routine to read in one character from the terminal
  * scbr()                      Function to set cbreak -echo for the terminal
  * sncbr()                     Function to set -cbreak echo for the terminal
  * newgame()           Subroutine to save the initial time and seed rnd()
@@ -51,7 +51,7 @@
  * from [x,1] to current line. cl_dn(x,y)
  * lear screen from [1,y] to end of display. standout(str)
  * rint the string in standout mode. set_score_output()
- * alled when output should be literally printed. * xputchar(ch)
+ * alled when output should be literally printed. * ttputch(ch)
  * rint one character in decoded output buffer. * flush_buf()
  * lush buffer with decoded output. * init_term()
  * erminal initialization -- setup termcap info *      char *tmcapcnv(sd,ss)
@@ -62,7 +62,7 @@
  */
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: io.c,v 1.19 2008/01/28 05:38:54 dholland Exp $");
+__RCSID("$NetBSD: io.c,v 1.28 2017/01/10 20:41:40 christos Exp $");
 #endif /* not lint */
 
 #include "header.h"
@@ -71,9 +71,11 @@ __RCSID("$NetBSD: io.c,v 1.19 2008/01/28 05:38:54 dholland Exp $");
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <termcap.h>
+#include <time.h>
+#include <term.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <ctype.h>
 
 #ifdef TERMIO
 #include <termio.h>
@@ -126,6 +128,9 @@ typedef char   *va_list;
 #define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
 #endif /* NOVARARGS */
 
+static int ttputch(int ch);
+static void flush_buf(void);
+
 #define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
 int             io_outfd; /* output file numbers */
 int             io_infd; /* input file numbers */
@@ -140,7 +145,7 @@ static char     lgetwbuf[LINBUFSIZE];       /* get line (word) buffer */
  *     Attributes off, clear screen, set scrolling region, set tty mode
  */
 void
-setupvt100()
+setupvt100(void)
 {
        clear();
        setscroll();
@@ -153,7 +158,7 @@ setupvt100()
  *     Attributes off, clear screen, unset scrolling region, restore tty mode
  */
 void
-clearvt100()
+clearvt100(void)
 {
        resetscroll();
        clear();
@@ -161,10 +166,10 @@ clearvt100()
 }
 
 /*
- *     lgetchar()      Routine to read in one character from the terminal
+ *     ttgetch()       Routine to read in one character from the terminal
  */
 int
-lgetchar()
+ttgetch(void)
 {
        char            byt;
 #ifdef EXTRA
@@ -181,7 +186,7 @@ lgetchar()
  *     like: system("stty cbreak -echo")
  */
 void
-scbr()
+scbr(void)
 {
        gtty(0, &ttx);
        doraw(ttx);
@@ -194,7 +199,7 @@ scbr()
  *     like: system("stty -cbreak echo")
  */
 void
-sncbr()
+sncbr(void)
 {
        gtty(0, &ttx);
        unraw(ttx);
@@ -205,12 +210,13 @@ sncbr()
  *     newgame()       Subroutine to save the initial time and seed rnd()
  */
 void
-newgame()
+newgame(void)
 {
        long  *p, *pe;
        for (p = c, pe = c + 100; p < pe; *p++ = 0);
        time(&initialtime);
        seedrand(initialtime);
+       srandom(initialtime);
        lcreat((char *) 0);     /* open buffering for output to terminal */
 }
 
@@ -260,8 +266,7 @@ lprintf(const char *fmt, ...)
  *     Returns nothing of value.
  */
 void
-lprint(x)
-       long   x;
+lprint(long x)
 {
        if (lpnt >= lpend)
                lflush();
@@ -280,9 +285,7 @@ lprint(x)
  *     Returns nothing of value
  */
 void
-lwrite(buf, len)
-       char  *buf;
-       int             len;
+lwrite(char *buf, int len)
 {
        char *s;
        u_char *t;
@@ -322,7 +325,7 @@ lwrite(buf, len)
  *  Returns 0 if EOF, otherwise the character
  */
 long 
-lgetc()
+lgetc(void)
 {
        int    i;
        if (ipoint != iepoint)
@@ -354,7 +357,7 @@ lgetc()
  *     Returns the int read
  */
 long 
-larn_lrint()
+larn_lrint(void)
 {
        unsigned long i;
        i = 255 & lgetc();
@@ -373,9 +376,7 @@ larn_lrint()
  *     Returns nothing of value
  */
 void
-lrfill(adr, num)
-       char  *adr;
-       int             num;
+lrfill(char *adr, int num)
 {
        u_char  *pnt;
        int    num2;
@@ -410,7 +411,7 @@ lrfill(adr, num)
  *     Returns pointer to a buffer that contains word.  If EOF, returns a NULL
  */
 char *
-lgetw()
+lgetw(void)
 {
        char  *lgp, cc;
        int    n = LINBUFSIZE, quote = 0;
@@ -438,7 +439,7 @@ lgetw()
  * Returns pointer to a buffer that contains the line.  If EOF, returns NULL
  */
 char *
-lgetl()
+lgetl(void)
 {
        int    i = LINBUFSIZE, ch;
        char  *str = lgetwbuf;
@@ -462,8 +463,7 @@ lgetl()
  *     Returns -1 if error, otherwise the file descriptor opened.
  */
 int
-lcreat(str)
-       char *str;
+lcreat(char *str)
 {
        lflush();
        lpnt = lpbuf;
@@ -488,8 +488,7 @@ lcreat(str)
  *     Returns -1 if error, otherwise the file descriptor opened.
  */
 int
-lopen(str)
-       char           *str;
+lopen(char *str)
 {
        ipoint = iepoint = MAXIBUF;
        if (str == NULL)
@@ -511,8 +510,7 @@ lopen(str)
  *     Returns -1 if error, otherwise the file descriptor opened.
  */
 int
-lappend(str)
-       char           *str;
+lappend(char *str)
 {
        lpnt = lpbuf;
        lpend = lpbuf + BUFBIG;
@@ -532,7 +530,7 @@ lappend(str)
  *     Returns nothing of value.
  */
 void
-lrclose()
+lrclose(void)
 {
        if (io_infd > 0) {
                close(io_infd);
@@ -546,7 +544,7 @@ lrclose()
  *     Returns nothing of value.
  */
 void
-lwclose()
+lwclose(void)
 {
        lflush();
        if (io_outfd > 2) {
@@ -616,8 +614,7 @@ cursor(x, y)
  * cursor(x,y)   Put cursor at specified coordinates staring at [1,1] (termcap)
  */
 void
-cursor(x, y)
-       int             x, y;
+cursor(int x, int y)
 {
        if (lpnt >= lpend)
                lflush();
@@ -632,7 +629,7 @@ cursor(x, y)
  *     Routine to position cursor at beginning of 24th line
  */
 void
-cursors()
+cursors(void)
 {
        cursor(1, 24);
 }
@@ -645,63 +642,32 @@ cursors()
  * obvious meanings.
  */
 
-struct tinfo   *info;
-char           *CM, *CE, *CD, *CL, *SO, *SE, *AL, *DL; /* Termcap capabilities */
-static char    *outbuf = 0;    /* translated output buffer */
-
+static char    *outbuf = 0;     /* translated output buffer */
 /*
  * init_term()         Terminal initialization -- setup termcap info
  */
 void
-init_term()
+init_term(void)
 {
-       char           *term;
-
-       switch (t_getent(&info, term = getenv("TERM"))) {
-       case -1:
-               write(2, "Cannot open termcap file.\n", 26);
-               exit(1);
-       case 0:
-               write(2, "Cannot find entry of ", 21);
-               write(2, term, strlen(term));
-               write(2, " in termcap\n", 12);
-               exit(1);
-       };
-
-       CM = t_agetstr(info, "cm");     /* Cursor motion */
-       CE = t_agetstr(info, "ce");     /* Clear to eoln */
-       CL = t_agetstr(info, "cl");     /* Clear screen */
-
-       /* OPTIONAL */
-       AL = t_agetstr(info, "al");     /* Insert line */
-       DL = t_agetstr(info, "dl");     /* Delete line */
-       SO = t_agetstr(info, "so");     /* Begin standout mode */
-       SE = t_agetstr(info, "se");     /* End standout mode */
-       CD = t_agetstr(info, "cd");     /* Clear to end of display */
-
-       if (!CM) {              /* can't find cursor motion entry */
-               write(2, "Sorry, for a ", 13);
-               write(2, term, strlen(term));
-               write(2, ", I can't find the cursor motion entry in termcap\n", 50);
+       setupterm(NULL, 0, NULL); /* will exit if invalid term */
+       if (!cursor_address) {
+               fprintf(stderr, "term does not have cursor_address.\n");
                exit(1);
        }
-       if (!CE) {              /* can't find clear to end of line entry */
-               write(2, "Sorry, for a ", 13);
-               write(2, term, strlen(term));
-               write(2, ", I can't find the clear to end of line entry in termcap\n", 57);
+       if (!clr_eol) {
+               fprintf(stderr, "term does not have clr_eol.\n");
                exit(1);
        }
-       if (!CL) {              /* can't find clear entire screen entry */
-               write(2, "Sorry, for a ", 13);
-               write(2, term, strlen(term));
-               write(2, ", I can't find the clear entire screen entry in termcap\n", 56);
+       if (!clear_screen) {
+               fprintf(stderr, "term does not have clear_screen.\n");
                exit(1);
        }
-       if ((outbuf = malloc(BUFBIG + 16)) == 0) {      /* get memory for
+       if ((outbuf = malloc(BUFBIG + 16)) == 0) {      /* get memory for
                                                         * decoded output buffer */
-               write(2, "Error malloc'ing memory for decoded output buffer\n", 50);
-               died(-285);     /* malloc() failure */
+           fprintf(stderr, "Error malloc'ing memory for decoded output buffer\n");
+           died(-285);     /* malloc() failure */
        }
+
 }
 #endif /* VT100 */
 
@@ -709,8 +675,7 @@ init_term()
  * cl_line(x,y)  Clear the whole line indicated by 'y' and leave cursor at [x,y]
  */
 void
-cl_line(x, y)
-       int             x, y;
+cl_line(int x, int y)
 {
 #ifdef VT100
        cursor(x, y);
@@ -726,8 +691,7 @@ cl_line(x, y)
  * cl_up(x,y) Clear screen from [x,1] to current position. Leave cursor at [x,y]
  */
 void
-cl_up(x, y)
-       int    x, y;
+cl_up(int x, int y)
 {
 #ifdef VT100
        cursor(x, y);
@@ -747,8 +711,7 @@ cl_up(x, y)
  * cl_dn(x,y)  Clear screen from [1,y] to end of display. Leave cursor at [x,y]
  */
 void
-cl_dn(x, y)
-       int    x, y;
+cl_dn(int x, int y)
 {
 #ifdef VT100
        cursor(x, y);
@@ -756,7 +719,7 @@ cl_dn(x, y)
 #else  /* VT100 */
        int    i;
        cursor(1, y);
-       if (!CD) {
+       if (!clr_eos) {
                *lpnt++ = CL_LINE;
                for (i = y; i <= 24; i++) {
                        *lpnt++ = CL_LINE;
@@ -793,7 +756,7 @@ standout(const char *str)
  * set_score_output()  Called when output should be literally printed.
  */
 void
-set_score_output()
+set_score_output(void)
 {
        enable_scroll = -1;
 }
@@ -809,13 +772,12 @@ set_score_output()
 static int      scrline = 18;  /* line # for wraparound instead of scrolling
                                 * if no DL */
 void
-lflush()
+lflush(void)
 {
        int    lpoint;
        u_char  *str;
        static int      curx = 0;
        static int      cury = 0;
-       char tgoto_buf[256];
 
        if ((lpoint = lpnt - lpbuf) > 0) {
 #ifdef EXTRA
@@ -830,96 +792,88 @@ lflush()
                }
                for (str = lpbuf; str < lpnt; str++) {
                        if (*str >= 32) {
-                               xputchar(*str);
+                               ttputch(*str);
                                curx++;
                        } else
                                switch (*str) {
                                case CLEAR:
-                                       tputs(CL, 0, xputchar);
+                                       tputs(clear_screen, 0, ttputch);
                                        curx = cury = 0;
                                        break;
 
                                case CL_LINE:
-                                       tputs(CE, 0, xputchar);
+                                       tputs(clr_eol, 0, ttputch);
                                        break;
 
                                case CL_DOWN:
-                                       tputs(CD, 0, xputchar);
+                                       tputs(clr_eos, 0, ttputch);
                                        break;
 
                                case ST_START:
-                                       tputs(SO, 0, xputchar);
+                                       tputs(enter_standout_mode, 0, ttputch);
                                        break;
 
                                case ST_END:
-                                       tputs(SE, 0, xputchar);
+                                       tputs(exit_standout_mode, 0, ttputch);
                                        break;
 
                                case CURSOR:
                                        curx = *++str - 1;
                                        cury = *++str - 1;
-                                       if (t_goto(info, CM, curx, cury,
-                                                  tgoto_buf, 255) == 0)
-                                               tputs(tgoto_buf, 0, xputchar);
+                                       tputs(tiparm(cursor_address,
+                                                   cury, curx), 0, ttputch);
                                        break;
 
                                case '\n':
                                        if ((cury == 23) && enable_scroll) {
-                                               if (!DL || !AL) {       /* wraparound or scroll? */
+                                               if (!delete_line ||
+                                                   !insert_line)
+                                               {       /* wraparound or scroll? */
                                                        if (++scrline > 23)
                                                                scrline = 19;
 
                                                        if (++scrline > 23)
                                                                scrline = 19;
-                                                       if (t_goto(info, CM, 0,
-                                                                  scrline,
-                                                                  tgoto_buf,
-                                                                  255) == 0)
-                                                               tputs(tgoto_buf,
-                                                                     0,
-                                                                     xputchar);
-                                                       tputs(CE, 0, xputchar);
+                                                       tputs(tiparm(
+                                                           cursor_address,
+                                                           scrline, 0),
+                                                           0, ttputch);
+                                                       tputs(clr_eol, 0,
+                                                           ttputch);
 
                                                        if (--scrline < 19)
                                                                scrline = 23;
-                                                       if (t_goto(info, CM, 0,
-                                                                  scrline,
-                                                                  tgoto_buf,
-                                                                  255) == 0)
-                                                               tputs(tgoto_buf,
-                                                                     0,
-                                                                     xputchar);
-                                                       tputs(CE, 0, xputchar);
+                                                       tputs(tiparm(
+                                                           cursor_address,
+                                                           scrline, 0),
+                                                           0, ttputch);
+                                                       tputs(clr_eol, 0,
+                                                           ttputch);
                                                } else {
-                                                       if (t_goto(info, CM, 0,
-                                                                  19,
-                                                                  tgoto_buf,
-                                                                  255) == 0)
-                                                               tputs(tgoto_buf,
-                                                                     0,
-                                                                     xputchar);
-                                                       tputs(DL, 0, xputchar);
-                                                       if (t_goto(info, CM, 0,
-                                                                  23,
-                                                                  tgoto_buf,
-                                                                  255) == 0)
-                                                               tputs(tgoto_buf,
-                                                                     0,
-                                                                     xputchar);
+                                                       tputs(tiparm(
+                                                           cursor_address,
+                                                           19, 0),
+                                                           0, ttputch);
+                                                       tputs(delete_line, 0,
+                                                           ttputch);
+                                                       tputs(tiparm(
+                                                           cursor_address,
+                                                           23, 0),
+                                                           0, ttputch);
                                                        /*
                                                         * tputs (AL, 0,
-                                                        * xputchar);
+                                                        * ttputch);
                                                         */
                                                }
                                        } else {
-                                               xputchar('\n');
+                                               ttputch('\n');
                                                cury++;
                                        }
                                        curx = 0;
                                        break;
 
                                default:
-                                       xputchar(*str);
+                                       ttputch(*str);
                                        curx++;
                                };
                }
@@ -951,10 +905,10 @@ lflush()
 #ifndef VT100
 static int      vindex = 0;
 /*
- * xputchar(ch)                Print one character in decoded output buffer.
+ * ttputch(ch)         Print one character in decoded output buffer.
  */
-int 
-xputchar(int ch)
+static int 
+ttputch(int ch)
 {
        outbuf[vindex++] = ch;
        if (vindex >= BUFBIG)
@@ -965,8 +919,8 @@ xputchar(int ch)
 /*
  * flush_buf()                 Flush buffer with decoded output.
  */
-void
-flush_buf()
+static void
+flush_buf(void)
 {
        if (vindex)
                write(io_outfd, outbuf, vindex);
@@ -979,8 +933,7 @@ flush_buf()
  *     Processes only the \33[#m sequence (converts . files for termcap use
  */
 char *
-tmcapcnv(sd, ss)
-       char  *sd, *ss;
+tmcapcnv(char *sd, char *ss)
 {
        int    tmstate = 0;     /* 0=normal, 1=\33 2=[ 3=# */
        char            tmdigit = 0;    /* the # in \33[#m */
@@ -1032,7 +985,7 @@ tmcapcnv(sd, ss)
  *     beep()  Routine to emit a beep if enabled (see no-beep in .larnopts)
  */
 void
-beep()
+beep(void)
 {
        if (!nobeep)
                *lpnt++ = '\7';