home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 4
/
DATAFILE_PDCD4.iso
/
unix
/
unixlib36d
/
src
/
c
/
termcap
< prev
Wrap
Text File
|
1994-03-08
|
16KB
|
979 lines
#ifdef __STDC__
static char sccs_id[] = "@(#) termcap.c 5.6 " __DATE__ " HJR";
#else
static char sccs_id[] = "@(#) termcap.c 5.6 25/2/92 HJR";
#endif
/* termcap.c (c) Copyright 1989/1990/1991/1992 H.Rogers */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef __STDC__ /* implies ANSI C compilation */
#include <stdlib.h>
#else
char *getenv ();
char *malloc ();
extern long strtol ();
#define const
#endif
#include "termcap.h"
#ifdef ARCH
#define T_BUILTIN /* builtin termcap */
static char *t_bname = "acorn0";
static unsigned char *t_btenv = (unsigned char *)
"av|acorn0|Acorn VDU Driver Mode 0:"
":li#32:co#80:am:cl=^L:bs:cm=^_%r%.%.:up=^K:ho=^^:bl=^G:bw:"
":ce=^W^H^E^F\\200\\200\\200\\200\\200\\200:"
":so=^W^Q^E\\200\\200\\200\\200\\200\\200\\200:"
":se=^W^Q^E\\200\\200\\200\\200\\200\\200\\200:"
":sb=^W^G^A^B\\200\\200\\200\\200\\200\\200:"
":sf=^W^G^A^C\\200\\200\\200\\200\\200\\200:"
":is=^C^F^D^O^V\\200:";
#endif
#define T_IOCTL /* use TIOCGWINSIZ to get LI,CO */
#ifdef T_IOCTL
#include "termio.h"
#ifdef __STDC__
extern int ioctl (int, int, void *);
#else
extern int ioctl ();
#endif
#endif
/* #define T_TEST */
/* test main() */
/* #define T_DEBUG */
/* debugging output to t_debug */
#define T_FILE "/etc/termcap" /* the database file */
#ifdef __STDC__
static int t_tgetnam (unsigned char **, unsigned char **);
static int t_tgetln (FILE *, unsigned char *);
static int t_tentcp (unsigned char *);
static unsigned char *t_tgetid (char *);
static unsigned char t_tcoord (int, unsigned int *);
#else
static int t_tgetnam (); /* extracts the next name from entry */
static int t_tgetln (); /* reads line from termcap file */
static int t_tentcp (); /* copies the termcap entry into bp */
static unsigned char *t_tgetid (); /* returns a pointer to a capability */
static unsigned char t_tcoord (); /* converts a coord to an output val */
#endif /* __STDC__ */
#define MAXTC 64 /* max. tc indirections */
static char __PC, *__BC, *__UP;
static char *t_tbp;
static unsigned char *t_tbpstart;
static int t_recurs, t_tbpspace;
static unsigned char *t_tenv;
#ifdef T_DEBUG
static FILE *t_debug;
#endif
/* tgetent() */
int
tgetent (bp, name)
char *bp, *name;
{
unsigned char *tenv, *tent, *tentbuf, tc;
unsigned char *tp1, *tp2;
char *fnam;
FILE *tfile = 0;
int nbyt, rval;
#ifdef T_DEBUG
if (!t_recurs)
{
if (!(t_debug = fopen ("t_debug", "w")))
return (-1);
setvbuf (t_debug, (char *) 0, _IONBF, BUFSIZ);
}
#endif
if (!(t_tbp = bp))
return (-1);
if (!name)
#ifdef T_BUILTIN
#ifdef T_DEBUG
fputs ("tgetent(\"(null)\")\n", t_debug);
#endif
name = t_bname;
#else
return (-1);
#endif
fnam = T_FILE;
if (!t_recurs)
t_tbpspace = 1024;
if (!t_recurs)
{
char *t = getenv ("TERM");
if (!t)
t = "";
if (!strcmp (name, t))
t_tenv = (unsigned char *) getenv ("TERMCAP");
else
t_tenv = 0;
}
if (tenv = t_tenv)
{
if (!t_recurs && *tenv != '/')
{
#ifdef T_BUILTIN
builtin:
#endif
while (isspace (*tenv))
tenv++;
tp1 = tp2 = tenv;
#ifdef T_DEBUG
fprintf (t_debug, "tgetent(\"%s\") < $TERMCAP\n", name);
#endif
while (t_tgetnam (&tp1, &tp2))
{
tc = *tp2;
*tp2 = 0;
#ifdef T_DEBUG
fprintf (t_debug, "t_tgetnam(): %s\n", (char *) tp1);
#endif
if (!strcmp ((char *) tp1, name))
{
*tp2 = tc;
#ifdef T_DEBUG
fputs ("t_tgetnam() - MATCH -\n", t_debug);
#endif
if (strlen ((char *) tenv) > t_tbpspace)
return (-1);
return (t_tentcp (tenv) ? -1 : 1);
}
else
*tp2 = tc;
}
}
else if (*tenv == '/')
{
#ifdef T_DEBUG
fprintf (t_debug, "tgetent(\"%s\") < %s\n", name, (char *) tenv);
#endif
fnam = (char *) tenv;
}
}
#ifdef T_DEBUG
if (fnam != (char *) t_tenv)
fprintf (t_debug, "tgetent(\"%s\") < /etc/termcap\n", name);
#endif
if (!(tfile = fopen (fnam, "r")))
#ifdef T_BUILTIN
{
if (fnam != (char *) t_tenv && !strcmp (name, t_bname))
{
tenv = t_btenv;
goto builtin;
}
else
return (-1);
}
#else
return (-1);
#endif
tent = tentbuf = (unsigned char *) malloc (1024);
while (nbyt = t_tgetln (tfile, tent))
{
if (*tent != '#')
{
while (isspace (*tent))
tent++;
tp1 = tp2 = tent;
while (t_tgetnam (&tp1, &tp2))
{
tc = *tp2;
*tp2 = 0;
#ifdef T_DEBUG
fprintf (t_debug, "t_tgetnam(): %s\n", (char *) tp1);
#endif
if (!strcmp ((char *) tp1, name))
{
*tp2 = tc;
#ifdef T_DEBUG
fprintf (t_debug, "t_tgetnam() - MATCH -\n");
fprintf (t_debug, "t_tgetnam(): [%d (%d)] %s\n", nbyt, t_tbpspace, tent);
#endif
if (nbyt > t_tbpspace || nbyt < 0)
{
free ((char *) tentbuf);
return (-1);
}
if (tfile)
{
fclose (tfile);
tfile = 0;
}
rval = t_tentcp (tent);
free ((char *) tentbuf);
return (rval ? -1 : 1);
}
else
*tp2 = tc;
}
}
}
if (tfile)
{
fclose (tfile);
tfile = 0;
}
free ((char *) tentbuf);
return (0);
}
/* t_tgetnam() */
static int
t_tgetnam (t1, t2)
unsigned char **t1, **t2;
{
register unsigned char *tp1 = *t1, *tp2 = *t2;
while ((*tp2 == '|') || isspace (*tp2))
tp2++;
if (*tp2 == ':')
return (0);
tp1 = tp2;
while ((*tp2 != '|') && (*tp2 != ':'))
tp2++;
*t1 = tp1;
*t2 = tp2;
return (-1);
}
/* t_tentcp() */
static int
t_tentcp (s)
unsigned char *s;
{
register unsigned char *s1, *s2, *sp;
char *t_tbp_;
char *tcnam, *tcn;
#ifdef T_DEBUG
fputs ("t_tentcp()\n", t_debug);
#endif
tcnam = (char *) malloc (256);
s1 = (unsigned char *) t_tbp;
s2 = s;
while (*s2)
{
if (*s2 == ':') /* strips out empty capabilities :: */
{
sp = s2 + 1;
while (isspace (*sp))
sp++;
if (*sp == ':')
s2 = sp;
}
*s1++ = *s2++;
}
*s1 = 0;
if (!t_recurs)
{
sp = (unsigned char *) t_tbp;
while (*sp++ != ':');
t_tbpstart = sp;
}
if (sp = t_tgetid ("tc"))
{
#ifdef T_DEBUG
fprintf (t_debug, "tgetent()# %d\n", t_recurs);
#endif
if (++t_recurs > (MAXTC - 1))
{
free (tcnam);
return (-1);
}
t_tbp_ = t_tbp;
tcn = tcnam;
s1 = sp = sp - 2;
t_tbpspace -= (int) (s1 - (unsigned char *) t_tbp);
if (tgetent ((char *) s1, tgetstr ("tc", &tcn)) < 1)
{
free (tcnam);
return (-1);
}
while (*sp++ != ':');
while (*s1++ = *sp++);
t_tbp = t_tbp_;
t_recurs--;
}
#ifdef T_DEBUG
fprintf (t_debug, "tgetent(): %s\n", t_tbp);
#endif
if (!t_recurs)
{
__PC = 0;
__BC = "\b";
__UP = "\v";
}
free (tcnam);
return (0);
}
/* t_tgetln() */
static int
t_tgetln (tfile, buf)
FILE *tfile;
register unsigned char *buf;
{
register unsigned char *bufp;
register int nbyt;
bufp = buf;
nbyt = 0;
while (-1)
{
if (!fgets ((char *) bufp, t_tbpspace - nbyt, tfile))
return (nbyt);
while (*bufp++);
bufp--;
if (*--bufp != '\n')
++bufp;
nbyt += (bufp - buf);
if (!nbyt)
continue;
if (*--bufp != '\\')
{
*++bufp = 0;
return (nbyt);
}
buf = bufp;
nbyt--;
}
}
/* t_tgetid() */
static unsigned char *
t_tgetid (id)
register char *id;
{
register unsigned char *bptr;
int found;
if ((!id) || (!t_tbpstart))
return (0);
bptr = t_tbpstart;
while (*bptr)
{
found = (((unsigned char) *id++ == *bptr++) ? \
((unsigned char) *id == *bptr) : 0);
bptr++;
id--;
if (found)
{
if (*bptr == '@')
return (0);
else
return (bptr);
}
while ((*bptr != ':') && (*bptr != 0))
bptr++;
if (*bptr == ':')
bptr++;
}
return (0);
}
/* tgetnum() */
int
tgetnum (id)
char *id;
{
register unsigned char *eptr;
int rval;
#ifdef TIOCGWINSZ
if (rval = (!strncmp (id, "li", 2) ? 1 : (!strncmp (id, "co", 2) ? -1 : 0)))
{
struct winsize w[1];
ioctl (2, TIOCGWINSZ, w);
return ((rval > 0) ? w->ws_row : w->ws_col);
}
#endif
if (!(eptr = t_tgetid (id)))
return (-1);
if (*e