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