-/* $NetBSD: io.c,v 1.12 2001/09/24 13:22:29 wiz Exp $ */
+/* $NetBSD: io.c,v 1.19 2008/01/28 05:38:54 dholland Exp $ */
/*
* io.c Larn is copyrighted 1986 by Noah Morgan.
* FILE INPUT ROUTINES
*
* long lgetc() read one character from input buffer
- * long lrint() read one integer from input buffer
+ * 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
*/
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: io.c,v 1.12 2001/09/24 13:22:29 wiz Exp $");
+__RCSID("$NetBSD: io.c,v 1.19 2008/01/28 05:38:54 dholland Exp $");
#endif /* not lint */
#include "header.h"
#include "extern.h"
#include <string.h>
#include <unistd.h>
+#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>
#include <fcntl.h>
#endif /* not TERMIO or TERMIOS */
#ifndef NOVARARGS /* if we have varargs */
-#ifdef __STDC__
#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
#else /* NOVARARGS */ /* if we don't have varargs */
typedef char *va_list;
#define va_dcl int va_alist;
#endif /* NOVARARGS */
#define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
-int lfd; /* output file numbers */
-int fd; /* input file numbers */
+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 */
long *p, *pe;
for (p = c, pe = c + 100; p < pe; *p++ = 0);
time(&initialtime);
- srand(initialtime);
+ seedrand(initialtime);
lcreat((char *) 0); /* open buffering for output to terminal */
}
* are done beforehand if needed.
* Returns nothing of value.
*/
-#ifdef lint
-/* VARARGS */
-lprintf(str)
- char *str;
-{
- char *str2;
- str2 = str;
- str = str2; /* to make lint happy */
-}
-/* VARARGS */
-sprintf(str)
- char *str;
-{
- char *str2;
- str2 = str;
- str = str2; /* to make lint happy */
-}
-#else /* lint */
-/* VARARGS */
-#ifdef __STDC__
-void lprintf(const char *fmt, ...)
-#else
void
-lprintf(va_alist)
-va_dcl
-#endif
+lprintf(const char *fmt, ...)
{
- va_list ap; /* pointer for variable argument list */
- char *outb, *tmpb;
- long wide, left, cont, n; /* data for lprintf */
- char db[12]; /* %d buffer in lprintf */
-#ifndef __STDC__
- char *fmt;
-
- va_start(ap); /* initialize the var args pointer */
- fmt = va_arg(ap, char *); /* pointer to format string */
-#else
+ va_list ap;
+ char buf[BUFBIG/2];
+
va_start(ap, fmt);
-#endif
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+
if (lpnt >= lpend)
lflush();
- outb = lpnt;
- for (;;) {
- while (*fmt != '%')
- if (*fmt)
- *outb++ = *fmt++;
- else {
- lpnt = outb;
- va_end(ap);
- return;
- }
- wide = 0;
- left = 1;
- cont = 1;
- while (cont)
- switch (*(++fmt)) {
- case 'd':
- n = va_arg(ap, long);
- if (n < 0) {
- n = -n;
- *outb++ = '-';
- if (wide)
- --wide;
- }
- tmpb = db + 11;
- *tmpb = (char) (n % 10 + '0');
- while (n > 9)
- *(--tmpb) = (char) ((n /= 10) % 10 + '0');
- if (wide == 0)
- while (tmpb < db + 12)
- *outb++ = *tmpb++;
- else {
- wide -= db - tmpb + 12;
- if (left)
- while (wide-- > 0)
- *outb++ = ' ';
- while (tmpb < db + 12)
- *outb++ = *tmpb++;
- if (left == 0)
- while (wide-- > 0)
- *outb++ = ' ';
- }
- cont = 0;
- break;
-
- case 's':
- tmpb = va_arg(ap, char *);
- if (wide == 0) {
- while ((*outb++ = *tmpb++) != '\0')
- continue;
- --outb;
- } else {
- n = wide - strlen(tmpb);
- if (left)
- while (n-- > 0)
- *outb++ = ' ';
- while ((*outb++ = *tmpb++) != '\0')
- continue;
- --outb;
- if (left == 0)
- while (n-- > 0)
- *outb++ = ' ';
- }
- cont = 0;
- break;
- case 'c':
- *outb++ = va_arg(ap, int);
- cont = 0;
- break;
-
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- wide = 10 * wide + *fmt - '0';
- break;
-
- case '-':
- left = 0;
- break;
-
- default:
- *outb++ = *fmt;
- cont = 0;
- break;
- };
- fmt++;
- }
- va_end(ap);
+ lprcat(buf);
}
-#endif /* lint */
/*
* lprint(long-integer) send binary integer to output buffer
char *buf;
int len;
{
- char *str;
- int num2;
+ char *s;
+ u_char *t;
+ int num2;
+
if (len > 399) { /* don't copy data if can just write it */
#ifdef EXTRA
c[BYTESOUT] += len;
#endif
#ifndef VT100
- for (str = buf; len > 0; --len)
- lprc(*str++);
+ for (s = buf; len > 0; --len)
+ lprc(*s++);
#else /* VT100 */
lflush();
- write(lfd, buf, len);
+ write(io_outfd, buf, len);
#endif /* VT100 */
} else
while (len) {
* output buffer */
if (num2 > len)
num2 = len;
- str = lpnt;
+ t = lpnt;
len -= num2;
while (num2--)
- *str++ = *buf++; /* copy in the bytes */
- lpnt = str;
+ *t++ = *buf++; /* copy in the bytes */
+ lpnt = t;
}
}
return (inbuffer[ipoint++]);
if (iepoint != MAXIBUF)
return (0);
- if ((i = read(fd, inbuffer, MAXIBUF)) <= 0) {
+ if ((i = read(io_infd, inbuffer, MAXIBUF)) <= 0) {
if (i != 0)
write(1, "error reading from input file\n", 30);
iepoint = ipoint = 0;
* Returns the int read
*/
long
-lrint()
+larn_lrint()
{
unsigned long i;
i = 255 & lgetc();
char *adr;
int num;
{
- char *pnt;
+ u_char *pnt;
int num2;
+
while (num) {
if (iepoint == ipoint) {
if (num > 5) { /* fast way */
- if (read(fd, adr, num) != num)
+ if (read(io_infd, adr, num) != num)
write(2, "error reading from input file\n", 30);
num = 0;
} else {
lcreat(str)
char *str;
{
+ lflush();
lpnt = lpbuf;
lpend = lpbuf + BUFBIG;
if (str == NULL)
- return (lfd = 1);
- if ((lfd = creat(str, 0644)) < 0) {
- lfd = 1;
+ 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 (lfd);
+ return (io_outfd);
}
/*
{
ipoint = iepoint = MAXIBUF;
if (str == NULL)
- return (fd = 0);
- if ((fd = open(str, 0)) < 0) {
+ return (io_infd = 0);
+ if ((io_infd = open(str, O_RDONLY)) < 0) {
lwclose();
- lfd = 1;
+ io_outfd = 1;
lpnt = lpbuf;
return (-1);
}
- return (fd);
+ return (io_infd);
}
/*
lpnt = lpbuf;
lpend = lpbuf + BUFBIG;
if (str == NULL)
- return (lfd = 1);
- if ((lfd = open(str, 2)) < 0) {
- lfd = 1;
+ return (io_outfd = 1);
+ if ((io_outfd = open(str, 2)) < 0) {
+ io_outfd = 1;
return (-1);
}
- lseek(lfd, 0, 2); /* seek to end of file */
- return (lfd);
+ lseek(io_outfd, 0, SEEK_END); /* seek to end of file */
+ return (io_outfd);
}
/*
void
lrclose()
{
- if (fd > 0)
- close(fd);
+ if (io_infd > 0) {
+ close(io_infd);
+ io_infd = 0;
+ }
}
/*
lwclose()
{
lflush();
- if (lfd > 2)
- close(lfd);
+ if (io_outfd > 2) {
+ close(io_outfd);
+ io_outfd = 1;
+ }
}
/*
* avoids calls to lprintf (time consuming)
*/
void
-lprcat(str)
- char *str;
+lprcat(const char *str)
{
- char *str2;
+ u_char *str2;
if (lpnt >= lpend)
lflush();
str2 = lpnt;
* obvious meanings.
*/
-static char *cap;
struct tinfo *info;
char *CM, *CE, *CD, *CL, *SO, *SE, *AL, *DL; /* Termcap capabilities */
static char *outbuf = 0; /* translated output buffer */
void
init_term()
{
- char *capptr;
char *term;
- cap = NULL;
switch (t_getent(&info, term = getenv("TERM"))) {
case -1:
write(2, "Cannot open termcap file.\n", 26);
exit(1);
};
- CM = t_agetstr(info, "cm", &cap, &capptr); /* Cursor motion */
- CE = t_agetstr(info, "ce", &cap, &capptr); /* Clear to eoln */
- CL = t_agetstr(info, "cl", &cap, &capptr); /* Clear screen */
+ 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", &cap, &capptr); /* Insert line */
- DL = t_agetstr(info, "dl", &cap, &capptr); /* Delete line */
- SO = t_agetstr(info, "so", &cap, &capptr); /* Begin standout mode */
- SE = t_agetstr(info, "se", &cap, &capptr); /* End standout mode */
- CD = t_agetstr(info, "cd", &cap, &capptr); /* Clear to end of display */
+ 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);
* standout(str) Print the argument string in inverse video (standout mode).
*/
void
-standout(str)
- char *str;
+standout(const char *str)
{
#ifdef VT100
setbold();
#endif
if (enable_scroll <= -1) {
flush_buf();
- if (write(lfd, lpbuf, lpoint) != lpoint)
+ if (write(io_outfd, lpbuf, lpoint) != lpoint)
write(2, "error writing to output file\n", 29);
lpnt = lpbuf; /* point back to beginning of buffer */
return;
#ifdef EXTRA
c[BYTESOUT] += lpoint;
#endif
- if (write(lfd, lpbuf, lpoint) != lpoint)
+ if (write(io_outfd, lpbuf, lpoint) != lpoint)
write(2, "error writing to output file\n", 29);
}
lpnt = lpbuf; /* point back to beginning of buffer */
* xputchar(ch) Print one character in decoded output buffer.
*/
int
-xputchar(c)
- int c;
+xputchar(int ch)
{
- outbuf[vindex++] = c;
+ outbuf[vindex++] = ch;
if (vindex >= BUFBIG)
flush_buf();
return (0);
flush_buf()
{
if (vindex)
- write(lfd, outbuf, vindex);
+ write(io_outfd, outbuf, vindex);
vindex = 0;
}