]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/makewhatis/makewhatis.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / makewhatis / makewhatis.c
1 /*-
2  * Copyright (c) 2002 John Rochester
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/utsname.h>
37
38 #include <ctype.h>
39 #include <dirent.h>
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <stringlist.h>
45 #include <unistd.h>
46 #include <zlib.h>
47
48 #define DEFAULT_MANPATH         "/usr/share/man"
49 #define LINE_ALLOC              4096
50
51 static char blank[] =           "";
52
53 /*
54  * Information collected about each man page in a section.
55  */
56 struct page_info {
57         char *  filename;
58         char *  name;
59         char *  suffix;
60         int     gzipped;
61         ino_t   inode;
62 };
63
64 /*
65  * An entry kept for each visited directory.
66  */
67 struct visited_dir {
68         dev_t           device;
69         ino_t           inode;
70         SLIST_ENTRY(visited_dir)        next;
71 };
72
73 /*
74  * an expanding string
75  */
76 struct sbuf {
77         char *  content;                /* the start of the buffer */
78         char *  end;                    /* just past the end of the content */
79         char *  last;                   /* the last allocated character */
80 };
81
82 /*
83  * Removes the last amount characters from the sbuf.
84  */
85 #define sbuf_retract(sbuf, amount)      \
86         ((sbuf)->end -= (amount))
87 /*
88  * Returns the length of the sbuf content.
89  */
90 #define sbuf_length(sbuf)               \
91         ((sbuf)->end - (sbuf)->content)
92
93 typedef char *edited_copy(char *from, char *to, int length);
94
95 static int append;                      /* -a flag: append to existing whatis */
96 static int verbose;                     /* -v flag: be verbose with warnings */
97 static int indent = 24;                 /* -i option: description indentation */
98 static const char *whatis_name="whatis";/* -n option: the name */
99 static char *common_output;             /* -o option: the single output file */
100 static char *locale;                    /* user's locale if -L is used */
101 static char *lang_locale;               /* short form of locale */
102 static const char *machine, *machine_arch;
103
104 static int exit_code;                   /* exit code to use when finished */
105 static SLIST_HEAD(, visited_dir) visited_dirs =
106     SLIST_HEAD_INITIALIZER(visited_dirs);
107
108 /*
109  * While the whatis line is being formed, it is stored in whatis_proto.
110  * When finished, it is reformatted into whatis_final and then appended
111  * to whatis_lines.
112  */
113 static struct sbuf *whatis_proto;
114 static struct sbuf *whatis_final;
115 static StringList *whatis_lines;        /* collected output lines */
116
117 static char tmp_file[MAXPATHLEN];       /* path of temporary file, if any */
118
119 /* A set of possible names for the NAME man page section */
120 static const char *name_section_titles[] = {
121         "NAME", "Name", "NAMN", "BEZEICHNUNG", "\xcc\xbe\xbe\xce",
122         "\xee\xe1\xfa\xf7\xe1\xee\xe9\xe5", NULL
123 };
124
125 /* A subset of the mdoc(7) commands to ignore */
126 static char mdoc_commands[] = "ArDvErEvFlLiNmPa";
127
128 /*
129  * Frees a struct page_info and its content.
130  */
131 static void
132 free_page_info(struct page_info *info)
133 {
134         free(info->filename);
135         free(info->name);
136         free(info->suffix);
137         free(info);
138 }
139
140 /*
141  * Allocates and fills in a new struct page_info given the
142  * name of the man section directory and the dirent of the file.
143  * If the file is not a man page, returns NULL.
144  */
145 static struct page_info *
146 new_page_info(char *dir, struct dirent *dirent)
147 {
148         struct page_info *info;
149         int basename_length;
150         char *suffix;
151         struct stat st;
152
153         info = (struct page_info *) malloc(sizeof(struct page_info));
154         if (info == NULL)
155                 err(1, "malloc");
156         basename_length = strlen(dirent->d_name);
157         suffix = &dirent->d_name[basename_length];
158         asprintf(&info->filename, "%s/%s", dir, dirent->d_name);
159         if ((info->gzipped = basename_length >= 4 && strcmp(&dirent->d_name[basename_length - 3], ".gz") == 0)) {
160                 suffix -= 3;
161                 *suffix = '\0';
162         }
163         for (;;) {
164                 if (--suffix == dirent->d_name || !isalnum(*suffix)) {
165                         if (*suffix == '.')
166                                 break;
167                         if (verbose)
168                                 warnx("%s: invalid man page name", info->filename);
169                         free(info->filename);
170                         free(info);
171                         return NULL;
172                 }
173         }
174         *suffix++ = '\0';
175         info->name = strdup(dirent->d_name);
176         info->suffix = strdup(suffix);
177         if (stat(info->filename, &st) < 0) {
178                 warn("%s", info->filename);
179                 free_page_info(info);
180                 return NULL;
181         }
182         if (!S_ISREG(st.st_mode)) {
183                 if (verbose && !S_ISDIR(st.st_mode))
184                         warnx("%s: not a regular file", info->filename);
185                 free_page_info(info);
186                 return NULL;
187         }
188         info->inode = st.st_ino;
189         return info;
190 }
191
192 /*
193  * Reset an sbuf's length to 0.
194  */
195 static void
196 sbuf_clear(struct sbuf *sbuf)
197 {
198         sbuf->end = sbuf->content;
199 }
200
201 /*
202  * Allocate a new sbuf.
203  */
204 static struct sbuf *
205 new_sbuf(void)
206 {
207         struct sbuf *sbuf = (struct sbuf *) malloc(sizeof(struct sbuf));
208         sbuf->content = (char *) malloc(LINE_ALLOC);
209         sbuf->last = sbuf->content + LINE_ALLOC - 1;
210         sbuf_clear(sbuf);
211         return sbuf;
212 }
213
214 /*
215  * Ensure that there is enough room in the sbuf for nchars more characters.
216  */
217 static void
218 sbuf_need(struct sbuf *sbuf, int nchars)
219 {
220         char *new_content;
221         size_t size, cntsize;
222
223         /* double the size of the allocation until the buffer is big enough */
224         while (sbuf->end + nchars > sbuf->last) {
225                 size = sbuf->last + 1 - sbuf->content;
226                 size *= 2;
227                 cntsize = sbuf->end - sbuf->content;
228
229                 new_content = (char *)malloc(size);
230                 memcpy(new_content, sbuf->content, cntsize);
231                 free(sbuf->content);
232                 sbuf->content = new_content;
233                 sbuf->end = new_content + cntsize;
234                 sbuf->last = new_content + size - 1;
235         }
236 }
237
238 /*
239  * Appends a string of a given length to the sbuf.
240  */
241 static void
242 sbuf_append(struct sbuf *sbuf, const char *text, int length)
243 {
244         if (length > 0) {
245                 sbuf_need(sbuf, length);
246                 memcpy(sbuf->end, text, length);
247                 sbuf->end += length;
248         }
249 }
250
251 /*
252  * Appends a null-terminated string to the sbuf.
253  */
254 static void
255 sbuf_append_str(struct sbuf *sbuf, char *text)
256 {
257         sbuf_append(sbuf, text, strlen(text));
258 }
259
260 /*
261  * Appends an edited null-terminated string to the sbuf.
262  */
263 static void
264 sbuf_append_edited(struct sbuf *sbuf, char *text, edited_copy copy)
265 {
266         int length = strlen(text);
267         if (length > 0) {
268                 sbuf_need(sbuf, length);
269                 sbuf->end = copy(text, sbuf->end, length);
270         }
271 }
272
273 /*
274  * Strips any of a set of chars from the end of the sbuf.
275  */
276 static void
277 sbuf_strip(struct sbuf *sbuf, const char *set)
278 {
279         while (sbuf->end > sbuf->content && strchr(set, sbuf->end[-1]) != NULL)
280                 sbuf->end--;
281 }
282
283 /*
284  * Returns the null-terminated string built by the sbuf.
285  */
286 static char *
287 sbuf_content(struct sbuf *sbuf)
288 {
289         *sbuf->end = '\0';
290         return sbuf->content;
291 }
292
293 /*
294  * Returns true if no man page exists in the directory with
295  * any of the names in the StringList.
296  */
297 static int
298 no_page_exists(char *dir, StringList *names, char *suffix)
299 {
300         char path[MAXPATHLEN];
301         size_t i;
302
303         for (i = 0; i < names->sl_cur; i++) {
304                 snprintf(path, sizeof path, "%s/%s.%s.gz", dir, names->sl_str[i], suffix);
305                 if (access(path, F_OK) < 0) {
306                         path[strlen(path) - 3] = '\0';
307                         if (access(path, F_OK) < 0)
308                                 continue;
309                 }
310                 return 0;
311         }
312         return 1;
313 }
314
315 static void
316 trap_signal(int sig __unused)
317 {
318         if (tmp_file[0] != '\0')
319                 unlink(tmp_file);
320         exit(1);
321 }
322
323 /*
324  * Attempts to open an output file.  Returns NULL if unsuccessful.
325  */
326 static FILE *
327 open_output(char *name)
328 {
329         FILE *output;
330
331         whatis_lines = sl_init();
332         if (append) {
333                 char line[LINE_ALLOC];
334
335                 output = fopen(name, "r");
336                 if (output == NULL) {
337                         warn("%s", name);
338                         exit_code = 1;
339                         return NULL;
340                 }
341                 while (fgets(line, sizeof line, output) != NULL) {
342                         line[strlen(line) - 1] = '\0';
343                         sl_add(whatis_lines, strdup(line));
344                 }
345         }
346         if (common_output == NULL) {
347                 snprintf(tmp_file, sizeof tmp_file, "%s.tmp", name);
348                 name = tmp_file;
349         }
350         output = fopen(name, "w");
351         if (output == NULL) {
352                 warn("%s", name);
353                 exit_code = 1;
354                 return NULL;
355         }
356         return output;
357 }
358
359 static int
360 linesort(const void *a, const void *b)
361 {
362         return strcmp((*(const char * const *)a), (*(const char * const *)b));
363 }
364
365 /*
366  * Writes the unique sorted lines to the output file.
367  */
368 static void
369 finish_output(FILE *output, char *name)
370 {
371         size_t i;
372         char *prev = NULL;
373
374         qsort(whatis_lines->sl_str, whatis_lines->sl_cur, sizeof(char *), linesort);
375         for (i = 0; i < whatis_lines->sl_cur; i++) {
376                 char *line = whatis_lines->sl_str[i];
377                 if (i > 0 && strcmp(line, prev) == 0)
378                         continue;
379                 prev = line;
380                 fputs(line, output);
381                 putc('\n', output);
382         }
383         fclose(output);
384         sl_free(whatis_lines, 1);
385         if (common_output == NULL) {
386                 rename(tmp_file, name);
387                 unlink(tmp_file);
388         }
389 }
390
391 static FILE *
392 open_whatis(char *mandir)
393 {
394         char filename[MAXPATHLEN];
395
396         snprintf(filename, sizeof filename, "%s/%s", mandir, whatis_name);
397         return open_output(filename);
398 }
399
400 static void
401 finish_whatis(FILE *output, char *mandir)
402 {
403         char filename[MAXPATHLEN];
404
405         snprintf(filename, sizeof filename, "%s/%s", mandir, whatis_name);
406         finish_output(output, filename);
407 }
408
409 /*
410  * Tests to see if the given directory has already been visited.
411  */
412 static int
413 already_visited(char *dir)
414 {
415         struct stat st;
416         struct visited_dir *visit;
417
418         if (stat(dir, &st) < 0) {
419                 warn("%s", dir);
420                 exit_code = 1;
421                 return 1;
422         }
423         SLIST_FOREACH(visit, &visited_dirs, next) {
424                 if (visit->inode == st.st_ino &&
425                     visit->device == st.st_dev) {
426                         warnx("already visited %s", dir);
427                         return 1;
428                 }
429         }
430         visit = (struct visited_dir *) malloc(sizeof(struct visited_dir));
431         visit->device = st.st_dev;
432         visit->inode = st.st_ino;
433         SLIST_INSERT_HEAD(&visited_dirs, visit, next);
434         return 0;
435 }
436
437 /*
438  * Removes trailing spaces from a string, returning a pointer to just
439  * beyond the new last character.
440  */
441 static char *
442 trim_rhs(char *str)
443 {
444         char *rhs = &str[strlen(str)];
445         while (--rhs > str && isspace(*rhs))
446                 ;
447         *++rhs = '\0';
448         return rhs;
449 }
450
451 /*
452  * Returns a pointer to the next non-space character in the string.
453  */
454 static char *
455 skip_spaces(char *s)
456 {
457         while (*s != '\0' && isspace(*s))
458                 s++;
459         return s;
460 }
461
462 /*
463  * Returns whether the string contains only digits.
464  */
465 static int
466 only_digits(char *line)
467 {
468         if (!isdigit(*line++))
469                 return 0;
470         while (isdigit(*line))
471                 line++;
472         return *line == '\0';
473 }
474
475 /*
476  * Returns whether the line is of one of the forms:
477  *      .Sh NAME
478  *      .Sh "NAME"
479  *      etc.
480  * assuming that section_start is ".Sh".
481  */
482 static int
483 name_section_line(char *line, const char *section_start)
484 {
485         char *rhs;
486         const char **title;
487
488         if (strncmp(line, section_start, 3) != 0)
489                 return 0;
490         line = skip_spaces(line + 3);
491         rhs = trim_rhs(line);
492         if (*line == '"') {
493                 line++;
494                 if (*--rhs == '"')
495                         *rhs = '\0';
496         }
497         for (title = name_section_titles; *title != NULL; title++)
498                 if (strcmp(*title, line) == 0)
499                         return 1;
500         return 0;
501 }
502
503 /*
504  * Copies characters while removing the most common nroff/troff
505  * markup:
506  *      \(em, \(mi, \s[+-N], \&
507  *      \fF, \f(fo, \f[font]
508  *      \*s, \*(st, \*[stringvar]
509  */
510 static char *
511 de_nroff_copy(char *from, char *to, int fromlen)
512 {
513         char *from_end = &from[fromlen];
514         while (from < from_end) {
515                 switch (*from) {
516                 case '\\':
517                         switch (*++from) {
518                         case '(':
519                                 if (strncmp(&from[1], "em", 2) == 0 ||
520                                                 strncmp(&from[1], "mi", 2) == 0) {
521                                         from += 3;
522                                         continue;
523                                 }
524                                 break;
525                         case 's':
526                                 if (*++from == '-')
527                                         from++;
528                                 while (isdigit(*from))
529                                         from++;
530                                 continue;
531                         case 'f':
532                         case '*':
533                                 if (*++from == '(')
534                                         from += 3;
535                                 else if (*from == '[') {
536                                         while (*++from != ']' && from < from_end);
537                                         from++;
538                                 } else
539                                         from++;
540                                 continue;
541                         case '&':
542                                 from++;
543                                 continue;
544                         }
545                         break;
546                 }
547                 *to++ = *from++;
548         }
549         return to;
550 }
551
552 /*
553  * Appends a string with the nroff formatting removed.
554  */
555 static void
556 add_nroff(char *text)
557 {
558         sbuf_append_edited(whatis_proto, text, de_nroff_copy);
559 }
560
561 /*
562  * Appends "name(suffix), " to whatis_final.
563  */
564 static void
565 add_whatis_name(char *name, char *suffix)
566 {
567         if (*name != '\0') {
568                 sbuf_append_str(whatis_final, name);
569                 sbuf_append(whatis_final, "(", 1);
570                 sbuf_append_str(whatis_final, suffix);
571                 sbuf_append(whatis_final, "), ", 3);
572         }
573 }
574
575 /*
576  * Processes an old-style man(7) line.  This ignores commands with only
577  * a single number argument.
578  */
579 static void
580 process_man_line(char *line)
581 {
582         if (*line == '.') {
583                 while (isalpha(*++line))
584                         ;
585                 line = skip_spaces(line);
586                 if (only_digits(line))
587                         return;
588         } else
589                 line = skip_spaces(line);
590         if (*line != '\0') {
591                 add_nroff(line);
592                 sbuf_append(whatis_proto, " ", 1);
593         }
594 }
595
596 /*
597  * Processes a new-style mdoc(7) line.
598  */
599 static void
600 process_mdoc_line(char *line)
601 {
602         int xref;
603         int arg = 0;
604         char *line_end = &line[strlen(line)];
605         int orig_length = sbuf_length(whatis_proto);
606         char *next;
607
608         if (*line == '\0')
609                 return;
610         if (line[0] != '.' || !isupper(line[1]) || !islower(line[2])) {
611                 add_nroff(skip_spaces(line));
612                 sbuf_append(whatis_proto, " ", 1);
613                 return;
614         }
615         xref = strncmp(line, ".Xr", 3) == 0;
616         line += 3;
617         while ((line = skip_spaces(line)) < line_end) {
618                 if (*line == '"') {
619                         next = ++line;
620                         for (;;) {
621                                 next = strchr(next, '"');
622                                 if (next == NULL)
623                                         break;
624                                 memmove(next, next + 1, strlen(next));
625                                 line_end--;
626                                 if (*next != '"')
627                                         break;
628                                 next++;
629                         }
630                 } else
631                         next = strpbrk(line, " \t");
632                 if (next != NULL)
633                         *next++ = '\0';
634                 else
635                         next = line_end;
636                 if (isupper(*line) && islower(line[1]) && line[2] == '\0') {
637                         if (strcmp(line, "Ns") == 0) {
638                                 arg = 0;
639                                 line = next;
640                                 continue;
641                         }
642                         if (strstr(mdoc_commands, line) != NULL) {
643                                 line = next;
644                                 continue;
645                         }
646                 }
647                 if (arg > 0 && strchr(",.:;?!)]", *line) == 0) {
648                         if (xref) {
649                                 sbuf_append(whatis_proto, "(", 1);
650                                 add_nroff(line);
651                                 sbuf_append(whatis_proto, ")", 1);
652                                 xref = 0;
653                                 line = blank;
654                         } else
655                                 sbuf_append(whatis_proto, " ", 1);
656                 }
657                 add_nroff(line);
658                 arg++;
659                 line = next;
660         }
661         if (sbuf_length(whatis_proto) > orig_length)
662                 sbuf_append(whatis_proto, " ", 1);
663 }
664
665 /*
666  * Collects a list of comma-separated names from the text.
667  */
668 static void
669 collect_names(StringList *names, char *text)
670 {
671         char *arg;
672
673         for (;;) {
674                 arg = text;
675                 text = strchr(text, ',');
676                 if (text != NULL)
677                         *text++ = '\0';
678                 sl_add(names, arg);
679                 if (text == NULL)
680                         return;
681                 if (*text == ' ')
682                         text++;
683         }
684 }
685
686 enum { STATE_UNKNOWN, STATE_MANSTYLE, STATE_MDOCNAME, STATE_MDOCDESC };
687
688 /*
689  * Processes a man page source into a single whatis line and adds it
690  * to whatis_lines.
691  */
692 static void
693 process_page(struct page_info *page, char *section_dir)
694 {
695         gzFile *in;
696         char buffer[4096];
697         char *line;
698         StringList *names;
699         char *descr;
700         int state = STATE_UNKNOWN;
701         size_t i;
702
703         sbuf_clear(whatis_proto);
704         if ((in = gzopen(page->filename, "r")) == NULL) {
705                 warn("%s", page->filename);
706                 exit_code = 1;
707                 return;
708         }
709         while (gzgets(in, buffer, sizeof buffer) != NULL) {
710                 line = buffer;
711                 if (strncmp(line, ".\\\"", 3) == 0)             /* ignore comments */
712                         continue;
713                 switch (state) {
714                 /*
715                  * haven't reached the NAME section yet.
716                  */
717                 case STATE_UNKNOWN:
718                         if (name_section_line(line, ".SH"))
719                                 state = STATE_MANSTYLE;
720                         else if (name_section_line(line, ".Sh"))
721                                 state = STATE_MDOCNAME;
722                         continue;
723                 /*
724                  * Inside an old-style .SH NAME section.
725                  */
726                 case STATE_MANSTYLE:
727                         if (strncmp(line, ".SH", 3) == 0)
728                                 break;
729                         if (strncmp(line, ".SS", 3) == 0)
730                                 break;
731                         trim_rhs(line);
732                         if (strcmp(line, ".") == 0)
733                                 continue;
734                         if (strncmp(line, ".IX", 3) == 0) {
735                                 line += 3;
736                                 line = skip_spaces(line);
737                         }
738                         process_man_line(line);
739                         continue;
740                 /*
741                  * Inside a new-style .Sh NAME section (the .Nm part).
742                  */
743                 case STATE_MDOCNAME:
744                         trim_rhs(line);
745                         if (strncmp(line, ".Nm", 3) == 0) {
746                                 process_mdoc_line(line);
747                                 continue;
748                         } else {
749                                 if (strcmp(line, ".") == 0)
750                                         continue;
751                                 sbuf_append(whatis_proto, "- ", 2);
752                                 state = STATE_MDOCDESC;
753                         }
754                         /* fall through */
755                 /*
756                  * Inside a new-style .Sh NAME section (after the .Nm-s).
757                  */
758                 case STATE_MDOCDESC:
759                         if (strncmp(line, ".Sh", 3) == 0)
760                                 break;
761                         trim_rhs(line);
762                         if (strcmp(line, ".") == 0)
763                                 continue;
764                         process_mdoc_line(line);
765                         continue;
766                 }
767                 break;
768         }
769         gzclose(in);
770         sbuf_strip(whatis_proto, " \t.-");
771         line = sbuf_content(whatis_proto);
772         /*
773          * line now contains the appropriate data, but without
774          * the proper indentation or the section appended to each name.
775          */
776         descr = strstr(line, " - ");
777         if (descr == NULL) {
778                 descr = strchr(line, ' ');
779                 if (descr == NULL) {
780                         if (verbose)
781                                 fprintf(stderr, "       ignoring junk description \"%s\"\n", line);
782                         return;
783                 }
784                 *descr++ = '\0';
785         } else {
786                 *descr = '\0';
787                 descr += 3;
788         }
789         names = sl_init();
790         collect_names(names, line);
791         sbuf_clear(whatis_final);
792         if (!sl_find(names, page->name) && no_page_exists(section_dir, names, page->suffix)) {
793                 /*
794                  * Add the page name since that's the only thing that
795                  * man(1) will find.
796                  */
797                 add_whatis_name(page->name, page->suffix);
798         }
799         for (i = 0; i < names->sl_cur; i++)
800                 add_whatis_name(names->sl_str[i], page->suffix);
801         sl_free(names, 0);
802         sbuf_retract(whatis_final, 2);          /* remove last ", " */
803         while (sbuf_length(whatis_final) < indent)
804                 sbuf_append(whatis_final, " ", 1);
805         sbuf_append(whatis_final, " - ", 3);
806         sbuf_append_str(whatis_final, skip_spaces(descr));
807         sl_add(whatis_lines, strdup(sbuf_content(whatis_final)));
808 }
809
810 /*
811  * Sorts pages first by inode number, then by name.
812  */
813 static int
814 pagesort(const void *a, const void *b)
815 {
816         const struct page_info *p1 = *(struct page_info * const *) a;
817         const struct page_info *p2 = *(struct page_info * const *) b;
818         if (p1->inode == p2->inode)
819                 return strcmp(p1->name, p2->name);
820         return p1->inode - p2->inode;
821 }
822
823 /*
824  * Processes a single man section.
825  */
826 static void
827 process_section(char *section_dir)
828 {
829         struct dirent **entries;
830         int nentries;
831         struct page_info **pages;
832         int npages = 0;
833         int i;
834         ino_t prev_inode = 0;
835
836         if (verbose)
837                 fprintf(stderr, "  %s\n", section_dir);
838
839         /*
840          * scan the man section directory for pages
841          */
842         nentries = scandir(section_dir, &entries, NULL, alphasort);
843         if (nentries < 0) {
844                 warn("%s", section_dir);
845                 exit_code = 1;
846                 return;
847         }
848         /*
849          * collect information about man pages
850          */
851         pages = (struct page_info **) calloc(nentries, sizeof(struct page_info *));
852         for (i = 0; i < nentries; i++) {
853                 struct page_info *info = new_page_info(section_dir, entries[i]);
854                 if (info != NULL)
855                         pages[npages++] = info;
856                 free(entries[i]);
857         }
858         free(entries);
859         qsort(pages, npages, sizeof(struct page_info *), pagesort);
860         /*
861          * process each unique page
862          */
863         for (i = 0; i < npages; i++) {
864                 struct page_info *page = pages[i];
865                 if (page->inode != prev_inode) {
866                         prev_inode = page->inode;
867                         if (verbose)
868                                 fprintf(stderr, "       reading %s\n", page->filename);
869                         process_page(page, section_dir);
870                 } else if (verbose)
871                         fprintf(stderr, "       skipping %s, duplicate\n", page->filename);
872                 free_page_info(page);
873         }
874         free(pages);
875 }
876
877 /*
878  * Returns whether the directory entry is a man page section.
879  */
880 static int
881 select_sections(struct dirent *entry)
882 {
883         char *p = &entry->d_name[3];
884
885         if (strncmp(entry->d_name, "man", 3) != 0)
886                 return 0;
887         while (*p != '\0') {
888                 if (!isalnum(*p++))
889                         return 0;
890         }
891         return 1;
892 }
893
894 /*
895  * Processes a single top-level man directory by finding all the
896  * sub-directories named man* and processing each one in turn.
897  */
898 static void
899 process_mandir(char *dir_name)
900 {
901         struct dirent **entries;
902         int nsections;
903         FILE *fp = NULL;
904         int i;
905         struct stat st;
906
907         if (already_visited(dir_name))
908                 return;
909         if (verbose)
910                 fprintf(stderr, "man directory %s\n", dir_name);
911         nsections = scandir(dir_name, &entries, select_sections, alphasort);
912         if (nsections < 0) {
913                 warn("%s", dir_name);
914                 exit_code = 1;
915                 return;
916         }
917         if (common_output == NULL && (fp = open_whatis(dir_name)) == NULL)
918                 return;
919         for (i = 0; i < nsections; i++) {
920                 char section_dir[MAXPATHLEN];
921                 snprintf(section_dir, sizeof section_dir, "%s/%s", dir_name, entries[i]->d_name);
922                 process_section(section_dir);
923                 snprintf(section_dir, sizeof section_dir, "%s/%s/%s", dir_name,
924                     entries[i]->d_name, machine);
925                 if (stat(section_dir, &st) == 0 && S_ISDIR(st.st_mode))
926                         process_section(section_dir);
927                 if (strcmp(machine_arch, machine) != 0) {
928                         snprintf(section_dir, sizeof section_dir, "%s/%s/%s",
929                             dir_name, entries[i]->d_name, machine_arch);
930                         if (stat(section_dir, &st) == 0 && S_ISDIR(st.st_mode))
931                                 process_section(section_dir);
932                 }
933                 free(entries[i]);
934         }
935         free(entries);
936         if (common_output == NULL)
937                 finish_whatis(fp, dir_name);
938 }
939
940 /*
941  * Processes one argument, which may be a colon-separated list of
942  * directories.
943  */
944 static void
945 process_argument(const char *arg)
946 {
947         char *dir;
948         char *mandir;
949         char *parg;
950
951         parg = strdup(arg);
952         if (parg == NULL)
953                 err(1, "out of memory");
954         while ((dir = strsep(&parg, ":")) != NULL) {
955                 if (locale != NULL) {
956                         asprintf(&mandir, "%s/%s", dir, locale);
957                         process_mandir(mandir);
958                         free(mandir);
959                         if (lang_locale != NULL) {
960                                 asprintf(&mandir, "%s/%s", dir, lang_locale);
961                                 process_mandir(mandir);
962                                 free(mandir);
963                         }
964                 } else {
965                         process_mandir(dir);
966                 }
967         }
968         free(parg);
969 }
970
971
972 int
973 main(int argc, char **argv)
974 {
975         int opt;
976         FILE *fp = NULL;
977
978         while ((opt = getopt(argc, argv, "ai:n:o:vL")) != -1) {
979                 switch (opt) {
980                 case 'a':
981                         append++;
982                         break;
983                 case 'i':
984                         indent = atoi(optarg);
985                         break;
986                 case 'n':
987                         whatis_name = optarg;
988                         break;
989                 case 'o':
990                         common_output = optarg;
991                         break;
992                 case 'v':
993                         verbose++;
994                         break;
995                 case 'L':
996                         locale = getenv("LC_ALL");
997                         if (locale == NULL)
998                                 locale = getenv("LC_CTYPE");
999                         if (locale == NULL)
1000                                 locale = getenv("LANG");
1001                         if (locale != NULL) {
1002                                 char *sep = strchr(locale, '_');
1003                                 if (sep != NULL && isupper(sep[1]) &&
1004                                     isupper(sep[2])) {
1005                                         asprintf(&lang_locale, "%.*s%s", sep - locale, locale, &sep[3]);
1006                                 }
1007                         }
1008                         break;
1009                 default:
1010                         fprintf(stderr, "usage: %s [-a] [-i indent] [-n name] [-o output_file] [-v] [-L] [directories...]\n", argv[0]);
1011                         exit(1);
1012                 }
1013         }
1014
1015         signal(SIGINT, trap_signal);
1016         signal(SIGHUP, trap_signal);
1017         signal(SIGQUIT, trap_signal);
1018         signal(SIGTERM, trap_signal);
1019         SLIST_INIT(&visited_dirs);
1020         whatis_proto = new_sbuf();
1021         whatis_final = new_sbuf();
1022
1023         if ((machine = getenv("MACHINE")) == NULL) {
1024                 static struct utsname utsname;
1025
1026                 if (uname(&utsname) == -1)
1027                         err(1, "uname");
1028                 machine = utsname.machine;
1029         }
1030
1031         if ((machine_arch = getenv("MACHINE_ARCH")) == NULL)
1032                 machine_arch = MACHINE_ARCH;
1033
1034         if (common_output != NULL && (fp = open_output(common_output)) == NULL)
1035                 err(1, "%s", common_output);
1036         if (optind == argc) {
1037                 const char *manpath = getenv("MANPATH");
1038                 if (manpath == NULL)
1039                         manpath = DEFAULT_MANPATH;
1040                 process_argument(manpath);
1041         } else {
1042                 while (optind < argc)
1043                         process_argument(argv[optind++]);
1044         }
1045         if (common_output != NULL)
1046                 finish_output(fp, common_output);
1047         exit(exit_code);
1048 }