1*13130Sralph /* 831.c 4.1 83/5/10 */ 2*13130Sralph 3*13130Sralph #if V831 4*13130Sralph /* 5*13130Sralph * Routines for dialing up on Vadic 831 6*13130Sralph */ 7*13130Sralph #include "tip.h" 8*13130Sralph #include <setjmp.h> 9*13130Sralph #include <errno.h> 10*13130Sralph #include <sgtty.h> 11*13130Sralph #include <sys/file.h> 12*13130Sralph #include <time.h> 13*13130Sralph 14*13130Sralph static char *sccsid = "@(#)v831.c 4.1 06/15/83"; 15*13130Sralph 16*13130Sralph struct mx_leaves { 17*13130Sralph char *name; 18*13130Sralph char rack,modem; 19*13130Sralph } pdevs[] = {{"/dev/cua0",'4','0'}, {"/dev/cua1",'4','1'}, {0}}; 20*13130Sralph 21*13130Sralph struct timeval zerotime = {0L, 0L}; 22*13130Sralph 23*13130Sralph #define unlike(a,b) (strcmp(a,b)) 24*13130Sralph #define pc(x) (c = x, write(AC,&c,1)) 25*13130Sralph #define ABORT 01 26*13130Sralph #define SI 017 27*13130Sralph #define STX 02 28*13130Sralph #define ETX 03 29*13130Sralph 30*13130Sralph int v831_abort(); 31*13130Sralph 32*13130Sralph int alarmtr(); 33*13130Sralph 34*13130Sralph static jmp_buf jmpbuf; 35*13130Sralph static int child = -1; 36*13130Sralph 37*13130Sralph v831_dialer(num, acu) 38*13130Sralph char *num, *acu; 39*13130Sralph { 40*13130Sralph extern errno; 41*13130Sralph char *p, *q, phone[40]; 42*13130Sralph char char_rv; 43*13130Sralph int lt, nw, connected = 1; 44*13130Sralph register int timelim; 45*13130Sralph 46*13130Sralph if (boolean(value(VERBOSE))) 47*13130Sralph printf("\nstarting call..."); 48*13130Sralph #ifdef DEBUG 49*13130Sralph printf ("(acu=%s)", acu); 50*13130Sralph #endif 51*13130Sralph if ((AC = open(acu, FRDWR)) < 0) { 52*13130Sralph if (errno == EBUSY) 53*13130Sralph printf("line busy..."); 54*13130Sralph else 55*13130Sralph printf("acu open error..."); 56*13130Sralph return (0); 57*13130Sralph } 58*13130Sralph if (setjmp(jmpbuf)) { 59*13130Sralph kill(child, SIGKILL); 60*13130Sralph close(AC); 61*13130Sralph return (0); 62*13130Sralph } 63*13130Sralph signal(SIGALRM, alarmtr); 64*13130Sralph timelim = 5 * strlen(num); 65*13130Sralph alarm(timelim < 30 ? 30 : timelim); 66*13130Sralph if ((child = fork()) == 0) { 67*13130Sralph /* 68*13130Sralph * ignore this stuff for aborts 69*13130Sralph */ 70*13130Sralph signal(SIGALRM, SIG_IGN); 71*13130Sralph signal(SIGINT, SIG_IGN); 72*13130Sralph signal(SIGQUIT, SIG_IGN); 73*13130Sralph sleep(2); 74*13130Sralph /*nw = write(AC, num, lt = strlen(num));*/ 75*13130Sralph char_rv = dialit (num, acu); 76*13130Sralph exit(char_rv != 'A'); 77*13130Sralph } 78*13130Sralph /* 79*13130Sralph * open line - will return on carrier 80*13130Sralph */ 81*13130Sralph if ((FD = open(DV, 2)) < 0) { 82*13130Sralph #ifdef DEBUG 83*13130Sralph printf("(after open, errno=%d)", errno); 84*13130Sralph #endif 85*13130Sralph if (errno == EIO) 86*13130Sralph printf("lost carrier..."); 87*13130Sralph else 88*13130Sralph printf("dialup line open failed..."); 89*13130Sralph alarm(0); 90*13130Sralph kill(child, SIGKILL); 91*13130Sralph close(AC); 92*13130Sralph return (0); 93*13130Sralph } 94*13130Sralph alarm(0); 95*13130Sralph /*ioctl(AC, TIOCHPCL, 0);*/ 96*13130Sralph signal(SIGALRM, SIG_DFL); 97*13130Sralph while ((nw = wait(<)) != child && nw != -1) 98*13130Sralph ; 99*13130Sralph fflush(stdout); 100*13130Sralph /*close(AC);*/ 101*13130Sralph if (lt != 0) { 102*13130Sralph close(AC); 103*13130Sralph return (0); 104*13130Sralph } 105*13130Sralph return (1); 106*13130Sralph } 107*13130Sralph 108*13130Sralph alarmtr() 109*13130Sralph { 110*13130Sralph alarm(0); 111*13130Sralph longjmp(jmpbuf, 1); 112*13130Sralph } 113*13130Sralph 114*13130Sralph /* 115*13130Sralph * Insurance, for some reason we don't seem to be 116*13130Sralph * hanging up... 117*13130Sralph */ 118*13130Sralph v831_disconnect() 119*13130Sralph { 120*13130Sralph struct sgttyb cntrl; 121*13130Sralph sleep(2); 122*13130Sralph #ifdef VMUNIX 123*13130Sralph #ifdef DEBUG 124*13130Sralph printf ("[disconnect: FD=%d]", FD); 125*13130Sralph #endif 126*13130Sralph if (FD > 0) 127*13130Sralph { 128*13130Sralph ioctl (FD, TIOCCDTR, 0); 129*13130Sralph ioctl (FD, TIOCGETP, &cntrl); 130*13130Sralph cntrl.sg_ispeed = 0; 131*13130Sralph cntrl.sg_ospeed = 0; 132*13130Sralph ioctl (FD, TIOCSETP, &cntrl); 133*13130Sralph ioctl (FD, TIOCNXCL, (struct sgttyb *)NULL); 134*13130Sralph } 135*13130Sralph #endif 136*13130Sralph close(FD); 137*13130Sralph } 138*13130Sralph 139*13130Sralph v831_abort() 140*13130Sralph { 141*13130Sralph #ifdef DEBUG 142*13130Sralph printf ("[abort: AC=%d]", AC); 143*13130Sralph #endif 144*13130Sralph sleep(2); 145*13130Sralph if (child > 0) 146*13130Sralph kill(child, SIGKILL); 147*13130Sralph if (AC > 0) 148*13130Sralph ioctl (FD, TIOCNXCL, (struct sgttyb *)NULL); 149*13130Sralph close(AC); 150*13130Sralph #ifdef VMUNIX 151*13130Sralph if (FD > 0) 152*13130Sralph ioctl(FD, TIOCCDTR, 0); 153*13130Sralph #endif 154*13130Sralph close(FD); 155*13130Sralph } 156*13130Sralph #endif 157*13130Sralph 158*13130Sralph static struct sgttyb cntrl; 159*13130Sralph dialit(string, acu) 160*13130Sralph register char *string; 161*13130Sralph char *acu; 162*13130Sralph { 163*13130Sralph char c, cc, *sanitize(); 164*13130Sralph int i; 165*13130Sralph register struct mx_leaves *lp = pdevs; 166*13130Sralph int test; 167*13130Sralph int nfds, fdsmask; 168*13130Sralph 169*13130Sralph string = sanitize(string); 170*13130Sralph #ifdef DEBUG 171*13130Sralph printf ("(dial string=%s)", string); 172*13130Sralph #endif 173*13130Sralph if(*string=='<' && string[1]==0) { 174*13130Sralph return('Z'); 175*13130Sralph } 176*13130Sralph 177*13130Sralph while(test = unlike(lp->name,acu)) 178*13130Sralph if(lp->name==0) { 179*13130Sralph printf("Unable to locate dialer (%s)\n", acu); 180*13130Sralph return('K'); 181*13130Sralph } else lp++; 182*13130Sralph 183*13130Sralph 184*13130Sralph gtty (AC,&cntrl); /* set raw, -echo, 2400 Baud */ 185*13130Sralph cntrl.sg_ispeed = cntrl.sg_ospeed = B2400; 186*13130Sralph cntrl.sg_flags = RAW | EVENP | ODDP; 187*13130Sralph stty (AC,&cntrl); 188*13130Sralph 189*13130Sralph /* check for characters waiting from dialer (throw them away) */ 190*13130Sralph 191*13130Sralph fdsmask = 1<<AC; 192*13130Sralph #ifdef DEBUG 193*13130Sralph printf ("{select returns=%d}", select (20, &fdsmask, 0, 0, &zerotime)); 194*13130Sralph #endif 195*13130Sralph 196*13130Sralph pc (STX); pc (lp->rack); pc (lp->modem); 197*13130Sralph for (;*string && *string!='<'; string++) 198*13130Sralph { 199*13130Sralph #ifdef DEBUG 200*13130Sralph printf ("%c", *string); 201*13130Sralph #endif 202*13130Sralph pc(*string); 203*13130Sralph } 204*13130Sralph pc(SI); pc(ETX); 205*13130Sralph 206*13130Sralph sleep (1); 207*13130Sralph i = read (AC, &c, 1); 208*13130Sralph #ifdef DEBUG 209*13130Sralph printf ("read response of %d chars, char = %c\n", i, c); 210*13130Sralph printf ("and errno is %d\n", errno); 211*13130Sralph #endif 212*13130Sralph 213*13130Sralph if (i !=1) c = 'M'; 214*13130Sralph if (c=='B' || c=='G') { 215*13130Sralph char oc = c; 216*13130Sralph pc(ABORT); 217*13130Sralph read (AC, &cc, 1); 218*13130Sralph #ifdef DEBUG 219*13130Sralph printf ("abort response=%c\n", cc); 220*13130Sralph #endif 221*13130Sralph c = oc; 222*13130Sralph v831_disconnect (); 223*13130Sralph } 224*13130Sralph 225*13130Sralph close(AC); 226*13130Sralph #ifdef DEBUG 227*13130Sralph printf ("dialit: returns %c\n", c); 228*13130Sralph #endif 229*13130Sralph return(c); 230*13130Sralph } 231*13130Sralph char * 232*13130Sralph sanitize(string) 233*13130Sralph register char *string; 234*13130Sralph { 235*13130Sralph static char buf[512]; 236*13130Sralph register char *cp = buf; 237*13130Sralph for(;*string; string++) { 238*13130Sralph switch(*string) { 239*13130Sralph case '0': case '1': case '2': case '3': case '4': 240*13130Sralph case '5': case '6': case '7': case '8': case '9': case '<': 241*13130Sralph *cp++ = *string; 242*13130Sralph break; 243*13130Sralph case '_': 244*13130Sralph *cp++ = '='; 245*13130Sralph break; 246*13130Sralph } 247*13130Sralph } 248*13130Sralph *cp++ = 0; 249*13130Sralph return(buf); 250*13130Sralph } 251