#define BUFSIZ 1024
#define MAXHOP 32
#include <ctype.h>
#include <locale.h>
#include <string.h>
static char *tbuf;
static char *filename;
static int hopcount;
char *tgetstr();
char *getenv();
static char *tdecode(char *str, char **area);
static char *tskip(char *bp);
static int tnchktc(void);
static int tnamatch(char *np);
static char *vgrind_msg;
int
tgetent(char *bp, char *name, char *file)
{
char *cp;
int c;
int i = 0, cnt = 0;
char ibuf[BUFSIZ];
char *cp2;
int tf;
tbuf = bp;
tf = 0;
filename = file;
tf = open(filename, 0);
if (tf < 0)
return (-1);
for (;;) {
cp = bp;
for (;;) {
if (i == cnt) {
cnt = read(tf, ibuf, BUFSIZ);
if (cnt <= 0) {
close(tf);
return (0);
}
i = 0;
}
c = ibuf[i++];
if (c == '\n') {
if (cp > bp && cp[-1] == '\\'){
cp--;
continue;
}
break;
}
if (cp >= bp+BUFSIZ) {
vgrind_msg = gettext("Vgrind entry too long\n");
write(2, vgrind_msg, strlen(vgrind_msg));
break;
} else
*cp++ = c;
}
*cp = 0;
if (tnamatch(name)) {
close(tf);
return(tnchktc());
}
}
}
static int
tnchktc(void)
{
char *p, *q;
char tcname[16];
char tcbuf[BUFSIZ];
char *holdtbuf = tbuf;
int l;
p = tbuf + strlen(tbuf) - 2;
while (*--p != ':')
if (p<tbuf) {
vgrind_msg = gettext("Bad vgrind entry\n");
write(2, vgrind_msg, strlen(vgrind_msg));
return (0);
}
p++;
if (p[0] != 't' || p[1] != 'c')
return(1);
strcpy(tcname,p+3);
q = tcname;
while (q && *q != ':')
q++;
*q = 0;
if (++hopcount > MAXHOP) {
vgrind_msg = gettext("Infinite tc= loop\n");
write(2, vgrind_msg, strlen(vgrind_msg));
return (0);
}
if (tgetent(tcbuf, tcname, filename) != 1)
return(0);
for (q=tcbuf; *q != ':'; q++)
;
l = p - holdtbuf + strlen(q);
if (l > BUFSIZ) {
vgrind_msg = gettext("Vgrind entry too long\n");
write(2, vgrind_msg, strlen(vgrind_msg));
q[BUFSIZ - (p-tbuf)] = 0;
}
strcpy(p, q+1);
tbuf = holdtbuf;
return(1);
}
static int
tnamatch(char *np)
{
char *Np, *Bp;
Bp = tbuf;
if (*Bp == '#')
return(0);
for (;;) {
for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
continue;
if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
return (1);
while (*Bp && *Bp != ':' && *Bp != '|')
Bp++;
if (*Bp == 0 || *Bp == ':')
return (0);
Bp++;
}
}
static char *
tskip(char *bp)
{
while (*bp && *bp != ':')
bp++;
if (*bp == ':')
bp++;
return (bp);
}
static int
tgetnum(char *id)
{
int i, base;
char *bp = tbuf;
for (;;) {
bp = tskip(bp);
if (*bp == 0)
return (-1);
if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
continue;
if (*bp == '@')
return(-1);
if (*bp != '#')
continue;
bp++;
base = 10;
if (*bp == '0')
base = 8;
i = 0;
while (isdigit(*bp))
i *= base, i += *bp++ - '0';
return (i);
}
}
int
tgetflag(char *id)
{
char *bp = tbuf;
for (;;) {
bp = tskip(bp);
if (!*bp)
return (0);
if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
if (!*bp || *bp == ':')
return (1);
else if (*bp == '@')
return(0);
}
}
}
char *
tgetstr(char *id, char **area)
{
char *bp = tbuf;
for (;;) {
bp = tskip(bp);
if (!*bp)
return (0);
if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
continue;
if (*bp == '@')
return(0);
if (*bp != '=')
continue;
bp++;
return (tdecode(bp, area));
}
}
static char *
tdecode(char *str, char **area)
{
char *cp;
int c;
int i;
cp = *area;
while (c = *str++) {
if (c == ':' && *(cp-1) != '\\')
break;
*cp++ = c;
}
*cp++ = 0;
str = *area;
*area = cp;
return (str);
}