Home | History | Annotate | Line # | Download | only in catman
catman.c revision 1.7
      1  1.1      jtc /*
      2  1.1      jtc  * Copyright (c) 1993 Winning Strategies, Inc.
      3  1.1      jtc  * All rights reserved.
      4  1.1      jtc  *
      5  1.1      jtc  * Redistribution and use in source and binary forms, with or without
      6  1.1      jtc  * modification, are permitted provided that the following conditions
      7  1.1      jtc  * are met:
      8  1.1      jtc  * 1. Redistributions of source code must retain the above copyright
      9  1.1      jtc  *    notice, this list of conditions and the following disclaimer.
     10  1.1      jtc  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      jtc  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      jtc  *    documentation and/or other materials provided with the distribution.
     13  1.1      jtc  * 3. All advertising materials mentioning features or use of this software
     14  1.1      jtc  *    must display the following acknowledgement:
     15  1.1      jtc  *      This product includes software developed by Winning Strategies, Inc.
     16  1.1      jtc  * 4. The name of the author may not be used to endorse or promote products
     17  1.3      jtc  *    derived from this software without specific prior written permission
     18  1.1      jtc  *
     19  1.1      jtc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1      jtc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1      jtc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1      jtc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1      jtc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1      jtc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1      jtc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1      jtc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1      jtc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1      jtc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1      jtc  */
     30  1.2  mycroft 
     31  1.7    lukem #include <sys/cdefs.h>
     32  1.2  mycroft #ifndef lint
     33  1.7    lukem __RCSID("$Id: catman.c,v 1.7 1997/10/18 04:11:02 lukem Exp $");
     34  1.2  mycroft #endif /* not lint */
     35  1.1      jtc 
     36  1.4   chopps #include <sys/types.h>
     37  1.4   chopps #include <sys/stat.h>
     38  1.4   chopps #include <sys/wait.h>
     39  1.6      cgd #include <dirent.h>
     40  1.6      cgd #include <err.h>
     41  1.6      cgd #include <errno.h>
     42  1.6      cgd #include <limits.h>
     43  1.1      jtc #include <stdio.h>
     44  1.1      jtc #include <stdlib.h>
     45  1.1      jtc #include <string.h>
     46  1.1      jtc #include <unistd.h>
     47  1.6      cgd #include <paths.h>
     48  1.1      jtc 
     49  1.6      cgd #include "pathnames.h"
     50  1.4   chopps 
     51  1.4   chopps int f_nowhatis;
     52  1.4   chopps int f_noaction;
     53  1.4   chopps int f_noformat;
     54  1.4   chopps int f_ignerr;
     55  1.4   chopps int f_noprint;
     56  1.4   chopps 
     57  1.4   chopps int dowhatis;
     58  1.4   chopps 
     59  1.6      cgd char *mp = _PATH_MAN;
     60  1.6      cgd char *sp = _MAN_SECTIONS;
     61  1.4   chopps 
     62  1.7    lukem void	catman __P((const char *, char *));
     63  1.7    lukem int	main __P((int, char **));
     64  1.7    lukem void	makewhatis __P((const char *));
     65  1.7    lukem void	dosystem __P((const char *));
     66  1.7    lukem void	usage __P((void));
     67  1.1      jtc 
     68  1.1      jtc int
     69  1.1      jtc main(argc, argv)
     70  1.4   chopps 	int argc;
     71  1.4   chopps 	char **argv;
     72  1.1      jtc {
     73  1.4   chopps 	int c;
     74  1.1      jtc 
     75  1.7    lukem 	while ((c = getopt(argc, argv, "knpswM:")) != -1) {
     76  1.1      jtc 		switch (c) {
     77  1.4   chopps 		case 'k':
     78  1.4   chopps 			f_ignerr = 1;
     79  1.4   chopps 			break;
     80  1.1      jtc 		case 'n':
     81  1.4   chopps 			f_nowhatis = 1;
     82  1.1      jtc 			break;
     83  1.1      jtc 		case 'p':
     84  1.4   chopps 			f_noaction = 1;
     85  1.4   chopps 			break;
     86  1.4   chopps 		case 's':
     87  1.4   chopps 			f_noprint = 1;
     88  1.1      jtc 			break;
     89  1.1      jtc 		case 'w':
     90  1.4   chopps 			f_noformat = 1;
     91  1.1      jtc 			break;
     92  1.1      jtc 		case 'M':
     93  1.1      jtc 			mp = optarg;
     94  1.1      jtc 			break;
     95  1.1      jtc 
     96  1.1      jtc 		case '?':
     97  1.1      jtc 		default:
     98  1.1      jtc 			usage();
     99  1.1      jtc 		}
    100  1.1      jtc 	}
    101  1.1      jtc 
    102  1.1      jtc 	argc -= optind;
    103  1.1      jtc 	argv += optind;
    104  1.1      jtc 
    105  1.4   chopps 	if (f_noprint && f_noaction)
    106  1.4   chopps 		f_noprint = 0;
    107  1.4   chopps 
    108  1.6      cgd 	if (argc > 1)
    109  1.6      cgd 		usage();
    110  1.6      cgd 	if (argc == 1)
    111  1.6      cgd 		sp = *argv;
    112  1.1      jtc 
    113  1.5   chopps 	if (f_noformat == 0 || f_nowhatis == 0)
    114  1.4   chopps 		catman(mp, sp);
    115  1.4   chopps 	if (f_nowhatis == 0 && dowhatis)
    116  1.4   chopps 		makewhatis(mp);
    117  1.1      jtc 
    118  1.1      jtc 	exit(0);
    119  1.1      jtc }
    120  1.1      jtc 
    121  1.1      jtc 
    122  1.1      jtc void
    123  1.1      jtc catman(path, section)
    124  1.4   chopps 	const char *path;
    125  1.4   chopps 	char *section;
    126  1.1      jtc {
    127  1.4   chopps 	char mandir[PATH_MAX];
    128  1.4   chopps 	char catdir[PATH_MAX];
    129  1.4   chopps 	char manpage[PATH_MAX];
    130  1.4   chopps 	char catpage[PATH_MAX];
    131  1.4   chopps 	char sysbuf[1024];
    132  1.4   chopps 	struct stat manstat;
    133  1.4   chopps 	struct stat catstat;
    134  1.4   chopps 	struct dirent *dp;
    135  1.4   chopps 	DIR *dirp;
    136  1.4   chopps 	char *s, *tmp;
    137  1.5   chopps 	int sectlen, error;
    138  1.4   chopps 
    139  1.4   chopps 	for (s = section; *s; s += sectlen) {
    140  1.6      cgd #ifdef notdef
    141  1.4   chopps 		tmp = s;
    142  1.4   chopps 		sectlen = 0;
    143  1.4   chopps 		if (isdigit(*tmp)) {
    144  1.4   chopps 			sectlen++;
    145  1.4   chopps 			tmp++;
    146  1.4   chopps 			while (*tmp && isdigit(*tmp) == 0) {
    147  1.4   chopps 				sectlen++;
    148  1.4   chopps 				tmp++;
    149  1.4   chopps 			}
    150  1.4   chopps 		}
    151  1.6      cgd #else
    152  1.6      cgd 		sectlen = 1;
    153  1.6      cgd #endif
    154  1.4   chopps 		if (sectlen == 0)
    155  1.4   chopps 			errx(1, "malformed section string");
    156  1.4   chopps 
    157  1.6      cgd 		sprintf(mandir, "%s/%s%.*s", path, _PATH_MANPAGES, sectlen, s);
    158  1.6      cgd 		sprintf(catdir, "%s/%s%.*s", path, _PATH_CATPAGES, sectlen, s);
    159  1.4   chopps 
    160  1.4   chopps 		if ((dirp = opendir(mandir)) == 0) {
    161  1.4   chopps 			warn("can't open %s", mandir);
    162  1.4   chopps 			continue;
    163  1.4   chopps 		}
    164  1.4   chopps 
    165  1.4   chopps 		if (stat(catdir, &catstat) < 0) {
    166  1.4   chopps 			if (errno != ENOENT) {
    167  1.4   chopps 				warn("can't stat %s", catdir);
    168  1.4   chopps 				closedir(dirp);
    169  1.4   chopps 				continue;
    170  1.4   chopps 			}
    171  1.4   chopps 			if (f_noprint == 0)
    172  1.4   chopps 				printf("mkdir %s\n", catdir);
    173  1.4   chopps 			if (f_noaction == 0 && mkdir(catdir, 0755) < 0) {
    174  1.4   chopps 				warn("can't create %s", catdir);
    175  1.4   chopps 				closedir(dirp);
    176  1.4   chopps 				return;
    177  1.1      jtc 			}
    178  1.1      jtc 
    179  1.1      jtc 		}
    180  1.1      jtc 
    181  1.4   chopps 		while ((dp = readdir(dirp)) != NULL) {
    182  1.4   chopps 			if (strcmp(dp->d_name, ".") == 0 ||
    183  1.4   chopps 			    strcmp(dp->d_name, "..") == 0)
    184  1.4   chopps 				continue;
    185  1.4   chopps 
    186  1.4   chopps 			sprintf(manpage, "%s/%s", mandir, dp->d_name);
    187  1.4   chopps 			sprintf(catpage, "%s/%s", catdir, dp->d_name);
    188  1.4   chopps 			if ((tmp = strrchr(catpage, '.')) != NULL)
    189  1.4   chopps 				strcpy(tmp, ".0");
    190  1.4   chopps 			else
    191  1.4   chopps 				continue;
    192  1.4   chopps 
    193  1.4   chopps 			if (stat(manpage, &manstat) < 0) {
    194  1.4   chopps 				warn("can't stat %s", manpage);
    195  1.4   chopps 				continue;
    196  1.4   chopps 			}
    197  1.4   chopps 
    198  1.4   chopps 			if (!S_ISREG(manstat.st_mode)) {
    199  1.4   chopps 				warnx("not a regular file %s", manpage);
    200  1.4   chopps 				continue;
    201  1.4   chopps 			}
    202  1.5   chopps 			if ((error = stat(catpage, &catstat)) &&
    203  1.5   chopps 			    errno != ENOENT) {
    204  1.4   chopps 				warn("can't stat %s", catpage);
    205  1.4   chopps 				continue;
    206  1.4   chopps 			}
    207  1.4   chopps 
    208  1.5   chopps 			if ((error && errno == ENOENT) ||
    209  1.4   chopps 			    manstat.st_mtime >= catstat.st_mtime) {
    210  1.5   chopps 				if (f_noformat)
    211  1.5   chopps 					dowhatis = 1;
    212  1.5   chopps 				else {
    213  1.5   chopps 					/*
    214  1.5   chopps 					 * manpage is out of date,
    215  1.5   chopps 					 * reformat
    216  1.5   chopps 					 */
    217  1.5   chopps 					sprintf(sysbuf, "nroff -mandoc %s > %s",
    218  1.5   chopps 					    manpage, catpage);
    219  1.5   chopps 					if (f_noprint == 0)
    220  1.5   chopps 						printf("%s\n", sysbuf);
    221  1.5   chopps 					if (f_noaction == 0)
    222  1.5   chopps 						dosystem(sysbuf);
    223  1.5   chopps 					dowhatis = 1;
    224  1.5   chopps 				}
    225  1.4   chopps 			}
    226  1.4   chopps 		}
    227  1.4   chopps 		closedir(dirp);
    228  1.1      jtc 	}
    229  1.1      jtc }
    230  1.1      jtc 
    231  1.1      jtc void
    232  1.1      jtc makewhatis(path)
    233  1.4   chopps 	const char *path;
    234  1.1      jtc {
    235  1.4   chopps 	char sysbuf[1024];
    236  1.1      jtc 
    237  1.6      cgd 	sprintf(sysbuf, "%s %s", _PATH_MAKEWHATIS, path);
    238  1.4   chopps 	if (f_noprint == 0)
    239  1.1      jtc 		printf("%s\n", sysbuf);
    240  1.4   chopps 	if (f_noaction == 0)
    241  1.4   chopps 		dosystem(sysbuf);
    242  1.1      jtc }
    243  1.1      jtc 
    244  1.4   chopps void
    245  1.4   chopps dosystem(cmd)
    246  1.4   chopps 	const char *cmd;
    247  1.4   chopps {
    248  1.4   chopps 	int status;
    249  1.4   chopps 
    250  1.4   chopps 	if ((status = system(cmd)) == 0)
    251  1.4   chopps 		return;
    252  1.4   chopps 
    253  1.4   chopps 	if (status == -1)
    254  1.4   chopps 		err(1, "cannot execute action");
    255  1.4   chopps 	if (WIFSIGNALED(status))
    256  1.4   chopps 		errx(1, "child was signaled to quit. aborting");
    257  1.4   chopps 	if (WIFSTOPPED(status))
    258  1.4   chopps 		errx(1, "child was stopped. aborting");
    259  1.4   chopps 	if (f_ignerr == 0)
    260  1.7    lukem 		errx(1, "*** Exited %d", status);
    261  1.7    lukem 	warnx("*** Exited %d (continuing)", status);
    262  1.4   chopps }
    263  1.1      jtc 
    264  1.1      jtc void
    265  1.1      jtc usage()
    266  1.1      jtc {
    267  1.6      cgd 	(void)fprintf(stderr,
    268  1.6      cgd 	    "usage: catman [-knpsw] [-M manpath] [sections]\n");
    269  1.1      jtc 	exit(1);
    270  1.1      jtc }
    271