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