]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/io.c
1 /* $NetBSD: io.c,v 1.28 2017/01/10 20:41:40 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 * 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.28 2017/01/10 20:41:40 christos Exp $");
83 #define stty(_a,_b) ioctl(_a,TCSETA,_b)
84 #define gtty(_a,_b) ioctl(_a,TCGETA,_b)
88 #define sgttyb termios
89 #define stty(_a,_b) tcsetattr(_a,TCSADRAIN,_b)
90 #define gtty(_a,_b) tcgetattr(_a,_b)
93 #if defined(TERMIO) || defined(TERMIOS)
94 static int rawflg
= 0;
95 static char saveeof
, saveeol
;
99 saveeof = _a.c_cc[VMIN]; \
100 saveeol = _a.c_cc[VTIME]; \
103 _a.c_cc[VTIME] = 1; \
104 _a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL)
106 _a.c_cc[VMIN] = saveeof; \
107 _a.c_cc[VTIME] = saveeol; \
108 _a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL
110 #else /* not TERMIO or TERMIOS */
113 #define CBREAK RAW /* V7 has no CBREAK */
116 #define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO)
117 #define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO)
119 #endif /* not TERMIO or TERMIOS */
121 #ifndef NOVARARGS /* if we have varargs */
123 #else /* NOVARARGS */ /* if we don't have varargs */
124 typedef char *va_list;
125 #define va_dcl int va_alist;
126 #define va_start(plist) plist = (char *) &va_alist
127 #define va_end(plist)
128 #define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
129 #endif /* NOVARARGS */
131 static int ttputch(int ch
);
132 static void flush_buf(void);
134 #define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
135 int io_outfd
; /* output file numbers */
136 int io_infd
; /* input file numbers */
137 static struct sgttyb ttx
;/* storage for the tty modes */
138 static int ipoint
= MAXIBUF
, iepoint
= MAXIBUF
; /* input buffering
140 static char lgetwbuf
[LINBUFSIZE
]; /* get line (word) buffer */
143 * setupvt100() Subroutine to set up terminal in correct mode for game
145 * Attributes off, clear screen, set scrolling region, set tty mode
152 scbr(); /* system("stty cbreak -echo"); */
156 * clearvt100() Subroutine to clean up terminal when the game is over
158 * Attributes off, clear screen, unset scrolling region, restore tty mode
165 sncbr(); /* system("stty -cbreak echo"); */
169 * ttgetch() Routine to read in one character from the terminal
178 lflush(); /* be sure output buffer is flushed */
179 read(0, &byt
, 1); /* get byte from terminal */
184 * scbr() Function to set cbreak -echo for the terminal
186 * like: system("stty cbreak -echo")
197 * sncbr() Function to set -cbreak echo for the terminal
199 * like: system("stty -cbreak echo")
210 * newgame() Subroutine to save the initial time and seed rnd()
216 for (p
= c
, pe
= c
+ 100; p
< pe
; *p
++ = 0);
218 seedrand(initialtime
);
219 srandom(initialtime
);
220 lcreat((char *) 0); /* open buffering for output to terminal */
224 * lprintf(format,args . . .) printf to the output buffer
228 * Enter with the format string in "format", as per printf() usage
229 * and any needed arguments following it
230 * Note: lprintf() only supports %s, %c and %d, with width modifier and left
231 * or right justification.
232 * No correct checking for output buffer overflow is done, but flushes
233 * are done beforehand if needed.
234 * Returns nothing of value.
237 lprintf(const char *fmt
, ...)
243 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
253 * lprint(long-integer) send binary integer to output buffer
256 * +---------+---------+---------+---------+
258 * | order | | | order |
259 * | byte | | | byte |
260 * +---------+---------+---------+---------+
261 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
263 * The save order is low order first, to high order (4 bytes total)
264 * and is written to be system independent.
265 * No checking for output buffer overflow is done, but flushes if needed!
266 * 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
288 lwrite(char *buf
, int len
)
294 if (len
> 399) { /* don't copy data if can just write it */
300 for (s
= buf
; len
> 0; --len
)
304 write(io_outfd
, buf
, len
);
309 lflush(); /* if buffer is full flush it */
310 num2
= lpbuf
+ BUFBIG
- lpnt
; /* # bytes left in
317 *t
++ = *buf
++; /* copy in the bytes */
323 * long lgetc() Read one character from input buffer
325 * Returns 0 if EOF, otherwise the character
331 if (ipoint
!= iepoint
)
332 return (inbuffer
[ipoint
++]);
333 if (iepoint
!= MAXIBUF
)
335 if ((i
= read(io_infd
, inbuffer
, MAXIBUF
)) <= 0) {
337 write(1, "error reading from input file\n", 30);
338 iepoint
= ipoint
= 0;
347 * long lrint() Read one integer from input buffer
349 * +---------+---------+---------+---------+
351 * | order | | | order |
352 * | byte | | | byte |
353 * +---------+---------+---------+---------+
354 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
356 * The save order is low order first, to high order (4 bytes total)
357 * Returns the int read
364 i
|= (255 & lgetc()) << 8;
365 i
|= (255 & lgetc()) << 16;
366 i
|= (255 & lgetc()) << 24;
371 * lrfill(address,number) put input bytes into a buffer
375 * Reads "number" bytes into the buffer pointed to by "address".
376 * Returns nothing of value
379 lrfill(char *adr
, int num
)
385 if (iepoint
== ipoint
) {
386 if (num
> 5) { /* fast way */
387 if (read(io_infd
, adr
, num
) != num
)
388 write(2, "error reading from input file\n", 30);
395 num2
= iepoint
- ipoint
; /* # of bytes left in
399 pnt
= inbuffer
+ ipoint
;
409 * char *lgetw() Get a whitespace ended word from input
411 * Returns pointer to a buffer that contains word. If EOF, returns a NULL
417 int n
= LINBUFSIZE
, quote
= 0;
421 while ((cc
<= 32) && (cc
> '\0')); /* eat whitespace */
422 for (;; --n
, cc
= lgetc()) {
423 if ((cc
== '\0') && (lgp
== lgetwbuf
))
424 return (NULL
); /* EOF */
425 if ((n
<= 1) || ((cc
<= 32) && (quote
== 0))) {
437 * char *lgetl() Function to read in a line ended by newline or EOF
439 * Returns pointer to a buffer that contains the line. If EOF, returns NULL
444 int i
= LINBUFSIZE
, ch
;
445 char *str
= lgetwbuf
;
447 if ((*str
++ = ch
= lgetc()) == '\0') {
448 if (str
== lgetwbuf
+ 1)
449 return (NULL
); /* EOF */
451 return (lgetwbuf
); /* line ended by EOF */
453 if ((ch
== '\n') || (i
<= 1))
454 goto ot
;/* line ended by \n */
459 * lcreat(filename) Create a new file for write
462 * lcreat((char*)0); means to the terminal
463 * Returns -1 if error, otherwise the file descriptor opened.
470 lpend
= lpbuf
+ BUFBIG
;
472 return (io_outfd
= 1);
473 if ((io_outfd
= creat(str
, 0644)) < 0) {
475 lprintf("error creating file <%s>: %s\n", str
,
484 * lopen(filename) Open a file for read
487 * lopen(0) means from the terminal
488 * Returns -1 if error, otherwise the file descriptor opened.
493 ipoint
= iepoint
= MAXIBUF
;
495 return (io_infd
= 0);
496 if ((io_infd
= open(str
, O_RDONLY
)) < 0) {
506 * lappend(filename) Open for append to an existing file
509 * lappend(0) means to the terminal
510 * Returns -1 if error, otherwise the file descriptor opened.
516 lpend
= lpbuf
+ BUFBIG
;
518 return (io_outfd
= 1);
519 if ((io_outfd
= open(str
, 2)) < 0) {
523 lseek(io_outfd
, 0, SEEK_END
); /* seek to end of file */
528 * lrclose() close the input file
530 * Returns nothing of value.
542 * lwclose() close output file flushing if needed
544 * Returns nothing of value.
557 * lprcat(string) append a string to the output buffer
558 * avoids calls to lprintf (time consuming)
561 lprcat(const char *str
)
567 while ((*str2
++ = *str
++) != '\0')
574 * cursor(x,y) Subroutine to set the cursor position
576 * x and y are the cursor coordinates, and lpbuff is the output buffer where
577 * escape sequence will be placed.
579 static char *y_num
[] = {
580 "\33[", "\33[", "\33[2", "\33[3", "\33[4", "\33[5", "\33[6",
581 "\33[7", "\33[8", "\33[9", "\33[10", "\33[11", "\33[12", "\33[13", "\33[14",
582 "\33[15", "\33[16", "\33[17", "\33[18", "\33[19", "\33[20", "\33[21", "\33[22",
585 static char *x_num
[] = {
586 "H", "H", ";2H", ";3H", ";4H", ";5H", ";6H", ";7H", ";8H", ";9H",
587 ";10H", ";11H", ";12H", ";13H", ";14H", ";15H", ";16H", ";17H", ";18H", ";19H",
588 ";20H", ";21H", ";22H", ";23H", ";24H", ";25H", ";26H", ";27H", ";28H", ";29H",
589 ";30H", ";31H", ";32H", ";33H", ";34H", ";35H", ";36H", ";37H", ";38H", ";39H",
590 ";40H", ";41H", ";42H", ";43H", ";44H", ";45H", ";46H", ";47H", ";48H", ";49H",
591 ";50H", ";51H", ";52H", ";53H", ";54H", ";55H", ";56H", ";57H", ";58H", ";59H",
592 ";60H", ";61H", ";62H", ";63H", ";64H", ";65H", ";66H", ";67H", ";68H", ";69H",
593 ";70H", ";71H", ";72H", ";73H", ";74H", ";75H", ";76H", ";77H", ";78H", ";79H",
604 p
= y_num
[y
]; /* get the string to print */
606 *lpnt
++ = *p
++; /* print the string */
608 p
= x_num
[x
]; /* get the string to print */
610 *lpnt
++ = *p
++; /* print the string */
614 * cursor(x,y) Put cursor at specified coordinates staring at [1,1] (termcap)
629 * Routine to position cursor at beginning of 24th line
639 * Warning: ringing the bell is control code 7. Don't use in defines.
640 * Don't change the order of these defines.
641 * Also used in helpfiles. Codes used in helpfiles should be \E[1 to \E[7 with
645 static char *outbuf
= 0; /* translated output buffer */
647 * init_term() Terminal initialization -- setup termcap info
652 setupterm(NULL
, 0, NULL
); /* will exit if invalid term */
653 if (!cursor_address
) {
654 fprintf(stderr
, "term does not have cursor_address.\n");
658 fprintf(stderr
, "term does not have clr_eol.\n");
662 fprintf(stderr
, "term does not have clear_screen.\n");
665 if ((outbuf
= malloc(BUFBIG
+ 16)) == 0) { /* get memory for
666 * decoded output buffer */
667 fprintf(stderr
, "Error malloc'ing memory for decoded output buffer\n");
668 died(-285); /* malloc() failure */
675 * cl_line(x,y) Clear the whole line indicated by 'y' and leave cursor at [x,y]
678 cl_line(int x
, int y
)
691 * cl_up(x,y) Clear screen from [x,1] to current position. Leave cursor at [x,y]
698 lprcat("\33[1J\33[2K");
702 for (i
= 1; i
<= y
; i
++) {
711 * cl_dn(x,y) Clear screen from [1,y] to end of display. Leave cursor at [x,y]
718 lprcat("\33[J\33[2K");
724 for (i
= y
; i
<= 24; i
++) {
737 * standout(str) Print the argument string in inverse video (standout mode).
740 standout(const char *str
)
756 * set_score_output() Called when output should be literally printed.
759 set_score_output(void)
765 * lflush() Flush the output buffer
767 * Returns nothing of value.
768 * for termcap version: Flush output in output buffer according to output
769 * status as indicated by `enable_scroll'
772 static int scrline
= 18; /* line # for wraparound instead of scrolling
782 if ((lpoint
= lpnt
- lpbuf
) > 0) {
784 c
[BYTESOUT
] += lpoint
;
786 if (enable_scroll
<= -1) {
788 if (write(io_outfd
, lpbuf
, lpoint
) != lpoint
)
789 write(2, "error writing to output file\n", 29);
790 lpnt
= lpbuf
; /* point back to beginning of buffer */
793 for (str
= lpbuf
; str
< lpnt
; str
++) {
800 tputs(clear_screen
, 0, ttputch
);
805 tputs(clr_eol
, 0, ttputch
);
809 tputs(clr_eos
, 0, ttputch
);
813 tputs(enter_standout_mode
, 0, ttputch
);
817 tputs(exit_standout_mode
, 0, ttputch
);
823 tputs(tiparm(cursor_address
,
824 cury
, curx
), 0, ttputch
);
828 if ((cury
== 23) && enable_scroll
) {
831 { /* wraparound or scroll? */
857 tputs(delete_line
, 0,
882 flush_buf(); /* flush real output buffer now */
886 * lflush() flush the output buffer
888 * Returns nothing of value.
894 if ((lpoint
= lpnt
- lpbuf
) > 0) {
896 c
[BYTESOUT
] += lpoint
;
898 if (write(io_outfd
, lpbuf
, lpoint
) != lpoint
)
899 write(2, "error writing to output file\n", 29);
901 lpnt
= lpbuf
; /* point back to beginning of buffer */
906 static int vindex
= 0;
908 * ttputch(ch) Print one character in decoded output buffer.
913 outbuf
[vindex
++] = ch
;
914 if (vindex
>= BUFBIG
)
920 * flush_buf() Flush buffer with decoded output.
926 write(io_outfd
, outbuf
, vindex
);
931 * char *tmcapcnv(sd,ss) Routine to convert VT100 escapes to termcap
933 * Processes only the \33[#m sequence (converts . files for termcap use
936 tmcapcnv(char *sd
, char *ss
)
938 int tmstate
= 0; /* 0=normal, 1=\33 2=[ 3=# */
939 char tmdigit
= 0; /* the # in \33[#m */
956 if (isdigit((u_char
)*ss
)) {
979 *sd
= 0; /* NULL terminator */
985 * beep() Routine to emit a beep if enabled (see no-beep in .larnopts)