]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.27 2015/10/11 21:12: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>
30 #include "mandoc_aux.h"
34 static void manconf_file(struct manconf
*, const char *);
36 static void manpath_add(struct manpaths
*, const char *, int);
37 static void manpath_parseline(struct manpaths
*, char *, int);
41 manconf_parse(struct manconf
*conf
, const char *file
,
42 char *defp
, char *auxp
)
45 char cmd
[(PATH_MAX
* 3) + 20];
50 strlcpy(cmd
, "manpath", sizeof(cmd
));
52 strlcat(cmd
, " -C ", sizeof(cmd
));
53 strlcat(cmd
, file
, sizeof(cmd
));
56 strlcat(cmd
, " -m ", sizeof(cmd
));
57 strlcat(cmd
, auxp
, sizeof(cmd
));
60 strlcat(cmd
, " -M ", sizeof(cmd
));
61 strlcat(cmd
, defp
, sizeof(cmd
));
64 /* Open manpath(1). Ignore errors. */
66 stream
= popen(cmd
, "r");
73 /* Read in as much output as we can. */
76 buf
= mandoc_realloc(buf
, bsz
+ 1024);
77 sz
= fread(buf
+ bsz
, 1, 1024, stream
);
81 if ( ! ferror(stream
) && feof(stream
) &&
82 bsz
&& '\n' == buf
[bsz
- 1]) {
84 manpath_parseline(&conf
->manpath
, buf
, 1);
92 /* Always prepend -m. */
93 manpath_parseline(&conf
->manpath
, auxp
, 1);
95 /* If -M is given, it overrides everything else. */
97 manpath_parseline(&conf
->manpath
, 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 manconf_file(conf
, file
);
112 /* Prepend man.conf(5) to MANPATH. */
113 if (':' == defp
[0]) {
114 manconf_file(conf
, file
);
115 manpath_parseline(&conf
->manpath
, defp
, 0);
119 /* Append man.conf(5) to MANPATH. */
120 if (':' == defp
[strlen(defp
) - 1]) {
121 manpath_parseline(&conf
->manpath
, defp
, 0);
122 manconf_file(conf
, file
);
126 /* Insert man.conf(5) into MANPATH. */
127 insert
= strstr(defp
, "::");
128 if (NULL
!= insert
) {
130 manpath_parseline(&conf
->manpath
, defp
, 0);
131 manconf_file(conf
, file
);
132 manpath_parseline(&conf
->manpath
, insert
+ 1, 0);
136 /* MANPATH overrides man.conf(5) completely. */
137 manpath_parseline(&conf
->manpath
, defp
, 0);
142 * Parse a FULL pathname from a colon-separated list of arrays.
145 manpath_parseline(struct manpaths
*dirs
, char *path
, int complain
)
152 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
153 manpath_add(dirs
, dir
, complain
);
157 * Add a directory to the array, ignoring bad directories.
158 * Grow the array one-by-one for simplicity's sake.
161 manpath_add(struct manpaths
*dirs
, const char *dir
, int complain
)
168 if (NULL
== (cp
= realpath(dir
, buf
))) {
170 warn("manpath: %s", dir
);
174 for (i
= 0; i
< dirs
->sz
; i
++)
175 if (0 == strcmp(dirs
->paths
[i
], dir
))
178 if (stat(cp
, &sb
) == -1) {
180 warn("manpath: %s", dir
);
184 dirs
->paths
= mandoc_reallocarray(dirs
->paths
,
185 dirs
->sz
+ 1, sizeof(char *));
187 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
191 manconf_free(struct manconf
*conf
)
195 for (i
= 0; i
< conf
->manpath
.sz
; i
++)
196 free(conf
->manpath
.paths
[i
]);
198 free(conf
->manpath
.paths
);
199 free(conf
->output
.includes
);
200 free(conf
->output
.man
);
201 free(conf
->output
.paper
);
202 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);
269 manconf_output(struct manoutput
*conf
, const char *cp
)
271 const char *const toks
[] = {
272 "includes", "man", "paper", "style",
273 "indent", "width", "fragment", "mdoc"
278 for (tok
= 0; tok
< sizeof(toks
)/sizeof(toks
[0]); tok
++) {
279 len
= strlen(toks
[tok
]);
280 if ( ! strncmp(cp
, toks
[tok
], len
) &&
281 strchr(" = ", cp
[len
]) != NULL
) {
285 while (isspace((unsigned char)*cp
))
291 if (tok
< 6 && *cp
== '\0')
296 if (conf
->includes
== NULL
)
297 conf
->includes
= mandoc_strdup(cp
);
300 if (conf
->man
== NULL
)
301 conf
->man
= mandoc_strdup(cp
);
304 if (conf
->paper
== NULL
)
305 conf
->paper
= mandoc_strdup(cp
);
308 if (conf
->style
== NULL
)
309 conf
->style
= mandoc_strdup(cp
);
312 if (conf
->indent
== 0)
313 conf
->indent
= strtonum(cp
, 0, 1000, NULL
);
316 if (conf
->width
== 0)
317 conf
->width
= strtonum(cp
, 58, 1000, NULL
);