#include <config.h>
#include <sys/signal.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <setjmp.h>
#include <sys/ioctl.h>
#include <sys/devinfo.h>
#include <termios.h>
#include <termio.h>
#include <sys/hft.h>
#include <sys/uio.h>
#include <sys/tty.h>
#define REMOTE 0x01
#undef ioctl
static char SCCSid[] = "com/gnuemacs/src,3.1,9021-90/05/03-5/3/90";
#define QDEV ((HFQPDEVCH<<8)|HFQPDEVCL)
#define QLOC ((HFQLOCCH<<8)|HFQLOCCL)
#define QPS ((HFQPRESCH<<8)|HFQPRESCL)
#ifndef TCGETS
#define TCGETS TCGETA
#endif
#ifndef TCSETS
#define TCSETS TCSETA
#endif
static int hfqry();
static int hfskbd();
char *xmalloc();
extern int errno;
static jmp_buf hftenv;
static int is_ack_vtd;
static SIGTYPE (*sav_alrm) ();
static struct hfctlreq req =
{ 0x1b,'[','x',0,0,0,21,HFCTLREQCH,HFCTLREQCL};
static struct hfctlack ACK =
{ 0x1b,'[','x',0,0,0,21,HFCTLACKCH,HFCTLACKCL};
#define HFTYPE(p) ((p->hf_typehi<<8)|(p->hf_typelo))
#define BYTE4(p) ((p)[0]<<24 | (p)[1]<<16 | (p)[2]<<8 | (p)[3])
#define RD_BUF(f,p,l) \
while ((l)) \
if ((j = emacs_read (f, p, l)) < 0) \
if (errno != EINTR) return (-1); \
else continue; \
else { (l) -= j; (p) += j; }
#ifdef PROTOTYPES
static GT_ACK (int fd, int req, char *buf);
static WR_REQ (int fd, int request, int cmdlen, char *cmd, int resplen);
static void hft_alrm(int sig);
#else
static GT_ACK ();
static WR_REQ ();
static void hft_alrm ();
#endif
hftctl (fd, request, arg)
int fd;
int request;
union {
struct hfintro *intro;
struct hfquery *query;
char *c;
} arg;
{
int i;
int fd_flag;
register union {
struct hfintro *cmd;
struct hfqphdevc *ph;
char *c;
} p;
int pty_new;
int pty_old;
int retcode;
struct termios term_new;
struct termios term_old;
struct devinfo devInfo;
if (ioctl (fd, IOCINFO, &devInfo) == -1) return(-1);
if (devInfo.devtype != DD_PSEU)
return (ioctl(fd, request, arg));
if ((fd_flag = fcntl (fd, F_GETFL, 0)) == -1) return (-1);
if (ioctl (fd, TCGETS, &term_old) == -1) return (-1);
pty_new = pty_old | REMOTE;
memcpy (&term_new, &term_old, sizeof (term_new));
term_new.c_iflag = 0;
term_new.c_oflag = 0;
term_new.c_lflag = 0;
for (i = 1; i <= 5; i++)
term_new.c_cc[i] = 0;
term_new.c_cc[0] = -1;
ioctl (fd, TCSETS, &term_new);
if (fcntl (fd, F_SETFL, fd_flag & ~O_NDELAY) == -1)
return(-1);
if (request == HFSKBD)
retcode = hfskbd (fd, request, arg.c);
else
retcode = hfqry (fd, request, arg.c);
fcntl (fd, F_SETFL, fd_flag);
ioctl (fd, TCSETS, &term_old);
return (retcode);
}
static int
hfskbd (fd, request, arg)
int fd;
int request;
struct hfbuf *arg;
{
WR_REQ(fd, request, arg->hf_buflen, arg->hf_bufp,0);
return (GT_ACK(fd, request, arg->hf_bufp));
}
static int
hfqry (fd, request, arg)
int fd;
int request;
struct hfquery *arg;
{
WR_REQ(fd, request, arg->hf_cmdlen, arg->hf_cmd, arg->hf_resplen);
return (GT_ACK(fd, request, arg->hf_resp));
}
static int
GT_ACK (fd, req, buf)
int fd;
int req;
char *buf;
{
struct hfctlack ack;
int i = sizeof (ack);
int j = 0;
union {
char *c;
struct hfctlack *ack;
} p;
is_ack_vtd = 0;
if (setjmp (hftenv))
{
errno = ENODEV;
return (-1);
}
alarm(3);
sav_alrm = signal (SIGALRM, hft_alrm);
p.ack = &ack;
while (! is_ack_vtd)
{
RD_BUF(fd, p.c, i);
if (! memcmp (&ack, &ACK, sizeof (HFINTROSZ))
&& (ack.hf_request == req))
{
is_ack_vtd = 1;
break;
}
p.ack = &ack;
++p.c;
i = sizeof (ack) - 1;
while ((*p.c != 0x1b) && i)
{ ++p.c; --i; }
(i ? memcpy (&ack, p.c, i) : 0);
p.ack = &ack;
p.c += i;
i = sizeof (ack) - i;
}
alarm(0);
signal (SIGALRM, sav_alrm);
if (i = ack.hf_arg_len)
{
RD_BUF(fd,buf,i);
}
if (errno = ack.hf_retcode)
return (-1);
else
return (0);
}
static void
hft_alrm (sig)
int sig;
{
signal (SIGALRM, sav_alrm);
if (is_ack_vtd)
return;
else
longjmp (hftenv, -1);
}
static int
WR_REQ (fd, request, cmdlen, cmd, resplen)
int fd;
int request;
int cmdlen;
char *cmd;
int resplen;
{
struct {
char *c;
struct hfctlreq *req;
} p;
int size;
req.hf_request = request;
req.hf_arg_len = cmdlen;
req.hf_rsp_len = resplen;
if (cmdlen)
{
size = sizeof (struct hfctlreq) + cmdlen;
if ((p.c = xmalloc(size)) == NULL)
return (-1);
memcpy (p.c, &req, sizeof (req));
memcpy (p.c + sizeof (req), cmd, cmdlen);
}
else
{
p.req = &req;
size = sizeof (req);
}
if (emacs_write (fd, p.c, size) == -1) return (-1);
if (p.req != &req)
xfree (p.c);
return (0);
}