catman.c revision 1.6 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.6 cgd static char rcsid[] = "$Id: catman.c,v 1.6 1994/04/27 22:37:12 cgd 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.6 cgd #include <dirent.h>
39 1.6 cgd #include <err.h>
40 1.6 cgd #include <errno.h>
41 1.6 cgd #include <limits.h>
42 1.1 jtc #include <stdio.h>
43 1.1 jtc #include <stdlib.h>
44 1.1 jtc #include <string.h>
45 1.1 jtc #include <unistd.h>
46 1.6 cgd #include <paths.h>
47 1.1 jtc
48 1.6 cgd #include "pathnames.h"
49 1.4 chopps
50 1.4 chopps int f_nowhatis;
51 1.4 chopps int f_noaction;
52 1.4 chopps int f_noformat;
53 1.4 chopps int f_ignerr;
54 1.4 chopps int f_noprint;
55 1.4 chopps
56 1.4 chopps int dowhatis;
57 1.4 chopps
58 1.6 cgd char *mp = _PATH_MAN;
59 1.6 cgd char *sp = _MAN_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.6 cgd while ((c = getopt(argc, argv, "knpswM:")) != 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.6 cgd if (argc > 1)
107 1.6 cgd usage();
108 1.6 cgd if (argc == 1)
109 1.6 cgd sp = *argv;
110 1.1 jtc
111 1.5 chopps if (f_noformat == 0 || f_nowhatis == 0)
112 1.4 chopps catman(mp, sp);
113 1.4 chopps if (f_nowhatis == 0 && dowhatis)
114 1.4 chopps makewhatis(mp);
115 1.1 jtc
116 1.1 jtc exit(0);
117 1.1 jtc }
118 1.1 jtc
119 1.1 jtc
120 1.1 jtc void
121 1.1 jtc catman(path, section)
122 1.4 chopps const char *path;
123 1.4 chopps char *section;
124 1.1 jtc {
125 1.4 chopps char mandir[PATH_MAX];
126 1.4 chopps char catdir[PATH_MAX];
127 1.4 chopps char manpage[PATH_MAX];
128 1.4 chopps char catpage[PATH_MAX];
129 1.4 chopps char sysbuf[1024];
130 1.4 chopps struct stat manstat;
131 1.4 chopps struct stat catstat;
132 1.4 chopps struct dirent *dp;
133 1.4 chopps DIR *dirp;
134 1.4 chopps char *s, *tmp;
135 1.5 chopps int sectlen, error;
136 1.4 chopps
137 1.4 chopps for (s = section; *s; s += sectlen) {
138 1.6 cgd #ifdef notdef
139 1.4 chopps tmp = s;
140 1.4 chopps sectlen = 0;
141 1.4 chopps if (isdigit(*tmp)) {
142 1.4 chopps sectlen++;
143 1.4 chopps tmp++;
144 1.4 chopps while (*tmp && isdigit(*tmp) == 0) {
145 1.4 chopps sectlen++;
146 1.4 chopps tmp++;
147 1.4 chopps }
148 1.4 chopps }
149 1.6 cgd #else
150 1.6 cgd sectlen = 1;
151 1.6 cgd #endif
152 1.4 chopps if (sectlen == 0)
153 1.4 chopps errx(1, "malformed section string");
154 1.4 chopps
155 1.6 cgd sprintf(mandir, "%s/%s%.*s", path, _PATH_MANPAGES, sectlen, s);
156 1.6 cgd sprintf(catdir, "%s/%s%.*s", path, _PATH_CATPAGES, sectlen, s);
157 1.4 chopps
158 1.4 chopps if ((dirp = opendir(mandir)) == 0) {
159 1.4 chopps warn("can't open %s", mandir);
160 1.4 chopps continue;
161 1.4 chopps }
162 1.4 chopps
163 1.4 chopps if (stat(catdir, &catstat) < 0) {
164 1.4 chopps if (errno != ENOENT) {
165 1.4 chopps warn("can't stat %s", catdir);
166 1.4 chopps closedir(dirp);
167 1.4 chopps continue;
168 1.4 chopps }
169 1.4 chopps if (f_noprint == 0)
170 1.4 chopps printf("mkdir %s\n", catdir);
171 1.4 chopps if (f_noaction == 0 && mkdir(catdir, 0755) < 0) {
172 1.4 chopps warn("can't create %s", catdir);
173 1.4 chopps closedir(dirp);
174 1.4 chopps return;
175 1.1 jtc }
176 1.1 jtc
177 1.1 jtc }
178 1.1 jtc
179 1.4 chopps while ((dp = readdir(dirp)) != NULL) {
180 1.4 chopps if (strcmp(dp->d_name, ".") == 0 ||
181 1.4 chopps strcmp(dp->d_name, "..") == 0)
182 1.4 chopps continue;
183 1.4 chopps
184 1.4 chopps sprintf(manpage, "%s/%s", mandir, dp->d_name);
185 1.4 chopps sprintf(catpage, "%s/%s", catdir, dp->d_name);
186 1.4 chopps if ((tmp = strrchr(catpage, '.')) != NULL)
187 1.4 chopps strcpy(tmp, ".0");
188 1.4 chopps else
189 1.4 chopps continue;
190 1.4 chopps
191 1.4 chopps if (stat(manpage, &manstat) < 0) {
192 1.4 chopps warn("can't stat %s", manpage);
193 1.4 chopps continue;
194 1.4 chopps }
195 1.4 chopps
196 1.4 chopps if (!S_ISREG(manstat.st_mode)) {
197 1.4 chopps warnx("not a regular file %s", manpage);
198 1.4 chopps continue;
199 1.4 chopps }
200 1.5 chopps if ((error = stat(catpage, &catstat)) &&
201 1.5 chopps errno != ENOENT) {
202 1.4 chopps warn("can't stat %s", catpage);
203 1.4 chopps continue;
204 1.4 chopps }
205 1.4 chopps
206 1.5 chopps if ((error && errno == ENOENT) ||
207 1.4 chopps manstat.st_mtime >= catstat.st_mtime) {
208 1.5 chopps if (f_noformat)
209 1.5 chopps dowhatis = 1;
210 1.5 chopps else {
211 1.5 chopps /*
212 1.5 chopps * manpage is out of date,
213 1.5 chopps * reformat
214 1.5 chopps */
215 1.5 chopps sprintf(sysbuf, "nroff -mandoc %s > %s",
216 1.5 chopps manpage, catpage);
217 1.5 chopps if (f_noprint == 0)
218 1.5 chopps printf("%s\n", sysbuf);
219 1.5 chopps if (f_noaction == 0)
220 1.5 chopps dosystem(sysbuf);
221 1.5 chopps dowhatis = 1;
222 1.5 chopps }
223 1.4 chopps }
224 1.4 chopps }
225 1.4 chopps closedir(dirp);
226 1.1 jtc }
227 1.1 jtc }
228 1.1 jtc
229 1.1 jtc void
230 1.1 jtc makewhatis(path)
231 1.4 chopps const char *path;
232 1.1 jtc {
233 1.4 chopps char sysbuf[1024];
234 1.1 jtc
235 1.6 cgd sprintf(sysbuf, "%s %s", _PATH_MAKEWHATIS, path);
236 1.4 chopps if (f_noprint == 0)
237 1.1 jtc printf("%s\n", sysbuf);
238 1.4 chopps if (f_noaction == 0)
239 1.4 chopps dosystem(sysbuf);
240 1.1 jtc }
241 1.1 jtc
242 1.4 chopps void
243 1.4 chopps dosystem(cmd)
244 1.4 chopps const char *cmd;
245 1.4 chopps {
246 1.4 chopps int status;
247 1.4 chopps
248 1.4 chopps if ((status = system(cmd)) == 0)
249 1.4 chopps return;
250 1.4 chopps
251 1.4 chopps if (status == -1)
252 1.4 chopps err(1, "cannot execute action");
253 1.4 chopps if (WIFSIGNALED(status))
254 1.4 chopps errx(1, "child was signaled to quit. aborting");
255 1.4 chopps if (WIFSTOPPED(status))
256 1.4 chopps errx(1, "child was stopped. aborting");
257 1.4 chopps if (f_ignerr == 0)
258 1.4 chopps errx(1,"*** Exited %d");
259 1.4 chopps warnx("*** Exited %d (continuing)");
260 1.4 chopps }
261 1.1 jtc
262 1.1 jtc void
263 1.1 jtc usage()
264 1.1 jtc {
265 1.6 cgd (void)fprintf(stderr,
266 1.6 cgd "usage: catman [-knpsw] [-M manpath] [sections]\n");
267 1.1 jtc exit(1);
268 1.1 jtc }
269