8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / mandoc / manpath.c
blob83c329ec9bbb6b2b2473f4046fcc6d8a08c08422
1 /* $Id: manpath.c,v 1.30 2016/05/28 13:44:13 schwarze Exp $ */
2 /*
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.
18 #include "config.h"
20 #include <sys/types.h>
21 #include <sys/stat.h>
23 #include <ctype.h>
24 #if HAVE_ERR
25 #include <err.h>
26 #endif
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "mandoc_aux.h"
33 #include "manconf.h"
35 #if !HAVE_MANPATH
36 static void manconf_file(struct manconf *, const char *);
37 #endif
38 static void manpath_add(struct manpaths *, const char *, int);
39 static void manpath_parseline(struct manpaths *, char *, int);
42 void
43 manconf_parse(struct manconf *conf, const char *file,
44 char *defp, char *auxp)
46 #if HAVE_MANPATH
47 char cmd[(PATH_MAX * 3) + 20];
48 FILE *stream;
49 char *buf;
50 size_t sz, bsz;
52 strlcpy(cmd, "manpath", sizeof(cmd));
53 if (file) {
54 strlcat(cmd, " -C ", sizeof(cmd));
55 strlcat(cmd, file, sizeof(cmd));
57 if (auxp) {
58 strlcat(cmd, " -m ", sizeof(cmd));
59 strlcat(cmd, auxp, sizeof(cmd));
61 if (defp) {
62 strlcat(cmd, " -M ", sizeof(cmd));
63 strlcat(cmd, defp, sizeof(cmd));
66 /* Open manpath(1). Ignore errors. */
68 stream = popen(cmd, "r");
69 if (NULL == stream)
70 return;
72 buf = NULL;
73 bsz = 0;
75 /* Read in as much output as we can. */
77 do {
78 buf = mandoc_realloc(buf, bsz + 1024);
79 sz = fread(buf + bsz, 1, 1024, stream);
80 bsz += sz;
81 } while (sz > 0);
83 if ( ! ferror(stream) && feof(stream) &&
84 bsz && '\n' == buf[bsz - 1]) {
85 buf[bsz - 1] = '\0';
86 manpath_parseline(&conf->manpath, buf, 1);
89 free(buf);
90 pclose(stream);
91 #else
92 char *insert;
94 /* Always prepend -m. */
95 manpath_parseline(&conf->manpath, auxp, 1);
97 /* If -M is given, it overrides everything else. */
98 if (NULL != defp) {
99 manpath_parseline(&conf->manpath, defp, 1);
100 return;
103 /* MANPATH and man.conf(5) cooperate. */
104 defp = getenv("MANPATH");
105 if (NULL == file)
106 file = MAN_CONF_FILE;
108 /* No MANPATH; use man.conf(5) only. */
109 if (NULL == defp || '\0' == defp[0]) {
110 manconf_file(conf, file);
111 return;
114 /* Prepend man.conf(5) to MANPATH. */
115 if (':' == defp[0]) {
116 manconf_file(conf, file);
117 manpath_parseline(&conf->manpath, defp, 0);
118 return;
121 /* Append man.conf(5) to MANPATH. */
122 if (':' == defp[strlen(defp) - 1]) {
123 manpath_parseline(&conf->manpath, defp, 0);
124 manconf_file(conf, file);
125 return;
128 /* Insert man.conf(5) into MANPATH. */
129 insert = strstr(defp, "::");
130 if (NULL != insert) {
131 *insert++ = '\0';
132 manpath_parseline(&conf->manpath, defp, 0);
133 manconf_file(conf, file);
134 manpath_parseline(&conf->manpath, insert + 1, 0);
135 return;
138 /* MANPATH overrides man.conf(5) completely. */
139 manpath_parseline(&conf->manpath, defp, 0);
140 #endif
144 * Parse a FULL pathname from a colon-separated list of arrays.
146 static void
147 manpath_parseline(struct manpaths *dirs, char *path, int complain)
149 char *dir;
151 if (NULL == path)
152 return;
154 for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
155 manpath_add(dirs, dir, complain);
159 * Add a directory to the array, ignoring bad directories.
160 * Grow the array one-by-one for simplicity's sake.
162 static void
163 manpath_add(struct manpaths *dirs, const char *dir, int complain)
165 char buf[PATH_MAX];
166 struct stat sb;
167 char *cp;
168 size_t i;
170 if (NULL == (cp = realpath(dir, buf))) {
171 if (complain)
172 warn("manpath: %s", dir);
173 return;
176 for (i = 0; i < dirs->sz; i++)
177 if (0 == strcmp(dirs->paths[i], dir))
178 return;
180 if (stat(cp, &sb) == -1) {
181 if (complain)
182 warn("manpath: %s", dir);
183 return;
186 dirs->paths = mandoc_reallocarray(dirs->paths,
187 dirs->sz + 1, sizeof(char *));
189 dirs->paths[dirs->sz++] = mandoc_strdup(cp);
192 void
193 manconf_free(struct manconf *conf)
195 size_t i;
197 for (i = 0; i < conf->manpath.sz; i++)
198 free(conf->manpath.paths[i]);
200 free(conf->manpath.paths);
201 free(conf->output.includes);
202 free(conf->output.man);
203 free(conf->output.paper);
204 free(conf->output.style);
207 #if !HAVE_MANPATH
208 static void
209 manconf_file(struct manconf *conf, const char *file)
211 const char *const toks[] = { "manpath", "output", "_whatdb" };
212 char manpath_default[] = MANPATH_DEFAULT;
214 FILE *stream;
215 char *line, *cp, *ep;
216 size_t linesz, tok, toklen;
217 ssize_t linelen;
219 if ((stream = fopen(file, "r")) == NULL)
220 goto out;
222 line = NULL;
223 linesz = 0;
225 while ((linelen = getline(&line, &linesz, stream)) != -1) {
226 cp = line;
227 ep = cp + linelen - 1;
228 while (ep > cp && isspace((unsigned char)*ep))
229 *ep-- = '\0';
230 while (isspace((unsigned char)*cp))
231 cp++;
232 if (cp == ep || *cp == '#')
233 continue;
235 for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
236 toklen = strlen(toks[tok]);
237 if (cp + toklen < ep &&
238 isspace((unsigned char)cp[toklen]) &&
239 strncmp(cp, toks[tok], toklen) == 0) {
240 cp += toklen;
241 while (isspace((unsigned char)*cp))
242 cp++;
243 break;
247 switch (tok) {
248 case 2: /* _whatdb */
249 while (ep > cp && ep[-1] != '/')
250 ep--;
251 if (ep == cp)
252 continue;
253 *ep = '\0';
254 /* FALLTHROUGH */
255 case 0: /* manpath */
256 manpath_add(&conf->manpath, cp, 0);
257 *manpath_default = '\0';
258 break;
259 case 1: /* output */
260 manconf_output(&conf->output, cp);
261 break;
262 default:
263 break;
266 free(line);
267 fclose(stream);
269 out:
270 if (*manpath_default != '\0')
271 manpath_parseline(&conf->manpath, manpath_default, 0);
273 #endif
275 void
276 manconf_output(struct manoutput *conf, const char *cp)
278 const char *const toks[] = {
279 "includes", "man", "paper", "style",
280 "indent", "width", "fragment", "mdoc"
283 size_t len, tok;
285 for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
286 len = strlen(toks[tok]);
287 if ( ! strncmp(cp, toks[tok], len) &&
288 strchr(" = ", cp[len]) != NULL) {
289 cp += len;
290 if (*cp == '=')
291 cp++;
292 while (isspace((unsigned char)*cp))
293 cp++;
294 break;
298 if (tok < 6 && *cp == '\0')
299 return;
301 switch (tok) {
302 case 0:
303 if (conf->includes == NULL)
304 conf->includes = mandoc_strdup(cp);
305 break;
306 case 1:
307 if (conf->man == NULL)
308 conf->man = mandoc_strdup(cp);
309 break;
310 case 2:
311 if (conf->paper == NULL)
312 conf->paper = mandoc_strdup(cp);
313 break;
314 case 3:
315 if (conf->style == NULL)
316 conf->style = mandoc_strdup(cp);
317 break;
318 case 4:
319 if (conf->indent == 0)
320 conf->indent = strtonum(cp, 0, 1000, NULL);
321 break;
322 case 5:
323 if (conf->width == 0)
324 conf->width = strtonum(cp, 58, 1000, NULL);
325 break;
326 case 6:
327 conf->fragment = 1;
328 break;
329 case 7:
330 conf->mdoc = 1;
331 break;
332 default:
333 break;