]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/io.c
1 /* $NetBSD: io.c,v 1.11 2001/02/05 00:57:33 christos 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 * 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()
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 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. * 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)
61 * Note: ** entries are available only in termcap mode.
63 #include <sys/cdefs.h>
65 __RCSID("$NetBSD: io.c,v 1.11 2001/02/05 00:57:33 christos Exp $");
80 #define stty(_a,_b) ioctl(_a,TCSETA,_b)
81 #define gtty(_a,_b) ioctl(_a,TCGETA,_b)
85 #define sgttyb termios
86 #define stty(_a,_b) tcsetattr(_a,TCSADRAIN,_b)
87 #define gtty(_a,_b) tcgetattr(_a,_b)
90 #if defined(TERMIO) || defined(TERMIOS)
91 static int rawflg
= 0;
92 static char saveeof
, saveeol
;
96 saveeof = _a.c_cc[VMIN]; \
97 saveeol = _a.c_cc[VTIME]; \
100 _a.c_cc[VTIME] = 1; \
101 _a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL)
103 _a.c_cc[VMIN] = saveeof; \
104 _a.c_cc[VTIME] = saveeol; \
105 _a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL
107 #else /* not TERMIO or TERMIOS */
110 #define CBREAK RAW /* V7 has no CBREAK */
113 #define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO)
114 #define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO)
116 #endif /* not TERMIO or TERMIOS */
118 #ifndef NOVARARGS /* if we have varargs */
124 #else /* NOVARARGS */ /* if we don't have varargs */
125 typedef char *va_list;
126 #define va_dcl int va_alist;
127 #define va_start(plist) plist = (char *) &va_alist
128 #define va_end(plist)
129 #define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
130 #endif /* NOVARARGS */
132 #define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
133 int lfd
; /* output file numbers */
134 int fd
; /* input file numbers */
135 static struct sgttyb ttx
;/* storage for the tty modes */
136 static int ipoint
= MAXIBUF
, iepoint
= MAXIBUF
; /* input buffering
138 static char lgetwbuf
[LINBUFSIZE
]; /* get line (word) buffer */
141 * setupvt100() Subroutine to set up terminal in correct mode for game
143 * Attributes off, clear screen, set scrolling region, set tty mode
150 scbr(); /* system("stty cbreak -echo"); */
154 * clearvt100() Subroutine to clean up terminal when the game is over
156 * Attributes off, clear screen, unset scrolling region, restore tty mode
163 sncbr(); /* system("stty -cbreak echo"); */
167 * lgetchar() Routine to read in one character from the terminal
176 lflush(); /* be sure output buffer is flushed */
177 read(0, &byt
, 1); /* get byte from terminal */
182 * scbr() Function to set cbreak -echo for the terminal
184 * like: system("stty cbreak -echo")
195 * sncbr() Function to set -cbreak echo for the terminal
197 * like: system("stty -cbreak echo")
208 * newgame() Subroutine to save the initial time and seed rnd()
214 for (p
= c
, pe
= c
+ 100; p
< pe
; *p
++ = 0);
217 lcreat((char *) 0); /* open buffering for output to terminal */
221 * lprintf(format,args . . .) printf to the output buffer
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.
240 str
= str2
; /* to make lint happy */
248 str
= str2
; /* to make lint happy */
253 void lprintf(const char *fmt
, ...)
260 va_list ap
; /* pointer for variable argument list */
262 long wide
, left
, cont
, n
; /* data for lprintf */
263 char db
[12]; /* %d buffer in lprintf */
267 va_start(ap
); /* initialize the var args pointer */
268 fmt
= va_arg(ap
, char *); /* pointer to format string */
289 n
= va_arg(ap
, long);
297 *tmpb
= (char) (n
% 10 + '0');
299 *(--tmpb
) = (char) ((n
/= 10) % 10 + '0');
301 while (tmpb
< db
+ 12)
304 wide
-= db
- tmpb
+ 12;
308 while (tmpb
< db
+ 12)
318 tmpb
= va_arg(ap
, char *);
320 while ((*outb
++ = *tmpb
++) != '\0')
324 n
= wide
- strlen(tmpb
);
328 while ((*outb
++ = *tmpb
++) != '\0')
339 *outb
++ = va_arg(ap
, int);
353 wide
= 10 * wide
+ *fmt
- '0';
372 * lprint(long-integer) send binary integer to output buffer
375 * +---------+---------+---------+---------+
377 * | order | | | order |
378 * | byte | | | byte |
379 * +---------+---------+---------+---------+
380 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
382 * The save order is low order first, to high order (4 bytes total)
383 * and is written to be system independent.
384 * No checking for output buffer overflow is done, but flushes if needed!
385 * Returns nothing of value.
394 *lpnt
++ = 255 & (x
>> 8);
395 *lpnt
++ = 255 & (x
>> 16);
396 *lpnt
++ = 255 & (x
>> 24);
400 * lwrite(buf,len) write a buffer to the output buffer
404 * Enter with the address and number of bytes to write out
405 * Returns nothing of value
414 if (len
> 399) { /* don't copy data if can just write it */
420 for (str
= buf
; len
> 0; --len
)
424 write(lfd
, buf
, len
);
429 lflush(); /* if buffer is full flush it */
430 num2
= lpbuf
+ BUFBIG
- lpnt
; /* # bytes left in
437 *str
++ = *buf
++; /* copy in the bytes */
443 * long lgetc() Read one character from input buffer
445 * Returns 0 if EOF, otherwise the character
451 if (ipoint
!= iepoint
)
452 return (inbuffer
[ipoint
++]);
453 if (iepoint
!= MAXIBUF
)
455 if ((i
= read(fd
, inbuffer
, MAXIBUF
)) <= 0) {
457 write(1, "error reading from input file\n", 30);
458 iepoint
= ipoint
= 0;
467 * long lrint() Read one integer from input buffer
469 * +---------+---------+---------+---------+
471 * | order | | | order |
472 * | byte | | | byte |
473 * +---------+---------+---------+---------+
474 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
476 * The save order is low order first, to high order (4 bytes total)
477 * Returns the int read
484 i
|= (255 & lgetc()) << 8;
485 i
|= (255 & lgetc()) << 16;
486 i
|= (255 & lgetc()) << 24;
491 * lrfill(address,number) put input bytes into a buffer
495 * Reads "number" bytes into the buffer pointed to by "address".
496 * Returns nothing of value
506 if (iepoint
== ipoint
) {
507 if (num
> 5) { /* fast way */
508 if (read(fd
, adr
, num
) != num
)
509 write(2, "error reading from input file\n", 30);
516 num2
= iepoint
- ipoint
; /* # of bytes left in
520 pnt
= inbuffer
+ ipoint
;
530 * char *lgetw() Get a whitespace ended word from input
532 * Returns pointer to a buffer that contains word. If EOF, returns a NULL
538 int n
= LINBUFSIZE
, quote
= 0;
542 while ((cc
<= 32) && (cc
> '\0')); /* eat whitespace */
543 for (;; --n
, cc
= lgetc()) {
544 if ((cc
== '\0') && (lgp
== lgetwbuf
))
545 return (NULL
); /* EOF */
546 if ((n
<= 1) || ((cc
<= 32) && (quote
== 0))) {
558 * char *lgetl() Function to read in a line ended by newline or EOF
560 * Returns pointer to a buffer that contains the line. If EOF, returns NULL
565 int i
= LINBUFSIZE
, ch
;
566 char *str
= lgetwbuf
;
568 if ((*str
++ = ch
= lgetc()) == '\0') {
569 if (str
== lgetwbuf
+ 1)
570 return (NULL
); /* EOF */
572 return (lgetwbuf
); /* line ended by EOF */
574 if ((ch
== '\n') || (i
<= 1))
575 goto ot
;/* line ended by \n */
580 * lcreat(filename) Create a new file for write
583 * lcreat((char*)0); means to the terminal
584 * Returns -1 if error, otherwise the file descriptor opened.
591 lpend
= lpbuf
+ BUFBIG
;
594 if ((lfd
= creat(str
, 0644)) < 0) {
596 lprintf("error creating file <%s>: %s\n", str
,
605 * lopen(filename) Open a file for read
608 * lopen(0) means from the terminal
609 * Returns -1 if error, otherwise the file descriptor opened.
615 ipoint
= iepoint
= MAXIBUF
;
618 if ((fd
= open(str
, 0)) < 0) {
628 * lappend(filename) Open for append to an existing file
631 * lappend(0) means to the terminal
632 * Returns -1 if error, otherwise the file descriptor opened.
639 lpend
= lpbuf
+ BUFBIG
;
642 if ((lfd
= open(str
, 2)) < 0) {
646 lseek(lfd
, 0, 2); /* seek to end of file */
651 * lrclose() close the input file
653 * Returns nothing of value.
663 * lwclose() close output file flushing if needed
665 * Returns nothing of value.
676 * lprcat(string) append a string to the output buffer
677 * avoids calls to lprintf (time consuming)
687 while ((*str2
++ = *str
++) != '\0')
694 * cursor(x,y) Subroutine to set the cursor position
696 * x and y are the cursor coordinates, and lpbuff is the output buffer where
697 * escape sequence will be placed.
699 static char *y_num
[] = {
700 "\33[", "\33[", "\33[2", "\33[3", "\33[4", "\33[5", "\33[6",
701 "\33[7", "\33[8", "\33[9", "\33[10", "\33[11", "\33[12", "\33[13", "\33[14",
702 "\33[15", "\33[16", "\33[17", "\33[18", "\33[19", "\33[20", "\33[21", "\33[22",
705 static char *x_num
[] = {
706 "H", "H", ";2H", ";3H", ";4H", ";5H", ";6H", ";7H", ";8H", ";9H",
707 ";10H", ";11H", ";12H", ";13H", ";14H", ";15H", ";16H", ";17H", ";18H", ";19H",
708 ";20H", ";21H", ";22H", ";23H", ";24H", ";25H", ";26H", ";27H", ";28H", ";29H",
709 ";30H", ";31H", ";32H", ";33H", ";34H", ";35H", ";36H", ";37H", ";38H", ";39H",
710 ";40H", ";41H", ";42H", ";43H", ";44H", ";45H", ";46H", ";47H", ";48H", ";49H",
711 ";50H", ";51H", ";52H", ";53H", ";54H", ";55H", ";56H", ";57H", ";58H", ";59H",
712 ";60H", ";61H", ";62H", ";63H", ";64H", ";65H", ";66H", ";67H", ";68H", ";69H",
713 ";70H", ";71H", ";72H", ";73H", ";74H", ";75H", ";76H", ";77H", ";78H", ";79H",
724 p
= y_num
[y
]; /* get the string to print */
726 *lpnt
++ = *p
++; /* print the string */
728 p
= x_num
[x
]; /* get the string to print */
730 *lpnt
++ = *p
++; /* print the string */
734 * cursor(x,y) Put cursor at specified coordinates staring at [1,1] (termcap)
750 * Routine to position cursor at beginning of 24th line
760 * Warning: ringing the bell is control code 7. Don't use in defines.
761 * Don't change the order of these defines.
762 * Also used in helpfiles. Codes used in helpfiles should be \E[1 to \E[7 with
768 char *CM
, *CE
, *CD
, *CL
, *SO
, *SE
, *AL
, *DL
; /* Termcap capabilities */
769 static char *outbuf
= 0; /* translated output buffer */
772 * init_term() Terminal initialization -- setup termcap info
781 switch (t_getent(&info
, term
= getenv("TERM"))) {
783 write(2, "Cannot open termcap file.\n", 26);
786 write(2, "Cannot find entry of ", 21);
787 write(2, term
, strlen(term
));
788 write(2, " in termcap\n", 12);
792 CM
= t_agetstr(info
, "cm", &cap
, &capptr
); /* Cursor motion */
793 CE
= t_agetstr(info
, "ce", &cap
, &capptr
); /* Clear to eoln */
794 CL
= t_agetstr(info
, "cl", &cap
, &capptr
); /* Clear screen */
797 AL
= t_agetstr(info
, "al", &cap
, &capptr
); /* Insert line */
798 DL
= t_agetstr(info
, "dl", &cap
, &capptr
); /* Delete line */
799 SO
= t_agetstr(info
, "so", &cap
, &capptr
); /* Begin standout mode */
800 SE
= t_agetstr(info
, "se", &cap
, &capptr
); /* End standout mode */
801 CD
= t_agetstr(info
, "cd", &cap
, &capptr
); /* Clear to end of display */
803 if (!CM
) { /* can't find cursor motion entry */
804 write(2, "Sorry, for a ", 13);
805 write(2, term
, strlen(term
));
806 write(2, ", I can't find the cursor motion entry in termcap\n", 50);
809 if (!CE
) { /* can't find clear to end of line entry */
810 write(2, "Sorry, for a ", 13);
811 write(2, term
, strlen(term
));
812 write(2, ", I can't find the clear to end of line entry in termcap\n", 57);
815 if (!CL
) { /* can't find clear entire screen entry */
816 write(2, "Sorry, for a ", 13);
817 write(2, term
, strlen(term
));
818 write(2, ", I can't find the clear entire screen entry in termcap\n", 56);
821 if ((outbuf
= malloc(BUFBIG
+ 16)) == 0) { /* get memory for
822 * decoded output buffer */
823 write(2, "Error malloc'ing memory for decoded output buffer\n", 50);
824 died(-285); /* malloc() failure */
830 * cl_line(x,y) Clear the whole line indicated by 'y' and leave cursor at [x,y]
847 * cl_up(x,y) Clear screen from [x,1] to current position. Leave cursor at [x,y]
855 lprcat("\33[1J\33[2K");
859 for (i
= 1; i
<= y
; i
++) {
868 * cl_dn(x,y) Clear screen from [1,y] to end of display. Leave cursor at [x,y]
876 lprcat("\33[J\33[2K");
882 for (i
= y
; i
<= 24; i
++) {
895 * standout(str) Print the argument string in inverse video (standout mode).
915 * set_score_output() Called when output should be literally printed.
924 * lflush() Flush the output buffer
926 * Returns nothing of value.
927 * for termcap version: Flush output in output buffer according to output
928 * status as indicated by `enable_scroll'
931 static int scrline
= 18; /* line # for wraparound instead of scrolling
942 if ((lpoint
= lpnt
- lpbuf
) > 0) {
944 c
[BYTESOUT
] += lpoint
;
946 if (enable_scroll
<= -1) {
948 if (write(lfd
, lpbuf
, lpoint
) != lpoint
)
949 write(2, "error writing to output file\n", 29);
950 lpnt
= lpbuf
; /* point back to beginning of buffer */
953 for (str
= lpbuf
; str
< lpnt
; str
++) {
960 tputs(CL
, 0, xputchar
);
965 tputs(CE
, 0, xputchar
);
969 tputs(CD
, 0, xputchar
);
973 tputs(SO
, 0, xputchar
);
977 tputs(SE
, 0, xputchar
);
983 if (t_goto(info
, CM
, curx
, cury
,
984 tgoto_buf
, 255) == 0)
985 tputs(tgoto_buf
, 0, xputchar
);
989 if ((cury
== 23) && enable_scroll
) {
990 if (!DL
|| !AL
) { /* wraparound or scroll? */
996 if (t_goto(info
, CM
, 0,
1003 tputs(CE
, 0, xputchar
);
1007 if (t_goto(info
, CM
, 0,
1014 tputs(CE
, 0, xputchar
);
1016 if (t_goto(info
, CM
, 0,
1023 tputs(DL
, 0, xputchar
);
1024 if (t_goto(info
, CM
, 0,
1050 flush_buf(); /* flush real output buffer now */
1054 * lflush() flush the output buffer
1056 * Returns nothing of value.
1062 if ((lpoint
= lpnt
- lpbuf
) > 0) {
1064 c
[BYTESOUT
] += lpoint
;
1066 if (write(lfd
, lpbuf
, lpoint
) != lpoint
)
1067 write(2, "error writing to output file\n", 29);
1069 lpnt
= lpbuf
; /* point back to beginning of buffer */
1074 static int vindex
= 0;
1076 * xputchar(ch) Print one character in decoded output buffer.
1082 outbuf
[vindex
++] = c
;
1083 if (vindex
>= BUFBIG
)
1089 * flush_buf() Flush buffer with decoded output.
1095 write(lfd
, outbuf
, vindex
);
1100 * char *tmcapcnv(sd,ss) Routine to convert VT100 escapes to termcap
1102 * Processes only the \33[#m sequence (converts . files for termcap use
1108 int tmstate
= 0; /* 0=normal, 1=\33 2=[ 3=# */
1109 char tmdigit
= 0; /* the # in \33[#m */
1126 if (isdigit((u_char
)*ss
)) {
1127 tmdigit
= *ss
- '0';
1149 *sd
= 0; /* NULL terminator */
1155 * beep() Routine to emit a beep if enabled (see no-beep in .larnopts)