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