119817Sdist /* 235492Sbostic * Copyright (c) 1983 The Regents of the University of California. 335492Sbostic * All rights reserved. 435492Sbostic * 5*42770Sbostic * %sccs.include.redist.c% 619817Sdist */ 719817Sdist 813280Ssam #ifndef lint 9*42770Sbostic static char sccsid[] = "@(#)v831.c 5.3 (Berkeley) 06/01/90"; 1035492Sbostic #endif /* not lint */ 1113130Sralph 1213130Sralph /* 1313130Sralph * Routines for dialing up on Vadic 831 1413130Sralph */ 1513153Sralph #include <sys/time.h> 1613153Sralph 1713153Sralph #include "tip.h" 1813130Sralph 1913153Sralph int v831_abort(); 2013186Ssam static int alarmtr(); 2113153Sralph extern errno; 2213130Sralph 2313130Sralph static jmp_buf jmpbuf; 2413130Sralph static int child = -1; 2513130Sralph 2613130Sralph v831_dialer(num, acu) 2713153Sralph char *num, *acu; 2813130Sralph { 2913153Sralph int status, pid, connected = 1; 3013153Sralph register int timelim; 3113130Sralph 3213153Sralph if (boolean(value(VERBOSE))) 3313153Sralph printf("\nstarting call..."); 3413130Sralph #ifdef DEBUG 3513153Sralph printf ("(acu=%s)\n", acu); 3613130Sralph #endif 3713186Ssam if ((AC = open(acu, O_RDWR)) < 0) { 3813153Sralph if (errno == EBUSY) 3913153Sralph printf("line busy..."); 4013153Sralph else 4113153Sralph printf("acu open error..."); 4213153Sralph return (0); 4313153Sralph } 4413153Sralph if (setjmp(jmpbuf)) { 4513153Sralph kill(child, SIGKILL); 4613153Sralph close(AC); 4713153Sralph return (0); 4813153Sralph } 4913153Sralph signal(SIGALRM, alarmtr); 5013153Sralph timelim = 5 * strlen(num); 5113153Sralph alarm(timelim < 30 ? 30 : timelim); 5213153Sralph if ((child = fork()) == 0) { 5313153Sralph /* 5413153Sralph * ignore this stuff for aborts 5513153Sralph */ 5613153Sralph signal(SIGALRM, SIG_IGN); 5713130Sralph signal(SIGINT, SIG_IGN); 5813153Sralph signal(SIGQUIT, SIG_IGN); 5913153Sralph sleep(2); 6013153Sralph exit(dialit(num, acu) != 'A'); 6113153Sralph } 6213153Sralph /* 6313153Sralph * open line - will return on carrier 6413153Sralph */ 6513186Ssam if ((FD = open(DV, O_RDWR)) < 0) { 6613130Sralph #ifdef DEBUG 6713153Sralph printf("(after open, errno=%d)\n", errno); 6813130Sralph #endif 6913153Sralph if (errno == EIO) 7013153Sralph printf("lost carrier..."); 7113153Sralph else 7213153Sralph printf("dialup line open failed..."); 7313153Sralph alarm(0); 7413153Sralph kill(child, SIGKILL); 7513153Sralph close(AC); 7613153Sralph return (0); 7713153Sralph } 7813153Sralph alarm(0); 7913153Sralph #ifdef notdef 8013153Sralph ioctl(AC, TIOCHPCL, 0); 8113153Sralph #endif 8213153Sralph signal(SIGALRM, SIG_DFL); 8313153Sralph while ((pid = wait(&status)) != child && pid != -1) 8413153Sralph ; 8513153Sralph if (status) { 8613153Sralph close(AC); 8713153Sralph return (0); 8813153Sralph } 8913153Sralph return (1); 9013130Sralph } 9113130Sralph 9213186Ssam static 9313130Sralph alarmtr() 9413130Sralph { 9513186Ssam 9613153Sralph alarm(0); 9713153Sralph longjmp(jmpbuf, 1); 9813130Sralph } 9913130Sralph 10013130Sralph /* 10113130Sralph * Insurance, for some reason we don't seem to be 10213130Sralph * hanging up... 10313130Sralph */ 10413130Sralph v831_disconnect() 10513130Sralph { 10613153Sralph struct sgttyb cntrl; 10713153Sralph 10813153Sralph sleep(2); 10913130Sralph #ifdef DEBUG 11013153Sralph printf("[disconnect: FD=%d]\n", FD); 11113130Sralph #endif 11213153Sralph if (FD > 0) { 11313153Sralph ioctl(FD, TIOCCDTR, 0); 11413153Sralph ioctl(FD, TIOCGETP, &cntrl); 11513153Sralph cntrl.sg_ispeed = cntrl.sg_ospeed = 0; 11613153Sralph ioctl(FD, TIOCSETP, &cntrl); 11713153Sralph ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL); 11813153Sralph } 11913153Sralph close(FD); 12013130Sralph } 12113130Sralph 12213130Sralph v831_abort() 12313130Sralph { 12413186Ssam 12513130Sralph #ifdef DEBUG 12613153Sralph printf("[abort: AC=%d]\n", AC); 12713130Sralph #endif 12813153Sralph sleep(2); 12913153Sralph if (child > 0) 13013153Sralph kill(child, SIGKILL); 13113153Sralph if (AC > 0) 13213153Sralph ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL); 13313153Sralph close(AC); 13413153Sralph if (FD > 0) 13513153Sralph ioctl(FD, TIOCCDTR, 0); 13613153Sralph close(FD); 13713130Sralph } 13813130Sralph 13913153Sralph /* 14013153Sralph * Sigh, this probably must be changed at each site. 14113153Sralph */ 14213153Sralph struct vaconfig { 14313153Sralph char *vc_name; 14413153Sralph char vc_rack; 14513153Sralph char vc_modem; 14613153Sralph } vaconfig[] = { 14713153Sralph { "/dev/cua0",'4','0' }, 14813153Sralph { "/dev/cua1",'4','1' }, 14913153Sralph { 0 } 15013153Sralph }; 15113153Sralph 15213153Sralph #define pc(x) (c = x, write(AC,&c,1)) 15313153Sralph #define ABORT 01 15413153Sralph #define SI 017 15513153Sralph #define STX 02 15613153Sralph #define ETX 03 15713153Sralph 15813186Ssam static 15913153Sralph dialit(phonenum, acu) 16013153Sralph register char *phonenum; 16113153Sralph char *acu; 16213130Sralph { 16313153Sralph register struct vaconfig *vp; 16413153Sralph struct sgttyb cntrl; 16513153Sralph char c, *sanitize(); 16613153Sralph int i, two = 2; 16713130Sralph 16813153Sralph phonenum = sanitize(phonenum); 16913130Sralph #ifdef DEBUG 17013153Sralph printf ("(dial phonenum=%s)\n", phonenum); 17113130Sralph #endif 17213153Sralph if (*phonenum == '<' && phonenum[1] == 0) 17313153Sralph return ('Z'); 17413153Sralph for (vp = vaconfig; vp->vc_name; vp++) 17513153Sralph if (strcmp(vp->vc_name, acu) == 0) 17613153Sralph break; 17713153Sralph if (vp->vc_name == 0) { 17813130Sralph printf("Unable to locate dialer (%s)\n", acu); 17913153Sralph return ('K'); 18013130Sralph } 18113153Sralph ioctl(AC, TIOCGETP, &cntrl); 18213153Sralph cntrl.sg_ispeed = cntrl.sg_ospeed = B2400; 18313153Sralph cntrl.sg_flags = RAW | EVENP | ODDP; 18413153Sralph ioctl(AC, TIOCSETP, &cntrl); 18513153Sralph ioctl(AC, TIOCFLUSH, &two); 18613153Sralph pc(STX); 18713153Sralph pc(vp->vc_rack); 18813153Sralph pc(vp->vc_modem); 18913153Sralph while (*phonenum && *phonenum != '<') 19013153Sralph pc(*phonenum++); 19113153Sralph pc(SI); 19213153Sralph pc(ETX); 19313153Sralph sleep(1); 19413153Sralph i = read(AC, &c, 1); 19513130Sralph #ifdef DEBUG 19613153Sralph printf("read %d chars, char=%c, errno %d\n", i, c, errno); 19713130Sralph #endif 19813153Sralph if (i != 1) 19913153Sralph c = 'M'; 20013153Sralph if (c == 'B' || c == 'G') { 20113153Sralph char cc, oc = c; 20213130Sralph 20313153Sralph pc(ABORT); 20413153Sralph read(AC, &cc, 1); 20513130Sralph #ifdef DEBUG 20613153Sralph printf("abort response=%c\n", cc); 20713130Sralph #endif 20813153Sralph c = oc; 20913153Sralph v831_disconnect(); 21013153Sralph } 21113153Sralph close(AC); 21213130Sralph #ifdef DEBUG 21313153Sralph printf("dialit: returns %c\n", c); 21413130Sralph #endif 21513153Sralph return (c); 21613130Sralph } 21713153Sralph 21813153Sralph static char * 21913153Sralph sanitize(s) 22013153Sralph register char *s; 22113130Sralph { 22213153Sralph static char buf[128]; 22313153Sralph register char *cp; 22413153Sralph 22513153Sralph for (cp = buf; *s; s++) { 22613153Sralph if (!isdigit(*s) && *s == '<' && *s != '_') 22713153Sralph continue; 22813153Sralph if (*s == '_') 22913153Sralph *s = '='; 23013153Sralph *cp++ = *s; 23113130Sralph } 23213153Sralph *cp++ = 0; 23313153Sralph return (buf); 23413130Sralph } 235