1 /* $NetBSD: manconf.c,v 1.7 2013/07/18 15:39:08 christos Exp $ */
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * manconf.c: provides interface for reading man.conf files
35 * note that this code is shared across all programs that read man.conf.
36 * (currently: apropos, catman, makewhatis, man, and whatis...)
39 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
43 #include <sys/cdefs.h>
46 static char sccsid
[] = "@(#)config.c 8.8 (Berkeley) 1/31/95";
48 __RCSID("$NetBSD: manconf.c,v 1.7 2013/07/18 15:39:08 christos Exp $");
52 #include <sys/types.h>
53 #include <sys/queue.h>
63 #include "pathnames.h"
65 TAILQ_HEAD(_head
, _tag
);
66 static struct _head head
; /* 'head' -- top level data structure */
69 * xstrdup: like strdup, but also returns length of string in lenp
72 xstrdup(const char *str
, size_t *lenp
)
77 len
= strlen(str
) + 1;
81 (void)memcpy(copy
, str
, len
);
83 *lenp
= len
- 1; /* subtract out the null */
90 * Read the configuration file and build a doubly linked
91 * list off of "head" that looks like:
93 * tag1 <-> entry <-> entry <-> entry
95 * tag2 <-> entry <-> entry <-> entry
97 * note: will err/errx out on error (fopen or malloc failure)
100 config(const char *fname
)
109 fname
= _PATH_MANCONF
;
110 if ((cfp
= fopen(fname
, "r")) == NULL
)
111 err(EXIT_FAILURE
, "%s", fname
);
113 for (lcnt
= 1; (p
= fgetln(cfp
, &len
)) != NULL
; ++lcnt
) {
114 if (len
== 1) /* Skip empty lines. */
116 if (p
[len
- 1] != '\n') { /* Skip corrupted lines. */
117 warnx("%s: line %d corrupted", fname
, lcnt
);
120 p
[len
- 1] = '\0'; /* Terminate the line. */
122 /* Skip leading space. */
123 for (/*EMPTY*/; *p
!= '\0' && isspace((unsigned char)*p
); ++p
)
125 /* Skip empty/comment lines. */
126 if (*p
== '\0' || *p
== '#')
128 /* Find first token. */
129 for (t
= p
; *t
&& !isspace((unsigned char)*t
); ++t
)
131 if (*t
== '\0') /* Need more than one token.*/
137 errx(EXIT_FAILURE
, "gettag: malloc failed");
140 * Attach new records. Check to see if it is a
141 * section record or not.
144 if (*p
== '_') { /* not a section record */
146 * Special cases: _build and _crunch take the
147 * rest of the line as a single entry.
149 if (!strcmp(p
, "_build") || !strcmp(p
, "_crunch")) {
151 * The reason we're not just using
152 * strtok(3) for all of the parsing is
153 * so we don't get caught if a line
154 * has only a single token on it.
156 while (*++t
&& isspace((unsigned char)*t
));
157 if (addentry(tp
, t
, 0) == -1)
159 "addentry: malloc failed");
161 for (++t
; (p
= strtok(t
, " \t\n")) != NULL
;
163 if (addentry(tp
, p
, 0) == -1)
165 "addentry: malloc failed");
169 } else { /* section record */
172 * section entries can either be all absolute
173 * paths or all relative paths, but not both.
175 type
= (char)((TAILQ_FIRST(&tp
->entrylist
) != NULL
) ?
176 *(TAILQ_FIRST(&tp
->entrylist
)->s
) : '\0');
178 for (++t
; (p
= strtok(t
, " \t\n")) != NULL
; t
= NULL
) {
180 /* ensure an assigned type */
184 /* check for illegal mix */
186 warnx("section %s: %s: invalid entry, does not match previous types",
188 warnx("man.conf cannot mix absolute and relative paths in an entry");
191 if (addentry(tp
, p
, 0) == -1)
193 "addentry: malloc failed");
202 * if (!create) return tag for given name if it exists, or NULL otherwise
204 * if (create) return tag for given name if it exists, try and create
205 * a new tag if it does not exist. return NULL if unable to create new
209 gettag(const char *name
, int create
)
213 TAILQ_FOREACH(tp
, &head
, q
)
214 if (!strcmp(name
, tp
->s
))
219 /* try and add it in */
220 tp
= malloc(sizeof(*tp
));
222 tp
->s
= xstrdup(name
, &tp
->len
);
228 TAILQ_INIT(&tp
->entrylist
);
229 TAILQ_INSERT_TAIL(&head
, tp
, q
);
235 * add an entry to a list.
236 * returns -1 if malloc failed, otherwise 0.
239 addentry(TAG
*tp
, const char *newent
, int ishead
)
243 ep
= malloc(sizeof(*ep
));
245 ep
->s
= xstrdup(newent
, &ep
->len
);
252 TAILQ_INSERT_HEAD(&tp
->entrylist
, ep
, q
);
254 TAILQ_INSERT_TAIL(&tp
->entrylist
, ep
, q
);