#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include "pathnames.h"
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
#define MAXHOP 32
#define tgetent agetent
#define tnchktc anchktc
#define tnamatch anamatch
#define tgetnum agetnum
#define tgetflag agetflag
#define tgetstr agetstr
#if 0
#define V_TERMCAP "REMOTE"
#define V_TERM "HOST"
#endif
static char *tbuf;
static int hopcount;
extern const char *conffile;
int tgetent(char *, char *);
int getent(char *, char *, const char *);
int tnchktc(void);
int tnamatch(char *);
static char *tskip(char *);
int64_t tgetnum(char *);
int tgetflag(char *);
char *tgetstr(char *, char **);
static char *tdecode(char *, char **);
int
tgetent(char *bp, char *name)
{
return (getent(bp, name, conffile));
}
int
getent(char *bp, char *name, const char *cfile)
{
int c;
int i = 0, cnt = 0;
char ibuf[BUFSIZ];
char *cp;
int tf;
tbuf = bp;
tf = 0;
if (cfile && *cfile)
tf = open(cfile, O_RDONLY);
if (tf < 0) {
syslog(LOG_INFO,
"<%s> open: %s", __func__, strerror(errno));
return (-2);
}
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 - 1) {
write(STDERR_FILENO, "Remcap entry too long\n",
22);
break;
} else
*cp++ = c;
}
*cp = 0;
if (tnamatch(name)) {
close(tf);
return (tnchktc());
}
}
}
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) {
write(STDERR_FILENO, "Bad remcap entry\n", 18);
return (0);
}
p++;
if (p[0] != 't' || p[1] != 'c')
return (1);
strlcpy(tcname, p + 3, sizeof tcname);
q = tcname;
while (*q && *q != ':')
q++;
*q = 0;
if (++hopcount > MAXHOP) {
write(STDERR_FILENO, "Infinite tc= loop\n", 18);
return (0);
}
if (getent(tcbuf, tcname, conffile) != 1) {
return (0);
}
for (q = tcbuf; *q++ != ':'; )
;
l = p - holdtbuf + strlen(q);
if (l > BUFSIZ) {
write(STDERR_FILENO, "Remcap entry too long\n", 23);
q[BUFSIZ - (p-holdtbuf)] = 0;
}
strcpy(p, q);
tbuf = holdtbuf;
return (1);
}
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)
{
int dquote;
dquote = 0;
while (*bp) {
switch (*bp) {
case ':':
if (!dquote)
goto breakbreak;
else
bp++;
break;
case '\\':
bp++;
if (isdigit(*bp)) {
while (isdigit(*bp++))
;
} else
bp++;
case '"':
dquote = (dquote ? 1 : 0);
bp++;
break;
default:
bp++;
break;
}
}
breakbreak:
if (*bp == ':')
bp++;
return (bp);
}
int64_t
tgetnum(char *id)
{
int64_t i;
int base;
char *bp = tbuf;
for (;;) {
bp = tskip(bp);
if (*bp == 0)
return (-1);
if (strncmp(bp, id, strlen(id)) != 0)
continue;
bp += strlen(id);
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 (strncmp(bp, id, strlen(id)) == 0) {
bp += strlen(id);
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 (strncmp(bp, id, strlen(id)) != 0)
continue;
bp += strlen(id);
if (*bp == '@')
return (0);
if (*bp != '=')
continue;
bp++;
return (tdecode(bp, area));
}
}
static char *
tdecode(char *str, char **area)
{
char *cp;
int c;
const char *dp;
int i;
char term;
term = ':';
cp = *area;
again:
if (*str == '"') {
term = '"';
str++;
}
while ((c = *str++) && c != term) {
switch (c) {
case '^':
c = *str++ & 037;
break;
case '\\':
dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
c = *str++;
nextc:
if (*dp++ == c) {
c = *dp++;
break;
}
dp++;
if (*dp)
goto nextc;
if (isdigit(c)) {
c -= '0', i = 2;
do
c <<= 3, c |= *str++ - '0';
while (--i && isdigit(*str));
}
break;
}
*cp++ = c;
}
if (c == term && term != ':') {
term = ':';
goto again;
}
*cp++ = 0;
str = *area;
*area = cp;
return (str);
}