home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / uuconf.subproj / vport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  6.7 KB  |  255 lines

  1. /* vport.c
  2.    Find a port in the V2 configuration files.
  3.  
  4.    Copyright (C) 1992, 1993 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP uuconf library.
  7.  
  8.    This library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public License
  10.    as published by the Free Software Foundation; either version 2 of
  11.    the License, or (at your option) any later version.
  12.  
  13.    This library is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with this library; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucnfi.h"
  27.  
  28. #if USE_RCS_ID
  29. const char _uuconf_vport_rcsid[] = "$Id: vport.c,v 1.9 1995/06/21 19:25:19 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33. #include <ctype.h>
  34.  
  35. /* Find a port in the V2 configuration files by name, baud rate, and
  36.    special purpose function.  */
  37.  
  38. int
  39. uuconf_v2_find_port (pglobal, zname, ibaud, ihighbaud, pifn, pinfo, qport)
  40.      pointer pglobal;
  41.      const char *zname;
  42.      long ibaud;
  43.      long ihighbaud;
  44.      int (*pifn) P((struct uuconf_port *, pointer));
  45.      pointer pinfo;
  46.      struct uuconf_port *qport;
  47. {
  48.   struct sglobal *qglobal = (struct sglobal *) pglobal;
  49.   FILE *e;
  50.   char *zline;
  51.   size_t cline;
  52.   char **pzsplit;
  53.   size_t csplit;
  54.   int iret;
  55.   int cchars;
  56.  
  57.   e = fopen (qglobal->qprocess->zv2devices, "r");
  58.   if (e == NULL)
  59.     {
  60.       if (FNO_SUCH_FILE ())
  61.     return UUCONF_NOT_FOUND;
  62.       qglobal->ierrno = errno;
  63.       qglobal->zfilename = qglobal->qprocess->zv2devices;
  64.       return (UUCONF_FOPEN_FAILED
  65.           | UUCONF_ERROR_ERRNO
  66.           | UUCONF_ERROR_FILENAME);
  67.     }
  68.  
  69.   zline = NULL;
  70.   cline = 0;
  71.   pzsplit = NULL;
  72.   csplit = 0;
  73.  
  74.   iret = UUCONF_NOT_FOUND;
  75.  
  76.   qglobal->ilineno = 0;
  77.  
  78.   while ((cchars = getline (&zline, &cline, e)) > 0)
  79.     {
  80.       int ctoks;
  81.       char *zend;
  82.       long ilow, ihigh;
  83.       pointer pblock;
  84.  
  85.       ++qglobal->ilineno;
  86.  
  87.       iret = UUCONF_NOT_FOUND;
  88.  
  89.       --cchars;
  90.       if (zline[cchars] == '\n')
  91.     zline[cchars] = '\0';
  92.       zline[strcspn (zline, "#")] = '\0';
  93.  
  94.       ctoks = _uuconf_istrsplit (zline, '\0', &pzsplit, &csplit);
  95.       if (ctoks < 0)
  96.     {
  97.       qglobal->ierrno = errno;
  98.       iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  99.       break;
  100.     }
  101.  
  102.       /* An entry in L-devices is
  103.      
  104.      type device dial-device baud dialer
  105.      
  106.      The type (normally "ACU") is treated as the name.  */
  107.  
  108.       /* If there aren't enough entries, ignore the line; this
  109.      should probably do something more useful.  */
  110.       if (ctoks < 4)
  111.     continue;
  112.  
  113.       /* Make sure the name matches any argument.  */
  114.       if (zname != NULL
  115.       && strcmp (pzsplit[0], zname) != 0)
  116.     continue;
  117.  
  118.       /* Get the baud rate.  */
  119.       ilow = strtol (pzsplit[3], &zend, 10);
  120.       if (*zend == '-')
  121.     ihigh = strtol (zend + 1, (char **) NULL, 10);
  122.       else
  123.     ihigh = ilow;
  124.  
  125.       /* Make sure the baud rate matches any argument.  */
  126.       if (ibaud != 0
  127.       && ilow != 0
  128.       && (ilow > ibaud || ihigh < ibaud))
  129.     continue;
  130.  
  131.       /* Now we must construct the port information, so that we can
  132.      pass it to pifn.  The port type is determined by it's name,
  133.      unfortunately.  The name "DIR" is used for a direct port, and
  134.      anything else for a modem port.  */
  135.       pblock = NULL;
  136.       _uuconf_uclear_port (qport);
  137.       qport->uuconf_zname = pzsplit[0];
  138.       if (strcmp (pzsplit[0], "DIR") == 0)
  139.     {
  140.       qport->uuconf_ttype = UUCONF_PORTTYPE_DIRECT;
  141.       qport->uuconf_u.uuconf_sdirect.uuconf_zdevice = pzsplit[1];
  142.       qport->uuconf_u.uuconf_sdirect.uuconf_ibaud = ilow;
  143.       qport->uuconf_u.uuconf_sdirect.uuconf_fcarrier = FALSE;
  144.       qport->uuconf_u.uuconf_sdirect.uuconf_fhardflow = TRUE;
  145.     }
  146.       else
  147.     {
  148.       qport->uuconf_ttype = UUCONF_PORTTYPE_MODEM;
  149.       qport->uuconf_u.uuconf_smodem.uuconf_zdevice = pzsplit[1];
  150.       if (strcmp (pzsplit[2], "-") != 0)
  151.         qport->uuconf_u.uuconf_smodem.uuconf_zdial_device = pzsplit[2];
  152.       else
  153.         qport->uuconf_u.uuconf_smodem.uuconf_zdial_device = NULL;
  154.       if (ilow == ihigh)
  155.         {
  156.           qport->uuconf_u.uuconf_smodem.uuconf_ibaud = ilow;
  157.           qport->uuconf_u.uuconf_smodem.uuconf_ilowbaud = 0L;
  158.           qport->uuconf_u.uuconf_smodem.uuconf_ihighbaud = 0L;
  159.         }
  160.       else
  161.         {
  162.           qport->uuconf_u.uuconf_smodem.uuconf_ibaud = 0L;
  163.           qport->uuconf_u.uuconf_smodem.uuconf_ilowbaud = ilow;
  164.           qport->uuconf_u.uuconf_smodem.uuconf_ihighbaud = ihigh;
  165.         }
  166.       qport->uuconf_u.uuconf_smodem.uuconf_fcarrier = TRUE;
  167.       qport->uuconf_u.uuconf_smodem.uuconf_fhardflow = TRUE;
  168.       if (ctoks < 5)
  169.         qport->uuconf_u.uuconf_smodem.uuconf_pzdialer = NULL;
  170.       else
  171.         {
  172.           size_t c;
  173.           char **pzd;
  174.  
  175.           /* We support dialer/token pairs, although normal V2
  176.          doesn't.  */
  177.           pblock = uuconf_malloc_block ();
  178.           if (pblock == NULL)
  179.         {
  180.           qglobal->ierrno = errno;
  181.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  182.           break;
  183.         }
  184.           c = (ctoks - 4) * sizeof (char *);
  185.           pzd = (char **) uuconf_malloc (pblock, c + sizeof (char *));
  186.           if (pzd == NULL)
  187.         {
  188.           qglobal->ierrno = errno;
  189.           uuconf_free_block (pblock);
  190.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  191.           break;
  192.         }
  193.           memcpy ((pointer) pzd, (pointer) (pzsplit + 4), c);
  194.           pzd[ctoks - 4] = NULL;
  195.  
  196.           qport->uuconf_u.uuconf_smodem.uuconf_pzdialer = pzd;
  197.         }
  198.       qport->uuconf_u.uuconf_smodem.uuconf_qdialer = NULL;
  199.     }
  200.  
  201.       if (pifn != NULL)
  202.     {
  203.       iret = (*pifn) (qport, pinfo);
  204.       if (iret != UUCONF_SUCCESS)
  205.         {
  206.           if (pblock != NULL)
  207.         uuconf_free_block (pblock);
  208.           if (iret != UUCONF_NOT_FOUND)
  209.         break;
  210.           continue;
  211.         }
  212.     }
  213.  
  214.       /* This is the port we want.  */
  215.       if (pblock == NULL)
  216.     {
  217.       pblock = uuconf_malloc_block ();
  218.       if (pblock == NULL)
  219.         {
  220.           qglobal->ierrno = errno;
  221.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  222.           break;
  223.         }
  224.     }
  225.  
  226.       if (uuconf_add_block (pblock, zline) != 0)
  227.     {
  228.       qglobal->ierrno = errno;
  229.       uuconf_free_block (pblock);
  230.       iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  231.       break;
  232.     }
  233.       zline = NULL;
  234.  
  235.       qport->uuconf_palloc = pblock;
  236.  
  237.       break;
  238.     }
  239.  
  240.   (void) fclose (e);
  241.  
  242.   if (zline != NULL)
  243.     free ((pointer) zline);
  244.   if (pzsplit != NULL)
  245.     free ((pointer) pzsplit);
  246.  
  247.   if (iret != UUCONF_SUCCESS && iret != UUCONF_NOT_FOUND)
  248.     {
  249.       qglobal->zfilename = qglobal->qprocess->zv2devices;
  250.       iret |= UUCONF_ERROR_FILENAME | UUCONF_ERROR_LINENO;
  251.     }
  252.  
  253.   return iret;
  254. }
  255.