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