122415Sdist /*
2*62317Sbostic * Copyright (c) 1983, 1993
3*62317Sbostic * The Regents of the University of California. All rights reserved.
435492Sbostic *
542770Sbostic * %sccs.include.redist.c%
622415Sdist */
722415Sdist
813277Ssam #ifndef lint
9*62317Sbostic static char sccsid[] = "@(#)biz31.c 8.1 (Berkeley) 06/06/93";
1035492Sbostic #endif /* not lint */
1113277Ssam
123687Sroot #include "tip.h"
133687Sroot
143687Sroot #define MAXRETRY 3 /* sync up retry count */
1513277Ssam #define DISCONNECT_CMD "\21\25\11\24" /* disconnection string */
163687Sroot
1739253Sbostic static void sigALRM();
1813277Ssam static int timeout = 0;
1913277Ssam static jmp_buf timeoutbuf;
203687Sroot
213687Sroot /*
224408Ssam * Dial up on a BIZCOMP Model 1031 with either
233687Sroot * tone dialing (mod = "f")
243687Sroot * pulse dialing (mod = "w")
253687Sroot */
263687Sroot static int
biz_dialer(num,mod)273687Sroot biz_dialer(num, mod)
284962Ssam char *num, *mod;
293687Sroot {
303687Sroot register int connected = 0;
313687Sroot
323687Sroot if (!bizsync(FD)) {
333687Sroot logent(value(HOST), "", "biz", "out of sync");
343687Sroot printf("bizcomp out of sync\n");
353687Sroot delock(uucplock);
363687Sroot exit(0);
373687Sroot }
383687Sroot if (boolean(value(VERBOSE)))
393687Sroot printf("\nstarting call...");
403687Sroot echo("#\rk$\r$\n"); /* disable auto-answer */
413687Sroot echo("$>$.$ #\r"); /* tone/pulse dialing */
423687Sroot echo(mod);
433687Sroot echo("$\r$\n");
443687Sroot echo("$>$.$ #\re$ "); /* disconnection sequence */
4513277Ssam echo(DISCONNECT_CMD);
463687Sroot echo("\r$\n$\r$\n");
473687Sroot echo("$>$.$ #\rr$ "); /* repeat dial */
483687Sroot echo(num);
493687Sroot echo("\r$\n");
503687Sroot if (boolean(value(VERBOSE)))
513687Sroot printf("ringing...");
523687Sroot /*
533687Sroot * The reply from the BIZCOMP should be:
543687Sroot * `^G NO CONNECTION\r\n^G\r\n' failure
553687Sroot * ` CONNECTION\r\n^G' success
563687Sroot */
573687Sroot connected = detect(" ");
583687Sroot #ifdef ACULOG
593687Sroot if (timeout) {
603687Sroot char line[80];
613687Sroot
623687Sroot sprintf(line, "%d second dial timeout",
633687Sroot number(value(DIALTIMEOUT)));
643687Sroot logent(value(HOST), num, "biz", line);
653687Sroot }
663687Sroot #endif
673687Sroot if (!connected)
683687Sroot flush(" NO CONNECTION\r\n\07\r\n");
693687Sroot else
703687Sroot flush("CONNECTION\r\n\07");
713687Sroot if (timeout)
724408Ssam biz31_disconnect(); /* insurance */
735132Ssam return (connected);
743687Sroot }
753687Sroot
biz31w_dialer(num,acu)764408Ssam biz31w_dialer(num, acu)
774962Ssam char *num, *acu;
783687Sroot {
7913277Ssam
805132Ssam return (biz_dialer(num, "w"));
813687Sroot }
823687Sroot
biz31f_dialer(num,acu)834408Ssam biz31f_dialer(num, acu)
844962Ssam char *num, *acu;
853687Sroot {
8613277Ssam
875132Ssam return (biz_dialer(num, "f"));
883687Sroot }
893687Sroot
biz31_disconnect()904408Ssam biz31_disconnect()
913687Sroot {
9213277Ssam
9313277Ssam write(FD, DISCONNECT_CMD, 4);
943687Sroot sleep(2);
953687Sroot ioctl(FD, TIOCFLUSH);
963687Sroot }
973687Sroot
biz31_abort()984408Ssam biz31_abort()
993687Sroot {
10013277Ssam
1013687Sroot write(FD, "\33", 1);
1023687Sroot }
1033687Sroot
1043687Sroot static int
echo(s)1053687Sroot echo(s)
1064962Ssam register char *s;
1073687Sroot {
1083687Sroot char c;
1093687Sroot
1105132Ssam while (c = *s++) switch (c) {
1115132Ssam
1125132Ssam case '$':
1135132Ssam read(FD, &c, 1);
1145132Ssam s++;
1155132Ssam break;
1165132Ssam
1175132Ssam case '#':
1185132Ssam c = *s++;
1195132Ssam write(FD, &c, 1);
1205132Ssam break;
1215132Ssam
1225132Ssam default:
1235132Ssam write(FD, &c, 1);
1245132Ssam read(FD, &c, 1);
1255132Ssam }
1263687Sroot }
1273687Sroot
12839253Sbostic static void
sigALRM()1293687Sroot sigALRM()
1303687Sroot {
13113277Ssam
1323687Sroot timeout = 1;
13313277Ssam longjmp(timeoutbuf, 1);
1343687Sroot }
1353687Sroot
1363687Sroot static int
detect(s)1373687Sroot detect(s)
1384962Ssam register char *s;
1393687Sroot {
14039253Sbostic sig_t f;
1413687Sroot char c;
1423687Sroot
14313277Ssam f = signal(SIGALRM, sigALRM);
1443687Sroot timeout = 0;
1455132Ssam while (*s) {
14613277Ssam if (setjmp(timeoutbuf)) {
14713277Ssam printf("\07timeout waiting for reply\n");
14813277Ssam biz31_abort();
14913277Ssam break;
15013277Ssam }
1513687Sroot alarm(number(value(DIALTIMEOUT)));
1523687Sroot read(FD, &c, 1);
1533687Sroot alarm(0);
1543687Sroot if (c != *s++)
15513277Ssam break;
1563687Sroot }
15713277Ssam signal(SIGALRM, f);
15813277Ssam return (timeout == 0);
1593687Sroot }
1603687Sroot
1613687Sroot static int
flush(s)1623687Sroot flush(s)
1634962Ssam register char *s;
1643687Sroot {
16539253Sbostic sig_t f;
1663687Sroot char c;
1673687Sroot
16813277Ssam f = signal(SIGALRM, sigALRM);
1695132Ssam while (*s++) {
17013277Ssam if (setjmp(timeoutbuf))
17113277Ssam break;
1723687Sroot alarm(10);
1733687Sroot read(FD, &c, 1);
1743687Sroot alarm(0);
1753687Sroot }
17613277Ssam signal(SIGALRM, f);
1773687Sroot timeout = 0; /* guard against disconnection */
1783687Sroot }
1793687Sroot
1803687Sroot /*
1813687Sroot * This convoluted piece of code attempts to get
1823687Sroot * the bizcomp in sync. If you don't have the capacity or nread
1833687Sroot * call there are gory ways to simulate this.
1843687Sroot */
1853687Sroot static int
bizsync(fd)1863687Sroot bizsync(fd)
1873687Sroot {
1883687Sroot #ifdef FIOCAPACITY
1893687Sroot struct capacity b;
1903687Sroot # define chars(b) ((b).cp_nbytes)
1913687Sroot # define IOCTL FIOCAPACITY
1923687Sroot #endif
1933687Sroot #ifdef FIONREAD
1943687Sroot long b;
1953687Sroot # define chars(b) (b)
1963687Sroot # define IOCTL FIONREAD
1973687Sroot #endif
1983687Sroot register int already = 0;
1993687Sroot char buf[10];
2003687Sroot
2013687Sroot retry:
2023687Sroot if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
2033687Sroot ioctl(fd, TIOCFLUSH);
2043687Sroot write(fd, "\rp>\r", 4);
2053687Sroot sleep(1);
2063687Sroot if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
2073687Sroot if (chars(b) != 10) {
2083687Sroot nono:
2093687Sroot if (already > MAXRETRY)
2105132Ssam return (0);
21113277Ssam write(fd, DISCONNECT_CMD, 4);
2123687Sroot sleep(2);
2133687Sroot already++;
2143687Sroot goto retry;
2153687Sroot } else {
2163687Sroot read(fd, buf, 10);
2173687Sroot if (strncmp(buf, "p >\r\n\r\n>", 8))
2183687Sroot goto nono;
2193687Sroot }
2203687Sroot }
2215132Ssam return (1);
2223687Sroot }
223