#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: io.c,v 1.31 2024/03/26 20:50:29 andvar Exp $");
#endif
#include "header.h"
#include "extern.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <term.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#ifdef TERMIO
#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
#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
#ifndef BSD
#define CBREAK RAW
#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
#ifndef NOVARARGS
#include <stdarg.h>
#else
typedef char *va_list;
#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
static int ttputch(int ch);
static void flush_buf(void);
#define LINBUFSIZE 128
int io_outfd;
int io_infd;
static struct sgttyb ttx;
static int ipoint = MAXIBUF, iepoint = MAXIBUF;
static char lgetwbuf[LINBUFSIZE];
void
setupvt100(void)
{
clear();
setscroll();
scbr();
}
void
clearvt100(void)
{
resetscroll();
clear();
sncbr();
}
int
ttgetch(void)
{
char byt;
#ifdef EXTRA
c[BYTESIN]++;
#endif
lflush();
read(0, &byt, 1);
return (byt);
}
void
scbr(void)
{
gtty(0, &ttx);
doraw(ttx);
stty(0, &ttx);
}
void
sncbr(void)
{
gtty(0, &ttx);
unraw(ttx);
stty(0, &ttx);
}
void
newgame(void)
{
long *p, *pe;
for (p = c, pe = c + 100; p < pe; *p++ = 0);
time(&initialtime);
seedrand(initialtime);
srandom(initialtime);
lcreat((char *) 0);
}
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();
lprcat(buf);
}
void
lprint(long x)
{
if (lpnt >= lpend)
lflush();
*lpnt++ = 255 & x;
*lpnt++ = 255 & (x >> 8);
*lpnt++ = 255 & (x >> 16);
*lpnt++ = 255 & (x >> 24);
}
void
lwrite(char *buf, int len)
{
char *s;
u_char *t;
int num2;
if (len > 399) {
#ifdef EXTRA
c[BYTESOUT] += len;
#endif
#ifndef VT100
for (s = buf; len > 0; --len)
lprc(*s++);
#else
lflush();
write(io_outfd, buf, len);
#endif
} else
while (len) {
if (lpnt >= lpend)
lflush();
num2 = lpbuf + BUFBIG - lpnt;
if (num2 > len)
num2 = len;
t = lpnt;
len -= num2;
while (num2--)
*t++ = *buf++;
lpnt = t;
}
}
long
lgetc(void)
{
int i;
if (ipoint != iepoint)
return (inbuffer[ipoint++]);
if (iepoint != MAXIBUF)
return (0);
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
larn_lrint(void)
{
unsigned long i;
i = 255 & lgetc();
i |= (255 & lgetc()) << 8;
i |= (255 & lgetc()) << 16;
i |= (255 & lgetc()) << 24;
return (i);
}
void
lrfill(char *adr, int num)
{
u_char *pnt;
int num2;
while (num) {
if (iepoint == ipoint) {
if (num > 5) {
if (read(io_infd, adr, num) != num)
write(2, "error reading from input file\n", 30);
num = 0;
} else {
*adr++ = lgetc();
--num;
}
} else {
num2 = iepoint - ipoint;
if (num2 > num)
num2 = num;
pnt = inbuffer + ipoint;
num -= num2;
ipoint += num2;
while (num2--)
*adr++ = *pnt++;
}
}
}
char *
lgetw(void)
{
char *lgp, cc;
int n = LINBUFSIZE, quote = 0;
lgp = lgetwbuf;
do
cc = lgetc();
while ((cc <= 32) && (cc > '\0'));
for (;; --n, cc = lgetc()) {
if ((cc == '\0') && (lgp == lgetwbuf))
return (NULL);
if ((n <= 1) || ((cc <= 32) && (quote == 0))) {
*lgp = '\0';
return (lgetwbuf);
}
if (cc != '"')
*lgp++ = cc;
else
quote ^= 1;
}
}
char *
lgetl(void)
{
int i = LINBUFSIZE, ch;
char *str = lgetwbuf;
for (;; --i) {
if ((*str++ = ch = lgetc()) == '\0') {
if (str == lgetwbuf + 1)
return (NULL);
ot: *str = '\0';
return (lgetwbuf);
}
if ((ch == '\n') || (i <= 1))
goto ot;
}
}
int
lcreat(char *str)
{
lflush();
lpnt = lpbuf;
lpend = lpbuf + BUFBIG;
if (str == NULL)
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);
}
return (io_outfd);
}
int
lopen(char *str)
{
ipoint = iepoint = MAXIBUF;
if (str == NULL)
return (io_infd = 0);
if ((io_infd = open(str, O_RDONLY)) < 0) {
lwclose();
io_outfd = 1;
lpnt = lpbuf;
return (-1);
}
return (io_infd);
}
int
lappend(char *str)
{
lpnt = lpbuf;
lpend = lpbuf + BUFBIG;
if (str == NULL)
return (io_outfd = 1);
if ((io_outfd = open(str, 2)) < 0) {
io_outfd = 1;
return (-1);
}
lseek(io_outfd, 0, SEEK_END);
return (io_outfd);
}
void
lrclose(void)
{
if (io_infd > 0) {
close(io_infd);
io_infd = 0;
}
}
void
lwclose(void)
{
lflush();
if (io_outfd > 2) {
close(io_outfd);
io_outfd = 1;
}
}
void
lprcat(const char *str)
{
u_char *str2;
if (lpnt >= lpend)
lflush();
str2 = lpnt;
while ((*str2++ = *str++) != '\0')
continue;
lpnt = str2 - 1;
}
#ifdef VT100
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];
while (*p)
*lpnt++ = *p++;
p = x_num[x];
while (*p)
*lpnt++ = *p++;
}
#else
void
cursor(int x, int y)
{
if (lpnt >= lpend)
lflush();
*lpnt++ = CURSOR;
*lpnt++ = x;
*lpnt++ = y;
}
#endif
void
cursors(void)
{
cursor(1, 24);
}
#ifndef VT100
static char *outbuf = 0;
void
init_term(void)
{
setupterm(NULL, 0, NULL);
if (!cursor_address) {
fprintf(stderr, "term does not have cursor_address.\n");
exit(1);
}
if (!clr_eol) {
fprintf(stderr, "term does not have clr_eol.\n");
exit(1);
}
if (!clear_screen) {
fprintf(stderr, "term does not have clear_screen.\n");
exit(1);
}
if ((outbuf = malloc(BUFBIG + 16)) == 0) {
fprintf(stderr, "Error malloc'ing memory for decoded output buffer\n");
died(-285);
}
}
#endif
void
cl_line(int x, int y)
{
#ifdef VT100
cursor(x, y);
lprcat("\33[2K");
#else
cursor(1, y);
*lpnt++ = CL_LINE;
cursor(x, y);
#endif
}
void
cl_up(int x, int y)
{
#ifdef VT100
cursor(x, y);
lprcat("\33[1J\33[2K");
#else
int i;
cursor(1, 1);
for (i = 1; i <= y; i++) {
*lpnt++ = CL_LINE;
*lpnt++ = '\n';
}
cursor(x, y);
#endif
}
void
cl_dn(int x, int y)
{
#ifdef VT100
cursor(x, y);
lprcat("\33[J\33[2K");
#else
int i;
cursor(1, y);
if (!clr_eos) {
*lpnt++ = CL_LINE;
for (i = y; i <= 24; i++) {
*lpnt++ = CL_LINE;
if (i != 24)
*lpnt++ = '\n';
}
cursor(x, y);
} else
*lpnt++ = CL_DOWN;
cursor(x, y);
#endif
}
void
standout(const char *str)
{
#ifdef VT100
setbold();
while (*str)
*lpnt++ = *str++;
resetbold();
#else
*lpnt++ = ST_START;
while (*str)
*lpnt++ = *str++;
*lpnt++ = ST_END;
#endif
}
void
set_score_output(void)
{
enable_scroll = -1;
}
#ifndef VT100
static int scrline = 18;
void
lflush(void)
{
int lpoint;
u_char *str;
static int curx = 0;
static int cury = 0;
if ((lpoint = lpnt - lpbuf) > 0) {
#ifdef EXTRA
c[BYTESOUT] += lpoint;
#endif
if (enable_scroll <= -1) {
flush_buf();
if (write(io_outfd, lpbuf, lpoint) != lpoint)
write(2, "error writing to output file\n", 29);
lpnt = lpbuf;
return;
}
for (str = lpbuf; str < lpnt; str++) {
if (*str >= 32) {
ttputch(*str);
curx++;
} else
switch (*str) {
case CLEAR:
tputs(clear_screen, 0, ttputch);
curx = cury = 0;
break;
case CL_LINE:
tputs(clr_eol, 0, ttputch);
break;
case CL_DOWN:
tputs(clr_eos, 0, ttputch);
break;
case ST_START:
tputs(enter_standout_mode, 0, ttputch);
break;
case ST_END:
tputs(exit_standout_mode, 0, ttputch);
break;
case CURSOR:
curx = *++str - 1;
cury = *++str - 1;
tputs(tiparm(cursor_address,
cury, curx), 0, ttputch);
break;
case '\n':
if ((cury == 23) && enable_scroll) {
if (!delete_line ||
!insert_line)
{
if (++scrline > 23)
scrline = 19;
if (++scrline > 23)
scrline = 19;
tputs(tiparm(
cursor_address,
scrline, 0),
0, ttputch);
tputs(clr_eol, 0,
ttputch);
if (--scrline < 19)
scrline = 23;
tputs(tiparm(
cursor_address,
scrline, 0),
0, ttputch);
tputs(clr_eol, 0,
ttputch);
} else {
tputs(tiparm(
cursor_address,
19, 0),
0, ttputch);
tputs(delete_line, 0,
ttputch);
tputs(tiparm(
cursor_address,
23, 0),
0, ttputch);
}
} else {
ttputch('\n');
cury++;
}
curx = 0;
break;
default:
ttputch(*str);
curx++;
};
}
}
lpnt = lpbuf;
flush_buf();
}
#else
void
lflush()
{
int lpoint;
if ((lpoint = lpnt - lpbuf) > 0) {
#ifdef EXTRA
c[BYTESOUT] += lpoint;
#endif
if (write(io_outfd, lpbuf, lpoint) != lpoint)
write(2, "error writing to output file\n", 29);
}
lpnt = lpbuf;
}
#endif
#ifndef VT100
static int vindex = 0;
static int
ttputch(int ch)
{
outbuf[vindex++] = ch;
if (vindex >= BUFBIG)
flush_buf();
return (0);
}
static void
flush_buf(void)
{
if (vindex)
write(io_outfd, outbuf, vindex);
vindex = 0;
}
char *
tmcapcnv(char *sd, char *ss)
{
int tmstate = 0;
char tmdigit = 0;
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;
};
ss++;
}
*sd = 0;
return (sd);
}
#endif
void
beep(void)
{
if (!nobeep)
*lpnt++ = '\7';
}