1 /* $NetBSD: io.c,v 1.23 2008/02/03 21:24:58 dholland Exp $ */
4 * io.c Larn is copyrighted 1986 by Noah Morgan.
6 * Below are the functions in this file:
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 * ttgetch() 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()
15 * FILE OUTPUT ROUTINES
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
22 * FILE OUTPUT MACROS (in header.h)
24 * lprc(character) put the character into the output
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
36 * FILE OPEN / CLOSE ROUTINES
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
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. * ttputch(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)
61 * Note: ** entries are available only in termcap mode.
63 #include <sys/cdefs.h>
65 __RCSID("$NetBSD: io.c,v 1.23 2008/02/03 21:24:58 dholland Exp $");
82 #define stty(_a,_b) ioctl(_a,TCSETA,_b)
83 #define gtty(_a,_b) ioctl(_a,TCGETA,_b)
87 #define sgttyb termios
88 #define stty(_a,_b) tcsetattr(_a,TCSADRAIN,_b)
89 #define gtty(_a,_b) tcgetattr(_a,_b)
92 #if defined(TERMIO) || defined(TERMIOS)
93 static int rawflg
= 0;
94 static char saveeof
, saveeol
;
98 saveeof = _a.c_cc[VMIN]; \
99 saveeol = _a.c_cc[VTIME]; \
102 _a.c_cc[VTIME] = 1; \
103 _a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL)
105 _a.c_cc[VMIN] = saveeof; \
106 _a.c_cc[VTIME] = saveeol; \
107 _a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL
109 #else /* not TERMIO or TERMIOS */
112 #define CBREAK RAW /* V7 has no CBREAK */
115 #define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO)
116 #define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO)
118 #endif /* not TERMIO or TERMIOS */
120 #ifndef NOVARARGS /* if we have varargs */
122 #else /* NOVARARGS */ /* if we don't have varargs */
123 typedef char *va_list;
124 #define va_dcl int va_alist;
125 #define va_start(plist) plist = (char *) &va_alist
126 #define va_end(plist)
127 #define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
128 #endif /* NOVARARGS */
130 static int ttputch(int ch
);
131 static void flush_buf(void);
133 #define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
134 int io_outfd
; /* output file numbers */
135 int io_infd
; /* input file numbers */
136 static struct sgttyb ttx
;/* storage for the tty modes */
137 static int ipoint
= MAXIBUF
, iepoint
= MAXIBUF
; /* input buffering
139 static char lgetwbuf
[LINBUFSIZE
]; /* get line (word) buffer */
142 * setupvt100() Subroutine to set up terminal in correct mode for game
144 * Attributes off, clear screen, set scrolling region, set tty mode
151 scbr(); /* system("stty cbreak -echo"); */
155 * clearvt100() Subroutine to clean up terminal when the game is over
157 * Attributes off, clear screen, unset scrolling region, restore tty mode
164 sncbr(); /* system("stty -cbreak echo"); */
168 * ttgetch() Routine to read in one character from the terminal
177 lflush(); /* be sure output buffer is flushed */
178 read(0, &byt
, 1); /* get byte from terminal */
183 * scbr() Function to set cbreak -echo for the terminal
185 * like: system("stty cbreak -echo")
196 * sncbr() Function to set -cbreak echo for the terminal
198 * like: system("stty -cbreak echo")
209 * newgame() Subroutine to save the initial time and seed rnd()
215 for (p
= c
, pe
= c
+ 100; p
< pe
; *p
++ = 0);
217 seedrand(initialtime
);
218 srandom(initialtime
);
219 lcreat((char *) 0); /* open buffering for output to terminal */
223 * lprintf(format,args . . .) printf to the output buffer
227 * Enter with the format string in "format", as per printf() usage
228 * and any needed arguments following it
229 * Note: lprintf() only supports %s, %c and %d, with width modifier and left
230 * or right justification.
231 * No correct checking for output buffer overflow is done, but flushes
232 * are done beforehand if needed.
233 * Returns nothing of value.
236 lprintf(const char *fmt
, ...)
242 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
252 * lprint(long-integer) send binary integer to output buffer
255 * +---------+---------+---------+---------+
257 * | order | | | order |
258 * | byte | | | byte |
259 * +---------+---------+---------+---------+
260 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
262 * The save order is low order first, to high order (4 bytes total)
263 * and is written to be system independent.
264 * No checking for output buffer overflow is done, but flushes if needed!
265 * Returns nothing of value.
274 *lpnt
++ = 255 & (x
>> 8);
275 *lpnt
++ = 255 & (x
>> 16);
276 *lpnt
++ = 255 & (x
>> 24);
280 * lwrite(buf,len) write a buffer to the output buffer
284 * Enter with the address and number of bytes to write out
285 * Returns nothing of value
296 if (len
> 399) { /* don't copy data if can just write it */
302 for (s
= buf
; len
> 0; --len
)
306 write(io_outfd
, buf
, len
);
311 lflush(); /* if buffer is full flush it */
312 num2
= lpbuf
+ BUFBIG
- lpnt
; /* # bytes left in
319 *t
++ = *buf
++; /* copy in the bytes */
325 * long lgetc() Read one character from input buffer
327 * Returns 0 if EOF, otherwise the character
333 if (ipoint
!= iepoint
)
334 return (inbuffer
[ipoint
++]);
335 if (iepoint
!= MAXIBUF
)
337 if ((i
= read(io_infd
, inbuffer
, MAXIBUF
)) <= 0) {
339 write(1, "error reading from input file\n", 30);
340 iepoint
= ipoint
= 0;
349 * long lrint() Read one integer from input buffer
351 * +---------+---------+---------+---------+
353 * | order | | | order |
354 * | byte | | | byte |
355 * +---------+---------+---------+---------+
356 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
358 * The save order is low order first, to high order (4 bytes total)
359 * Returns the int read
366 i
|= (255 & lgetc()) << 8;
367 i
|= (255 & lgetc()) << 16;
368 i
|= (255 & lgetc()) << 24;
373 * lrfill(address,number) put input bytes into a buffer
377 * Reads "number" bytes into the buffer pointed to by "address".
378 * Returns nothing of value
389 if (iepoint
== ipoint
) {
390 if (num
> 5) { /* fast way */
391 if (read(io_infd
, adr
, num
) != num
)
392 write(2, "error reading from input file\n", 30);
399 num2
= iepoint
- ipoint
; /* # of bytes left in
403 pnt
= inbuffer
+ ipoint
;
413 * char *lgetw() Get a whitespace ended word from input
415 * Returns pointer to a buffer that contains word. If EOF, returns a NULL
421 int n
= LINBUFSIZE
, quote
= 0;
425 while ((cc
<= 32) && (cc
> '\0')); /* eat whitespace */
426 for (;; --n
, cc
= lgetc()) {
427 if ((cc
== '\0') && (lgp
== lgetwbuf
))
428 return (NULL
); /* EOF */
429 if ((n
<= 1) || ((cc
<= 32) && (quote
== 0))) {
441 * char *lgetl() Function to read in a line ended by newline or EOF
443 * Returns pointer to a buffer that contains the line. If EOF, returns NULL
448 int i
= LINBUFSIZE
, ch
;
449 char *str
= lgetwbuf
;
451 if ((*str
++ = ch
= lgetc()) == '\0') {
452 if (str
== lgetwbuf
+ 1)
453 return (NULL
); /* EOF */
455 return (lgetwbuf
); /* line ended by EOF */
457 if ((ch
== '\n') || (i
<= 1))
458 goto ot
;/* line ended by \n */
463 * lcreat(filename) Create a new file for write
466 * lcreat((char*)0); means to the terminal
467 * Returns -1 if error, otherwise the file descriptor opened.
475 lpend
= lpbuf
+ BUFBIG
;
477 return (io_outfd
= 1);
478 if ((io_outfd
= creat(str
, 0644)) < 0) {
480 lprintf("error creating file <%s>: %s\n", str
,
489 * lopen(filename) Open a file for read
492 * lopen(0) means from the terminal
493 * Returns -1 if error, otherwise the file descriptor opened.
499 ipoint
= iepoint
= MAXIBUF
;
501 return (io_infd
= 0);
502 if ((io_infd
= open(str
, O_RDONLY
)) < 0) {
512 * lappend(filename) Open for append to an existing file
515 * lappend(0) means to the terminal
516 * Returns -1 if error, otherwise the file descriptor opened.
523 lpend
= lpbuf
+ BUFBIG
;
525 return (io_outfd
= 1);
526 if ((io_outfd
= open(str
, 2)) < 0) {
530 lseek(io_outfd
, 0, SEEK_END
); /* seek to end of file */
535 * lrclose() close the input file
537 * Returns nothing of value.
549 * lwclose() close output file flushing if needed
551 * Returns nothing of value.
564 * lprcat(string) append a string to the output buffer
565 * avoids calls to lprintf (time consuming)
568 lprcat(const char *str
)
574 while ((*str2
++ = *str
++) != '\0')
581 * cursor(x,y) Subroutine to set the cursor position
583 * x and y are the cursor coordinates, and lpbuff is the output buffer where
584 * escape sequence will be placed.
586 static char *y_num
[] = {
587 "\33[", "\33[", "\33[2", "\33[3", "\33[4", "\33[5", "\33[6",
588 "\33[7", "\33[8", "\33[9", "\33[10", "\33[11", "\33[12", "\33[13", "\33[14",
589 "\33[15", "\33[16", "\33[17", "\33[18", "\33[19", "\33[20", "\33[21", "\33[22",
592 static char *x_num
[] = {
593 "H", "H", ";2H", ";3H", ";4H", ";5H", ";6H", ";7H", ";8H", ";9H",
594 ";10H", ";11H", ";12H", ";13H", ";14H", ";15H", ";16H", ";17H", ";18H", ";19H",
595 ";20H", ";21H", ";22H", ";23H", ";24H", ";25H", ";26H", ";27H", ";28H", ";29H",
596 ";30H", ";31H", ";32H", ";33H", ";34H", ";35H", ";36H", ";37H", ";38H", ";39H",
597 ";40H", ";41H", ";42H", ";43H", ";44H", ";45H", ";46H", ";47H", ";48H", ";49H",
598 ";50H", ";51H", ";52H", ";53H", ";54H", ";55H", ";56H", ";57H", ";58H", ";59H",
599 ";60H", ";61H", ";62H", ";63H", ";64H", ";65H", ";66H", ";67H", ";68H", ";69H",
600 ";70H", ";71H", ";72H", ";73H", ";74H", ";75H", ";76H", ";77H", ";78H", ";79H",
611 p
= y_num
[y
]; /* get the string to print */
613 *lpnt
++ = *p
++; /* print the string */
615 p
= x_num
[x
]; /* get the string to print */
617 *lpnt
++ = *p
++; /* print the string */
621 * cursor(x,y) Put cursor at specified coordinates staring at [1,1] (termcap)
637 * Routine to position cursor at beginning of 24th line
647 * Warning: ringing the bell is control code 7. Don't use in defines.
648 * Don't change the order of these defines.
649 * Also used in helpfiles. Codes used in helpfiles should be \E[1 to \E[7 with
653 static struct tinfo
*info
;
654 static char *CM
, *CE
, *CD
, *CL
, *SO
, *SE
, *AL
, *DL
; /* Termcap capabilities */
655 static char *outbuf
= 0; /* translated output buffer */
658 * init_term() Terminal initialization -- setup termcap info
665 switch (t_getent(&info
, term
= getenv("TERM"))) {
667 write(2, "Cannot open termcap file.\n", 26);
670 write(2, "Cannot find entry of ", 21);
671 write(2, term
, strlen(term
));
672 write(2, " in termcap\n", 12);
676 CM
= t_agetstr(info
, "cm"); /* Cursor motion */
677 CE
= t_agetstr(info
, "ce"); /* Clear to eoln */
678 CL
= t_agetstr(info
, "cl"); /* Clear screen */
681 AL
= t_agetstr(info
, "al"); /* Insert line */
682 DL
= t_agetstr(info
, "dl"); /* Delete line */
683 SO
= t_agetstr(info
, "so"); /* Begin standout mode */
684 SE
= t_agetstr(info
, "se"); /* End standout mode */
685 CD
= t_agetstr(info
, "cd"); /* Clear to end of display */
687 if (!CM
) { /* can't find cursor motion entry */
688 write(2, "Sorry, for a ", 13);
689 write(2, term
, strlen(term
));
690 write(2, ", I can't find the cursor motion entry in termcap\n", 50);
693 if (!CE
) { /* can't find clear to end of line entry */
694 write(2, "Sorry, for a ", 13);
695 write(2, term
, strlen(term
));
696 write(2, ", I can't find the clear to end of line entry in termcap\n", 57);
699 if (!CL
) { /* can't find clear entire screen entry */
700 write(2, "Sorry, for a ", 13);
701 write(2, term
, strlen(term
));
702 write(2, ", I can't find the clear entire screen entry in termcap\n", 56);
705 if ((outbuf
= malloc(BUFBIG
+ 16)) == 0) { /* get memory for
706 * decoded output buffer */
707 write(2, "Error malloc'ing memory for decoded output buffer\n", 50);
708 died(-285); /* malloc() failure */
714 * cl_line(x,y) Clear the whole line indicated by 'y' and leave cursor at [x,y]
731 * cl_up(x,y) Clear screen from [x,1] to current position. Leave cursor at [x,y]
739 lprcat("\33[1J\33[2K");
743 for (i
= 1; i
<= y
; i
++) {
752 * cl_dn(x,y) Clear screen from [1,y] to end of display. Leave cursor at [x,y]
760 lprcat("\33[J\33[2K");
766 for (i
= y
; i
<= 24; i
++) {
779 * standout(str) Print the argument string in inverse video (standout mode).
782 standout(const char *str
)
798 * set_score_output() Called when output should be literally printed.
807 * lflush() Flush the output buffer
809 * Returns nothing of value.
810 * for termcap version: Flush output in output buffer according to output
811 * status as indicated by `enable_scroll'
814 static int scrline
= 18; /* line # for wraparound instead of scrolling
825 if ((lpoint
= lpnt
- lpbuf
) > 0) {
827 c
[BYTESOUT
] += lpoint
;
829 if (enable_scroll
<= -1) {
831 if (write(io_outfd
, lpbuf
, lpoint
) != lpoint
)
832 write(2, "error writing to output file\n", 29);
833 lpnt
= lpbuf
; /* point back to beginning of buffer */
836 for (str
= lpbuf
; str
< lpnt
; str
++) {
843 tputs(CL
, 0, ttputch
);
848 tputs(CE
, 0, ttputch
);
852 tputs(CD
, 0, ttputch
);
856 tputs(SO
, 0, ttputch
);
860 tputs(SE
, 0, ttputch
);
866 if (t_goto(info
, CM
, curx
, cury
,
867 tgoto_buf
, 255) == 0)
868 tputs(tgoto_buf
, 0, ttputch
);
872 if ((cury
== 23) && enable_scroll
) {
873 if (!DL
|| !AL
) { /* wraparound or scroll? */
879 if (t_goto(info
, CM
, 0,
886 tputs(CE
, 0, ttputch
);
890 if (t_goto(info
, CM
, 0,
897 tputs(CE
, 0, ttputch
);
899 if (t_goto(info
, CM
, 0,
906 tputs(DL
, 0, ttputch
);
907 if (t_goto(info
, CM
, 0,
933 flush_buf(); /* flush real output buffer now */
937 * lflush() flush the output buffer
939 * Returns nothing of value.
945 if ((lpoint
= lpnt
- lpbuf
) > 0) {
947 c
[BYTESOUT
] += lpoint
;
949 if (write(io_outfd
, lpbuf
, lpoint
) != lpoint
)
950 write(2, "error writing to output file\n", 29);
952 lpnt
= lpbuf
; /* point back to beginning of buffer */
957 static int vindex
= 0;
959 * ttputch(ch) Print one character in decoded output buffer.
964 outbuf
[vindex
++] = ch
;
965 if (vindex
>= BUFBIG
)
971 * flush_buf() Flush buffer with decoded output.
977 write(io_outfd
, outbuf
, vindex
);
982 * char *tmcapcnv(sd,ss) Routine to convert VT100 escapes to termcap
984 * Processes only the \33[#m sequence (converts . files for termcap use
990 int tmstate
= 0; /* 0=normal, 1=\33 2=[ 3=# */
991 char tmdigit
= 0; /* the # in \33[#m */
1008 if (isdigit((u_char
)*ss
)) {
1009 tmdigit
= *ss
- '0';
1031 *sd
= 0; /* NULL terminator */
1037 * beep() Routine to emit a beep if enabled (see no-beep in .larnopts)