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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/utsname.h>
49 #define DEFAULT_MANPATH "/usr/share/man"
51 #define TOP_LEVEL_DIR 0 /* signifies a top-level man directory */
52 #define MAN_SECTION_DIR 1 /* signifies a man section directory */
53 #define UNKNOWN 2 /* signifies an unclassifiable directory */
55 #define TEST_EXISTS 0x01
57 #define TEST_FILE 0x04
58 #define TEST_READABLE 0x08
59 #define TEST_WRITABLE 0x10
61 static int verbose; /* -v flag: be verbose with warnings */
62 static int pretend; /* -n, -p flags: print out what would be done
63 instead of actually doing it */
64 static int force; /* -f flag: force overwriting all cat pages */
65 static int rm_junk; /* -r flag: remove garbage pages */
66 static char *locale; /* user's locale if -L is used */
67 static char *lang_locale; /* short form of locale */
68 static const char *machine, *machine_arch;
69 static int exit_code; /* exit code to use when finished */
72 * -T argument for nroff
74 static const char *nroff_device = "ascii";
77 * Mapping from locale to nroff device
79 static const char *locale_device[] = {
81 "ISO8859-1", "latin1",
82 "ISO8859-15", "latin1",
86 #define BZ2_CMD "bzip2"
87 #define BZ2_EXT ".bz2"
88 #define BZ2CAT_CMD "bz"
92 enum Ziptype {NONE, BZIP, GZIP};
95 static int starting_dir;
96 static char tmp_file[MAXPATHLEN];
97 static struct stat test_st;
100 * A hashtable is an array of chains composed of this entry structure.
106 struct hash_entry *next;
109 #define HASHTABLE_ALLOC 16384 /* allocation for hashtable (power of 2) */
110 #define HASH_MASK (HASHTABLE_ALLOC - 1)
112 static struct hash_entry *visited[HASHTABLE_ALLOC];
113 static struct hash_entry *links[HASHTABLE_ALLOC];
116 * Inserts a string into a hashtable keyed by inode & device number.
119 insert_hashtable(struct hash_entry **table,
124 struct hash_entry *new_entry;
125 struct hash_entry **chain;
127 new_entry = (struct hash_entry *) malloc(sizeof(struct hash_entry));
128 if (new_entry == NULL)
129 err(1, "can't insert into hashtable");
130 chain = &table[inode_number & HASH_MASK];
131 new_entry->inode_number = inode_number;
132 new_entry->device_number = device_number;
133 new_entry->data = data;
134 new_entry->next = *chain;
139 * Finds a string in a hashtable keyed by inode & device number.
142 find_hashtable(struct hash_entry **table,
146 struct hash_entry *chain;
148 chain = table[inode_number & HASH_MASK];
149 while (chain != NULL) {
150 if (chain->inode_number == inode_number &&
151 chain->device_number == device_number)
159 trap_signal(int sig __unused)
161 if (tmp_file[0] != '\0')
167 * Deals with junk files in the man or cat section directories.
170 junk(const char *mandir, const char *name, const char *reason)
173 fprintf(stderr, "%s/%s: %s\n", mandir, name, reason);
175 fprintf(stderr, "rm %s/%s\n", mandir, name);
176 if (!pretend && unlink(name) < 0)
177 warn("%s/%s", mandir, name);
182 * Returns TOP_LEVEL_DIR for .../man, MAN_SECTION_DIR for .../manXXX,
183 * and UNKNOWN for everything else.
186 directory_type(char *dir)
191 p = strrchr(dir, '/');
192 if (p == NULL || p[1] != '\0')
200 if (strncmp(p, "man", 3) == 0) {
203 return TOP_LEVEL_DIR;
204 while (isalnum((unsigned char)*p) || *p == '_') {
206 return MAN_SECTION_DIR;
213 * Tests whether the given file name (without a preceding path)
214 * is a proper man page name (like "mk-amd-map.8.gz").
215 * Only alphanumerics and '_' are allowed after the last '.' and
216 * the last '.' can't be the first or last characters.
219 is_manpage_name(char *name)
221 char *lastdot = NULL;
225 if (!isalnum((unsigned char)*n)) {
244 return lastdot > name && lastdot + 1 < n;
248 is_bzipped(char *name)
250 int len = strlen(name);
251 return len >= 5 && strcmp(&name[len - 4], BZ2_EXT) == 0;
255 is_gzipped(char *name)
257 int len = strlen(name);
258 return len >= 4 && strcmp(&name[len - 3], GZ_EXT) == 0;
262 * Converts manXXX to catXXX.
265 get_cat_section(char *section)
269 cat_section = strdup(section);
270 strncpy(cat_section, "cat", 3);
275 * Tests to see if the given directory has already been visited.
278 already_visited(char *mandir, char *dir, int count_visit)
282 if (stat(dir, &st) < 0) {
284 warn("%s/%s", mandir, dir);
290 if (find_hashtable(visited, st.st_ino, st.st_dev) != NULL) {
292 warnx("already visited %s/%s", mandir, dir);
294 warnx("already visited %s", dir);
298 insert_hashtable(visited, st.st_ino, st.st_dev, "");
303 * Returns a set of TEST_* bits describing a file's type and permissions.
304 * If mod_time isn't NULL, it will contain the file's modification time.
307 test_path(char *name, time_t *mod_time)
311 if (stat(name, &test_st) < 0)
313 result = TEST_EXISTS;
314 if (mod_time != NULL)
315 *mod_time = test_st.st_mtime;
316 if (S_ISDIR(test_st.st_mode))
318 else if (S_ISREG(test_st.st_mode))
320 if (access(name, R_OK))
321 result |= TEST_READABLE;
322 if (access(name, W_OK))
323 result |= TEST_WRITABLE;
328 * Checks whether a file is a symbolic link.
331 is_symlink(char *path)
335 return lstat(path, &st) >= 0 && S_ISLNK(st.st_mode);
339 * Tests to see if the given directory can be written to.
342 check_writable(char *mandir)
344 if (verbose && !(test_path(mandir, NULL) & TEST_WRITABLE))
345 fprintf(stderr, "%s: not writable - will only be able to write to existing cat directories\n", mandir);
349 * If the directory exists, attempt to make it writable, otherwise
350 * attempt to create it.
353 make_writable_dir(char *mandir, char *dir)
357 if ((test = test_path(dir, NULL)) != 0) {
358 if (!(test & TEST_WRITABLE) && chmod(dir, 0755) < 0) {
359 warn("%s/%s: chmod", mandir, dir);
364 if (verbose || pretend)
365 fprintf(stderr, "mkdir %s\n", dir);
368 if (mkdir(dir, 0755) < 0) {
369 warn("%s/%s: mkdir", mandir, dir);
379 * Processes a single man page source by using nroff to create
380 * the preformatted cat page.
383 process_page(char *mandir, char *src, char *cat, enum Ziptype zipped)
385 int src_test, cat_test;
386 time_t src_mtime, cat_mtime;
387 char cmd[MAXPATHLEN];
390 const char *link_name;
392 src_test = test_path(src, &src_mtime);
393 if (!(src_test & (TEST_FILE|TEST_READABLE))) {
394 if (!(src_test & TEST_DIR)) {
395 warnx("%s/%s: unreadable", mandir, src);
397 if (rm_junk && is_symlink(src))
398 junk(mandir, src, "bogus symlink");
402 src_dev = test_st.st_dev;
403 src_ino = test_st.st_ino;
404 cat_test = test_path(cat, &cat_mtime);
405 if (cat_test & (TEST_FILE|TEST_READABLE)) {
406 if (!force && cat_mtime >= src_mtime) {
408 fprintf(stderr, "\t%s/%s: up to date\n",
415 * Is the man page a link to one we've already processed?
417 if ((link_name = find_hashtable(links, src_ino, src_dev)) != NULL) {
418 if (verbose || pretend) {
419 fprintf(stderr, "%slink %s -> %s\n",
420 verbose ? "\t" : "", cat, link_name);
423 link(link_name, cat);
426 insert_hashtable(links, src_ino, src_dev, strdup(cat));
427 if (verbose || pretend) {
428 fprintf(stderr, "%sformat %s -> %s\n",
429 verbose ? "\t" : "", src, cat);
433 snprintf(tmp_file, sizeof tmp_file, "%s.tmp", cat);
434 snprintf(cmd, sizeof cmd,
435 "%scat %s | tbl | nroff -c -T%s -man | %s > %s.tmp",
436 zipped == BZIP ? BZ2CAT_CMD : zipped == GZIP ? GZCAT_CMD : "",
438 zipped == BZIP ? BZ2_CMD : zipped == GZIP ? GZ_CMD : "cat",
440 if (system(cmd) != 0)
441 err(1, "formatting pipeline");
442 if (rename(tmp_file, cat) < 0)
448 * Scan the man section directory for pages and process each one,
449 * then check for junk in the corresponding cat section.
452 scan_section(char *mandir, char *section, char *cat_section)
454 struct dirent **entries;
455 char **expected = NULL;
461 char page_path[MAXPATHLEN];
462 char cat_path[MAXPATHLEN];
463 char zip_path[MAXPATHLEN];
466 * scan the man section directory for pages
468 npages = scandir(section, &entries, NULL, alphasort);
470 warn("%s/%s", mandir, section);
474 if (verbose || rm_junk) {
476 * Maintain a list of all cat pages that should exist,
477 * corresponding to existing man pages.
479 expected = (char **) calloc(npages, sizeof(char *));
481 for (i = 0; i < npages; free(entries[i++])) {
482 page_name = entries[i]->d_name;
483 snprintf(page_path, sizeof page_path, "%s/%s", section,
485 if (!is_manpage_name(page_name)) {
486 if (!(test_path(page_path, NULL) & TEST_DIR)) {
487 junk(mandir, page_path,
488 "invalid man page name");
492 zipped = is_bzipped(page_name) ? BZIP :
493 is_gzipped(page_name) ? GZIP : NONE;
494 if (zipped != NONE) {
495 snprintf(cat_path, sizeof cat_path, "%s/%s",
496 cat_section, page_name);
497 if (expected != NULL)
498 expected[nexpected++] = strdup(page_name);
499 process_page(mandir, page_path, cat_path, zipped);
502 * We've got an uncompressed man page,
503 * check to see if there's a (preferred)
506 snprintf(zip_path, sizeof zip_path, "%s%s",
508 if (test_path(zip_path, NULL) != 0) {
509 junk(mandir, page_path,
510 "man page unused due to existing " GZ_EXT);
514 "warning, %s is uncompressed\n",
517 snprintf(cat_path, sizeof cat_path, "%s/%s",
518 cat_section, page_name);
519 if (expected != NULL) {
520 asprintf(&expected[nexpected++],
523 process_page(mandir, page_path, cat_path, NONE);
528 if (expected == NULL)
531 * scan cat sections for junk
533 npages = scandir(cat_section, &entries, NULL, alphasort);
535 for (i = 0; i < npages; free(entries[i++])) {
536 const char *junk_reason;
539 page_name = entries[i]->d_name;
540 if (strcmp(page_name, ".") == 0 || strcmp(page_name, "..") == 0)
543 * Keep the index into the expected cat page list
544 * ahead of the name we've found.
546 while (e < nexpected &&
547 (cmp = strcmp(page_name, expected[e])) > 0)
551 /* we have an unexpected page */
552 snprintf(cat_path, sizeof cat_path, "%s/%s", cat_section,
554 if (!is_manpage_name(page_name)) {
555 if (test_path(cat_path, NULL) & TEST_DIR)
557 junk_reason = "invalid cat page name";
558 } else if (!is_gzipped(page_name) && e + 1 < nexpected &&
559 strncmp(page_name, expected[e + 1], strlen(page_name)) == 0 &&
560 strlen(expected[e + 1]) == strlen(page_name) + 3) {
561 junk_reason = "cat page unused due to existing " GZ_EXT;
563 junk_reason = "cat page without man page";
564 junk(mandir, cat_path, junk_reason);
567 while (e < nexpected)
574 * Processes a single man section.
577 process_section(char *mandir, char *section)
581 if (already_visited(mandir, section, 1))
584 fprintf(stderr, " section %s\n", section);
585 cat_section = get_cat_section(section);
586 if (make_writable_dir(mandir, cat_section))
587 scan_section(mandir, section, cat_section);
592 select_sections(const struct dirent *entry)
597 name = strdup(entry->d_name);
598 ret = directory_type(name) == MAN_SECTION_DIR;
604 * Processes a single top-level man directory. If section isn't NULL,
605 * it will only process that section sub-directory, otherwise it will
606 * process all of them.
609 process_mandir(char *dir_name, char *section)
611 fchdir(starting_dir);
612 if (already_visited(NULL, dir_name, section == NULL))
614 check_writable(dir_name);
616 fprintf(stderr, "man directory %s\n", dir_name);
618 fprintf(stderr, "cd %s\n", dir_name);
619 if (chdir(dir_name) < 0) {
620 warn("%s: chdir", dir_name);
624 if (section != NULL) {
625 process_section(dir_name, section);
627 struct dirent **entries;
628 char *machine_dir, *arch_dir;
632 nsections = scandir(".", &entries, select_sections, alphasort);
634 warn("%s", dir_name);
638 for (i = 0; i < nsections; i++) {
639 process_section(dir_name, entries[i]->d_name);
640 asprintf(&machine_dir, "%s/%s", entries[i]->d_name,
642 if (test_path(machine_dir, NULL) & TEST_DIR)
643 process_section(dir_name, machine_dir);
645 if (strcmp(machine_arch, machine) != 0) {
646 asprintf(&arch_dir, "%s/%s", entries[i]->d_name,
648 if (test_path(arch_dir, NULL) & TEST_DIR)
649 process_section(dir_name, arch_dir);
659 * Processes one argument, which may be a colon-separated list of
663 process_argument(const char *arg)
672 err(1, "out of memory");
673 while ((dir = strsep(&parg, ":")) != NULL) {
674 switch (directory_type(dir)) {
676 if (locale != NULL) {
677 asprintf(&mandir, "%s/%s", dir, locale);
678 process_mandir(mandir, NULL);
680 if (lang_locale != NULL) {
681 asprintf(&mandir, "%s/%s", dir,
683 process_mandir(mandir, NULL);
687 process_mandir(dir, NULL);
690 case MAN_SECTION_DIR: {
691 mandir = strdup(dirname(dir));
692 section = strdup(basename(dir));
693 process_mandir(mandir, section);
699 warnx("%s: directory name not in proper man form", dir);
707 determine_locale(void)
711 if ((locale = setlocale(LC_CTYPE, "")) == NULL) {
712 warnx("-L option used, but no locale found\n");
715 sep = strchr(locale, '_');
716 if (sep != NULL && isupper((unsigned char)sep[1])
717 && isupper((unsigned char)sep[2])) {
718 asprintf(&lang_locale, "%.*s%s", (int)(sep - 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]);
757 fprintf(stderr, " | nice -5 su -m man\n");
760 while ((opt = getopt(argc, argv, "vnfLrh")) != -1) {
782 if ((starting_dir = open(".", 0)) < 0) {
786 signal(SIGINT, trap_signal);
787 signal(SIGHUP, trap_signal);
788 signal(SIGQUIT, trap_signal);
789 signal(SIGTERM, trap_signal);
791 if ((machine = getenv("MACHINE")) == NULL) {
792 static struct utsname utsname;
794 if (uname(&utsname) == -1)
796 machine = utsname.machine;
799 if ((machine_arch = getenv("MACHINE_ARCH")) == NULL)
800 machine_arch = MACHINE_ARCH;
802 if (optind == argc) {
803 const char *manpath = getenv("MANPATH");
805 manpath = DEFAULT_MANPATH;
806 process_argument(manpath);
808 while (optind < argc)
809 process_argument(argv[optind++]);