home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / tip / aculib / v831.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  6.8 KB  |  260 lines

  1. /*
  2.  * Copyright (c) 1983 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)v831.c    5.5 (Berkeley) 3/2/91";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Routines for dialing up on Vadic 831
  40.  */
  41. #include "tip.h"
  42.  
  43. int    v831_abort();
  44. static    void alarmtr();
  45. extern    int errno;
  46.  
  47. static jmp_buf jmpbuf;
  48. static int child = -1;
  49.  
  50. v831_dialer(num, acu)
  51.         char *num, *acu;
  52. {
  53.         int status, pid, connected = 1;
  54.         register int timelim;
  55.     static int dialit();
  56.  
  57.         if (boolean(value(VERBOSE)))
  58.                 printf("\nstarting call...");
  59. #ifdef DEBUG
  60.         printf ("(acu=%s)\n", acu);
  61. #endif
  62.         if ((AC = open(acu, O_RDWR)) < 0) {
  63.                 if (errno == EBUSY)
  64.                         printf("line busy...");
  65.                 else
  66.                         printf("acu open error...");
  67.                 return (0);
  68.         }
  69.         if (setjmp(jmpbuf)) {
  70.                 kill(child, SIGKILL);
  71.                 close(AC);
  72.                 return (0);
  73.         }
  74.         signal(SIGALRM, alarmtr);
  75.         timelim = 5 * strlen(num);
  76.         alarm(timelim < 30 ? 30 : timelim);
  77.         if ((child = fork()) == 0) {
  78.                 /*
  79.                  * ignore this stuff for aborts
  80.                  */
  81.                 signal(SIGALRM, SIG_IGN);
  82.         signal(SIGINT, SIG_IGN);
  83.                 signal(SIGQUIT, SIG_IGN);
  84.                 sleep(2);
  85.                 exit(dialit(num, acu) != 'A');
  86.         }
  87.         /*
  88.          * open line - will return on carrier
  89.          */
  90.         if ((FD = open(DV, O_RDWR)) < 0) {
  91. #ifdef DEBUG
  92.                 printf("(after open, errno=%d)\n", errno);
  93. #endif
  94.                 if (errno == EIO)
  95.                         printf("lost carrier...");
  96.                 else
  97.                         printf("dialup line open failed...");
  98.                 alarm(0);
  99.                 kill(child, SIGKILL);
  100.                 close(AC);
  101.                 return (0);
  102.         }
  103.         alarm(0);
  104. #ifdef notdef
  105.         ioctl(AC, TIOCHPCL, 0);
  106. #endif
  107.         signal(SIGALRM, SIG_DFL);
  108.         while ((pid = wait(&status)) != child && pid != -1)
  109.                 ;
  110.         if (status) {
  111.                 close(AC);
  112.                 return (0);
  113.         }
  114.         return (1);
  115. }
  116.  
  117. static void
  118. alarmtr()
  119. {
  120.         alarm(0);
  121.         longjmp(jmpbuf, 1);
  122. }
  123.  
  124. /*
  125.  * Insurance, for some reason we don't seem to be
  126.  *  hanging up...
  127.  */
  128. v831_disconnect()
  129. {
  130.         struct sgttyb cntrl;
  131.  
  132.         sleep(2);
  133. #ifdef DEBUG
  134.         printf("[disconnect: FD=%d]\n", FD);
  135. #endif
  136.         if (FD > 0) {
  137.                 ioctl(FD, TIOCCDTR, 0);
  138.                 ioctl(FD, TIOCGETP, &cntrl);
  139.                 cntrl.sg_ispeed = cntrl.sg_ospeed = 0;
  140.                 ioctl(FD, TIOCSETP, &cntrl);
  141.                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
  142.         }
  143.         close(FD);
  144. }
  145.  
  146. v831_abort()
  147. {
  148.  
  149. #ifdef DEBUG
  150.         printf("[abort: AC=%d]\n", AC);
  151. #endif
  152.         sleep(2);
  153.         if (child > 0)
  154.                 kill(child, SIGKILL);
  155.         if (AC > 0)
  156.                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
  157.                 close(AC);
  158.         if (FD > 0)
  159.                 ioctl(FD, TIOCCDTR, 0);
  160.         close(FD);
  161. }
  162.  
  163. /*
  164.  * Sigh, this probably must be changed at each site.
  165.  */
  166. struct vaconfig {
  167.     char    *vc_name;
  168.     char    vc_rack;
  169.     char    vc_modem;
  170. } vaconfig[] = {
  171.     { "/dev/cua0",'4','0' },
  172.     { "/dev/cua1",'4','1' },
  173.     { 0 }
  174. };
  175.  
  176. #define pc(x)    (c = x, write(AC,&c,1))
  177. #define ABORT    01
  178. #define SI    017
  179. #define STX    02
  180. #define ETX    03
  181.  
  182. static int
  183. dialit(phonenum, acu)
  184.     register char *phonenum;
  185.     char *acu;
  186. {
  187.         register struct vaconfig *vp;
  188.     struct sgttyb cntrl;
  189.         char c;
  190.         int i, two = 2;
  191.     static char *sanitize();
  192.  
  193.         phonenum = sanitize(phonenum);
  194. #ifdef DEBUG
  195.         printf ("(dial phonenum=%s)\n", phonenum);
  196. #endif
  197.         if (*phonenum == '<' && phonenum[1] == 0)
  198.                 return ('Z');
  199.     for (vp = vaconfig; vp->vc_name; vp++)
  200.         if (strcmp(vp->vc_name, acu) == 0)
  201.             break;
  202.     if (vp->vc_name == 0) {
  203.         printf("Unable to locate dialer (%s)\n", acu);
  204.         return ('K');
  205.     }
  206.         ioctl(AC, TIOCGETP, &cntrl);
  207.         cntrl.sg_ispeed = cntrl.sg_ospeed = B2400;
  208.         cntrl.sg_flags = RAW | EVENP | ODDP;
  209.         ioctl(AC, TIOCSETP, &cntrl);
  210.     ioctl(AC, TIOCFLUSH, &two);
  211.         pc(STX);
  212.     pc(vp->vc_rack);
  213.     pc(vp->vc_modem);
  214.     while (*phonenum && *phonenum != '<')
  215.         pc(*phonenum++);
  216.         pc(SI);
  217.     pc(ETX);
  218.         sleep(1);
  219.         i = read(AC, &c, 1);
  220. #ifdef DEBUG
  221.         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
  222. #endif
  223.         if (i != 1)
  224.         c = 'M';
  225.         if (c == 'B' || c == 'G') {
  226.                 char cc, oc = c;
  227.  
  228.                 pc(ABORT);
  229.                 read(AC, &cc, 1);
  230. #ifdef DEBUG
  231.                 printf("abort response=%c\n", cc);
  232. #endif
  233.                 c = oc;
  234.                 v831_disconnect();
  235.         }
  236.         close(AC);
  237. #ifdef DEBUG
  238.         printf("dialit: returns %c\n", c);
  239. #endif
  240.         return (c);
  241. }
  242.  
  243. static char *
  244. sanitize(s)
  245.     register char *s;
  246. {
  247.         static char buf[128];
  248.         register char *cp;
  249.  
  250.         for (cp = buf; *s; s++) {
  251.         if (!isdigit(*s) && *s == '<' && *s != '_')
  252.             continue;
  253.         if (*s == '_')
  254.             *s = '=';
  255.         *cp++ = *s;
  256.     }
  257.         *cp++ = 0;
  258.         return (buf);
  259. }
  260.