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