home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / modemcap / dial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  5.2 KB  |  256 lines

  1. #include <fcntl.h>
  2. #include <dial.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include "modemcap.h"
  6.  
  7. #ifdef    UNIX_V7
  8. typedef    int    void;
  9. #endif
  10. static    char    lockfile[64];
  11. static    int    modemfd = -1;
  12. extern    int    merrno;
  13.  
  14. static    findline (line, baud)
  15. char    *line;
  16. int    baud;
  17. {
  18.     int    exists = 0;
  19.     int    l_baud;
  20.     char    l_line[DVC_LEN+1];
  21.     char    l_type[DVC_LEN+1];
  22.     char    buf[64];
  23.     FILE    *fp;
  24.  
  25.     if ((fp = fopen (LDEVS, "r")) == NULL)
  26.         return (merrno = NO_Ldv);
  27.  
  28.     while (fgets (buf, 64, fp) != NULL) {
  29.         if (buf[0] == '#')        /* ignore comment lines            */
  30.             continue;
  31.  
  32.         if (sscanf (buf, "%s%s%*s%d", l_type, l_line, &l_baud) != 3)
  33.             continue;        /* mangled line                */
  34.  
  35.         if (strcmp (l_type, "DIR") != 0)
  36.             continue;        /* not a direct connect line        */
  37.  
  38.         if (strcmp (l_line, line) == 0)
  39.             exists++;        /* say device exists at some baud rate    */
  40.         else
  41.             continue;        /* wrong device                */
  42.  
  43.         if (l_baud == baud) {        /* found device at desired baud rate    */
  44.             fclose (fp);
  45.             return (1);
  46.         }
  47.     }
  48.     if (exists)
  49.         return (merrno = ILL_BD);
  50.     else
  51.         return (merrno = DV_NT_K);
  52. }
  53.  
  54. static    char    *findmodem (line)
  55. {
  56.     static    char    modemtype[16];
  57.     char    device[DVC_LEN + 1];
  58.     char    buf[82];
  59.     FILE    *fp;
  60.  
  61.     if ((fp = fopen (_MODEMTYPE_, "r")) == NULL)
  62.         return (NULL);
  63.  
  64.     while (fgets (buf, 82, fp) != NULL) {
  65.         if (buf[0] == '#')
  66.             continue;
  67.  
  68.         sscanf (buf, "%s%s\n", modemtype, device);
  69.         if (strcmp (line, device) == 0) {
  70.             fclose (fp);
  71.             return (modemtype);
  72.         }
  73.     }
  74.     fclose (fp);
  75.     return (NULL);
  76. }
  77.  
  78. struct speedlist {
  79.     int    value;
  80.     int    name;
  81. } speeds[] = {
  82.     {    0,     0},
  83.     {  110,  B110},
  84.     {  300,  B300},
  85.     {  600,  B600},
  86.     { 1200, B1200},
  87.     { 2400, B2400},
  88.     { 4800, B4800},
  89.     { 9600, B9600},
  90.     {19200, EXTA},
  91.     {38400, EXTB},
  92.     {   -1, -1}
  93. };
  94.  
  95. static    findspeed (speed)
  96. int    speed;
  97. {
  98.     register struct    speedlist *ps;
  99.  
  100.     for (ps = speeds; ps->value >= 0; ps++)
  101.         if (ps->value == speed)
  102.             return (ps->name);
  103.  
  104.     return (0);
  105. }
  106.  
  107. alarmcatch ()
  108. {
  109.     long    timebuf[2];
  110.  
  111.     time (&timebuf[0]);
  112.     timebuf[1] = timebuf[0];
  113.  
  114.     utime (lockfile, timebuf);
  115.     signal (SIGALRM, alarmcatch);
  116.     alarm (3600);
  117. }
  118.  
  119. hupcatch ()
  120. {
  121.     close (modemfd);
  122.     unlink (lockfile);
  123.     signal (SIGHUP, SIG_DFL);
  124. }
  125.  
  126. int    dial (call)
  127. CALL    *call;
  128. {
  129.     char    modemline[64];            /* device name                */
  130.     char    *modemname;            /* modemcap name of modem        */
  131.     char    *strcpy (),
  132.         *strcat ();
  133. #ifdef    UNIX_S5
  134.     struct    termio    termio;
  135. #endif
  136. #ifdef    UNIX_V7
  137.     struct    sgttyb    termio;
  138. #endif
  139.     int    fd;
  140.     int    err;
  141.  
  142.     strcat (strcpy (modemline, DEVDIR), call->line);
  143.     strcat (strcpy (lockfile, LOCK), call->line);
  144.  
  145.     /*
  146.      * FIX - Version 7 does not have three operand open(), write
  147.      * conditional compilation for this here ...
  148.      */
  149.  
  150.     if (access (lockfile, 0) == 0 || (fd = creat (lockfile, 0)) == -1)
  151.         return (merrno = DV_NT_A);    /* lock existed or couldn't be created    */
  152.     else
  153.         close (fd);            /* created lock, now close descriptor    */
  154.  
  155.     fd = -1;                /* channel illegal until line is opened    */
  156.  
  157.     if ((err = findline (call->line, call->baud)) <= 0)
  158.         goto error;
  159.  
  160.     if ((modemname = findmodem (call->line)) == NULL) { /* can't determine the type of modem    */
  161.         err = DV_NT_K;
  162.         goto error;
  163.     }
  164.     if ((fd = open (modemline, O_RDWR)) < 0) { /* can't open modem line        */
  165.         err = L_PROB;
  166.         goto error;
  167.     }
  168. #ifdef    UNIX_S5
  169.     if (call->attr != (struct termio *) 0) { /* set attributes            */
  170.         if (ioctl (fd, TCSETA, call->attr) == -1) { /* some ioctl() problem    */
  171.             err = L_PROB;
  172.             goto error;
  173.         }
  174.     } else {
  175.         ioctl (fd, TCGETA, &termio);
  176.         if ((termio.c_cflag = findspeed (call->baud)) == 0) {
  177.             err = ILL_BD;
  178.             goto error;
  179.         }
  180.         termio.c_cflag |= (CS8|CREAD|HUPCL);
  181.         termio.c_iflag = 0;
  182.         termio.c_oflag = 0;
  183.         termio.c_lflag = 0;
  184.         termio.c_cc[VMIN] = 1;
  185.         termio.c_cc[VTIME] = 1;
  186.         if (ioctl (fd, TCSETA, &termio) == -1) {
  187.             err = L_PROB;
  188.             goto error;
  189.         }
  190.     }
  191. #endif
  192. #ifdef    UNIX_V7
  193.     if (call->attr != (struct sgttyb *) 0) { /* set attributes            */
  194.         if (gtty (fd, call->attr) == -1) { /* some gtty() problem */
  195.             err = L_PROB;
  196.             goto error;
  197.         }
  198.     } else {
  199.         gtty (fd, &termio);
  200.         if ((termio.sg_ispeed = findspeed (call->baud)) == 0) {
  201.             err = ILL_BD;
  202.             goto error;
  203.         }
  204.         termio.sg_ospeed = termio.sg_ispeed;
  205.         termio.sg_flags = RAW|ANYP;
  206.         termio.sg_erase = -1;
  207.         termio.sg_kill = -1;
  208.         if (stty (fd, &termio) == -1) {
  209.             err = L_PROB;
  210.             goto error;
  211.         }
  212.     }
  213. #endif
  214.     initmodem (modemname, fd);        /* setup modemcap variables        */
  215.     if (call->telno == (char *) 0)        /* no phone number, connection complete    */
  216.         goto okay;
  217.  
  218.     if (! DI) {                /* modem has no ACU!!!            */
  219.         err = A_PROB;            /* no ACU to attach to            */
  220.         goto error;
  221.     }
  222.     if (BD != call->baud) {            /* is connection desired at high speed?    */
  223.         if (BL != call->baud)    {    /* is connection desired at low speed?    */
  224.             err = ILL_BD;        /* modem can't handle this speed    */
  225.             goto error;
  226.         }
  227.         BD = BL;            /* set baud to low baud rate        */
  228.         CO = CL;            /* set connect reply to low baud reply    */
  229.     }
  230.     if (err = mdial (call->telno, fd))     /* some error trying to dial        */
  231.         goto error;
  232.  
  233.     signal (SIGALRM, alarmcatch);        /* set catcher for ALARM clock        */
  234.     signal (SIGHUP, hupcatch);        /* set catcher for HANG UP        */
  235.     alarm (3600);                /* set clock for 1 hour to touch lock    */
  236.  
  237. okay:
  238.     return (modemfd = fd);
  239.  
  240. error:
  241.     unlink (lockfile);
  242.     if (fd > 2)
  243.         close (fd);
  244.     return (merrno = err);
  245. }
  246.  
  247. void    undial (fd)
  248. int    fd;
  249. {
  250.     if (fd > 2)
  251.         close (fd);
  252.  
  253.     unlink (lockfile);
  254.     alarm (0);
  255. }
  256.