Annotation of mandoc/manpath.c, Revision 1.26
1.26 ! schwarze 1: /* $Id: manpath.c,v 1.25 2015/05/07 12:08:13 schwarze Exp $ */
1.1 kristaps 2: /*
1.20 schwarze 3: * Copyright (c) 2011, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 4: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5: *
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.
9: *
1.22 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.22 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 kristaps 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.
17: */
18: #include "config.h"
1.16 schwarze 19:
20: #include <sys/types.h>
1.18 schwarze 21: #include <sys/stat.h>
1.1 kristaps 22:
23: #include <ctype.h>
24: #include <limits.h>
25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28:
1.13 schwarze 29: #include "mandoc_aux.h"
1.23 schwarze 30: #include "manconf.h"
1.1 kristaps 31:
1.26 ! schwarze 32: #if !HAVE_MANPATH
1.23 schwarze 33: static void manconf_file(struct manconf *, const char *);
1.26 ! schwarze 34: #endif
1.18 schwarze 35: static void manpath_add(struct manpaths *, const char *, int);
36: static void manpath_parseline(struct manpaths *, char *, int);
1.1 kristaps 37:
1.23 schwarze 38:
1.1 kristaps 39: void
1.23 schwarze 40: manconf_parse(struct manconf *conf, const char *file,
1.5 schwarze 41: char *defp, char *auxp)
1.1 kristaps 42: {
1.17 schwarze 43: #if HAVE_MANPATH
1.11 schwarze 44: char cmd[(PATH_MAX * 3) + 20];
1.6 kristaps 45: FILE *stream;
46: char *buf;
47: size_t sz, bsz;
48:
49: strlcpy(cmd, "manpath", sizeof(cmd));
50: if (file) {
51: strlcat(cmd, " -C ", sizeof(cmd));
52: strlcat(cmd, file, sizeof(cmd));
53: }
54: if (auxp) {
55: strlcat(cmd, " -m ", sizeof(cmd));
56: strlcat(cmd, auxp, sizeof(cmd));
57: }
58: if (defp) {
59: strlcat(cmd, " -M ", sizeof(cmd));
60: strlcat(cmd, defp, sizeof(cmd));
61: }
62:
63: /* Open manpath(1). Ignore errors. */
64:
65: stream = popen(cmd, "r");
66: if (NULL == stream)
67: return;
68:
69: buf = NULL;
70: bsz = 0;
71:
72: /* Read in as much output as we can. */
73:
74: do {
75: buf = mandoc_realloc(buf, bsz + 1024);
1.9 kristaps 76: sz = fread(buf + bsz, 1, 1024, stream);
1.6 kristaps 77: bsz += sz;
78: } while (sz > 0);
79:
80: if ( ! ferror(stream) && feof(stream) &&
81: bsz && '\n' == buf[bsz - 1]) {
82: buf[bsz - 1] = '\0';
1.24 schwarze 83: manpath_parseline(&conf->manpath, buf, 1);
1.6 kristaps 84: }
1.1 kristaps 85:
1.6 kristaps 86: free(buf);
87: pclose(stream);
88: #else
1.8 kristaps 89: char *insert;
1.4 schwarze 90:
1.8 kristaps 91: /* Always prepend -m. */
1.23 schwarze 92: manpath_parseline(&conf->manpath, auxp, 1);
1.10 schwarze 93:
1.8 kristaps 94: /* If -M is given, it overrides everything else. */
95: if (NULL != defp) {
1.23 schwarze 96: manpath_parseline(&conf->manpath, defp, 1);
1.8 kristaps 97: return;
98: }
1.1 kristaps 99:
1.8 kristaps 100: /* MANPATH and man.conf(5) cooperate. */
101: defp = getenv("MANPATH");
102: if (NULL == file)
103: file = MAN_CONF_FILE;
104:
105: /* No MANPATH; use man.conf(5) only. */
106: if (NULL == defp || '\0' == defp[0]) {
1.23 schwarze 107: manconf_file(conf, file);
1.8 kristaps 108: return;
109: }
110:
111: /* Prepend man.conf(5) to MANPATH. */
112: if (':' == defp[0]) {
1.23 schwarze 113: manconf_file(conf, file);
114: manpath_parseline(&conf->manpath, defp, 0);
1.8 kristaps 115: return;
116: }
117:
118: /* Append man.conf(5) to MANPATH. */
1.9 kristaps 119: if (':' == defp[strlen(defp) - 1]) {
1.23 schwarze 120: manpath_parseline(&conf->manpath, defp, 0);
121: manconf_file(conf, file);
1.8 kristaps 122: return;
123: }
124:
125: /* Insert man.conf(5) into MANPATH. */
126: insert = strstr(defp, "::");
127: if (NULL != insert) {
128: *insert++ = '\0';
1.23 schwarze 129: manpath_parseline(&conf->manpath, defp, 0);
130: manconf_file(conf, file);
131: manpath_parseline(&conf->manpath, insert + 1, 0);
1.8 kristaps 132: return;
133: }
134:
135: /* MANPATH overrides man.conf(5) completely. */
1.23 schwarze 136: manpath_parseline(&conf->manpath, defp, 0);
1.6 kristaps 137: #endif
1.1 kristaps 138: }
139:
140: /*
141: * Parse a FULL pathname from a colon-separated list of arrays.
142: */
1.6 kristaps 143: static void
1.18 schwarze 144: manpath_parseline(struct manpaths *dirs, char *path, int complain)
1.1 kristaps 145: {
146: char *dir;
147:
148: if (NULL == path)
149: return;
150:
151: for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
1.18 schwarze 152: manpath_add(dirs, dir, complain);
1.1 kristaps 153: }
154:
155: /*
156: * Add a directory to the array, ignoring bad directories.
157: * Grow the array one-by-one for simplicity's sake.
158: */
159: static void
1.18 schwarze 160: manpath_add(struct manpaths *dirs, const char *dir, int complain)
1.1 kristaps 161: {
162: char buf[PATH_MAX];
1.18 schwarze 163: struct stat sb;
1.1 kristaps 164: char *cp;
1.9 kristaps 165: size_t i;
1.1 kristaps 166:
1.18 schwarze 167: if (NULL == (cp = realpath(dir, buf))) {
168: if (complain) {
169: fputs("manpath: ", stderr);
170: perror(dir);
171: }
1.1 kristaps 172: return;
1.18 schwarze 173: }
1.1 kristaps 174:
175: for (i = 0; i < dirs->sz; i++)
176: if (0 == strcmp(dirs->paths[i], dir))
177: return;
178:
1.18 schwarze 179: if (stat(cp, &sb) == -1) {
180: if (complain) {
181: fputs("manpath: ", stderr);
182: perror(dir);
183: }
184: return;
185: }
186:
1.15 schwarze 187: dirs->paths = mandoc_reallocarray(dirs->paths,
188: dirs->sz + 1, sizeof(char *));
1.1 kristaps 189:
190: dirs->paths[dirs->sz++] = mandoc_strdup(cp);
1.2 kristaps 191: }
192:
193: void
1.23 schwarze 194: manconf_free(struct manconf *conf)
1.2 kristaps 195: {
1.9 kristaps 196: size_t i;
1.2 kristaps 197:
1.23 schwarze 198: for (i = 0; i < conf->manpath.sz; i++)
199: free(conf->manpath.paths[i]);
1.2 kristaps 200:
1.23 schwarze 201: free(conf->manpath.paths);
202: free(conf->output.includes);
203: free(conf->output.man);
204: free(conf->output.paper);
205: free(conf->output.style);
1.2 kristaps 206: }
207:
1.26 ! schwarze 208: #if !HAVE_MANPATH
1.23 schwarze 209: static void
210: manconf_file(struct manconf *conf, const char *file)
1.2 kristaps 211: {
1.23 schwarze 212: const char *const toks[] = { "manpath", "output", "_whatdb" };
1.25 schwarze 213: char manpath_default[] = MANPATH_DEFAULT;
1.22 schwarze 214:
1.2 kristaps 215: FILE *stream;
1.22 schwarze 216: char *cp, *ep;
217: size_t len, tok;
1.1 kristaps 218:
1.22 schwarze 219: if ((stream = fopen(file, "r")) == NULL)
1.25 schwarze 220: goto out;
1.1 kristaps 221:
1.22 schwarze 222: while ((cp = fgetln(stream, &len)) != NULL) {
223: ep = cp + len;
224: if (ep[-1] != '\n')
1.1 kristaps 225: break;
1.22 schwarze 226: *--ep = '\0';
227: while (isspace((unsigned char)*cp))
228: cp++;
229: if (*cp == '#')
1.1 kristaps 230: continue;
1.22 schwarze 231:
232: for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
233: len = strlen(toks[tok]);
234: if (cp + len < ep &&
235: isspace((unsigned char)cp[len]) &&
236: !strncmp(cp, toks[tok], len)) {
237: cp += len;
238: while (isspace((unsigned char)*cp))
239: cp++;
240: break;
241: }
242: }
243:
244: switch (tok) {
1.23 schwarze 245: case 2: /* _whatdb */
1.22 schwarze 246: while (ep > cp && ep[-1] != '/')
247: ep--;
248: if (ep == cp)
249: continue;
250: *ep = '\0';
251: /* FALLTHROUGH */
252: case 0: /* manpath */
1.23 schwarze 253: manpath_add(&conf->manpath, cp, 0);
1.25 schwarze 254: *manpath_default = '\0';
1.23 schwarze 255: break;
256: case 1: /* output */
257: manconf_output(&conf->output, cp);
1.22 schwarze 258: break;
259: default:
260: break;
261: }
1.1 kristaps 262: }
1.25 schwarze 263: fclose(stream);
1.1 kristaps 264:
1.25 schwarze 265: out:
266: if (*manpath_default != '\0')
267: manpath_parseline(&conf->manpath, manpath_default, 0);
1.23 schwarze 268: }
1.26 ! schwarze 269: #endif
1.23 schwarze 270:
271: void
272: manconf_output(struct manoutput *conf, const char *cp)
273: {
274: const char *const toks[] = {
275: "includes", "man", "paper", "style",
276: "indent", "width", "fragment", "mdoc"
277: };
278:
279: size_t len, tok;
280:
281: for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
282: len = strlen(toks[tok]);
283: if ( ! strncmp(cp, toks[tok], len) &&
284: strchr(" = ", cp[len]) != NULL) {
285: cp += len;
286: if (*cp == '=')
287: cp++;
288: while (isspace((unsigned char)*cp))
289: cp++;
290: break;
291: }
292: }
293:
294: if (tok < 6 && *cp == '\0')
295: return;
296:
297: switch (tok) {
298: case 0:
299: if (conf->includes == NULL)
300: conf->includes = mandoc_strdup(cp);
301: break;
302: case 1:
303: if (conf->man == NULL)
304: conf->man = mandoc_strdup(cp);
305: break;
306: case 2:
307: if (conf->paper == NULL)
308: conf->paper = mandoc_strdup(cp);
309: break;
310: case 3:
311: if (conf->style == NULL)
312: conf->style = mandoc_strdup(cp);
313: break;
314: case 4:
315: if (conf->indent == 0)
316: conf->indent = strtonum(cp, 0, 1000, NULL);
317: break;
318: case 5:
319: if (conf->width == 0)
320: conf->width = strtonum(cp, 58, 1000, NULL);
321: break;
322: case 6:
323: conf->fragment = 1;
324: break;
325: case 7:
326: conf->mdoc = 1;
327: break;
328: default:
329: break;
330: }
1.1 kristaps 331: }
CVSweb