Annotation of src/usr.bin/man/config.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD: config.c,v 1.7 1995/09/28 06:05:21 tls Exp $ */
1.1 deraadt 2: /* $NetBSD: config.c,v 1.7 1995/09/28 06:05:21 tls Exp $ */
3:
4: /*
5: * Copyright (c) 1989, 1993, 1995
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)config.c 8.8 (Berkeley) 1/31/95";
40: #else
1.2 ! deraadt 41: static char rcsid[] = "$OpenBSD: config.c,v 1.7 1995/09/28 06:05:21 tls Exp $";
1.1 deraadt 42: #endif
43: #endif /* not lint */
44:
45: #include <sys/types.h>
46: #include <sys/queue.h>
47:
48: #include <ctype.h>
49: #include <err.h>
50: #include <errno.h>
51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <string.h>
54:
55: #include "config.h"
56: #include "pathnames.h"
57:
58: struct _head head;
59:
60: /*
61: * config --
62: *
63: * Read the configuration file and build a doubly linked
64: * list that looks like:
65: *
66: * tag1 <-> record <-> record <-> record
67: * |
68: * tag2 <-> record <-> record <-> record
69: */
70: void
71: config(fname)
72: char *fname;
73: {
74: TAG *tp;
75: ENTRY *ep;
76: FILE *cfp;
77: size_t len;
78: int lcnt;
79: char *p, *t;
80:
81: if (fname == NULL)
82: fname = _PATH_MANCONF;
83: if ((cfp = fopen(fname, "r")) == NULL)
84: err(1, "%s", fname);
85: TAILQ_INIT(&head);
86: for (lcnt = 1; (p = fgetln(cfp, &len)) != NULL; ++lcnt) {
87: if (len == 1) /* Skip empty lines. */
88: continue;
89: if (p[len - 1] != '\n') { /* Skip corrupted lines. */
90: warnx("%s: line %d corrupted", fname, lcnt);
91: continue;
92: }
93: p[len - 1] = '\0'; /* Terminate the line. */
94:
95: /* Skip leading space. */
96: for (; *p != '\0' && isspace(*p); ++p);
97: /* Skip empty/comment lines. */
98: if (*p == '\0' || *p == '#')
99: continue;
100: /* Find first token. */
101: for (t = p; *t && !isspace(*t); ++t);
102: if (*t == '\0') /* Need more than one token.*/
103: continue;
104: *t = '\0';
105:
106: for (tp = head.tqh_first; /* Find any matching tag. */
107: tp != NULL && strcmp(p, tp->s); tp = tp->q.tqe_next);
108:
109: if (tp == NULL) /* Create a new tag. */
110: tp = addlist(p);
111:
112: /*
113: * Attach new records. The keyword _build takes the rest of
114: * the line as a single entity, everything else is white
115: * space separated. The reason we're not just using strtok(3)
116: * for all of the parsing is so we don't get caught if a line
117: * has only a single token on it.
118: */
119: if (!strcmp(p, "_build")) {
120: while (*++t && isspace(*t));
121: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
122: (ep->s = strdup(t)) == NULL)
123: err(1, NULL);
124: TAILQ_INSERT_TAIL(&tp->list, ep, q);
125: } else for (++t; (p = strtok(t, " \t\n")) != NULL; t = NULL) {
126: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
127: (ep->s = strdup(p)) == NULL)
128: err(1, NULL);
129: TAILQ_INSERT_TAIL(&tp->list, ep, q);
130: }
131: }
132:
133: fclose(cfp);
134: }
135:
136: /*
137: * addlist --
138: * Add a tag to the list.
139: */
140: TAG *
141: addlist(name)
142: char *name;
143: {
144: TAG *tp;
145:
146: if ((tp = calloc(1, sizeof(TAG))) == NULL ||
147: (tp->s = strdup(name)) == NULL)
148: err(1, NULL);
149: TAILQ_INIT(&tp->list);
150: TAILQ_INSERT_TAIL(&head, tp, q);
151: return (tp);
152: }
153:
154: /*
155: * getlist --
156: * Return the linked list of entries for a tag if it exists.
157: */
158: TAG *
159: getlist(name)
160: char *name;
161: {
162: TAG *tp;
163:
164: for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next)
165: if (!strcmp(name, tp->s))
166: return (tp);
167: return (NULL);
168: }
169:
170: void
171: debug(l)
172: char *l;
173: {
174: TAG *tp;
175: ENTRY *ep;
176:
177: (void)printf("%s ===============\n", l);
178: for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next) {
179: printf("%s\n", tp->s);
180: for (ep = tp->list.tqh_first; ep != NULL; ep = ep->q.tqe_next)
181: printf("\t%s\n", ep->s);
182: }
183: }