xref: /netbsd-src/usr.bin/man/manconf.c (revision 20ec07777ab18ae8e85f5e606a6d5894a436c77c)
1*20ec0777Suwe /*	$NetBSD: manconf.c,v 1.8 2014/02/17 02:53:48 uwe Exp $	*/
27c96dd15Sthorpej 
37c96dd15Sthorpej /*
47c96dd15Sthorpej  * Copyright (c) 1989, 1993, 1995
57c96dd15Sthorpej  *	The Regents of the University of California.  All rights reserved.
67c96dd15Sthorpej  *
77c96dd15Sthorpej  * Redistribution and use in source and binary forms, with or without
87c96dd15Sthorpej  * modification, are permitted provided that the following conditions
97c96dd15Sthorpej  * are met:
107c96dd15Sthorpej  * 1. Redistributions of source code must retain the above copyright
117c96dd15Sthorpej  *    notice, this list of conditions and the following disclaimer.
127c96dd15Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
137c96dd15Sthorpej  *    notice, this list of conditions and the following disclaimer in the
147c96dd15Sthorpej  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
167c96dd15Sthorpej  *    may be used to endorse or promote products derived from this software
177c96dd15Sthorpej  *    without specific prior written permission.
187c96dd15Sthorpej  *
197c96dd15Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
207c96dd15Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217c96dd15Sthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227c96dd15Sthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237c96dd15Sthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247c96dd15Sthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257c96dd15Sthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267c96dd15Sthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277c96dd15Sthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287c96dd15Sthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297c96dd15Sthorpej  * SUCH DAMAGE.
307c96dd15Sthorpej  */
317c96dd15Sthorpej 
32d09fe2c4Schuck /*
33d09fe2c4Schuck  * manconf.c: provides interface for reading man.conf files
34d09fe2c4Schuck  *
35d09fe2c4Schuck  * note that this code is shared across all programs that read man.conf.
36d09fe2c4Schuck  * (currently: apropos, catman, makewhatis, man, and whatis...)
37d09fe2c4Schuck  */
38d09fe2c4Schuck 
39171d6532Slukem #if HAVE_NBTOOL_CONFIG_H
40171d6532Slukem #include "nbtool_config.h"
41b6f409eeSthorpej #endif
42b6f409eeSthorpej 
437c96dd15Sthorpej #include <sys/cdefs.h>
447c96dd15Sthorpej #ifndef lint
457c96dd15Sthorpej #if 0
467c96dd15Sthorpej static char sccsid[] = "@(#)config.c	8.8 (Berkeley) 1/31/95";
477c96dd15Sthorpej #else
48*20ec0777Suwe __RCSID("$NetBSD: manconf.c,v 1.8 2014/02/17 02:53:48 uwe Exp $");
497c96dd15Sthorpej #endif
507c96dd15Sthorpej #endif /* not lint */
517c96dd15Sthorpej 
527c96dd15Sthorpej #include <sys/types.h>
537c96dd15Sthorpej #include <sys/queue.h>
547c96dd15Sthorpej 
557c96dd15Sthorpej #include <ctype.h>
567c96dd15Sthorpej #include <err.h>
577c96dd15Sthorpej #include <errno.h>
587c96dd15Sthorpej #include <stdio.h>
597c96dd15Sthorpej #include <stdlib.h>
607c96dd15Sthorpej #include <string.h>
617c96dd15Sthorpej 
627c96dd15Sthorpej #include "manconf.h"
637c96dd15Sthorpej #include "pathnames.h"
647c96dd15Sthorpej 
65d09fe2c4Schuck TAILQ_HEAD(_head, _tag);
66d09fe2c4Schuck static struct _head head;	/* 'head' -- top level data structure */
67d09fe2c4Schuck 
68d09fe2c4Schuck /*
69d09fe2c4Schuck  * xstrdup: like strdup, but also returns length of string in lenp
70d09fe2c4Schuck  */
71d09fe2c4Schuck static char *
xstrdup(const char * str,size_t * lenp)72d09fe2c4Schuck xstrdup(const char *str, size_t *lenp)
73d09fe2c4Schuck {
74d09fe2c4Schuck 	size_t len;
75d09fe2c4Schuck 	char *copy;
76d09fe2c4Schuck 
77d09fe2c4Schuck 	len = strlen(str) + 1;
78d09fe2c4Schuck 	copy = malloc(len);
79d09fe2c4Schuck 	if (!copy)
806f6f89d2Schristos 		return NULL;
816f6f89d2Schristos 	(void)memcpy(copy, str, len);
82d09fe2c4Schuck 	if (lenp)
83d09fe2c4Schuck 		*lenp = len - 1;	/* subtract out the null */
846f6f89d2Schristos 	return copy;
85d09fe2c4Schuck }
867c96dd15Sthorpej 
877c96dd15Sthorpej /*
887c96dd15Sthorpej  * config --
897c96dd15Sthorpej  *
907c96dd15Sthorpej  * Read the configuration file and build a doubly linked
91d09fe2c4Schuck  * list off of "head" that looks like:
927c96dd15Sthorpej  *
93d09fe2c4Schuck  *	tag1 <-> entry <-> entry <-> entry
947c96dd15Sthorpej  *	|
95d09fe2c4Schuck  *	tag2 <-> entry <-> entry <-> entry
96d09fe2c4Schuck  *
97d09fe2c4Schuck  * note: will err/errx out on error (fopen or malloc failure)
987c96dd15Sthorpej  */
997c96dd15Sthorpej void
config(const char * fname)100d09fe2c4Schuck config(const char *fname)
1017c96dd15Sthorpej {
1027c96dd15Sthorpej 	TAG *tp;
1037c96dd15Sthorpej 	FILE *cfp;
1047c96dd15Sthorpej 	size_t len;
1057c96dd15Sthorpej 	int lcnt;
1067c96dd15Sthorpej 	char *p, *t, type;
1077c96dd15Sthorpej 
1087c96dd15Sthorpej 	if (fname == NULL)
1097c96dd15Sthorpej 		fname = _PATH_MANCONF;
1107c96dd15Sthorpej 	if ((cfp = fopen(fname, "r")) == NULL)
1116f6f89d2Schristos 		err(EXIT_FAILURE, "%s", fname);
1127c96dd15Sthorpej 	TAILQ_INIT(&head);
1137c96dd15Sthorpej 	for (lcnt = 1; (p = fgetln(cfp, &len)) != NULL; ++lcnt) {
1147c96dd15Sthorpej 		if (len == 1)			/* Skip empty lines. */
1157c96dd15Sthorpej 			continue;
1167c96dd15Sthorpej 		if (p[len - 1] != '\n') {	/* Skip corrupted lines. */
1177c96dd15Sthorpej 			warnx("%s: line %d corrupted", fname, lcnt);
1187c96dd15Sthorpej 			continue;
1197c96dd15Sthorpej 		}
1207c96dd15Sthorpej 		p[len - 1] = '\0';		/* Terminate the line. */
1217c96dd15Sthorpej 
1227c96dd15Sthorpej 						/* Skip leading space. */
1236f6f89d2Schristos 		for (/*EMPTY*/; *p != '\0' && isspace((unsigned char)*p); ++p)
1246f6f89d2Schristos 			continue;
1257c96dd15Sthorpej 						/* Skip empty/comment lines. */
1267c96dd15Sthorpej 		if (*p == '\0' || *p == '#')
1277c96dd15Sthorpej 			continue;
1287c96dd15Sthorpej 						/* Find first token. */
1296f6f89d2Schristos 		for (t = p; *t && !isspace((unsigned char)*t); ++t)
1306f6f89d2Schristos 			continue;
1317c96dd15Sthorpej 		if (*t == '\0')			/* Need more than one token.*/
1327c96dd15Sthorpej 			continue;
1337c96dd15Sthorpej 		*t = '\0';
1347c96dd15Sthorpej 
135d09fe2c4Schuck 		tp = gettag(p, 1);
136d09fe2c4Schuck 		if (!tp)
1376f6f89d2Schristos 			errx(EXIT_FAILURE, "gettag: malloc failed");
1387c96dd15Sthorpej 
1397c96dd15Sthorpej 		/*
1407c96dd15Sthorpej 		 * Attach new records. Check to see if it is a
1417c96dd15Sthorpej 		 * section record or not.
1427c96dd15Sthorpej 		 */
1437c96dd15Sthorpej 
1447c96dd15Sthorpej 		if (*p == '_') {		/* not a section record */
1457c96dd15Sthorpej 			/*
1467c96dd15Sthorpej 			 * Special cases: _build and _crunch take the
1477c96dd15Sthorpej 			 * rest of the line as a single entry.
1487c96dd15Sthorpej 			 */
1497c96dd15Sthorpej 			if (!strcmp(p, "_build") || !strcmp(p, "_crunch")) {
150*20ec0777Suwe 				const char *u;
151*20ec0777Suwe 
1527c96dd15Sthorpej 				/*
1537c96dd15Sthorpej 				 * The reason we're not just using
1547c96dd15Sthorpej 				 * strtok(3) for all of the parsing is
1557c96dd15Sthorpej 				 * so we don't get caught if a line
1567c96dd15Sthorpej 				 * has only a single token on it.
1577c96dd15Sthorpej 				 */
1587c96dd15Sthorpej 				while (*++t && isspace((unsigned char)*t));
159*20ec0777Suwe #ifndef HAVE_NBTOOL_CONFIG_H
160*20ec0777Suwe 				/* pre-verify user-supplied command format */
161*20ec0777Suwe 				u = t;
162*20ec0777Suwe 				while (*u && !isspace((unsigned char)*u))
163*20ec0777Suwe 					++u;
164*20ec0777Suwe 				while (*u && isspace((unsigned char)*u))
165*20ec0777Suwe 					++u;
166*20ec0777Suwe 				if (fmtcheck(u, "%s") != u) {
167*20ec0777Suwe 					warnx("%s:%d: invalid %s command ignored",
168*20ec0777Suwe 					      fname, lcnt, p);
169*20ec0777Suwe 					continue;
170*20ec0777Suwe 				}
171*20ec0777Suwe #endif	/* !HAVE_NBTOOL_CONFIG_H */
1726f6f89d2Schristos 				if (addentry(tp, t, 0) == -1)
1736f6f89d2Schristos 					errx(EXIT_FAILURE,
1746f6f89d2Schristos 					    "addentry: malloc failed");
1757c96dd15Sthorpej 			} else {
1767c96dd15Sthorpej 				for (++t; (p = strtok(t, " \t\n")) != NULL;
177d09fe2c4Schuck 				     t = NULL) {
1786f6f89d2Schristos 					if (addentry(tp, p, 0) == -1)
1796f6f89d2Schristos 						errx(EXIT_FAILURE,
180d09fe2c4Schuck 						   "addentry: malloc failed");
181d09fe2c4Schuck 				}
1827c96dd15Sthorpej 			}
1837c96dd15Sthorpej 
1847c96dd15Sthorpej 		} else {			/* section record */
1857c96dd15Sthorpej 
1867c96dd15Sthorpej 			/*
1877c96dd15Sthorpej 			 * section entries can either be all absolute
1887c96dd15Sthorpej 			 * paths or all relative paths, but not both.
1897c96dd15Sthorpej 			 */
190ef965bffSchristos 			type = (char)((TAILQ_FIRST(&tp->entrylist) != NULL) ?
191ef965bffSchristos 			    *(TAILQ_FIRST(&tp->entrylist)->s) : '\0');
1927c96dd15Sthorpej 
1937c96dd15Sthorpej 			for (++t; (p = strtok(t, " \t\n")) != NULL; t = NULL) {
1947c96dd15Sthorpej 
1957c96dd15Sthorpej 				/* ensure an assigned type */
1967c96dd15Sthorpej 				if (type == 0)
1977c96dd15Sthorpej 					type = *p;
1987c96dd15Sthorpej 
1997c96dd15Sthorpej 				/* check for illegal mix */
2007c96dd15Sthorpej 				if (*p != type) {
2017c96dd15Sthorpej 	warnx("section %s: %s: invalid entry, does not match previous types",
2027c96dd15Sthorpej 	      tp->s, p);
2037c96dd15Sthorpej 	warnx("man.conf cannot mix absolute and relative paths in an entry");
2047c96dd15Sthorpej 					continue;
2057c96dd15Sthorpej 				}
2066f6f89d2Schristos 				if (addentry(tp, p, 0) == -1)
2076f6f89d2Schristos 					errx(EXIT_FAILURE,
2086f6f89d2Schristos 					    "addentry: malloc failed");
2097c96dd15Sthorpej 			}
2107c96dd15Sthorpej 		}
2117c96dd15Sthorpej 	}
2126f6f89d2Schristos 	(void)fclose(cfp);
2137c96dd15Sthorpej }
2147c96dd15Sthorpej 
2157c96dd15Sthorpej /*
216d09fe2c4Schuck  * gettag --
217d09fe2c4Schuck  *	if (!create) return tag for given name if it exists, or NULL otherwise
218d09fe2c4Schuck  *
219d09fe2c4Schuck  *	if (create) return tag for given name if it exists, try and create
220d09fe2c4Schuck  *	a new tag if it does not exist.  return NULL if unable to create new
221d09fe2c4Schuck  *	tag.
2227c96dd15Sthorpej  */
2237c96dd15Sthorpej TAG *
gettag(const char * name,int create)224d09fe2c4Schuck gettag(const char *name, int create)
2257c96dd15Sthorpej {
2267c96dd15Sthorpej 	TAG *tp;
2277c96dd15Sthorpej 
2287c96dd15Sthorpej 	TAILQ_FOREACH(tp, &head, q)
2297c96dd15Sthorpej 		if (!strcmp(name, tp->s))
2306f6f89d2Schristos 			return tp;
231d09fe2c4Schuck 	if (!create)
2326f6f89d2Schristos 		return NULL;
233d09fe2c4Schuck 
234d09fe2c4Schuck 	/* try and add it in */
235d09fe2c4Schuck 	tp = malloc(sizeof(*tp));
236d09fe2c4Schuck 	if (tp)
237d09fe2c4Schuck 		tp->s = xstrdup(name, &tp->len);
238d09fe2c4Schuck 	if (!tp || !tp->s) {
239d09fe2c4Schuck 		if (tp)
240d09fe2c4Schuck 			free(tp);
2416f6f89d2Schristos 		return NULL;
242d09fe2c4Schuck 	}
243d09fe2c4Schuck 	TAILQ_INIT(&tp->entrylist);
2447c96dd15Sthorpej 	TAILQ_INSERT_TAIL(&head, tp, q);
2456f6f89d2Schristos 	return tp;
2467c96dd15Sthorpej }
2477c96dd15Sthorpej 
2487c96dd15Sthorpej /*
2497c96dd15Sthorpej  * addentry --
2507c96dd15Sthorpej  *	add an entry to a list.
251d09fe2c4Schuck  *	returns -1 if malloc failed, otherwise 0.
2527c96dd15Sthorpej  */
253d09fe2c4Schuck int
addentry(TAG * tp,const char * newent,int ishead)2546f6f89d2Schristos addentry(TAG *tp, const char *newent, int ishead)
2557c96dd15Sthorpej {
2567c96dd15Sthorpej 	ENTRY *ep;
2577c96dd15Sthorpej 
258d09fe2c4Schuck 	ep = malloc(sizeof(*ep));
259d09fe2c4Schuck 	if (ep)
260d09fe2c4Schuck 		ep->s = xstrdup(newent, &ep->len);
261d09fe2c4Schuck 	if (!ep || !ep->s) {
262d09fe2c4Schuck 		if (ep)
263d09fe2c4Schuck 			free(ep);
2646f6f89d2Schristos 		return -1;
265d09fe2c4Schuck 	}
2666f6f89d2Schristos 	if (ishead)
267d09fe2c4Schuck 		TAILQ_INSERT_HEAD(&tp->entrylist, ep, q);
2687c96dd15Sthorpej 	else
269d09fe2c4Schuck 		TAILQ_INSERT_TAIL(&tp->entrylist, ep, q);
2707c96dd15Sthorpej 
2716f6f89d2Schristos 	return 0;
2727c96dd15Sthorpej }
273