]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.21 2015/03/22 18:14:30 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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>
30 #include "mandoc_aux.h"
33 #define MAN_CONF_FILE "/etc/man.conf"
34 #define MAN_CONF_KEY "_whatdb"
36 static void manpath_add(struct manpaths
*, const char *, int);
37 static void manpath_parseline(struct manpaths
*, char *, int);
40 manpath_parse(struct manpaths
*dirs
, const char *file
,
41 char *defp
, char *auxp
)
44 char cmd
[(PATH_MAX
* 3) + 20];
49 strlcpy(cmd
, "manpath", sizeof(cmd
));
51 strlcat(cmd
, " -C ", sizeof(cmd
));
52 strlcat(cmd
, file
, sizeof(cmd
));
55 strlcat(cmd
, " -m ", sizeof(cmd
));
56 strlcat(cmd
, auxp
, sizeof(cmd
));
59 strlcat(cmd
, " -M ", sizeof(cmd
));
60 strlcat(cmd
, defp
, sizeof(cmd
));
63 /* Open manpath(1). Ignore errors. */
65 stream
= popen(cmd
, "r");
72 /* Read in as much output as we can. */
75 buf
= mandoc_realloc(buf
, bsz
+ 1024);
76 sz
= fread(buf
+ bsz
, 1, 1024, stream
);
80 if ( ! ferror(stream
) && feof(stream
) &&
81 bsz
&& '\n' == buf
[bsz
- 1]) {
83 manpath_parseline(dirs
, buf
, 1);
89 char manpath_default
[] = MANPATH_DEFAULT
;
92 /* Always prepend -m. */
93 manpath_parseline(dirs
, auxp
, 1);
95 /* If -M is given, it overrides everything else. */
97 manpath_parseline(dirs
, defp
, 1);
101 /* MANPATH and man.conf(5) cooperate. */
102 defp
= getenv("MANPATH");
104 file
= MAN_CONF_FILE
;
106 /* No MANPATH; use man.conf(5) only. */
107 if (NULL
== defp
|| '\0' == defp
[0]) {
108 manpath_manconf(dirs
, file
);
110 manpath_parseline(dirs
, manpath_default
, 0);
114 /* Prepend man.conf(5) to MANPATH. */
115 if (':' == defp
[0]) {
116 manpath_manconf(dirs
, file
);
117 manpath_parseline(dirs
, defp
, 0);
121 /* Append man.conf(5) to MANPATH. */
122 if (':' == defp
[strlen(defp
) - 1]) {
123 manpath_parseline(dirs
, defp
, 0);
124 manpath_manconf(dirs
, file
);
128 /* Insert man.conf(5) into MANPATH. */
129 insert
= strstr(defp
, "::");
130 if (NULL
!= insert
) {
132 manpath_parseline(dirs
, defp
, 0);
133 manpath_manconf(dirs
, file
);
134 manpath_parseline(dirs
, insert
+ 1, 0);
138 /* MANPATH overrides man.conf(5) completely. */
139 manpath_parseline(dirs
, 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 fputs("manpath: ", stderr
);
178 for (i
= 0; i
< dirs
->sz
; i
++)
179 if (0 == strcmp(dirs
->paths
[i
], dir
))
182 if (stat(cp
, &sb
) == -1) {
184 fputs("manpath: ", stderr
);
190 dirs
->paths
= mandoc_reallocarray(dirs
->paths
,
191 dirs
->sz
+ 1, sizeof(char *));
193 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
197 manpath_free(struct manpaths
*p
)
201 for (i
= 0; i
< p
->sz
; i
++)
208 manpath_manconf(struct manpaths
*dirs
, const char *file
)
214 keysz
= strlen(MAN_CONF_KEY
);
217 if (NULL
== (stream
= fopen(file
, "r")))
220 while (NULL
!= (p
= fgetln(stream
, &len
))) {
221 if (0 == len
|| '\n' != p
[--len
])
224 while (isspace((unsigned char)*p
))
226 if (strncmp(MAN_CONF_KEY
, p
, keysz
))
229 while (isspace((unsigned char)*p
))
233 if (NULL
== (q
= strrchr(p
, '/')))
236 manpath_add(dirs
, p
, 0);