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

  1. /* hport.c
  2.    Find a port in the HDB 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_hport_rcsid[] = "$Id: hport.c,v 1.14 1995/06/21 19:22:58 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33. #include <ctype.h>
  34.  
  35. /* Find a port in the HDB configuration files by name, baud rate, and
  36.    special purpose function.  */
  37.  
  38. int
  39. uuconf_hdb_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.   char *zline;
  50.   size_t cline;
  51.   char **pzsplit;
  52.   size_t csplit;
  53.   int iret;
  54.   char **pz;
  55.  
  56.   zline = NULL;
  57.   cline = 0;
  58.   pzsplit = NULL;
  59.   csplit = 0;
  60.  
  61.   iret = UUCONF_NOT_FOUND;
  62.  
  63.   for (pz = qglobal->qprocess->pzhdb_devices; *pz != NULL; pz++)
  64.     {
  65.       FILE *e;
  66.       int cchars;
  67.  
  68.       qglobal->ilineno = 0;
  69.  
  70.       e = fopen (*pz, "r");
  71.       if (e == NULL)
  72.     {
  73.       if (FNO_SUCH_FILE ())
  74.         continue;
  75.       qglobal->ierrno = errno;
  76.       iret = UUCONF_FOPEN_FAILED | UUCONF_ERROR_ERRNO;
  77.       break;
  78.     }
  79.  
  80.       iret = UUCONF_NOT_FOUND;
  81.  
  82.       while ((cchars = _uuconf_getline (qglobal, &zline, &cline, e)) > 0)
  83.     {
  84.       int ctoks;
  85.       char *z, *zprotos, *zport;
  86.       long ilow, ihigh;
  87.       pointer pblock;
  88.       char ***ppzdialer;
  89.  
  90.       ++qglobal->ilineno;
  91.  
  92.       iret = UUCONF_NOT_FOUND;
  93.  
  94.       --cchars;
  95.       if (zline[cchars] == '\n')
  96.         zline[cchars] = '\0';
  97.       if (isspace (BUCHAR (zline[0])) || zline[0] == '#')
  98.         continue;
  99.  
  100.       ctoks = _uuconf_istrsplit (zline, '\0', &pzsplit, &csplit);
  101.       if (ctoks < 0)
  102.         {
  103.           qglobal->ierrno = errno;
  104.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  105.           break;
  106.         }
  107.  
  108.       /* An entry in Devices is
  109.  
  110.          type device dial-device baud dialer-token pairs
  111.  
  112.          The type (normally "ACU") is treated as the name.  */
  113.  
  114.       /* If there aren't enough entries, ignore the line; this
  115.          should probably do something more useful.  */
  116.       if (ctoks < 4)
  117.         continue;
  118.  
  119.       /* There may be a comma separated list of protocols after
  120.          the name.  */
  121.       zprotos = strchr (pzsplit[0], ',');
  122.       if (zprotos != NULL)
  123.         {
  124.           *zprotos = '\0';
  125.           ++zprotos;
  126.         }
  127.  
  128.       zport = pzsplit[0];
  129.  
  130.       /* Get any modem class, and pick up the baud rate while
  131.          we're at it.  The modem class will be appended to the
  132.          name, so we need to get it before we see if we've found
  133.          the port with the right name.  */
  134.       z = pzsplit[3];
  135.       if (strcasecmp (z, "Any") == 0
  136.           || strcmp (z, "-") == 0)
  137.         {
  138.           ilow = 0L;
  139.           ihigh = 0L;
  140.         }
  141.       else
  142.         {
  143.           char *zend;
  144.  
  145.           while (*z != '\0' && ! isdigit (BUCHAR (*z)))
  146.         ++z;
  147.  
  148.           ilow = strtol (z, &zend, 10);
  149.           if (*zend == '-')
  150.         ihigh = strtol (zend + 1, (char **) NULL, 10);
  151.           else
  152.         ihigh = ilow;
  153.  
  154.           if (z != pzsplit[3])
  155.         {
  156.           size_t cclass, cport;
  157.  
  158.           cclass = z - pzsplit[3];
  159.           cport = strlen (pzsplit[0]);
  160.           zport = malloc (cport + cclass + 1);
  161.           if (zport == NULL)
  162.             {
  163.               qglobal->ierrno = errno;
  164.               iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  165.               break;
  166.             }
  167.           memcpy ((pointer) zport, (pointer) pzsplit[0], cport);
  168.           memcpy ((pointer) (zport + cport), (pointer) pzsplit[3],
  169.               cclass);
  170.           zport[cport + cclass] = '\0';
  171.         }
  172.         }
  173.  
  174.       /* Make sure the name and baud rate match any argument.  */
  175.       if ((zname != NULL
  176.            && strcmp (zport, zname) != 0)
  177.           || (ibaud != 0
  178.           && ilow != 0
  179.           && (ilow > ibaud || ihigh < ibaud)))
  180.         {
  181.           if (zport != pzsplit[0])
  182.         free ((pointer) zport);
  183.           continue;
  184.         }
  185.  
  186.       /* Some systems permit ,M after the device name.  This means
  187.          to open the port with O_NDELAY and then change it.  We
  188.          just ignore this flag, although perhaps we should record
  189.          it somewhere.  */
  190.       pzsplit[1][strcspn (pzsplit[1], ",")] = '\0';
  191.  
  192.       /* Now we must construct the port information, so that we
  193.          can pass it to pifn.  The port type is determined by its
  194.          name, unfortunately.  The name "Direct" is used for a
  195.          direct port, "TCP" for a TCP port, and anything else for
  196.          a modem port.  */
  197.       pblock = NULL;
  198.       _uuconf_uclear_port (qport);
  199.       qport->uuconf_zname = zport;
  200.       qport->uuconf_zprotocols = zprotos;
  201.       if (strcmp (pzsplit[0], "Direct") == 0)
  202.         {
  203.           qport->uuconf_ttype = UUCONF_PORTTYPE_DIRECT;
  204.           qport->uuconf_u.uuconf_sdirect.uuconf_zdevice = pzsplit[1];
  205.           qport->uuconf_u.uuconf_sdirect.uuconf_ibaud = ilow;
  206.           qport->uuconf_u.uuconf_sdirect.uuconf_fcarrier = FALSE;
  207.           qport->uuconf_u.uuconf_sdirect.uuconf_fhardflow = TRUE;
  208.           ppzdialer = NULL;
  209.         }
  210.       else if (strcmp (pzsplit[0], "TCP") == 0)
  211.         {
  212.           /* For a TCP port, the device name is taken as the TCP
  213.          port to use.  */
  214.           qport->uuconf_ttype = UUCONF_PORTTYPE_TCP;
  215.           qport->uuconf_ireliable
  216.         = (UUCONF_RELIABLE_ENDTOEND | UUCONF_RELIABLE_RELIABLE
  217.            | UUCONF_RELIABLE_EIGHT | UUCONF_RELIABLE_FULLDUPLEX
  218.            | UUCONF_RELIABLE_SPECIFIED);
  219.           qport->uuconf_u.uuconf_stcp.uuconf_zport = pzsplit[1];
  220.           ppzdialer = &qport->uuconf_u.uuconf_stcp.uuconf_pzdialer;
  221.         }
  222.       else if (ctoks >= 5
  223.            && (strcmp (pzsplit[4], "TLI") == 0
  224.                || strcmp (pzsplit[4], "TLIS") == 0))
  225.         {
  226.           qport->uuconf_ttype = UUCONF_PORTTYPE_TLI;
  227.           qport->uuconf_u.uuconf_stli.uuconf_zdevice = pzsplit[1];
  228.           qport->uuconf_u.uuconf_stli.uuconf_fstream
  229.         = strcmp (pzsplit[4], "TLIS") == 0;
  230.           qport->uuconf_u.uuconf_stli.uuconf_pzpush = NULL;
  231.           qport->uuconf_u.uuconf_stli.uuconf_zservaddr = NULL;
  232.           qport->uuconf_ireliable
  233.         = (UUCONF_RELIABLE_ENDTOEND | UUCONF_RELIABLE_RELIABLE
  234.            | UUCONF_RELIABLE_EIGHT | UUCONF_RELIABLE_FULLDUPLEX
  235.            | UUCONF_RELIABLE_SPECIFIED);
  236.           ppzdialer = &qport->uuconf_u.uuconf_stli.uuconf_pzdialer;
  237.         }
  238.       else
  239.         {
  240.           qport->uuconf_ttype = UUCONF_PORTTYPE_MODEM;
  241.           qport->uuconf_u.uuconf_smodem.uuconf_zdevice = pzsplit[1];
  242.           if (strcmp (pzsplit[2], "-") != 0)
  243.         qport->uuconf_u.uuconf_smodem.uuconf_zdial_device =
  244.           pzsplit[2];
  245.           else
  246.         qport->uuconf_u.uuconf_smodem.uuconf_zdial_device = NULL;
  247.           if (ilow == ihigh)
  248.         {
  249.           qport->uuconf_u.uuconf_smodem.uuconf_ibaud = ilow;
  250.           qport->uuconf_u.uuconf_smodem.uuconf_ilowbaud = 0L;
  251.           qport->uuconf_u.uuconf_smodem.uuconf_ihighbaud = 0L;
  252.         }
  253.           else
  254.         {
  255.           qport->uuconf_u.uuconf_smodem.uuconf_ibaud = 0L;
  256.           qport->uuconf_u.uuconf_smodem.uuconf_ilowbaud = ilow;
  257.           qport->uuconf_u.uuconf_smodem.uuconf_ihighbaud = ihigh;
  258.         }
  259.           qport->uuconf_u.uuconf_smodem.uuconf_fcarrier = TRUE;
  260.           qport->uuconf_u.uuconf_smodem.uuconf_fhardflow = TRUE;
  261.           qport->uuconf_u.uuconf_smodem.uuconf_qdialer = NULL;
  262.           ppzdialer = &qport->uuconf_u.uuconf_smodem.uuconf_pzdialer;
  263.         }
  264.  
  265.       if (ppzdialer != NULL)
  266.         {
  267.           if (ctoks < 5)
  268.         *ppzdialer = NULL;
  269.           else
  270.         {
  271.           size_t c;
  272.           char **pzd;
  273.  
  274.           pblock = uuconf_malloc_block ();
  275.           if (pblock == NULL)
  276.             {
  277.               qglobal->ierrno = errno;
  278.               iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  279.               break;
  280.             }
  281.           c = (ctoks - 4) * sizeof (char *);
  282.           pzd = (char **) uuconf_malloc (pblock, c + sizeof (char *));
  283.           if (pzd == NULL)
  284.             {
  285.               qglobal->ierrno = errno;
  286.               uuconf_free_block (pblock);
  287.               iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  288.               break;
  289.             }
  290.           memcpy ((pointer) pzd, (pointer) (pzsplit + 4), c);
  291.           pzd[ctoks - 4] = NULL;
  292.  
  293.           *ppzdialer = pzd;
  294.         }
  295.         }
  296.  
  297.       if (pifn != NULL)
  298.         {
  299.           iret = (*pifn) (qport, pinfo);
  300.           if (iret != UUCONF_SUCCESS)
  301.         {
  302.           if (zport != pzsplit[0])
  303.             free ((pointer) zport);
  304.           if (pblock != NULL)
  305.             uuconf_free_block (pblock);
  306.           if (iret != UUCONF_NOT_FOUND)
  307.             break;
  308.           continue;
  309.         }
  310.         }
  311.  
  312.       /* This is the port we want.  */
  313.       if (pblock == NULL)
  314.         {
  315.           pblock = uuconf_malloc_block ();
  316.           if (pblock == NULL)
  317.         {
  318.           qglobal->ierrno = errno;
  319.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  320.           break;
  321.         }
  322.         }
  323.  
  324.       if (uuconf_add_block (pblock, zline) != 0
  325.           || (zport != pzsplit[0]
  326.           && uuconf_add_block (pblock, zport) != 0))
  327.         {
  328.           qglobal->ierrno = errno;
  329.           uuconf_free_block (pblock);
  330.           iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  331.           break;
  332.         }
  333.       zline = NULL;
  334.  
  335.       qport->uuconf_palloc = pblock;
  336.  
  337.       iret = UUCONF_SUCCESS;
  338.  
  339.       break;
  340.     }
  341.  
  342.       (void) fclose (e);
  343.  
  344.       if (iret != UUCONF_NOT_FOUND)
  345.     break;
  346.     }
  347.  
  348.   if (zline != NULL)
  349.     free ((pointer) zline);
  350.   if (pzsplit != NULL)
  351.     free ((pointer) pzsplit);
  352.  
  353.   if (iret != UUCONF_SUCCESS && iret != UUCONF_NOT_FOUND)
  354.     {
  355.       qglobal->zfilename = *pz;
  356.       iret |= UUCONF_ERROR_FILENAME | UUCONF_ERROR_LINENO;
  357.     }
  358.  
  359.   return iret;
  360. }
  361.