NetBSD/games/larn/io.c

1041 lines
22 KiB
C
Raw Normal View History

/* $NetBSD: io.c,v 1.20 2008/01/28 05:48:57 dholland Exp $ */
/*
* io.c Larn is copyrighted 1986 by Noah Morgan.
*
* Below are the functions in this file:
*
* 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
* 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()
*
* FILE OUTPUT ROUTINES
*
* lprintf(format,args . . .) printf to the output buffer lprint(integer)
* end binary integer to output buffer lwrite(buf,len)
* rite a buffer to the output buffer lprcat(str)
* ent string to output buffer
*
* FILE OUTPUT MACROS (in header.h)
*
* lprc(character) put the character into the output
* buffer
*
* FILE INPUT ROUTINES
*
* long lgetc() read one character from input buffer
2006-05-11 14:23:24 +04:00
* long larn_lrint() read one integer from input buffer
* lrfill(address,number) put input bytes into a buffer char
* *lgetw() get a whitespace ended word from
* input char *lgetl() get a \n or EOF ended line
* from input
*
* FILE OPEN / CLOSE ROUTINES
*
* lcreat(filename) create a new file for write
* lopen(filename) open a file for read
* lappend(filename) open for append to an existing file
* lrclose() close the input file
* lwclose() close output file lflush()
* lush the output buffer
*
* Other Routines
*
* cursor(x,y) position cursor at [x,y]
* cursors() position cursor at [1,24]
* (saves memory) cl_line(x,y) Clear line at [1,y] and leave
* cursor at [x,y] cl_up(x,y) Clear screen
* 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)
* 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)
* outine to convert VT100 \33's to termcap format beep()
* e to emit a beep if enabled (see no-beep in .larnopts)
*
1993-03-21 12:45:37 +03:00
* Note: ** entries are available only in termcap mode.
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: io.c,v 1.20 2008/01/28 05:48:57 dholland Exp $");
#endif /* not lint */
1993-03-21 12:45:37 +03:00
#include "header.h"
#include "extern.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>
#include <fcntl.h>
#include <errno.h>
1993-03-21 12:45:37 +03:00
#ifdef TERMIO
1993-03-21 12:45:37 +03:00
#include <termio.h>
#define sgttyb termio
#define stty(_a,_b) ioctl(_a,TCSETA,_b)
#define gtty(_a,_b) ioctl(_a,TCGETA,_b)
#endif
#ifdef TERMIOS
#include <termios.h>
#define sgttyb termios
#define stty(_a,_b) tcsetattr(_a,TCSADRAIN,_b)
#define gtty(_a,_b) tcgetattr(_a,_b)
#endif
1993-03-21 12:45:37 +03:00
#if defined(TERMIO) || defined(TERMIOS)
static int rawflg = 0;
static char saveeof, saveeol;
#define doraw(_a) \
if(!rawflg) { \
++rawflg; \
saveeof = _a.c_cc[VMIN]; \
saveeol = _a.c_cc[VTIME]; \
} \
_a.c_cc[VMIN] = 1; \
_a.c_cc[VTIME] = 1; \
_a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL)
#define unraw(_a) \
_a.c_cc[VMIN] = saveeof; \
_a.c_cc[VTIME] = saveeol; \
_a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL
#else /* not TERMIO or TERMIOS */
1993-03-21 12:45:37 +03:00
#ifndef BSD
#define CBREAK RAW /* V7 has no CBREAK */
#endif
#define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO)
#define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO)
#include <sgtty.h>
#endif /* not TERMIO or TERMIOS */
1993-03-21 12:45:37 +03:00
#ifndef NOVARARGS /* if we have varargs */
#include <stdarg.h>
#else /* NOVARARGS */ /* if we don't have varargs */
typedef char *va_list;
1993-03-21 12:45:37 +03:00
#define va_dcl int va_alist;
#define va_start(plist) plist = (char *) &va_alist
#define va_end(plist)
#define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
#endif /* NOVARARGS */
1993-03-21 12:45:37 +03:00
#define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
2008-01-28 08:38:53 +03:00
int io_outfd; /* output file numbers */
int io_infd; /* input file numbers */
static struct sgttyb ttx;/* storage for the tty modes */
static int ipoint = MAXIBUF, iepoint = MAXIBUF; /* input buffering
* pointers */
static char lgetwbuf[LINBUFSIZE]; /* get line (word) buffer */
1993-03-21 12:45:37 +03:00
/*
* setupvt100() Subroutine to set up terminal in correct mode for game
1993-03-21 12:45:37 +03:00
*
* Attributes off, clear screen, set scrolling region, set tty mode
1993-03-21 12:45:37 +03:00
*/
void
1993-03-21 12:45:37 +03:00
setupvt100()
{
clear();
setscroll();
scbr(); /* system("stty cbreak -echo"); */
}
1993-03-21 12:45:37 +03:00
/*
* clearvt100() Subroutine to clean up terminal when the game is over
1993-03-21 12:45:37 +03:00
*
* Attributes off, clear screen, unset scrolling region, restore tty mode
1993-03-21 12:45:37 +03:00
*/
void
1993-03-21 12:45:37 +03:00
clearvt100()
{
resetscroll();
clear();
sncbr(); /* system("stty -cbreak echo"); */
}
1993-03-21 12:45:37 +03:00
/*
* lgetchar() Routine to read in one character from the terminal
1993-03-21 12:45:37 +03:00
*/
int
lgetchar()
{
char byt;
1993-03-21 12:45:37 +03:00
#ifdef EXTRA
c[BYTESIN]++;
#endif
lflush(); /* be sure output buffer is flushed */
read(0, &byt, 1); /* get byte from terminal */
return (byt);
}
1993-03-21 12:45:37 +03:00
/*
* scbr() Function to set cbreak -echo for the terminal
*
* like: system("stty cbreak -echo")
*/
void
1993-03-21 12:45:37 +03:00
scbr()
{
gtty(0, &ttx);
doraw(ttx);
stty(0, &ttx);
}
1993-03-21 12:45:37 +03:00
/*
* sncbr() Function to set -cbreak echo for the terminal
*
* like: system("stty -cbreak echo")
*/
void
1993-03-21 12:45:37 +03:00
sncbr()
{
gtty(0, &ttx);
unraw(ttx);
stty(0, &ttx);
}
1993-03-21 12:45:37 +03:00
/*
* newgame() Subroutine to save the initial time and seed rnd()
1993-03-21 12:45:37 +03:00
*/
void
1993-03-21 12:45:37 +03:00
newgame()
{
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 */
}
1993-03-21 12:45:37 +03:00
/*
* lprintf(format,args . . .) printf to the output buffer
* char *format;
* ??? args . . .
*
* Enter with the format string in "format", as per printf() usage
* and any needed arguments following it
* Note: lprintf() only supports %s, %c and %d, with width modifier and left
* or right justification.
* No correct checking for output buffer overflow is done, but flushes
1993-03-21 12:45:37 +03:00
* are done beforehand if needed.
* Returns nothing of value.
*/
void
lprintf(const char *fmt, ...)
{
va_list ap;
char buf[BUFBIG/2];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (lpnt >= lpend)
lflush();
1993-03-21 12:45:37 +03:00
lprcat(buf);
}
1993-03-21 12:45:37 +03:00
/*
* lprint(long-integer) send binary integer to output buffer
1993-03-21 12:45:37 +03:00
* long integer;
*
* +---------+---------+---------+---------+
* | high | | | low |
* | order | | | order |
* | byte | | | byte |
1993-03-21 12:45:37 +03:00
* +---------+---------+---------+---------+
* 31 --- 24 23 --- 16 15 --- 8 7 --- 0
1993-03-21 12:45:37 +03:00
*
* The save order is low order first, to high order (4 bytes total)
* and is written to be system independent.
1993-03-21 12:45:37 +03:00
* No checking for output buffer overflow is done, but flushes if needed!
* Returns nothing of value.
*/
void
1993-03-21 12:45:37 +03:00
lprint(x)
long x;
{
if (lpnt >= lpend)
lflush();
*lpnt++ = 255 & x;
*lpnt++ = 255 & (x >> 8);
*lpnt++ = 255 & (x >> 16);
*lpnt++ = 255 & (x >> 24);
}
1993-03-21 12:45:37 +03:00
/*
* lwrite(buf,len) write a buffer to the output buffer
1993-03-21 12:45:37 +03:00
* char *buf;
* int len;
*
1993-03-21 12:45:37 +03:00
* Enter with the address and number of bytes to write out
* Returns nothing of value
*/
void
lwrite(buf, len)
char *buf;
int len;
{
2008-01-28 08:38:53 +03:00
char *s;
u_char *t;
int num2;
if (len > 399) { /* don't copy data if can just write it */
1993-03-21 12:45:37 +03:00
#ifdef EXTRA
c[BYTESOUT] += len;
#endif
#ifndef VT100
2008-01-28 08:38:53 +03:00
for (s = buf; len > 0; --len)
lprc(*s++);
#else /* VT100 */
1993-03-21 12:45:37 +03:00
lflush();
2008-01-28 08:38:53 +03:00
write(io_outfd, buf, len);
#endif /* VT100 */
} else
while (len) {
if (lpnt >= lpend)
lflush(); /* if buffer is full flush it */
num2 = lpbuf + BUFBIG - lpnt; /* # bytes left in
* output buffer */
if (num2 > len)
num2 = len;
2008-01-28 08:38:53 +03:00
t = lpnt;
len -= num2;
while (num2--)
2008-01-28 08:38:53 +03:00
*t++ = *buf++; /* copy in the bytes */
lpnt = t;
1993-03-21 12:45:37 +03:00
}
}
1993-03-21 12:45:37 +03:00
/*
* long lgetc() Read one character from input buffer
1993-03-21 12:45:37 +03:00
*
* Returns 0 if EOF, otherwise the character
*/
long
lgetc()
{
int i;
if (ipoint != iepoint)
return (inbuffer[ipoint++]);
if (iepoint != MAXIBUF)
return (0);
2008-01-28 08:38:53 +03:00
if ((i = read(io_infd, inbuffer, MAXIBUF)) <= 0) {
if (i != 0)
write(1, "error reading from input file\n", 30);
iepoint = ipoint = 0;
return (0);
}
ipoint = 1;
iepoint = i;
return (*inbuffer);
}
/*
* long lrint() Read one integer from input buffer
1993-03-21 12:45:37 +03:00
*
* +---------+---------+---------+---------+
* | high | | | low |
* | order | | | order |
* | byte | | | byte |
1993-03-21 12:45:37 +03:00
* +---------+---------+---------+---------+
* 31 --- 24 23 --- 16 15 --- 8 7 --- 0
1993-03-21 12:45:37 +03:00
*
* The save order is low order first, to high order (4 bytes total)
* Returns the int read
*/
long
2006-05-11 14:23:24 +04:00
larn_lrint()
{
unsigned long i;
i = 255 & lgetc();
i |= (255 & lgetc()) << 8;
i |= (255 & lgetc()) << 16;
i |= (255 & lgetc()) << 24;
return (i);
}
1993-03-21 12:45:37 +03:00
/*
* lrfill(address,number) put input bytes into a buffer
1993-03-21 12:45:37 +03:00
* char *address;
* int number;
*
* Reads "number" bytes into the buffer pointed to by "address".
* Returns nothing of value
*/
void
lrfill(adr, num)
char *adr;
int num;
{
2008-01-28 08:38:53 +03:00
u_char *pnt;
int num2;
2008-01-28 08:38:53 +03:00
while (num) {
if (iepoint == ipoint) {
if (num > 5) { /* fast way */
2008-01-28 08:38:53 +03:00
if (read(io_infd, adr, num) != num)
write(2, "error reading from input file\n", 30);
num = 0;
} else {
*adr++ = lgetc();
--num;
1993-03-21 12:45:37 +03:00
}
} else {
num2 = iepoint - ipoint; /* # of bytes left in
* the buffer */
if (num2 > num)
num2 = num;
pnt = inbuffer + ipoint;
num -= num2;
ipoint += num2;
while (num2--)
*adr++ = *pnt++;
1993-03-21 12:45:37 +03:00
}
}
}
1993-03-21 12:45:37 +03:00
/*
* char *lgetw() Get a whitespace ended word from input
*
* Returns pointer to a buffer that contains word. If EOF, returns a NULL
*/
char *
lgetw()
{
char *lgp, cc;
int n = LINBUFSIZE, quote = 0;
1993-03-21 12:45:37 +03:00
lgp = lgetwbuf;
do
cc = lgetc();
while ((cc <= 32) && (cc > '\0')); /* eat whitespace */
for (;; --n, cc = lgetc()) {
if ((cc == '\0') && (lgp == lgetwbuf))
return (NULL); /* EOF */
if ((n <= 1) || ((cc <= 32) && (quote == 0))) {
*lgp = '\0';
return (lgetwbuf);
1993-03-21 12:45:37 +03:00
}
if (cc != '"')
*lgp++ = cc;
else
quote ^= 1;
1993-03-21 12:45:37 +03:00
}
}
1993-03-21 12:45:37 +03:00
/*
* char *lgetl() Function to read in a line ended by newline or EOF
1993-03-21 12:45:37 +03:00
*
* Returns pointer to a buffer that contains the line. If EOF, returns NULL
*/
char *
lgetl()
{
int i = LINBUFSIZE, ch;
char *str = lgetwbuf;
for (;; --i) {
if ((*str++ = ch = lgetc()) == '\0') {
if (str == lgetwbuf + 1)
return (NULL); /* EOF */
ot: *str = '\0';
return (lgetwbuf); /* line ended by EOF */
1993-03-21 12:45:37 +03:00
}
if ((ch == '\n') || (i <= 1))
goto ot;/* line ended by \n */
1993-03-21 12:45:37 +03:00
}
}
1993-03-21 12:45:37 +03:00
/*
* lcreat(filename) Create a new file for write
* char *filename;
*
* lcreat((char*)0); means to the terminal
* Returns -1 if error, otherwise the file descriptor opened.
*/
int
1993-03-21 12:45:37 +03:00
lcreat(str)
char *str;
{
2007-04-22 06:09:02 +04:00
lflush();
lpnt = lpbuf;
lpend = lpbuf + BUFBIG;
if (str == NULL)
2008-01-28 08:38:53 +03:00
return (io_outfd = 1);
if ((io_outfd = creat(str, 0644)) < 0) {
io_outfd = 1;
lprintf("error creating file <%s>: %s\n", str,
strerror(errno));
lflush();
return (-1);
1993-03-21 12:45:37 +03:00
}
2008-01-28 08:38:53 +03:00
return (io_outfd);
}
1993-03-21 12:45:37 +03:00
/*
* lopen(filename) Open a file for read
* char *filename;
*
* lopen(0) means from the terminal
* Returns -1 if error, otherwise the file descriptor opened.
*/
int
1993-03-21 12:45:37 +03:00
lopen(str)
char *str;
{
1993-03-21 12:45:37 +03:00
ipoint = iepoint = MAXIBUF;
if (str == NULL)
2008-01-28 08:38:53 +03:00
return (io_infd = 0);
if ((io_infd = open(str, O_RDONLY)) < 0) {
lwclose();
2008-01-28 08:38:53 +03:00
io_outfd = 1;
lpnt = lpbuf;
return (-1);
1993-03-21 12:45:37 +03:00
}
2008-01-28 08:38:53 +03:00
return (io_infd);
}
1993-03-21 12:45:37 +03:00
/*
* lappend(filename) Open for append to an existing file
* char *filename;
*
* lappend(0) means to the terminal
* Returns -1 if error, otherwise the file descriptor opened.
*/
int
1993-03-21 12:45:37 +03:00
lappend(str)
char *str;
{
lpnt = lpbuf;
lpend = lpbuf + BUFBIG;
if (str == NULL)
2008-01-28 08:38:53 +03:00
return (io_outfd = 1);
if ((io_outfd = open(str, 2)) < 0) {
io_outfd = 1;
return (-1);
1993-03-21 12:45:37 +03:00
}
2008-01-28 08:38:53 +03:00
lseek(io_outfd, 0, SEEK_END); /* seek to end of file */
return (io_outfd);
}
1993-03-21 12:45:37 +03:00
/*
* lrclose() close the input file
1993-03-21 12:45:37 +03:00
*
* Returns nothing of value.
*/
void
1993-03-21 12:45:37 +03:00
lrclose()
{
2008-01-28 08:38:53 +03:00
if (io_infd > 0) {
close(io_infd);
io_infd = 0;
}
}
1993-03-21 12:45:37 +03:00
/*
* lwclose() close output file flushing if needed
1993-03-21 12:45:37 +03:00
*
* Returns nothing of value.
*/
void
1993-03-21 12:45:37 +03:00
lwclose()
{
lflush();
2008-01-28 08:38:53 +03:00
if (io_outfd > 2) {
close(io_outfd);
io_outfd = 1;
}
}
1993-03-21 12:45:37 +03:00
/*
* lprcat(string) append a string to the output buffer
* avoids calls to lprintf (time consuming)
1993-03-21 12:45:37 +03:00
*/
void
2008-01-28 08:38:53 +03:00
lprcat(const char *str)
{
2008-01-28 08:38:53 +03:00
u_char *str2;
if (lpnt >= lpend)
lflush();
1993-03-21 12:45:37 +03:00
str2 = lpnt;
while ((*str2++ = *str++) != '\0')
continue;
1993-03-21 12:45:37 +03:00
lpnt = str2 - 1;
}
1993-03-21 12:45:37 +03:00
#ifdef VT100
/*
* cursor(x,y) Subroutine to set the cursor position
*
* x and y are the cursor coordinates, and lpbuff is the output buffer where
* escape sequence will be placed.
*/
static char *y_num[] = {
"\33[", "\33[", "\33[2", "\33[3", "\33[4", "\33[5", "\33[6",
"\33[7", "\33[8", "\33[9", "\33[10", "\33[11", "\33[12", "\33[13", "\33[14",
"\33[15", "\33[16", "\33[17", "\33[18", "\33[19", "\33[20", "\33[21", "\33[22",
"\33[23", "\33[24"};
static char *x_num[] = {
"H", "H", ";2H", ";3H", ";4H", ";5H", ";6H", ";7H", ";8H", ";9H",
";10H", ";11H", ";12H", ";13H", ";14H", ";15H", ";16H", ";17H", ";18H", ";19H",
";20H", ";21H", ";22H", ";23H", ";24H", ";25H", ";26H", ";27H", ";28H", ";29H",
";30H", ";31H", ";32H", ";33H", ";34H", ";35H", ";36H", ";37H", ";38H", ";39H",
";40H", ";41H", ";42H", ";43H", ";44H", ";45H", ";46H", ";47H", ";48H", ";49H",
";50H", ";51H", ";52H", ";53H", ";54H", ";55H", ";56H", ";57H", ";58H", ";59H",
";60H", ";61H", ";62H", ";63H", ";64H", ";65H", ";66H", ";67H", ";68H", ";69H",
";70H", ";71H", ";72H", ";73H", ";74H", ";75H", ";76H", ";77H", ";78H", ";79H",
";80H"};
void
cursor(x, y)
int x, y;
{
char *p;
if (lpnt >= lpend)
lflush();
p = y_num[y]; /* get the string to print */
while (*p)
*lpnt++ = *p++; /* print the string */
p = x_num[x]; /* get the string to print */
while (*p)
*lpnt++ = *p++; /* print the string */
}
#else /* VT100 */
1993-03-21 12:45:37 +03:00
/*
* cursor(x,y) Put cursor at specified coordinates staring at [1,1] (termcap)
*/
void
cursor(x, y)
int x, y;
{
if (lpnt >= lpend)
lflush();
1993-03-21 12:45:37 +03:00
*lpnt++ = CURSOR;
*lpnt++ = x;
*lpnt++ = y;
}
#endif /* VT100 */
1993-03-21 12:45:37 +03:00
/*
* Routine to position cursor at beginning of 24th line
*/
void
1993-03-21 12:45:37 +03:00
cursors()
{
cursor(1, 24);
}
1993-03-21 12:45:37 +03:00
#ifndef VT100
/*
* Warning: ringing the bell is control code 7. Don't use in defines.
* Don't change the order of these defines.
* Also used in helpfiles. Codes used in helpfiles should be \E[1 to \E[7 with
* obvious meanings.
*/
struct tinfo *info;
char *CM, *CE, *CD, *CL, *SO, *SE, *AL, *DL; /* Termcap capabilities */
static char *outbuf = 0; /* translated output buffer */
1993-03-21 12:45:37 +03:00
/*
* init_term() Terminal initialization -- setup termcap info
*/
void
1993-03-21 12:45:37 +03:00
init_term()
{
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);
};
2001-11-02 21:27:00 +03:00
CM = t_agetstr(info, "cm"); /* Cursor motion */
CE = t_agetstr(info, "ce"); /* Clear to eoln */
CL = t_agetstr(info, "cl"); /* Clear screen */
/* OPTIONAL */
2001-11-02 21:27:00 +03:00
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);
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);
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);
exit(1);
}
if ((outbuf = malloc(BUFBIG + 16)) == 0) { /* get memory for
* decoded output buffer */
write(2, "Error malloc'ing memory for decoded output buffer\n", 50);
1993-03-21 12:45:37 +03:00
died(-285); /* malloc() failure */
}
}
#endif /* VT100 */
1993-03-21 12:45:37 +03:00
/*
* 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;
{
1993-03-21 12:45:37 +03:00
#ifdef VT100
cursor(x, y);
lprcat("\33[2K");
#else /* VT100 */
cursor(1, y);
*lpnt++ = CL_LINE;
cursor(x, y);
#endif /* VT100 */
}
1993-03-21 12:45:37 +03:00
/*
* 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;
{
1993-03-21 12:45:37 +03:00
#ifdef VT100
cursor(x, y);
lprcat("\33[1J\33[2K");
#else /* VT100 */
int i;
cursor(1, 1);
for (i = 1; i <= y; i++) {
*lpnt++ = CL_LINE;
*lpnt++ = '\n';
1993-03-21 12:45:37 +03:00
}
cursor(x, y);
#endif /* VT100 */
}
1993-03-21 12:45:37 +03:00
/*
* 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;
{
1993-03-21 12:45:37 +03:00
#ifdef VT100
cursor(x, y);
lprcat("\33[J\33[2K");
#else /* VT100 */
int i;
cursor(1, y);
if (!CD) {
1993-03-21 12:45:37 +03:00
*lpnt++ = CL_LINE;
for (i = y; i <= 24; i++) {
*lpnt++ = CL_LINE;
if (i != 24)
*lpnt++ = '\n';
1993-03-21 12:45:37 +03:00
}
cursor(x, y);
} else
1993-03-21 12:45:37 +03:00
*lpnt++ = CL_DOWN;
cursor(x, y);
#endif /* VT100 */
}
1993-03-21 12:45:37 +03:00
/*
* standout(str) Print the argument string in inverse video (standout mode).
*/
void
2008-01-28 08:38:53 +03:00
standout(const char *str)
{
1993-03-21 12:45:37 +03:00
#ifdef VT100
setbold();
while (*str)
*lpnt++ = *str++;
resetbold();
#else /* VT100 */
1993-03-21 12:45:37 +03:00
*lpnt++ = ST_START;
while (*str)
*lpnt++ = *str++;
*lpnt++ = ST_END;
#endif /* VT100 */
}
1993-03-21 12:45:37 +03:00
/*
* set_score_output() Called when output should be literally printed.
*/
void
1993-03-21 12:45:37 +03:00
set_score_output()
{
1993-03-21 12:45:37 +03:00
enable_scroll = -1;
}
1993-03-21 12:45:37 +03:00
/*
* lflush() Flush the output buffer
1993-03-21 12:45:37 +03:00
*
* Returns nothing of value.
* for termcap version: Flush output in output buffer according to output
* status as indicated by `enable_scroll'
1993-03-21 12:45:37 +03:00
*/
#ifndef VT100
static int scrline = 18; /* line # for wraparound instead of scrolling
* if no DL */
void
lflush()
{
int lpoint;
u_char *str;
static int curx = 0;
static int cury = 0;
char tgoto_buf[256];
if ((lpoint = lpnt - lpbuf) > 0) {
1993-03-21 12:45:37 +03:00
#ifdef EXTRA
c[BYTESOUT] += lpoint;
#endif
if (enable_scroll <= -1) {
1993-03-21 12:45:37 +03:00
flush_buf();
2008-01-28 08:38:53 +03:00
if (write(io_outfd, lpbuf, lpoint) != lpoint)
write(2, "error writing to output file\n", 29);
1993-03-21 12:45:37 +03:00
lpnt = lpbuf; /* point back to beginning of buffer */
return;
}
for (str = lpbuf; str < lpnt; str++) {
if (*str >= 32) {
xputchar(*str);
curx++;
} else
switch (*str) {
case CLEAR:
tputs(CL, 0, xputchar);
curx = cury = 0;
break;
case CL_LINE:
tputs(CE, 0, xputchar);
break;
case CL_DOWN:
tputs(CD, 0, xputchar);
break;
case ST_START:
tputs(SO, 0, xputchar);
break;
case ST_END:
tputs(SE, 0, xputchar);
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);
break;
case '\n':
if ((cury == 23) && enable_scroll) {
if (!DL || !AL) { /* 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);
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);
} 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 (AL, 0,
* xputchar);
*/
}
} else {
xputchar('\n');
cury++;
}
curx = 0;
break;
default:
xputchar(*str);
curx++;
1993-03-21 12:45:37 +03:00
};
}
}
lpnt = lpbuf;
flush_buf(); /* flush real output buffer now */
}
#else /* VT100 */
1993-03-21 12:45:37 +03:00
/*
* lflush() flush the output buffer
1993-03-21 12:45:37 +03:00
*
* Returns nothing of value.
*/
void
1993-03-21 12:45:37 +03:00
lflush()
{
int lpoint;
if ((lpoint = lpnt - lpbuf) > 0) {
1993-03-21 12:45:37 +03:00
#ifdef EXTRA
c[BYTESOUT] += lpoint;
#endif
2008-01-28 08:38:53 +03:00
if (write(io_outfd, lpbuf, lpoint) != lpoint)
write(2, "error writing to output file\n", 29);
}
lpnt = lpbuf; /* point back to beginning of buffer */
}
#endif /* VT100 */
1993-03-21 12:45:37 +03:00
#ifndef VT100
static int vindex = 0;
1993-03-21 12:45:37 +03:00
/*
* xputchar(ch) Print one character in decoded output buffer.
1993-03-21 12:45:37 +03:00
*/
int
2008-01-28 08:38:53 +03:00
xputchar(int ch)
{
2008-01-28 08:38:53 +03:00
outbuf[vindex++] = ch;
if (vindex >= BUFBIG)
flush_buf();
return (0);
}
1993-03-21 12:45:37 +03:00
/*
* flush_buf() Flush buffer with decoded output.
*/
void
1993-03-21 12:45:37 +03:00
flush_buf()
{
if (vindex)
2008-01-28 08:38:53 +03:00
write(io_outfd, outbuf, vindex);
vindex = 0;
}
1993-03-21 12:45:37 +03:00
/*
* char *tmcapcnv(sd,ss) Routine to convert VT100 escapes to termcap
* format
* Processes only the \33[#m sequence (converts . files for termcap use
*/
char *
tmcapcnv(sd, ss)
char *sd, *ss;
{
int tmstate = 0; /* 0=normal, 1=\33 2=[ 3=# */
char tmdigit = 0; /* the # in \33[#m */
while (*ss) {
switch (tmstate) {
case 0:
if (*ss == '\33') {
tmstate++;
break;
}
ign: *sd++ = *ss;
ign2: tmstate = 0;
break;
case 1:
if (*ss != '[')
goto ign;
tmstate++;
break;
case 2:
if (isdigit((u_char)*ss)) {
tmdigit = *ss - '0';
tmstate++;
break;
}
if (*ss == 'm') {
*sd++ = ST_END;
goto ign2;
}
goto ign;
case 3:
if (*ss == 'm') {
if (tmdigit)
*sd++ = ST_START;
else
*sd++ = ST_END;
goto ign2;
}
default:
goto ign;
};
1993-03-21 12:45:37 +03:00
ss++;
}
*sd = 0; /* NULL terminator */
return (sd);
}
#endif /* VT100 */
1993-03-21 12:45:37 +03:00
/*
* beep() Routine to emit a beep if enabled (see no-beep in .larnopts)
1993-03-21 12:45:37 +03:00
*/
void
1993-03-21 12:45:37 +03:00
beep()
{
if (!nobeep)
*lpnt++ = '\7';
}