-+-+-+-+-+-+-+-+ START OF PART 13 -+-+-+-+-+-+-+-+
X *  standout(str)       Print the string in standout mode.
X *  set_score_output()  Called when output should be literally printed.
X ** ttputch(ch)     Print one character in decoded output buffer.
X ** flush_buf()     Flush buffer with decoded output.
X ** init_term()     Terminal initialization -- setup termcap info
X ** char *tmcapcnv(sd,ss)   Routine to convert VT100 \33's to termcap format
X *  beep()          Routine to emit a beep if enabled (see no-beep in .larno
Vpts)
X *
X * Note: ** entries are available only in termcap mode.
X */
X
X#include "header.h"
X#include "larndefs.h"
X#include <ctype.h>
X
X#ifdef EXTRA
X# include "player.h"
X#endif
X#ifdef SYSV /* system III or system V */
X# ifndef MSDOS
X#   include <termio.h>
X# endif
X#define sgttyb termio
X#define stty(_a,_b) ioctl(_a,TCSETA,_b)
X#define gtty(_a,_b) ioctl(_a,TCGETA,_b)
X#ifndef MSDOS
Xstatic int rawflg = 0;
Xstatic char saveeof,saveeol;
X#define doraw(_a) if(!rawflg)`7B++rawflg;saveeof=_a.c_cc`5BVMIN`5D;saveeol=_
Va.c_cc`5BVTIME`5D;`7D\
X    _a.c_cc`5BVMIN`5D=1;_a.c_cc`5BVTIME`5D=1;_a.c_lflag &= `7E(ICANON`7CECHO
V`7CECHOE`7CECHOK`7CECHONL)
X#define unraw(_a) _a.c_cc`5BVMIN`5D=saveeof;_a.c_cc`5BVTIME`5D=saveeol;_a.c_
Vlflag `7C= ICANON`7CECHO`7CECHOE`7CECHOK`7CECHONL
X#endif
X#else not SYSV
X#ifdef VMS
X#include    <descrip.h>
X#include    <ssdef.h>
X#include    <stsdef.h>
X#include    <iodef.h>
X#include    <ttdef.h>
X#include    <tt2def.h>
X#else VMS
X#ifndef BSD
X#define CBREAK RAW      /* V7 has no CBREAK */
X#endif
X#define doraw(_a) (_a.sg_flags `7C= CBREAK,_a.sg_flags &= `7EECHO)
X#define unraw(_a) (_a.sg_flags &= `7ECBREAK,_a.sg_flags `7C= ECHO)
X#include <sgtty.h>
X#endif not SYSV
X#endif VMS
X
X#ifndef NOVARARGS   /* if we have varargs */
X#include <varargs.h>
X#else NOVARARGS /* if we don't have varargs */
Xtypedef char *va_list;
X#define va_dcl int va_alist;
X#define va_start(plist) plist = (char *) &va_alist
X#define va_end(plist)
X#define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))`5B-1`5D
X#endif NOVARARGS
X
X#define LINBUFSIZE 128      /* size of the lgetw() and lgetl() buffer
V       */
Xint lfd;            /*  output file numbers     */
Xint fd;             /*  input file numbers      */
X# ifndef MSDOS
X# ifndef VMS
Xstatic struct sgttyb ttx;   /* storage for the tty modes
V                    */
X# else
Xint iochan;         /* storage for the tty channel  */
Xint ttx`5B3`5D;         /* storage for the tty modes    */
Xint cbflag;         /* cbreak flag.  Set when SCBRd */
X# endif
X# endif
Xstatic int ipoint=MAXIBUF,iepoint=MAXIBUF;  /*  input buffering pointers
V    */
Xstatic char lgetwbuf`5BLINBUFSIZE`5D;   /* get line (word) buffer
V               */
X
X#ifdef MSDOS
X# include <setjmp.h>
X extern jmp_buf save_jbuf;
X extern int save_mode;
X#endif
X
X# ifdef MSDOS
X# include <fcntl.h>     /* For O_BINARY */
Xstatic int (*getchfn)();
Xint getche(), kgetch();
X# endif
X
X/*
X *  setupvt100()        Subroutine to set up terminal in correct mode for ga
Vme
X *
X *  Attributes off, clear screen, set scrolling region, set tty mode`20
X */
Xsetupvt100()
X    `7B
X#ifdef VMS
X    struct  dsc$descriptor  idsc;
X    register int        status;
X
X    idsc.dsc$a_pointer = "SYS$COMMAND";
X    idsc.dsc$w_length  = strlen(idsc.dsc$a_pointer);
X    idsc.dsc$b_dtype   = DSC$K_DTYPE_T;
X    idsc.dsc$b_class   = DSC$K_CLASS_S;
X    status = SYS$ASSIGN(&idsc, &iochan, 0, 0);
X    if (status&STS$M_SUCCESS == 0)
X        exit(status);
X#endif
X    lprc(T_INIT);
X    clear();  setscroll();  scbr(); /* system("stty cbreak -echo"); */
X# ifdef MSDOS
X    setraw();
X    setcursor();
X
X    /* Select normal ASCII and line drawing character sets.
X     */
X    if (DECRainbow)
X        lprcat("\033(B\033)0");
X# endif
X    `7D
X
X/*
X *  clearvt100()        Subroutine to clean up terminal when the game is ove
Vr
X *
X *  Attributes off, clear screen, unset scrolling region, restore tty mode`2
V0
X */
Xclearvt100()
X    `7B
X    lprc(T_END);
X    resetscroll();  clear();  sncbr(); /* system("stty -cbreak echo"); */
X# ifdef MSDOS
X    unsetraw();
X    resetcursor();
X# endif
X#ifdef VMS
X    SYS$DASSGN(iochan);
X#endif
X    `7D
X
X/*
X *  ttgetch()       Routine to read in one character from the terminal
X */
X
X#ifdef VMS
X
Xttgetch()
X`7B
X#define NIBUF   80      /* characters in the buffer.    */
X    static char ibuf`5BNIBUF`5D;
X    static int  ibufi = 0;
X    static int  nibuf = 0;
X    int status;
X    int iosb`5B2`5D;
X    int term`5B2`5D;
X
X    lflush();       /* be sure output buffer is flushed */
X    term`5B0`5D = 0;
X    term`5B1`5D = 0;
X    while (ibufi >= nibuf) `7B
X        if (cbflag) `7B
X        /* cbroken */
X            ibufi   = 0;
X/*
X            status = SYS$QIOW(0, iochan, IO$_READLBLK`7CIO$M_TIMED,
X                 iosb, 0, 0, ibuf, NIBUF, 0, term, 0, 0);
X*/
X            status = SYS$QIOW(0, iochan, IO$_READLBLK,
X                 iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
X        if (status != SS$_NORMAL)
X            continue;
X        status = iosb`5B0`5D & 0xFFFF;
X        if (status!=SS$_NORMAL && status!=SS$_TIMEOUT)
X            continue;
X        nibuf = (iosb`5B0`5D>>16) + (iosb`5B1`5D>>16);
X        if (nibuf == 0)`20
X        `7B
X        status = SYS$QIOW(0, iochan, IO$_READLBLK,
X            iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
X        if (status != SS$_NORMAL)
X            continue;
X        if ((iosb`5B0`5D&0xFFFF) != SS$_NORMAL)
X            continue;
X        nibuf = (iosb`5B0`5D>>16) + (iosb`5B1`5D>>16);
X        `7D
X        `7D else `7B
X        /* not cbroken */
X        try_again:
X            ibufi    = 0;
X            status   = SYS$QIOW(0, iochan, IO$_READLBLK, iosb,
X                    0, 0, ibuf, NIBUF, 0, 0, 0, 0);
X            if (status != SS$_NORMAL)
X                goto try_again;
X            if ((iosb`5B0`5D&0xFFFF) != SS$_NORMAL)
X                goto try_again;
X            nibuf = (iosb`5B0`5D>>16) + (iosb`5B1`5D>>16);
X        `7D
X    `7D
X    if ((ibuf`5Bibufi`5D&0xFF) == '\r')     /* carriage return */
X        ibuf`5Bibufi`5D = '\n';     /* turns to newline */
X    return (ibuf`5Bibufi++`5D&0xFF);
X`7D
X#else VMS
X
Xttgetch()
X    `7B
X    char byt;
X#ifdef EXTRA
X    c`5BBYTESIN`5D++;
X#endif EXTRA
X    lflush();       /* be sure output buffer is flushed */
X# ifdef MSDOS
X    if ((byt = (*getchfn)()) == '\r')
X        byt = '\n';
X    return byt;
X# else MSDOS
X    read(0,&byt,1);     /* get byte from terminal */
X    return(byt);
X# endif MSDOS
X    `7D
X# endif VMS
X
X/*
X *  scbr()      Function to set cbreak -echo for the terminal
X *
X *  like: system("stty cbreak -echo")
X */
Xscbr()
X    `7B
X# ifdef MSDOS
X    /* Set up to use the direct console input call which may
X     * read from the keypad;
X     */
X    getchfn = kgetch;
X# else
X# ifdef VMS
X    int status;
X    int iosb`5B2`5D;
X
X    cbflag = 1;
X    status = SYS$QIOW(0, iochan, IO$_SENSEMODE, iosb, 0, 0,
X              ttx, sizeof(ttx), 0, 0, 0, 0);
X    if (status&STS$M_SUCCESS == 0)
X        exit(status);
X    ttx`5B1`5D `7C= TT$M_NOECHO;
X    ttx`5B2`5D `7C= TT2$M_PASTHRU;
X    status = SYS$QIOW(0, iochan, IO$_SETMODE, iosb, 0, 0,
X              ttx, sizeof(ttx), 0, 0, 0, 0);
X    if (status&STS$M_SUCCESS == 0)
X        exit(status);
X# else
X    gtty(0,&ttx);       doraw(ttx);     stty(0,&ttx);
X# endif
X# endif
X    `7D
X
X/*
X *  sncbr()     Function to set -cbreak echo for the terminal
X *
X *  like: system("stty -cbreak echo")
X */
Xsncbr()
X    `7B
X# ifdef MSDOS
X    /* Set up to use the direct console input call with echo, getche()
X     */
X    getchfn = getche;
X# else
X# ifdef VMS
X    int status;
X    int iosb`5B2`5D;
X    cbflag = 0;
X    status = SYS$QIOW(0, iochan, IO$_SENSEMODE, iosb, 0, 0,
X              ttx, sizeof(ttx), 0, 0, 0, 0);
X    if (status&STS$M_SUCCESS == 0)
X        exit(status);
X    ttx`5B1`5D &= `7ETT$M_NOECHO;
X    ttx`5B2`5D &= `7ETT2$M_PASTHRU;
X    status = SYS$QIOW(0, iochan, IO$_SETMODE, iosb, 0, 0,
X              ttx, sizeof(ttx), 0, 0, 0, 0);
X    if (status&STS$M_SUCCESS == 0)
X        exit(status);
X# else
X    gtty(0,&ttx);       unraw(ttx);     stty(0,&ttx);
X# endif
X# endif
X    `7D
X
X/*
X *  newgame()       Subroutine to save the initial time and seed rnd()
X */
Xnewgame()
X`7B
X    register long *p,*pe;
X    for (p=c,pe=c+100; p<pe; *p++ =0);
X    time(&initialtime);
X    srand(initialtime);
X    lcreat((char*)0);   /* open buffering for output to terminal */
X`7D
X
X/*
X *  lprintf(format,args . . .)      printf to the output buffer
X *      char *format;
X *      ??? args . . .
X *
X *  Enter with the format string in "format", as per printf() usage
X *      and any needed arguments following it
X *  Note: lprintf() only supports %s, %c and %d, with width modifier and lef
Vt
X *      or right justification.
X *  No correct checking for output buffer overflow is done, but flushes`20
X *      are done beforehand if needed.
X *  Returns nothing of value.
X */
X#ifdef lint
X/*VARARGS*/
Xlprintf(str)
X    char *str;
X    `7B
X    char *str2;
X    str2 = str;
X    str = str2; /* to make lint happy */
X    `7D
X/*VARARGS*/
Xsprintf(str)
X    char *str;
X    `7B
X    char *str2;
X    str2 = str;
X    str = str2; /* to make lint happy */
X    `7D
X#else lint
X/*VARARGS*/
Xlprintf(va_alist)
Xva_dcl
X    `7B
X    va_list ap; /* pointer for variable argument list */
X    register char *fmt;
X    register char *outb,*tmpb;
X    register long wide,left,cont,n;     /* data for lprintf */
X    char db`5B12`5D;            /* %d buffer in lprintf */
X
X    va_start(ap);   /* initialize the var args pointer */
X    fmt = va_arg(ap, char *);   /* pointer to format string */
X    if (lpnt >= lpend) lflush();`20
X    outb = lpnt;
X    for ( ; ; )
X        `7B
X        while (*fmt != '%')
X            if (*fmt) *outb++ = *fmt++;  else `7B lpnt=outb;  return; `7D
X        wide = 0;   left = 1;   cont=1;
X        while (cont)
X          switch(*(++fmt))
X            `7B
X            case 'd':   n = va_arg(ap, long);
X                        if (n<0) `7B n = -n;  *outb++ = '-';  if (wide) --wi
Vde; `7D
X                        tmpb = db+11;   *tmpb = (char)(n % 10 + '0');
X                        while (n>9)  *(--tmpb) = (char)((n /= 10) % 10 + '0'
V);
X                        if (wide==0)  while (tmpb < db+12) *outb++ = *tmpb++
V;
X                        else
X                            `7B
X                            wide -= db-tmpb+12;
X                            if (left)  while (wide-- > 0) *outb++ = ' ';
X                            while (tmpb < db+12) *outb++ = *tmpb++;
X                            if (left==0)  while (wide-- > 0) *outb++ = ' ';
X                            `7D
X                        cont=0; break;
X
X            case 's':   tmpb = va_arg(ap, char *);
X                        if (wide==0)  `7B while (*outb++ = *tmpb++);  --outb
V; `7D`20
X                        else
X                            `7B
X                            n = wide - strlen(tmpb);
X                            if (left)  while (n-- > 0) *outb++ = ' ';
X                            while (*outb++ = *tmpb++);  --outb;
X                            if (left==0)  while (n-- > 0) *outb++ = ' ';
X                            `7D
X                        cont=0; break;
X
X            case 'c':   *outb++ = va_arg(ap, int);  cont=0;  break;
X
X            case '0':
X            case '1':
X            case '2':
X            case '3':
X            case '4':
X            case '5':
X            case '6':
X            case '7':
X            case '8':
X            case '9':   wide = 10*wide + *fmt - '0';    break;
X
X            case '-':   left = 0;   break;
X
X            default:    *outb++ = *fmt;  cont=0;    break;
X            `7D;
X        fmt++;
X        `7D
X    va_end(ap);
X    `7D
X#endif lint
X
X/*
X *  lprint(long-integer)                send binary integer to output buffer
X *      long integer;
X *
X *      +---------+---------+---------+---------+
X *      `7C   high  `7C         `7C         `7C   low   `7C
X *      `7C  order  `7C         `7C         `7C  order  `7C
X *      `7C   byte  `7C         `7C         `7C   byte  `7C
X *      +---------+---------+---------+---------+
X *     31  ---  24 23 --- 16 15 ---  8 7  ---   0
X *
X *  The save order is low order first, to high order (4 bytes total)
X *      and is written to be system independent.
X *  No checking for output buffer overflow is done, but flushes if needed!
X *  Returns nothing of value.
X */
Xlprint(x)
X    register long x;
X    `7B
X    if (lpnt >= lpend) lflush();
X    *lpnt++ =  255 & x;         *lpnt++ =  255 & (x>>8);
X    *lpnt++ =  255 & (x>>16);   *lpnt++ =  255 & (x>>24);
X    `7D
X
X/*
X *  lwrite(buf,len)         write a buffer to the output buffer
X *      char *buf;
X *      int len;
X * `20
X *  Enter with the address and number of bytes to write out
X *  Returns nothing of value
X */
Xlwrite(buf,len)
X    register char *buf;
X    int len;
X    `7B
X    register char *str;
X    register int num2;
X    if (len > 399)  /* don't copy data if can just write it */
X        `7B
X#ifdef EXTRA
X        c`5BBYTESOUT`5D += len;
X#endif
X
X#ifndef VT100
X        for (str=buf;  len>0; --len)
X            lprc(*str++);
X#else VT100
X        lflush();
X        write(lfd,buf,len);
X#endif VT100
X        `7D`20
X    else while (len)
X        `7B
X        if (lpnt >= lpend) lflush();    /* if buffer is full flush it   */
X        num2 = lpbuf+BUFBIG-lpnt;   /*  # bytes left in output buffer   */
X        if (num2 > len) num2=len;
X        str = lpnt;  len -= num2;
X        while (num2--)  *str++ = *buf++;    /* copy in the bytes */
X        lpnt = str;
X        `7D
X    `7D
X
X/*
X *  long lgetc()        Read one character from input buffer
X *
X *  Returns 0 if EOF, otherwise the character
X */
Xlong lgetc()
X    `7B
X    register int i;
X
X    if (ipoint != iepoint)  return(inbuffer`5Bipoint++`5D);
X    if (iepoint!=MAXIBUF)   return(0);
X    if ((i=vread(fd,inbuffer,MAXIBUF))<=0) `7B
X        if (i!=0)
X            write(1,"error reading from input file\n",30);
X    iepoint = ipoint = 0;
X    return(0);
X    `7D
+-+-+-+-+-+-+-+-  END  OF PART 13 +-+-+-+-+-+-+-+-
