]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.36 2018/10/02 14:56:47 schwarze Exp $ */
3 * Copyright (c) 2011,2014,2015,2017,2018 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>
32 #include "mandoc_aux.h"
35 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
)
46 /* Always prepend -m. */
47 manpath_parseline(&conf
->manpath
, auxp
, 1);
49 /* If -M is given, it overrides everything else. */
51 manpath_parseline(&conf
->manpath
, defp
, 1);
55 /* MANPATH and man.conf(5) cooperate. */
56 defp
= getenv("MANPATH");
60 /* No MANPATH; use man.conf(5) only. */
61 if (NULL
== defp
|| '\0' == defp
[0]) {
62 manconf_file(conf
, file
);
66 /* Prepend man.conf(5) to MANPATH. */
68 manconf_file(conf
, file
);
69 manpath_parseline(&conf
->manpath
, defp
, 0);
73 /* Append man.conf(5) to MANPATH. */
74 if (':' == defp
[strlen(defp
) - 1]) {
75 manpath_parseline(&conf
->manpath
, defp
, 0);
76 manconf_file(conf
, file
);
80 /* Insert man.conf(5) into MANPATH. */
81 insert
= strstr(defp
, "::");
84 manpath_parseline(&conf
->manpath
, defp
, 0);
85 manconf_file(conf
, file
);
86 manpath_parseline(&conf
->manpath
, insert
+ 1, 0);
90 /* MANPATH overrides man.conf(5) completely. */
91 manpath_parseline(&conf
->manpath
, defp
, 0);
95 manpath_base(struct manpaths
*dirs
)
97 char path_base
[] = MANPATH_BASE
;
98 manpath_parseline(dirs
, path_base
, 0);
102 * Parse a FULL pathname from a colon-separated list of arrays.
105 manpath_parseline(struct manpaths
*dirs
, char *path
, int complain
)
112 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
113 manpath_add(dirs
, dir
, complain
);
117 * Add a directory to the array, ignoring bad directories.
118 * Grow the array one-by-one for simplicity's sake.
121 manpath_add(struct manpaths
*dirs
, const char *dir
, int complain
)
128 if (NULL
== (cp
= realpath(dir
, buf
))) {
130 warn("manpath: %s", dir
);
134 for (i
= 0; i
< dirs
->sz
; i
++)
135 if (0 == strcmp(dirs
->paths
[i
], dir
))
138 if (stat(cp
, &sb
) == -1) {
140 warn("manpath: %s", dir
);
144 dirs
->paths
= mandoc_reallocarray(dirs
->paths
,
145 dirs
->sz
+ 1, sizeof(char *));
147 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
151 manconf_free(struct manconf
*conf
)
155 for (i
= 0; i
< conf
->manpath
.sz
; i
++)
156 free(conf
->manpath
.paths
[i
]);
158 free(conf
->manpath
.paths
);
159 free(conf
->output
.includes
);
160 free(conf
->output
.man
);
161 free(conf
->output
.paper
);
162 free(conf
->output
.style
);
166 manconf_file(struct manconf
*conf
, const char *file
)
168 const char *const toks
[] = { "manpath", "output", "_whatdb" };
169 char manpath_default
[] = MANPATH_DEFAULT
;
172 char *line
, *cp
, *ep
;
173 size_t linesz
, tok
, toklen
;
176 if ((stream
= fopen(file
, "r")) == NULL
)
182 while ((linelen
= getline(&line
, &linesz
, stream
)) != -1) {
184 ep
= cp
+ linelen
- 1;
185 while (ep
> cp
&& isspace((unsigned char)*ep
))
187 while (isspace((unsigned char)*cp
))
189 if (cp
== ep
|| *cp
== '#')
192 for (tok
= 0; tok
< sizeof(toks
)/sizeof(toks
[0]); tok
++) {
193 toklen
= strlen(toks
[tok
]);
194 if (cp
+ toklen
< ep
&&
195 isspace((unsigned char)cp
[toklen
]) &&
196 strncmp(cp
, toks
[tok
], toklen
) == 0) {
198 while (isspace((unsigned char)*cp
))
205 case 2: /* _whatdb */
206 while (ep
> cp
&& ep
[-1] != '/')
212 case 0: /* manpath */
213 manpath_add(&conf
->manpath
, cp
, 0);
214 *manpath_default
= '\0';
217 manconf_output(&conf
->output
, cp
, 1);
227 if (*manpath_default
!= '\0')
228 manpath_parseline(&conf
->manpath
, manpath_default
, 0);
232 manconf_output(struct manoutput
*conf
, const char *cp
, int fromfile
)
234 const char *const toks
[] = {
235 "includes", "man", "paper", "style",
236 "indent", "width", "fragment", "mdoc", "noval", "toc"
243 for (tok
= 0; tok
< sizeof(toks
)/sizeof(toks
[0]); tok
++) {
244 len
= strlen(toks
[tok
]);
245 if ( ! strncmp(cp
, toks
[tok
], len
) &&
246 strchr(" = ", cp
[len
]) != NULL
) {
250 while (isspace((unsigned char)*cp
))
256 if (tok
< 6 && *cp
== '\0') {
257 warnx("-O %s=?: Missing argument value", toks
[tok
]);
260 if ((tok
== 6 || tok
== 7) && *cp
!= '\0') {
261 warnx("-O %s: Does not take a value: %s", toks
[tok
], cp
);
267 if (conf
->includes
!= NULL
) {
268 oldval
= mandoc_strdup(conf
->includes
);
271 conf
->includes
= mandoc_strdup(cp
);
274 if (conf
->man
!= NULL
) {
275 oldval
= mandoc_strdup(conf
->man
);
278 conf
->man
= mandoc_strdup(cp
);
281 if (conf
->paper
!= NULL
) {
282 oldval
= mandoc_strdup(conf
->paper
);
285 conf
->paper
= mandoc_strdup(cp
);
288 if (conf
->style
!= NULL
) {
289 oldval
= mandoc_strdup(conf
->style
);
292 conf
->style
= mandoc_strdup(cp
);
296 mandoc_asprintf(&oldval
, "%zu", conf
->indent
);
299 conf
->indent
= strtonum(cp
, 0, 1000, &errstr
);
302 warnx("-O indent=%s is %s", cp
, errstr
);
306 mandoc_asprintf(&oldval
, "%zu", conf
->width
);
309 conf
->width
= strtonum(cp
, 1, 1000, &errstr
);
312 warnx("-O width=%s is %s", cp
, errstr
);
328 warnx("-O %s: Bad argument", cp
);
332 warnx("-O %s=%s: Option already set to %s",
333 toks
[tok
], cp
, oldval
);