]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.25 2015/05/07 12:08:13 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>
29 #include "mandoc_aux.h"
32 static void manconf_file(struct manconf
*, const char *);
33 static void manpath_add(struct manpaths
*, const char *, int);
34 static void manpath_parseline(struct manpaths
*, char *, int);
38 manconf_parse(struct manconf
*conf
, const char *file
,
39 char *defp
, char *auxp
)
42 char cmd
[(PATH_MAX
* 3) + 20];
47 strlcpy(cmd
, "manpath", sizeof(cmd
));
49 strlcat(cmd
, " -C ", sizeof(cmd
));
50 strlcat(cmd
, file
, sizeof(cmd
));
53 strlcat(cmd
, " -m ", sizeof(cmd
));
54 strlcat(cmd
, auxp
, sizeof(cmd
));
57 strlcat(cmd
, " -M ", sizeof(cmd
));
58 strlcat(cmd
, defp
, sizeof(cmd
));
61 /* Open manpath(1). Ignore errors. */
63 stream
= popen(cmd
, "r");
70 /* Read in as much output as we can. */
73 buf
= mandoc_realloc(buf
, bsz
+ 1024);
74 sz
= fread(buf
+ bsz
, 1, 1024, stream
);
78 if ( ! ferror(stream
) && feof(stream
) &&
79 bsz
&& '\n' == buf
[bsz
- 1]) {
81 manpath_parseline(&conf
->manpath
, buf
, 1);
89 /* Always prepend -m. */
90 manpath_parseline(&conf
->manpath
, auxp
, 1);
92 /* If -M is given, it overrides everything else. */
94 manpath_parseline(&conf
->manpath
, defp
, 1);
98 /* MANPATH and man.conf(5) cooperate. */
99 defp
= getenv("MANPATH");
101 file
= MAN_CONF_FILE
;
103 /* No MANPATH; use man.conf(5) only. */
104 if (NULL
== defp
|| '\0' == defp
[0]) {
105 manconf_file(conf
, file
);
109 /* Prepend man.conf(5) to MANPATH. */
110 if (':' == defp
[0]) {
111 manconf_file(conf
, file
);
112 manpath_parseline(&conf
->manpath
, defp
, 0);
116 /* Append man.conf(5) to MANPATH. */
117 if (':' == defp
[strlen(defp
) - 1]) {
118 manpath_parseline(&conf
->manpath
, defp
, 0);
119 manconf_file(conf
, file
);
123 /* Insert man.conf(5) into MANPATH. */
124 insert
= strstr(defp
, "::");
125 if (NULL
!= insert
) {
127 manpath_parseline(&conf
->manpath
, defp
, 0);
128 manconf_file(conf
, file
);
129 manpath_parseline(&conf
->manpath
, insert
+ 1, 0);
133 /* MANPATH overrides man.conf(5) completely. */
134 manpath_parseline(&conf
->manpath
, defp
, 0);
139 * Parse a FULL pathname from a colon-separated list of arrays.
142 manpath_parseline(struct manpaths
*dirs
, char *path
, int complain
)
149 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
150 manpath_add(dirs
, dir
, complain
);
154 * Add a directory to the array, ignoring bad directories.
155 * Grow the array one-by-one for simplicity's sake.
158 manpath_add(struct manpaths
*dirs
, const char *dir
, int complain
)
165 if (NULL
== (cp
= realpath(dir
, buf
))) {
167 fputs("manpath: ", stderr
);
173 for (i
= 0; i
< dirs
->sz
; i
++)
174 if (0 == strcmp(dirs
->paths
[i
], dir
))
177 if (stat(cp
, &sb
) == -1) {
179 fputs("manpath: ", stderr
);
185 dirs
->paths
= mandoc_reallocarray(dirs
->paths
,
186 dirs
->sz
+ 1, sizeof(char *));
188 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
192 manconf_free(struct manconf
*conf
)
196 for (i
= 0; i
< conf
->manpath
.sz
; i
++)
197 free(conf
->manpath
.paths
[i
]);
199 free(conf
->manpath
.paths
);
200 free(conf
->output
.includes
);
201 free(conf
->output
.man
);
202 free(conf
->output
.paper
);
203 free(conf
->output
.style
);
207 manconf_file(struct manconf
*conf
, const char *file
)
209 const char *const toks
[] = { "manpath", "output", "_whatdb" };
210 char manpath_default
[] = MANPATH_DEFAULT
;
216 if ((stream
= fopen(file
, "r")) == NULL
)
219 while ((cp
= fgetln(stream
, &len
)) != NULL
) {
224 while (isspace((unsigned char)*cp
))
229 for (tok
= 0; tok
< sizeof(toks
)/sizeof(toks
[0]); tok
++) {
230 len
= strlen(toks
[tok
]);
232 isspace((unsigned char)cp
[len
]) &&
233 !strncmp(cp
, toks
[tok
], len
)) {
235 while (isspace((unsigned char)*cp
))
242 case 2: /* _whatdb */
243 while (ep
> cp
&& ep
[-1] != '/')
249 case 0: /* manpath */
250 manpath_add(&conf
->manpath
, cp
, 0);
251 *manpath_default
= '\0';
254 manconf_output(&conf
->output
, cp
);
263 if (*manpath_default
!= '\0')
264 manpath_parseline(&conf
->manpath
, manpath_default
, 0);
268 manconf_output(struct manoutput
*conf
, const char *cp
)
270 const char *const toks
[] = {
271 "includes", "man", "paper", "style",
272 "indent", "width", "fragment", "mdoc"
277 for (tok
= 0; tok
< sizeof(toks
)/sizeof(toks
[0]); tok
++) {
278 len
= strlen(toks
[tok
]);
279 if ( ! strncmp(cp
, toks
[tok
], len
) &&
280 strchr(" = ", cp
[len
]) != NULL
) {
284 while (isspace((unsigned char)*cp
))
290 if (tok
< 6 && *cp
== '\0')
295 if (conf
->includes
== NULL
)
296 conf
->includes
= mandoc_strdup(cp
);
299 if (conf
->man
== NULL
)
300 conf
->man
= mandoc_strdup(cp
);
303 if (conf
->paper
== NULL
)
304 conf
->paper
= mandoc_strdup(cp
);
307 if (conf
->style
== NULL
)
308 conf
->style
= mandoc_strdup(cp
);
311 if (conf
->indent
== 0)
312 conf
->indent
= strtonum(cp
, 0, 1000, NULL
);
315 if (conf
->width
== 0)
316 conf
->width
= strtonum(cp
, 58, 1000, NULL
);