version 1.14, 2011/11/27 22:57:53 |
version 1.24, 2011/12/04 14:23:29 |
|
|
#include <stdint.h> |
#include <stdint.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
#include <unistd.h> |
|
|
#ifdef __linux__ |
#if defined(__linux__) |
|
# include <endian.h> |
# 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 |
Line 97 static void dbt_put(DB *, const char *, DBT *, DBT |
|
Line 102 static void dbt_put(DB *, const char *, DBT *, DBT |
|
static void hash_put(DB *, const struct buf *, uint64_t); |
static void hash_put(DB *, const struct buf *, uint64_t); |
static void hash_reset(DB **); |
static void hash_reset(DB **); |
static void index_merge(const struct of *, struct mparse *, |
static void index_merge(const struct of *, struct mparse *, |
struct buf *, struct buf *, |
struct buf *, struct buf *, DB *, |
DB *, DB *, const char *, |
DB *, const char *, DB *, const char *, |
DB *, const char *, int, int, |
|
recno_t, const recno_t *, size_t); |
recno_t, const recno_t *, size_t); |
static void index_prune(const struct of *, DB *, |
static void index_prune(const struct of *, DB *, |
const char *, DB *, const char *, |
const char *, DB *, const char *, |
int, recno_t *, recno_t **, size_t *); |
recno_t *, recno_t **, size_t *); |
static void ofile_argbuild(char *[], int, int, int, |
static void ofile_argbuild(int, char *[], struct of **); |
struct of **); |
|
static int ofile_dirbuild(const char *, const char *, |
static int ofile_dirbuild(const char *, const char *, |
const char *, int, int, int, |
const char *, int, struct of **); |
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 *, struct buf *, |
const struct of *); |
const struct of *); |
Line 258 static const pmdoc_nf mdocs[MDOC_MAX] = { |
|
Line 260 static const pmdoc_nf mdocs[MDOC_MAX] = { |
|
}; |
}; |
|
|
static const char *progname; |
static const char *progname; |
|
static int use_all; /* Use all directories and files. */ |
|
static int verb; /* Output verbosity level. */ |
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
Line 268 main(int argc, char *argv[]) |
|
Line 272 main(int argc, char *argv[]) |
|
const char *dir; |
const char *dir; |
char ibuf[MAXPATHLEN], /* index fname */ |
char ibuf[MAXPATHLEN], /* index fname */ |
fbuf[MAXPATHLEN]; /* btree fname */ |
fbuf[MAXPATHLEN]; /* btree fname */ |
int verb, /* output verbosity */ |
int ch, i, flags; |
use_all, /* use all directories and files */ |
|
ch, i, flags; |
|
DB *idx, /* index database */ |
DB *idx, /* index database */ |
*db, /* keyword database */ |
*db, /* keyword database */ |
*hash; /* temporary keyword hashtable */ |
*hash; /* temporary keyword hashtable */ |
Line 377 main(int argc, char *argv[]) |
|
Line 379 main(int argc, char *argv[]) |
|
printf("%s: Opened\n", ibuf); |
printf("%s: Opened\n", ibuf); |
} |
} |
|
|
ofile_argbuild(argv, argc, use_all, verb, &of); |
ofile_argbuild(argc, argv, &of); |
if (NULL == of) |
if (NULL == of) |
goto out; |
goto out; |
|
|
of = of->first; |
of = of->first; |
|
|
index_prune(of, db, fbuf, idx, ibuf, verb, |
index_prune(of, db, fbuf, idx, ibuf, |
&maxrec, &recs, &recsz); |
&maxrec, &recs, &recsz); |
|
|
if (OP_UPDATE == op) |
/* |
|
* Go to the root of the respective manual tree |
|
* such that .so links work. In case of failure, |
|
* just prod on, even though .so links won't work. |
|
*/ |
|
|
|
if (OP_UPDATE == op) { |
|
chdir(dir); |
index_merge(of, mp, &dbuf, &buf, hash, |
index_merge(of, mp, &dbuf, &buf, hash, |
db, fbuf, idx, ibuf, use_all, |
db, fbuf, idx, ibuf, |
verb, maxrec, recs, reccur); |
maxrec, recs, reccur); |
|
} |
|
|
goto out; |
goto out; |
} |
} |
Line 450 main(int argc, char *argv[]) |
|
Line 460 main(int argc, char *argv[]) |
|
of = NULL; |
of = NULL; |
|
|
if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL, |
if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL, |
0, use_all, verb, &of)) |
0, &of)) |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
|
|
if (NULL == of) |
if (NULL == of) |
Line 458 main(int argc, char *argv[]) |
|
Line 468 main(int argc, char *argv[]) |
|
|
|
of = of->first; |
of = of->first; |
|
|
|
/* |
|
* Go to the root of the respective manual tree |
|
* such that .so links work. In case of failure, |
|
* just prod on, even though .so links won't work. |
|
*/ |
|
|
|
chdir(dirs.paths[i]); |
index_merge(of, mp, &dbuf, &buf, hash, db, fbuf, |
index_merge(of, mp, &dbuf, &buf, hash, db, fbuf, |
idx, ibuf, use_all, verb, |
idx, ibuf, maxrec, recs, reccur); |
maxrec, recs, reccur); |
|
} |
} |
|
|
out: |
out: |
|
|
|
|
void |
void |
index_merge(const struct of *of, struct mparse *mp, |
index_merge(const struct of *of, struct mparse *mp, |
struct buf *dbuf, struct buf *buf, |
struct buf *dbuf, struct buf *buf, DB *hash, |
DB *hash, DB *db, const char *dbf, |
DB *db, const char *dbf, DB *idx, const char *idxf, |
DB *idx, const char *idxf, int use_all, int verb, |
|
recno_t maxrec, const recno_t *recs, size_t reccur) |
recno_t maxrec, const recno_t *recs, size_t reccur) |
{ |
{ |
recno_t rec; |
recno_t rec; |
Line 592 index_merge(const struct of *of, struct mparse *mp, |
|
Line 607 index_merge(const struct of *of, struct mparse *mp, |
|
*/ |
*/ |
|
|
dbuf->len = 0; |
dbuf->len = 0; |
|
buf_append(dbuf, mdoc ? "mdoc" : (man ? "man" : "cat")); |
buf_appendb(dbuf, fn, strlen(fn) + 1); |
buf_appendb(dbuf, fn, strlen(fn) + 1); |
buf_appendb(dbuf, msec, strlen(msec) + 1); |
buf_appendb(dbuf, msec, strlen(msec) + 1); |
buf_appendb(dbuf, mtitle, strlen(mtitle) + 1); |
buf_appendb(dbuf, mtitle, strlen(mtitle) + 1); |
Line 614 index_merge(const struct of *of, struct mparse *mp, |
|
Line 630 index_merge(const struct of *of, struct mparse *mp, |
|
* into the database. |
* into the database. |
*/ |
*/ |
|
|
vbuf.rec = rec; |
vbuf.rec = htobe32(rec); |
seq = R_FIRST; |
seq = R_FIRST; |
while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) { |
while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) { |
seq = R_NEXT; |
seq = R_NEXT; |
|
vbuf.mask = htobe64(*(uint64_t *)val.data); |
vbuf.mask = *(uint64_t *)val.data; |
|
val.size = sizeof(struct db_val); |
val.size = sizeof(struct db_val); |
val.data = &vbuf; |
val.data = &vbuf; |
|
|
if (verb > 1) |
|
printf("%s: Added keyword: %s\n", |
|
fn, (char *)key.data); |
|
dbt_put(db, dbf, &key, &val); |
dbt_put(db, dbf, &key, &val); |
} |
} |
if (ch < 0) { |
if (ch < 0) { |
Line 649 index_merge(const struct of *of, struct mparse *mp, |
|
Line 660 index_merge(const struct of *of, struct mparse *mp, |
|
|
|
if (verb) |
if (verb) |
printf("%s: Added index\n", fn); |
printf("%s: Added index\n", fn); |
|
|
dbt_put(idx, idxf, &key, &val); |
dbt_put(idx, idxf, &key, &val); |
} |
} |
} |
} |
Line 661 index_merge(const struct of *of, struct mparse *mp, |
|
Line 673 index_merge(const struct of *of, struct mparse *mp, |
|
*/ |
*/ |
static void |
static void |
index_prune(const struct of *ofile, DB *db, const char *dbf, |
index_prune(const struct of *ofile, DB *db, const char *dbf, |
DB *idx, const char *idxf, int verb, |
DB *idx, const char *idxf, |
recno_t *maxrec, recno_t **recs, size_t *recsz) |
recno_t *maxrec, recno_t **recs, size_t *recsz) |
{ |
{ |
const struct of *of; |
const struct of *of; |
const char *fn; |
const char *fn, *cp; |
struct db_val *vbuf; |
struct db_val *vbuf; |
unsigned seq, sseq; |
unsigned seq, sseq; |
DBT key, val; |
DBT key, val; |
Line 677 index_prune(const struct of *ofile, DB *db, const char |
|
Line 689 index_prune(const struct of *ofile, DB *db, const char |
|
while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) { |
while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) { |
seq = R_NEXT; |
seq = R_NEXT; |
*maxrec = *(recno_t *)key.data; |
*maxrec = *(recno_t *)key.data; |
if (0 == val.size) { |
cp = val.data; |
if (reccur >= *recsz) { |
|
*recsz += MANDOC_SLOP; |
|
*recs = mandoc_realloc(*recs, |
|
*recsz * sizeof(recno_t)); |
|
} |
|
(*recs)[(int)reccur] = *maxrec; |
|
reccur++; |
|
continue; |
|
} |
|
|
|
fn = (char *)val.data; |
/* Deleted records are zero-sized. Skip them. */ |
|
|
|
if (0 == val.size) |
|
goto cont; |
|
|
|
/* |
|
* Make sure we're sane. |
|
* Read past our mdoc/man/cat type to the next string, |
|
* then make sure it's bounded by a NUL. |
|
* Failing any of these, we go into our error handler. |
|
*/ |
|
|
|
if (NULL == (fn = memchr(cp, '\0', val.size))) |
|
break; |
|
if (++fn - cp >= (int)val.size) |
|
break; |
|
if (NULL == memchr(fn, '\0', val.size - (fn - cp))) |
|
break; |
|
|
|
/* |
|
* Search for the file in those we care about. |
|
* XXX: build this into a tree. Too slow. |
|
*/ |
|
|
for (of = ofile; of; of = of->next) |
for (of = ofile; of; of = of->next) |
if (0 == strcmp(fn, of->fname)) |
if (0 == strcmp(fn, of->fname)) |
break; |
break; |
Line 696 index_prune(const struct of *ofile, DB *db, const char |
|
Line 722 index_prune(const struct of *ofile, DB *db, const char |
|
if (NULL == of) |
if (NULL == of) |
continue; |
continue; |
|
|
|
/* |
|
* Search through the keyword database, throwing out all |
|
* references to our file. |
|
*/ |
|
|
sseq = R_FIRST; |
sseq = R_FIRST; |
while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) { |
while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) { |
sseq = R_NEXT; |
sseq = R_NEXT; |
assert(sizeof(struct db_val) == val.size); |
if (sizeof(struct db_val) != val.size) |
|
break; |
|
|
vbuf = val.data; |
vbuf = val.data; |
if (*maxrec != vbuf->rec) |
if (*maxrec != betoh32(vbuf->rec)) |
continue; |
continue; |
if (verb) |
|
printf("%s: Deleted keyword: %s\n", |
if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0) |
fn, (char *)key.data); |
|
ch = (*db->del)(db, &key, R_CURSOR); |
|
if (ch < 0) |
|
break; |
break; |
} |
} |
|
|
if (ch < 0) { |
if (ch < 0) { |
perror(dbf); |
perror(dbf); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
|
} else if (1 != ch) { |
|
fprintf(stderr, "%s: Corrupt database\n", dbf); |
|
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
if (verb) |
if (verb) |
Line 720 index_prune(const struct of *ofile, DB *db, const char |
|
Line 754 index_prune(const struct of *ofile, DB *db, const char |
|
|
|
val.size = 0; |
val.size = 0; |
ch = (*idx->put)(idx, &key, &val, R_CURSOR); |
ch = (*idx->put)(idx, &key, &val, R_CURSOR); |
if (ch < 0) { |
|
perror(idxf); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
|
|
|
if (ch < 0) |
|
break; |
|
cont: |
if (reccur >= *recsz) { |
if (reccur >= *recsz) { |
*recsz += MANDOC_SLOP; |
*recsz += MANDOC_SLOP; |
*recs = mandoc_realloc |
*recs = mandoc_realloc |
Line 734 index_prune(const struct of *ofile, DB *db, const char |
|
Line 767 index_prune(const struct of *ofile, DB *db, const char |
|
(*recs)[(int)reccur] = *maxrec; |
(*recs)[(int)reccur] = *maxrec; |
reccur++; |
reccur++; |
} |
} |
|
|
|
if (ch < 0) { |
|
perror(idxf); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} else if (1 != ch) { |
|
fprintf(stderr, "%s: Corrupt index\n", idxf); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
|
(*maxrec)++; |
(*maxrec)++; |
} |
} |
|
|
Line 1335 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
Line 1377 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
} |
} |
|
|
static void |
static void |
ofile_argbuild(char *argv[], int argc, int use_all, int verb, |
ofile_argbuild(int argc, char *argv[], struct of **of) |
struct of **of) |
|
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
char *sec, *arch, *title, *p; |
char *sec, *arch, *title, *p; |
Line 1376 ofile_argbuild(char *argv[], int argc, int use_all, in |
|
Line 1417 ofile_argbuild(char *argv[], int argc, int use_all, in |
|
*p = '\0'; |
*p = '\0'; |
continue; |
continue; |
} |
} |
if (strncmp("man", p + 1, 3)) { |
if (0 == strncmp("man", p + 1, 3)) |
src_form |= MANDOC_SRC; |
src_form |= MANDOC_SRC; |
arch = p + 1; |
else if (0 == strncmp("cat", p + 1, 3)) |
} else if (strncmp("cat", p + 1, 3)) { |
|
src_form |= MANDOC_FORM; |
src_form |= MANDOC_FORM; |
|
else |
arch = p + 1; |
arch = p + 1; |
} |
|
break; |
break; |
} |
} |
if (NULL == title) |
if (NULL == title) |
Line 1428 ofile_argbuild(char *argv[], int argc, int use_all, in |
|
Line 1468 ofile_argbuild(char *argv[], int argc, int use_all, in |
|
*/ |
*/ |
static int |
static int |
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, int use_all, int verb, struct of **of) |
int p_src_form, struct of **of) |
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
struct stat sb; |
struct stat sb; |
Line 1494 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1534 ofile_dirbuild(const char *dir, const char* psec, cons |
|
printf("%s: Scanning\n", buf); |
printf("%s: Scanning\n", buf); |
|
|
if ( ! ofile_dirbuild(buf, sec, arch, |
if ( ! ofile_dirbuild(buf, sec, arch, |
src_form, use_all, verb, of)) |
src_form, of)) |
return(0); |
return(0); |
} |
} |
if (DT_REG != dp->d_type || |
if (DT_REG != dp->d_type || |