#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "man.h"
#include "stringlist.h"
struct page_info {
char *filename;
char *name;
char *suffix;
ino_t inode;
};
struct sbuf {
char *content;
char *end;
char *last;
};
#define sbuf_retract(sbuf, amount) ((sbuf)->end -= (amount))
#define sbuf_length(sbuf) ((sbuf)->end - (sbuf)->content)
typedef char *edited_copy(char *from, char *to, int length);
static struct sbuf *whatis_proto;
static struct sbuf *whatis_final;
static stringlist *whatis_lines;
static char tempfile[MAXPATHLEN];
#define MDOC_COMMANDS "ArDvErEvFlLiNmPa"
static void
free_page_info(struct page_info *info)
{
free(info->filename);
free(info->name);
free(info->suffix);
free(info);
}
static struct page_info *
new_page_info(char *dir, struct dirent *dirent)
{
struct page_info *info;
int basename_length;
char *suffix;
struct stat st;
if ((info = malloc(sizeof (struct page_info))) == NULL)
err(1, "malloc");
basename_length = strlen(dirent->d_name);
suffix = &dirent->d_name[basename_length];
if (asprintf(&info->filename, "%s/%s", dir, dirent->d_name) == -1)
err(1, "asprintf");
for (;;) {
if (--suffix == dirent->d_name || !isalnum(*suffix)) {
if (*suffix == '.')
break;
free(info->filename);
free(info);
return (NULL);
}
}
*suffix++ = '\0';
info->name = strdup(dirent->d_name);
info->suffix = strdup(suffix);
if (stat(info->filename, &st) < 0) {
warn("%s", info->filename);
free_page_info(info);
return (NULL);
}
if (!S_ISREG(st.st_mode)) {
free_page_info(info);
return (NULL);
}
info->inode = st.st_ino;
return (info);
}
static void
sbuf_clear(struct sbuf *sbuf)
{
sbuf->end = sbuf->content;
}
static struct sbuf *
new_sbuf(void)
{
struct sbuf *sbuf;
if ((sbuf = malloc(sizeof (struct sbuf))) == NULL)
err(1, "malloc");
if ((sbuf->content = (char *)malloc(LINE_ALLOC)) == NULL)
err(1, "malloc");
sbuf->last = sbuf->content + LINE_ALLOC - 1;
sbuf_clear(sbuf);
return (sbuf);
}
static void
sbuf_need(struct sbuf *sbuf, int nchars)
{
char *new_content;
size_t size, cntsize;
size_t grow = 128;
while (grow < nchars) {
grow += 128;
}
if (sbuf->end + nchars > sbuf->last) {
size = sbuf->last + 1 - sbuf->content;
size += grow;
cntsize = sbuf->end - sbuf->content;
if ((new_content = realloc(sbuf->content, size)) == NULL) {
perror("realloc");
if (tempfile[0] != '\0')
(void) unlink(tempfile);
exit(1);
}
sbuf->content = new_content;
sbuf->end = new_content + cntsize;
sbuf->last = new_content + size - 1;
}
}
static void
sbuf_append(struct sbuf *sbuf, const char *text, int length)
{
if (length > 0) {
sbuf_need(sbuf, length);
(void) memcpy(sbuf->end, text, length);
sbuf->end += length;
}
}
static void
sbuf_append_str(struct sbuf *sbuf, char *text)
{
sbuf_append(sbuf, text, strlen(text));
}
static void
sbuf_append_edited(struct sbuf *sbuf, char *text, edited_copy copy)
{
int length;
if ((length = strlen(text)) > 0) {
sbuf_need(sbuf, length);
sbuf->end = copy(text, sbuf->end, length);
}
}
static void
sbuf_strip(struct sbuf *sbuf, const char *set)
{
while (sbuf->end > sbuf->content && strchr(set, sbuf->end[-1]) != NULL)
sbuf->end--;
}
static char *
sbuf_content(struct sbuf *sbuf)
{
*sbuf->end = '\0';
return (sbuf->content);
}
static int
no_page_exists(char *dir, stringlist *names, char *suffix)
{
char path[MAXPATHLEN];
char *suffixes[] = { "", ".gz", ".bz2", NULL };
size_t i;
int j;
for (i = 0; i < names->sl_cur; i++) {
for (j = 0; suffixes[j] != NULL; j++) {
(void) snprintf(path, MAXPATHLEN, "%s/%s.%s%s",
dir, names->sl_str[i], suffix, suffixes[j]);
if (access(path, F_OK) == 0) {
return (0);
}
}
}
return (1);
}
static void
trap_signal(int sig)
{
if (tempfile[0] != '\0')
(void) unlink(tempfile);
exit(1);
}
static FILE *
open_output(char *name)
{
FILE *output;
whatis_lines = sl_init();
(void) snprintf(tempfile, MAXPATHLEN, "%s.tmp", name);
name = tempfile;
if ((output = fopen(name, "w")) == NULL) {
warn("%s", name);
return (NULL);
}
return (output);
}
static int
linesort(const void *a, const void *b)
{
return (strcmp((*(const char * const *)a), (*(const char * const *)b)));
}
static void
finish_output(FILE *output, char *name)
{
size_t i;
char *prev = NULL;
qsort(whatis_lines->sl_str, whatis_lines->sl_cur, sizeof (char *),
linesort);
for (i = 0; i < whatis_lines->sl_cur; i++) {
char *line = whatis_lines->sl_str[i];
if (i > 0 && strcmp(line, prev) == 0)
continue;
prev = line;
(void) fputs(line, output);
(void) putc('\n', output);
}
(void) fclose(output);
sl_free(whatis_lines, 1);
(void) rename(tempfile, name);
(void) unlink(tempfile);
}
static FILE *
open_whatis(char *mandir)
{
char filename[MAXPATHLEN];
(void) snprintf(filename, MAXPATHLEN, "%s/%s", mandir, WHATIS);
return (open_output(filename));
}
static void
finish_whatis(FILE *output, char *mandir)
{
char filename[MAXPATHLEN];
(void) snprintf(filename, MAXPATHLEN, "%s/%s", mandir, WHATIS);
finish_output(output, filename);
}
static char *
trim_rhs(char *str)
{
char *rhs;
rhs = &str[strlen(str)];
while (--rhs > str && isspace(*rhs))
;
*++rhs = '\0';
return (rhs);
}
static char *
skip_spaces(char *s)
{
while (*s != '\0' && isspace(*s))
s++;
return (s);
}
static int
name_section_line(char *line, const char *section_start)
{
char *rhs;
if (strncmp(line, section_start, 3) != 0)
return (0);
line = skip_spaces(line + 3);
rhs = trim_rhs(line);
if (*line == '"') {
line++;
if (*--rhs == '"')
*rhs = '\0';
}
if (strcmp(line, "NAME") == 0)
return (1);
return (0);
}
static char *
de_nroff_copy(char *from, char *to, int fromlen)
{
char *from_end = &from[fromlen];
while (from < from_end) {
switch (*from) {
case '\\':
switch (*++from) {
case '(':
if (strncmp(&from[1], "em", 2) == 0 ||
strncmp(&from[1], "mi", 2) == 0) {
from += 3;
continue;
}
break;
case 's':
if (*++from == '-')
from++;
while (isdigit(*from))
from++;
continue;
case 'f':
case '*':
if (*++from == '(') {
from += 3;
} else if (*from == '[') {
while (*++from != ']' &&
from < from_end)
;
from++;
} else {
from++;
}
continue;
case '&':
from++;
continue;
}
break;
}
*to++ = *from++;
}
return (to);
}
static void
add_nroff(char *text)
{
sbuf_append_edited(whatis_proto, text, de_nroff_copy);
}
static void
add_whatis_name(char *name, char *suffix)
{
if (*name != '\0') {
sbuf_append_str(whatis_final, name);
sbuf_append(whatis_final, "(", 1);
sbuf_append_str(whatis_final, suffix);
sbuf_append(whatis_final, "), ", 3);
}
}
static void
process_man_line(char *line)
{
char *p;
if (*line == '.') {
while (isalpha(*++line))
;
p = line = skip_spaces(line);
while (*p != '\0') {
if (!isdigit(*p))
break;
p++;
}
if (*p == '\0')
return;
} else
line = skip_spaces(line);
if (*line != '\0') {
add_nroff(line);
sbuf_append(whatis_proto, " ", 1);
}
}
static void
process_mdoc_line(char *line)
{
int xref;
int arg = 0;
char *line_end = &line[strlen(line)];
int orig_length = sbuf_length(whatis_proto);
char *next;
if (*line == '\0')
return;
if (line[0] != '.' || !isupper(line[1]) || !islower(line[2])) {
add_nroff(skip_spaces(line));
sbuf_append(whatis_proto, " ", 1);
return;
}
xref = strncmp(line, ".Xr", 3) == 0;
line += 3;
while ((line = skip_spaces(line)) < line_end) {
if (*line == '"') {
next = ++line;
for (;;) {
next = strchr(next, '"');
if (next == NULL)
break;
(void) memmove(next, next + 1, strlen(next));
line_end--;
if (*next != '"')
break;
next++;
}
} else {
next = strpbrk(line, " \t");
}
if (next != NULL)
*next++ = '\0';
else
next = line_end;
if (isupper(*line) && islower(line[1]) && line[2] == '\0') {
if (strcmp(line, "Ns") == 0) {
arg = 0;
line = next;
continue;
}
if (strstr(line, MDOC_COMMANDS) != NULL) {
line = next;
continue;
}
}
if (arg > 0 && strchr(",.:;?!)]", *line) == 0) {
if (xref) {
sbuf_append(whatis_proto, "(", 1);
add_nroff(line);
sbuf_append(whatis_proto, ")", 1);
xref = 0;
} else {
sbuf_append(whatis_proto, " ", 1);
}
}
add_nroff(line);
arg++;
line = next;
}
if (sbuf_length(whatis_proto) > orig_length)
sbuf_append(whatis_proto, " ", 1);
}
static void
collect_names(stringlist *names, char *text)
{
char *arg;
for (;;) {
arg = text;
text = strchr(text, ',');
if (text != NULL)
*text++ = '\0';
(void) sl_add(names, arg);
if (text == NULL)
return;
if (*text == ' ')
text++;
}
}
enum { STATE_UNKNOWN, STATE_MANSTYLE, STATE_MDOCNAME, STATE_MDOCDESC };
static void
process_page(struct page_info *page, char *section_dir)
{
FILE *fp;
stringlist *names;
char *descr;
int state = STATE_UNKNOWN;
size_t i;
char *line = NULL;
size_t linecap = 0;
sbuf_clear(whatis_proto);
if ((fp = fopen(page->filename, "r")) == NULL) {
warn("%s", page->filename);
return;
}
while (getline(&line, &linecap, fp) > 0) {
if (strncmp(line, ".\\\"", 3) == 0)
continue;
switch (state) {
case STATE_UNKNOWN:
if (name_section_line(line, ".SH"))
state = STATE_MANSTYLE;
else if (name_section_line(line, ".Sh"))
state = STATE_MDOCNAME;
continue;
case STATE_MANSTYLE: {
char *altline;
if (strncmp(line, ".SH", 3) == 0 ||
strncmp(line, ".SS", 3) == 0)
break;
(void) trim_rhs(line);
if (strcmp(line, ".") == 0)
continue;
altline = line;
if (strncmp(altline, ".IX", 3) == 0) {
altline += 3;
altline = skip_spaces(altline);
}
process_man_line(altline);
continue;
}
case STATE_MDOCNAME:
(void) trim_rhs(line);
if (strncmp(line, ".Nm", 3) == 0) {
process_mdoc_line(line);
continue;
} else {
if (strcmp(line, ".") == 0)
continue;
sbuf_append(whatis_proto, "- ", 2);
state = STATE_MDOCDESC;
}
case STATE_MDOCDESC:
if (strncmp(line, ".Sh", 3) == 0)
break;
(void) trim_rhs(line);
if (strcmp(line, ".") == 0)
continue;
process_mdoc_line(line);
continue;
}
break;
}
(void) fclose(fp);
sbuf_strip(whatis_proto, " \t.-");
line = sbuf_content(whatis_proto);
descr = strstr(line, " - ");
if (descr == NULL) {
descr = strchr(line, ' ');
if (descr == NULL)
return;
*descr++ = '\0';
} else {
*descr = '\0';
descr += 3;
}
names = sl_init();
collect_names(names, line);
sbuf_clear(whatis_final);
if (!sl_find(names, page->name) &&
no_page_exists(section_dir, names, page->suffix)) {
add_whatis_name(page->name, page->suffix);
}
for (i = 0; i < names->sl_cur; i++)
add_whatis_name(names->sl_str[i], page->suffix);
sl_free(names, 0);
sbuf_retract(whatis_final, 2);
while (sbuf_length(whatis_final) < INDENT)
sbuf_append(whatis_final, " ", 1);
sbuf_append(whatis_final, " - ", 3);
sbuf_append_str(whatis_final, skip_spaces(descr));
(void) sl_add(whatis_lines, strdup(sbuf_content(whatis_final)));
}
static int
pagesort(const void *a, const void *b)
{
const struct page_info *p1 = *(struct page_info * const *) a;
const struct page_info *p2 = *(struct page_info * const *) b;
if (p1->inode == p2->inode)
return (strcmp(p1->name, p2->name));
return (p1->inode - p2->inode);
}
static void
process_section(char *section_dir)
{
struct dirent **entries;
int nentries;
struct page_info **pages;
int npages = 0;
int i;
ino_t prev_inode = 0;
nentries = scandir(section_dir, &entries, NULL, alphasort);
pages = (struct page_info **)calloc(nentries,
sizeof (struct page_info *));
for (i = 0; i < nentries; i++) {
struct page_info *info = new_page_info(section_dir, entries[i]);
if (info != NULL)
pages[npages++] = info;
free(entries[i]);
}
free(entries);
qsort(pages, npages, sizeof (struct page_info *), pagesort);
for (i = 0; i < npages; i++) {
struct page_info *page = pages[i];
if (page->inode != prev_inode) {
prev_inode = page->inode;
process_page(page, section_dir);
}
free_page_info(page);
}
free(pages);
}
static int
select_sections(const struct dirent *entry)
{
const char *p = &entry->d_name[3];
if (strncmp(entry->d_name, "man", 3) != 0)
return (0);
while (*p != '\0') {
if (!isalnum(*p++))
return (0);
}
return (1);
}
void
mwpath(char *path)
{
FILE *fp = NULL;
struct dirent **entries;
int nsections;
int i;
(void) signal(SIGINT, trap_signal);
(void) signal(SIGHUP, trap_signal);
(void) signal(SIGQUIT, trap_signal);
(void) signal(SIGTERM, trap_signal);
whatis_proto = new_sbuf();
whatis_final = new_sbuf();
nsections = scandir(path, &entries, select_sections, alphasort);
if ((fp = open_whatis(path)) == NULL)
return;
for (i = 0; i < nsections; i++) {
char section_dir[MAXPATHLEN];
(void) snprintf(section_dir, MAXPATHLEN, "%s/%s",
path, entries[i]->d_name);
process_section(section_dir);
free(entries[i]);
}
free(entries);
finish_whatis(fp, path);
}