]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.7 2011/12/13 21:09:01 kristaps Exp $ */
3 * Copyright (c) 2011 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
22 #include <sys/param.h>
34 #define MAN_CONF_FILE "/etc/man.conf"
35 #define MAN_CONF_KEY "_whatdb"
37 static void manpath_add(struct manpaths
*, const char *);
38 static void manpath_parseline(struct manpaths
*, char *);
41 manpath_parse(struct manpaths
*dirs
, const char *file
,
42 char *defp
, char *auxp
)
45 char cmd
[(MAXPATHLEN
* 3) + 20];
50 strlcpy(cmd
, "manpath", sizeof(cmd
));
52 strlcat(cmd
, " -C ", sizeof(cmd
));
53 strlcat(cmd
, file
, sizeof(cmd
));
56 strlcat(cmd
, " -m ", sizeof(cmd
));
57 strlcat(cmd
, auxp
, sizeof(cmd
));
60 strlcat(cmd
, " -M ", sizeof(cmd
));
61 strlcat(cmd
, defp
, sizeof(cmd
));
64 /* Open manpath(1). Ignore errors. */
66 stream
= popen(cmd
, "r");
73 /* Read in as much output as we can. */
76 buf
= mandoc_realloc(buf
, bsz
+ 1024);
77 sz
= fread(buf
+ (int)bsz
, 1, 1024, stream
);
81 if ( ! ferror(stream
) && feof(stream
) &&
82 bsz
&& '\n' == buf
[bsz
- 1]) {
84 manpath_parseline(dirs
, buf
);
90 manpath_parseline(dirs
, auxp
);
93 defp
= getenv("MANPATH");
96 manpath_manconf(dirs
, file
? file
: MAN_CONF_FILE
);
98 manpath_parseline(dirs
, defp
);
103 * Parse a FULL pathname from a colon-separated list of arrays.
106 manpath_parseline(struct manpaths
*dirs
, char *path
)
113 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
114 manpath_add(dirs
, dir
);
118 * Add a directory to the array, ignoring bad directories.
119 * Grow the array one-by-one for simplicity's sake.
122 manpath_add(struct manpaths
*dirs
, const char *dir
)
128 if (NULL
== (cp
= realpath(dir
, buf
)))
131 for (i
= 0; i
< dirs
->sz
; i
++)
132 if (0 == strcmp(dirs
->paths
[i
], dir
))
135 dirs
->paths
= mandoc_realloc
137 ((size_t)dirs
->sz
+ 1) * sizeof(char *));
139 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
143 manpath_free(struct manpaths
*p
)
147 for (i
= 0; i
< p
->sz
; i
++)
154 manpath_manconf(struct manpaths
*dirs
, const char *file
)
160 keysz
= strlen(MAN_CONF_KEY
);
163 if (NULL
== (stream
= fopen(file
, "r")))
166 while (NULL
!= (p
= fgetln(stream
, &len
))) {
167 if (0 == len
|| '\n' != p
[--len
])
170 while (isspace((unsigned char)*p
))
172 if (strncmp(MAN_CONF_KEY
, p
, keysz
))
179 if (NULL
== (q
= strrchr(p
, '/')))
182 manpath_add(dirs
, p
);