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