78ed81a3 |
1 | /* tinit.c |
2 | Initialize for reading Taylor UUCP configuration files. |
3 | |
4 | Copyright (C) 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | |
22 | The author of the program may be contacted at ian@airs.com or |
23 | c/o Infinity Development Systems, P.O. Box 520, Waltham, MA 02254. |
24 | */ |
25 | |
26 | #include "uucnfi.h" |
27 | |
28 | #if USE_RCS_ID |
29 | const char _uuconf_tinit_rcsid[] = "$Id: tinit.c,v 1.1 1993/08/04 19:35:14 jtc Exp $"; |
30 | #endif |
31 | |
32 | #include <errno.h> |
33 | \f |
34 | /* Local functions. */ |
35 | |
36 | static int itset_default P((struct sglobal *qglobal, char ***ppzvar, |
37 | const char *zfile)); |
38 | static int itadd P((pointer pglobal, int argc, char **argv, pointer pvar, |
39 | pointer pinfo)); |
40 | static int itunknown P((pointer pglobal, int argc, char **argv, pointer pvar, |
41 | pointer pinfo)); |
42 | static int itprogram P((pointer pglobal, int argc, char **argv, pointer pvar, |
43 | pointer pinfo)); |
44 | \f |
45 | static const struct cmdtab_offset asCmds[] = |
46 | { |
47 | { "nodename", UUCONF_CMDTABTYPE_STRING, |
48 | offsetof (struct sprocess, zlocalname), NULL }, |
49 | { "hostname", UUCONF_CMDTABTYPE_STRING, |
50 | offsetof (struct sprocess, zlocalname), NULL }, |
51 | { "uuname", UUCONF_CMDTABTYPE_STRING, |
52 | offsetof (struct sprocess, zlocalname), NULL }, |
53 | { "spool", UUCONF_CMDTABTYPE_STRING, |
54 | offsetof (struct sprocess, zspooldir), NULL }, |
55 | { "pubdir", UUCONF_CMDTABTYPE_STRING, |
56 | offsetof (struct sprocess, zpubdir), NULL }, |
57 | { "lockdir", UUCONF_CMDTABTYPE_STRING, |
58 | offsetof (struct sprocess, zlockdir), NULL }, |
59 | { "logfile", UUCONF_CMDTABTYPE_STRING, |
60 | offsetof (struct sprocess, zlogfile), NULL }, |
61 | { "statfile", UUCONF_CMDTABTYPE_STRING, |
62 | offsetof (struct sprocess, zstatsfile), NULL }, |
63 | { "debugfile", UUCONF_CMDTABTYPE_STRING, |
64 | offsetof (struct sprocess, zdebugfile), NULL }, |
65 | { "debug", UUCONF_CMDTABTYPE_STRING, |
66 | offsetof (struct sprocess, zdebug), NULL }, |
67 | { "max-uuxqts", UUCONF_CMDTABTYPE_INT, |
68 | offsetof (struct sprocess, cmaxuuxqts), NULL }, |
69 | { "sysfile", UUCONF_CMDTABTYPE_FN | 0, |
70 | offsetof (struct sprocess, pzsysfiles), itadd }, |
71 | { "portfile", UUCONF_CMDTABTYPE_FN | 0, |
72 | offsetof (struct sprocess, pzportfiles), itadd }, |
73 | { "dialfile", UUCONF_CMDTABTYPE_FN | 0, |
74 | offsetof (struct sprocess, pzdialfiles), itadd }, |
75 | { "dialcodefile", UUCONF_CMDTABTYPE_FN | 0, |
76 | offsetof (struct sprocess, pzdialcodefiles), itadd }, |
77 | { "callfile", UUCONF_CMDTABTYPE_FN | 0, |
78 | offsetof (struct sprocess, pzcallfiles), itadd }, |
79 | { "passwdfile", UUCONF_CMDTABTYPE_FN | 0, |
80 | offsetof (struct sprocess, pzpwdfiles), itadd }, |
81 | { "unknown", UUCONF_CMDTABTYPE_FN, offsetof (struct sprocess, qunknown), |
82 | itunknown }, |
83 | { "v2-files", UUCONF_CMDTABTYPE_BOOLEAN, |
84 | offsetof (struct sprocess, fv2), NULL }, |
85 | { "hdb-files", UUCONF_CMDTABTYPE_BOOLEAN, |
86 | offsetof (struct sprocess, fhdb), NULL }, |
87 | { "bnu-files", UUCONF_CMDTABTYPE_BOOLEAN, |
88 | offsetof (struct sprocess, fhdb), NULL }, |
89 | { "timetable", UUCONF_CMDTABTYPE_FN | 3, |
90 | offsetof (struct sprocess, pztimetables), _uuconf_itimetable }, |
91 | { NULL, 0, 0, NULL } |
92 | }; |
93 | |
94 | #define CCMDS (sizeof asCmds / sizeof asCmds[0]) |
95 | \f |
96 | /* This structure is used to pass information into the command table |
97 | functions. */ |
98 | |
99 | struct sinfo |
100 | { |
101 | /* The program name. */ |
102 | const char *zname; |
103 | /* A pointer to the command table being used, passed to isystem so |
104 | it can call uuconf_cmd_args. */ |
105 | struct uuconf_cmdtab *qcmds; |
106 | }; |
107 | \f |
108 | /* Initialize the routines which read the Taylor UUCP configuration |
109 | files. */ |
110 | |
111 | int |
112 | uuconf_taylor_init (ppglobal, zprogram, zname) |
113 | pointer *ppglobal; |
114 | const char *zprogram; |
115 | const char *zname; |
116 | { |
117 | struct sglobal **pqglobal = (struct sglobal **) ppglobal; |
118 | int iret; |
119 | char *zcopy; |
120 | struct sglobal *qglobal; |
121 | boolean fdefault; |
122 | FILE *e; |
123 | struct sinfo si; |
124 | |
125 | if (*pqglobal == NULL) |
126 | { |
127 | iret = _uuconf_iinit_global (pqglobal); |
128 | if (iret != UUCONF_SUCCESS) |
129 | return iret; |
130 | } |
131 | |
132 | qglobal = *pqglobal; |
133 | |
134 | if (zname != NULL) |
135 | { |
136 | size_t csize; |
137 | |
138 | csize = strlen (zname) + 1; |
139 | zcopy = uuconf_malloc (qglobal->pblock, csize); |
140 | if (zcopy == NULL) |
141 | { |
142 | qglobal->ierrno = errno; |
143 | return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO; |
144 | } |
145 | memcpy ((pointer) zcopy, (pointer) zname, csize); |
146 | fdefault = FALSE; |
147 | } |
148 | else |
149 | { |
150 | zcopy = uuconf_malloc (qglobal->pblock, |
151 | sizeof NEWCONFIGLIB + sizeof CONFIGFILE - 1); |
152 | if (zcopy == NULL) |
153 | { |
154 | qglobal->ierrno = errno; |
155 | return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO; |
156 | } |
157 | memcpy ((pointer) zcopy, (pointer) NEWCONFIGLIB, |
158 | sizeof NEWCONFIGLIB - 1); |
159 | memcpy ((pointer) (zcopy + sizeof NEWCONFIGLIB - 1), |
160 | (pointer) CONFIGFILE, sizeof CONFIGFILE); |
161 | fdefault = TRUE; |
162 | } |
163 | |
164 | qglobal->qprocess->zconfigfile = zcopy; |
165 | |
166 | e = fopen (zcopy, "r"); |
167 | if (e == NULL) |
168 | { |
169 | if (! fdefault) |
170 | { |
171 | qglobal->ierrno = errno; |
172 | qglobal->zfilename = zcopy; |
173 | return (UUCONF_FOPEN_FAILED |
174 | | UUCONF_ERROR_ERRNO |
175 | | UUCONF_ERROR_FILENAME); |
176 | } |
177 | |
178 | /* There is no config file, so just use the default values. */ |
179 | } |
180 | else |
181 | { |
182 | struct uuconf_cmdtab as[CCMDS]; |
183 | |
184 | _uuconf_ucmdtab_base (asCmds, CCMDS, (char *) qglobal->qprocess, |
185 | as); |
186 | |
187 | if (zprogram == NULL) |
188 | zprogram = "uucp"; |
189 | |
190 | si.zname = zprogram; |
191 | si.qcmds = as; |
192 | iret = uuconf_cmd_file (qglobal, e, as, (pointer) &si, itprogram, |
193 | UUCONF_CMDTABFLAG_BACKSLASH, |
194 | qglobal->pblock); |
195 | |
196 | (void) fclose (e); |
197 | |
198 | if (iret != UUCONF_SUCCESS) |
199 | { |
200 | qglobal->zfilename = zcopy; |
201 | return iret | UUCONF_ERROR_FILENAME; |
202 | } |
203 | } |
204 | |
205 | /* Get the defaults for the file names. */ |
206 | |
207 | iret = itset_default (qglobal, &qglobal->qprocess->pzsysfiles, SYSFILE); |
208 | if (iret != UUCONF_SUCCESS) |
209 | return iret; |
210 | iret = itset_default (qglobal, &qglobal->qprocess->pzportfiles, PORTFILE); |
211 | if (iret != UUCONF_SUCCESS) |
212 | return iret; |
213 | iret = itset_default (qglobal, &qglobal->qprocess->pzdialfiles, DIALFILE); |
214 | if (iret != UUCONF_SUCCESS) |
215 | return iret; |
216 | iret = itset_default (qglobal, &qglobal->qprocess->pzdialcodefiles, |
217 | DIALCODEFILE); |
218 | if (iret != UUCONF_SUCCESS) |
219 | return iret; |
220 | iret = itset_default (qglobal, &qglobal->qprocess->pzpwdfiles, PASSWDFILE); |
221 | if (iret != UUCONF_SUCCESS) |
222 | return iret; |
223 | iret = itset_default (qglobal, &qglobal->qprocess->pzcallfiles, CALLFILE); |
224 | if (iret != UUCONF_SUCCESS) |
225 | return iret; |
226 | |
227 | return UUCONF_SUCCESS; |
228 | } |
229 | \f |
230 | /* Add new strings to a variable. */ |
231 | |
232 | /*ARGSUSED*/ |
233 | static int |
234 | itadd (pglobal, argc, argv, pvar, pinfo) |
235 | pointer pglobal; |
236 | int argc; |
237 | char **argv; |
238 | pointer pvar; |
239 | pointer pinfo; |
240 | { |
241 | struct sglobal *qglobal = (struct sglobal *) pglobal; |
242 | char ***ppz = (char ***) pvar; |
243 | int i; |
244 | int iret; |
245 | |
246 | if (argc == 1) |
247 | { |
248 | iret = _uuconf_iadd_string (qglobal, NULL, FALSE, FALSE, ppz, |
249 | qglobal->pblock); |
250 | if (iret != UUCONF_SUCCESS) |
251 | return iret; |
252 | } |
253 | else |
254 | { |
255 | for (i = 1; i < argc; i++) |
256 | { |
257 | iret = _uuconf_iadd_string (qglobal, argv[i], TRUE, FALSE, ppz, |
258 | qglobal->pblock); |
259 | if (iret != UUCONF_SUCCESS) |
260 | return iret; |
261 | } |
262 | } |
263 | |
264 | return UUCONF_CMDTABRET_CONTINUE; |
265 | } |
266 | \f |
267 | /* Handle an "unknown" command. We accumulate this into a linked |
268 | list, and only parse them later in uuconf_unknown_system_info. */ |
269 | |
270 | /*ARGSUSED*/ |
271 | static int |
272 | itunknown (pglobal, argc, argv, pvar, pinfo) |
273 | pointer pglobal; |
274 | int argc; |
275 | char **argv; |
276 | pointer pvar; |
277 | pointer pinfo; |
278 | { |
279 | struct sglobal *qglobal = (struct sglobal *) pglobal; |
280 | struct sunknown **pq = (struct sunknown **) pvar; |
281 | struct sunknown *q; |
282 | |
283 | q = (struct sunknown *) uuconf_malloc (qglobal->pblock, |
284 | sizeof (struct sunknown)); |
285 | if (q == NULL) |
286 | { |
287 | qglobal->ierrno = errno; |
288 | return (UUCONF_MALLOC_FAILED |
289 | | UUCONF_ERROR_ERRNO |
290 | | UUCONF_CMDTABRET_EXIT); |
291 | } |
292 | q->qnext = NULL; |
293 | q->ilineno = qglobal->ilineno; |
294 | q->cargs = argc - 1; |
295 | q->pzargs = (char **) uuconf_malloc (qglobal->pblock, |
296 | (argc - 1) * sizeof (char *)); |
297 | if (q->pzargs == NULL) |
298 | { |
299 | qglobal->ierrno = errno; |
300 | return (UUCONF_MALLOC_FAILED |
301 | | UUCONF_ERROR_ERRNO |
302 | | UUCONF_CMDTABRET_EXIT); |
303 | } |
304 | memcpy ((pointer) q->pzargs, (pointer) (argv + 1), |
305 | (argc - 1) * sizeof (char *)); |
306 | |
307 | while (*pq != NULL) |
308 | pq = &(*pq)->qnext; |
309 | |
310 | *pq = q; |
311 | |
312 | return UUCONF_CMDTABRET_KEEP; |
313 | } |
314 | \f |
315 | /* If we encounter an unknown command, see if it is the program with |
316 | which we were invoked. If it was, pass the remaining arguments |
317 | back through the table. */ |
318 | |
319 | /*ARGSUSED*/ |
320 | static int |
321 | itprogram (pglobal, argc, argv, pvar, pinfo) |
322 | pointer pglobal; |
323 | int argc; |
324 | char **argv; |
325 | pointer pvar; |
326 | pointer pinfo; |
327 | { |
328 | struct sglobal *qglobal = (struct sglobal *) pglobal; |
329 | struct sinfo *qinfo = (struct sinfo *) pinfo; |
330 | |
331 | if (argc <= 1 |
332 | || strcasecmp (qinfo->zname, argv[0]) != 0) |
333 | return UUCONF_CMDTABRET_CONTINUE; |
334 | |
335 | return uuconf_cmd_args (pglobal, argc - 1, argv + 1, qinfo->qcmds, |
336 | (pointer) NULL, (uuconf_cmdtabfn) NULL, 0, |
337 | qglobal->pblock); |
338 | } |
339 | \f |
340 | /* If a filename was not set by the configuration file, add in the |
341 | default value. */ |
342 | |
343 | static int |
344 | itset_default (qglobal, ppzvar, zfile) |
345 | struct sglobal *qglobal; |
346 | char ***ppzvar; |
347 | const char *zfile; |
348 | { |
349 | size_t clen; |
350 | char *zadd; |
351 | |
352 | if (*ppzvar != NULL) |
353 | return UUCONF_SUCCESS; |
354 | |
355 | clen = strlen (zfile); |
356 | zadd = (char *) uuconf_malloc (qglobal->pblock, |
357 | sizeof NEWCONFIGLIB + clen); |
358 | if (zadd == NULL) |
359 | { |
360 | qglobal->ierrno = errno; |
361 | return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO; |
362 | } |
363 | |
364 | memcpy ((pointer) zadd, (pointer) NEWCONFIGLIB, sizeof NEWCONFIGLIB - 1); |
365 | memcpy ((pointer) (zadd + sizeof NEWCONFIGLIB - 1), (pointer) zfile, |
366 | clen + 1); |
367 | |
368 | return _uuconf_iadd_string (qglobal, zadd, FALSE, FALSE, ppzvar, |
369 | qglobal->pblock); |
370 | } |