1 /* $Id: manpath.c,v 1.29 2015/11/07 17:58:55 schwarze Exp $ */
3 * Copyright (c) 2011, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
20 #include <sys/types.h>
32 #include "mandoc_aux.h"
36 static void manconf_file(struct manconf *, const char *);
38 static void manpath_add(struct manpaths *, const char *, int);
39 static void manpath_parseline(struct manpaths *, char *, int);
43 manconf_parse(struct manconf *conf, const char *file,
44 char *defp, char *auxp)
47 char cmd[(PATH_MAX * 3) + 20];
52 strlcpy(cmd, "manpath", sizeof(cmd));
54 strlcat(cmd, " -C ", sizeof(cmd));
55 strlcat(cmd, file, sizeof(cmd));
58 strlcat(cmd, " -m ", sizeof(cmd));
59 strlcat(cmd, auxp, sizeof(cmd));
62 strlcat(cmd, " -M ", sizeof(cmd));
63 strlcat(cmd, defp, sizeof(cmd));
66 /* Open manpath(1). Ignore errors. */
68 stream = popen(cmd, "r");
75 /* Read in as much output as we can. */
78 buf = mandoc_realloc(buf, bsz + 1024);
79 sz = fread(buf + bsz, 1, 1024, stream);
83 if ( ! ferror(stream) && feof(stream) &&
84 bsz && '\n' == buf[bsz - 1]) {
86 manpath_parseline(&conf->manpath, buf, 1);
94 /* Always prepend -m. */
95 manpath_parseline(&conf->manpath, auxp, 1);
97 /* If -M is given, it overrides everything else. */
99 manpath_parseline(&conf->manpath, defp, 1);
103 /* MANPATH and man.conf(5) cooperate. */
104 defp = getenv("MANPATH");
106 file = MAN_CONF_FILE;
108 /* No MANPATH; use man.conf(5) only. */
109 if (NULL == defp || '\0' == defp[0]) {
110 manconf_file(conf, file);
114 /* Prepend man.conf(5) to MANPATH. */
115 if (':' == defp[0]) {
116 manconf_file(conf, file);
117 manpath_parseline(&conf->manpath, defp, 0);
121 /* Append man.conf(5) to MANPATH. */
122 if (':' == defp[strlen(defp) - 1]) {
123 manpath_parseline(&conf->manpath, defp, 0);
124 manconf_file(conf, file);
128 /* Insert man.conf(5) into MANPATH. */
129 insert = strstr(defp, "::");
130 if (NULL != insert) {
132 manpath_parseline(&conf->manpath, defp, 0);
133 manconf_file(conf, file);
134 manpath_parseline(&conf->manpath, insert + 1, 0);
138 /* MANPATH overrides man.conf(5) completely. */
139 manpath_parseline(&conf->manpath, defp, 0);
144 * Parse a FULL pathname from a colon-separated list of arrays.
147 manpath_parseline(struct manpaths *dirs, char *path, int complain)
154 for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
155 manpath_add(dirs, dir, complain);
159 * Add a directory to the array, ignoring bad directories.
160 * Grow the array one-by-one for simplicity's sake.
163 manpath_add(struct manpaths *dirs, const char *dir, int complain)
170 if (NULL == (cp = realpath(dir, buf))) {
172 warn("manpath: %s", dir);
176 for (i = 0; i < dirs->sz; i++)
177 if (0 == strcmp(dirs->paths[i], dir))
180 if (stat(cp, &sb) == -1) {
182 warn("manpath: %s", dir);
186 dirs->paths = mandoc_reallocarray(dirs->paths,
187 dirs->sz + 1, sizeof(char *));
189 dirs->paths[dirs->sz++] = mandoc_strdup(cp);
193 manconf_free(struct manconf *conf)
197 for (i = 0; i < conf->manpath.sz; i++)
198 free(conf->manpath.paths[i]);
200 free(conf->manpath.paths);
201 free(conf->output.includes);
202 free(conf->output.man);
203 free(conf->output.paper);
204 free(conf->output.style);
209 manconf_file(struct manconf *conf, const char *file)
211 const char *const toks[] = { "manpath", "output", "_whatdb" };
212 char manpath_default[] = MANPATH_DEFAULT;
215 char *line, *cp, *ep;
216 size_t linesz, tok, toklen;
219 if ((stream = fopen(file, "r")) == NULL)
225 while ((linelen = getline(&line, &linesz, stream)) != -1) {
231 while (isspace((unsigned char)*cp))
236 for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
237 toklen = strlen(toks[tok]);
238 if (cp + toklen < ep &&
239 isspace((unsigned char)cp[toklen]) &&
240 strncmp(cp, toks[tok], toklen) == 0) {
242 while (isspace((unsigned char)*cp))
249 case 2: /* _whatdb */
250 while (ep > cp && ep[-1] != '/')
256 case 0: /* manpath */
257 manpath_add(&conf->manpath, cp, 0);
258 *manpath_default = '\0';
261 manconf_output(&conf->output, cp);
271 if (*manpath_default != '\0')
272 manpath_parseline(&conf->manpath, manpath_default, 0);
277 manconf_output(struct manoutput *conf, const char *cp)
279 const char *const toks[] = {
280 "includes", "man", "paper", "style",
281 "indent", "width", "fragment", "mdoc"
286 for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
287 len = strlen(toks[tok]);
288 if ( ! strncmp(cp, toks[tok], len) &&
289 strchr(" = ", cp[len]) != NULL) {
293 while (isspace((unsigned char)*cp))
299 if (tok < 6 && *cp == '\0')
304 if (conf->includes == NULL)
305 conf->includes = mandoc_strdup(cp);
308 if (conf->man == NULL)
309 conf->man = mandoc_strdup(cp);
312 if (conf->paper == NULL)
313 conf->paper = mandoc_strdup(cp);
316 if (conf->style == NULL)
317 conf->style = mandoc_strdup(cp);
320 if (conf->indent == 0)
321 conf->indent = strtonum(cp, 0, 1000, NULL);
324 if (conf->width == 0)
325 conf->width = strtonum(cp, 58, 1000, NULL);