Commit | Line | Data |
---|---|---|
1ef31675 | 1 | /* |
03bd0b3c | 2 | * Copyright (c) 1989, 1993, 1995 |
00186a84 | 3 | * The Regents of the University of California. All rights reserved. |
1ef31675 | 4 | * |
ad787160 C |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
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. | |
20 | * | |
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 | |
31 | * SUCH DAMAGE. | |
1ef31675 KB |
32 | */ |
33 | ||
34 | #ifndef lint | |
fd88f5c5 | 35 | static char sccsid[] = "@(#)config.c 8.8 (Berkeley) 1/31/95"; |
1ef31675 KB |
36 | #endif /* not lint */ |
37 | ||
da0c0e56 KB |
38 | #include <sys/types.h> |
39 | #include <sys/queue.h> | |
40 | ||
41 | #include <ctype.h> | |
42 | #include <err.h> | |
1ef31675 | 43 | #include <errno.h> |
da0c0e56 | 44 | #include <stdio.h> |
011519cd | 45 | #include <stdlib.h> |
da0c0e56 | 46 | #include <string.h> |
011519cd | 47 | |
da0c0e56 KB |
48 | #include "config.h" |
49 | #include "pathnames.h" | |
1ef31675 | 50 | |
c46750de | 51 | struct _head head; |
011519cd | 52 | |
1ef31675 | 53 | /* |
da0c0e56 KB |
54 | * config -- |
55 | * | |
56 | * Read the configuration file and build a doubly linked | |
57 | * list that looks like: | |
58 | * | |
59 | * tag1 <-> record <-> record <-> record | |
60 | * | | |
61 | * tag2 <-> record <-> record <-> record | |
1ef31675 | 62 | */ |
da0c0e56 | 63 | void |
7d9403e1 KB |
64 | config(fname) |
65 | char *fname; | |
1ef31675 | 66 | { |
c46750de KB |
67 | TAG *tp; |
68 | ENTRY *ep; | |
da0c0e56 | 69 | FILE *cfp; |
011519cd | 70 | size_t len; |
da0c0e56 KB |
71 | int lcnt; |
72 | char *p, *t; | |
73 | ||
7d9403e1 KB |
74 | if (fname == NULL) |
75 | fname = _PATH_MANCONF; | |
76 | if ((cfp = fopen(fname, "r")) == NULL) | |
77 | err(1, "%s", fname); | |
c46750de | 78 | TAILQ_INIT(&head); |
10c2c2ed | 79 | for (lcnt = 1; (p = fgetln(cfp, &len)) != NULL; ++lcnt) { |
97231dd4 | 80 | if (len == 1) /* Skip empty lines. */ |
da0c0e56 KB |
81 | continue; |
82 | if (p[len - 1] != '\n') { /* Skip corrupted lines. */ | |
7d9403e1 | 83 | warnx("%s: line %d corrupted", fname, lcnt); |
da0c0e56 | 84 | continue; |
011519cd | 85 | } |
da0c0e56 KB |
86 | p[len - 1] = '\0'; /* Terminate the line. */ |
87 | ||
88 | /* Skip leading space. */ | |
89 | for (; *p != '\0' && isspace(*p); ++p); | |
90 | /* Skip empty/comment lines. */ | |
91 | if (*p == '\0' || *p == '#') | |
011519cd | 92 | continue; |
da0c0e56 KB |
93 | /* Find first token. */ |
94 | for (t = p; *t && !isspace(*t); ++t); | |
95 | if (*t == '\0') /* Need more than one token.*/ | |
011519cd | 96 | continue; |
da0c0e56 KB |
97 | *t = '\0'; |
98 | ||
c46750de KB |
99 | for (tp = head.tqh_first; /* Find any matching tag. */ |
100 | tp != NULL && strcmp(p, tp->s); tp = tp->q.tqe_next); | |
da0c0e56 | 101 | |
c46750de KB |
102 | if (tp == NULL) /* Create a new tag. */ |
103 | tp = addlist(p); | |
da0c0e56 KB |
104 | |
105 | /* | |
106 | * Attach new records. The keyword _build takes the rest of | |
107 | * the line as a single entity, everything else is white | |
108 | * space separated. The reason we're not just using strtok(3) | |
109 | * for all of the parsing is so we don't get caught if a line | |
110 | * has only a single token on it. | |
111 | */ | |
112 | if (!strcmp(p, "_build")) { | |
113 | while (*++t && isspace(*t)); | |
114 | if ((ep = malloc(sizeof(ENTRY))) == NULL || | |
115 | (ep->s = strdup(t)) == NULL) | |
116 | err(1, NULL); | |
c46750de | 117 | TAILQ_INSERT_TAIL(&tp->list, ep, q); |
18cef530 | 118 | } else for (++t; (p = strtok(t, " \t\n")) != NULL; t = NULL) { |
da0c0e56 KB |
119 | if ((ep = malloc(sizeof(ENTRY))) == NULL || |
120 | (ep->s = strdup(p)) == NULL) | |
121 | err(1, NULL); | |
c46750de | 122 | TAILQ_INSERT_TAIL(&tp->list, ep, q); |
011519cd KB |
123 | } |
124 | } | |
03bd0b3c JSP |
125 | |
126 | fclose(cfp); | |
011519cd | 127 | } |
1ef31675 | 128 | |
da0c0e56 KB |
129 | /* |
130 | * addlist -- | |
131 | * Add a tag to the list. | |
132 | */ | |
c46750de | 133 | TAG * |
da0c0e56 KB |
134 | addlist(name) |
135 | char *name; | |
011519cd | 136 | { |
c46750de | 137 | TAG *tp; |
da0c0e56 | 138 | |
c46750de KB |
139 | if ((tp = calloc(1, sizeof(TAG))) == NULL || |
140 | (tp->s = strdup(name)) == NULL) | |
da0c0e56 | 141 | err(1, NULL); |
c46750de KB |
142 | TAILQ_INIT(&tp->list); |
143 | TAILQ_INSERT_TAIL(&head, tp, q); | |
144 | return (tp); | |
011519cd KB |
145 | } |
146 | ||
da0c0e56 KB |
147 | /* |
148 | * getlist -- | |
c46750de | 149 | * Return the linked list of entries for a tag if it exists. |
da0c0e56 | 150 | */ |
c46750de | 151 | TAG * |
da0c0e56 KB |
152 | getlist(name) |
153 | char *name; | |
011519cd | 154 | { |
c46750de | 155 | TAG *tp; |
011519cd | 156 | |
c46750de KB |
157 | for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next) |
158 | if (!strcmp(name, tp->s)) | |
159 | return (tp); | |
da0c0e56 | 160 | return (NULL); |
1ef31675 KB |
161 | } |
162 | ||
da0c0e56 KB |
163 | void |
164 | debug(l) | |
165 | char *l; | |
011519cd | 166 | { |
c46750de KB |
167 | TAG *tp; |
168 | ENTRY *ep; | |
011519cd | 169 | |
da0c0e56 | 170 | (void)printf("%s ===============\n", l); |
c46750de KB |
171 | for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next) { |
172 | printf("%s\n", tp->s); | |
173 | for (ep = tp->list.tqh_first; ep != NULL; ep = ep->q.tqe_next) | |
da0c0e56 | 174 | printf("\t%s\n", ep->s); |
011519cd | 175 | } |
1ef31675 | 176 | } |