xref: /openbsd-src/usr.bin/mandoc/manpath.c (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1 /*	$OpenBSD: manpath.c,v 1.29 2020/07/21 15:08:49 schwarze Exp $ */
2 /*
3  * Copyright (c) 2011,2014,2015,2017-2019 Ingo Schwarze <schwarze@openbsd.org>
4  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "mandoc_aux.h"
29 #include "mandoc.h"
30 #include "manconf.h"
31 
32 #define MAN_CONF_FILE	"/etc/man.conf"
33 #define MANPATH_BASE	"/usr/share/man:/usr/X11R6/man"
34 #define MANPATH_DEFAULT	"/usr/share/man:/usr/X11R6/man:/usr/local/man"
35 
36 static	void	 manconf_file(struct manconf *, const char *);
37 static	void	 manpath_add(struct manpaths *, const char *, char);
38 static	void	 manpath_parseline(struct manpaths *, char *, char);
39 
40 
41 void
42 manconf_parse(struct manconf *conf, const char *file,
43 		char *defp, char *auxp)
44 {
45 	char		*insert;
46 
47 	/* Always prepend -m. */
48 	manpath_parseline(&conf->manpath, auxp, 'm');
49 
50 	/* If -M is given, it overrides everything else. */
51 	if (NULL != defp) {
52 		manpath_parseline(&conf->manpath, defp, 'M');
53 		return;
54 	}
55 
56 	/* MANPATH and man.conf(5) cooperate. */
57 	defp = getenv("MANPATH");
58 	if (NULL == file)
59 		file = MAN_CONF_FILE;
60 
61 	/* No MANPATH; use man.conf(5) only. */
62 	if (NULL == defp || '\0' == defp[0]) {
63 		manconf_file(conf, file);
64 		return;
65 	}
66 
67 	/* Prepend man.conf(5) to MANPATH. */
68 	if (':' == defp[0]) {
69 		manconf_file(conf, file);
70 		manpath_parseline(&conf->manpath, defp, '\0');
71 		return;
72 	}
73 
74 	/* Append man.conf(5) to MANPATH. */
75 	if (':' == defp[strlen(defp) - 1]) {
76 		manpath_parseline(&conf->manpath, defp, '\0');
77 		manconf_file(conf, file);
78 		return;
79 	}
80 
81 	/* Insert man.conf(5) into MANPATH. */
82 	insert = strstr(defp, "::");
83 	if (NULL != insert) {
84 		*insert++ = '\0';
85 		manpath_parseline(&conf->manpath, defp, '\0');
86 		manconf_file(conf, file);
87 		manpath_parseline(&conf->manpath, insert + 1, '\0');
88 		return;
89 	}
90 
91 	/* MANPATH overrides man.conf(5) completely. */
92 	manpath_parseline(&conf->manpath, defp, '\0');
93 }
94 
95 void
96 manpath_base(struct manpaths *dirs)
97 {
98 	char path_base[] = MANPATH_BASE;
99 	manpath_parseline(dirs, path_base, '\0');
100 }
101 
102 /*
103  * Parse a FULL pathname from a colon-separated list of arrays.
104  */
105 static void
106 manpath_parseline(struct manpaths *dirs, char *path, char option)
107 {
108 	char	*dir;
109 
110 	if (NULL == path)
111 		return;
112 
113 	for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
114 		manpath_add(dirs, dir, option);
115 }
116 
117 /*
118  * Add a directory to the array, ignoring bad directories.
119  * Grow the array one-by-one for simplicity's sake.
120  */
121 static void
122 manpath_add(struct manpaths *dirs, const char *dir, char option)
123 {
124 	char		 buf[PATH_MAX];
125 	struct stat	 sb;
126 	char		*cp;
127 	size_t		 i;
128 
129 	if ((cp = realpath(dir, buf)) == NULL)
130 		goto fail;
131 
132 	for (i = 0; i < dirs->sz; i++)
133 		if (strcmp(dirs->paths[i], dir) == 0)
134 			return;
135 
136 	if (stat(cp, &sb) == -1)
137 		goto fail;
138 
139 	dirs->paths = mandoc_reallocarray(dirs->paths,
140 	    dirs->sz + 1, sizeof(*dirs->paths));
141 	dirs->paths[dirs->sz++] = mandoc_strdup(cp);
142 	return;
143 
144 fail:
145 	if (option != '\0')
146 		mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0,
147 		    "-%c %s: %s", option, dir, strerror(errno));
148 }
149 
150 void
151 manconf_free(struct manconf *conf)
152 {
153 	size_t		 i;
154 
155 	for (i = 0; i < conf->manpath.sz; i++)
156 		free(conf->manpath.paths[i]);
157 
158 	free(conf->manpath.paths);
159 	free(conf->output.includes);
160 	free(conf->output.man);
161 	free(conf->output.paper);
162 	free(conf->output.style);
163 }
164 
165 static void
166 manconf_file(struct manconf *conf, const char *file)
167 {
168 	const char *const toks[] = { "manpath", "output" };
169 	char manpath_default[] = MANPATH_DEFAULT;
170 
171 	FILE		*stream;
172 	char		*line, *cp, *ep;
173 	size_t		 linesz, tok, toklen;
174 	ssize_t		 linelen;
175 
176 	if ((stream = fopen(file, "r")) == NULL)
177 		goto out;
178 
179 	line = NULL;
180 	linesz = 0;
181 
182 	while ((linelen = getline(&line, &linesz, stream)) != -1) {
183 		cp = line;
184 		ep = cp + linelen - 1;
185 		while (ep > cp && isspace((unsigned char)*ep))
186 			*ep-- = '\0';
187 		while (isspace((unsigned char)*cp))
188 			cp++;
189 		if (cp == ep || *cp == '#')
190 			continue;
191 
192 		for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
193 			toklen = strlen(toks[tok]);
194 			if (cp + toklen < ep &&
195 			    isspace((unsigned char)cp[toklen]) &&
196 			    strncmp(cp, toks[tok], toklen) == 0) {
197 				cp += toklen;
198 				while (isspace((unsigned char)*cp))
199 					cp++;
200 				break;
201 			}
202 		}
203 
204 		switch (tok) {
205 		case 0:  /* manpath */
206 			manpath_add(&conf->manpath, cp, '\0');
207 			*manpath_default = '\0';
208 			break;
209 		case 1:  /* output */
210 			manconf_output(&conf->output, cp, 1);
211 			break;
212 		default:
213 			break;
214 		}
215 	}
216 	free(line);
217 	fclose(stream);
218 
219 out:
220 	if (*manpath_default != '\0')
221 		manpath_parseline(&conf->manpath, manpath_default, '\0');
222 }
223 
224 int
225 manconf_output(struct manoutput *conf, const char *cp, int fromfile)
226 {
227 	const char *const toks[] = {
228 	    "includes", "man", "paper", "style", "indent", "width",
229 	    "tag", "outfilename", "tagfilename",
230 	    "fragment", "mdoc", "noval", "toc"
231 	};
232 	const size_t ntoks = sizeof(toks) / sizeof(toks[0]);
233 
234 	const char	*errstr;
235 	char		*oldval;
236 	size_t		 len, tok;
237 
238 	for (tok = 0; tok < ntoks; tok++) {
239 		len = strlen(toks[tok]);
240 		if (strncmp(cp, toks[tok], len) == 0 &&
241 		    strchr(" =	", cp[len]) != NULL) {
242 			cp += len;
243 			if (*cp == '=')
244 				cp++;
245 			while (isspace((unsigned char)*cp))
246 				cp++;
247 			break;
248 		}
249 	}
250 
251 	if (tok < 8 && *cp == '\0') {
252 		mandoc_msg(MANDOCERR_BADVAL_MISS, 0, 0, "-O %s=?", toks[tok]);
253 		return -1;
254 	}
255 	if (tok > 8 && tok < ntoks && *cp != '\0') {
256 		mandoc_msg(MANDOCERR_BADVAL, 0, 0, "-O %s=%s", toks[tok], cp);
257 		return -1;
258 	}
259 
260 	switch (tok) {
261 	case 0:
262 		if (conf->includes != NULL) {
263 			oldval = mandoc_strdup(conf->includes);
264 			break;
265 		}
266 		conf->includes = mandoc_strdup(cp);
267 		return 0;
268 	case 1:
269 		if (conf->man != NULL) {
270 			oldval = mandoc_strdup(conf->man);
271 			break;
272 		}
273 		conf->man = mandoc_strdup(cp);
274 		return 0;
275 	case 2:
276 		if (conf->paper != NULL) {
277 			oldval = mandoc_strdup(conf->paper);
278 			break;
279 		}
280 		conf->paper = mandoc_strdup(cp);
281 		return 0;
282 	case 3:
283 		if (conf->style != NULL) {
284 			oldval = mandoc_strdup(conf->style);
285 			break;
286 		}
287 		conf->style = mandoc_strdup(cp);
288 		return 0;
289 	case 4:
290 		if (conf->indent) {
291 			mandoc_asprintf(&oldval, "%zu", conf->indent);
292 			break;
293 		}
294 		conf->indent = strtonum(cp, 0, 1000, &errstr);
295 		if (errstr == NULL)
296 			return 0;
297 		mandoc_msg(MANDOCERR_BADVAL_BAD, 0, 0,
298 		    "-O indent=%s is %s", cp, errstr);
299 		return -1;
300 	case 5:
301 		if (conf->width) {
302 			mandoc_asprintf(&oldval, "%zu", conf->width);
303 			break;
304 		}
305 		conf->width = strtonum(cp, 1, 1000, &errstr);
306 		if (errstr == NULL)
307 			return 0;
308 		mandoc_msg(MANDOCERR_BADVAL_BAD, 0, 0,
309 		    "-O width=%s is %s", cp, errstr);
310 		return -1;
311 	case 6:
312 		if (conf->tag != NULL) {
313 			oldval = mandoc_strdup(conf->tag);
314 			break;
315 		}
316 		conf->tag = mandoc_strdup(cp);
317 		return 0;
318 	case 7:
319 		if (conf->outfilename != NULL) {
320 			oldval = mandoc_strdup(conf->outfilename);
321 			break;
322 		}
323 		conf->outfilename = mandoc_strdup(cp);
324 		return 0;
325 	case 8:
326 		if (conf->tagfilename != NULL) {
327 			oldval = mandoc_strdup(conf->tagfilename);
328 			break;
329 		}
330 		conf->tagfilename = mandoc_strdup(cp);
331 		return 0;
332 	case 9:
333 		conf->fragment = 1;
334 		return 0;
335 	case 10:
336 		conf->mdoc = 1;
337 		return 0;
338 	case 11:
339 		conf->noval = 1;
340 		return 0;
341 	case 12:
342 		conf->toc = 1;
343 		return 0;
344 	default:
345 		mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-O %s", cp);
346 		return -1;
347 	}
348 	if (fromfile) {
349 		free(oldval);
350 		return 0;
351 	} else {
352 		mandoc_msg(MANDOCERR_BADVAL_DUPE, 0, 0,
353 		    "-O %s=%s: already set to %s", toks[tok], cp, oldval);
354 		free(oldval);
355 		return -1;
356 	}
357 }
358