home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 7 Games
/
07-Games.zip
/
MVTREK.ZIP
/
TERMIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-06-03
|
5KB
|
247 lines
/* terminal I/O */
#include "vtrek.h"
#ifdef CPM /* OS/2, MSDOS and UNIX users don't need to fiddle */
/*============================================================================
TERMINAL PATCH AREA FOR CP/M VERSION
To modify the CP/M object code using DDT, ZSID or whatever:-
Use the D(ump) command to look for the string 'CURSES' and patch the
following bytes.
The clear-screen and clear-to-end-of-line strings are each 16 bytes
long. The first byte indicates the number of significant characters
so 15 bytes are available for each function. */
char DDTloc[] = {'C','U','R','S','E','S'}; /* No trailing NUL */
char cl[16] = {7,025,027,0177,0177,0177,0177,0177}; /* clear screen */
char ce[16] = {6,026,0177,0177,0177,0177,0177}; /* clear to end of line */
/* The following three control strings are used to build the cursor-addressing
sequence. The format is similar to the above except that each string is
eight bytes long, i.e. length byte and up to 7 characters. */
char cpLead[8] = {1,9}; /* Lead-in sequence (before either coordinate) */
char cpMid[8] = {0}; /* Between coordinates */
char cpTrail[8] = {4,0177,0177,0177,0177}; /* After 2nd co-ordinate */
/* The next byte should be ZERO if the terminal expects to receive the
y-coordinate (line number) first, or NON-ZERO if it expects the x-
coordinate (column number) first. */
char xFirst = 1;
/* The next byte should be ZERO if the terminal expects co-ordinates to
be single-byte binary numbers, or NON-ZERO if the terminal expects
co-ordinates to be ASCII digits. */
char asciiXY = 0;
/* Finally, the next two bytes are the 'offsets' to add to the x (column)
and y (line) coordinates for cursor addressing. The home position is
(1,1) so that for most terminals the offset will be 0x1F (31). */
char xOffset = 0xFF;
char yOffset = 0xFF;
#endif /* End of CP/M terminal patch area */
/*=========================================================================*/
#ifdef UNIX
#include <sgtty.h>
static int saveflags;
static char cl[5], cm[20], ce[20], bp[1024], name[20];
char PC, BC[5], UP[5];
#endif
/* initialize the termimal mode */
terminit()
{
#ifdef UNIX
struct sgttyb tty;
char *p, *getenv();
if (p = getenv("TERM"))
strcpy(name, p);
else {
fprintf(stderr, "TERM not set\n");
exit(1);
}
if (tgetent(bp, name) != 1) {
fprintf(stderr, "Can't get termcap entry\n");
exit(1);
}
p = cm;
tgetstr("cm", &p);
p = cl;
tgetstr("cl", &p);
p = ce;
tgetstr("ce", &p);
p = UP;
tgetstr("up", &p);
p = BC;
tgetstr("bc", &p);
gtty(0, &tty);
saveflags = tty.sg_flags;
tty.sg_flags |= RAW;
tty.sg_flags &= ~(ECHO | XTABS);
stty(0, &tty);
#endif
}
/* reset the terminal mode */
termreset()
{
#ifdef UNIX
struct sgttyb tty;
gtty(0, &tty);
tty.sg_flags = saveflags;
stty(0, &tty);
#endif
}
/* get a character from the terminal */
#ifndef OS2 /* MS-DOS version of Hi-Tech C has this already */
/* and so does the Microsoft C library */
getch()
{
#ifdef AZTEC
int ch;
while ((ch = CPM(6, 0xff)) == 0)
;
return ch;
#endif
#ifdef HI_TECH_C
int ch;
while (!(ch = bdos(6,0xFF)));
return ch;
#endif
#ifdef UNIX
return getchar() & 0177;
#endif
}
#endif
/* write a character */
#ifndef OS2
putch(ch)
int ch;
{
#ifdef AZTEC
CPM(6, ch);
#endif
#ifdef HI_TECH_C
bdos(6,ch);
#else /* UNIX */
putchar(ch);
#endif
}
#endif
#ifndef UNIX
/* see if a character is ready to be read */
chready()
{
#ifdef OS2
fflush(stdout);
return kbhit();
fflush(stdout);
#endif
#ifdef CPM
#ifdef HI_TECH_C
return bdos(11,0);
#else /* AZTEC */
return CPM(11,0);
#endif
#endif
}
#endif
#ifdef CPM
void vputs(s)
unsigned char *s;
{
unsigned char l;
for (l=*s++;l--;)
putch(*s++);
}
void coord(xy)
unsigned char xy;
{
if (asciiXY)
printf("%u",xy);
else
putch(xy);
}
#endif
/* move cursor */
moveyx(ypos,xpos)
int ypos,xpos;
{
#ifdef OS2
fflush(stdout);
printf("%c[%d;%dH",033,ypos,xpos);
fflush(stdout);
#endif
#ifdef UNIX
fputs(tgoto(cm, xpos - 1, ypos - 1), stdout);
#endif
#ifdef CPM
xpos += xOffset;
ypos += yOffset;
vputs(cpLead);
coord(xFirst?xpos:ypos);
vputs(cpMid);
coord(xFirst?ypos:xpos);
vputs(cpTrail);
#endif
}
/* clear screen */
cls()
{
#ifdef UNIX
fputs(cl, stdout);
#endif
#ifdef OS2
fflush(stdout);
fputs("\033[2J",stdout);
fflush(stdout);
#endif
#ifdef CPM
vputs(cl);
#endif
}
/* clear end of line */
ceol()
{
#ifdef UNIX
fputs(ce, stdout);
#endif
#ifdef OS2
fflush(stdout);
fputs("\033[K",stdout);
fflush(stdout);
#endif
#ifdef CPM
vputs(ce);
#endif
}