version 1.39, 2011/12/25 14:58:39 |
version 1.49.2.15, 2014/03/23 12:37:58 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
#include "config.h" |
#include "config.h" |
#endif |
#endif |
|
|
#include <sys/param.h> |
|
#include <sys/types.h> |
#include <sys/types.h> |
|
|
#include <assert.h> |
#include <assert.h> |
|
#include <ctype.h> |
#include <dirent.h> |
#include <dirent.h> |
|
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <getopt.h> |
#include <getopt.h> |
|
#include <limits.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdint.h> |
#include <stdint.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
#if defined(__linux__) |
#if defined(__APPLE__) |
|
# include <libkern/OSByteOrder.h> |
|
#elif defined(__linux__) |
# include <endian.h> |
# include <endian.h> |
|
#elif defined(__sun) |
|
# include <sys/byteorder.h> |
|
# include <sys/stat.h> |
|
#else |
|
# include <sys/endian.h> |
|
#endif |
|
|
|
#if defined(__linux__) || defined(__sun) |
# include <db_185.h> |
# include <db_185.h> |
#elif defined(__APPLE__) |
|
# include <libkern/OSByteOrder.h> |
|
# include <db.h> |
|
#else |
#else |
# include <db.h> |
# include <db.h> |
#endif |
#endif |
|
|
#include "man.h" |
#include "man.h" |
#include "mdoc.h" |
#include "mdoc.h" |
#include "mandoc.h" |
#include "mandoc.h" |
|
#include "mandoc_aux.h" |
#include "mandocdb.h" |
#include "mandocdb.h" |
#include "manpath.h" |
#include "manpath.h" |
|
|
|
|
/* Access to the mandoc database on disk. */ |
/* Access to the mandoc database on disk. */ |
|
|
struct mdb { |
struct mdb { |
char idxn[MAXPATHLEN]; /* index db filename */ |
char idxn[PATH_MAX]; /* index db filename */ |
char dbn[MAXPATHLEN]; /* keyword db filename */ |
char dbn[PATH_MAX]; /* keyword db filename */ |
DB *idx; /* index recno database */ |
DB *idx; /* index recno database */ |
DB *db; /* keyword btree database */ |
DB *db; /* keyword btree database */ |
}; |
}; |
Line 125 static void index_merge(const struct of *, struct m |
|
Line 135 static void index_merge(const struct of *, struct m |
|
struct mdb *, struct recs *); |
struct mdb *, struct recs *); |
static void index_prune(const struct of *, struct mdb *, |
static void index_prune(const struct of *, struct mdb *, |
struct recs *); |
struct recs *); |
static void ofile_argbuild(int, char *[], struct of **); |
static void ofile_argbuild(int, char *[], struct of **, |
|
const char *); |
static void ofile_dirbuild(const char *, const char *, |
static void ofile_dirbuild(const char *, const char *, |
const char *, int, struct of **); |
const char *, int, struct of **); |
static void ofile_free(struct of *); |
static void ofile_free(struct of *); |
static void pformatted(DB *, struct buf *, struct buf *, |
static void pformatted(DB *, struct buf *, |
const struct of *); |
struct buf *, const struct of *); |
static int pman_node(MAN_ARGS); |
static int pman_node(MAN_ARGS); |
static void pmdoc_node(MDOC_ARGS); |
static void pmdoc_node(MDOC_ARGS); |
static int pmdoc_head(MDOC_ARGS); |
static int pmdoc_head(MDOC_ARGS); |
Line 278 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
Line 289 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
}; |
}; |
|
|
static const char *progname; |
static const char *progname; |
|
static int mparse_options; /* abort the parse early */ |
static int use_all; /* Use all directories and files. */ |
static int use_all; /* Use all directories and files. */ |
static int verb; /* Output verbosity level. */ |
static int verb; /* Output verbosity level. */ |
static int warnings; /* Potential problems in manuals. */ |
static int warnings; /* Potential problems in manuals. */ |
Line 296 main(int argc, char *argv[]) |
|
Line 308 main(int argc, char *argv[]) |
|
int ch, i, flags; |
int ch, i, flags; |
DB *hash; /* temporary keyword hashtable */ |
DB *hash; /* temporary keyword hashtable */ |
BTREEINFO info; /* btree configuration */ |
BTREEINFO info; /* btree configuration */ |
size_t sz1, sz2; |
size_t sz1, sz2, ipath; |
struct buf buf, /* keyword buffer */ |
struct buf buf, /* keyword buffer */ |
dbuf; /* description buffer */ |
dbuf; /* description buffer */ |
struct of *of; /* list of files for processing */ |
struct of *of; /* list of files for processing */ |
Line 318 main(int argc, char *argv[]) |
|
Line 330 main(int argc, char *argv[]) |
|
hash = NULL; |
hash = NULL; |
op = OP_DEFAULT; |
op = OP_DEFAULT; |
dir = NULL; |
dir = NULL; |
|
mparse_options = MPARSE_SO; |
|
|
while (-1 != (ch = getopt(argc, argv, "aC:d:tu:vW"))) |
while (-1 != (ch = getopt(argc, argv, "aC:d:Qtu:vW"))) |
switch (ch) { |
switch (ch) { |
case ('a'): |
case ('a'): |
use_all = 1; |
use_all = 1; |
Line 342 main(int argc, char *argv[]) |
|
Line 355 main(int argc, char *argv[]) |
|
dir = optarg; |
dir = optarg; |
op = OP_UPDATE; |
op = OP_UPDATE; |
break; |
break; |
|
case ('Q'): |
|
mparse_options |= MPARSE_QUICK; |
|
break; |
case ('t'): |
case ('t'): |
dup2(STDOUT_FILENO, STDERR_FILENO); |
dup2(STDOUT_FILENO, STDERR_FILENO); |
if (op) { |
if (op) { |
Line 381 main(int argc, char *argv[]) |
|
Line 397 main(int argc, char *argv[]) |
|
} |
} |
|
|
memset(&info, 0, sizeof(BTREEINFO)); |
memset(&info, 0, sizeof(BTREEINFO)); |
|
info.lorder = 4321; |
info.flags = R_DUP; |
info.flags = R_DUP; |
|
|
mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL); |
mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL, NULL); |
|
|
memset(&buf, 0, sizeof(struct buf)); |
memset(&buf, 0, sizeof(struct buf)); |
memset(&dbuf, 0, sizeof(struct buf)); |
memset(&dbuf, 0, sizeof(struct buf)); |
Line 393 main(int argc, char *argv[]) |
|
Line 410 main(int argc, char *argv[]) |
|
buf.cp = mandoc_malloc(buf.size); |
buf.cp = mandoc_malloc(buf.size); |
dbuf.cp = mandoc_malloc(dbuf.size); |
dbuf.cp = mandoc_malloc(dbuf.size); |
|
|
flags = O_CREAT | O_RDWR; |
|
if (OP_DEFAULT == op || OP_CONFFILE == op) |
|
flags |= O_TRUNC; |
|
|
|
if (OP_TEST == op) { |
if (OP_TEST == op) { |
ofile_argbuild(argc, argv, &of); |
ofile_argbuild(argc, argv, &of, NULL); |
if (NULL == of) |
if (NULL == of) |
goto out; |
goto out; |
index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs); |
index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs); |
Line 406 main(int argc, char *argv[]) |
|
Line 419 main(int argc, char *argv[]) |
|
} |
} |
|
|
if (OP_UPDATE == op || OP_DELETE == op) { |
if (OP_UPDATE == op || OP_DELETE == op) { |
strlcat(mdb.dbn, dir, MAXPATHLEN); |
if (NULL == realpath(dir, pbuf)) { |
strlcat(mdb.dbn, "/", MAXPATHLEN); |
perror(dir); |
sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN); |
exit((int)MANDOCLEVEL_BADARG); |
|
} |
|
if (strlcat(pbuf, "/", PATH_MAX) >= PATH_MAX) { |
|
fprintf(stderr, "%s: path too long\n", pbuf); |
|
exit((int)MANDOCLEVEL_BADARG); |
|
} |
|
|
strlcat(mdb.idxn, dir, MAXPATHLEN); |
strlcat(mdb.dbn, pbuf, PATH_MAX); |
strlcat(mdb.idxn, "/", MAXPATHLEN); |
sz1 = strlcat(mdb.dbn, MANDOC_DB, PATH_MAX); |
sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN); |
|
|
|
if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) { |
strlcat(mdb.idxn, pbuf, PATH_MAX); |
fprintf(stderr, "%s: path too long\n", dir); |
sz2 = strlcat(mdb.idxn, MANDOC_IDX, PATH_MAX); |
|
|
|
if (sz1 >= PATH_MAX || sz2 >= PATH_MAX) { |
|
fprintf(stderr, "%s: path too long\n", mdb.idxn); |
exit((int)MANDOCLEVEL_BADARG); |
exit((int)MANDOCLEVEL_BADARG); |
} |
} |
|
|
|
flags = O_CREAT | O_RDWR; |
mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info); |
mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info); |
mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL); |
mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL); |
|
|
Line 430 main(int argc, char *argv[]) |
|
Line 451 main(int argc, char *argv[]) |
|
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
ofile_argbuild(argc, argv, &of); |
ofile_argbuild(argc, argv, &of, pbuf); |
|
|
if (NULL == of) |
if (NULL == of) |
goto out; |
goto out; |
Line 474 main(int argc, char *argv[]) |
|
Line 495 main(int argc, char *argv[]) |
|
} else |
} else |
manpath_parse(&dirs, dir, NULL, NULL); |
manpath_parse(&dirs, dir, NULL, NULL); |
|
|
for (i = 0; i < dirs.sz; i++) { |
for (ipath = 0; ipath < dirs.sz; ipath++) { |
mdb.idxn[0] = mdb.dbn[0] = '\0'; |
|
|
|
strlcat(mdb.dbn, dirs.paths[i], MAXPATHLEN); |
/* |
strlcat(mdb.dbn, "/", MAXPATHLEN); |
* Go to the root of the respective manual tree. |
sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN); |
* This must work or no manuals may be found: |
|
* They are indexed relative to the root. |
|
*/ |
|
|
strlcat(mdb.idxn, dirs.paths[i], MAXPATHLEN); |
if (-1 == chdir(dirs.paths[ipath])) { |
strlcat(mdb.idxn, "/", MAXPATHLEN); |
perror(dirs.paths[ipath]); |
sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN); |
exit((int)MANDOCLEVEL_SYSERR); |
|
|
if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) { |
|
fprintf(stderr, "%s: path too long\n", |
|
dirs.paths[i]); |
|
exit((int)MANDOCLEVEL_BADARG); |
|
} |
} |
|
|
if (mdb.db) |
/* Create a new database in two temporary files. */ |
(*mdb.db->close)(mdb.db); |
|
if (mdb.idx) |
|
(*mdb.idx->close)(mdb.idx); |
|
|
|
mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info); |
flags = O_CREAT | O_EXCL | O_RDWR; |
mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL); |
while (NULL == mdb.db) { |
|
strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX); |
if (NULL == mdb.db) { |
strlcat(mdb.dbn, ".XXXXXXXXXX", PATH_MAX); |
perror(mdb.dbn); |
if (NULL == mktemp(mdb.dbn)) { |
exit((int)MANDOCLEVEL_SYSERR); |
perror(mdb.dbn); |
} else if (NULL == mdb.idx) { |
exit((int)MANDOCLEVEL_SYSERR); |
perror(mdb.idxn); |
} |
exit((int)MANDOCLEVEL_SYSERR); |
mdb.db = dbopen(mdb.dbn, flags, 0644, |
|
DB_BTREE, &info); |
|
if (NULL == mdb.db && EEXIST != errno) { |
|
perror(mdb.dbn); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
} |
} |
|
while (NULL == mdb.idx) { |
|
strlcpy(mdb.idxn, MANDOC_IDX, PATH_MAX); |
|
strlcat(mdb.idxn, ".XXXXXXXXXX", PATH_MAX); |
|
if (NULL == mktemp(mdb.idxn)) { |
|
perror(mdb.idxn); |
|
unlink(mdb.dbn); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
mdb.idx = dbopen(mdb.idxn, flags, 0644, |
|
DB_RECNO, NULL); |
|
if (NULL == mdb.idx && EEXIST != errno) { |
|
perror(mdb.idxn); |
|
unlink(mdb.dbn); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
} |
|
|
ofile_free(of); |
/* |
of = NULL; |
* Search for manuals and fill the new database. |
|
*/ |
|
|
if (-1 == chdir(dirs.paths[i])) { |
ofile_dirbuild(".", "", "", 0, &of); |
perror(dirs.paths[i]); |
|
exit((int)MANDOCLEVEL_SYSERR); |
if (NULL != of) { |
|
index_merge(of, mp, &dbuf, &buf, hash, |
|
&mdb, &recs); |
|
ofile_free(of); |
|
of = NULL; |
} |
} |
|
|
ofile_dirbuild(".", "", "", 0, &of); |
(*mdb.db->close)(mdb.db); |
if (NULL == of) |
(*mdb.idx->close)(mdb.idx); |
continue; |
mdb.db = NULL; |
|
mdb.idx = NULL; |
|
|
/* |
/* |
* Go to the root of the respective manual tree. |
* Replace the old database with the new one. |
* This must work or no manuals may be found (they're |
* This is not perfectly atomic, |
* indexed relative to the root). |
* but i cannot think of a better way. |
*/ |
*/ |
|
|
if (-1 == chdir(dirs.paths[i])) { |
if (-1 == rename(mdb.dbn, MANDOC_DB)) { |
perror(dirs.paths[i]); |
perror(MANDOC_DB); |
|
unlink(mdb.dbn); |
|
unlink(mdb.idxn); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
if (-1 == rename(mdb.idxn, MANDOC_IDX)) { |
index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs); |
perror(MANDOC_IDX); |
|
unlink(MANDOC_DB); |
|
unlink(MANDOC_IDX); |
|
unlink(mdb.idxn); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
} |
} |
|
|
out: |
out: |
|
|
|
|
usage: |
usage: |
fprintf(stderr, |
fprintf(stderr, |
"usage: %s [-avvv] [-C file] | dir ... | -t file ...\n" |
"usage: %s [-aQvvv] [-C file] | dir ... | -t file ...\n" |
" -d dir [file ...] | " |
" -d dir [file ...] | " |
"-u dir [file ...]\n", |
"-u dir [file ...]\n", |
progname); |
progname); |
Line 569 index_merge(const struct of *of, struct mparse *mp, |
|
Line 617 index_merge(const struct of *of, struct mparse *mp, |
|
recno_t rec; |
recno_t rec; |
int ch, skip; |
int ch, skip; |
DBT key, val; |
DBT key, val; |
|
DB *files; /* temporary file name table */ |
struct mdoc *mdoc; |
struct mdoc *mdoc; |
struct man *man; |
struct man *man; |
const char *fn, *msec, *march, *mtitle; |
const char *fn, *msec, *march, *mtitle; |
|
char *p; |
uint64_t mask; |
uint64_t mask; |
size_t sv; |
size_t sv; |
unsigned seq; |
unsigned seq; |
uint64_t vbuf[2]; |
uint64_t vbuf[2]; |
char type; |
char type; |
|
|
|
static char emptystring[] = ""; |
|
|
|
if (warnings) { |
|
files = NULL; |
|
hash_reset(&files); |
|
} |
|
|
rec = 0; |
rec = 0; |
for (of = of->first; of; of = of->next) { |
for (of = of->first; of; of = of->next) { |
fn = of->fname; |
fn = of->fname; |
Line 595 index_merge(const struct of *of, struct mparse *mp, |
|
Line 652 index_merge(const struct of *of, struct mparse *mp, |
|
if ((MANDOC_SRC & of->src_form || |
if ((MANDOC_SRC & of->src_form || |
! (MANDOC_FORM & of->src_form)) && |
! (MANDOC_FORM & of->src_form)) && |
MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn)) |
MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn)) |
mparse_result(mp, &mdoc, &man); |
mparse_result(mp, &mdoc, &man, NULL); |
|
|
if (NULL != mdoc) { |
if (NULL != mdoc) { |
msec = mdoc_meta(mdoc)->msec; |
msec = mdoc_meta(mdoc)->msec; |
Line 614 index_merge(const struct of *of, struct mparse *mp, |
|
Line 671 index_merge(const struct of *of, struct mparse *mp, |
|
} |
} |
|
|
/* |
/* |
* By default, skip a file if the manual section |
* Check whether the manual section given in a file |
* and architecture given in the file disagree |
* agrees with the directory where the file is located. |
* with the directory where the file is located. |
* Some manuals have suffixes like (3p) on their |
|
* section number either inside the file or in the |
|
* directory name, some are linked into more than one |
|
* section, like encrypt(1) = makekey(8). Do not skip |
|
* manuals for such reasons. |
*/ |
*/ |
|
|
skip = 0; |
skip = 0; |
assert(of->sec); |
assert(of->sec); |
assert(msec); |
assert(msec); |
if (strcasecmp(msec, of->sec)) { |
if (warnings) |
if (warnings) |
if (strcasecmp(msec, of->sec)) |
fprintf(stderr, "%s: " |
fprintf(stderr, "%s: " |
"section \"%s\" manual " |
"section \"%s\" manual " |
"in \"%s\" directory\n", |
"in \"%s\" directory\n", |
fn, msec, of->sec); |
fn, msec, of->sec); |
skip = 1; |
|
} |
|
|
|
|
/* |
|
* Manual page directories exist for each kernel |
|
* architecture as returned by machine(1). |
|
* However, many manuals only depend on the |
|
* application architecture as returned by arch(1). |
|
* For example, some (2/ARM) manuals are shared |
|
* across the "armish" and "zaurus" kernel |
|
* architectures. |
|
* A few manuals are even shared across completely |
|
* different architectures, for example fdformat(1) |
|
* on amd64, i386, sparc, and sparc64. |
|
* Thus, warn about architecture mismatches, |
|
* but don't skip manuals for this reason. |
|
*/ |
|
|
assert(of->arch); |
assert(of->arch); |
assert(march); |
assert(march); |
if (strcasecmp(march, of->arch)) { |
if (warnings) |
if (warnings) |
if (strcasecmp(march, of->arch)) |
fprintf(stderr, "%s: " |
fprintf(stderr, "%s: " |
"architecture \"%s\" manual " |
"architecture \"%s\" manual " |
"in \"%s\" directory\n", |
"in \"%s\" directory\n", |
fn, march, of->arch); |
fn, march, of->arch); |
skip = 1; |
|
} |
|
|
|
/* |
/* |
* By default, skip a file if the title given |
* By default, skip a file if the title given |
* in the file disagrees with the file name. |
* in the file disagrees with the file name. |
* If both agree, use the file name as the title, |
* Do not warn, this happens for all MLINKs. |
* because the one in the file usually is all caps. |
|
*/ |
*/ |
|
|
assert(of->title); |
assert(of->title); |
assert(mtitle); |
assert(mtitle); |
if (strcasecmp(mtitle, of->title)) { |
if (strcasecmp(mtitle, of->title)) |
if (warnings) |
|
fprintf(stderr, "%s: " |
|
"title \"%s\" in file " |
|
"but \"%s\" in filename\n", |
|
fn, mtitle, of->title); |
|
skip = 1; |
skip = 1; |
} else |
|
mtitle = of->title; |
|
|
|
|
/* |
|
* Build a title string for the file. If it matches |
|
* the location of the file, remember the title as |
|
* found; else, remember it as missing. |
|
*/ |
|
|
|
if (warnings) { |
|
buf->len = 0; |
|
buf_appendb(buf, mtitle, strlen(mtitle)); |
|
buf_appendb(buf, "(", 1); |
|
buf_appendb(buf, msec, strlen(msec)); |
|
if ('\0' != *march) { |
|
buf_appendb(buf, "/", 1); |
|
buf_appendb(buf, march, strlen(march)); |
|
} |
|
buf_appendb(buf, ")", 2); |
|
for (p = buf->cp; '\0' != *p; p++) |
|
*p = tolower((unsigned char)*p); |
|
key.data = buf->cp; |
|
key.size = buf->len; |
|
val.data = NULL; |
|
val.size = 0; |
|
if (0 == skip) |
|
val.data = emptystring; |
|
else { |
|
ch = (*files->get)(files, &key, &val, 0); |
|
if (ch < 0) { |
|
perror("hash"); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} else if (ch > 0) { |
|
val.data = (void *)fn; |
|
val.size = strlen(fn) + 1; |
|
} else |
|
val.data = NULL; |
|
} |
|
if (NULL != val.data && |
|
(*files->put)(files, &key, &val, 0) < 0) { |
|
perror("hash"); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
} |
|
|
if (skip && !use_all) |
if (skip && !use_all) |
continue; |
continue; |
|
|
/* |
/* |
* The index record value consists of a nil-terminated |
* The index record value consists of a nil-terminated |
* filename, a nil-terminated manual section, and a |
* filename, a nil-terminated manual section, and a |
* nil-terminated description. Since the description |
* nil-terminated description. Use the actual |
* may not be set, we set a sentinel to see if we're |
* location of the file, such that the user can find |
* going to write a nil byte in its place. |
* it with man(1). Since the description may not be |
|
* set, we set a sentinel to see if we're going to |
|
* write a nil byte in its place. |
*/ |
*/ |
|
|
dbuf->len = 0; |
dbuf->len = 0; |
type = mdoc ? 'd' : (man ? 'a' : 'c'); |
type = mdoc ? 'd' : (man ? 'a' : 'c'); |
buf_appendb(dbuf, &type, 1); |
buf_appendb(dbuf, &type, 1); |
buf_appendb(dbuf, fn, strlen(fn) + 1); |
buf_appendb(dbuf, fn, strlen(fn) + 1); |
buf_appendb(dbuf, msec, strlen(msec) + 1); |
buf_appendb(dbuf, of->sec, strlen(of->sec) + 1); |
buf_appendb(dbuf, mtitle, strlen(mtitle) + 1); |
buf_appendb(dbuf, of->title, strlen(of->title) + 1); |
buf_appendb(dbuf, march, strlen(march) + 1); |
buf_appendb(dbuf, of->arch, strlen(of->arch) + 1); |
|
|
sv = dbuf->len; |
sv = dbuf->len; |
|
|
Line 702 index_merge(const struct of *of, struct mparse *mp, |
|
Line 810 index_merge(const struct of *of, struct mparse *mp, |
|
continue; |
continue; |
|
|
/* |
/* |
|
* Make sure the file name is always registered |
|
* as an .Nm search key. |
|
*/ |
|
buf->len = 0; |
|
buf_append(buf, of->title); |
|
hash_put(hash, buf, TYPE_Nm); |
|
|
|
/* |
* Reclaim an empty index record, if available. |
* Reclaim an empty index record, if available. |
* Use its record number for all new btree nodes. |
* Use its record number for all new btree nodes. |
*/ |
*/ |
Line 733 index_merge(const struct of *of, struct mparse *mp, |
|
Line 849 index_merge(const struct of *of, struct mparse *mp, |
|
} |
} |
if (ch < 0) { |
if (ch < 0) { |
perror("hash"); |
perror("hash"); |
|
unlink(mdb->dbn); |
|
unlink(mdb->idxn); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
Line 755 index_merge(const struct of *of, struct mparse *mp, |
|
Line 873 index_merge(const struct of *of, struct mparse *mp, |
|
|
|
dbt_put(mdb->idx, mdb->idxn, &key, &val); |
dbt_put(mdb->idx, mdb->idxn, &key, &val); |
} |
} |
|
|
|
/* |
|
* Iterate the remembered file titles and check that |
|
* all files can be found by their main title. |
|
*/ |
|
|
|
if (warnings) { |
|
seq = R_FIRST; |
|
while (0 == (*files->seq)(files, &key, &val, seq)) { |
|
seq = R_NEXT; |
|
if (val.size) |
|
fprintf(stderr, "%s: probably " |
|
"unreachable, title is %s\n", |
|
(char *)val.data, (char *)key.data); |
|
} |
|
(*files->close)(files); |
|
} |
} |
} |
|
|
/* |
/* |
|
|
pman_node(MAN_ARGS) |
pman_node(MAN_ARGS) |
{ |
{ |
const struct man_node *head, *body; |
const struct man_node *head, *body; |
const char *start, *sv; |
char *start, *title; |
size_t sz; |
size_t sz; |
|
|
if (NULL == n) |
if (NULL == n) |
Line 1269 pman_node(MAN_ARGS) |
|
Line 1404 pman_node(MAN_ARGS) |
|
NULL != (head = (head->child)) && |
NULL != (head = (head->child)) && |
MAN_TEXT == head->type && |
MAN_TEXT == head->type && |
0 == strcmp(head->string, "NAME") && |
0 == strcmp(head->string, "NAME") && |
NULL != (body = body->child) && |
NULL != body->child) { |
MAN_TEXT == body->type) { |
|
|
|
assert(body->string); |
/* |
start = sv = body->string; |
* Suck the entire NAME section into memory. |
|
* Yes, we might run away. |
|
* But too many manuals have big, spread-out |
|
* NAME sections over many lines. |
|
*/ |
|
|
|
title = NULL; |
|
man_deroff(&title, body); |
|
if (NULL == title) |
|
return(0); |
|
|
/* |
/* |
* Go through a special heuristic dance here. |
* Go through a special heuristic dance here. |
* This is why -man manuals are great! |
* This is why -man manuals are great! |
Line 1285 pman_node(MAN_ARGS) |
|
Line 1428 pman_node(MAN_ARGS) |
|
* the name parts here. |
* the name parts here. |
*/ |
*/ |
|
|
|
start = title; |
for ( ;; ) { |
for ( ;; ) { |
sz = strcspn(start, " ,"); |
sz = strcspn(start, " ,"); |
if ('\0' == start[(int)sz]) |
if ('\0' == start[(int)sz]) |
Line 1309 pman_node(MAN_ARGS) |
|
Line 1453 pman_node(MAN_ARGS) |
|
|
|
buf->len = 0; |
buf->len = 0; |
|
|
if (sv == start) { |
if (start == title) { |
buf_append(buf, start); |
buf_append(buf, start); |
|
free(title); |
return(1); |
return(1); |
} |
} |
|
|
while (' ' == *start) |
while (isspace((unsigned char)*start)) |
start++; |
start++; |
|
|
if (0 == strncmp(start, "-", 1)) |
if (0 == strncmp(start, "-", 1)) |
start += 1; |
start += 1; |
|
else if (0 == strncmp(start, "\\-\\-", 4)) |
|
start += 4; |
else if (0 == strncmp(start, "\\-", 2)) |
else if (0 == strncmp(start, "\\-", 2)) |
start += 2; |
start += 2; |
else if (0 == strncmp(start, "\\(en", 4)) |
else if (0 == strncmp(start, "\\(en", 4)) |
Line 1334 pman_node(MAN_ARGS) |
|
Line 1481 pman_node(MAN_ARGS) |
|
buf_appendb(buf, start, sz); |
buf_appendb(buf, start, sz); |
|
|
hash_put(hash, buf, TYPE_Nd); |
hash_put(hash, buf, TYPE_Nd); |
|
free(title); |
} |
} |
} |
} |
|
|
Line 1349 pman_node(MAN_ARGS) |
|
Line 1497 pman_node(MAN_ARGS) |
|
* By necessity, this involves rather crude guesswork. |
* By necessity, this involves rather crude guesswork. |
*/ |
*/ |
static void |
static void |
pformatted(DB *hash, struct buf *buf, struct buf *dbuf, |
pformatted(DB *hash, struct buf *buf, |
const struct of *of) |
struct buf *dbuf, const struct of *of) |
{ |
{ |
FILE *stream; |
FILE *stream; |
char *line, *p; |
char *line, *p, *title; |
size_t len, plen; |
size_t len, plen, titlesz; |
|
|
if (NULL == (stream = fopen(of->fname, "r"))) { |
if (NULL == (stream = fopen(of->fname, "r"))) { |
if (warnings) |
if (warnings) |
Line 1387 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
Line 1535 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
while (NULL != (line = fgetln(stream, &len))) |
while (NULL != (line = fgetln(stream, &len))) |
if ('\n' != *line && ' ' != *line) |
if ('\n' != *line && ' ' != *line) |
break; |
break; |
|
|
|
/* |
|
* Read up until the next section into a buffer. |
|
* Strip the leading and trailing newline from each read line, |
|
* appending a trailing space. |
|
* Ignore empty (whitespace-only) lines. |
|
*/ |
|
|
|
titlesz = 0; |
|
title = NULL; |
|
|
|
while (NULL != (line = fgetln(stream, &len))) { |
|
if (' ' != *line || '\n' != line[(int)len - 1]) |
|
break; |
|
while (len > 0 && isspace((unsigned char)*line)) { |
|
line++; |
|
len--; |
|
} |
|
if (1 == len) |
|
continue; |
|
title = mandoc_realloc(title, titlesz + len); |
|
memcpy(title + titlesz, line, len); |
|
titlesz += len; |
|
title[(int)titlesz - 1] = ' '; |
|
} |
|
|
|
|
/* |
/* |
* If no page content can be found, or the input line |
* If no page content can be found, or the input line |
* is already the next section header, or there is no |
* is already the next section header, or there is no |
Line 1395 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
Line 1569 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
* description. |
* description. |
*/ |
*/ |
|
|
line = fgetln(stream, &len); |
if (NULL == title || '\0' == *title) { |
if (NULL == line || ' ' != *line || '\n' != line[(int)len - 1]) { |
|
if (warnings) |
if (warnings) |
fprintf(stderr, "%s: cannot find NAME section\n", |
fprintf(stderr, "%s: cannot find NAME section\n", |
of->fname); |
of->fname); |
buf_appendb(dbuf, buf->cp, buf->size); |
buf_appendb(dbuf, buf->cp, buf->size); |
hash_put(hash, buf, TYPE_Nd); |
hash_put(hash, buf, TYPE_Nd); |
fclose(stream); |
fclose(stream); |
|
free(title); |
return; |
return; |
} |
} |
|
|
line[(int)--len] = '\0'; |
title = mandoc_realloc(title, titlesz + 1); |
|
title[(int)titlesz] = '\0'; |
|
|
/* |
/* |
* Skip to the first dash. |
* Skip to the first dash. |
Line 1414 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
Line 1589 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
* bytes). |
* bytes). |
*/ |
*/ |
|
|
if (NULL != (p = strstr(line, "- "))) { |
if (NULL != (p = strstr(title, "- "))) { |
for (p += 2; ' ' == *p || '\b' == *p; p++) |
for (p += 2; ' ' == *p || '\b' == *p; p++) |
/* Skip to next word. */ ; |
/* Skip to next word. */ ; |
} else { |
} else { |
if (warnings) |
if (warnings) |
fprintf(stderr, "%s: no dash in title line\n", |
fprintf(stderr, "%s: no dash in title line\n", |
of->fname); |
of->fname); |
p = line; |
p = title; |
} |
} |
|
|
if ((plen = strlen(p)) > 70) { |
plen = strlen(p); |
plen = 70; |
|
p[plen] = '\0'; |
|
} |
|
|
|
/* Strip backspace-encoding from line. */ |
/* Strip backspace-encoding from line. */ |
|
|
Line 1446 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
Line 1618 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
buf_appendb(buf, p, plen + 1); |
buf_appendb(buf, p, plen + 1); |
hash_put(hash, buf, TYPE_Nd); |
hash_put(hash, buf, TYPE_Nd); |
fclose(stream); |
fclose(stream); |
|
free(title); |
} |
} |
|
|
static void |
static void |
ofile_argbuild(int argc, char *argv[], struct of **of) |
ofile_argbuild(int argc, char *argv[], struct of **of, |
|
const char *basedir) |
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[PATH_MAX]; |
char *sec, *arch, *title, *p; |
char pbuf[PATH_MAX]; |
|
const char *sec, *arch, *title; |
|
char *relpath, *p; |
int i, src_form; |
int i, src_form; |
struct of *nof; |
struct of *nof; |
|
|
for (i = 0; i < argc; i++) { |
for (i = 0; i < argc; i++) { |
|
if (NULL == (relpath = realpath(argv[i], pbuf))) { |
|
perror(argv[i]); |
|
continue; |
|
} |
|
if (NULL != basedir) { |
|
if (strstr(pbuf, basedir) != pbuf) { |
|
fprintf(stderr, "%s: file outside " |
|
"base directory %s\n", |
|
pbuf, basedir); |
|
continue; |
|
} |
|
relpath = pbuf + strlen(basedir); |
|
} |
|
|
/* |
/* |
* Try to infer the manual section, architecture and |
* Try to infer the manual section, architecture and |
Line 1465 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
Line 1654 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
* cat<section>[/<arch>]/<title>.0 |
* cat<section>[/<arch>]/<title>.0 |
*/ |
*/ |
|
|
if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) { |
if (strlcpy(buf, relpath, sizeof(buf)) >= sizeof(buf)) { |
fprintf(stderr, "%s: path too long\n", argv[i]); |
fprintf(stderr, "%s: path too long\n", relpath); |
continue; |
continue; |
} |
} |
sec = arch = title = ""; |
sec = arch = title = ""; |
Line 1502 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
Line 1691 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
fprintf(stderr, |
fprintf(stderr, |
"%s: cannot deduce title " |
"%s: cannot deduce title " |
"from filename\n", |
"from filename\n", |
argv[i]); |
relpath); |
title = buf; |
title = buf; |
} |
} |
|
|
Line 1511 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
Line 1700 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
*/ |
*/ |
|
|
nof = mandoc_calloc(1, sizeof(struct of)); |
nof = mandoc_calloc(1, sizeof(struct of)); |
nof->fname = mandoc_strdup(argv[i]); |
nof->fname = mandoc_strdup(relpath); |
nof->sec = mandoc_strdup(sec); |
nof->sec = mandoc_strdup(sec); |
nof->arch = mandoc_strdup(arch); |
nof->arch = mandoc_strdup(arch); |
nof->title = mandoc_strdup(title); |
nof->title = mandoc_strdup(title); |
Line 1521 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
Line 1710 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
* Add the structure to the list. |
* Add the structure to the list. |
*/ |
*/ |
|
|
if (verb > 1) |
|
printf("%s: scheduling\n", argv[i]); |
|
if (NULL == *of) { |
if (NULL == *of) { |
*of = nof; |
*of = nof; |
(*of)->first = nof; |
(*of)->first = nof; |
|
|
ofile_dirbuild(const char *dir, const char* psec, const char *parch, |
ofile_dirbuild(const char *dir, const char* psec, const char *parch, |
int p_src_form, struct of **of) |
int p_src_form, struct of **of) |
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[PATH_MAX]; |
|
#if defined(__sun) |
|
struct stat sb; |
|
#endif |
size_t sz; |
size_t sz; |
DIR *d; |
DIR *d; |
const char *fn, *sec, *arch; |
const char *fn, *sec, *arch; |
Line 1569 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1759 ofile_dirbuild(const char *dir, const char* psec, cons |
|
|
|
src_form = p_src_form; |
src_form = p_src_form; |
|
|
|
#if defined(__sun) |
|
stat(dp->d_name, &sb); |
|
if (S_IFDIR & sb.st_mode) { |
|
#else |
if (DT_DIR == dp->d_type) { |
if (DT_DIR == dp->d_type) { |
|
#endif |
sec = psec; |
sec = psec; |
arch = parch; |
arch = parch; |
|
|
Line 1612 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1807 ofile_dirbuild(const char *dir, const char* psec, cons |
|
} |
} |
|
|
buf[0] = '\0'; |
buf[0] = '\0'; |
strlcat(buf, dir, MAXPATHLEN); |
strlcat(buf, dir, PATH_MAX); |
strlcat(buf, "/", MAXPATHLEN); |
strlcat(buf, "/", PATH_MAX); |
sz = strlcat(buf, fn, MAXPATHLEN); |
sz = strlcat(buf, fn, PATH_MAX); |
|
|
if (MAXPATHLEN <= sz) { |
if (PATH_MAX <= sz) { |
if (warnings) fprintf(stderr, "%s/%s: " |
if (warnings) fprintf(stderr, "%s/%s: " |
"path too long\n", dir, fn); |
"path too long\n", dir, fn); |
continue; |
continue; |
} |
} |
|
|
if (verb > 1) |
|
printf("%s: scanning\n", buf); |
|
|
|
ofile_dirbuild(buf, sec, arch, src_form, of); |
ofile_dirbuild(buf, sec, arch, src_form, of); |
continue; |
continue; |
} |
} |
|
|
|
#if defined(__sun) |
|
if (0 == S_IFREG & sb.st_mode) { |
|
#else |
if (DT_REG != dp->d_type) { |
if (DT_REG != dp->d_type) { |
|
#endif |
if (warnings) |
if (warnings) |
fprintf(stderr, |
fprintf(stderr, |
"%s/%s: not a regular file\n", |
"%s/%s: not a regular file\n", |
Line 1687 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1883 ofile_dirbuild(const char *dir, const char* psec, cons |
|
if (0 == use_all && MANDOC_FORM & src_form && |
if (0 == use_all && MANDOC_FORM & src_form && |
'\0' != *psec) { |
'\0' != *psec) { |
buf[0] = '\0'; |
buf[0] = '\0'; |
strlcat(buf, dir, MAXPATHLEN); |
strlcat(buf, dir, PATH_MAX); |
p = strrchr(buf, '/'); |
p = strrchr(buf, '/'); |
if ('\0' != *parch && NULL != p) |
if ('\0' != *parch && NULL != p) |
for (p--; p > buf; p--) |
for (p--; p > buf; p--) |
Line 1699 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1895 ofile_dirbuild(const char *dir, const char* psec, cons |
|
p++; |
p++; |
if (0 == strncmp("cat", p, 3)) |
if (0 == strncmp("cat", p, 3)) |
memcpy(p, "man", 3); |
memcpy(p, "man", 3); |
strlcat(buf, "/", MAXPATHLEN); |
strlcat(buf, "/", PATH_MAX); |
sz = strlcat(buf, fn, MAXPATHLEN); |
sz = strlcat(buf, fn, PATH_MAX); |
if (sz >= MAXPATHLEN) { |
if (sz >= PATH_MAX) { |
if (warnings) fprintf(stderr, |
if (warnings) fprintf(stderr, |
"%s/%s: path too long\n", |
"%s/%s: path too long\n", |
dir, fn); |
dir, fn); |
Line 1710 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1906 ofile_dirbuild(const char *dir, const char* psec, cons |
|
q = strrchr(buf, '.'); |
q = strrchr(buf, '.'); |
if (NULL != q && p < q++) { |
if (NULL != q && p < q++) { |
*q = '\0'; |
*q = '\0'; |
sz = strlcat(buf, psec, MAXPATHLEN); |
sz = strlcat(buf, psec, PATH_MAX); |
if (sz >= MAXPATHLEN) { |
if (sz >= PATH_MAX) { |
if (warnings) fprintf(stderr, |
if (warnings) fprintf(stderr, |
"%s/%s: path too long\n", |
"%s/%s: path too long\n", |
dir, fn); |
dir, fn); |
Line 1725 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1921 ofile_dirbuild(const char *dir, const char* psec, cons |
|
buf[0] = '\0'; |
buf[0] = '\0'; |
assert('.' == dir[0]); |
assert('.' == dir[0]); |
if ('/' == dir[1]) { |
if ('/' == dir[1]) { |
strlcat(buf, dir + 2, MAXPATHLEN); |
strlcat(buf, dir + 2, PATH_MAX); |
strlcat(buf, "/", MAXPATHLEN); |
strlcat(buf, "/", PATH_MAX); |
} |
} |
sz = strlcat(buf, fn, MAXPATHLEN); |
sz = strlcat(buf, fn, PATH_MAX); |
if (sz >= MAXPATHLEN) { |
if (sz >= PATH_MAX) { |
if (warnings) fprintf(stderr, |
if (warnings) fprintf(stderr, |
"%s/%s: path too long\n", dir, fn); |
"%s/%s: path too long\n", dir, fn); |
continue; |
continue; |
Line 1754 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1950 ofile_dirbuild(const char *dir, const char* psec, cons |
|
* Add the structure to the list. |
* Add the structure to the list. |
*/ |
*/ |
|
|
if (verb > 1) |
|
printf("%s: scheduling\n", buf); |
|
if (NULL == *of) { |
if (NULL == *of) { |
*of = nof; |
*of = nof; |
(*of)->first = nof; |
(*of)->first = nof; |
Line 1774 ofile_free(struct of *of) |
|
Line 1968 ofile_free(struct of *of) |
|
{ |
{ |
struct of *nof; |
struct of *nof; |
|
|
while (of) { |
if (NULL != of) |
|
of = of->first; |
|
|
|
while (NULL != of) { |
nof = of->next; |
nof = of->next; |
free(of->fname); |
free(of->fname); |
free(of->sec); |
free(of->sec); |