]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.44 2021/11/05 18:03:08 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 *, int);
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
, char *pend
, char *pbeg
)
42 int use_path_from_file
= 1;
44 /* Always prepend -m. */
45 manpath_parseline(&conf
->manpath
, pbeg
, 'm');
47 if (pend
!= NULL
&& *pend
!= '\0') {
48 /* If -M is given, it overrides everything else. */
49 manpath_parseline(&conf
->manpath
, pend
, 'M');
50 use_path_from_file
= 0;
52 } else if ((pbeg
= getenv("MANPATH")) == NULL
|| *pbeg
== '\0') {
53 /* No MANPATH; use man.conf(5) only. */
55 } else if (*pbeg
== ':') {
56 /* Prepend man.conf(5) to MANPATH. */
59 } else if ((pend
= strstr(pbeg
, "::")) != NULL
) {
60 /* Insert man.conf(5) into MANPATH. */
63 } else if (pbeg
[strlen(pbeg
) - 1] == ':') {
64 /* Append man.conf(5) to MANPATH. */
67 /* MANPATH overrides man.conf(5) completely. */
68 use_path_from_file
= 0;
72 manpath_parseline(&conf
->manpath
, pbeg
, '\0');
76 manconf_file(conf
, file
, use_path_from_file
);
78 manpath_parseline(&conf
->manpath
, pend
, '\0');
82 manpath_base(struct manpaths
*dirs
)
84 char path_base
[] = MANPATH_BASE
;
85 manpath_parseline(dirs
, path_base
, '\0');
89 * Parse a FULL pathname from a colon-separated list of arrays.
92 manpath_parseline(struct manpaths
*dirs
, char *path
, char option
)
99 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
100 manpath_add(dirs
, dir
, option
);
104 * Add a directory to the array, ignoring bad directories.
105 * Grow the array one-by-one for simplicity's sake.
108 manpath_add(struct manpaths
*dirs
, const char *dir
, char option
)
115 if ((cp
= realpath(dir
, buf
)) == NULL
)
118 for (i
= 0; i
< dirs
->sz
; i
++)
119 if (strcmp(dirs
->paths
[i
], dir
) == 0)
122 if (stat(cp
, &sb
) == -1)
125 dirs
->paths
= mandoc_reallocarray(dirs
->paths
,
126 dirs
->sz
+ 1, sizeof(*dirs
->paths
));
127 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
132 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
133 "-%c %s: %s", option
, dir
, strerror(errno
));
137 manconf_free(struct manconf
*conf
)
141 for (i
= 0; i
< conf
->manpath
.sz
; i
++)
142 free(conf
->manpath
.paths
[i
]);
144 free(conf
->manpath
.paths
);
145 free(conf
->output
.includes
);
146 free(conf
->output
.man
);
147 free(conf
->output
.paper
);
148 free(conf
->output
.style
);
152 manconf_file(struct manconf
*conf
, const char *file
, int use_path_from_file
)
154 const char *const toks
[] = { "manpath", "output" };
155 char manpath_default
[] = MANPATH_DEFAULT
;
158 char *line
, *cp
, *ep
;
159 size_t linesz
, tok
, toklen
;
162 if ((stream
= fopen(file
, "r")) == NULL
)
168 while ((linelen
= getline(&line
, &linesz
, stream
)) != -1) {
170 ep
= cp
+ linelen
- 1;
171 while (ep
> cp
&& isspace((unsigned char)*ep
))
173 while (isspace((unsigned char)*cp
))
175 if (cp
== ep
|| *cp
== '#')
178 for (tok
= 0; tok
< sizeof(toks
)/sizeof(toks
[0]); tok
++) {
179 toklen
= strlen(toks
[tok
]);
180 if (cp
+ toklen
< ep
&&
181 isspace((unsigned char)cp
[toklen
]) &&
182 strncmp(cp
, toks
[tok
], toklen
) == 0) {
184 while (isspace((unsigned char)*cp
))
191 case 0: /* manpath */
192 if (use_path_from_file
)
193 manpath_add(&conf
->manpath
, cp
, '\0');
194 *manpath_default
= '\0';
197 manconf_output(&conf
->output
, cp
, 1);
207 if (use_path_from_file
&& *manpath_default
!= '\0')
208 manpath_parseline(&conf
->manpath
, manpath_default
, '\0');
212 manconf_output(struct manoutput
*conf
, const char *cp
, int fromfile
)
214 const char *const toks
[] = {
215 /* Tokens requiring an argument. */
216 "includes", "man", "paper", "style", "indent", "width",
217 "outfilename", "tagfilename",
218 /* Token taking an optional argument. */
220 /* Tokens not taking arguments. */
221 "fragment", "mdoc", "noval", "toc"
223 const size_t ntoks
= sizeof(toks
) / sizeof(toks
[0]);
229 for (tok
= 0; tok
< ntoks
; tok
++) {
230 len
= strlen(toks
[tok
]);
231 if (strncmp(cp
, toks
[tok
], len
) == 0 &&
232 strchr(" = ", cp
[len
]) != NULL
) {
236 while (isspace((unsigned char)*cp
))
242 if (tok
< 8 && *cp
== '\0') {
243 mandoc_msg(MANDOCERR_BADVAL_MISS
, 0, 0, "-O %s=?", toks
[tok
]);
246 if (tok
> 8 && tok
< ntoks
&& *cp
!= '\0') {
247 mandoc_msg(MANDOCERR_BADVAL
, 0, 0, "-O %s=%s", toks
[tok
], cp
);
253 if (conf
->includes
!= NULL
) {
254 oldval
= mandoc_strdup(conf
->includes
);
257 conf
->includes
= mandoc_strdup(cp
);
260 if (conf
->man
!= NULL
) {
261 oldval
= mandoc_strdup(conf
->man
);
264 conf
->man
= mandoc_strdup(cp
);
267 if (conf
->paper
!= NULL
) {
268 oldval
= mandoc_strdup(conf
->paper
);
271 conf
->paper
= mandoc_strdup(cp
);
274 if (conf
->style
!= NULL
) {
275 oldval
= mandoc_strdup(conf
->style
);
278 conf
->style
= mandoc_strdup(cp
);
282 mandoc_asprintf(&oldval
, "%zu", conf
->indent
);
285 conf
->indent
= strtonum(cp
, 0, 1000, &errstr
);
288 mandoc_msg(MANDOCERR_BADVAL_BAD
, 0, 0,
289 "-O indent=%s is %s", cp
, errstr
);
293 mandoc_asprintf(&oldval
, "%zu", conf
->width
);
296 conf
->width
= strtonum(cp
, 1, 1000, &errstr
);
299 mandoc_msg(MANDOCERR_BADVAL_BAD
, 0, 0,
300 "-O width=%s is %s", cp
, errstr
);
303 if (conf
->outfilename
!= NULL
) {
304 oldval
= mandoc_strdup(conf
->outfilename
);
307 conf
->outfilename
= mandoc_strdup(cp
);
310 if (conf
->tagfilename
!= NULL
) {
311 oldval
= mandoc_strdup(conf
->tagfilename
);
314 conf
->tagfilename
= mandoc_strdup(cp
);
317 * If the index of the following token changes,
318 * do not forget to adjust the range check above the switch.
321 if (conf
->tag
!= NULL
) {
322 oldval
= mandoc_strdup(conf
->tag
);
325 conf
->tag
= mandoc_strdup(cp
);
340 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-O %s", cp
);
347 mandoc_msg(MANDOCERR_BADVAL_DUPE
, 0, 0,
348 "-O %s=%s: already set to %s", toks
[tok
], cp
, oldval
);