Home | History | Annotate | Line # | Download | only in larn
io.c revision 1.17.4.1
      1  1.17.4.1      matt /*	io.c,v 1.17 2007/04/22 02:09:02 mouse Exp	*/
      2       1.2   mycroft 
      3       1.7  christos /*
      4       1.7  christos  * io.c			 Larn is copyrighted 1986 by Noah Morgan.
      5       1.7  christos  *
      6       1.7  christos  * Below are the functions in this file:
      7       1.7  christos  *
      8       1.7  christos  * setupvt100() 	Subroutine to set up terminal in correct mode for game
      9       1.7  christos  * clearvt100()  	Subroutine to clean up terminal when the game is over
     10  1.17.4.1      matt  * ttgetch() 		Routine to read in one character from the terminal
     11       1.7  christos  * scbr()			Function to set cbreak -echo for the terminal
     12       1.7  christos  * sncbr()			Function to set -cbreak echo for the terminal
     13       1.7  christos  * newgame() 		Subroutine to save the initial time and seed rnd()
     14       1.7  christos  *
     15       1.7  christos  * FILE OUTPUT ROUTINES
     16       1.7  christos  *
     17       1.7  christos  * lprintf(format,args . . .)	printf to the output buffer lprint(integer)
     18       1.7  christos  * end binary integer to output buffer lwrite(buf,len)
     19       1.7  christos  * rite a buffer to the output buffer lprcat(str)
     20       1.7  christos  * ent string to output buffer
     21       1.7  christos  *
     22       1.7  christos  * FILE OUTPUT MACROS (in header.h)
     23       1.7  christos  *
     24       1.7  christos  * lprc(character)				put the character into the output
     25       1.7  christos  * buffer
     26       1.7  christos  *
     27       1.7  christos  * FILE INPUT ROUTINES
     28       1.7  christos  *
     29       1.7  christos  * long lgetc()				read one character from input buffer
     30      1.16       mrg  * long larn_lrint()			read one integer from input buffer
     31       1.7  christos  * lrfill(address,number)		put input bytes into a buffer char
     32       1.7  christos  * *lgetw()				get a whitespace ended word from
     33       1.7  christos  * input char *lgetl()				get a \n or EOF ended line
     34       1.7  christos  * from input
     35       1.7  christos  *
     36       1.7  christos  * FILE OPEN / CLOSE ROUTINES
     37       1.7  christos  *
     38       1.7  christos  * lcreat(filename)			create a new file for write
     39       1.7  christos  * lopen(filename)				open a file for read
     40       1.7  christos  * lappend(filename)			open for append to an existing file
     41       1.7  christos  * lrclose()					close the input file
     42       1.7  christos  * lwclose()					close output file lflush()
     43       1.7  christos  * lush the output buffer
     44       1.7  christos  *
     45       1.7  christos  * Other Routines
     46       1.7  christos  *
     47       1.7  christos  * cursor(x,y)					position cursor at [x,y]
     48       1.7  christos  * cursors()					position cursor at [1,24]
     49       1.7  christos  * (saves memory) cl_line(x,y)         		Clear line at [1,y] and leave
     50       1.7  christos  * cursor at [x,y] cl_up(x,y)    				Clear screen
     51       1.7  christos  * from [x,1] to current line. cl_dn(x,y)
     52       1.7  christos  * lear screen from [1,y] to end of display. standout(str)
     53       1.7  christos  * rint the string in standout mode. set_score_output()
     54  1.17.4.1      matt  * alled when output should be literally printed. * ttputch(ch)
     55       1.7  christos  * rint one character in decoded output buffer. * flush_buf()
     56       1.7  christos  * lush buffer with decoded output. * init_term()
     57       1.7  christos  * erminal initialization -- setup termcap info *	char *tmcapcnv(sd,ss)
     58       1.7  christos  * outine to convert VT100 \33's to termcap format beep()
     59       1.7  christos  * e to emit a beep if enabled (see no-beep in .larnopts)
     60       1.7  christos  *
     61       1.1       cgd  * Note: ** entries are available only in termcap mode.
     62       1.1       cgd  */
     63       1.7  christos #include <sys/cdefs.h>
     64       1.7  christos #ifndef lint
     65  1.17.4.1      matt __RCSID("io.c,v 1.17 2007/04/22 02:09:02 mouse Exp");
     66       1.7  christos #endif /* not lint */
     67       1.1       cgd 
     68       1.1       cgd #include "header.h"
     69       1.7  christos #include "extern.h"
     70       1.4       cgd #include <string.h>
     71       1.7  christos #include <unistd.h>
     72  1.17.4.1      matt #include <stdio.h>
     73       1.7  christos #include <stdlib.h>
     74       1.7  christos #include <termcap.h>
     75       1.7  christos #include <fcntl.h>
     76       1.5       mrg #include <errno.h>
     77  1.17.4.1      matt #include <ctype.h>
     78       1.1       cgd 
     79       1.7  christos #ifdef TERMIO
     80       1.1       cgd #include <termio.h>
     81       1.1       cgd #define sgttyb termio
     82       1.1       cgd #define stty(_a,_b) ioctl(_a,TCSETA,_b)
     83       1.1       cgd #define gtty(_a,_b) ioctl(_a,TCGETA,_b)
     84       1.7  christos #endif
     85       1.7  christos #ifdef TERMIOS
     86       1.7  christos #include <termios.h>
     87       1.7  christos #define sgttyb termios
     88       1.7  christos #define stty(_a,_b) tcsetattr(_a,TCSADRAIN,_b)
     89       1.7  christos #define gtty(_a,_b) tcgetattr(_a,_b)
     90       1.7  christos #endif
     91       1.1       cgd 
     92       1.7  christos #if defined(TERMIO) || defined(TERMIOS)
     93       1.7  christos static int      rawflg = 0;
     94       1.7  christos static char     saveeof, saveeol;
     95       1.7  christos #define doraw(_a) \
     96       1.7  christos 	if(!rawflg) { \
     97       1.7  christos 		++rawflg; \
     98       1.7  christos 		saveeof = _a.c_cc[VMIN]; \
     99       1.7  christos 		saveeol = _a.c_cc[VTIME]; \
    100       1.7  christos 	} \
    101       1.7  christos     	_a.c_cc[VMIN] = 1; \
    102       1.7  christos 	_a.c_cc[VTIME] = 1; \
    103       1.7  christos 	_a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL)
    104       1.7  christos #define unraw(_a) \
    105       1.7  christos 	_a.c_cc[VMIN] = saveeof; \
    106       1.7  christos 	_a.c_cc[VTIME] = saveeol; \
    107       1.7  christos 	_a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL
    108       1.7  christos 
    109       1.7  christos #else	/* not TERMIO or TERMIOS */
    110       1.1       cgd 
    111       1.1       cgd #ifndef BSD
    112       1.1       cgd #define CBREAK RAW		/* V7 has no CBREAK */
    113       1.1       cgd #endif
    114       1.1       cgd 
    115       1.1       cgd #define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO)
    116       1.1       cgd #define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO)
    117       1.1       cgd #include <sgtty.h>
    118       1.7  christos #endif	/* not TERMIO or TERMIOS */
    119       1.1       cgd 
    120       1.1       cgd #ifndef NOVARARGS	/* if we have varargs */
    121       1.7  christos #include <stdarg.h>
    122       1.7  christos #else	/* NOVARARGS */	/* if we don't have varargs */
    123       1.7  christos typedef char   *va_list;
    124       1.1       cgd #define va_dcl int va_alist;
    125       1.1       cgd #define va_start(plist) plist = (char *) &va_alist
    126       1.1       cgd #define va_end(plist)
    127       1.1       cgd #define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
    128       1.7  christos #endif	/* NOVARARGS */
    129       1.1       cgd 
    130  1.17.4.1      matt static int ttputch(int ch);
    131  1.17.4.1      matt static void flush_buf(void);
    132  1.17.4.1      matt 
    133       1.7  christos #define LINBUFSIZE 128	/* size of the lgetw() and lgetl() buffer */
    134  1.17.4.1      matt int             io_outfd; /* output file numbers */
    135  1.17.4.1      matt int             io_infd; /* input file numbers */
    136       1.7  christos static struct sgttyb ttx;/* storage for the tty modes */
    137       1.7  christos static int      ipoint = MAXIBUF, iepoint = MAXIBUF;	/* input buffering
    138       1.7  christos 							 * pointers    */
    139       1.7  christos static char     lgetwbuf[LINBUFSIZE];	/* get line (word) buffer */
    140       1.1       cgd 
    141       1.1       cgd /*
    142       1.7  christos  *	setupvt100() Subroutine to set up terminal in correct mode for game
    143       1.1       cgd  *
    144       1.7  christos  *	Attributes off, clear screen, set scrolling region, set tty mode
    145       1.1       cgd  */
    146       1.7  christos void
    147       1.1       cgd setupvt100()
    148       1.7  christos {
    149       1.7  christos 	clear();
    150       1.7  christos 	setscroll();
    151       1.7  christos 	scbr();			/* system("stty cbreak -echo"); */
    152       1.7  christos }
    153       1.1       cgd 
    154       1.1       cgd /*
    155       1.7  christos  *	clearvt100() 	Subroutine to clean up terminal when the game is over
    156       1.1       cgd  *
    157       1.7  christos  *	Attributes off, clear screen, unset scrolling region, restore tty mode
    158       1.1       cgd  */
    159       1.7  christos void
    160       1.1       cgd clearvt100()
    161       1.7  christos {
    162       1.7  christos 	resetscroll();
    163       1.7  christos 	clear();
    164       1.7  christos 	sncbr();		/* system("stty -cbreak echo"); */
    165       1.7  christos }
    166       1.1       cgd 
    167       1.1       cgd /*
    168  1.17.4.1      matt  *	ttgetch() 	Routine to read in one character from the terminal
    169       1.1       cgd  */
    170       1.7  christos int
    171  1.17.4.1      matt ttgetch()
    172       1.7  christos {
    173       1.7  christos 	char            byt;
    174       1.1       cgd #ifdef EXTRA
    175       1.1       cgd 	c[BYTESIN]++;
    176       1.1       cgd #endif
    177       1.1       cgd 	lflush();		/* be sure output buffer is flushed */
    178       1.7  christos 	read(0, &byt, 1);	/* get byte from terminal */
    179       1.7  christos 	return (byt);
    180       1.7  christos }
    181       1.1       cgd 
    182       1.1       cgd /*
    183       1.1       cgd  *	scbr()		Function to set cbreak -echo for the terminal
    184       1.1       cgd  *
    185       1.1       cgd  *	like: system("stty cbreak -echo")
    186       1.1       cgd  */
    187       1.7  christos void
    188       1.1       cgd scbr()
    189       1.7  christos {
    190       1.7  christos 	gtty(0, &ttx);
    191       1.7  christos 	doraw(ttx);
    192       1.7  christos 	stty(0, &ttx);
    193       1.7  christos }
    194       1.1       cgd 
    195       1.1       cgd /*
    196       1.1       cgd  *	sncbr()		Function to set -cbreak echo for the terminal
    197       1.1       cgd  *
    198       1.1       cgd  *	like: system("stty -cbreak echo")
    199       1.1       cgd  */
    200       1.7  christos void
    201       1.1       cgd sncbr()
    202       1.7  christos {
    203       1.7  christos 	gtty(0, &ttx);
    204       1.7  christos 	unraw(ttx);
    205       1.7  christos 	stty(0, &ttx);
    206       1.7  christos }
    207       1.1       cgd 
    208       1.1       cgd /*
    209       1.7  christos  *	newgame() 	Subroutine to save the initial time and seed rnd()
    210       1.1       cgd  */
    211       1.7  christos void
    212       1.1       cgd newgame()
    213       1.7  christos {
    214       1.7  christos 	long  *p, *pe;
    215       1.7  christos 	for (p = c, pe = c + 100; p < pe; *p++ = 0);
    216       1.7  christos 	time(&initialtime);
    217      1.15     perry 	seedrand(initialtime);
    218  1.17.4.1      matt 	srandom(initialtime);
    219       1.7  christos 	lcreat((char *) 0);	/* open buffering for output to terminal */
    220       1.7  christos }
    221       1.1       cgd 
    222       1.1       cgd /*
    223       1.1       cgd  *	lprintf(format,args . . .)		printf to the output buffer
    224       1.1       cgd  *		char *format;
    225       1.1       cgd  *		??? args . . .
    226       1.1       cgd  *
    227       1.1       cgd  *	Enter with the format string in "format", as per printf() usage
    228       1.1       cgd  *		and any needed arguments following it
    229       1.1       cgd  *	Note: lprintf() only supports %s, %c and %d, with width modifier and left
    230       1.1       cgd  *		or right justification.
    231       1.7  christos  *	No correct checking for output buffer overflow is done, but flushes
    232       1.1       cgd  *		are done beforehand if needed.
    233       1.1       cgd  *	Returns nothing of value.
    234       1.1       cgd  */
    235  1.17.4.1      matt void
    236  1.17.4.1      matt lprintf(const char *fmt, ...)
    237       1.7  christos {
    238  1.17.4.1      matt 	va_list ap;
    239  1.17.4.1      matt 	char buf[BUFBIG/2];
    240       1.1       cgd 
    241       1.7  christos 	va_start(ap, fmt);
    242  1.17.4.1      matt 	vsnprintf(buf, sizeof(buf), fmt, ap);
    243  1.17.4.1      matt 	va_end(ap);
    244  1.17.4.1      matt 
    245       1.7  christos 	if (lpnt >= lpend)
    246       1.7  christos 		lflush();
    247       1.1       cgd 
    248  1.17.4.1      matt 	lprcat(buf);
    249       1.7  christos }
    250       1.1       cgd 
    251       1.1       cgd /*
    252       1.7  christos  *	lprint(long-integer)	send binary integer to output buffer
    253       1.1       cgd  *		long integer;
    254       1.1       cgd  *
    255       1.1       cgd  *		+---------+---------+---------+---------+
    256       1.7  christos  *		|   high  |	    |	      |	  low	|
    257       1.7  christos  *		|  order  |	    |	      |  order	|
    258       1.7  christos  *		|   byte  |	    |	      |	  byte	|
    259       1.1       cgd  *		+---------+---------+---------+---------+
    260       1.7  christos  *	        31  ---  24 23 --- 16 15 ---  8 7  ---   0
    261       1.1       cgd  *
    262       1.1       cgd  *	The save order is low order first, to high order (4 bytes total)
    263       1.7  christos  *	and is written to be system independent.
    264       1.1       cgd  *	No checking for output buffer overflow is done, but flushes if needed!
    265       1.1       cgd  *	Returns nothing of value.
    266       1.1       cgd  */
    267       1.7  christos void
    268       1.1       cgd lprint(x)
    269       1.7  christos 	long   x;
    270       1.7  christos {
    271       1.7  christos 	if (lpnt >= lpend)
    272       1.7  christos 		lflush();
    273       1.7  christos 	*lpnt++ = 255 & x;
    274       1.7  christos 	*lpnt++ = 255 & (x >> 8);
    275       1.7  christos 	*lpnt++ = 255 & (x >> 16);
    276       1.7  christos 	*lpnt++ = 255 & (x >> 24);
    277       1.7  christos }
    278       1.1       cgd 
    279       1.1       cgd /*
    280       1.7  christos  *	lwrite(buf,len)		write a buffer to the output buffer
    281       1.1       cgd  *		char *buf;
    282       1.1       cgd  *		int len;
    283       1.7  christos  *
    284       1.1       cgd  *	Enter with the address and number of bytes to write out
    285       1.1       cgd  *	Returns nothing of value
    286       1.1       cgd  */
    287       1.7  christos void
    288       1.7  christos lwrite(buf, len)
    289       1.7  christos 	char  *buf;
    290       1.7  christos 	int             len;
    291       1.7  christos {
    292  1.17.4.1      matt 	char *s;
    293  1.17.4.1      matt 	u_char *t;
    294  1.17.4.1      matt 	int num2;
    295  1.17.4.1      matt 
    296       1.7  christos 	if (len > 399) {	/* don't copy data if can just write it */
    297       1.1       cgd #ifdef EXTRA
    298       1.1       cgd 		c[BYTESOUT] += len;
    299       1.1       cgd #endif
    300       1.1       cgd 
    301       1.1       cgd #ifndef VT100
    302  1.17.4.1      matt 		for (s = buf; len > 0; --len)
    303  1.17.4.1      matt 			lprc(*s++);
    304       1.7  christos #else	/* VT100 */
    305       1.1       cgd 		lflush();
    306  1.17.4.1      matt 		write(io_outfd, buf, len);
    307       1.7  christos #endif	/* VT100 */
    308       1.7  christos 	} else
    309       1.7  christos 		while (len) {
    310       1.7  christos 			if (lpnt >= lpend)
    311       1.7  christos 				lflush();	/* if buffer is full flush it	 */
    312       1.7  christos 			num2 = lpbuf + BUFBIG - lpnt;	/* # bytes left in
    313       1.7  christos 							 * output buffer	 */
    314       1.7  christos 			if (num2 > len)
    315       1.7  christos 				num2 = len;
    316  1.17.4.1      matt 			t = lpnt;
    317       1.7  christos 			len -= num2;
    318       1.7  christos 			while (num2--)
    319  1.17.4.1      matt 				*t++ = *buf++;	/* copy in the bytes */
    320  1.17.4.1      matt 			lpnt = t;
    321       1.1       cgd 		}
    322       1.7  christos }
    323       1.1       cgd 
    324       1.1       cgd /*
    325       1.7  christos  *	long lgetc()	Read one character from input buffer
    326       1.1       cgd  *
    327       1.1       cgd  *  Returns 0 if EOF, otherwise the character
    328       1.1       cgd  */
    329       1.7  christos long
    330       1.7  christos lgetc()
    331       1.7  christos {
    332       1.7  christos 	int    i;
    333       1.7  christos 	if (ipoint != iepoint)
    334       1.7  christos 		return (inbuffer[ipoint++]);
    335       1.7  christos 	if (iepoint != MAXIBUF)
    336       1.7  christos 		return (0);
    337  1.17.4.1      matt 	if ((i = read(io_infd, inbuffer, MAXIBUF)) <= 0) {
    338       1.7  christos 		if (i != 0)
    339       1.7  christos 			write(1, "error reading from input file\n", 30);
    340       1.7  christos 		iepoint = ipoint = 0;
    341       1.7  christos 		return (0);
    342       1.7  christos 	}
    343       1.7  christos 	ipoint = 1;
    344       1.7  christos 	iepoint = i;
    345       1.7  christos 	return (*inbuffer);
    346       1.7  christos }
    347       1.1       cgd 
    348       1.1       cgd /*
    349       1.7  christos  *	long lrint()	Read one integer from input buffer
    350       1.1       cgd  *
    351       1.1       cgd  *		+---------+---------+---------+---------+
    352       1.7  christos  *		|   high  |	    |	      |	  low	|
    353       1.7  christos  *		|  order  |	    |	      |  order	|
    354       1.7  christos  *		|   byte  |	    |	      |	  byte	|
    355       1.1       cgd  *		+---------+---------+---------+---------+
    356       1.7  christos  *	       31  ---  24 23 --- 16 15 ---  8 7  ---   0
    357       1.1       cgd  *
    358       1.1       cgd  *	The save order is low order first, to high order (4 bytes total)
    359       1.1       cgd  *	Returns the int read
    360       1.1       cgd  */
    361       1.7  christos long
    362      1.16       mrg larn_lrint()
    363       1.7  christos {
    364       1.7  christos 	unsigned long i;
    365       1.7  christos 	i = 255 & lgetc();
    366       1.7  christos 	i |= (255 & lgetc()) << 8;
    367       1.7  christos 	i |= (255 & lgetc()) << 16;
    368       1.7  christos 	i |= (255 & lgetc()) << 24;
    369       1.7  christos 	return (i);
    370       1.7  christos }
    371       1.1       cgd 
    372       1.1       cgd /*
    373       1.7  christos  *	lrfill(address,number)		put input bytes into a buffer
    374       1.1       cgd  *		char *address;
    375       1.1       cgd  *		int number;
    376       1.1       cgd  *
    377       1.1       cgd  *	Reads "number" bytes into the buffer pointed to by "address".
    378       1.1       cgd  *	Returns nothing of value
    379       1.1       cgd  */
    380       1.7  christos void
    381       1.7  christos lrfill(adr, num)
    382       1.7  christos 	char  *adr;
    383       1.7  christos 	int             num;
    384       1.7  christos {
    385  1.17.4.1      matt 	u_char  *pnt;
    386       1.7  christos 	int    num2;
    387  1.17.4.1      matt 
    388       1.7  christos 	while (num) {
    389       1.7  christos 		if (iepoint == ipoint) {
    390       1.7  christos 			if (num > 5) {	/* fast way */
    391  1.17.4.1      matt 				if (read(io_infd, adr, num) != num)
    392       1.7  christos 					write(2, "error reading from input file\n", 30);
    393       1.7  christos 				num = 0;
    394       1.7  christos 			} else {
    395       1.7  christos 				*adr++ = lgetc();
    396       1.7  christos 				--num;
    397       1.1       cgd 			}
    398       1.7  christos 		} else {
    399       1.7  christos 			num2 = iepoint - ipoint;	/* # of bytes left in
    400       1.7  christos 							 * the buffer	 */
    401       1.7  christos 			if (num2 > num)
    402       1.7  christos 				num2 = num;
    403       1.7  christos 			pnt = inbuffer + ipoint;
    404       1.7  christos 			num -= num2;
    405       1.7  christos 			ipoint += num2;
    406       1.7  christos 			while (num2--)
    407       1.7  christos 				*adr++ = *pnt++;
    408       1.1       cgd 		}
    409       1.1       cgd 	}
    410       1.7  christos }
    411       1.1       cgd 
    412       1.1       cgd /*
    413       1.1       cgd  *	char *lgetw()			Get a whitespace ended word from input
    414       1.1       cgd  *
    415       1.1       cgd  *	Returns pointer to a buffer that contains word.  If EOF, returns a NULL
    416       1.1       cgd  */
    417       1.7  christos char *
    418       1.7  christos lgetw()
    419       1.7  christos {
    420       1.7  christos 	char  *lgp, cc;
    421       1.7  christos 	int    n = LINBUFSIZE, quote = 0;
    422       1.1       cgd 	lgp = lgetwbuf;
    423       1.7  christos 	do
    424       1.7  christos 		cc = lgetc();
    425       1.7  christos 	while ((cc <= 32) && (cc > '\0'));	/* eat whitespace */
    426       1.7  christos 	for (;; --n, cc = lgetc()) {
    427       1.7  christos 		if ((cc == '\0') && (lgp == lgetwbuf))
    428       1.7  christos 			return (NULL);	/* EOF */
    429       1.7  christos 		if ((n <= 1) || ((cc <= 32) && (quote == 0))) {
    430       1.7  christos 			*lgp = '\0';
    431       1.7  christos 			return (lgetwbuf);
    432       1.1       cgd 		}
    433       1.7  christos 		if (cc != '"')
    434       1.7  christos 			*lgp++ = cc;
    435       1.7  christos 		else
    436       1.7  christos 			quote ^= 1;
    437       1.1       cgd 	}
    438       1.7  christos }
    439       1.1       cgd 
    440       1.1       cgd /*
    441       1.7  christos  *	char *lgetl()	Function to read in a line ended by newline or EOF
    442       1.1       cgd  *
    443       1.7  christos  * Returns pointer to a buffer that contains the line.  If EOF, returns NULL
    444       1.1       cgd  */
    445       1.7  christos char *
    446       1.7  christos lgetl()
    447       1.7  christos {
    448       1.7  christos 	int    i = LINBUFSIZE, ch;
    449       1.7  christos 	char  *str = lgetwbuf;
    450       1.7  christos 	for (;; --i) {
    451       1.7  christos 		if ((*str++ = ch = lgetc()) == '\0') {
    452       1.7  christos 			if (str == lgetwbuf + 1)
    453       1.7  christos 				return (NULL);	/* EOF */
    454       1.7  christos 	ot:		*str = '\0';
    455       1.7  christos 			return (lgetwbuf);	/* line ended by EOF */
    456       1.1       cgd 		}
    457       1.7  christos 		if ((ch == '\n') || (i <= 1))
    458       1.7  christos 			goto ot;/* line ended by \n */
    459       1.1       cgd 	}
    460       1.7  christos }
    461       1.1       cgd 
    462       1.1       cgd /*
    463       1.1       cgd  *	lcreat(filename)			Create a new file for write
    464       1.1       cgd  *		char *filename;
    465       1.1       cgd  *
    466       1.1       cgd  *	lcreat((char*)0); means to the terminal
    467       1.1       cgd  *	Returns -1 if error, otherwise the file descriptor opened.
    468       1.1       cgd  */
    469       1.7  christos int
    470       1.1       cgd lcreat(str)
    471       1.1       cgd 	char *str;
    472       1.7  christos {
    473      1.17     mouse 	lflush();
    474       1.7  christos 	lpnt = lpbuf;
    475       1.7  christos 	lpend = lpbuf + BUFBIG;
    476       1.7  christos 	if (str == NULL)
    477  1.17.4.1      matt 		return (io_outfd = 1);
    478  1.17.4.1      matt 	if ((io_outfd = creat(str, 0644)) < 0) {
    479  1.17.4.1      matt 		io_outfd = 1;
    480       1.7  christos 		lprintf("error creating file <%s>: %s\n", str,
    481       1.5       mrg 			strerror(errno));
    482       1.7  christos 		lflush();
    483       1.7  christos 		return (-1);
    484       1.1       cgd 	}
    485  1.17.4.1      matt 	return (io_outfd);
    486       1.7  christos }
    487       1.1       cgd 
    488       1.1       cgd /*
    489       1.1       cgd  *	lopen(filename)			Open a file for read
    490       1.1       cgd  *		char *filename;
    491       1.1       cgd  *
    492       1.1       cgd  *	lopen(0) means from the terminal
    493       1.1       cgd  *	Returns -1 if error, otherwise the file descriptor opened.
    494       1.1       cgd  */
    495       1.7  christos int
    496       1.1       cgd lopen(str)
    497       1.7  christos 	char           *str;
    498       1.7  christos {
    499       1.1       cgd 	ipoint = iepoint = MAXIBUF;
    500       1.7  christos 	if (str == NULL)
    501  1.17.4.1      matt 		return (io_infd = 0);
    502  1.17.4.1      matt 	if ((io_infd = open(str, O_RDONLY)) < 0) {
    503       1.7  christos 		lwclose();
    504  1.17.4.1      matt 		io_outfd = 1;
    505       1.7  christos 		lpnt = lpbuf;
    506       1.7  christos 		return (-1);
    507       1.1       cgd 	}
    508  1.17.4.1      matt 	return (io_infd);
    509       1.7  christos }
    510       1.1       cgd 
    511       1.1       cgd /*
    512       1.1       cgd  *	lappend(filename)		Open for append to an existing file
    513       1.1       cgd  *		char *filename;
    514       1.1       cgd  *
    515       1.1       cgd  *	lappend(0) means to the terminal
    516       1.1       cgd  *	Returns -1 if error, otherwise the file descriptor opened.
    517       1.1       cgd  */
    518       1.7  christos int
    519       1.1       cgd lappend(str)
    520       1.7  christos 	char           *str;
    521       1.7  christos {
    522       1.7  christos 	lpnt = lpbuf;
    523       1.7  christos 	lpend = lpbuf + BUFBIG;
    524       1.7  christos 	if (str == NULL)
    525  1.17.4.1      matt 		return (io_outfd = 1);
    526  1.17.4.1      matt 	if ((io_outfd = open(str, 2)) < 0) {
    527  1.17.4.1      matt 		io_outfd = 1;
    528       1.7  christos 		return (-1);
    529       1.7  christos 	}
    530  1.17.4.1      matt 	lseek(io_outfd, 0, SEEK_END);	/* seek to end of file */
    531  1.17.4.1      matt 	return (io_outfd);
    532       1.7  christos }
    533       1.1       cgd 
    534       1.1       cgd /*
    535       1.7  christos  *	lrclose() close the input file
    536       1.1       cgd  *
    537       1.1       cgd  *	Returns nothing of value.
    538       1.1       cgd  */
    539       1.7  christos void
    540       1.1       cgd lrclose()
    541       1.7  christos {
    542  1.17.4.1      matt 	if (io_infd > 0) {
    543  1.17.4.1      matt 		close(io_infd);
    544  1.17.4.1      matt 		io_infd = 0;
    545  1.17.4.1      matt 	}
    546       1.7  christos }
    547       1.1       cgd 
    548       1.1       cgd /*
    549       1.7  christos  *	lwclose() close output file flushing if needed
    550       1.1       cgd  *
    551       1.1       cgd  *	Returns nothing of value.
    552       1.1       cgd  */
    553       1.7  christos void
    554       1.1       cgd lwclose()
    555       1.7  christos {
    556       1.7  christos 	lflush();
    557  1.17.4.1      matt 	if (io_outfd > 2) {
    558  1.17.4.1      matt 		close(io_outfd);
    559  1.17.4.1      matt 		io_outfd = 1;
    560  1.17.4.1      matt 	}
    561       1.7  christos }
    562       1.1       cgd 
    563       1.1       cgd /*
    564       1.7  christos  *	lprcat(string)	append a string to the output buffer
    565       1.7  christos  *			    	avoids calls to lprintf (time consuming)
    566       1.1       cgd  */
    567       1.7  christos void
    568  1.17.4.1      matt lprcat(const char *str)
    569       1.7  christos {
    570  1.17.4.1      matt 	u_char  *str2;
    571       1.7  christos 	if (lpnt >= lpend)
    572       1.7  christos 		lflush();
    573       1.1       cgd 	str2 = lpnt;
    574       1.7  christos 	while ((*str2++ = *str++) != '\0')
    575       1.7  christos 		continue;
    576       1.1       cgd 	lpnt = str2 - 1;
    577       1.7  christos }
    578       1.1       cgd 
    579       1.1       cgd #ifdef VT100
    580       1.1       cgd /*
    581       1.1       cgd  *	cursor(x,y) 		Subroutine to set the cursor position
    582       1.1       cgd  *
    583       1.1       cgd  *	x and y are the cursor coordinates, and lpbuff is the output buffer where
    584       1.7  christos  *	escape sequence will be placed.
    585       1.1       cgd  */
    586       1.7  christos static char    *y_num[] = {
    587       1.7  christos "\33[", "\33[", "\33[2", "\33[3", "\33[4", "\33[5", "\33[6",
    588       1.7  christos "\33[7", "\33[8", "\33[9", "\33[10", "\33[11", "\33[12", "\33[13", "\33[14",
    589       1.7  christos "\33[15", "\33[16", "\33[17", "\33[18", "\33[19", "\33[20", "\33[21", "\33[22",
    590       1.7  christos "\33[23", "\33[24"};
    591       1.7  christos 
    592       1.7  christos static char    *x_num[] = {
    593       1.7  christos "H", "H", ";2H", ";3H", ";4H", ";5H", ";6H", ";7H", ";8H", ";9H",
    594       1.7  christos ";10H", ";11H", ";12H", ";13H", ";14H", ";15H", ";16H", ";17H", ";18H", ";19H",
    595       1.7  christos ";20H", ";21H", ";22H", ";23H", ";24H", ";25H", ";26H", ";27H", ";28H", ";29H",
    596       1.7  christos ";30H", ";31H", ";32H", ";33H", ";34H", ";35H", ";36H", ";37H", ";38H", ";39H",
    597       1.7  christos ";40H", ";41H", ";42H", ";43H", ";44H", ";45H", ";46H", ";47H", ";48H", ";49H",
    598       1.7  christos ";50H", ";51H", ";52H", ";53H", ";54H", ";55H", ";56H", ";57H", ";58H", ";59H",
    599       1.7  christos ";60H", ";61H", ";62H", ";63H", ";64H", ";65H", ";66H", ";67H", ";68H", ";69H",
    600       1.7  christos ";70H", ";71H", ";72H", ";73H", ";74H", ";75H", ";76H", ";77H", ";78H", ";79H",
    601       1.7  christos ";80H"};
    602       1.7  christos 
    603       1.7  christos void
    604       1.7  christos cursor(x, y)
    605       1.7  christos 	int             x, y;
    606       1.7  christos {
    607       1.7  christos 	char  *p;
    608       1.7  christos 	if (lpnt >= lpend)
    609       1.7  christos 		lflush();
    610       1.1       cgd 
    611       1.7  christos 	p = y_num[y];		/* get the string to print */
    612       1.7  christos 	while (*p)
    613       1.7  christos 		*lpnt++ = *p++;	/* print the string */
    614       1.7  christos 
    615       1.7  christos 	p = x_num[x];		/* get the string to print */
    616       1.7  christos 	while (*p)
    617       1.7  christos 		*lpnt++ = *p++;	/* print the string */
    618       1.7  christos }
    619       1.7  christos #else	/* VT100 */
    620       1.1       cgd /*
    621       1.1       cgd  * cursor(x,y)	  Put cursor at specified coordinates staring at [1,1] (termcap)
    622       1.1       cgd  */
    623       1.7  christos void
    624       1.7  christos cursor(x, y)
    625       1.7  christos 	int             x, y;
    626       1.7  christos {
    627       1.7  christos 	if (lpnt >= lpend)
    628       1.7  christos 		lflush();
    629       1.1       cgd 
    630       1.7  christos 	*lpnt++ = CURSOR;
    631       1.7  christos 	*lpnt++ = x;
    632       1.7  christos 	*lpnt++ = y;
    633       1.7  christos }
    634       1.7  christos #endif	/* VT100 */
    635       1.1       cgd 
    636       1.1       cgd /*
    637       1.1       cgd  *	Routine to position cursor at beginning of 24th line
    638       1.1       cgd  */
    639       1.7  christos void
    640       1.1       cgd cursors()
    641       1.7  christos {
    642       1.7  christos 	cursor(1, 24);
    643       1.7  christos }
    644       1.1       cgd 
    645       1.1       cgd #ifndef VT100
    646       1.1       cgd /*
    647       1.1       cgd  * Warning: ringing the bell is control code 7. Don't use in defines.
    648       1.1       cgd  * Don't change the order of these defines.
    649       1.1       cgd  * Also used in helpfiles. Codes used in helpfiles should be \E[1 to \E[7 with
    650       1.1       cgd  * obvious meanings.
    651       1.1       cgd  */
    652       1.1       cgd 
    653       1.9     blymn struct tinfo   *info;
    654       1.7  christos char           *CM, *CE, *CD, *CL, *SO, *SE, *AL, *DL;	/* Termcap capabilities */
    655       1.7  christos static char    *outbuf = 0;	/* translated output buffer */
    656       1.1       cgd 
    657       1.1       cgd /*
    658       1.1       cgd  * init_term()		Terminal initialization -- setup termcap info
    659       1.1       cgd  */
    660       1.7  christos void
    661       1.1       cgd init_term()
    662       1.7  christos {
    663       1.7  christos 	char           *term;
    664       1.7  christos 
    665       1.9     blymn 	switch (t_getent(&info, term = getenv("TERM"))) {
    666       1.7  christos 	case -1:
    667       1.7  christos 		write(2, "Cannot open termcap file.\n", 26);
    668       1.7  christos 		exit(1);
    669       1.7  christos 	case 0:
    670       1.7  christos 		write(2, "Cannot find entry of ", 21);
    671       1.7  christos 		write(2, term, strlen(term));
    672       1.7  christos 		write(2, " in termcap\n", 12);
    673       1.7  christos 		exit(1);
    674       1.7  christos 	};
    675       1.7  christos 
    676      1.13  christos 	CM = t_agetstr(info, "cm");	/* Cursor motion */
    677      1.13  christos 	CE = t_agetstr(info, "ce");	/* Clear to eoln */
    678      1.13  christos 	CL = t_agetstr(info, "cl");	/* Clear screen */
    679       1.7  christos 
    680       1.7  christos 	/* OPTIONAL */
    681      1.13  christos 	AL = t_agetstr(info, "al");	/* Insert line */
    682      1.13  christos 	DL = t_agetstr(info, "dl");	/* Delete line */
    683      1.13  christos 	SO = t_agetstr(info, "so");	/* Begin standout mode */
    684      1.13  christos 	SE = t_agetstr(info, "se");	/* End standout mode */
    685      1.13  christos 	CD = t_agetstr(info, "cd");	/* Clear to end of display */
    686       1.7  christos 
    687       1.7  christos 	if (!CM) {		/* can't find cursor motion entry */
    688       1.7  christos 		write(2, "Sorry, for a ", 13);
    689       1.7  christos 		write(2, term, strlen(term));
    690       1.7  christos 		write(2, ", I can't find the cursor motion entry in termcap\n", 50);
    691       1.7  christos 		exit(1);
    692       1.7  christos 	}
    693       1.7  christos 	if (!CE) {		/* can't find clear to end of line entry */
    694       1.7  christos 		write(2, "Sorry, for a ", 13);
    695       1.7  christos 		write(2, term, strlen(term));
    696       1.7  christos 		write(2, ", I can't find the clear to end of line entry in termcap\n", 57);
    697       1.7  christos 		exit(1);
    698       1.7  christos 	}
    699       1.7  christos 	if (!CL) {		/* can't find clear entire screen entry */
    700       1.7  christos 		write(2, "Sorry, for a ", 13);
    701       1.7  christos 		write(2, term, strlen(term));
    702       1.7  christos 		write(2, ", I can't find the clear entire screen entry in termcap\n", 56);
    703       1.7  christos 		exit(1);
    704       1.7  christos 	}
    705       1.7  christos 	if ((outbuf = malloc(BUFBIG + 16)) == 0) {	/* get memory for
    706       1.7  christos 							 * decoded output buffer */
    707       1.7  christos 		write(2, "Error malloc'ing memory for decoded output buffer\n", 50);
    708       1.1       cgd 		died(-285);	/* malloc() failure */
    709       1.1       cgd 	}
    710       1.7  christos }
    711       1.7  christos #endif	/* VT100 */
    712       1.1       cgd 
    713       1.1       cgd /*
    714       1.1       cgd  * cl_line(x,y)  Clear the whole line indicated by 'y' and leave cursor at [x,y]
    715       1.1       cgd  */
    716       1.7  christos void
    717       1.7  christos cl_line(x, y)
    718       1.7  christos 	int             x, y;
    719       1.7  christos {
    720       1.1       cgd #ifdef VT100
    721       1.7  christos 	cursor(x, y);
    722       1.7  christos 	lprcat("\33[2K");
    723       1.7  christos #else	/* VT100 */
    724       1.7  christos 	cursor(1, y);
    725       1.7  christos 	*lpnt++ = CL_LINE;
    726       1.7  christos 	cursor(x, y);
    727       1.7  christos #endif	/* VT100 */
    728       1.7  christos }
    729       1.1       cgd 
    730       1.1       cgd /*
    731       1.1       cgd  * cl_up(x,y) Clear screen from [x,1] to current position. Leave cursor at [x,y]
    732       1.1       cgd  */
    733       1.7  christos void
    734       1.7  christos cl_up(x, y)
    735       1.7  christos 	int    x, y;
    736       1.7  christos {
    737       1.1       cgd #ifdef VT100
    738       1.7  christos 	cursor(x, y);
    739       1.7  christos 	lprcat("\33[1J\33[2K");
    740       1.7  christos #else	/* VT100 */
    741       1.7  christos 	int    i;
    742       1.7  christos 	cursor(1, 1);
    743       1.7  christos 	for (i = 1; i <= y; i++) {
    744       1.7  christos 		*lpnt++ = CL_LINE;
    745       1.7  christos 		*lpnt++ = '\n';
    746       1.1       cgd 	}
    747       1.7  christos 	cursor(x, y);
    748       1.7  christos #endif	/* VT100 */
    749       1.7  christos }
    750       1.1       cgd 
    751       1.1       cgd /*
    752       1.1       cgd  * cl_dn(x,y) 	Clear screen from [1,y] to end of display. Leave cursor at [x,y]
    753       1.1       cgd  */
    754       1.7  christos void
    755       1.7  christos cl_dn(x, y)
    756       1.7  christos 	int    x, y;
    757       1.7  christos {
    758       1.1       cgd #ifdef VT100
    759       1.7  christos 	cursor(x, y);
    760       1.7  christos 	lprcat("\33[J\33[2K");
    761       1.7  christos #else	/* VT100 */
    762       1.7  christos 	int    i;
    763       1.7  christos 	cursor(1, y);
    764       1.7  christos 	if (!CD) {
    765       1.1       cgd 		*lpnt++ = CL_LINE;
    766       1.7  christos 		for (i = y; i <= 24; i++) {
    767       1.7  christos 			*lpnt++ = CL_LINE;
    768       1.7  christos 			if (i != 24)
    769       1.7  christos 				*lpnt++ = '\n';
    770       1.1       cgd 		}
    771       1.7  christos 		cursor(x, y);
    772       1.7  christos 	} else
    773       1.1       cgd 		*lpnt++ = CL_DOWN;
    774       1.7  christos 	cursor(x, y);
    775       1.7  christos #endif	/* VT100 */
    776       1.7  christos }
    777       1.1       cgd 
    778       1.1       cgd /*
    779       1.1       cgd  * standout(str)	Print the argument string in inverse video (standout mode).
    780       1.1       cgd  */
    781       1.7  christos void
    782  1.17.4.1      matt standout(const char *str)
    783       1.7  christos {
    784       1.1       cgd #ifdef VT100
    785       1.1       cgd 	setbold();
    786       1.1       cgd 	while (*str)
    787       1.1       cgd 		*lpnt++ = *str++;
    788       1.1       cgd 	resetbold();
    789       1.7  christos #else	/* VT100 */
    790       1.1       cgd 	*lpnt++ = ST_START;
    791       1.1       cgd 	while (*str)
    792       1.1       cgd 		*lpnt++ = *str++;
    793       1.1       cgd 	*lpnt++ = ST_END;
    794       1.7  christos #endif	/* VT100 */
    795       1.7  christos }
    796       1.1       cgd 
    797       1.1       cgd /*
    798       1.1       cgd  * set_score_output() 	Called when output should be literally printed.
    799       1.1       cgd  */
    800       1.7  christos void
    801       1.1       cgd set_score_output()
    802       1.7  christos {
    803       1.1       cgd 	enable_scroll = -1;
    804       1.7  christos }
    805       1.1       cgd 
    806       1.1       cgd /*
    807       1.7  christos  *	lflush()	Flush the output buffer
    808       1.1       cgd  *
    809       1.1       cgd  *	Returns nothing of value.
    810       1.1       cgd  *	for termcap version: Flush output in output buffer according to output
    811       1.7  christos  *	status as indicated by `enable_scroll'
    812       1.1       cgd  */
    813       1.1       cgd #ifndef VT100
    814       1.7  christos static int      scrline = 18;	/* line # for wraparound instead of scrolling
    815       1.7  christos 				 * if no DL */
    816       1.7  christos void
    817       1.7  christos lflush()
    818       1.7  christos {
    819       1.7  christos 	int    lpoint;
    820       1.7  christos 	u_char  *str;
    821       1.7  christos 	static int      curx = 0;
    822       1.7  christos 	static int      cury = 0;
    823       1.9     blymn 	char tgoto_buf[256];
    824       1.1       cgd 
    825       1.7  christos 	if ((lpoint = lpnt - lpbuf) > 0) {
    826       1.1       cgd #ifdef EXTRA
    827       1.1       cgd 		c[BYTESOUT] += lpoint;
    828       1.1       cgd #endif
    829       1.7  christos 		if (enable_scroll <= -1) {
    830       1.1       cgd 			flush_buf();
    831  1.17.4.1      matt 			if (write(io_outfd, lpbuf, lpoint) != lpoint)
    832       1.7  christos 				write(2, "error writing to output file\n", 29);
    833       1.1       cgd 			lpnt = lpbuf;	/* point back to beginning of buffer */
    834       1.1       cgd 			return;
    835       1.7  christos 		}
    836       1.7  christos 		for (str = lpbuf; str < lpnt; str++) {
    837       1.7  christos 			if (*str >= 32) {
    838  1.17.4.1      matt 				ttputch(*str);
    839       1.7  christos 				curx++;
    840       1.7  christos 			} else
    841       1.7  christos 				switch (*str) {
    842       1.7  christos 				case CLEAR:
    843  1.17.4.1      matt 					tputs(CL, 0, ttputch);
    844       1.7  christos 					curx = cury = 0;
    845       1.7  christos 					break;
    846       1.7  christos 
    847       1.7  christos 				case CL_LINE:
    848  1.17.4.1      matt 					tputs(CE, 0, ttputch);
    849       1.7  christos 					break;
    850       1.7  christos 
    851       1.7  christos 				case CL_DOWN:
    852  1.17.4.1      matt 					tputs(CD, 0, ttputch);
    853       1.7  christos 					break;
    854       1.7  christos 
    855       1.7  christos 				case ST_START:
    856  1.17.4.1      matt 					tputs(SO, 0, ttputch);
    857       1.7  christos 					break;
    858       1.7  christos 
    859       1.7  christos 				case ST_END:
    860  1.17.4.1      matt 					tputs(SE, 0, ttputch);
    861       1.7  christos 					break;
    862       1.7  christos 
    863       1.7  christos 				case CURSOR:
    864       1.7  christos 					curx = *++str - 1;
    865       1.7  christos 					cury = *++str - 1;
    866       1.9     blymn 					if (t_goto(info, CM, curx, cury,
    867       1.9     blymn 						   tgoto_buf, 255) == 0)
    868  1.17.4.1      matt 						tputs(tgoto_buf, 0, ttputch);
    869       1.7  christos 					break;
    870       1.7  christos 
    871       1.7  christos 				case '\n':
    872       1.7  christos 					if ((cury == 23) && enable_scroll) {
    873       1.7  christos 						if (!DL || !AL) {	/* wraparound or scroll? */
    874       1.7  christos 							if (++scrline > 23)
    875       1.7  christos 								scrline = 19;
    876       1.7  christos 
    877       1.7  christos 							if (++scrline > 23)
    878       1.7  christos 								scrline = 19;
    879       1.9     blymn 							if (t_goto(info, CM, 0,
    880       1.9     blymn 								   scrline,
    881       1.9     blymn 								   tgoto_buf,
    882       1.9     blymn 								   255) == 0)
    883       1.9     blymn 								tputs(tgoto_buf,
    884       1.9     blymn 								      0,
    885  1.17.4.1      matt 								      ttputch);
    886  1.17.4.1      matt 							tputs(CE, 0, ttputch);
    887       1.7  christos 
    888       1.7  christos 							if (--scrline < 19)
    889       1.7  christos 								scrline = 23;
    890       1.9     blymn 							if (t_goto(info, CM, 0,
    891       1.9     blymn 								   scrline,
    892       1.9     blymn 								   tgoto_buf,
    893       1.9     blymn 								   255) == 0)
    894       1.9     blymn 								tputs(tgoto_buf,
    895       1.9     blymn 								      0,
    896  1.17.4.1      matt 								      ttputch);
    897  1.17.4.1      matt 							tputs(CE, 0, ttputch);
    898       1.7  christos 						} else {
    899       1.9     blymn 							if (t_goto(info, CM, 0,
    900       1.9     blymn 								   19,
    901       1.9     blymn 								   tgoto_buf,
    902       1.9     blymn 								   255) == 0)
    903       1.9     blymn 								tputs(tgoto_buf,
    904       1.9     blymn 								      0,
    905  1.17.4.1      matt 								      ttputch);
    906  1.17.4.1      matt 							tputs(DL, 0, ttputch);
    907       1.9     blymn 							if (t_goto(info, CM, 0,
    908       1.9     blymn 								   23,
    909       1.9     blymn 								   tgoto_buf,
    910       1.9     blymn 								   255) == 0)
    911       1.9     blymn 								tputs(tgoto_buf,
    912       1.9     blymn 								      0,
    913  1.17.4.1      matt 								      ttputch);
    914       1.7  christos 							/*
    915       1.7  christos 							 * tputs (AL, 0,
    916  1.17.4.1      matt 							 * ttputch);
    917       1.7  christos 							 */
    918       1.7  christos 						}
    919       1.7  christos 					} else {
    920  1.17.4.1      matt 						ttputch('\n');
    921       1.7  christos 						cury++;
    922       1.7  christos 					}
    923       1.7  christos 					curx = 0;
    924       1.7  christos 					break;
    925       1.1       cgd 
    926       1.7  christos 				default:
    927  1.17.4.1      matt 					ttputch(*str);
    928       1.7  christos 					curx++;
    929       1.1       cgd 				};
    930       1.1       cgd 		}
    931       1.7  christos 	}
    932       1.1       cgd 	lpnt = lpbuf;
    933       1.7  christos 	flush_buf();		/* flush real output buffer now */
    934       1.7  christos }
    935       1.7  christos #else	/* VT100 */
    936       1.1       cgd /*
    937       1.7  christos  *	lflush()		flush the output buffer
    938       1.1       cgd  *
    939       1.1       cgd  *	Returns nothing of value.
    940       1.1       cgd  */
    941       1.7  christos void
    942       1.1       cgd lflush()
    943       1.7  christos {
    944       1.7  christos 	int    lpoint;
    945       1.7  christos 	if ((lpoint = lpnt - lpbuf) > 0) {
    946       1.1       cgd #ifdef EXTRA
    947       1.1       cgd 		c[BYTESOUT] += lpoint;
    948       1.1       cgd #endif
    949  1.17.4.1      matt 		if (write(io_outfd, lpbuf, lpoint) != lpoint)
    950       1.7  christos 			write(2, "error writing to output file\n", 29);
    951       1.7  christos 	}
    952       1.7  christos 	lpnt = lpbuf;		/* point back to beginning of buffer */
    953       1.7  christos }
    954       1.7  christos #endif	/* VT100 */
    955       1.1       cgd 
    956       1.1       cgd #ifndef VT100
    957       1.7  christos static int      vindex = 0;
    958       1.1       cgd /*
    959  1.17.4.1      matt  * ttputch(ch)		Print one character in decoded output buffer.
    960       1.1       cgd  */
    961  1.17.4.1      matt static int
    962  1.17.4.1      matt ttputch(int ch)
    963       1.7  christos {
    964  1.17.4.1      matt 	outbuf[vindex++] = ch;
    965       1.7  christos 	if (vindex >= BUFBIG)
    966       1.7  christos 		flush_buf();
    967       1.8     lukem 	return (0);
    968       1.7  christos }
    969       1.1       cgd 
    970       1.1       cgd /*
    971       1.1       cgd  * flush_buf()			Flush buffer with decoded output.
    972       1.1       cgd  */
    973  1.17.4.1      matt static void
    974       1.1       cgd flush_buf()
    975       1.7  christos {
    976       1.7  christos 	if (vindex)
    977  1.17.4.1      matt 		write(io_outfd, outbuf, vindex);
    978       1.4       cgd 	vindex = 0;
    979       1.7  christos }
    980       1.1       cgd 
    981       1.1       cgd /*
    982       1.7  christos  *	char *tmcapcnv(sd,ss)  Routine to convert VT100 escapes to termcap
    983       1.7  christos  *	format
    984       1.7  christos  *	Processes only the \33[#m sequence (converts . files for termcap use
    985       1.7  christos  */
    986       1.7  christos char *
    987       1.7  christos tmcapcnv(sd, ss)
    988       1.7  christos 	char  *sd, *ss;
    989       1.7  christos {
    990       1.7  christos 	int    tmstate = 0;	/* 0=normal, 1=\33 2=[ 3=# */
    991       1.7  christos 	char            tmdigit = 0;	/* the # in \33[#m */
    992       1.7  christos 	while (*ss) {
    993       1.7  christos 		switch (tmstate) {
    994       1.7  christos 		case 0:
    995       1.7  christos 			if (*ss == '\33') {
    996       1.7  christos 				tmstate++;
    997       1.7  christos 				break;
    998       1.7  christos 			}
    999       1.7  christos 	ign:		*sd++ = *ss;
   1000       1.7  christos 	ign2:		tmstate = 0;
   1001       1.7  christos 			break;
   1002       1.7  christos 		case 1:
   1003       1.7  christos 			if (*ss != '[')
   1004       1.7  christos 				goto ign;
   1005       1.7  christos 			tmstate++;
   1006       1.7  christos 			break;
   1007       1.7  christos 		case 2:
   1008       1.7  christos 			if (isdigit((u_char)*ss)) {
   1009       1.7  christos 				tmdigit = *ss - '0';
   1010       1.7  christos 				tmstate++;
   1011       1.7  christos 				break;
   1012       1.7  christos 			}
   1013       1.7  christos 			if (*ss == 'm') {
   1014       1.7  christos 				*sd++ = ST_END;
   1015       1.7  christos 				goto ign2;
   1016       1.7  christos 			}
   1017       1.7  christos 			goto ign;
   1018       1.7  christos 		case 3:
   1019       1.7  christos 			if (*ss == 'm') {
   1020       1.7  christos 				if (tmdigit)
   1021       1.7  christos 					*sd++ = ST_START;
   1022       1.7  christos 				else
   1023       1.7  christos 					*sd++ = ST_END;
   1024       1.7  christos 				goto ign2;
   1025       1.7  christos 			}
   1026       1.7  christos 		default:
   1027       1.7  christos 			goto ign;
   1028       1.7  christos 		};
   1029       1.1       cgd 		ss++;
   1030       1.1       cgd 	}
   1031       1.7  christos 	*sd = 0;		/* NULL terminator */
   1032       1.7  christos 	return (sd);
   1033       1.7  christos }
   1034       1.7  christos #endif	/* VT100 */
   1035       1.1       cgd 
   1036       1.1       cgd /*
   1037       1.7  christos  *	beep()	Routine to emit a beep if enabled (see no-beep in .larnopts)
   1038       1.1       cgd  */
   1039       1.7  christos void
   1040       1.1       cgd beep()
   1041       1.7  christos {
   1042       1.7  christos 	if (!nobeep)
   1043       1.7  christos 		*lpnt++ = '\7';
   1044       1.7  christos }
   1045