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

  1. /* tinit.c
  2.    Initialize for reading Taylor UUCP configuration files.
  3.  
  4.    Copyright (C) 1992, 1993, 1994, 1995 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_tinit_rcsid[] = "$Id: tinit.c,v 1.13 1995/06/28 14:38:51 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33.  
  34. /* Local functions.  */
  35.  
  36. static int itset_default P((struct sglobal *qglobal, char ***ppzvar,
  37.                 const char *zfile));
  38. static int itdebug P((pointer pglobal, int argc, char **argv, pointer pvar,
  39.               pointer pinfo));
  40. static int itaddfile P((pointer pglobal, int argc, char **argv, pointer pvar,
  41.             pointer pinfo));
  42. static int itunknown P((pointer pglobal, int argc, char **argv, pointer pvar,
  43.             pointer pinfo));
  44. static int itprogram P((pointer pglobal, int argc, char **argv, pointer pvar,
  45.             pointer pinfo));
  46.  
  47. static const struct cmdtab_offset asCmds[] =
  48. {
  49.   { "nodename", UUCONF_CMDTABTYPE_STRING,
  50.       offsetof (struct sprocess, zlocalname), NULL },
  51.   { "hostname", UUCONF_CMDTABTYPE_STRING,
  52.       offsetof (struct sprocess, zlocalname), NULL },
  53.   { "uuname", UUCONF_CMDTABTYPE_STRING,
  54.       offsetof (struct sprocess, zlocalname), NULL },
  55.   { "spool", UUCONF_CMDTABTYPE_STRING,
  56.       offsetof (struct sprocess, zspooldir), NULL },
  57.   { "pubdir", UUCONF_CMDTABTYPE_STRING,
  58.       offsetof (struct sprocess, zpubdir), NULL },
  59.   { "lockdir", UUCONF_CMDTABTYPE_STRING,
  60.       offsetof (struct sprocess, zlockdir), NULL },
  61.   { "logfile", UUCONF_CMDTABTYPE_STRING,
  62.       offsetof (struct sprocess, zlogfile), NULL },
  63.   { "statfile", UUCONF_CMDTABTYPE_STRING,
  64.       offsetof (struct sprocess, zstatsfile), NULL },
  65.   { "debugfile", UUCONF_CMDTABTYPE_STRING,
  66.       offsetof (struct sprocess, zdebugfile), NULL },
  67.   { "debug", UUCONF_CMDTABTYPE_FN | 0,
  68.       offsetof (struct sprocess, zdebug), itdebug },
  69.   { "strip-login", UUCONF_CMDTABTYPE_BOOLEAN,
  70.       offsetof (struct sprocess, fstrip_login), NULL },
  71.   { "strip-proto", UUCONF_CMDTABTYPE_BOOLEAN,
  72.       offsetof (struct sprocess, fstrip_proto), NULL },
  73.   { "max-uuxqts", UUCONF_CMDTABTYPE_INT,
  74.       offsetof (struct sprocess, cmaxuuxqts), NULL },
  75.   { "run-uuxqt", UUCONF_CMDTABTYPE_STRING,
  76.       offsetof (struct sprocess, zrunuuxqt), NULL },
  77.   { "sysfile", UUCONF_CMDTABTYPE_FN | 0,
  78.       offsetof (struct sprocess, pzsysfiles), itaddfile },
  79.   { "portfile", UUCONF_CMDTABTYPE_FN | 0,
  80.       offsetof (struct sprocess, pzportfiles), itaddfile },
  81.   { "dialfile", UUCONF_CMDTABTYPE_FN | 0,
  82.       offsetof (struct sprocess, pzdialfiles), itaddfile },
  83.   { "dialcodefile", UUCONF_CMDTABTYPE_FN | 0,
  84.       offsetof (struct sprocess, pzdialcodefiles), itaddfile },
  85.   { "callfile", UUCONF_CMDTABTYPE_FN | 0,
  86.       offsetof (struct sprocess, pzcallfiles), itaddfile },
  87.   { "passwdfile", UUCONF_CMDTABTYPE_FN | 0,
  88.       offsetof (struct sprocess, pzpwdfiles), itaddfile },
  89.   { "unknown", UUCONF_CMDTABTYPE_FN, offsetof (struct sprocess, qunknown),
  90.       itunknown },
  91.   { "v2-files", UUCONF_CMDTABTYPE_BOOLEAN,
  92.       offsetof (struct sprocess, fv2), NULL },
  93.   { "hdb-files", UUCONF_CMDTABTYPE_BOOLEAN,
  94.       offsetof (struct sprocess, fhdb), NULL },
  95.   { "bnu-files", UUCONF_CMDTABTYPE_BOOLEAN,
  96.       offsetof (struct sprocess, fhdb), NULL },
  97.   { "timetable", UUCONF_CMDTABTYPE_FN | 3,
  98.       offsetof (struct sprocess, pztimetables), _uuconf_itimetable },
  99.   { NULL, 0, 0, NULL }
  100. };
  101.  
  102. #define CCMDS (sizeof asCmds / sizeof asCmds[0])
  103.  
  104. /* This structure is used to pass information into the command table
  105.    functions.  */
  106.  
  107. struct sinfo
  108. {
  109.   /* The program name.  */
  110.   const char *zname;
  111.   /* A pointer to the command table being used, passed to isystem so
  112.      it can call uuconf_cmd_args.  */
  113.   struct uuconf_cmdtab *qcmds;
  114. };
  115.  
  116. /* Initialize the routines which read the Taylor UUCP configuration
  117.    files.  */
  118.  
  119. int
  120. uuconf_taylor_init (ppglobal, zprogram, zname)
  121.      pointer *ppglobal;
  122.      const char *zprogram;
  123.      const char *zname;
  124. {
  125.   struct sglobal **pqglobal = (struct sglobal **) ppglobal;
  126.   int iret;
  127.   char *zcopy;
  128.   struct sglobal *qglobal;
  129.   boolean fdefault;
  130.   FILE *e;
  131.   struct sinfo si;
  132.  
  133.   if (*pqglobal == NULL)
  134.     {
  135.       iret = _uuconf_iinit_global (pqglobal);
  136.       if (iret != UUCONF_SUCCESS)
  137.     return iret;
  138.     }
  139.  
  140.   qglobal = *pqglobal;
  141.  
  142.   if (zname != NULL)
  143.     {
  144.       size_t csize;
  145.  
  146.       csize = strlen (zname) + 1;
  147.       zcopy = uuconf_malloc (qglobal->pblock, csize);
  148.       if (zcopy == NULL)
  149.     {
  150.       qglobal->ierrno = errno;
  151.       return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  152.     }
  153.       memcpy ((pointer) zcopy, (pointer) zname, csize);
  154.       fdefault = FALSE;
  155.     }
  156.   else
  157.     {
  158.       zcopy = uuconf_malloc (qglobal->pblock,
  159.                  sizeof NEWCONFIGLIB + sizeof CONFIGFILE - 1);
  160.       if (zcopy == NULL)
  161.     {
  162.       qglobal->ierrno = errno;
  163.       return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  164.     }
  165.       memcpy ((pointer) zcopy, (pointer) NEWCONFIGLIB,
  166.           sizeof NEWCONFIGLIB - 1);
  167.       memcpy ((pointer) (zcopy + sizeof NEWCONFIGLIB - 1),
  168.           (pointer) CONFIGFILE, sizeof CONFIGFILE);
  169.       fdefault = TRUE;
  170.     }
  171.  
  172.   qglobal->qprocess->zconfigfile = zcopy;
  173.  
  174.   e = fopen (zcopy, "r");
  175.   if (e == NULL)
  176.     {
  177.       if (! fdefault)
  178.     {
  179.       qglobal->ierrno = errno;
  180.       qglobal->zfilename = zcopy;
  181.       return (UUCONF_FOPEN_FAILED
  182.           | UUCONF_ERROR_ERRNO
  183.           | UUCONF_ERROR_FILENAME);
  184.     }
  185.  
  186.       /* There is no config file, so just use the default values.  */
  187.     }
  188.   else
  189.     {
  190.       struct uuconf_cmdtab as[CCMDS];
  191.  
  192.       _uuconf_ucmdtab_base (asCmds, CCMDS, (char *) qglobal->qprocess,
  193.                 as);
  194.  
  195.       if (zprogram == NULL)
  196.     zprogram = "uucp";
  197.  
  198.       si.zname = zprogram;
  199.       si.qcmds = as;
  200.       iret = uuconf_cmd_file (qglobal, e, as, (pointer) &si, itprogram,
  201.                   UUCONF_CMDTABFLAG_BACKSLASH,
  202.                   qglobal->pblock);
  203.  
  204.       (void) fclose (e);
  205.  
  206.       if (iret != UUCONF_SUCCESS)
  207.     {
  208.       qglobal->zfilename = zcopy;
  209.       return iret | UUCONF_ERROR_FILENAME;
  210.     }
  211.     }
  212.  
  213.   /* Get the defaults for the file names.  */
  214.  
  215.   iret = itset_default (qglobal, &qglobal->qprocess->pzsysfiles, SYSFILE);
  216.   if (iret != UUCONF_SUCCESS)
  217.     return iret;
  218.   iret = itset_default (qglobal, &qglobal->qprocess->pzportfiles, PORTFILE);
  219.   if (iret != UUCONF_SUCCESS)
  220.     return iret;
  221.   iret = itset_default (qglobal, &qglobal->qprocess->pzdialfiles, DIALFILE);
  222.   if (iret != UUCONF_SUCCESS)
  223.     return iret;
  224.   iret = itset_default (qglobal, &qglobal->qprocess->pzdialcodefiles,
  225.             DIALCODEFILE);
  226.   if (iret != UUCONF_SUCCESS)
  227.     return iret;
  228.   iret = itset_default (qglobal, &qglobal->qprocess->pzpwdfiles, PASSWDFILE);
  229.   if (iret != UUCONF_SUCCESS)
  230.     return iret;
  231.   iret = itset_default (qglobal, &qglobal->qprocess->pzcallfiles, CALLFILE);
  232.   if (iret != UUCONF_SUCCESS)
  233.     return iret;
  234.  
  235.   return UUCONF_SUCCESS;
  236. }
  237.  
  238. /* Local interface to the _uuconf_idebug_cmd function, which handles
  239.    the "debug" command.  */
  240.  
  241. static int
  242. itdebug (pglobal, argc, argv, pvar, pinfo)
  243.      pointer pglobal;
  244.      int argc;
  245.      char **argv;
  246.      pointer pvar;
  247.      pointer pinfo;
  248. {
  249.   struct sglobal *qglobal = (struct sglobal *) pglobal;
  250.   char **pzdebug = (char **) pvar;
  251.  
  252.   return _uuconf_idebug_cmd (qglobal, pzdebug, argc, argv,
  253.                  qglobal->pblock);
  254. }
  255.  
  256. /* Add new filenames to a list of files.  */
  257.  
  258. /*ARGSUSED*/
  259. static int
  260. itaddfile (pglobal, argc, argv, pvar, pinfo)
  261.      pointer pglobal;
  262.      int argc;
  263.      char **argv;
  264.      pointer pvar;
  265.      pointer pinfo;
  266. {
  267.   struct sglobal *qglobal = (struct sglobal *) pglobal;
  268.   char ***ppz = (char ***) pvar;
  269.   int i;
  270.   int iret;
  271.  
  272.   if (argc == 1)
  273.     {
  274.       iret = _uuconf_iadd_string (qglobal, NULL, FALSE, FALSE, ppz,
  275.                   qglobal->pblock);
  276.       if (iret != UUCONF_SUCCESS)
  277.     return iret;
  278.     }
  279.   else
  280.     {
  281.       for (i = 1; i < argc; i++)
  282.     {
  283.       char *z;
  284.       boolean fallocated;
  285.  
  286.       MAKE_ABSOLUTE (z, fallocated, argv[i], NEWCONFIGLIB,
  287.              qglobal->pblock);
  288.       if (z == NULL)
  289.         {
  290.           qglobal->ierrno = errno;
  291.           return (UUCONF_MALLOC_FAILED
  292.               | UUCONF_ERROR_ERRNO
  293.               | UUCONF_CMDTABRET_EXIT);
  294.         }
  295.       iret = _uuconf_iadd_string (qglobal, z, ! fallocated, FALSE, ppz,
  296.                       qglobal->pblock);
  297.       if (iret != UUCONF_SUCCESS)
  298.         return iret;
  299.     }
  300.     }
  301.  
  302.   return UUCONF_CMDTABRET_CONTINUE;
  303. }
  304.  
  305. /* Handle an "unknown" command.  We accumulate this into a linked
  306.    list, and only parse them later in uuconf_unknown_system_info.  */
  307.  
  308. /*ARGSUSED*/
  309. static int
  310. itunknown (pglobal, argc, argv, pvar, pinfo)
  311.      pointer pglobal;
  312.      int argc;
  313.      char **argv;
  314.      pointer pvar;
  315.      pointer pinfo;
  316. {
  317.   struct sglobal *qglobal = (struct sglobal *) pglobal;
  318.   struct sunknown **pq = (struct sunknown **) pvar;
  319.   struct sunknown *q;
  320.  
  321.   q = (struct sunknown *) uuconf_malloc (qglobal->pblock,
  322.                      sizeof (struct sunknown));
  323.   if (q == NULL)
  324.     {
  325.       qglobal->ierrno = errno;
  326.       return (UUCONF_MALLOC_FAILED
  327.           | UUCONF_ERROR_ERRNO
  328.           | UUCONF_CMDTABRET_EXIT);
  329.     }
  330.   q->qnext = NULL;
  331.   q->ilineno = qglobal->ilineno;
  332.   q->cargs = argc - 1;
  333.   q->pzargs = (char **) uuconf_malloc (qglobal->pblock,
  334.                        (argc - 1) * sizeof (char *));
  335.   if (q->pzargs == NULL)
  336.     {
  337.       qglobal->ierrno = errno;
  338.       return (UUCONF_MALLOC_FAILED
  339.           | UUCONF_ERROR_ERRNO
  340.           | UUCONF_CMDTABRET_EXIT);
  341.     }
  342.   memcpy ((pointer) q->pzargs, (pointer) (argv + 1),
  343.       (argc - 1) * sizeof (char *));
  344.  
  345.   while (*pq != NULL)
  346.     pq = &(*pq)->qnext;
  347.  
  348.   *pq = q;
  349.  
  350.   return UUCONF_CMDTABRET_KEEP;
  351. }
  352.  
  353. /* If we encounter an unknown command, see if it is the program with
  354.    which we were invoked.  If it was, pass the remaining arguments
  355.    back through the table.  */
  356.  
  357. /*ARGSUSED*/
  358. static int
  359. itprogram (pglobal, argc, argv, pvar, pinfo)
  360.      pointer pglobal;
  361.      int argc;
  362.      char **argv;
  363.      pointer pvar;
  364.      pointer pinfo;
  365. {
  366.   struct sglobal *qglobal = (struct sglobal *) pglobal;
  367.   struct sinfo *qinfo = (struct sinfo *) pinfo;
  368.  
  369.   if (argc <= 1
  370.       || strcasecmp (qinfo->zname, argv[0]) != 0)
  371.     return UUCONF_CMDTABRET_CONTINUE;
  372.  
  373.   return uuconf_cmd_args (pglobal, argc - 1, argv + 1, qinfo->qcmds,
  374.               (pointer) NULL, (uuconf_cmdtabfn) NULL, 0,
  375.               qglobal->pblock);
  376. }
  377.  
  378. /* If a filename was not set by the configuration file, add in the
  379.    default value.  */
  380.  
  381. static int
  382. itset_default (qglobal, ppzvar, zfile)
  383.      struct sglobal *qglobal;
  384.      char ***ppzvar;
  385.      const char *zfile;
  386. {
  387.   size_t clen;
  388.   char *zadd;
  389.  
  390.   if (*ppzvar != NULL)
  391.     return UUCONF_SUCCESS;
  392.  
  393.   clen = strlen (zfile);
  394.   zadd = (char *) uuconf_malloc (qglobal->pblock,
  395.                  sizeof NEWCONFIGLIB + clen);
  396.   if (zadd == NULL)
  397.     {
  398.       qglobal->ierrno = errno;
  399.       return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
  400.     }
  401.  
  402.   memcpy ((pointer) zadd, (pointer) NEWCONFIGLIB, sizeof NEWCONFIGLIB - 1);
  403.   memcpy ((pointer) (zadd + sizeof NEWCONFIGLIB - 1), (pointer) zfile,
  404.       clen + 1);
  405.  
  406.   return _uuconf_iadd_string (qglobal, zadd, FALSE, FALSE, ppzvar,
  407.                   qglobal->pblock);
  408. }
  409.  
  410. /* Handle the "debug" command which is documented to take multiple
  411.    arguments.  This is also called by the ``debug'' command in a sys
  412.    file.  It returns a CMDTABRET code.  This should probably be in its
  413.    own file, but the only other place it is called is from tsinfo.c,
  414.    and any user of tsinfo.c it sure to link in this file as well.  */
  415.  
  416. int
  417. _uuconf_idebug_cmd (qglobal, pzdebug, argc, argv, pblock)
  418.      struct sglobal *qglobal;
  419.      char **pzdebug;
  420.      int argc;
  421.      char **argv;
  422.      pointer pblock;
  423. {
  424.   if (argc == 1)
  425.     {
  426.       *pzdebug = NULL;
  427.       return UUCONF_CMDTABRET_CONTINUE;
  428.     }
  429.   else if (argc == 2)
  430.     {
  431.       *pzdebug = argv[1];
  432.       return UUCONF_CMDTABRET_KEEP;
  433.     }
  434.   else
  435.     {
  436.       size_t cdebug;
  437.       int i;
  438.       char *zdebug;
  439.  
  440.       cdebug = 0;
  441.       for (i = 1; i < argc; i++)
  442.     cdebug += strlen (argv[i]) + 1;
  443.       zdebug = (char *) uuconf_malloc (pblock, cdebug);
  444.       if (zdebug == NULL)
  445.     {
  446.       qglobal->ierrno = errno;
  447.       return (UUCONF_MALLOC_FAILED
  448.           | UUCONF_ERROR_ERRNO
  449.           | UUCONF_CMDTABRET_EXIT);
  450.     }
  451.       cdebug = 0;
  452.       for (i = 1; i < argc; i++)
  453.     {
  454.       size_t clen;
  455.  
  456.       clen = strlen (argv[i]);
  457.       memcpy (zdebug + cdebug, argv[i], clen);
  458.       zdebug[cdebug + clen] = ' ';
  459.       cdebug += clen + 1;
  460.     }
  461.       zdebug[cdebug - 1] = '\0';
  462.       *pzdebug = zdebug;
  463.       return UUCONF_CMDTABRET_CONTINUE;
  464.     }
  465. }
  466.