version 1.3, 2011/07/15 09:06:23 |
version 1.4, 2011/07/15 10:15:24 |
|
|
#include <sys/param.h> |
#include <sys/param.h> |
|
|
#include <assert.h> |
#include <assert.h> |
|
#include <dirent.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <getopt.h> |
#include <getopt.h> |
#include <stdio.h> |
#include <stdio.h> |
|
|
struct of { |
struct of { |
char *fname; |
char *fname; |
struct of *next; |
struct of *next; |
|
struct of *first; |
}; |
}; |
|
|
/* Buffer for storing growable data. */ |
/* Buffer for storing growable data. */ |
Line 107 static void index_merge(const struct of *, struct m |
|
Line 109 static void index_merge(const struct of *, struct m |
|
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 *); |
|
static int ofile_build(const char *, struct of **); |
|
static void ofile_free(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 void pmdoc_An(MDOC_ARGS); |
static void pmdoc_An(MDOC_ARGS); |
Line 259 main(int argc, char *argv[]) |
|
Line 263 main(int argc, char *argv[]) |
|
{ |
{ |
struct mparse *mp; /* parse sequence */ |
struct mparse *mp; /* parse sequence */ |
enum op op; /* current operation */ |
enum op op; /* current operation */ |
const char *dir; /* result dir (default: cwd) */ |
|
char ibuf[MAXPATHLEN], /* index fname */ |
char ibuf[MAXPATHLEN], /* index fname */ |
fbuf[MAXPATHLEN]; /* btree fname */ |
fbuf[MAXPATHLEN]; /* btree fname */ |
int ch, verb, i; |
int 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 */ |
enum mandoclevel ec; /* exit status */ |
|
BTREEINFO info; /* btree configuration */ |
BTREEINFO info; /* btree configuration */ |
recno_t maxrec; /* supremum of all records */ |
recno_t maxrec; /* supremum of all records */ |
recno_t *recs; /* buffer of empty records */ |
recno_t *recs; /* buffer of empty records */ |
Line 274 main(int argc, char *argv[]) |
|
Line 276 main(int argc, char *argv[]) |
|
reccur; /* valid number of recs */ |
reccur; /* valid number of recs */ |
struct buf buf, /* keyword buffer */ |
struct buf buf, /* keyword buffer */ |
dbuf; /* description buffer */ |
dbuf; /* description buffer */ |
struct of *ofile; |
struct of *of; |
extern int optind; |
extern int optind; |
extern char *optarg; |
extern char *optarg; |
|
|
Line 284 main(int argc, char *argv[]) |
|
Line 286 main(int argc, char *argv[]) |
|
else |
else |
++progname; |
++progname; |
|
|
ofile = NULL; |
of = NULL; |
dir = ""; |
|
verb = 0; |
|
db = idx = NULL; |
db = idx = NULL; |
mp = NULL; |
mp = NULL; |
hash = NULL; |
hash = NULL; |
Line 294 main(int argc, char *argv[]) |
|
Line 294 main(int argc, char *argv[]) |
|
recsz = reccur = 0; |
recsz = reccur = 0; |
maxrec = 0; |
maxrec = 0; |
op = OP_NEW; |
op = OP_NEW; |
ec = MANDOCLEVEL_SYSERR; |
|
|
|
memset(&buf, 0, sizeof(struct buf)); |
memset(&buf, 0, sizeof(struct buf)); |
memset(&dbuf, 0, sizeof(struct buf)); |
memset(&dbuf, 0, sizeof(struct buf)); |
|
|
while (-1 != (ch = getopt(argc, argv, "d:ruv"))) |
while (-1 != (ch = getopt(argc, argv, ""))) |
switch (ch) { |
switch (ch) { |
case ('d'): |
|
dir = optarg; |
|
break; |
|
case ('r'): |
|
op = OP_DELETE; |
|
break; |
|
case ('u'): |
|
op = OP_UPDATE; |
|
break; |
|
case ('v'): |
|
verb++; |
|
break; |
|
default: |
default: |
usage(); |
usage(); |
return((int)MANDOCLEVEL_BADARG); |
return((int)MANDOCLEVEL_BADARG); |
Line 321 main(int argc, char *argv[]) |
|
Line 308 main(int argc, char *argv[]) |
|
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
ibuf[0] = ibuf[MAXPATHLEN - 2] = |
memset(&info, 0, sizeof(BTREEINFO)); |
fbuf[0] = fbuf[MAXPATHLEN - 2] = '\0'; |
info.flags = R_DUP; |
|
|
strlcat(fbuf, dir, MAXPATHLEN); |
mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL); |
strlcat(fbuf, MANDOC_DB, MAXPATHLEN); |
|
|
|
strlcat(ibuf, dir, MAXPATHLEN); |
flags = OP_NEW == op ? O_CREAT|O_TRUNC|O_RDWR : O_CREAT|O_RDWR; |
strlcat(ibuf, MANDOC_IDX, MAXPATHLEN); |
|
|
|
if ('\0' != fbuf[MAXPATHLEN - 2] || |
buf.size = dbuf.size = MANDOC_BUFSZ; |
'\0' != ibuf[MAXPATHLEN - 2]) { |
|
fprintf(stderr, "%s: Path too long\n", dir); |
|
goto out; |
|
} |
|
|
|
/* |
buf.cp = mandoc_malloc(buf.size); |
* For the keyword database, open a BTREE database that allows |
dbuf.cp = mandoc_malloc(dbuf.size); |
* duplicates. |
|
* For the index database, use a standard RECNO database type. |
|
* Truncate the database if we're creating a new one. |
|
*/ |
|
|
|
memset(&info, 0, sizeof(BTREEINFO)); |
for (i = 0; i < argc; i++) { |
info.flags = R_DUP; |
ibuf[0] = ibuf[MAXPATHLEN - 2] = |
|
fbuf[0] = fbuf[MAXPATHLEN - 2] = '\0'; |
|
|
if (OP_NEW == op) { |
strlcat(fbuf, argv[i], MAXPATHLEN); |
db = dbopen(fbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info); |
strlcat(fbuf, MANDOC_DB, MAXPATHLEN); |
idx = dbopen(ibuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL); |
|
} else { |
|
db = dbopen(fbuf, O_CREAT|O_RDWR, 0644, DB_BTREE, &info); |
|
idx = dbopen(ibuf, O_CREAT|O_RDWR, 0644, DB_RECNO, NULL); |
|
} |
|
|
|
if (NULL == db) { |
strlcat(ibuf, argv[i], MAXPATHLEN); |
perror(fbuf); |
strlcat(ibuf, MANDOC_IDX, MAXPATHLEN); |
goto out; |
|
} else if (NULL == db) { |
|
perror(ibuf); |
|
goto out; |
|
} |
|
|
|
ofile = mandoc_calloc(argc, sizeof(struct of)); |
if ('\0' != fbuf[MAXPATHLEN - 2] || |
for (i = 0; i < argc; i++) { |
'\0' != ibuf[MAXPATHLEN - 2]) { |
ofile[i].next = &ofile[i + 1]; |
fprintf(stderr, "%s: Path too long\n", argv[i]); |
ofile[i].fname = argv[i]; |
break; |
} |
} |
|
|
ofile[argc - 1].next = NULL; |
db = dbopen(fbuf, flags, 0644, DB_BTREE, &info); |
|
idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL); |
|
|
/* |
if (NULL == db) { |
* If we're going to delete or update a database, remove the |
perror(fbuf); |
* entries now (both the index and all keywords pointing to it). |
break; |
* This doesn't actually remove them: it only sets their record |
} else if (NULL == db) { |
* value lengths to zero. |
perror(ibuf); |
* While doing so, add the empty records to a list we'll access |
break; |
* later in re-adding entries to the database. |
} |
*/ |
|
|
|
if (OP_DELETE == op || OP_UPDATE == op) |
ofile_free(of); |
index_prune(ofile, db, fbuf, idx, ibuf, |
of = NULL; |
&maxrec, &recs, &recsz); |
|
|
|
if (OP_DELETE == op) { |
if ( ! ofile_build(argv[i], &of)) |
ec = MANDOCLEVEL_OK; |
break; |
goto out; |
of = of->first; |
} |
|
|
|
/* |
if (OP_DELETE == op || OP_UPDATE == op) |
* Add records to the database. |
index_prune(of, db, fbuf, idx, ibuf, |
* Try parsing each manual given on the command line. |
&maxrec, &recs, &recsz); |
* If we fail, then emit an error and keep on going. |
|
* Take resulting trees and push them down into the database code. |
|
* Use the auto-parser and don't report any errors. |
|
*/ |
|
|
|
mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL); |
if (OP_DELETE == op) |
|
continue; |
|
|
buf.size = dbuf.size = MANDOC_BUFSZ; |
index_merge(of, mp, &dbuf, &buf, hash, db, |
buf.cp = mandoc_malloc(buf.size); |
fbuf, idx, ibuf, maxrec, recs, reccur); |
dbuf.cp = mandoc_malloc(dbuf.size); |
} |
|
|
index_merge(ofile, mp, &dbuf, &buf, hash, db, |
|
fbuf, idx, ibuf, maxrec, recs, reccur); |
|
|
|
ec = MANDOCLEVEL_OK; |
|
out: |
|
if (db) |
if (db) |
(*db->close)(db); |
(*db->close)(db); |
if (idx) |
if (idx) |
|
|
if (mp) |
if (mp) |
mparse_free(mp); |
mparse_free(mp); |
|
|
free(ofile); |
ofile_free(of); |
free(buf.cp); |
free(buf.cp); |
free(dbuf.cp); |
free(dbuf.cp); |
free(recs); |
free(recs); |
|
|
return((int)ec); |
return(i < argc ? MANDOCLEVEL_SYSERR : MANDOCLEVEL_OK); |
} |
} |
|
|
void |
void |
Line 1144 pman_node(MAN_ARGS) |
|
Line 1102 pman_node(MAN_ARGS) |
|
return(0); |
return(0); |
} |
} |
|
|
|
/* |
|
* Recursively build up a list of files to parse. |
|
* We use this instead of ftw() and so on because I don't want global |
|
* variables hanging around. |
|
* This ignores the mandoc.db and mandoc.index files, but assumes that |
|
* everything else is a manual. |
|
* Pass in a pointer to a NULL structure for the first invocation. |
|
*/ |
|
static int |
|
ofile_build(const char *dir, struct of **of) |
|
{ |
|
DIR *d; |
|
const char *fn; |
|
struct of *nof; |
|
struct dirent *dp; |
|
|
|
if (NULL == (d = opendir(dir))) { |
|
perror(dir); |
|
return(0); |
|
} |
|
|
|
while (NULL != (dp = readdir(d))) { |
|
fn = dp->d_name; |
|
if (DT_DIR == dp->d_type) { |
|
if (strcmp(".", fn) && strcmp("..", fn)) |
|
if ( ! ofile_build(dp->d_name, of)) |
|
return(0); |
|
continue; |
|
} else if (DT_REG != dp->d_type) |
|
continue; |
|
|
|
if (0 == strcmp(MANDOC_DB, fn) || |
|
0 == strcmp(MANDOC_IDX, fn)) |
|
continue; |
|
|
|
nof = mandoc_calloc(1, sizeof(struct of)); |
|
nof->fname = mandoc_strdup(fn); |
|
|
|
if (NULL == *of) { |
|
*of = nof; |
|
(*of)->first = nof; |
|
} else { |
|
(*of)->next = nof; |
|
*of = nof; |
|
} |
|
} |
|
|
|
return(1); |
|
} |
|
|
static void |
static void |
|
ofile_free(struct of *of) |
|
{ |
|
struct of *nof; |
|
|
|
while (of) { |
|
nof = of->next; |
|
free(of->fname); |
|
free(of); |
|
of = nof; |
|
} |
|
} |
|
|
|
static void |
usage(void) |
usage(void) |
{ |
{ |
|
|
fprintf(stderr, "usage: %s [-ruv] [-d path] [file...]\n", |
fprintf(stderr, "usage: %s [dir...]\n", progname); |
progname); |
|
} |
} |