Annotation of src/usr.bin/man/config.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: config.c,v 1.2 1996/06/26 05:36:59 deraadt 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.3 ! deraadt 41: static char rcsid[] = "$OpenBSD: config.c,v 1.2 1996/06/26 05:36:59 deraadt 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
1.3 ! deraadt 71: config(char *fname)
1.1 deraadt 72: {
73: TAG *tp;
74: ENTRY *ep;
75: FILE *cfp;
76: size_t len;
77: int lcnt;
78: char *p, *t;
79:
80: if (fname == NULL)
81: fname = _PATH_MANCONF;
82: if ((cfp = fopen(fname, "r")) == NULL)
83: err(1, "%s", fname);
84: TAILQ_INIT(&head);
85: for (lcnt = 1; (p = fgetln(cfp, &len)) != NULL; ++lcnt) {
86: if (len == 1) /* Skip empty lines. */
87: continue;
88: if (p[len - 1] != '\n') { /* Skip corrupted lines. */
89: warnx("%s: line %d corrupted", fname, lcnt);
90: continue;
91: }
92: p[len - 1] = '\0'; /* Terminate the line. */
93:
94: /* Skip leading space. */
95: for (; *p != '\0' && isspace(*p); ++p);
96: /* Skip empty/comment lines. */
97: if (*p == '\0' || *p == '#')
98: continue;
99: /* Find first token. */
100: for (t = p; *t && !isspace(*t); ++t);
101: if (*t == '\0') /* Need more than one token.*/
102: continue;
103: *t = '\0';
104:
105: for (tp = head.tqh_first; /* Find any matching tag. */
106: tp != NULL && strcmp(p, tp->s); tp = tp->q.tqe_next);
107:
108: if (tp == NULL) /* Create a new tag. */
109: tp = addlist(p);
110:
111: /*
112: * Attach new records. The keyword _build takes the rest of
113: * the line as a single entity, everything else is white
114: * space separated. The reason we're not just using strtok(3)
115: * for all of the parsing is so we don't get caught if a line
116: * has only a single token on it.
117: */
118: if (!strcmp(p, "_build")) {
119: while (*++t && isspace(*t));
120: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
121: (ep->s = strdup(t)) == NULL)
122: err(1, NULL);
123: TAILQ_INSERT_TAIL(&tp->list, ep, q);
124: } else for (++t; (p = strtok(t, " \t\n")) != NULL; t = NULL) {
125: if ((ep = malloc(sizeof(ENTRY))) == NULL ||
126: (ep->s = strdup(p)) == NULL)
127: err(1, NULL);
128: TAILQ_INSERT_TAIL(&tp->list, ep, q);
129: }
130: }
131:
132: fclose(cfp);
133: }
134:
135: /*
136: * addlist --
137: * Add a tag to the list.
138: */
139: TAG *
1.3 ! deraadt 140: addlist(char *name)
1.1 deraadt 141: {
142: TAG *tp;
143:
144: if ((tp = calloc(1, sizeof(TAG))) == NULL ||
145: (tp->s = strdup(name)) == NULL)
146: err(1, NULL);
147: TAILQ_INIT(&tp->list);
148: TAILQ_INSERT_TAIL(&head, tp, q);
149: return (tp);
150: }
151:
152: /*
153: * getlist --
154: * Return the linked list of entries for a tag if it exists.
155: */
156: TAG *
1.3 ! deraadt 157: getlist(char *name)
1.1 deraadt 158: {
159: TAG *tp;
160:
161: for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next)
162: if (!strcmp(name, tp->s))
163: return (tp);
164: return (NULL);
165: }
166:
167: void
1.3 ! deraadt 168: debug(char *l)
1.1 deraadt 169: {
170: TAG *tp;
171: ENTRY *ep;
172:
173: (void)printf("%s ===============\n", l);
174: for (tp = head.tqh_first; tp != NULL; tp = tp->q.tqe_next) {
175: printf("%s\n", tp->s);
176: for (ep = tp->list.tqh_first; ep != NULL; ep = ep->q.tqe_next)
177: printf("\t%s\n", ep->s);
178: }
179: }