version 1.45, 2012/03/23 05:45:45 |
version 1.49.2.1, 2013/09/17 21:32:07 |
|
|
/* $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 <errno.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> |
|
|
/* 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 137 static void index_merge(const struct of *, struct m |
|
Line 137 static void index_merge(const struct of *, struct m |
|
const char *); |
const char *); |
static void index_prune(const struct of *, struct mdb *, |
static void index_prune(const struct of *, struct mdb *, |
struct recs *, const char *); |
struct recs *, const char *); |
static void ofile_argbuild(int, char *[], |
static void ofile_argbuild(int, char *[], |
struct of **, const 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 **, char *); |
const char *, int, struct of **, char *); |
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 char *); |
const struct of *, const char *); |
static int pman_node(MAN_ARGS); |
static int pman_node(MAN_ARGS); |
static void pmdoc_node(MDOC_ARGS); |
static void pmdoc_node(MDOC_ARGS); |
Line 305 main(int argc, char *argv[]) |
|
Line 305 main(int argc, char *argv[]) |
|
enum op op; /* current operation */ |
enum op op; /* current operation */ |
const char *dir; |
const char *dir; |
int ch, i, flags; |
int ch, i, flags; |
char dirbuf[MAXPATHLEN]; |
char dirbuf[PATH_MAX]; |
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; |
Line 396 main(int argc, char *argv[]) |
|
Line 396 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); |
mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, 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 410 main(int argc, char *argv[]) |
|
Line 410 main(int argc, char *argv[]) |
|
ofile_argbuild(argc, argv, &of, "."); |
ofile_argbuild(argc, argv, &of, "."); |
if (NULL == of) |
if (NULL == of) |
goto out; |
goto out; |
index_merge(of, mp, &dbuf, &buf, |
index_merge(of, mp, &dbuf, &buf, |
hash, &mdb, &recs, "."); |
hash, &mdb, &recs, "."); |
goto out; |
goto out; |
} |
} |
|
|
if (OP_UPDATE == op || OP_DELETE == op) { |
if (OP_UPDATE == op || OP_DELETE == op) { |
strlcat(mdb.dbn, dir, MAXPATHLEN); |
strlcat(mdb.dbn, dir, PATH_MAX); |
strlcat(mdb.dbn, "/", MAXPATHLEN); |
strlcat(mdb.dbn, "/", PATH_MAX); |
sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN); |
sz1 = strlcat(mdb.dbn, MANDOC_DB, PATH_MAX); |
|
|
strlcat(mdb.idxn, dir, MAXPATHLEN); |
strlcat(mdb.idxn, dir, PATH_MAX); |
strlcat(mdb.idxn, "/", MAXPATHLEN); |
strlcat(mdb.idxn, "/", PATH_MAX); |
sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN); |
sz2 = strlcat(mdb.idxn, MANDOC_IDX, PATH_MAX); |
|
|
if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) { |
if (sz1 >= PATH_MAX || sz2 >= PATH_MAX) { |
fprintf(stderr, "%s: path too long\n", dir); |
fprintf(stderr, "%s: path too long\n", dir); |
exit((int)MANDOCLEVEL_BADARG); |
exit((int)MANDOCLEVEL_BADARG); |
} |
} |
Line 492 main(int argc, char *argv[]) |
|
Line 492 main(int argc, char *argv[]) |
|
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
strlcpy(mdb.dbn, MANDOC_DB, MAXPATHLEN); |
strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX); |
strlcpy(mdb.idxn, MANDOC_IDX, MAXPATHLEN); |
strlcpy(mdb.idxn, MANDOC_IDX, PATH_MAX); |
|
|
flags = O_CREAT | O_TRUNC | O_RDWR; |
flags = O_CREAT | O_TRUNC | O_RDWR; |
mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info); |
mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info); |
Line 511 main(int argc, char *argv[]) |
|
Line 511 main(int argc, char *argv[]) |
|
* Search for manuals and fill the new database. |
* Search for manuals and fill the new database. |
*/ |
*/ |
|
|
strlcpy(dirbuf, dirs.paths[i], MAXPATHLEN); |
strlcpy(dirbuf, dirs.paths[i], PATH_MAX); |
ofile_dirbuild(".", "", "", 0, &of, dirbuf); |
ofile_dirbuild(".", "", "", 0, &of, dirbuf); |
|
|
if (NULL != of) { |
if (NULL != of) { |
|
|
|
|
usage: |
usage: |
fprintf(stderr, |
fprintf(stderr, |
"usage: %s [-avvv] [-C file] | dir ... | -t file ...\n" |
"usage: %s [-av] [-C file] | dir ... | -t file ...\n" |
" -d dir [file ...] | " |
" -d dir [file ...] | " |
"-u dir [file ...]\n", |
"-u dir [file ...]\n", |
progname); |
progname); |
Line 651 index_merge(const struct of *of, struct mparse *mp, |
|
Line 651 index_merge(const struct of *of, struct mparse *mp, |
|
assert(march); |
assert(march); |
if (strcasecmp(march, of->arch)) |
if (strcasecmp(march, of->arch)) |
WARNING(fn, basedir, "Architecture \"%s\" " |
WARNING(fn, basedir, "Architecture \"%s\" " |
"manual in \"%s\" directory", |
"manual in \"%s\" directory", |
march, of->arch); |
march, of->arch); |
|
|
/* |
/* |
Line 822 index_merge(const struct of *of, struct mparse *mp, |
|
Line 822 index_merge(const struct of *of, struct mparse *mp, |
|
while (0 == (*files->seq)(files, &key, &val, seq)) { |
while (0 == (*files->seq)(files, &key, &val, seq)) { |
seq = R_NEXT; |
seq = R_NEXT; |
if (val.size) |
if (val.size) |
fprintf(stderr, "%s: probably " |
WARNING((char *)val.data, basedir, |
"unreachable, title is %s\n", |
"Probably unreachable, title " |
(char *)val.data, (char *)key.data); |
"is %s", (char *)key.data); |
} |
} |
(*files->close)(files); |
(*files->close)(files); |
} |
} |
Line 837 index_merge(const struct of *of, struct mparse *mp, |
|
Line 837 index_merge(const struct of *of, struct mparse *mp, |
|
* in `idx' (zeroing its value size). |
* in `idx' (zeroing its value size). |
*/ |
*/ |
static void |
static void |
index_prune(const struct of *ofile, struct mdb *mdb, |
index_prune(const struct of *ofile, struct mdb *mdb, |
struct recs *recs, const char *basedir) |
struct recs *recs, const char *basedir) |
{ |
{ |
const struct of *of; |
const struct of *of; |
Line 913 index_prune(const struct of *ofile, struct mdb *mdb, |
|
Line 913 index_prune(const struct of *ofile, struct mdb *mdb, |
|
} |
} |
|
|
if (verb) |
if (verb) |
printf("%s: Deleting from index: %s\n", |
printf("%s: Deleting from index: %s\n", |
basedir, fn); |
basedir, fn); |
|
|
val.size = 0; |
val.size = 0; |
|
|
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, *sv, *title; |
size_t sz; |
size_t sz, titlesz; |
|
|
if (NULL == n) |
if (NULL == n) |
return(0); |
return(0); |
Line 1347 pman_node(MAN_ARGS) |
|
Line 1347 pman_node(MAN_ARGS) |
|
NULL != (body = body->child) && |
NULL != (body = body->child) && |
MAN_TEXT == body->type) { |
MAN_TEXT == body->type) { |
|
|
assert(body->string); |
title = NULL; |
start = sv = body->string; |
titlesz = 0; |
|
/* |
|
* 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. |
|
*/ |
|
for ( ; NULL != body; body = body->next) { |
|
if (MAN_TEXT != body->type) |
|
break; |
|
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) |
|
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 1386 pman_node(MAN_ARGS) |
|
Line 1432 pman_node(MAN_ARGS) |
|
|
|
if (sv == start) { |
if (sv == start) { |
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)) |
Line 1411 pman_node(MAN_ARGS) |
|
Line 1458 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); |
} |
} |
} |
} |
|
|
|
|
ofile_argbuild(int argc, char *argv[], |
ofile_argbuild(int argc, char *argv[], |
struct of **of, const char *basedir) |
struct of **of, const char *basedir) |
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[PATH_MAX]; |
const char *sec, *arch, *title; |
const char *sec, *arch, *title; |
char *p; |
char *p; |
int i, src_form; |
int i, src_form; |
|
|
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, char *basedir) |
int p_src_form, struct of **of, char *basedir) |
{ |
{ |
char buf[MAXPATHLEN]; |
char buf[PATH_MAX]; |
size_t sz; |
size_t sz; |
DIR *d; |
DIR *d; |
const char *fn, *sec, *arch; |
const char *fn, *sec, *arch; |
Line 1701 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1749 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); |
strlcat(basedir, "/", MAXPATHLEN); |
strlcat(basedir, "/", PATH_MAX); |
strlcat(basedir, fn, MAXPATHLEN); |
strlcat(basedir, fn, PATH_MAX); |
sz = strlcat(buf, fn, MAXPATHLEN); |
sz = strlcat(buf, fn, PATH_MAX); |
|
|
if (MAXPATHLEN <= sz) { |
if (PATH_MAX <= sz) { |
WARNING(fn, basedir, "Path too long"); |
WARNING(fn, basedir, "Path too long"); |
continue; |
continue; |
} |
} |
|
|
ofile_dirbuild(buf, sec, arch, |
ofile_dirbuild(buf, sec, arch, |
src_form, of, basedir); |
src_form, of, basedir); |
|
|
p = strrchr(basedir, '/'); |
p = strrchr(basedir, '/'); |
Line 1766 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1814 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 1778 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1826 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) { |
WARNING(fn, basedir, "Path too long"); |
WARNING(fn, basedir, "Path too long"); |
continue; |
continue; |
} |
} |
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) { |
WARNING(fn, basedir, "Path too long"); |
WARNING(fn, basedir, "Path too long"); |
continue; |
continue; |
} |
} |
Line 1800 ofile_dirbuild(const char *dir, const char* psec, cons |
|
Line 1848 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) { |
WARNING(fn, basedir, "Path too long"); |
WARNING(fn, basedir, "Path too long"); |
continue; |
continue; |
} |
} |