version 1.49.2.2, 2013/09/17 22:48:53 |
version 1.49.2.16, 2014/04/23 21:31:38 |
|
|
#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" |
|
|
Line 281 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 299 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 321 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 345 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 387 main(int argc, char *argv[]) |
|
Line 400 main(int argc, char *argv[]) |
|
info.lorder = 4321; |
info.lorder = 4321; |
info.flags = R_DUP; |
info.flags = R_DUP; |
|
|
mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, 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 482 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++) { |
|
|
/* |
/* |
* Go to the root of the respective manual tree. |
* Go to the root of the respective manual tree. |
Line 490 main(int argc, char *argv[]) |
|
Line 503 main(int argc, char *argv[]) |
|
* They are indexed relative to the root. |
* They are indexed relative to the root. |
*/ |
*/ |
|
|
if (-1 == chdir(dirs.paths[i])) { |
if (-1 == chdir(dirs.paths[ipath])) { |
perror(dirs.paths[i]); |
perror(dirs.paths[ipath]); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX); |
/* Create a new database in two temporary files. */ |
strlcpy(mdb.idxn, MANDOC_IDX, PATH_MAX); |
|
|
|
flags = O_CREAT | O_TRUNC | O_RDWR; |
flags = O_CREAT | O_EXCL | O_RDWR; |
mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info); |
while (NULL == mdb.db) { |
mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL); |
strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX); |
|
strlcat(mdb.dbn, ".XXXXXXXXXX", PATH_MAX); |
if (NULL == mdb.db) { |
if (NULL == mktemp(mdb.dbn)) { |
perror(mdb.dbn); |
perror(mdb.dbn); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} else if (NULL == mdb.idx) { |
} |
perror(mdb.idxn); |
mdb.db = dbopen(mdb.dbn, flags, 0644, |
exit((int)MANDOCLEVEL_SYSERR); |
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); |
|
} |
|
} |
|
|
/* |
/* |
* Search for manuals and fill the new database. |
* Search for manuals and fill the new database. |
Line 527 main(int argc, char *argv[]) |
|
Line 559 main(int argc, char *argv[]) |
|
(*mdb.idx->close)(mdb.idx); |
(*mdb.idx->close)(mdb.idx); |
mdb.db = NULL; |
mdb.db = NULL; |
mdb.idx = NULL; |
mdb.idx = NULL; |
|
|
|
/* |
|
* Replace the old database with the new one. |
|
* This is not perfectly atomic, |
|
* but i cannot think of a better way. |
|
*/ |
|
|
|
if (-1 == rename(mdb.dbn, MANDOC_DB)) { |
|
perror(MANDOC_DB); |
|
unlink(mdb.dbn); |
|
unlink(mdb.idxn); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
if (-1 == rename(mdb.idxn, MANDOC_IDX)) { |
|
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 576 index_merge(const struct of *of, struct mparse *mp, |
|
Line 628 index_merge(const struct of *of, struct mparse *mp, |
|
uint64_t vbuf[2]; |
uint64_t vbuf[2]; |
char type; |
char type; |
|
|
|
static char emptystring[] = ""; |
|
|
if (warnings) { |
if (warnings) { |
files = NULL; |
files = NULL; |
hash_reset(&files); |
hash_reset(&files); |
Line 598 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 688 index_merge(const struct of *of, struct mparse *mp, |
|
Line 742 index_merge(const struct of *of, struct mparse *mp, |
|
} |
} |
buf_appendb(buf, ")", 2); |
buf_appendb(buf, ")", 2); |
for (p = buf->cp; '\0' != *p; p++) |
for (p = buf->cp; '\0' != *p; p++) |
*p = tolower(*p); |
*p = tolower((unsigned char)*p); |
key.data = buf->cp; |
key.data = buf->cp; |
key.size = buf->len; |
key.size = buf->len; |
val.data = NULL; |
val.data = NULL; |
val.size = 0; |
val.size = 0; |
if (0 == skip) |
if (0 == skip) |
val.data = ""; |
val.data = emptystring; |
else { |
else { |
ch = (*files->get)(files, &key, &val, 0); |
ch = (*files->get)(files, &key, &val, 0); |
if (ch < 0) { |
if (ch < 0) { |
Line 930 index_prune(const struct of *ofile, struct mdb *mdb, s |
|
Line 984 index_prune(const struct of *ofile, struct mdb *mdb, s |
|
cont: |
cont: |
if (recs->cur >= recs->size) { |
if (recs->cur >= recs->size) { |
recs->size += MANDOC_SLOP; |
recs->size += MANDOC_SLOP; |
recs->stack = mandoc_realloc(recs->stack, |
recs->stack = mandoc_reallocarray(recs->stack, |
recs->size * sizeof(recno_t)); |
recs->size, sizeof(recno_t)); |
} |
} |
|
|
recs->stack[(int)recs->cur] = recs->last; |
recs->stack[(int)recs->cur] = recs->last; |
|
|
pman_node(MAN_ARGS) |
pman_node(MAN_ARGS) |
{ |
{ |
const struct man_node *head, *body; |
const struct man_node *head, *body; |
char *start, *sv, *title; |
char *start, *title; |
size_t sz, titlesz; |
size_t sz; |
|
|
if (NULL == n) |
if (NULL == n) |
return(0); |
return(0); |
Line 1350 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) { |
|
|
|
title = NULL; |
|
titlesz = 0; |
|
/* |
/* |
* Suck the entire NAME section into memory. |
* Suck the entire NAME section into memory. |
* Yes, we might run away. |
* Yes, we might run away. |
* But too many manuals have big, spread-out |
* But too many manuals have big, spread-out |
* NAME sections over many lines. |
* NAME sections over many lines. |
*/ |
*/ |
for ( ; NULL != body; body = body->next) { |
|
if (MAN_TEXT != body->type) |
title = NULL; |
break; |
man_deroff(&title, body); |
if (0 == (sz = strlen(body->string))) |
|
continue; |
|
title = mandoc_realloc |
|
(title, titlesz + sz + 1); |
|
memcpy(title + titlesz, body->string, sz); |
|
titlesz += sz + 1; |
|
title[(int)titlesz - 1] = ' '; |
|
} |
|
if (NULL == title) |
if (NULL == title) |
return(0); |
return(0); |
|
|
title = mandoc_realloc(title, titlesz + 1); |
|
title[(int)titlesz] = '\0'; |
|
|
|
/* Skip leading space. */ |
|
|
|
sv = title; |
|
while (isspace((unsigned char)*sv)) |
|
sv++; |
|
|
|
if (0 == (sz = strlen(sv))) { |
|
free(title); |
|
return(0); |
|
} |
|
|
|
/* Erase trailing space. */ |
|
|
|
start = &sv[sz - 1]; |
|
while (start > sv && isspace((unsigned char)*start)) |
|
*start-- = '\0'; |
|
|
|
if (start == sv) { |
|
free(title); |
|
return(0); |
|
} |
|
|
|
start = sv; |
|
|
|
/* |
/* |
* 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 1412 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 1436 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); |
free(title); |
return(1); |
return(1); |
Line 1708 ofile_argbuild(int argc, char *argv[], struct of **of, |
|
Line 1725 ofile_argbuild(int argc, char *argv[], struct of **of, |
|
* Recursively build up a list of files to parse. |
* Recursively build up a list of files to parse. |
* We use this instead of ftw() and so on because I don't want global |
* We use this instead of ftw() and so on because I don't want global |
* variables hanging around. |
* variables hanging around. |
* This ignores the mandocdb.db and mandocdb.index files, but assumes that |
* This ignores the mandoc.db and mandoc.index files, but assumes that |
* everything else is a manual. |
* everything else is a manual. |
* Pass in a pointer to a NULL structure for the first invocation. |
* Pass in a pointer to a NULL structure for the first invocation. |
*/ |
*/ |
Line 1717 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1734 ofile_dirbuild(const char *dir, const char* psec, cons |
|
int p_src_form, struct of **of) |
int p_src_form, struct of **of) |
{ |
{ |
char buf[PATH_MAX]; |
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 1739 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 1796 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1821 ofile_dirbuild(const char *dir, const char* psec, cons |
|
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", |