]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.41 2020/02/10 14:42:10 schwarze Exp $ */
3 * Copyright (c) 2011,2014,2015,2017-2019 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 *);
35 static void manpath_add(struct manpaths
*, const char *, char);
36 static void manpath_parseline(struct manpaths
*, char *, char);
40 manconf_parse(struct manconf
*conf
, const char *file
,
41 char *defp
, char *auxp
)
45 /* Always prepend -m. */
46 manpath_parseline(&conf
->manpath
, auxp
, 'm');
48 /* If -M is given, it overrides everything else. */
50 manpath_parseline(&conf
->manpath
, defp
, 'M');
54 /* MANPATH and man.conf(5) cooperate. */
55 defp
= getenv("MANPATH");
59 /* No MANPATH; use man.conf(5) only. */
60 if (NULL
== defp
|| '\0' == defp
[0]) {
61 manconf_file(conf
, file
);
65 /* Prepend man.conf(5) to MANPATH. */
67 manconf_file(conf
, file
);
68 manpath_parseline(&conf
->manpath
, defp
, '\0');
72 /* Append man.conf(5) to MANPATH. */
73 if (':' == defp
[strlen(defp
) - 1]) {
74 manpath_parseline(&conf
->manpath
, defp
, '\0');
75 manconf_file(conf
, file
);
79 /* Insert man.conf(5) into MANPATH. */
80 insert
= strstr(defp
, "::");
83 manpath_parseline(&conf
->manpath
, defp
, '\0');
84 manconf_file(conf
, file
);
85 manpath_parseline(&conf
->manpath
, insert
+ 1, '\0');
89 /* MANPATH overrides man.conf(5) completely. */
90 manpath_parseline(&conf
->manpath
, defp
, '\0');
94 manpath_base(struct manpaths
*dirs
)
96 char path_base
[] = MANPATH_BASE
;
97 manpath_parseline(dirs
, path_base
, '\0');
101 * Parse a FULL pathname from a colon-separated list of arrays.
104 manpath_parseline(struct manpaths
*dirs
, char *path
, char option
)
111 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
112 manpath_add(dirs
, dir
, option
);
116 * Add a directory to the array, ignoring bad directories.
117 * Grow the array one-by-one for simplicity's sake.
120 manpath_add(struct manpaths
*dirs
, const char *dir
, char option
)
127 if ((cp
= realpath(dir
, buf
)) == NULL
)
130 for (i
= 0; i
< dirs
->sz
; i
++)
131 if (strcmp(dirs
->paths
[i
], dir
) == 0)
134 if (stat(cp
, &sb
) == -1)
137 dirs
->paths
= mandoc_reallocarray(dirs
->paths
,
138 dirs
->sz
+ 1, sizeof(*dirs
->paths
));
139 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
144 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
145 "-%c %s: %s", option
, dir
, strerror(errno
));
149 manconf_free(struct manconf
*conf
)
153 for (i
= 0; i
< conf
->manpath
.sz
; i
++)
154 free(conf
->manpath
.paths
[i
]);
156 free(conf
->manpath
.paths
);
157 free(conf
->output
.includes
);
158 free(conf
->output
.man
);
159 free(conf
->output
.paper
);
160 free(conf
->output
.style
);
164 manconf_file(struct manconf
*conf
, const char *file
)
166 const char *const toks
[] = { "manpath", "output" };
167 char manpath_default
[] = MANPATH_DEFAULT
;
170 char *line
, *cp
, *ep
;
171 size_t linesz
, tok
, toklen
;
174 if ((stream
= fopen(file
, "r")) == NULL
)
180 while ((linelen
= getline(&line
, &linesz
, stream
)) != -1) {
182 ep
= cp
+ linelen
- 1;
183 while (ep
> cp
&& isspace((unsigned char)*ep
))
185 while (isspace((unsigned char)*cp
))
187 if (cp
== ep
|| *cp
== '#')
190 for (tok
= 0; tok
< sizeof(toks
)/sizeof(toks
[0]); tok
++) {
191 toklen
= strlen(toks
[tok
]);
192 if (cp
+ toklen
< ep
&&
193 isspace((unsigned char)cp
[toklen
]) &&
194 strncmp(cp
, toks
[tok
], toklen
) == 0) {
196 while (isspace((unsigned char)*cp
))
203 case 0: /* manpath */
204 manpath_add(&conf
->manpath
, cp
, '\0');
205 *manpath_default
= '\0';
208 manconf_output(&conf
->output
, cp
, 1);
218 if (*manpath_default
!= '\0')
219 manpath_parseline(&conf
->manpath
, manpath_default
, '\0');
223 manconf_output(struct manoutput
*conf
, const char *cp
, int fromfile
)
225 const char *const toks
[] = {
226 "includes", "man", "paper", "style", "indent", "width",
227 "tag", "fragment", "mdoc", "noval", "toc"
229 const size_t ntoks
= sizeof(toks
) / sizeof(toks
[0]);
235 for (tok
= 0; tok
< ntoks
; tok
++) {
236 len
= strlen(toks
[tok
]);
237 if (strncmp(cp
, toks
[tok
], len
) == 0 &&
238 strchr(" = ", cp
[len
]) != NULL
) {
242 while (isspace((unsigned char)*cp
))
248 if (tok
< 6 && *cp
== '\0') {
249 mandoc_msg(MANDOCERR_BADVAL_MISS
, 0, 0, "-O %s=?", toks
[tok
]);
252 if (tok
> 6 && tok
< ntoks
&& *cp
!= '\0') {
253 mandoc_msg(MANDOCERR_BADVAL
, 0, 0, "-O %s=%s", toks
[tok
], cp
);
259 if (conf
->includes
!= NULL
) {
260 oldval
= mandoc_strdup(conf
->includes
);
263 conf
->includes
= mandoc_strdup(cp
);
266 if (conf
->man
!= NULL
) {
267 oldval
= mandoc_strdup(conf
->man
);
270 conf
->man
= mandoc_strdup(cp
);
273 if (conf
->paper
!= NULL
) {
274 oldval
= mandoc_strdup(conf
->paper
);
277 conf
->paper
= mandoc_strdup(cp
);
280 if (conf
->style
!= NULL
) {
281 oldval
= mandoc_strdup(conf
->style
);
284 conf
->style
= mandoc_strdup(cp
);
288 mandoc_asprintf(&oldval
, "%zu", conf
->indent
);
291 conf
->indent
= strtonum(cp
, 0, 1000, &errstr
);
294 mandoc_msg(MANDOCERR_BADVAL_BAD
, 0, 0,
295 "-O indent=%s is %s", cp
, errstr
);
299 mandoc_asprintf(&oldval
, "%zu", conf
->width
);
302 conf
->width
= strtonum(cp
, 1, 1000, &errstr
);
305 mandoc_msg(MANDOCERR_BADVAL_BAD
, 0, 0,
306 "-O width=%s is %s", cp
, errstr
);
309 if (conf
->tag
!= NULL
) {
310 oldval
= mandoc_strdup(conf
->tag
);
313 conf
->tag
= mandoc_strdup(cp
);
328 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-O %s", cp
);
335 mandoc_msg(MANDOCERR_BADVAL_DUPE
, 0, 0,
336 "-O %s=%s: already set to %s", toks
[tok
], cp
, oldval
);