catman.c revision 1.22 1 /* $NetBSD: catman.c,v 1.22 2003/07/13 12:27:14 itojun Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Baldassare Dante Profeta <dante (at) mclink.it>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
44 #include <sys/utsname.h>
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fnmatch.h>
50 #include <limits.h>
51 #include <libgen.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <glob.h>
57
58 #include "manconf.h"
59 #include "pathnames.h"
60
61 int f_nowhatis = 0;
62 int f_noaction = 0;
63 int f_noformat = 0;
64 int f_ignerr = 0;
65 int f_noprint = 0;
66 int dowhatis = 0;
67
68 TAG *defp; /* pointer to _default list */
69
70 int main __P((int, char * const *));
71 static void setdefentries __P((char *, char *, const char *));
72 static void uniquepath __P((void));
73 static void catman __P((void));
74 static void scanmandir __P((const char *, const char *));
75 static int splitentry __P((char *, char *, size_t, char *, size_t));
76 static void setcatsuffix __P((char *, const char *, const char *));
77 static void makecat __P((const char *, const char *, const char *,
78 const char *));
79 static void makewhatis __P((void));
80 static void dosystem __P((const char *));
81 static void usage __P((void));
82
83
84 int
85 main(argc, argv)
86 int argc;
87 char * const *argv;
88 {
89 char *m_path = NULL;
90 char *m_add = NULL;
91 int c;
92
93 while ((c = getopt(argc, argv, "km:M:npsw")) != -1) {
94 switch (c) {
95 case 'k':
96 f_ignerr = 1;
97 break;
98 case 'n':
99 f_nowhatis = 1;
100 break;
101 case 'p':
102 f_noaction = 1;
103 break;
104 case 's':
105 f_noprint = 1;
106 break;
107 case 'w':
108 f_noformat = 1;
109 break;
110 case 'm':
111 m_add = optarg;
112 break;
113 case 'M':
114 m_path = optarg;
115 break;
116 default:
117 usage();
118 }
119 }
120
121 argc -= optind;
122 argv += optind;
123
124 if (f_noprint && f_noaction)
125 f_noprint = 0;
126
127 if (argc > 1)
128 usage();
129
130 config(_PATH_MANCONF);
131 setdefentries(m_path, m_add, (argc == 0) ? NULL : argv[argc-1]);
132 uniquepath();
133
134 if (f_noformat == 0 || f_nowhatis == 0)
135 catman();
136 if (f_nowhatis == 0 && dowhatis)
137 makewhatis();
138
139 return(0);
140 }
141
142 static void
143 setdefentries(m_path, m_add, sections)
144 char *m_path;
145 char *m_add;
146 const char *sections;
147 {
148 TAG *defnewp, *sectnewp, *subp;
149 ENTRY *e_defp, *e_subp;
150 const char *p;
151 char *slashp, *machine;
152 char buf[MAXPATHLEN * 2];
153 int i;
154
155 /* Get the machine type. */
156 if ((machine = getenv("MACHINE")) == NULL) {
157 struct utsname utsname;
158
159 if (uname(&utsname) == -1) {
160 perror("uname");
161 exit(1);
162 }
163 machine = utsname.machine;
164 }
165
166 /* If there's no _default list, create an empty one. */
167 defp = getlist("_default", 1);
168
169 subp = getlist("_subdir", 1);
170
171 /*
172 * 0: If one or more sections was specified, rewrite _subdir list.
173 */
174 if (sections != NULL) {
175 sectnewp = getlist("_section_new", 1);
176 for (p = sections; *p;) {
177 i = snprintf(buf, sizeof(buf), "man%c", *p++);
178 for (; *p && !isdigit(*p) && i < sizeof(buf) - 1; i++)
179 buf[i] = *p++;
180 buf[i] = '\0';
181 addentry(sectnewp, buf, 0);
182 }
183 subp = sectnewp;
184 }
185
186 /*
187 * 1: If the user specified a MANPATH variable, or set the -M
188 * option, we replace the _default list with the user's list,
189 * appending the entries in the _subdir list and the machine.
190 */
191 if (m_path == NULL)
192 m_path = getenv("MANPATH");
193 if (m_path != NULL) {
194 while ((e_defp = TAILQ_FIRST(&defp->list)) != NULL) {
195 free(e_defp->s);
196 TAILQ_REMOVE(&defp->list, e_defp, q);
197 }
198 for (p = strtok(m_path, ":");
199 p != NULL; p = strtok(NULL, ":")) {
200 slashp = p[strlen(p) - 1] == '/' ? "" : "/";
201 TAILQ_FOREACH(e_subp, &subp->list, q) {
202 if (!strncmp(e_subp->s, "cat", 3))
203 continue;
204 (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
205 p, slashp, e_subp->s, machine);
206 addentry(defp, buf, 0);
207 }
208 }
209 }
210
211 /*
212 * 2: If the user did not specify MANPATH, -M or a section, rewrite
213 * the _default list to include the _subdir list and the machine.
214 */
215 if (m_path == NULL) {
216 defp = getlist("_default", 1);
217 defnewp = getlist("_default_new1", 1);
218
219 TAILQ_FOREACH(e_defp, &defp->list, q) {
220 slashp =
221 e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
222 TAILQ_FOREACH(e_subp, &subp->list, q) {
223 if (!strncmp(e_subp->s, "cat", 3))
224 continue;
225 (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
226 e_defp->s, slashp, e_subp->s, machine);
227 addentry(defnewp, buf, 0);
228 }
229 }
230 defp = defnewp;
231 }
232
233 /*
234 * 3: If the user set the -m option, insert the user's list before
235 * whatever list we have, again appending the _subdir list and
236 * the machine.
237 */
238 if (m_add != NULL)
239 for (p = strtok(m_add, ":"); p != NULL; p = strtok(NULL, ":")) {
240 slashp = p[strlen(p) - 1] == '/' ? "" : "/";
241 TAILQ_FOREACH(e_subp, &subp->list, q) {
242 if (!strncmp(e_subp->s, "cat", 3))
243 continue;
244 (void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
245 p, slashp, e_subp->s, machine);
246 addentry(defp, buf, 1);
247 }
248 }
249 }
250
251 /*
252 * Remove entries (directory) which are symbolic links to other entries.
253 * Some examples are showed below:
254 * 1) if /usr/X11 -> /usr/X11R6 then remove all /usr/X11/man entries.
255 * 2) if /usr/local/man -> /usr/share/man then remove all /usr/local/man
256 * entries
257 */
258 static void
259 uniquepath(void)
260 {
261 TAG *defnewp;
262 ENTRY *e_defp;
263 glob_t manpaths;
264 struct stat st1;
265 struct stat st2;
266 struct stat st3;
267 int i, j, len, lnk, gflags;
268 char path[PATH_MAX], *p;
269
270 gflags = 0;
271 TAILQ_FOREACH(e_defp, &defp->list, q) {
272 glob(e_defp->s, GLOB_BRACE | GLOB_NOSORT | gflags, NULL,
273 &manpaths);
274 gflags = GLOB_APPEND;
275 }
276
277 defnewp = getlist("_default_new2", 1);
278
279 for (i = 0; i < manpaths.gl_pathc; i++) {
280 lnk = 0;
281 lstat(manpaths.gl_pathv[i], &st1);
282 for (j = 0; j < manpaths.gl_pathc; j++) {
283 if (i != j) {
284 lstat(manpaths.gl_pathv[j], &st2);
285 if (st1.st_ino == st2.st_ino) {
286 strlcpy(path, manpaths.gl_pathv[i],
287 sizeof(path));
288 for (p = path; *(p+1) != '\0';) {
289 p = dirname(p);
290 lstat(p, &st3);
291 if (S_ISLNK(st3.st_mode)) {
292 lnk = 1;
293 break;
294 }
295 }
296 } else {
297 len = readlink(manpaths.gl_pathv[i],
298 path, sizeof(path) - 1);
299 if (len == -1)
300 continue;
301 path[len] = '\0';
302 if (!strcmp(path, manpaths.gl_pathv[j]))
303 lnk = 1;
304 }
305 if (lnk)
306 break;
307 }
308 }
309
310 if (!lnk)
311 addentry(defnewp, manpaths.gl_pathv[i], 0);
312 }
313
314 globfree(&manpaths);
315
316 defp = defnewp;
317 }
318
319 static void
320 catman(void)
321 {
322 ENTRY *e_path;
323 const char *mandir;
324 char catdir[PATH_MAX], *cp;
325
326 TAILQ_FOREACH(e_path, &defp->list, q) {
327 mandir = e_path->s;
328 strlcpy(catdir, mandir, sizeof(catdir));
329 if (!(cp = strstr(catdir, "man/man")))
330 continue;
331 cp += 4; *cp++ = 'c'; *cp++ = 'a'; *cp = 't';
332 scanmandir(catdir, mandir);
333 }
334 }
335
336 static void
337 scanmandir(catdir, mandir)
338 const char *catdir;
339 const char *mandir;
340 {
341 TAG *buildp, *crunchp;
342 ENTRY *e_build, *e_crunch;
343 char manpage[PATH_MAX];
344 char catpage[PATH_MAX];
345 char linkname[PATH_MAX];
346 char buffer[PATH_MAX], *bp;
347 char tmp[PATH_MAX];
348 char buildsuff[256], buildcmd[256];
349 char crunchsuff[256], crunchcmd[256];
350 char match[256];
351 struct stat manstat;
352 struct stat catstat;
353 struct stat lnkstat;
354 struct dirent *dp;
355 DIR *dirp;
356 int len, error;
357
358 if ((dirp = opendir(mandir)) == 0) {
359 warn("can't open %s", mandir);
360 return;
361 }
362
363 if (stat(catdir, &catstat) < 0) {
364 if (errno != ENOENT) {
365 warn("can't stat %s", catdir);
366 closedir(dirp);
367 return;
368 }
369 if (f_noprint == 0)
370 printf("mkdir %s\n", catdir);
371 if (f_noaction == 0 && mkdir(catdir, 0755) < 0) {
372 warn("can't create %s", catdir);
373 closedir(dirp);
374 return;
375 }
376 }
377
378 while ((dp = readdir(dirp)) != NULL) {
379 if (strcmp(dp->d_name, ".") == 0 ||
380 strcmp(dp->d_name, "..") == 0)
381 continue;
382
383 snprintf(manpage, sizeof(manpage), "%s/%s", mandir, dp->d_name);
384 snprintf(catpage, sizeof(catpage), "%s/%s", catdir, dp->d_name);
385
386 e_build = NULL;
387 buildp = getlist("_build", 1);
388 TAILQ_FOREACH(e_build, &buildp->list, q) {
389 splitentry(e_build->s, buildsuff, sizeof(buildsuff),
390 buildcmd, sizeof(buildcmd));
391 snprintf(match, sizeof(match), "*%s",
392 buildsuff);
393 if (!fnmatch(match, manpage, 0))
394 break;
395 }
396
397 if (e_build == NULL)
398 continue;
399
400 e_crunch = NULL;
401 crunchp = getlist("_crunch", 1);
402 TAILQ_FOREACH(e_crunch, &crunchp->list, q) {
403 splitentry(e_crunch->s, crunchsuff, sizeof(crunchsuff),
404 crunchcmd, sizeof(crunchcmd));
405 snprintf(match, sizeof(match), "*%s", crunchsuff);
406 if (!fnmatch(match, manpage, 0))
407 break;
408 }
409
410 if (lstat(manpage, &manstat) <0) {
411 warn("can't stat %s", manpage);
412 continue;
413 } else {
414 if (S_ISLNK(manstat.st_mode)) {
415 strlcpy(buffer, catpage, sizeof(buffer));
416 strlcpy(linkname, basename(buffer),
417 sizeof(linkname));
418 len = readlink(manpage, buffer,
419 sizeof(buffer) - 1);
420 buffer[PATH_MAX - 1] = '\0';
421 if (len == -1) {
422 warn("can't stat read symbolic link %s",
423 manpage);
424 continue;
425 }
426 buffer[len] = '\0';
427 bp = basename(buffer);
428 strlcpy(tmp, manpage, sizeof(tmp));
429 snprintf(manpage, sizeof(manpage), "%s/%s",
430 dirname(tmp), bp);
431 strlcpy(tmp, catpage, sizeof(tmp));
432 snprintf(catpage, sizeof(catpage), "%s/%s",
433 dirname(tmp), buffer);
434 }
435 else
436 *linkname = '\0';
437 }
438
439 if (!e_crunch) {
440 *crunchsuff = *crunchcmd = '\0';
441 }
442 setcatsuffix(catpage, buildsuff, crunchsuff);
443 if (*linkname != '\0')
444 setcatsuffix(linkname, buildsuff, crunchsuff);
445
446 if (stat(manpage, &manstat) < 0) {
447 warn("can't stat %s", manpage);
448 continue;
449 }
450
451 if (!S_ISREG(manstat.st_mode)) {
452 warnx("not a regular file %s", manpage);
453 continue;
454 }
455
456 if ((error = stat(catpage, &catstat)) &&
457 errno != ENOENT) {
458 warn("can't stat %s", catpage);
459 continue;
460 }
461
462 if ((error && errno == ENOENT) ||
463 manstat.st_mtime > catstat.st_mtime) {
464 if (f_noformat) {
465 dowhatis = 1;
466 } else {
467 /*
468 * reformat out of date manpage
469 */
470 makecat(manpage, catpage, buildcmd, crunchcmd);
471 dowhatis = 1;
472 }
473 }
474
475 if (*linkname != '\0') {
476 strlcpy(tmp, catpage, sizeof(tmp));
477 snprintf(tmp, sizeof(tmp), "%s/%s", dirname(tmp),
478 linkname);
479 if ((error = lstat(tmp, &lnkstat)) &&
480 errno != ENOENT) {
481 warn("can't stat %s", tmp);
482 continue;
483 }
484
485 if (error && errno == ENOENT) {
486 if (f_noformat) {
487 dowhatis = 1;
488 } else {
489 /*
490 * create symbolic link
491 */
492 if (f_noprint == 0)
493 printf("ln -s %s %s\n", catpage,
494 linkname);
495 if (f_noaction == 0) {
496 strlcpy(tmp, catpage,
497 sizeof(tmp));
498 if (chdir(dirname(tmp)) == -1) {
499 warn("can't chdir");
500 continue;
501 }
502
503 if (symlink(catpage, linkname)
504 == -1) {
505 warn("can't create"
506 " symbolic"
507 " link %s",
508 linkname);
509 continue;
510 }
511 }
512 dowhatis = 1;
513 }
514 }
515 }
516 }
517 closedir(dirp);
518 }
519
520 static int
521 splitentry(s, first, firstlen, second, secondlen)
522 char *s;
523 char *first;
524 size_t firstlen;
525 char *second;
526 size_t secondlen;
527 {
528 char *c;
529
530 for (c = s; *c != '\0' && !isspace(*c); ++c)
531 ;
532 if (*c == '\0')
533 return(0);
534 if (c - s + 1 > firstlen)
535 return(0);
536 strncpy(first, s, c-s);
537 first[c-s] = '\0';
538 for (; *c != '\0' && isspace(*c); ++c)
539 ;
540 if (strlcpy(second, c, secondlen) >= secondlen)
541 return(0);
542 return(1);
543 }
544
545 static void
546 setcatsuffix(catpage, suffix, crunchsuff)
547 char *catpage;
548 const char *suffix;
549 const char *crunchsuff;
550 {
551 TAG *tp;
552 char *p;
553
554 for (p = catpage + strlen(catpage); p != catpage; p--)
555 if (!fnmatch(suffix, p, 0)) {
556 tp = getlist("_suffix", 1);
557 if (! TAILQ_EMPTY(&tp->list)) {
558 sprintf(p, "%s%s",
559 TAILQ_FIRST(&tp->list)->s, crunchsuff);
560 } else {
561 sprintf(p, ".0%s", crunchsuff);
562 }
563 break;
564 }
565 }
566
567 static void
568 makecat(manpage, catpage, buildcmd, crunchcmd)
569 const char *manpage;
570 const char *catpage;
571 const char *buildcmd;
572 const char *crunchcmd;
573 {
574 char crunchbuf[1024];
575 char sysbuf[2048];
576
577 snprintf(sysbuf, sizeof(sysbuf), buildcmd, manpage);
578
579 if (*crunchcmd != '\0') {
580 snprintf(crunchbuf, sizeof(crunchbuf), crunchcmd, catpage);
581 snprintf(sysbuf, sizeof(sysbuf), "%s | %s", sysbuf, crunchbuf);
582 } else {
583 snprintf(sysbuf, sizeof(sysbuf), "%s > %s", sysbuf, catpage);
584 }
585
586 if (f_noprint == 0)
587 printf("%s\n", sysbuf);
588 if (f_noaction == 0)
589 dosystem(sysbuf);
590 }
591
592 static void
593 makewhatis(void)
594 {
595 TAG *whatdbp;
596 ENTRY *e_whatdb;
597 char sysbuf[1024];
598
599 whatdbp = getlist("_whatdb", 1);
600 TAILQ_FOREACH(e_whatdb, &whatdbp->list, q) {
601 snprintf(sysbuf, sizeof(sysbuf), "%s %s",
602 _PATH_WHATIS, dirname(e_whatdb->s));
603 if (f_noprint == 0)
604 printf("%s\n", sysbuf);
605 if (f_noaction == 0)
606 dosystem(sysbuf);
607 }
608 }
609
610 static void
611 dosystem(cmd)
612 const char *cmd;
613 {
614 int status;
615
616 if ((status = system(cmd)) == 0)
617 return;
618
619 if (status == -1)
620 err(1, "cannot execute action");
621 if (WIFSIGNALED(status))
622 errx(1, "child was signaled to quit. aborting");
623 if (WIFSTOPPED(status))
624 errx(1, "child was stopped. aborting");
625 if (f_ignerr == 0)
626 errx(1, "*** Exited %d", status);
627 warnx("*** Exited %d (continuing)", status);
628 }
629
630 static void
631 usage(void)
632 {
633 (void)fprintf(stderr,
634 "usage: catman [-knpsw] [-m manpath] [sections]\n");
635 (void)fprintf(stderr,
636 " catman [-knpsw] [-M manpath] [sections]\n");
637 exit(1);
638 }
639