#include "tip.h"
#include <termios.h>
#include <sys/ioctl.h>
#define MAXRETRY 5
static int dialtimeout = 0;
static jmp_buf timeoutbuf;
static void echo(char *);
static void sigALRM(int);
static int gobble(char, char *);
static int vensync(int);
#define delay(num,denom) busyloop(CPUSPEED*num/denom)
#define CPUSPEED 1000000
#define DELAY(n) do { long N = (n); while (--N > 0); } while (0)
#define busyloop(n) do { DELAY(n); } while (0)
int
ven_dialer(char *num, char *acu)
{
char *cp;
int connected = 0;
char *msg, line[80];
struct termios cntrl;
if (!vensync(FD)) {
printf("can't synchronize with ventel\n");
#ifdef ACULOG
logent(value(HOST), num, "ventel", "can't synch up");
#endif
return (0);
}
if (boolean(value(VERBOSE)))
printf("\ndialing...");
fflush(stdout);
tcgetattr(FD, &cntrl);
cntrl.c_cflag |= HUPCL;
tcsetattr(FD, TCSANOW, &cntrl);
echo("#k$\r$\n$D$I$A$L$:$ ");
for (cp = num; *cp; cp++) {
delay(1, 10);
write(FD, cp, 1);
}
delay(1, 10);
write(FD, "\r", 1);
gobble('\n', line);
if (gobble('\n', line))
connected = gobble('!', line);
tcflush(FD, TCIOFLUSH);
#ifdef ACULOG
if (dialtimeout) {
(void)snprintf(line, sizeof line, "%ld second dial timeout",
number(value(DIALTIMEOUT)));
logent(value(HOST), num, "ventel", line);
}
#endif
if (dialtimeout)
ven_disconnect();
if (connected || dialtimeout || !boolean(value(VERBOSE)))
return (connected);
cp = strchr(line, '\r');
if (cp)
*cp = '\0';
for (cp = line; (cp = strchr(cp, ' ')) != NULL; cp++)
if (cp[1] == ' ')
break;
if (cp) {
while (*cp == ' ')
cp++;
msg = cp;
while (*cp) {
if (isupper(*cp))
*cp = tolower(*cp);
cp++;
}
printf("%s...", msg);
}
return (connected);
}
void
ven_disconnect(void)
{
close(FD);
}
void
ven_abort(void)
{
write(FD, "\03", 1);
close(FD);
}
static void
echo(char *s)
{
char c;
while ((c = *s++) != '\0')
switch (c) {
case '$':
read(FD, &c, 1);
s++;
break;
case '#':
c = *s++;
write(FD, &c, 1);
break;
default:
write(FD, &c, 1);
read(FD, &c, 1);
}
}
static void
sigALRM(int signo)
{
printf("\07timeout waiting for reply\n");
dialtimeout = 1;
longjmp(timeoutbuf, 1);
}
static int
gobble(char match, char response[])
{
char *cp = response;
sig_t f;
char c;
f = signal(SIGALRM, sigALRM);
dialtimeout = 0;
do {
if (setjmp(timeoutbuf)) {
signal(SIGALRM, f);
*cp = '\0';
return (0);
}
alarm(number(value(DIALTIMEOUT)));
read(FD, cp, 1);
alarm(0);
c = (*cp++ &= 0177);
#ifdef notdef
if (boolean(value(VERBOSE)))
putchar(c);
#endif
} while (c != '\n' && c != match);
signal(SIGALRM, SIG_DFL);
*cp = '\0';
return (c == match);
}
#define min(a,b) ((a)>(b)?(b):(a))
static int
vensync(int fd)
{
int already = 0, nread;
char buf[60];
ioctl(FD, TIOCCDTR, 0);
sleep(1);
ioctl(FD, TIOCSDTR, 0);
while (already < MAXRETRY) {
write(fd, "\r", 1);
delay(1,10);
write(fd, "\r", 1);
sleep(2);
if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
perror("tip: ioctl");
continue;
}
while (nread > 0) {
read(fd, buf, min(nread, 60));
if ((buf[nread - 1] & 0177) == '$')
return (1);
nread -= min(nread, 60);
}
sleep(1);
already++;
}
return (0);
}