95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright (c) 2002 John Rochester
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * All rights reserved.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Redistribution and use in source and binary forms, with or without
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * modification, are permitted provided that the following conditions
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * notice, this list of conditions and the following disclaimer,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * in this position and unchanged.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * documentation and/or other materials provided with the distribution.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * 3. The name of the author may not be used to endorse or promote products
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * derived from this software without specific prior written permission
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org>
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/* Information collected about each man page in a section */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/* An expanding string */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore char *content; /* the start of the buffer */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore char *end; /* just past the end of the content */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore char *last; /* the last allocated character */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/* Remove the last amount characters from the sbuf */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#define sbuf_retract(sbuf, amount) ((sbuf)->end -= (amount))
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/* Return the length of the sbuf content */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore#define sbuf_length(sbuf) ((sbuf)->end - (sbuf)->content)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoretypedef char *edited_copy(char *from, char *to, int length);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * While the whatis line is being formed, it is stored in whatis_proto.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * When finished, it is reformatted into whatis_final and then appended
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * to whatis_lines.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic stringlist *whatis_lines; /* collected output lines */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic char tempfile[MAXPATHLEN]; /* path of temporary file, if any */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/* Free a struct page_info and its content */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Allocate and fill in a new struct page_info given the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * name of the man section directory and the dirent of the file.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * If the file is not a man page, return NULL.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic struct page_info *
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorenew_page_info(char *dir, struct dirent *dirent)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ((info = malloc(sizeof (struct page_info))) == NULL)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (asprintf(&info->filename, "%s/%s", dir, dirent->d_name) == -1)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (--suffix == dirent->d_name || !isalnum(*suffix)) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Reset sbuf length to 0.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Allocate a new sbuf.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorestatic struct sbuf *
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ((sbuf = malloc(sizeof (struct sbuf))) == NULL)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ((sbuf->content = (char *)malloc(LINE_ALLOC)) == NULL)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore sbuf->last = sbuf->content + LINE_ALLOC - 1;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Ensure that there is enough room in the sbuf
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * for nchars more characters.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore grow += 128; /* we grow in chunks of 128 bytes */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Grow if the buffer isn't big enough */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ((new_content = realloc(sbuf->content, size)) == NULL) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Append a string of a given length to the sbuf.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoresbuf_append(struct sbuf *sbuf, const char *text, int length)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Append a null-terminated string to the sbuf.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoresbuf_append_str(struct sbuf *sbuf, char *text)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Append an edited null-terminated string to the sbuf.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoresbuf_append_edited(struct sbuf *sbuf, char *text, edited_copy copy)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Strip any of a set of chars from the end of the sbuf.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoresbuf_strip(struct sbuf *sbuf, const char *set)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while (sbuf->end > sbuf->content && strchr(set, sbuf->end[-1]) != NULL)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Return the null-terminated string built by the sbuf.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Return true if no man page exists in the directory with
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * any of the names in the stringlist.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreno_page_exists(char *dir, stringlist *names, char *suffix)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore char *suffixes[] = { "", ".gz", ".bz2", NULL };
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (void) snprintf(path, MAXPATHLEN, "%s/%s.%s%s",
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore dir, names->sl_str[i], suffix, suffixes[j]);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore/* ARGSUSED sig */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Attempt to open an output file.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Return NULL if unsuccessful.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (void) snprintf(tempfile, MAXPATHLEN, "%s.tmp", name);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore return (strcmp((*(const char * const *)a), (*(const char * const *)b)));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Write the unique sorted lines to the output file.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore qsort(whatis_lines->sl_str, whatis_lines->sl_cur, sizeof (char *),
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < whatis_lines->sl_cur; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (void) snprintf(filename, MAXPATHLEN, "%s/%s", mandir, WHATIS);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (void) snprintf(filename, MAXPATHLEN, "%s/%s", mandir, WHATIS);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Remove trailing spaces from a string, returning a pointer to just
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * beyond the new last character.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Return a pointer to the next non-space character in the string.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Return whether the line is of one of the forms:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * assuming that section_start is ".Sh".
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorename_section_line(char *line, const char *section_start)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Copy characters while removing the most common nroff/troff markup:
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * \(em, \(mi, \s[+-N], \&
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * \fF, \f(fo, \f[font]
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * \*s, \*(st, \*[stringvar]
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amorede_nroff_copy(char *from, char *to, int fromlen)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore switch (*++from) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Append a string with the nroff formatting removed.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore sbuf_append_edited(whatis_proto, text, de_nroff_copy);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Appends "name(suffix), " to whatis_final
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Processes an old-style man(7) line. This ignores commands with only
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * a single number argument.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while (*p != '\0') {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (*p == '\0')
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Processes a new-style mdoc(7) line.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore int orig_length = sbuf_length(whatis_proto);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (line[0] != '.' || !isupper(line[1]) || !islower(line[2])) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while ((line = skip_spaces(line)) < line_end) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (void) memmove(next, next + 1, strlen(next));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (isupper(*line) && islower(line[1]) && line[2] == '\0') {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (arg > 0 && strchr(",.:;?!)]", *line) == 0) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if (sbuf_length(whatis_proto) > orig_length)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Collect a list of comma-separated names from the text.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreenum { STATE_UNKNOWN, STATE_MANSTYLE, STATE_MDOCNAME, STATE_MDOCDESC };
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Process a man page source into a single whatis line and add it
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * to whatis_lines.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amoreprocess_page(struct page_info *page, char *section_dir)
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore if ((fp = fopen(page->filename, "r")) == NULL) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Skip comments */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Haven't reached the NAME section yet */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Inside an old-style .SH NAME section */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Inside a new-style .Sh NAME section (the .Nm part) */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* FALLTHROUGH */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Inside a new-style .Sh NAME section (after the .Nm-s) */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Line now contains the appropriate data, but without the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * proper indentation or the section appended to each name.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore no_page_exists(section_dir, names, page->suffix)) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Add the page name since that's the only
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * thing that man(1) will find.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore add_whatis_name(names->sl_str[i], page->suffix);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Remove last ", " */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore sbuf_append_str(whatis_final, skip_spaces(descr));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (void) sl_add(whatis_lines, strdup(sbuf_content(whatis_final)));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Sort pages first by inode number, then by name.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const struct page_info *p1 = *(struct page_info * const *) a;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore const struct page_info *p2 = *(struct page_info * const *) b;
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Process a single man section.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Scan the man section directory for pages */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore nentries = scandir(section_dir, &entries, NULL, alphasort);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Collect information about man pages */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore pages = (struct page_info **)calloc(nentries,
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore sizeof (struct page_info *));
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < nentries; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore struct page_info *info = new_page_info(section_dir, entries[i]);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore qsort(pages, npages, sizeof (struct page_info *), pagesort);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore /* Process each unique page */
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < npages; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Return whether the directory entry is a man page section.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore while (*p != '\0') {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * Process a single top-level man directory by finding all the
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore * sub-directories named man* and processing each one in turn.
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore nsections = scandir(path, &entries, select_sections, alphasort);
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore for (i = 0; i < nsections; i++) {
95c635efb7c3b86efc493e0447eaec7aecca3f0fGarrett D'Amore (void) snprintf(section_dir, MAXPATHLEN, "%s/%s",