version 1.25, 2011/12/07 01:57:20 |
version 1.35, 2011/12/16 08:04:34 |
|
|
|
|
#include <sys/param.h> |
#include <sys/param.h> |
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/stat.h> |
|
|
|
#include <assert.h> |
#include <assert.h> |
#include <dirent.h> |
#include <dirent.h> |
Line 107 static void index_merge(const struct of *, struct m |
|
Line 106 static void index_merge(const struct of *, struct m |
|
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 *, |
recno_t *, recno_t **, size_t *); |
recno_t *, recno_t **, size_t *, |
|
size_t *); |
static void ofile_argbuild(int, char *[], struct of **); |
static void ofile_argbuild(int, char *[], struct of **); |
static int 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 *, struct buf *, |
Line 272 main(int argc, char *argv[]) |
|
Line 272 main(int argc, char *argv[]) |
|
struct manpaths dirs; |
struct manpaths dirs; |
enum op op; /* current operation */ |
enum op op; /* current operation */ |
const char *dir; |
const char *dir; |
char ibuf[MAXPATHLEN], /* index fname */ |
char *conf_file; |
|
char *cp; |
|
char pbuf[PATH_MAX], |
|
ibuf[MAXPATHLEN], /* index fname */ |
fbuf[MAXPATHLEN]; /* btree fname */ |
fbuf[MAXPATHLEN]; /* btree fname */ |
int ch, i, flags; |
int ch, i, flags; |
DB *idx, /* index database */ |
DB *idx, /* index database */ |
Line 309 main(int argc, char *argv[]) |
|
Line 312 main(int argc, char *argv[]) |
|
maxrec = 0; |
maxrec = 0; |
op = OP_NEW; |
op = OP_NEW; |
dir = NULL; |
dir = NULL; |
|
conf_file = NULL; |
|
|
while (-1 != (ch = getopt(argc, argv, "ad:u:v"))) |
while (-1 != (ch = getopt(argc, argv, "aC:d:u:v"))) |
switch (ch) { |
switch (ch) { |
case ('a'): |
case ('a'): |
use_all = 1; |
use_all = 1; |
break; |
break; |
|
case ('C'): |
|
conf_file = optarg; |
|
break; |
case ('d'): |
case ('d'): |
dir = optarg; |
dir = optarg; |
op = OP_UPDATE; |
op = OP_UPDATE; |
Line 376 main(int argc, char *argv[]) |
|
Line 383 main(int argc, char *argv[]) |
|
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
if (verb > 2) { |
|
printf("%s: Opened\n", fbuf); |
|
printf("%s: Opened\n", ibuf); |
|
} |
|
|
|
ofile_argbuild(argc, argv, &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, |
index_prune(of, db, fbuf, idx, ibuf, |
&maxrec, &recs, &recsz); |
&maxrec, &recs, &recsz, &reccur); |
|
|
/* |
/* |
* Go to the root of the respective manual tree |
* Go to the root of the respective manual tree. |
* such that .so links work. In case of failure, |
* This must work or no manuals may be found (they're |
* just prod on, even though .so links won't work. |
* indexed relative to the root). |
*/ |
*/ |
|
|
if (OP_UPDATE == op) { |
if (OP_UPDATE == op) { |
chdir(dir); |
if (-1 == chdir(dir)) { |
|
perror(dir); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
index_merge(of, mp, &dbuf, &buf, hash, |
index_merge(of, mp, &dbuf, &buf, hash, |
db, fbuf, idx, ibuf, |
db, fbuf, idx, ibuf, |
maxrec, recs, reccur); |
maxrec, recs, reccur); |
Line 413 main(int argc, char *argv[]) |
|
Line 419 main(int argc, char *argv[]) |
|
*/ |
*/ |
|
|
if (argc > 0) { |
if (argc > 0) { |
dirs.paths = mandoc_malloc(argc * sizeof(char *)); |
dirs.paths = mandoc_calloc(argc, sizeof(char *)); |
dirs.sz = argc; |
dirs.sz = argc; |
for (i = 0; i < argc; i++) |
for (i = 0; i < argc; i++) { |
dirs.paths[i] = mandoc_strdup(argv[i]); |
if (NULL == (cp = realpath(argv[i], pbuf))) { |
|
perror(argv[i]); |
|
goto out; |
|
} |
|
dirs.paths[i] = mandoc_strdup(cp); |
|
} |
} else |
} else |
manpath_parse(&dirs, NULL, NULL); |
manpath_parse(&dirs, conf_file, NULL, NULL); |
|
|
for (i = 0; i < dirs.sz; i++) { |
for (i = 0; i < dirs.sz; i++) { |
ibuf[0] = fbuf[0] = '\0'; |
ibuf[0] = fbuf[0] = '\0'; |
Line 453 main(int argc, char *argv[]) |
|
Line 464 main(int argc, char *argv[]) |
|
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
if (verb > 2) { |
|
printf("%s: Truncated\n", fbuf); |
|
printf("%s: Truncated\n", ibuf); |
|
} |
|
|
|
ofile_free(of); |
ofile_free(of); |
of = NULL; |
of = NULL; |
|
|
if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL, |
if (-1 == chdir(dirs.paths[i])) { |
0, &of)) |
perror(dirs.paths[i]); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
|
|
ofile_dirbuild(".", NULL, NULL, 0, &of); |
|
|
if (NULL == of) |
if (NULL == of) |
continue; |
continue; |
|
|
of = of->first; |
of = of->first; |
|
|
/* |
/* |
* Go to the root of the respective manual tree |
* Go to the root of the respective manual tree. |
* such that .so links work. In case of failure, |
* This must work or no manuals may be found (they're |
* just prod on, even though .so links won't work. |
* indexed relative to the root). |
*/ |
*/ |
|
|
chdir(dirs.paths[i]); |
if (-1 == chdir(dirs.paths[i])) { |
|
perror(dirs.paths[i]); |
|
exit((int)MANDOCLEVEL_SYSERR); |
|
} |
|
|
index_merge(of, mp, &dbuf, &buf, hash, db, fbuf, |
index_merge(of, mp, &dbuf, &buf, hash, db, fbuf, |
idx, ibuf, maxrec, recs, reccur); |
idx, ibuf, maxrec, recs, reccur); |
} |
} |
Line 520 index_merge(const struct of *of, struct mparse *mp, |
|
Line 533 index_merge(const struct of *of, struct mparse *mp, |
|
fn = of->fname; |
fn = of->fname; |
|
|
/* |
/* |
* Reclaim an empty index record, if available. |
* Try interpreting the file as mdoc(7) or man(7) |
|
* source code, unless it is already known to be |
|
* formatted. Fall back to formatted mode. |
*/ |
*/ |
|
|
if (reccur > 0) { |
|
--reccur; |
|
rec = recs[(int)reccur]; |
|
} else if (maxrec > 0) { |
|
rec = maxrec; |
|
maxrec = 0; |
|
} else |
|
rec++; |
|
|
|
mparse_reset(mp); |
mparse_reset(mp); |
hash_reset(&hash); |
|
mdoc = NULL; |
mdoc = NULL; |
man = NULL; |
man = NULL; |
|
|
/* |
|
* Try interpreting the file as mdoc(7) or man(7) |
|
* source code, unless it is already known to be |
|
* formatted. Fall back to formatted mode. |
|
*/ |
|
|
|
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)) |
Line 571 index_merge(const struct of *of, struct mparse *mp, |
|
Line 570 index_merge(const struct of *of, struct mparse *mp, |
|
if (0 == use_all) { |
if (0 == use_all) { |
assert(of->sec); |
assert(of->sec); |
assert(msec); |
assert(msec); |
if (strcmp(msec, of->sec)) |
if (strcasecmp(msec, of->sec)) |
continue; |
continue; |
|
|
if (NULL == arch) { |
if (NULL == arch) { |
if (NULL != of->arch) |
if (NULL != of->arch) |
continue; |
continue; |
} else if (NULL == of->arch || |
} else if (NULL == of->arch || |
strcmp(arch, of->arch)) |
strcasecmp(arch, of->arch)) |
continue; |
continue; |
} |
} |
|
|
Line 617 index_merge(const struct of *of, struct mparse *mp, |
|
Line 616 index_merge(const struct of *of, struct mparse *mp, |
|
|
|
sv = dbuf->len; |
sv = dbuf->len; |
|
|
/* Fix the record number in the btree value. */ |
/* |
|
* Collect keyword/mask pairs. |
|
* Each pair will become a new btree node. |
|
*/ |
|
|
|
hash_reset(&hash); |
if (mdoc) |
if (mdoc) |
pmdoc_node(hash, buf, dbuf, |
pmdoc_node(hash, buf, dbuf, |
mdoc_node(mdoc), mdoc_meta(mdoc)); |
mdoc_node(mdoc), mdoc_meta(mdoc)); |
Line 628 index_merge(const struct of *of, struct mparse *mp, |
|
Line 631 index_merge(const struct of *of, struct mparse *mp, |
|
pformatted(hash, buf, dbuf, of); |
pformatted(hash, buf, dbuf, of); |
|
|
/* |
/* |
* Copy from the in-memory hashtable of pending keywords |
* Reclaim an empty index record, if available. |
* into the database. |
* Use its record number for all new btree nodes. |
*/ |
*/ |
|
|
|
if (reccur > 0) { |
|
--reccur; |
|
rec = recs[(int)reccur]; |
|
} else if (maxrec > 0) { |
|
rec = maxrec; |
|
maxrec = 0; |
|
} else |
|
rec++; |
vbuf.rec = htobe32(rec); |
vbuf.rec = htobe32(rec); |
|
|
|
/* |
|
* Copy from the in-memory hashtable of pending |
|
* keyword/mask pairs into the database. |
|
*/ |
|
|
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; |
Line 675 index_merge(const struct of *of, struct mparse *mp, |
|
Line 692 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, |
DB *idx, const char *idxf, recno_t *maxrec, |
recno_t *maxrec, recno_t **recs, size_t *recsz) |
recno_t **recs, size_t *recsz, size_t *reccur) |
{ |
{ |
const struct of *of; |
const struct of *of; |
const char *fn, *cp; |
const char *fn, *cp; |
struct db_val *vbuf; |
struct db_val *vbuf; |
unsigned seq, sseq; |
unsigned seq, sseq; |
DBT key, val; |
DBT key, val; |
size_t reccur; |
|
int ch; |
int ch; |
|
|
reccur = 0; |
*reccur = 0; |
seq = R_FIRST; |
seq = R_FIRST; |
while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) { |
while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) { |
seq = R_NEXT; |
seq = R_NEXT; |
Line 760 index_prune(const struct of *ofile, DB *db, const char |
|
Line 776 index_prune(const struct of *ofile, DB *db, const char |
|
if (ch < 0) |
if (ch < 0) |
break; |
break; |
cont: |
cont: |
if (reccur >= *recsz) { |
if (*reccur >= *recsz) { |
*recsz += MANDOC_SLOP; |
*recsz += MANDOC_SLOP; |
*recs = mandoc_realloc |
*recs = mandoc_realloc |
(*recs, *recsz * sizeof(recno_t)); |
(*recs, *recsz * sizeof(recno_t)); |
} |
} |
|
|
(*recs)[(int)reccur] = *maxrec; |
(*recs)[(int)*reccur] = *maxrec; |
reccur++; |
(*reccur)++; |
} |
} |
|
|
if (ch < 0) { |
if (ch < 0) { |
Line 1281 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
Line 1297 pformatted(DB *hash, struct buf *buf, struct buf *dbuf |
|
buf_append(buf, of->title); |
buf_append(buf, of->title); |
hash_put(hash, buf, TYPE_Nm); |
hash_put(hash, buf, TYPE_Nm); |
|
|
while (NULL != (line = fgetln(stream, &len)) && '\n' != *line) |
/* Skip to first blank line. */ |
/* Skip to first blank line. */ ; |
|
|
|
while (NULL != (line = fgetln(stream, &len)) && |
while (NULL != (line = fgetln(stream, &len))) |
('\n' == *line || ' ' == *line)) |
if ('\n' == *line) |
/* Skip to first section header. */ ; |
break; |
|
|
/* |
/* |
* If no page content can be found, |
* Assume the first line that is not indented |
* reuse the page title as the page description. |
* is the first section header. Skip to it. |
*/ |
*/ |
|
|
if (NULL == (line = fgetln(stream, &len))) { |
while (NULL != (line = fgetln(stream, &len))) |
|
if ('\n' != *line && ' ' != *line) |
|
break; |
|
|
|
/* |
|
* If no page content can be found, or the input line |
|
* is already the next section header, or there is no |
|
* trailing newline, reuse the page title as the page |
|
* description. |
|
*/ |
|
|
|
line = fgetln(stream, &len); |
|
if (NULL == line || ' ' != *line || '\n' != line[(int)len - 1]) { |
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); |
return; |
return; |
} |
} |
fclose(stream); |
|
|
|
|
line[(int)--len] = '\0'; |
|
|
/* |
/* |
* If there is a dash, skip to the text following it. |
* Skip to the first dash. |
|
* Use the remaining line as the description (no more than 70 |
|
* bytes). |
*/ |
*/ |
|
|
for (p = line, plen = len; plen; p++, plen--) |
if (NULL != (p = strstr(line, "- "))) { |
if ('-' == *p) |
for (p += 2; ' ' == *p || '\b' == *p; p++) |
break; |
/* Skip to next word. */ ; |
for ( ; plen; p++, plen--) |
} else |
if ('-' != *p && ' ' != *p && 8 != *p) |
|
break; |
|
if (0 == plen) { |
|
p = line; |
p = line; |
plen = len; |
|
|
if ((plen = strlen(p)) > 70) { |
|
plen = 70; |
|
p[plen] = '\0'; |
} |
} |
|
|
/* |
/* Strip backspace-encoding from line. */ |
* Copy the rest of the line, but no more than 70 bytes. |
|
*/ |
|
|
|
if (70 < plen) |
while (NULL != (line = memchr(p, '\b', plen))) { |
plen = 70; |
len = line - p; |
p[plen-1] = '\0'; |
if (0 == len) { |
buf_appendb(dbuf, p, plen); |
memmove(line, line + 1, plen--); |
|
continue; |
|
} |
|
memmove(line - 1, line + 1, plen - len); |
|
plen -= 2; |
|
} |
|
|
|
buf_appendb(dbuf, p, plen + 1); |
buf->len = 0; |
buf->len = 0; |
buf_appendb(buf, p, plen); |
buf_appendb(buf, p, plen + 1); |
hash_put(hash, buf, TYPE_Nd); |
hash_put(hash, buf, TYPE_Nd); |
|
fclose(stream); |
} |
} |
|
|
static void |
static void |
Line 1398 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
Line 1434 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
* Add the structure to the list. |
* Add the structure to the list. |
*/ |
*/ |
|
|
if (verb > 2) |
|
printf("%s: Scheduling\n", argv[i]); |
|
if (NULL == *of) { |
if (NULL == *of) { |
*of = nof; |
*of = nof; |
(*of)->first = nof; |
(*of)->first = nof; |
Line 1419 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
Line 1453 ofile_argbuild(int argc, char *argv[], struct of **of) |
|
* 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. |
*/ |
*/ |
static int |
static void |
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[MAXPATHLEN]; |
struct stat sb; |
|
size_t sz; |
size_t sz; |
DIR *d; |
DIR *d; |
const char *fn, *sec, *arch; |
const char *fn, *sec, *arch; |
Line 1435 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1468 ofile_dirbuild(const char *dir, const char* psec, cons |
|
|
|
if (NULL == (d = opendir(dir))) { |
if (NULL == (d = opendir(dir))) { |
perror(dir); |
perror(dir); |
return(0); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
while (NULL != (dp = readdir(d))) { |
while (NULL != (dp = readdir(d))) { |
Line 1480 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1513 ofile_dirbuild(const char *dir, const char* psec, cons |
|
|
|
if (MAXPATHLEN <= sz) { |
if (MAXPATHLEN <= sz) { |
fprintf(stderr, "%s: Path too long\n", dir); |
fprintf(stderr, "%s: Path too long\n", dir); |
return(0); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
if (verb > 2) |
ofile_dirbuild(buf, sec, arch, src_form, of); |
printf("%s: Scanning\n", buf); |
|
|
|
if ( ! ofile_dirbuild(buf, sec, arch, |
|
src_form, of)) |
|
return(0); |
|
} |
} |
|
|
if (DT_REG != dp->d_type || |
if (DT_REG != dp->d_type || |
(NULL == psec && !use_all) || |
(NULL == psec && !use_all) || |
!strcmp(MANDOC_DB, fn) || |
! strcmp(MANDOC_DB, fn) || |
!strcmp(MANDOC_IDX, fn)) |
! strcmp(MANDOC_IDX, fn)) |
continue; |
continue; |
|
|
/* |
/* |
Line 1532 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1561 ofile_dirbuild(const char *dir, const char* psec, cons |
|
buf[0] = '\0'; |
buf[0] = '\0'; |
strlcat(buf, dir, MAXPATHLEN); |
strlcat(buf, dir, MAXPATHLEN); |
p = strrchr(buf, '/'); |
p = strrchr(buf, '/'); |
|
if (NULL != parch && NULL != p) |
|
for (p--; p > buf; p--) |
|
if ('/' == *p) |
|
break; |
if (NULL == p) |
if (NULL == p) |
p = buf; |
p = buf; |
else |
else |
Line 1553 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1586 ofile_dirbuild(const char *dir, const char* psec, cons |
|
"%s: Path too long\n", buf); |
"%s: Path too long\n", buf); |
continue; |
continue; |
} |
} |
if (0 == stat(buf, &sb)) |
if (0 == access(buf, R_OK)) |
continue; |
continue; |
} |
} |
} |
} |
|
|
|
assert('.' == dir[0]); |
|
assert('/' == dir[1]); |
buf[0] = '\0'; |
buf[0] = '\0'; |
strlcat(buf, dir, MAXPATHLEN); |
strlcat(buf, dir + 2, MAXPATHLEN); |
strlcat(buf, "/", MAXPATHLEN); |
strlcat(buf, "/", MAXPATHLEN); |
sz = strlcat(buf, fn, MAXPATHLEN); |
sz = strlcat(buf, fn, MAXPATHLEN); |
if (sz >= MAXPATHLEN) { |
if (sz >= MAXPATHLEN) { |
Line 1588 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1623 ofile_dirbuild(const char *dir, const char* psec, cons |
|
* Add the structure to the list. |
* Add the structure to the list. |
*/ |
*/ |
|
|
if (verb > 2) |
|
printf("%s: Scheduling\n", buf); |
|
if (NULL == *of) { |
if (NULL == *of) { |
*of = nof; |
*of = nof; |
(*of)->first = nof; |
(*of)->first = nof; |
Line 1601 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1634 ofile_dirbuild(const char *dir, const char* psec, cons |
|
} |
} |
|
|
closedir(d); |
closedir(d); |
return(1); |
|
} |
} |
|
|
static void |
static void |
|
|
{ |
{ |
|
|
fprintf(stderr, "usage: %s [-v] " |
fprintf(stderr, "usage: %s [-v] " |
"[-d dir [files...] |" |
"[-C file] |" |
" -u dir [files...] |" |
" dir ... |" |
" dir...]\n", progname); |
" -d dir [file ...] |" |
|
" -u dir [file ...]\n", progname); |
} |
} |