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