1/* diacod.c
2   Translate a dialcode.
3
4   Copyright (C) 1992, 1993, 2002 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.
23   */
24
25#include "uucnfi.h"
26
27#if USE_RCS_ID
28const char _uuconf_diacod_rcsid[] = "$Id: diacod.c,v 1.12 2002/03/05 19:10:42 ian Rel $";
29#endif
30
31#include <errno.h>
32
33static int idcode P((pointer pglobal, int argc, char **argv,
34		     pointer pinfo, pointer pvar));
35
36/* Get the name of the UUCP log file.  */
37
38int
39uuconf_dialcode (pglobal, zdial, pznum)
40     pointer pglobal;
41     const char *zdial;
42     char **pznum;
43{
44  struct sglobal *qglobal = (struct sglobal *) pglobal;
45  struct uuconf_cmdtab as[2];
46  char **pz;
47  int iret;
48
49  as[0].uuconf_zcmd = zdial;
50  as[0].uuconf_itype = UUCONF_CMDTABTYPE_FN | 0;
51  as[0].uuconf_pvar = (pointer) pznum;
52  as[0].uuconf_pifn = idcode;
53
54  as[1].uuconf_zcmd = NULL;
55
56  *pznum = NULL;
57
58  iret = UUCONF_SUCCESS;
59
60  for (pz = qglobal->qprocess->pzdialcodefiles; *pz != NULL; pz++)
61    {
62      FILE *e;
63
64      e = fopen (*pz, "r");
65      if (e == NULL)
66	{
67	  if (FNO_SUCH_FILE ())
68	    continue;
69	  qglobal->ierrno = errno;
70	  iret = UUCONF_FOPEN_FAILED | UUCONF_ERROR_ERRNO;
71	  break;
72	}
73
74      iret = uuconf_cmd_file (pglobal, e, as, (pointer) NULL,
75			      (uuconf_cmdtabfn) NULL, 0, (pointer) NULL);
76      (void) fclose (e);
77
78      if (iret != UUCONF_SUCCESS || *pznum != NULL)
79	break;
80    }
81
82  if (iret != UUCONF_SUCCESS)
83    {
84      qglobal->zfilename = *pz;
85      iret |= UUCONF_ERROR_FILENAME;
86    }
87  else if (*pznum == NULL)
88    iret = UUCONF_NOT_FOUND;
89
90  return iret;
91}
92
93/* This is called if the dialcode is found.  It copies the number into
94   the heap and gets out of reading the file.  */
95
96/*ARGSUSED*/
97static int
98idcode (pglobal, argc, argv, pvar, pinfo)
99     pointer pglobal;
100     int argc;
101     char **argv;
102     pointer pvar;
103     pointer pinfo ATTRIBUTE_UNUSED;
104{
105  struct sglobal *qglobal = (struct sglobal *) pglobal;
106  char **pznum = (char **) pvar;
107
108  if (argc == 1)
109    {
110      *pznum = malloc (1);
111      if (*pznum != NULL)
112	**pznum = '\0';
113    }
114  else if (argc == 2)
115    *pznum = strdup (argv[1]);
116  else
117    return UUCONF_SYNTAX_ERROR | UUCONF_CMDTABRET_EXIT;
118
119  if (*pznum == NULL)
120    {
121      qglobal->ierrno = errno;
122      return (UUCONF_MALLOC_FAILED
123	      | UUCONF_ERROR_ERRNO
124	      | UUCONF_CMDTABRET_EXIT);
125    }
126
127  return UUCONF_CMDTABRET_EXIT;
128}
129