2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software donated to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)conf.c 8.2 (Berkeley) 3/27/94
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/syslog.h>
52 #define ALLOC(ty) (xmalloc(sizeof(ty)))
54 typedef struct path path;
56 qelem p_q; /* 2-way linked list */
57 int p_lno; /* Line number of this record */
58 char *p_args; /* copy of arg string (malloc) */
59 char *p_key; /* Pathname to match (also p_argv[0]) */
60 regex_t p_rx; /* RE to match against pathname () */
61 int p_rxvalid; /* non-zero if valid regular expression */
62 int p_argc; /* number of elements in arg string */
63 char **p_argv; /* argv[] pointers into arg string (malloc) */
66 static char *conf_file; /* XXX for regerror */
67 static path *curp; /* XXX for regerror */
70 * Add an element to a 2-way list,
73 static void ins_que(elem, pred)
76 qelem *p = pred->q_forw;
84 * Remove an element from a 2-way list
86 static void rem_que(elem)
89 qelem *p = elem->q_forw;
90 qelem *p2 = elem->q_back;
96 * Error checking malloc
98 static void *xmalloc(siz)
101 void *p = malloc(siz);
104 syslog(LOG_ALERT, "malloc: failed to get %d bytes", siz);
109 * Insert the path in the list.
110 * If there is already an element with the same key then
111 * the *second* one is ignored (return 0). If the key is
112 * not found then the path is added to the end of the list
115 static int pinsert(p0, q0)
124 for (q = q0->q_forw; q != q0; q = q->q_forw) {
125 path *p = (path *) q;
126 if (strcmp(p->p_key, p0->p_key) == 0)
129 ins_que(&p0->p_q, q0->q_back);
134 static path *palloc(cline, lno)
145 * Implement comment chars
147 s = strchr(cline, '#');
152 * Do a pass through the string to count the number
157 for (s = key; s != NULL; ) {
159 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
171 * Now do another pass and generate a new path structure
175 p->p_argv = xmalloc(c * sizeof(char *));
176 p->p_args = strdup(cline);
178 for (s = p->p_args; s != NULL; ) {
180 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
190 for (c = 0; c < p->p_argc; c++)
191 printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
194 p->p_key = p->p_argv[0];
195 if (strpbrk(p->p_key, RE_CHARS)) {
199 val = regcomp(&p->p_rx, p->p_key, REG_EXTENDED | REG_NOSUB);
201 char errbuf[_POSIX2_LINE_MAX];
202 regerror(val, &p->p_rx, errbuf, sizeof errbuf);
203 syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
204 conf_file, curp->p_lno, curp->p_key, errbuf);
220 * Free a path structure
229 free((char *) p->p_argv);
234 * Discard all currently held path structures on q0.
235 * and add all the ones on xq.
237 static void preplace(q0, xq)
242 * While the list is not empty,
243 * take the first element off the list
246 while (q0->q_forw != q0) {
247 qelem *q = q0->q_forw;
251 while (xq->q_forw != xq) {
252 qelem *q = xq->q_forw;
259 * Read the lines from the configuration file and
260 * add them to the list of paths.
262 static void readfp(q0, fp)
266 char cline[LINE_MAX];
271 * Make a new empty list.
273 q.q_forw = q.q_back = &q;
276 * Read the lines from the configuration file.
278 while (fgets(cline, sizeof(cline), fp)) {
279 path *p = palloc(cline, nread+1);
280 if (p && !pinsert(p, &q))
286 * If some records were read, then throw
287 * away the old list and replace with the
295 * Read the configuration file (conf) and replace
296 * the existing path list with the new version.
297 * If the file is not readable, then no changes take place
299 void conf_read(q, conf)
303 FILE *fp = fopen(conf, "r");
305 conf_file = conf; /* XXX */
307 conf_file = 0; /* XXX */
310 syslog(LOG_ERR, "open config file \"%s\": %s", conf, strerror(errno));
315 char **conf_match(q0, key)
321 for (q = q0->q_forw; q != q0; q = q->q_forw) {
322 path *p = (path *) q;
324 if (!regexec(&p->p_rx, key, 0, 0, 0)) {
325 return p->p_argv + 1;
328 if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
329 return (p->p_argv+1);