2006-04-10 18:39:06 +04:00
|
|
|
/* $NetBSD: catman.c,v 1.26 2006/04/10 14:39:06 chuck Exp $ */
|
1999-04-04 14:56:38 +04:00
|
|
|
|
1993-07-28 01:29:27 +04:00
|
|
|
/*
|
1999-04-04 14:56:38 +04:00
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
1993-07-28 01:29:27 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
1999-04-04 14:56:38 +04:00
|
|
|
* Author: Baldassare Dante Profeta <dante@mclink.it>
|
|
|
|
*
|
1993-07-28 01:29:27 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1999-04-04 14:56:38 +04:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
1993-07-28 01:29:27 +04:00
|
|
|
*
|
1999-04-04 14:56:38 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
1993-07-28 01:29:27 +04:00
|
|
|
*/
|
|
|
|
|
1994-04-27 00:15:50 +04:00
|
|
|
#include <sys/types.h>
|
1999-04-04 14:56:38 +04:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/param.h>
|
1994-04-27 00:15:50 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
2000-01-09 07:54:53 +03:00
|
|
|
#include <sys/utsname.h>
|
1999-04-04 14:56:38 +04:00
|
|
|
#include <ctype.h>
|
1994-04-28 02:37:11 +04:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1999-04-04 14:56:38 +04:00
|
|
|
#include <fnmatch.h>
|
1994-04-28 02:37:11 +04:00
|
|
|
#include <limits.h>
|
1999-04-04 14:56:38 +04:00
|
|
|
#include <libgen.h>
|
1993-07-28 01:29:27 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1999-04-04 14:56:38 +04:00
|
|
|
#include <glob.h>
|
1993-07-28 01:29:27 +04:00
|
|
|
|
2002-09-14 00:40:04 +04:00
|
|
|
#include "manconf.h"
|
1994-04-28 02:37:11 +04:00
|
|
|
#include "pathnames.h"
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
int f_nowhatis = 0;
|
|
|
|
int f_noaction = 0;
|
|
|
|
int f_noformat = 0;
|
|
|
|
int f_ignerr = 0;
|
|
|
|
int f_noprint = 0;
|
|
|
|
int dowhatis = 0;
|
|
|
|
|
2000-05-30 01:05:34 +04:00
|
|
|
TAG *defp; /* pointer to _default list */
|
|
|
|
|
2006-04-10 18:39:06 +04:00
|
|
|
int main(int, char * const *);
|
|
|
|
static void setdefentries(char *, char *, const char *);
|
|
|
|
static void uniquepath(void);
|
|
|
|
static void catman(void);
|
|
|
|
static void scanmandir(const char *, const char *);
|
|
|
|
static int splitentry(char *, char *, size_t, char *, size_t);
|
|
|
|
static void setcatsuffix(char *, const char *, const char *);
|
|
|
|
static void makecat(const char *, const char *, const char *, const char *);
|
|
|
|
static void makewhatis(void);
|
|
|
|
static void dosystem(const char *);
|
|
|
|
static void usage(void);
|
1994-04-27 00:15:50 +04:00
|
|
|
|
1993-07-28 01:29:27 +04:00
|
|
|
|
|
|
|
int
|
2006-04-10 18:39:06 +04:00
|
|
|
main(int argc, char * const *argv)
|
1993-07-28 01:29:27 +04:00
|
|
|
{
|
1999-04-04 14:56:38 +04:00
|
|
|
char *m_path = NULL;
|
|
|
|
char *m_add = NULL;
|
1994-04-27 00:15:50 +04:00
|
|
|
int c;
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
while ((c = getopt(argc, argv, "km:M:npsw")) != -1) {
|
1993-07-28 01:29:27 +04:00
|
|
|
switch (c) {
|
1994-04-27 00:15:50 +04:00
|
|
|
case 'k':
|
|
|
|
f_ignerr = 1;
|
|
|
|
break;
|
1993-07-28 01:29:27 +04:00
|
|
|
case 'n':
|
1994-04-27 00:15:50 +04:00
|
|
|
f_nowhatis = 1;
|
1993-07-28 01:29:27 +04:00
|
|
|
break;
|
|
|
|
case 'p':
|
1994-04-27 00:15:50 +04:00
|
|
|
f_noaction = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
f_noprint = 1;
|
1993-07-28 01:29:27 +04:00
|
|
|
break;
|
|
|
|
case 'w':
|
1994-04-27 00:15:50 +04:00
|
|
|
f_noformat = 1;
|
1993-07-28 01:29:27 +04:00
|
|
|
break;
|
1999-04-04 14:56:38 +04:00
|
|
|
case 'm':
|
|
|
|
m_add = optarg;
|
|
|
|
break;
|
1993-07-28 01:29:27 +04:00
|
|
|
case 'M':
|
1999-04-04 14:56:38 +04:00
|
|
|
m_path = optarg;
|
1993-07-28 01:29:27 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1994-04-27 00:15:50 +04:00
|
|
|
if (f_noprint && f_noaction)
|
|
|
|
f_noprint = 0;
|
|
|
|
|
1994-04-28 02:37:11 +04:00
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
1999-04-04 14:56:38 +04:00
|
|
|
|
|
|
|
config(_PATH_MANCONF);
|
2000-05-30 01:05:34 +04:00
|
|
|
setdefentries(m_path, m_add, (argc == 0) ? NULL : argv[argc-1]);
|
1999-04-04 14:56:38 +04:00
|
|
|
uniquepath();
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1994-04-27 00:39:22 +04:00
|
|
|
if (f_noformat == 0 || f_nowhatis == 0)
|
1999-04-04 14:56:38 +04:00
|
|
|
catman();
|
1994-04-27 00:15:50 +04:00
|
|
|
if (f_nowhatis == 0 && dowhatis)
|
1999-04-04 14:56:38 +04:00
|
|
|
makewhatis();
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
return(0);
|
1993-07-28 01:29:27 +04:00
|
|
|
}
|
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
static void
|
2006-04-10 18:39:06 +04:00
|
|
|
setdefentries(char *m_path, char *m_add, const char *sections)
|
1999-04-04 14:56:38 +04:00
|
|
|
{
|
2000-05-30 01:05:34 +04:00
|
|
|
TAG *defnewp, *sectnewp, *subp;
|
|
|
|
ENTRY *e_defp, *e_subp;
|
|
|
|
const char *p;
|
|
|
|
char *slashp, *machine;
|
1999-04-04 14:56:38 +04:00
|
|
|
char buf[MAXPATHLEN * 2];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get the machine type. */
|
2000-01-09 07:54:53 +03:00
|
|
|
if ((machine = getenv("MACHINE")) == NULL) {
|
|
|
|
struct utsname utsname;
|
|
|
|
|
|
|
|
if (uname(&utsname) == -1) {
|
|
|
|
perror("uname");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
machine = utsname.machine;
|
|
|
|
}
|
1999-04-04 14:56:38 +04:00
|
|
|
|
|
|
|
/* If there's no _default list, create an empty one. */
|
2006-04-10 18:39:06 +04:00
|
|
|
defp = gettag("_default", 1);
|
|
|
|
subp = gettag("_subdir", 1);
|
|
|
|
if (defp == NULL || subp == NULL)
|
|
|
|
err(1, "malloc");
|
2000-05-30 01:05:34 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
/*
|
|
|
|
* 0: If one or more sections was specified, rewrite _subdir list.
|
|
|
|
*/
|
|
|
|
if (sections != NULL) {
|
2006-04-10 18:39:06 +04:00
|
|
|
if ((sectnewp = gettag("_section_new", 1)) == NULL)
|
|
|
|
err(1, "malloc");
|
2003-05-09 04:47:46 +04:00
|
|
|
for (p = sections; *p;) {
|
2000-05-30 01:05:34 +04:00
|
|
|
i = snprintf(buf, sizeof(buf), "man%c", *p++);
|
2004-10-30 00:33:06 +04:00
|
|
|
for (; *p && !isdigit((unsigned char)*p) && i < sizeof(buf) - 1; i++)
|
2000-05-30 01:05:34 +04:00
|
|
|
buf[i] = *p++;
|
|
|
|
buf[i] = '\0';
|
2006-04-10 18:39:06 +04:00
|
|
|
if (addentry(sectnewp, buf, 0) < 0)
|
|
|
|
err(1, "malloc");
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
2000-05-30 01:05:34 +04:00
|
|
|
subp = sectnewp;
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1: If the user specified a MANPATH variable, or set the -M
|
|
|
|
* option, we replace the _default list with the user's list,
|
|
|
|
* appending the entries in the _subdir list and the machine.
|
|
|
|
*/
|
|
|
|
if (m_path == NULL)
|
|
|
|
m_path = getenv("MANPATH");
|
|
|
|
if (m_path != NULL) {
|
2006-04-10 18:39:06 +04:00
|
|
|
while ((e_defp = TAILQ_FIRST(&defp->entrylist)) != NULL) {
|
1999-04-04 14:56:38 +04:00
|
|
|
free(e_defp->s);
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_REMOVE(&defp->entrylist, e_defp, q);
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
for (p = strtok(m_path, ":");
|
|
|
|
p != NULL; p = strtok(NULL, ":")) {
|
|
|
|
slashp = p[strlen(p) - 1] == '/' ? "" : "/";
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_FOREACH(e_subp, &subp->entrylist, q) {
|
2003-05-09 04:47:46 +04:00
|
|
|
if (!strncmp(e_subp->s, "cat", 3))
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
|
|
|
|
p, slashp, e_subp->s, machine);
|
2006-04-10 18:39:06 +04:00
|
|
|
if (addentry(defp, buf, 0) < 0)
|
|
|
|
err(1, "malloc");
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2: If the user did not specify MANPATH, -M or a section, rewrite
|
|
|
|
* the _default list to include the _subdir list and the machine.
|
|
|
|
*/
|
|
|
|
if (m_path == NULL) {
|
2006-04-10 18:39:06 +04:00
|
|
|
defp = gettag("_default", 1);
|
|
|
|
defnewp = gettag("_default_new1", 1);
|
|
|
|
if (defp == NULL || defnewp == NULL)
|
|
|
|
err(1, "malloc");
|
2002-06-11 08:39:52 +04:00
|
|
|
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_FOREACH(e_defp, &defp->entrylist, q) {
|
1999-04-04 14:56:38 +04:00
|
|
|
slashp =
|
|
|
|
e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_FOREACH(e_subp, &subp->entrylist, q) {
|
2003-05-09 04:47:46 +04:00
|
|
|
if (!strncmp(e_subp->s, "cat", 3))
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
|
2000-05-30 01:05:34 +04:00
|
|
|
e_defp->s, slashp, e_subp->s, machine);
|
2006-04-10 18:39:06 +04:00
|
|
|
if (addentry(defnewp, buf, 0) < 0)
|
|
|
|
err(1, "malloc");
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
}
|
2000-05-30 01:05:34 +04:00
|
|
|
defp = defnewp;
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 3: If the user set the -m option, insert the user's list before
|
|
|
|
* whatever list we have, again appending the _subdir list and
|
|
|
|
* the machine.
|
|
|
|
*/
|
|
|
|
if (m_add != NULL)
|
|
|
|
for (p = strtok(m_add, ":"); p != NULL; p = strtok(NULL, ":")) {
|
|
|
|
slashp = p[strlen(p) - 1] == '/' ? "" : "/";
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_FOREACH(e_subp, &subp->entrylist, q) {
|
2003-05-09 04:47:46 +04:00
|
|
|
if (!strncmp(e_subp->s, "cat", 3))
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
|
|
|
(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
|
|
|
|
p, slashp, e_subp->s, machine);
|
2006-04-10 18:39:06 +04:00
|
|
|
if (addentry(defp, buf, 1) < 0)
|
|
|
|
err(1, "malloc");
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
/*
|
|
|
|
* Remove entries (directory) which are symbolic links to other entries.
|
|
|
|
* Some examples are showed below:
|
|
|
|
* 1) if /usr/X11 -> /usr/X11R6 then remove all /usr/X11/man entries.
|
|
|
|
* 2) if /usr/local/man -> /usr/share/man then remove all /usr/local/man
|
|
|
|
* entries
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
uniquepath(void)
|
|
|
|
{
|
2000-05-30 01:05:34 +04:00
|
|
|
TAG *defnewp;
|
1999-04-04 14:56:38 +04:00
|
|
|
ENTRY *e_defp;
|
|
|
|
glob_t manpaths;
|
|
|
|
struct stat st1;
|
|
|
|
struct stat st2;
|
|
|
|
struct stat st3;
|
2006-03-26 22:11:22 +04:00
|
|
|
int len, lnk, gflags;
|
|
|
|
size_t i, j;
|
1999-04-04 14:56:38 +04:00
|
|
|
char path[PATH_MAX], *p;
|
|
|
|
|
2002-06-11 08:39:52 +04:00
|
|
|
gflags = 0;
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_FOREACH(e_defp, &defp->entrylist, q) {
|
2002-06-11 08:39:52 +04:00
|
|
|
glob(e_defp->s, GLOB_BRACE | GLOB_NOSORT | gflags, NULL,
|
|
|
|
&manpaths);
|
|
|
|
gflags = GLOB_APPEND;
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
2006-04-10 18:39:06 +04:00
|
|
|
if ((defnewp = gettag("_default_new2", 1)) == NULL)
|
|
|
|
err(1, "malloc");
|
1999-04-04 14:56:38 +04:00
|
|
|
|
2003-05-09 04:47:46 +04:00
|
|
|
for (i = 0; i < manpaths.gl_pathc; i++) {
|
1999-04-04 14:56:38 +04:00
|
|
|
lnk = 0;
|
|
|
|
lstat(manpaths.gl_pathv[i], &st1);
|
2003-05-09 04:47:46 +04:00
|
|
|
for (j = 0; j < manpaths.gl_pathc; j++) {
|
2000-05-30 01:05:34 +04:00
|
|
|
if (i != j) {
|
1999-04-04 14:56:38 +04:00
|
|
|
lstat(manpaths.gl_pathv[j], &st2);
|
2000-05-30 01:05:34 +04:00
|
|
|
if (st1.st_ino == st2.st_ino) {
|
2003-07-13 16:27:14 +04:00
|
|
|
strlcpy(path, manpaths.gl_pathv[i],
|
|
|
|
sizeof(path));
|
2003-05-09 04:47:46 +04:00
|
|
|
for (p = path; *(p+1) != '\0';) {
|
1999-04-04 14:56:38 +04:00
|
|
|
p = dirname(p);
|
|
|
|
lstat(p, &st3);
|
2000-05-30 01:05:34 +04:00
|
|
|
if (S_ISLNK(st3.st_mode)) {
|
1999-04-04 14:56:38 +04:00
|
|
|
lnk = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = readlink(manpaths.gl_pathv[i],
|
2003-05-09 04:47:46 +04:00
|
|
|
path, sizeof(path) - 1);
|
2000-05-30 01:05:34 +04:00
|
|
|
if (len == -1)
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
2003-05-09 04:43:46 +04:00
|
|
|
path[len] = '\0';
|
2000-05-30 01:05:34 +04:00
|
|
|
if (!strcmp(path, manpaths.gl_pathv[j]))
|
1999-04-04 14:56:38 +04:00
|
|
|
lnk = 1;
|
|
|
|
}
|
2000-05-30 01:05:34 +04:00
|
|
|
if (lnk)
|
1999-04-04 14:56:38 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-05-30 01:05:34 +04:00
|
|
|
|
2006-04-10 18:39:06 +04:00
|
|
|
if (!lnk) {
|
|
|
|
if (addentry(defnewp, manpaths.gl_pathv[i], 0) < 0)
|
|
|
|
err(1, "malloc");
|
|
|
|
}
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
globfree(&manpaths);
|
|
|
|
|
2000-05-30 01:05:34 +04:00
|
|
|
defp = defnewp;
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
catman(void)
|
|
|
|
{
|
|
|
|
ENTRY *e_path;
|
2000-05-30 01:05:34 +04:00
|
|
|
const char *mandir;
|
1999-04-04 14:56:38 +04:00
|
|
|
char catdir[PATH_MAX], *cp;
|
|
|
|
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_FOREACH(e_path, &defp->entrylist, q) {
|
1999-04-04 14:56:38 +04:00
|
|
|
mandir = e_path->s;
|
2003-07-13 16:27:14 +04:00
|
|
|
strlcpy(catdir, mandir, sizeof(catdir));
|
2003-05-09 04:47:46 +04:00
|
|
|
if (!(cp = strstr(catdir, "man/man")))
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
2003-05-09 04:47:46 +04:00
|
|
|
cp += 4; *cp++ = 'c'; *cp++ = 'a'; *cp = 't';
|
1999-04-04 14:56:38 +04:00
|
|
|
scanmandir(catdir, mandir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-10 18:39:06 +04:00
|
|
|
scanmandir(const char *catdir, const char *mandir)
|
1993-07-28 01:29:27 +04:00
|
|
|
{
|
1999-04-04 14:56:38 +04:00
|
|
|
TAG *buildp, *crunchp;
|
|
|
|
ENTRY *e_build, *e_crunch;
|
1994-04-27 00:15:50 +04:00
|
|
|
char manpage[PATH_MAX];
|
|
|
|
char catpage[PATH_MAX];
|
1999-04-04 14:56:38 +04:00
|
|
|
char linkname[PATH_MAX];
|
|
|
|
char buffer[PATH_MAX], *bp;
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
char buildsuff[256], buildcmd[256];
|
|
|
|
char crunchsuff[256], crunchcmd[256];
|
|
|
|
char match[256];
|
1994-04-27 00:15:50 +04:00
|
|
|
struct stat manstat;
|
|
|
|
struct stat catstat;
|
1999-04-04 14:56:38 +04:00
|
|
|
struct stat lnkstat;
|
1994-04-27 00:15:50 +04:00
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
1999-04-04 14:56:38 +04:00
|
|
|
int len, error;
|
|
|
|
|
|
|
|
if ((dirp = opendir(mandir)) == 0) {
|
|
|
|
warn("can't open %s", mandir);
|
|
|
|
return;
|
|
|
|
}
|
1994-04-27 00:15:50 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
if (stat(catdir, &catstat) < 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
warn("can't stat %s", catdir);
|
|
|
|
closedir(dirp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (f_noprint == 0)
|
|
|
|
printf("mkdir %s\n", catdir);
|
2003-05-09 04:47:46 +04:00
|
|
|
if (f_noaction == 0 && mkdir(catdir, 0755) < 0) {
|
1999-04-04 14:56:38 +04:00
|
|
|
warn("can't create %s", catdir);
|
|
|
|
closedir(dirp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1994-04-27 00:15:50 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if (strcmp(dp->d_name, ".") == 0 ||
|
|
|
|
strcmp(dp->d_name, "..") == 0)
|
1994-04-27 00:15:50 +04:00
|
|
|
continue;
|
1999-04-04 14:56:38 +04:00
|
|
|
|
|
|
|
snprintf(manpage, sizeof(manpage), "%s/%s", mandir, dp->d_name);
|
|
|
|
snprintf(catpage, sizeof(catpage), "%s/%s", catdir, dp->d_name);
|
|
|
|
|
1999-04-20 18:22:32 +04:00
|
|
|
e_build = NULL;
|
2006-04-10 18:39:06 +04:00
|
|
|
if ((buildp = gettag("_build", 1)) == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
TAILQ_FOREACH(e_build, &buildp->entrylist, q) {
|
2003-07-13 16:27:14 +04:00
|
|
|
splitentry(e_build->s, buildsuff, sizeof(buildsuff),
|
|
|
|
buildcmd, sizeof(buildcmd));
|
2002-06-11 08:39:52 +04:00
|
|
|
snprintf(match, sizeof(match), "*%s",
|
|
|
|
buildsuff);
|
2003-05-09 04:47:46 +04:00
|
|
|
if (!fnmatch(match, manpage, 0))
|
2002-06-11 08:39:52 +04:00
|
|
|
break;
|
1994-04-27 00:15:50 +04:00
|
|
|
}
|
|
|
|
|
2003-05-09 04:47:46 +04:00
|
|
|
if (e_build == NULL)
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
1999-04-20 18:22:32 +04:00
|
|
|
|
|
|
|
e_crunch = NULL;
|
2006-04-10 18:39:06 +04:00
|
|
|
if ((crunchp = gettag("_crunch", 1)) == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
TAILQ_FOREACH(e_crunch, &crunchp->entrylist, q) {
|
2003-07-13 16:27:14 +04:00
|
|
|
splitentry(e_crunch->s, crunchsuff, sizeof(crunchsuff),
|
|
|
|
crunchcmd, sizeof(crunchcmd));
|
2002-06-11 08:39:52 +04:00
|
|
|
snprintf(match, sizeof(match), "*%s", crunchsuff);
|
2003-05-09 04:47:46 +04:00
|
|
|
if (!fnmatch(match, manpage, 0))
|
2002-06-11 08:39:52 +04:00
|
|
|
break;
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lstat(manpage, &manstat) <0) {
|
|
|
|
warn("can't stat %s", manpage);
|
|
|
|
continue;
|
|
|
|
} else {
|
2003-05-09 04:47:46 +04:00
|
|
|
if (S_ISLNK(manstat.st_mode)) {
|
2003-07-13 16:27:14 +04:00
|
|
|
strlcpy(buffer, catpage, sizeof(buffer));
|
|
|
|
strlcpy(linkname, basename(buffer),
|
|
|
|
sizeof(linkname));
|
2003-05-09 04:43:46 +04:00
|
|
|
len = readlink(manpage, buffer,
|
|
|
|
sizeof(buffer) - 1);
|
|
|
|
if (len == -1) {
|
1999-04-04 14:56:38 +04:00
|
|
|
warn("can't stat read symbolic link %s",
|
2003-07-13 16:27:14 +04:00
|
|
|
manpage);
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
buffer[len] = '\0';
|
|
|
|
bp = basename(buffer);
|
2003-07-13 16:27:14 +04:00
|
|
|
strlcpy(tmp, manpage, sizeof(tmp));
|
1999-04-04 14:56:38 +04:00
|
|
|
snprintf(manpage, sizeof(manpage), "%s/%s",
|
2003-07-13 16:27:14 +04:00
|
|
|
dirname(tmp), bp);
|
|
|
|
strlcpy(tmp, catpage, sizeof(tmp));
|
1999-04-04 14:56:38 +04:00
|
|
|
snprintf(catpage, sizeof(catpage), "%s/%s",
|
2003-07-13 16:27:14 +04:00
|
|
|
dirname(tmp), buffer);
|
1994-04-27 00:15:50 +04:00
|
|
|
}
|
1999-04-04 14:56:38 +04:00
|
|
|
else
|
2003-05-09 04:47:46 +04:00
|
|
|
*linkname = '\0';
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
1993-07-28 01:29:27 +04:00
|
|
|
|
2003-05-09 04:47:46 +04:00
|
|
|
if (!e_crunch) {
|
1999-04-04 14:56:38 +04:00
|
|
|
*crunchsuff = *crunchcmd = '\0';
|
1994-04-27 00:15:50 +04:00
|
|
|
}
|
1999-04-04 14:56:38 +04:00
|
|
|
setcatsuffix(catpage, buildsuff, crunchsuff);
|
2003-05-09 04:47:46 +04:00
|
|
|
if (*linkname != '\0')
|
1999-04-04 14:56:38 +04:00
|
|
|
setcatsuffix(linkname, buildsuff, crunchsuff);
|
1994-04-27 00:15:50 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
if (stat(manpage, &manstat) < 0) {
|
|
|
|
warn("can't stat %s", manpage);
|
|
|
|
continue;
|
|
|
|
}
|
1994-04-27 00:15:50 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
if (!S_ISREG(manstat.st_mode)) {
|
2003-05-09 04:47:46 +04:00
|
|
|
warnx("not a regular file %s", manpage);
|
1999-04-04 14:56:38 +04:00
|
|
|
continue;
|
|
|
|
}
|
1994-04-27 00:15:50 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
if ((error = stat(catpage, &catstat)) &&
|
|
|
|
errno != ENOENT) {
|
|
|
|
warn("can't stat %s", catpage);
|
|
|
|
continue;
|
|
|
|
}
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
if ((error && errno == ENOENT) ||
|
1999-04-20 09:02:36 +04:00
|
|
|
manstat.st_mtime > catstat.st_mtime) {
|
1999-04-04 14:56:38 +04:00
|
|
|
if (f_noformat) {
|
|
|
|
dowhatis = 1;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* reformat out of date manpage
|
|
|
|
*/
|
|
|
|
makecat(manpage, catpage, buildcmd, crunchcmd);
|
|
|
|
dowhatis = 1;
|
1994-04-27 00:15:50 +04:00
|
|
|
}
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
2003-05-09 04:47:46 +04:00
|
|
|
if (*linkname != '\0') {
|
2003-07-13 16:27:14 +04:00
|
|
|
strlcpy(tmp, catpage, sizeof(tmp));
|
1999-04-04 14:56:38 +04:00
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", dirname(tmp),
|
|
|
|
linkname);
|
|
|
|
if ((error = lstat(tmp, &lnkstat)) &&
|
1994-04-27 00:39:22 +04:00
|
|
|
errno != ENOENT) {
|
1999-04-04 14:56:38 +04:00
|
|
|
warn("can't stat %s", tmp);
|
1994-04-27 00:15:50 +04:00
|
|
|
continue;
|
1993-07-28 01:29:27 +04:00
|
|
|
}
|
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
if (error && errno == ENOENT) {
|
|
|
|
if (f_noformat) {
|
1994-04-27 00:39:22 +04:00
|
|
|
dowhatis = 1;
|
1999-04-04 14:56:38 +04:00
|
|
|
} else {
|
1994-04-27 00:39:22 +04:00
|
|
|
/*
|
1999-04-04 14:56:38 +04:00
|
|
|
* create symbolic link
|
1994-04-27 00:39:22 +04:00
|
|
|
*/
|
|
|
|
if (f_noprint == 0)
|
1999-04-04 14:56:38 +04:00
|
|
|
printf("ln -s %s %s\n", catpage,
|
2003-05-09 04:47:46 +04:00
|
|
|
linkname);
|
1999-04-04 14:56:38 +04:00
|
|
|
if (f_noaction == 0) {
|
2003-07-13 16:27:14 +04:00
|
|
|
strlcpy(tmp, catpage,
|
|
|
|
sizeof(tmp));
|
2003-05-09 04:47:46 +04:00
|
|
|
if (chdir(dirname(tmp)) == -1) {
|
1999-04-04 14:56:38 +04:00
|
|
|
warn("can't chdir");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-05-30 01:05:34 +04:00
|
|
|
if (symlink(catpage, linkname)
|
1999-04-04 14:56:38 +04:00
|
|
|
== -1) {
|
|
|
|
warn("can't create"
|
|
|
|
" symbolic"
|
|
|
|
" link %s",
|
|
|
|
linkname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
1994-04-27 00:39:22 +04:00
|
|
|
dowhatis = 1;
|
|
|
|
}
|
1994-04-27 00:15:50 +04:00
|
|
|
}
|
1993-07-28 01:29:27 +04:00
|
|
|
}
|
|
|
|
}
|
1999-04-04 14:56:38 +04:00
|
|
|
closedir(dirp);
|
1993-07-28 01:29:27 +04:00
|
|
|
}
|
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
static int
|
2006-04-10 18:39:06 +04:00
|
|
|
splitentry(char *s, char *first, size_t firstlen, char *second,
|
|
|
|
size_t secondlen)
|
1993-07-28 01:29:27 +04:00
|
|
|
{
|
1999-04-04 14:56:38 +04:00
|
|
|
char *c;
|
|
|
|
|
2004-10-30 00:33:06 +04:00
|
|
|
for (c = s; *c != '\0' && !isspace((unsigned char)*c); ++c)
|
2003-05-09 04:47:46 +04:00
|
|
|
;
|
|
|
|
if (*c == '\0')
|
1999-04-04 14:56:38 +04:00
|
|
|
return(0);
|
2003-07-13 16:27:14 +04:00
|
|
|
if (c - s + 1 > firstlen)
|
|
|
|
return(0);
|
1999-04-04 14:56:38 +04:00
|
|
|
strncpy(first, s, c-s);
|
|
|
|
first[c-s] = '\0';
|
2004-10-30 00:33:06 +04:00
|
|
|
for (; *c != '\0' && isspace((unsigned char)*c); ++c)
|
2003-05-09 04:47:46 +04:00
|
|
|
;
|
2003-07-13 16:27:14 +04:00
|
|
|
if (strlcpy(second, c, secondlen) >= secondlen)
|
|
|
|
return(0);
|
|
|
|
return(1);
|
1999-04-04 14:56:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-10 18:39:06 +04:00
|
|
|
setcatsuffix(char *catpage, const char *suffix, const char *crunchsuff)
|
1999-04-04 14:56:38 +04:00
|
|
|
{
|
|
|
|
TAG *tp;
|
|
|
|
char *p;
|
|
|
|
|
2003-05-09 04:47:46 +04:00
|
|
|
for (p = catpage + strlen(catpage); p != catpage; p--)
|
|
|
|
if (!fnmatch(suffix, p, 0)) {
|
2006-04-10 18:39:06 +04:00
|
|
|
if ((tp = gettag("_suffix", 1)) == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
if (! TAILQ_EMPTY(&tp->entrylist)) {
|
2002-06-11 08:39:52 +04:00
|
|
|
sprintf(p, "%s%s",
|
2006-04-10 18:39:06 +04:00
|
|
|
TAILQ_FIRST(&tp->entrylist)->s, crunchsuff);
|
1999-04-04 14:56:38 +04:00
|
|
|
} else {
|
|
|
|
sprintf(p, ".0%s", crunchsuff);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-10 18:39:06 +04:00
|
|
|
makecat(const char *manpage, const char *catpage, const char *buildcmd,
|
|
|
|
const char *crunchcmd)
|
1999-04-04 14:56:38 +04:00
|
|
|
{
|
|
|
|
char crunchbuf[1024];
|
|
|
|
char sysbuf[2048];
|
|
|
|
|
|
|
|
snprintf(sysbuf, sizeof(sysbuf), buildcmd, manpage);
|
|
|
|
|
2003-05-09 04:47:46 +04:00
|
|
|
if (*crunchcmd != '\0') {
|
1999-04-04 14:56:38 +04:00
|
|
|
snprintf(crunchbuf, sizeof(crunchbuf), crunchcmd, catpage);
|
|
|
|
snprintf(sysbuf, sizeof(sysbuf), "%s | %s", sysbuf, crunchbuf);
|
|
|
|
} else {
|
|
|
|
snprintf(sysbuf, sizeof(sysbuf), "%s > %s", sysbuf, catpage);
|
|
|
|
}
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1994-04-27 00:15:50 +04:00
|
|
|
if (f_noprint == 0)
|
1993-07-28 01:29:27 +04:00
|
|
|
printf("%s\n", sysbuf);
|
1994-04-27 00:15:50 +04:00
|
|
|
if (f_noaction == 0)
|
|
|
|
dosystem(sysbuf);
|
1993-07-28 01:29:27 +04:00
|
|
|
}
|
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
static void
|
|
|
|
makewhatis(void)
|
|
|
|
{
|
|
|
|
TAG *whatdbp;
|
|
|
|
ENTRY *e_whatdb;
|
|
|
|
char sysbuf[1024];
|
|
|
|
|
2006-04-10 18:39:06 +04:00
|
|
|
if ((whatdbp = gettag("_whatdb", 1)) == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
TAILQ_FOREACH(e_whatdb, &whatdbp->entrylist, q) {
|
1999-04-04 14:56:38 +04:00
|
|
|
snprintf(sysbuf, sizeof(sysbuf), "%s %s",
|
2002-06-11 08:39:52 +04:00
|
|
|
_PATH_WHATIS, dirname(e_whatdb->s));
|
1999-04-04 14:56:38 +04:00
|
|
|
if (f_noprint == 0)
|
|
|
|
printf("%s\n", sysbuf);
|
|
|
|
if (f_noaction == 0)
|
|
|
|
dosystem(sysbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-04-10 18:39:06 +04:00
|
|
|
dosystem(const char *cmd)
|
1994-04-27 00:15:50 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if ((status = system(cmd)) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (status == -1)
|
|
|
|
err(1, "cannot execute action");
|
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
errx(1, "child was signaled to quit. aborting");
|
|
|
|
if (WIFSTOPPED(status))
|
|
|
|
errx(1, "child was stopped. aborting");
|
|
|
|
if (f_ignerr == 0)
|
1997-10-18 08:11:01 +04:00
|
|
|
errx(1, "*** Exited %d", status);
|
|
|
|
warnx("*** Exited %d (continuing)", status);
|
1994-04-27 00:15:50 +04:00
|
|
|
}
|
1993-07-28 01:29:27 +04:00
|
|
|
|
1999-04-04 14:56:38 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
1993-07-28 01:29:27 +04:00
|
|
|
{
|
1994-04-28 02:37:11 +04:00
|
|
|
(void)fprintf(stderr,
|
1999-04-04 14:56:38 +04:00
|
|
|
"usage: catman [-knpsw] [-m manpath] [sections]\n");
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
" catman [-knpsw] [-M manpath] [sections]\n");
|
1993-07-28 01:29:27 +04:00
|
|
|
exit(1);
|
|
|
|
}
|