2 * Copyright (c) 2002 John Rochester
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/usr.bin/catman/catman.c,v 1.9 2003/06/10 02:18:00 ache Exp $
31 #include <sys/types.h>
33 #include <sys/param.h>
47 #define DEFAULT_MANPATH "/usr/share/man"
49 #define TOP_LEVEL_DIR 0 /* signifies a top-level man directory */
50 #define MAN_SECTION_DIR 1 /* signifies a man section directory */
51 #define UNKNOWN 2 /* signifies an unclassifiable directory */
53 #define TEST_EXISTS 0x01
55 #define TEST_FILE 0x04
56 #define TEST_READABLE 0x08
57 #define TEST_WRITABLE 0x10
58 #define TEST_EXECUTABLE 0x20
60 static int verbose; /* -v flag: be verbose with warnings */
61 static int pretend; /* -n, -p flags: print out what would be done
62 instead of actually doing it */
63 static int force; /* -f flag: force overwriting all cat pages */
64 static int rm_junk; /* -r flag: remove garbage pages */
65 static char *locale; /* user's locale if -L is used */
66 static char *lang_locale; /* short form of locale */
67 static const char *machine;
68 static int exit_code; /* exit code to use when finished */
71 * -T argument for nroff
73 static const char *nroff_device = "ascii";
76 * Mapping from locale to nroff device
78 static const char *locale_device[] = {
80 "ISO8859-1", "latin1",
81 "ISO8859-15", "latin1",
85 #define BZ2_CMD "bzip2"
86 #define BZ2_EXT ".bz2"
87 #define BZ2CAT_CMD "bz"
91 enum Ziptype {NONE, BZIP, GZIP};
94 static gid_t gids[NGROUPS_MAX];
96 static int starting_dir;
97 static char tmp_file[MAXPATHLEN];
101 * A hashtable is an array of chains composed of this entry structure.
107 struct hash_entry *next;
110 #define HASHTABLE_ALLOC 16384 /* allocation for hashtable (power of 2) */
111 #define HASH_MASK (HASHTABLE_ALLOC - 1)
113 static struct hash_entry *visited[HASHTABLE_ALLOC];
114 static struct hash_entry *links[HASHTABLE_ALLOC];
117 * Inserts a string into a hashtable keyed by inode & device number.
120 insert_hashtable(struct hash_entry **table, ino_t inode_number,
121 dev_t device_number, const char *data)
123 struct hash_entry *new_entry;
124 struct hash_entry **chain;
126 new_entry = malloc(sizeof(struct hash_entry));
127 if (new_entry == NULL)
128 err(1, "can't insert into hashtable");
129 chain = &table[inode_number & HASH_MASK];
130 new_entry->inode_number = inode_number;
131 new_entry->device_number = device_number;
132 new_entry->data = data;
133 new_entry->next = *chain;
138 * Finds a string in a hashtable keyed by inode & device number.
141 find_hashtable(struct hash_entry **table, ino_t inode_number,
144 struct hash_entry *chain;
146 chain = table[inode_number & HASH_MASK];
147 while (chain != NULL) {
148 if (chain->inode_number == inode_number &&
149 chain->device_number == device_number)
157 trap_signal(int sig __unused)
159 if (tmp_file[0] != '\0')
165 * Deals with junk files in the man or cat section directories.
168 junk(const char *mandir, const char *name, const char *reason)
171 fprintf(stderr, "%s/%s: %s\n", mandir, name, reason);
173 fprintf(stderr, "rm %s/%s\n", mandir, name);
174 if (!pretend && unlink(name) < 0)
175 warn("%s/%s", mandir, name);
180 * Returns TOP_LEVEL_DIR for .../man, MAN_SECTION_DIR for .../manXXX,
181 * and UNKNOWN for everything else.
184 directory_type(char *dir)
189 p = strrchr(dir, '/');
190 if (p == NULL || p[1] != '\0')
198 if (strncmp(p, "man", 3) == 0) {
201 return TOP_LEVEL_DIR;
202 while (isalnum((unsigned char)*p) || *p == '_') {
204 return(MAN_SECTION_DIR);
211 * Tests whether the given file name (without a preceding path)
212 * is a proper man page name (like "mk-amd-map.8.gz").
213 * Only alphanumerics and '_' are allowed after the last '.' and
214 * the last '.' can't be the first or last characters.
217 is_manpage_name(char *name)
219 char *lastdot = NULL;
222 for (n = name; *n != '\0'; n++) {
241 return(lastdot > name && lastdot + 1 < n);
245 is_bzipped(char *name)
247 int len = strlen(name);
248 return(len >= 5 && strcmp(&name[len - 4], BZ2_EXT) == 0);
252 is_gzipped(char *name)
254 int len = strlen(name);
255 return(len >= 4 && strcmp(&name[len - 3], GZ_EXT) == 0);
259 * Converts manXXX to catXXX.
262 get_cat_section(char *section)
266 cat_section = strdup(section);
267 strncpy(cat_section, "cat", 3);
272 * Tests to see if the given directory has already been visited.
275 already_visited(char *mandir, char *dir, int count_visit)
279 if (stat(dir, &st) < 0) {
281 warn("%s/%s", mandir, dir);
287 if (find_hashtable(visited, st.st_ino, st.st_dev) != NULL) {
289 warnx("already visited %s/%s", mandir, dir);
291 warnx("already visited %s", dir);
295 insert_hashtable(visited, st.st_ino, st.st_dev, "");
300 * Returns a set of TEST_* bits describing a file's type and permissions.
301 * If mod_time isn't NULL, it will contain the file's modification time.
304 test_path(char *name, time_t *mod_time)
308 if (stat(name, &test_st) < 0)
310 result = TEST_EXISTS;
311 if (mod_time != NULL)
312 *mod_time = test_st.st_mtime;
313 if (S_ISDIR(test_st.st_mode))
315 else if (S_ISREG(test_st.st_mode))
317 if (test_st.st_uid == uid) {
318 test_st.st_mode >>= 6;
321 for (i = 0; i < ngids; i++) {
322 if (test_st.st_gid == gids[i]) {
323 test_st.st_mode >>= 3;
328 if (test_st.st_mode & S_IROTH)
329 result |= TEST_READABLE;
330 if (test_st.st_mode & S_IWOTH)
331 result |= TEST_WRITABLE;
332 if (test_st.st_mode & S_IXOTH)
333 result |= TEST_EXECUTABLE;
338 * Checks whether a file is a symbolic link.
341 is_symlink(char *path)
345 return(lstat(path, &st) >= 0 && S_ISLNK(st.st_mode));
349 * Tests to see if the given directory can be written to.
352 check_writable(char *mandir)
354 if (verbose && !(test_path(mandir, NULL) & TEST_WRITABLE))
355 fprintf(stderr, "%s: not writable - will only be able to write "
356 "to existing cat directories\n", mandir);
360 * If the directory exists, attempt to make it writable, otherwise
361 * attempt to create it.
364 make_writable_dir(char *mandir, char *dir)
368 if ((test = test_path(dir, NULL)) != 0) {
369 if (!(test & TEST_WRITABLE) && chmod(dir, 0755) < 0) {
370 warn("%s/%s: chmod", mandir, dir);
375 if (verbose || pretend)
376 fprintf(stderr, "mkdir %s\n", dir);
379 if (mkdir(dir, 0755) < 0) {
380 warn("%s/%s: mkdir", mandir, dir);
390 * Processes a single man page source by using nroff to create
391 * the preformatted cat page.
394 process_page(char *mandir, char *src, char *cat, enum Ziptype zipped)
396 int src_test, cat_test;
397 time_t src_mtime, cat_mtime;
398 char cmd[MAXPATHLEN];
401 const char *link_name;
403 src_test = test_path(src, &src_mtime);
404 if (!(src_test & (TEST_FILE|TEST_READABLE))) {
405 if (!(src_test & TEST_DIR)) {
406 warnx("%s/%s: unreadable", mandir, src);
408 if (rm_junk && is_symlink(src))
409 junk(mandir, src, "bogus symlink");
413 src_dev = test_st.st_dev;
414 src_ino = test_st.st_ino;
415 cat_test = test_path(cat, &cat_mtime);
416 if (cat_test & (TEST_FILE|TEST_READABLE)) {
417 if (!force && cat_mtime >= src_mtime) {
419 fprintf(stderr, "\t%s/%s: up to date\n",
425 * Is the man page a link to one we've already processed?
427 if ((link_name = find_hashtable(links, src_ino, src_dev)) != NULL) {
428 if (verbose || pretend)
429 fprintf(stderr, "%slink %s -> %s\n",
430 verbose ? "\t" : "", cat, link_name);
432 link(link_name, cat);
435 insert_hashtable(links, src_ino, src_dev, strdup(cat));
436 if (verbose || pretend) {
437 fprintf(stderr, "%sformat %s -> %s\n",
438 verbose ? "\t" : "", src, cat);
442 snprintf(tmp_file, sizeof tmp_file, "%s.tmp", cat);
443 snprintf(cmd, sizeof cmd,
444 "%scat %s | tbl | nroff -T%s -man | col | %s > %s.tmp",
445 zipped == BZIP ? BZ2CAT_CMD : zipped == GZIP ? GZCAT_CMD : "",
447 zipped == BZIP ? BZ2_CMD : zipped == GZIP ? GZ_CMD : "cat",
449 if (system(cmd) != 0)
450 err(1, "formatting pipeline");
451 if (rename(tmp_file, cat) < 0)
457 * Scan the man section directory for pages and process each one,
458 * then check for junk in the corresponding cat section.
461 scan_section(char *mandir, char *section, char *cat_section)
463 struct dirent **entries;
464 char **expected = NULL;
470 char page_path[MAXPATHLEN];
471 char cat_path[MAXPATHLEN];
472 char zip_path[MAXPATHLEN];
475 * scan the man section directory for pages
477 npages = scandir(section, &entries, NULL, alphasort);
479 warn("%s/%s", mandir, section);
483 if (verbose || rm_junk) {
485 * Maintain a list of all cat pages that should exist,
486 * corresponding to existing man pages.
488 expected = (char **) calloc(npages, sizeof(char *));
490 for (i = 0; i < npages; free(entries[i++])) {
491 page_name = entries[i]->d_name;
492 snprintf(page_path, sizeof page_path, "%s/%s", section,
494 if (!is_manpage_name(page_name)) {
495 if (!(test_path(page_path, NULL) & TEST_DIR)) {
496 junk(mandir, page_path,
497 "invalid man page name");
501 zipped = is_bzipped(page_name) ? BZIP :
502 is_gzipped(page_name) ? GZIP : NONE;
503 if (zipped != NONE) {
504 snprintf(cat_path, sizeof cat_path, "%s/%s",
505 cat_section, page_name);
506 if (expected != NULL)
507 expected[nexpected++] = strdup(page_name);
508 process_page(mandir, page_path, cat_path, zipped);
511 * We've got an uncompressed man page,
512 * check to see if there's a (preferred)
515 snprintf(zip_path, sizeof zip_path, "%s%s",
517 if (test_path(zip_path, NULL) != 0) {
518 junk(mandir, page_path,
519 "man page unused due to existing " GZ_EXT);
523 "warning, %s is uncompressed\n",
526 snprintf(cat_path, sizeof cat_path, "%s/%s",
527 cat_section, page_name);
528 if (expected != NULL) {
529 asprintf(&expected[nexpected++],
532 process_page(mandir, page_path, cat_path, NONE);
537 if (expected == NULL)
540 * scan cat sections for junk
542 npages = scandir(cat_section, &entries, NULL, alphasort);
544 for (i = 0; i < npages; free(entries[i++])) {
545 const char *junk_reason;
548 page_name = entries[i]->d_name;
549 if (strcmp(page_name, ".") == 0 || strcmp(page_name, "..") == 0)
552 * Keep the index into the expected cat page list
553 * ahead of the name we've found.
555 while (e < nexpected &&
556 (cmp = strcmp(page_name, expected[e])) > 0)
560 /* we have an unexpected page */
561 snprintf(cat_path, sizeof cat_path, "%s/%s", cat_section,
563 if (!is_manpage_name(page_name)) {
564 if (test_path(cat_path, NULL) & TEST_DIR)
566 junk_reason = "invalid cat page name";
567 } else if (!is_gzipped(page_name) && e + 1 < nexpected &&
568 strncmp(page_name, expected[e + 1], strlen(page_name)) == 0 &&
569 strlen(expected[e + 1]) == strlen(page_name) + 3) {
570 junk_reason = "cat page unused due to existing " GZ_EXT;
572 junk_reason = "cat page without man page";
573 junk(mandir, cat_path, junk_reason);
576 while (e < nexpected)
583 * Processes a single man section.
586 process_section(char *mandir, char *section)
590 if (already_visited(mandir, section, 1))
593 fprintf(stderr, " section %s\n", section);
594 cat_section = get_cat_section(section);
595 if (make_writable_dir(mandir, cat_section))
596 scan_section(mandir, section, cat_section);
601 select_sections(const struct dirent *entry)
606 name = strdup(entry->d_name);
607 ret = directory_type(name) == MAN_SECTION_DIR;
613 * Processes a single top-level man directory. If section isn't NULL,
614 * it will only process that section sub-directory, otherwise it will
615 * process all of them.
618 process_mandir(char *dir_name, char *section)
620 fchdir(starting_dir);
621 if (already_visited(NULL, dir_name, section == NULL))
623 check_writable(dir_name);
625 fprintf(stderr, "man directory %s\n", dir_name);
627 fprintf(stderr, "cd %s\n", dir_name);
628 if (chdir(dir_name) < 0) {
629 warn("%s: chdir", dir_name);
633 if (section != NULL) {
634 process_section(dir_name, section);
636 struct dirent **entries;
641 nsections = scandir(".", &entries, select_sections, alphasort);
643 warn("%s", dir_name);
647 for (i = 0; i < nsections; i++) {
648 process_section(dir_name, entries[i]->d_name);
649 asprintf(&machine_dir, "%s/%s", entries[i]->d_name,
651 if (test_path(machine_dir, NULL) & TEST_DIR)
652 process_section(dir_name, machine_dir);
661 * Processes one argument, which may be a colon-separated list of
665 process_argument(const char *arg)
674 err(1, "out of memory");
675 while ((dir = strsep(&parg, ":")) != NULL) {
676 switch (directory_type(dir)) {
678 if (locale != NULL) {
679 asprintf(&mandir, "%s/%s", dir, locale);
680 process_mandir(mandir, NULL);
682 if (lang_locale != NULL) {
683 asprintf(&mandir, "%s/%s", dir,
685 process_mandir(mandir, NULL);
689 process_mandir(dir, NULL);
692 case MAN_SECTION_DIR: {
693 mandir = strdup(dirname(dir));
694 section = strdup(basename(dir));
695 process_mandir(mandir, section);
701 warnx("%s: directory name not in proper man form", dir);
709 determine_locale(void)
713 if ((locale = setlocale(LC_CTYPE, "")) == NULL) {
714 warnx("-L option used, but no locale found\n");
717 sep = strchr(locale, '_');
718 if (sep != NULL && isupper(sep[1]) && isupper(sep[2]))
719 asprintf(&lang_locale, "%.*s%s", (int)(sep - locale), locale,
721 sep = nl_langinfo(CODESET);
722 if (sep != NULL && *sep != '\0' && strcmp(sep, "US-ASCII") != 0) {
725 for (i = 0; locale_device[i] != NULL; i += 2) {
726 if (strcmp(sep, locale_device[i]) == 0) {
727 nroff_device = locale_device[i + 1];
733 if (lang_locale != NULL)
734 fprintf(stderr, "short locale is %s\n", lang_locale);
735 fprintf(stderr, "nroff device is %s\n", nroff_device);
742 fprintf(stderr, "usage: %s [-fLnrv] [directories ...]\n",
748 main(int argc, char **argv)
752 if ((uid = getuid()) == 0) {
753 fprintf(stderr, "don't run %s as root, use:\n echo", argv[0]);
754 for (optind = 0; optind < argc; optind++)
755 fprintf(stderr, " %s", argv[optind]);
756 fprintf(stderr, " | nice -5 su -m man\n");
759 while ((opt = getopt(argc, argv, "vnfLrh")) != -1) {
781 ngids = getgroups(NGROUPS_MAX, gids);
782 if ((starting_dir = open(".", 0)) < 0)
785 signal(SIGINT, trap_signal);
786 signal(SIGHUP, trap_signal);
787 signal(SIGQUIT, trap_signal);
788 signal(SIGTERM, trap_signal);
790 if ((machine = getenv("MACHINE")) == NULL)
793 if (optind == argc) {
794 const char *manpath = getenv("MANPATH");
796 manpath = DEFAULT_MANPATH;
797 process_argument(manpath);
799 while (optind < argc)
800 process_argument(argv[optind++]);