Home | History | Annotate | Line # | Download | only in catman
catman.c revision 1.5
      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.2  mycroft #ifndef lint
     32  1.4   chopps static char rcsid[] = "$Id: catman.c,v 1.5 1994/04/26 20:39:22 chopps Exp $";
     33  1.2  mycroft #endif /* not lint */
     34  1.1      jtc 
     35  1.4   chopps #include <sys/types.h>
     36  1.4   chopps #include <sys/stat.h>
     37  1.4   chopps #include <sys/wait.h>
     38  1.1      jtc #include <stdio.h>
     39  1.1      jtc #include <stdlib.h>
     40  1.1      jtc #include <string.h>
     41  1.1      jtc #include <dirent.h>
     42  1.1      jtc #include <unistd.h>
     43  1.1      jtc #include <limits.h>
     44  1.1      jtc #include <errno.h>
     45  1.1      jtc #include <err.h>
     46  1.1      jtc 
     47  1.4   chopps 
     48  1.4   chopps int f_nowhatis;
     49  1.4   chopps int f_noaction;
     50  1.4   chopps int f_noformat;
     51  1.4   chopps int f_ignerr;
     52  1.4   chopps int f_noprint;
     53  1.4   chopps 
     54  1.4   chopps int dowhatis;
     55  1.4   chopps 
     56  1.4   chopps char manpath[] = "/usr/share/man";
     57  1.4   chopps char sections[] = "12345678lnop";
     58  1.4   chopps char *mp = manpath;
     59  1.4   chopps char *sp = sections;
     60  1.4   chopps 
     61  1.4   chopps void usage __P((void));
     62  1.4   chopps void catman __P((const char *, char *));
     63  1.4   chopps void makewhatis __P((const char *));
     64  1.4   chopps void dosystem __P((const char *));
     65  1.1      jtc 
     66  1.1      jtc int
     67  1.1      jtc main(argc, argv)
     68  1.4   chopps 	int argc;
     69  1.4   chopps 	char **argv;
     70  1.1      jtc {
     71  1.4   chopps 	int c;
     72  1.1      jtc 
     73  1.4   chopps 	while ((c = getopt(argc, argv, "knpwM:")) != EOF) {
     74  1.1      jtc 		switch (c) {
     75  1.4   chopps 		case 'k':
     76  1.4   chopps 			f_ignerr = 1;
     77  1.4   chopps 			break;
     78  1.1      jtc 		case 'n':
     79  1.4   chopps 			f_nowhatis = 1;
     80  1.1      jtc 			break;
     81  1.1      jtc 		case 'p':
     82  1.4   chopps 			f_noaction = 1;
     83  1.4   chopps 			break;
     84  1.4   chopps 		case 's':
     85  1.4   chopps 			f_noprint = 1;
     86  1.1      jtc 			break;
     87  1.1      jtc 		case 'w':
     88  1.4   chopps 			f_noformat = 1;
     89  1.1      jtc 			break;
     90  1.1      jtc 		case 'M':
     91  1.1      jtc 			mp = optarg;
     92  1.1      jtc 			break;
     93  1.1      jtc 
     94  1.1      jtc 		case '?':
     95  1.1      jtc 		default:
     96  1.1      jtc 			usage();
     97  1.1      jtc 		}
     98  1.1      jtc 	}
     99  1.1      jtc 
    100  1.1      jtc 	argc -= optind;
    101  1.1      jtc 	argv += optind;
    102  1.1      jtc 
    103  1.4   chopps 	if (f_noprint && f_noaction)
    104  1.4   chopps 		f_noprint = 0;
    105  1.4   chopps 
    106  1.4   chopps 	if (argc == 0)
    107  1.1      jtc 		usage ();
    108  1.4   chopps 	sp = *argv;
    109  1.1      jtc 
    110  1.5   chopps 	if (f_noformat == 0 || f_nowhatis == 0)
    111  1.4   chopps 		catman(mp, sp);
    112  1.4   chopps 	if (f_nowhatis == 0 && dowhatis)
    113  1.4   chopps 		makewhatis(mp);
    114  1.1      jtc 
    115  1.1      jtc 	exit(0);
    116  1.1      jtc }
    117  1.1      jtc 
    118  1.1      jtc 
    119  1.1      jtc void
    120  1.1      jtc catman(path, section)
    121  1.4   chopps 	const char *path;
    122  1.4   chopps 	char *section;
    123  1.1      jtc {
    124  1.4   chopps 	char mandir[PATH_MAX];
    125  1.4   chopps 	char catdir[PATH_MAX];
    126  1.4   chopps 	char manpage[PATH_MAX];
    127  1.4   chopps 	char catpage[PATH_MAX];
    128  1.4   chopps 	char sysbuf[1024];
    129  1.4   chopps 	struct stat manstat;
    130  1.4   chopps 	struct stat catstat;
    131  1.4   chopps 	struct dirent *dp;
    132  1.4   chopps 	DIR *dirp;
    133  1.4   chopps 	char *s, *tmp;
    134  1.5   chopps 	int sectlen, error;
    135  1.4   chopps 
    136  1.4   chopps 	for (s = section; *s; s += sectlen) {
    137  1.4   chopps 		tmp = s;
    138  1.4   chopps 		sectlen = 0;
    139  1.4   chopps 		if (isdigit(*tmp)) {
    140  1.4   chopps 			sectlen++;
    141  1.4   chopps 			tmp++;
    142  1.4   chopps 			while (*tmp && isdigit(*tmp) == 0) {
    143  1.4   chopps 				sectlen++;
    144  1.4   chopps 				tmp++;
    145  1.4   chopps 			}
    146  1.4   chopps 		}
    147  1.4   chopps 		if (sectlen == 0)
    148  1.4   chopps 			errx(1, "malformed section string");
    149  1.4   chopps 
    150  1.4   chopps 		sprintf(mandir, "%s/man%.*s", path, sectlen, s);
    151  1.4   chopps 		sprintf(catdir, "%s/cat%.*s", path, sectlen, s);
    152  1.4   chopps 
    153  1.4   chopps 		if ((dirp = opendir(mandir)) == 0) {
    154  1.4   chopps 			warn("can't open %s", mandir);
    155  1.4   chopps 			continue;
    156  1.4   chopps 		}
    157  1.4   chopps 
    158  1.4   chopps 		if (stat(catdir, &catstat) < 0) {
    159  1.4   chopps 			if (errno != ENOENT) {
    160  1.4   chopps 				warn("can't stat %s", catdir);
    161  1.4   chopps 				closedir(dirp);
    162  1.4   chopps 				continue;
    163  1.4   chopps 			}
    164  1.4   chopps 			if (f_noprint == 0)
    165  1.4   chopps 				printf("mkdir %s\n", catdir);
    166  1.4   chopps 			if (f_noaction == 0 && mkdir(catdir, 0755) < 0) {
    167  1.4   chopps 				warn("can't create %s", catdir);
    168  1.4   chopps 				closedir(dirp);
    169  1.4   chopps 				return;
    170  1.1      jtc 			}
    171  1.1      jtc 
    172  1.1      jtc 		}
    173  1.1      jtc 
    174  1.4   chopps 		while ((dp = readdir(dirp)) != NULL) {
    175  1.4   chopps 			if (strcmp(dp->d_name, ".") == 0 ||
    176  1.4   chopps 			    strcmp(dp->d_name, "..") == 0)
    177  1.4   chopps 				continue;
    178  1.4   chopps 
    179  1.4   chopps 			sprintf(manpage, "%s/%s", mandir, dp->d_name);
    180  1.4   chopps 			sprintf(catpage, "%s/%s", catdir, dp->d_name);
    181  1.4   chopps 			if ((tmp = strrchr(catpage, '.')) != NULL)
    182  1.4   chopps 				strcpy(tmp, ".0");
    183  1.4   chopps 			else
    184  1.4   chopps 				continue;
    185  1.4   chopps 
    186  1.4   chopps 			if (stat(manpage, &manstat) < 0) {
    187  1.4   chopps 				warn("can't stat %s", manpage);
    188  1.4   chopps 				continue;
    189  1.4   chopps 			}
    190  1.4   chopps 
    191  1.4   chopps 			if (!S_ISREG(manstat.st_mode)) {
    192  1.4   chopps 				warnx("not a regular file %s", manpage);
    193  1.4   chopps 				continue;
    194  1.4   chopps 			}
    195  1.5   chopps 			if ((error = stat(catpage, &catstat)) &&
    196  1.5   chopps 			    errno != ENOENT) {
    197  1.4   chopps 				warn("can't stat %s", catpage);
    198  1.4   chopps 				continue;
    199  1.4   chopps 			}
    200  1.4   chopps 
    201  1.5   chopps 			if ((error && errno == ENOENT) ||
    202  1.4   chopps 			    manstat.st_mtime >= catstat.st_mtime) {
    203  1.5   chopps 				if (f_noformat)
    204  1.5   chopps 					dowhatis = 1;
    205  1.5   chopps 				else {
    206  1.5   chopps 					/*
    207  1.5   chopps 					 * manpage is out of date,
    208  1.5   chopps 					 * reformat
    209  1.5   chopps 					 */
    210  1.5   chopps 					sprintf(sysbuf, "nroff -mandoc %s > %s",
    211  1.5   chopps 					    manpage, catpage);
    212  1.5   chopps 					if (f_noprint == 0)
    213  1.5   chopps 						printf("%s\n", sysbuf);
    214  1.5   chopps 					if (f_noaction == 0)
    215  1.5   chopps 						dosystem(sysbuf);
    216  1.5   chopps 					dowhatis = 1;
    217  1.5   chopps 				}
    218  1.4   chopps 			}
    219  1.4   chopps 		}
    220  1.4   chopps 		closedir(dirp);
    221  1.1      jtc 	}
    222  1.1      jtc }
    223  1.1      jtc 
    224  1.1      jtc void
    225  1.1      jtc makewhatis(path)
    226  1.4   chopps 	const char *path;
    227  1.1      jtc {
    228  1.4   chopps 	char sysbuf[1024];
    229  1.1      jtc 
    230  1.1      jtc 	sprintf(sysbuf, "/usr/libexec/makewhatis %s", path);
    231  1.4   chopps 	if (f_noprint == 0)
    232  1.1      jtc 		printf("%s\n", sysbuf);
    233  1.4   chopps 	if (f_noaction == 0)
    234  1.4   chopps 		dosystem(sysbuf);
    235  1.1      jtc }
    236  1.1      jtc 
    237  1.4   chopps void
    238  1.4   chopps dosystem(cmd)
    239  1.4   chopps 	const char *cmd;
    240  1.4   chopps {
    241  1.4   chopps 	int status;
    242  1.4   chopps 
    243  1.4   chopps 	if ((status = system(cmd)) == 0)
    244  1.4   chopps 		return;
    245  1.4   chopps 
    246  1.4   chopps 	if (status == -1)
    247  1.4   chopps 		err(1, "cannot execute action");
    248  1.4   chopps 	if (WIFSIGNALED(status))
    249  1.4   chopps 		errx(1, "child was signaled to quit. aborting");
    250  1.4   chopps 	if (WIFSTOPPED(status))
    251  1.4   chopps 		errx(1, "child was stopped. aborting");
    252  1.4   chopps 	if (f_ignerr == 0)
    253  1.4   chopps 		errx(1,"*** Exited %d");
    254  1.4   chopps 	warnx("*** Exited %d (continuing)");
    255  1.4   chopps }
    256  1.1      jtc 
    257  1.1      jtc void
    258  1.1      jtc usage()
    259  1.1      jtc {
    260  1.4   chopps 	(void)fprintf(stderr, "usage: catman [-knpw] [-M manpath]"
    261  1.4   chopps 	    " [sections]\n");
    262  1.1      jtc 	exit(1);
    263  1.1      jtc }
    264