]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/io.c
2 static char rcsid
[] = "$Id: io.c,v 1.2 1993/08/02 17:20:05 mycroft Exp $";
5 /* io.c Larn is copyrighted 1986 by Noah Morgan.
7 * Below are the functions in this file:
9 * setupvt100() Subroutine to set up terminal in correct mode for game
10 * clearvt100() Subroutine to clean up terminal when the game is over
11 * getchar() Routine to read in one character from the terminal
12 * scbr() Function to set cbreak -echo for the terminal
13 * sncbr() Function to set -cbreak echo for the terminal
14 * newgame() Subroutine to save the initial time and seed rnd()
16 * FILE OUTPUT ROUTINES
18 * lprintf(format,args . . .) printf to the output buffer
19 * lprint(integer) send binary integer to output buffer
20 * lwrite(buf,len) write a buffer to the output buffer
21 * lprcat(str) sent string to output buffer
23 * FILE OUTPUT MACROS (in header.h)
25 * lprc(character) put the character into the output buffer
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
32 * char *lgetw() get a whitespace ended word from input
33 * char *lgetl() get a \n or EOF ended line from input
35 * FILE OPEN / CLOSE ROUTINES
37 * lcreat(filename) create a new file for write
38 * lopen(filename) open a file for read
39 * lappend(filename) open for append to an existing file
40 * lrclose() close the input file
41 * lwclose() close output file
42 * lflush() flush the output buffer
46 * cursor(x,y) position cursor at [x,y]
47 * cursors() position cursor at [1,24] (saves memory)
48 * cl_line(x,y) Clear line at [1,y] and leave cursor at [x,y]
49 * cl_up(x,y) Clear screen from [x,1] to current line.
50 * cl_dn(x,y) Clear screen from [1,y] to end of display.
51 * standout(str) Print the string in standout mode.
52 * set_score_output() Called when output should be literally printed.
53 ** putchar(ch) Print one character in decoded output buffer.
54 ** flush_buf() Flush buffer with decoded output.
55 ** init_term() Terminal initialization -- setup termcap info
56 ** char *tmcapcnv(sd,ss) Routine to convert VT100 \33's to termcap format
57 * beep() Routine to emit a beep if enabled (see no-beep in .larnopts)
59 * Note: ** entries are available only in termcap mode.
64 #ifdef SYSV /* system III or system V */
67 #define stty(_a,_b) ioctl(_a,TCSETA,_b)
68 #define gtty(_a,_b) ioctl(_a,TCGETA,_b)
69 static int rawflg
= 0;
70 static char saveeof
,saveeol
;
71 #define doraw(_a) if(!rawflg){++rawflg;saveeof=_a.c_cc[VMIN];saveeol=_a.c_cc[VTIME];}\
72 _a.c_cc[VMIN]=1;_a.c_cc[VTIME]=1;_a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL)
73 #define unraw(_a) _a.c_cc[VMIN]=saveeof;_a.c_cc[VTIME]=saveeol;_a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL
78 #define CBREAK RAW /* V7 has no CBREAK */
81 #define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO)
82 #define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO)
86 #ifndef NOVARARGS /* if we have varargs */
88 #else NOVARARGS /* if we don't have varargs */
89 typedef char *va_list;
90 #define va_dcl int va_alist;
91 #define va_start(plist) plist = (char *) &va_alist
93 #define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1]
96 #define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */
97 int lfd
; /* output file numbers */
98 int fd
; /* input file numbers */
99 static struct sgttyb ttx
; /* storage for the tty modes */
100 static int ipoint
=MAXIBUF
,iepoint
=MAXIBUF
; /* input buffering pointers */
101 static char lgetwbuf
[LINBUFSIZE
]; /* get line (word) buffer */
104 * setupvt100() Subroutine to set up terminal in correct mode for game
106 * Attributes off, clear screen, set scrolling region, set tty mode
110 clear(); setscroll(); scbr(); /* system("stty cbreak -echo"); */
114 * clearvt100() Subroutine to clean up terminal when the game is over
116 * Attributes off, clear screen, unset scrolling region, restore tty mode
120 resetscroll(); clear(); sncbr(); /* system("stty -cbreak echo"); */
124 * getchar() Routine to read in one character from the terminal
132 lflush(); /* be sure output buffer is flushed */
133 read(0,&byt
,1); /* get byte from terminal */
138 * scbr() Function to set cbreak -echo for the terminal
140 * like: system("stty cbreak -echo")
144 gtty(0,&ttx
); doraw(ttx
); stty(0,&ttx
);
148 * sncbr() Function to set -cbreak echo for the terminal
150 * like: system("stty -cbreak echo")
154 gtty(0,&ttx
); unraw(ttx
); stty(0,&ttx
);
158 * newgame() Subroutine to save the initial time and seed rnd()
162 register long *p
,*pe
;
163 for (p
=c
,pe
=c
+100; p
<pe
; *p
++ =0);
164 time(&initialtime
); srand(initialtime
);
165 lcreat((char*)0); /* open buffering for output to terminal */
169 * lprintf(format,args . . .) printf to the output buffer
173 * Enter with the format string in "format", as per printf() usage
174 * and any needed arguments following it
175 * Note: lprintf() only supports %s, %c and %d, with width modifier and left
176 * or right justification.
177 * No correct checking for output buffer overflow is done, but flushes
178 * are done beforehand if needed.
179 * Returns nothing of value.
188 str
= str2
; /* to make lint happy */
196 str
= str2
; /* to make lint happy */
203 va_list ap
; /* pointer for variable argument list */
205 register char *outb
,*tmpb
;
206 register long wide
,left
,cont
,n
; /* data for lprintf */
207 char db
[12]; /* %d buffer in lprintf */
209 va_start(ap
); /* initialize the var args pointer */
210 fmt
= va_arg(ap
, char *); /* pointer to format string */
211 if (lpnt
>= lpend
) lflush();
216 if (*fmt
) *outb
++ = *fmt
++; else { lpnt
=outb
; return; }
217 wide
= 0; left
= 1; cont
=1;
221 case 'd': n
= va_arg(ap
, long);
222 if (n
<0) { n
= -n
; *outb
++ = '-'; if (wide
) --wide
; }
223 tmpb
= db
+11; *tmpb
= (char)(n
% 10 + '0');
224 while (n
>9) *(--tmpb
) = (char)((n
/= 10) % 10 + '0');
225 if (wide
==0) while (tmpb
< db
+12) *outb
++ = *tmpb
++;
229 if (left
) while (wide
-- > 0) *outb
++ = ' ';
230 while (tmpb
< db
+12) *outb
++ = *tmpb
++;
231 if (left
==0) while (wide
-- > 0) *outb
++ = ' ';
235 case 's': tmpb
= va_arg(ap
, char *);
236 if (wide
==0) { while (*outb
++ = *tmpb
++); --outb
; }
239 n
= wide
- strlen(tmpb
);
240 if (left
) while (n
-- > 0) *outb
++ = ' ';
241 while (*outb
++ = *tmpb
++); --outb
;
242 if (left
==0) while (n
-- > 0) *outb
++ = ' ';
246 case 'c': *outb
++ = va_arg(ap
, int); cont
=0; break;
257 case '9': wide
= 10*wide
+ *fmt
- '0'; break;
259 case '-': left
= 0; break;
261 default: *outb
++ = *fmt
; cont
=0; break;
270 * lprint(long-integer) send binary integer to output buffer
273 * +---------+---------+---------+---------+
275 * | order | | | order |
276 * | byte | | | byte |
277 * +---------+---------+---------+---------+
278 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
280 * The save order is low order first, to high order (4 bytes total)
281 * and is written to be system independent.
282 * No checking for output buffer overflow is done, but flushes if needed!
283 * Returns nothing of value.
288 if (lpnt
>= lpend
) lflush();
289 *lpnt
++ = 255 & x
; *lpnt
++ = 255 & (x
>>8);
290 *lpnt
++ = 255 & (x
>>16); *lpnt
++ = 255 & (x
>>24);
294 * lwrite(buf,len) write a buffer to the output buffer
298 * Enter with the address and number of bytes to write out
299 * Returns nothing of value
307 if (len
> 399) /* don't copy data if can just write it */
314 for (str
=buf
; len
>0; --len
)
323 if (lpnt
>= lpend
) lflush(); /* if buffer is full flush it */
324 num2
= lpbuf
+BUFBIG
-lpnt
; /* # bytes left in output buffer */
325 if (num2
> len
) num2
=len
;
326 str
= lpnt
; len
-= num2
;
327 while (num2
--) *str
++ = *buf
++; /* copy in the bytes */
333 * long lgetc() Read one character from input buffer
335 * Returns 0 if EOF, otherwise the character
340 if (ipoint
!= iepoint
) return(inbuffer
[ipoint
++]);
341 if (iepoint
!=MAXIBUF
) return(0);
342 if ((i
=read(fd
,inbuffer
,MAXIBUF
))<=0)
344 if (i
!=0) write(1,"error reading from input file\n",30);
345 iepoint
= ipoint
= 0; return(0);
347 ipoint
=1; iepoint
=i
; return(*inbuffer
);
351 * long lrint() Read one integer from input buffer
353 * +---------+---------+---------+---------+
355 * | order | | | order |
356 * | byte | | | byte |
357 * +---------+---------+---------+---------+
358 * 31 --- 24 23 --- 16 15 --- 8 7 --- 0
360 * The save order is low order first, to high order (4 bytes total)
361 * Returns the int read
365 register unsigned long i
;
366 i
= 255 & lgetc(); i
|= (255 & lgetc()) << 8;
367 i
|= (255 & lgetc()) << 16; i
|= (255 & lgetc()) << 24;
372 * lrfill(address,number) put input bytes into a buffer
376 * Reads "number" bytes into the buffer pointed to by "address".
377 * Returns nothing of value
387 if (iepoint
== ipoint
)
389 if (num
>5) /* fast way */
391 if (read(fd
,adr
,num
) != num
)
392 write(2,"error reading from input file\n",30);
395 else { *adr
++ = lgetc(); --num
; }
399 num2
= iepoint
-ipoint
; /* # of bytes left in the buffer */
400 if (num2
> num
) num2
=num
;
401 pnt
= inbuffer
+ipoint
; num
-= num2
; ipoint
+= num2
;
402 while (num2
--) *adr
++ = *pnt
++;
408 * char *lgetw() Get a whitespace ended word from input
410 * Returns pointer to a buffer that contains word. If EOF, returns a NULL
414 register char *lgp
,cc
;
415 register int n
=LINBUFSIZE
,quote
=0;
417 do cc
=lgetc(); while ((cc
<= 32) && (cc
> NULL
)); /* eat whitespace */
418 for ( ; ; --n
,cc
=lgetc())
420 if ((cc
==NULL
) && (lgp
==lgetwbuf
)) return(NULL
); /* EOF */
421 if ((n
<=1) || ((cc
<=32) && (quote
==0))) { *lgp
=NULL
; return(lgetwbuf
); }
422 if (cc
!= '"') *lgp
++ = cc
; else quote
^= 1;
427 * char *lgetl() Function to read in a line ended by newline or EOF
429 * Returns pointer to a buffer that contains the line. If EOF, returns NULL
433 register int i
=LINBUFSIZE
,ch
;
434 register char *str
=lgetwbuf
;
437 if ((*str
++ = ch
= lgetc()) == NULL
)
439 if (str
== lgetwbuf
+1) return(NULL
); /* EOF */
440 ot
: *str
= NULL
; return(lgetwbuf
); /* line ended by EOF */
442 if ((ch
=='\n') || (i
<=1)) goto ot
; /* line ended by \n */
447 * lcreat(filename) Create a new file for write
450 * lcreat((char*)0); means to the terminal
451 * Returns -1 if error, otherwise the file descriptor opened.
456 lpnt
= lpbuf
; lpend
= lpbuf
+BUFBIG
;
457 if (str
==NULL
) return(lfd
=1);
458 if ((lfd
=creat(str
,0644)) < 0)
460 lfd
=1; lprintf("error creating file <%s>\n",str
); lflush(); return(-1);
466 * lopen(filename) Open a file for read
469 * lopen(0) means from the terminal
470 * Returns -1 if error, otherwise the file descriptor opened.
475 ipoint
= iepoint
= MAXIBUF
;
476 if (str
==NULL
) return(fd
=0);
477 if ((fd
=open(str
,0)) < 0)
479 lwclose(); lfd
=1; lpnt
=lpbuf
; return(-1);
485 * lappend(filename) Open for append to an existing file
488 * lappend(0) means to the terminal
489 * Returns -1 if error, otherwise the file descriptor opened.
494 lpnt
= lpbuf
; lpend
= lpbuf
+BUFBIG
;
495 if (str
==NULL
) return(lfd
=1);
496 if ((lfd
=open(str
,2)) < 0)
500 lseek(lfd
,0,2); /* seek to end of file */
505 * lrclose() close the input file
507 * Returns nothing of value.
511 if (fd
> 0) close(fd
);
515 * lwclose() close output file flushing if needed
517 * Returns nothing of value.
521 lflush(); if (lfd
> 2) close(lfd
);
525 * lprcat(string) append a string to the output buffer
526 * avoids calls to lprintf (time consuming)
532 if (lpnt
>= lpend
) lflush();
534 while (*str2
++ = *str
++);
540 * cursor(x,y) Subroutine to set the cursor position
542 * x and y are the cursor coordinates, and lpbuff is the output buffer where
543 * escape sequence will be placed.
545 static char *y_num
[]= { "\33[","\33[","\33[2","\33[3","\33[4","\33[5","\33[6",
546 "\33[7","\33[8","\33[9","\33[10","\33[11","\33[12","\33[13","\33[14",
547 "\33[15","\33[16","\33[17","\33[18","\33[19","\33[20","\33[21","\33[22",
550 static char *x_num
[]= { "H","H",";2H",";3H",";4H",";5H",";6H",";7H",";8H",";9H",
551 ";10H",";11H",";12H",";13H",";14H",";15H",";16H",";17H",";18H",";19H",
552 ";20H",";21H",";22H",";23H",";24H",";25H",";26H",";27H",";28H",";29H",
553 ";30H",";31H",";32H",";33H",";34H",";35H",";36H",";37H",";38H",";39H",
554 ";40H",";41H",";42H",";43H",";44H",";45H",";46H",";47H",";48H",";49H",
555 ";50H",";51H",";52H",";53H",";54H",";55H",";56H",";57H",";58H",";59H",
556 ";60H",";61H",";62H",";63H",";64H",";65H",";66H",";67H",";68H",";69H",
557 ";70H",";71H",";72H",";73H",";74H",";75H",";76H",";77H",";78H",";79H",
564 if (lpnt
>= lpend
) lflush();
566 p
= y_num
[y
]; /* get the string to print */
567 while (*p
) *lpnt
++ = *p
++; /* print the string */
569 p
= x_num
[x
]; /* get the string to print */
570 while (*p
) *lpnt
++ = *p
++; /* print the string */
574 * cursor(x,y) Put cursor at specified coordinates staring at [1,1] (termcap)
579 if (lpnt
>= lpend
) lflush ();
581 *lpnt
++ = CURSOR
; *lpnt
++ = x
; *lpnt
++ = y
;
586 * Routine to position cursor at beginning of 24th line
595 * Warning: ringing the bell is control code 7. Don't use in defines.
596 * Don't change the order of these defines.
597 * Also used in helpfiles. Codes used in helpfiles should be \E[1 to \E[7 with
601 static char cap
[256];
602 char *CM
, *CE
, *CD
, *CL
, *SO
, *SE
, *AL
, *DL
;/* Termcap capabilities */
603 static char *outbuf
=0; /* translated output buffer */
608 * init_term() Terminal initialization -- setup termcap info
613 char *capptr
= cap
+10;
616 switch (tgetent(termbuf
, term
= getenv("TERM")))
619 write(2, "Cannot open termcap file.\n", 26); exit();
621 write(2, "Cannot find entry of ", 21);
622 write(2, term
, strlen (term
));
623 write(2, " in termcap\n", 12);
627 CM
= tgetstr("cm", &capptr
); /* Cursor motion */
628 CE
= tgetstr("ce", &capptr
); /* Clear to eoln */
629 CL
= tgetstr("cl", &capptr
); /* Clear screen */
632 AL
= tgetstr("al", &capptr
); /* Insert line */
633 DL
= tgetstr("dl", &capptr
); /* Delete line */
634 SO
= tgetstr("so", &capptr
); /* Begin standout mode */
635 SE
= tgetstr("se", &capptr
); /* End standout mode */
636 CD
= tgetstr("cd", &capptr
); /* Clear to end of display */
638 if (!CM
) /* can't find cursor motion entry */
640 write(2, "Sorry, for a ",13); write(2, term
, strlen(term
));
641 write(2, ", I can't find the cursor motion entry in termcap\n",50);
644 if (!CE
) /* can't find clear to end of line entry */
646 write(2, "Sorry, for a ",13); write(2, term
, strlen(term
));
647 write(2,", I can't find the clear to end of line entry in termcap\n",57);
650 if (!CL
) /* can't find clear entire screen entry */
652 write(2, "Sorry, for a ",13); write(2, term
, strlen(term
));
653 write(2, ", I can't find the clear entire screen entry in termcap\n",56);
656 if ((outbuf
=malloc(BUFBIG
+16))==0) /* get memory for decoded output buffer*/
658 write(2,"Error malloc'ing memory for decoded output buffer\n",50);
659 died(-285); /* malloc() failure */
665 * cl_line(x,y) Clear the whole line indicated by 'y' and leave cursor at [x,y]
671 cursor(x
,y
); lprcat("\33[2K");
673 cursor(1,y
); *lpnt
++ = CL_LINE
; cursor(x
,y
);
678 * cl_up(x,y) Clear screen from [x,1] to current position. Leave cursor at [x,y]
684 cursor(x
,y
); lprcat("\33[1J\33[2K");
688 for (i
=1; i
<=y
; i
++) { *lpnt
++ = CL_LINE
; *lpnt
++ = '\n'; }
694 * cl_dn(x,y) Clear screen from [1,y] to end of display. Leave cursor at [x,y]
700 cursor(x
,y
); lprcat("\33[J\33[2K");
707 for (i
=y
; i
<=24; i
++) { *lpnt
++ = CL_LINE
; if (i
!=24) *lpnt
++ = '\n'; }
717 * standout(str) Print the argument string in inverse video (standout mode).
736 * set_score_output() Called when output should be literally printed.
744 * lflush() Flush the output buffer
746 * Returns nothing of value.
747 * for termcap version: Flush output in output buffer according to output
748 * status as indicated by `enable_scroll'
751 static int scrline
=18; /* line # for wraparound instead of scrolling if no DL */
759 if ((lpoint
= lpnt
- lpbuf
) > 0)
762 c
[BYTESOUT
] += lpoint
;
764 if (enable_scroll
<= -1)
767 if (write(lfd
,lpbuf
,lpoint
) != lpoint
)
768 write(2,"error writing to output file\n",29);
769 lpnt
= lpbuf
; /* point back to beginning of buffer */
772 for (str
= lpbuf
; str
< lpnt
; str
++)
774 if (*str
>=32) { putchar (*str
); curx
++; }
777 case CLEAR
: tputs (CL
, 0, putchar
); curx
= cury
= 0;
780 case CL_LINE
: tputs (CE
, 0, putchar
);
783 case CL_DOWN
: tputs (CD
, 0, putchar
);
786 case ST_START
: tputs (SO
, 0, putchar
);
789 case ST_END
: tputs (SE
, 0, putchar
);
792 case CURSOR
: curx
= *++str
- 1; cury
= *++str
- 1;
793 tputs (tgoto (CM
, curx
, cury
), 0, putchar
);
796 case '\n': if ((cury
== 23) && enable_scroll
)
798 if (!DL
|| !AL
) /* wraparound or scroll? */
800 if (++scrline
> 23) scrline
=19;
802 if (++scrline
> 23) scrline
=19;
803 tputs (tgoto (CM
, 0, scrline
), 0, putchar
);
804 tputs (CE
, 0, putchar
);
806 if (--scrline
< 19) scrline
=23;
807 tputs (tgoto (CM
, 0, scrline
), 0, putchar
);
808 tputs (CE
, 0, putchar
);
812 tputs (tgoto (CM
, 0, 19), 0, putchar
);
813 tputs (DL
, 0, putchar
);
814 tputs (tgoto (CM
, 0, 23), 0, putchar
);
815 /* tputs (AL, 0, putchar); */
820 putchar ('\n'); cury
++;
825 default: putchar (*str
); curx
++;
830 flush_buf(); /* flush real output buffer now */
834 * lflush() flush the output buffer
836 * Returns nothing of value.
841 if ((lpoint
= lpnt
- lpbuf
) > 0)
844 c
[BYTESOUT
] += lpoint
;
846 if (write(lfd
,lpbuf
,lpoint
) != lpoint
)
847 write(2,"error writing to output file\n",29);
849 lpnt
= lpbuf
; /* point back to beginning of buffer */
856 * putchar(ch) Print one character in decoded output buffer.
862 if (index
>= BUFBIG
) flush_buf();
866 * flush_buf() Flush buffer with decoded output.
870 if (index
) write(lfd
, outbuf
, index
);
875 * char *tmcapcnv(sd,ss) Routine to convert VT100 escapes to termcap format
877 * Processes only the \33[#m sequence (converts . files for termcap use
879 char *tmcapcnv(sd
,ss
)
880 register char *sd
,*ss
;
882 register int tmstate
=0; /* 0=normal, 1=\33 2=[ 3=# */
883 char tmdigit
=0; /* the # in \33[#m */
888 case 0: if (*ss
=='\33') { tmstate
++; break; }
892 case 1: if (*ss
!='[') goto ign
;
895 case 2: if (isdigit(*ss
)) { tmdigit
= *ss
-'0'; tmstate
++; break; }
896 if (*ss
== 'm') { *sd
++ = ST_END
; goto ign2
; }
898 case 3: if (*ss
== 'm')
900 if (tmdigit
) *sd
++ = ST_START
;
908 *sd
=0; /* NULL terminator */
914 * beep() Routine to emit a beep if enabled (see no-beep in .larnopts)
918 if (!nobeep
) *lpnt
++ = '\7';