version 1.1, 2011/04/02 15:40:40 |
version 1.2, 2011/04/03 10:14:45 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
|
#ifdef HAVE_CONFIG_H |
|
#include "config.h" |
|
#endif |
|
|
#include <sys/param.h> |
#include <sys/param.h> |
|
|
#include <assert.h> |
#include <assert.h> |
|
|
#include "mandoc.h" |
#include "mandoc.h" |
|
|
#define MANDOC_DB "mandoc.db" |
#define MANDOC_DB "mandoc.db" |
#define MANDOC_BUFSZ 10 |
#define MANDOC_BUFSZ BUFSIZ |
|
|
enum type { |
enum type { |
MANDOC_NONE = 0, |
MANDOC_NONE = 0, |
Line 201 static const pmdoc_nf mdocs[MDOC_MAX] = { |
|
Line 205 static const pmdoc_nf mdocs[MDOC_MAX] = { |
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
struct mparse *mp; |
struct mparse *mp; /* parse sequence */ |
struct mdoc *mdoc; |
struct mdoc *mdoc; /* resulting mdoc */ |
struct man *man; |
const char *fn, |
const char *f, *fn; |
*dir; /* result dir (default: cwd) */ |
size_t sz; |
char fbuf[MAXPATHLEN], /* btree fname */ |
char fbuf[MAXPATHLEN]; |
fbbuf[MAXPATHLEN]; /* btree backup fname */ |
int c; |
int c; |
DB *db; |
DB *db; /* open database */ |
DBT key, val; |
DBT key, val; /* persistent entries */ |
size_t ksz, vsz; |
size_t ksz, vsz; /* entry buffer sizes */ |
BTREEINFO info; |
BTREEINFO info; /* btree configuration */ |
extern int optind; |
extern int optind; |
extern char *optarg; |
extern char *optarg; |
|
|
f = MANDOC_DB; |
|
|
|
progname = strrchr(argv[0], '/'); |
progname = strrchr(argv[0], '/'); |
if (progname == NULL) |
if (progname == NULL) |
progname = argv[0]; |
progname = argv[0]; |
else |
else |
++progname; |
++progname; |
|
|
while (-1 != (c = getopt(argc, argv, "f:V"))) |
dir = "./"; |
|
|
|
while (-1 != (c = getopt(argc, argv, "d:V"))) |
switch (c) { |
switch (c) { |
case ('f'): |
case ('d'): |
f = optarg; |
dir = optarg; |
break; |
break; |
case ('V'): |
case ('V'): |
version(); |
version(); |
Line 245 main(int argc, char *argv[]) |
|
Line 249 main(int argc, char *argv[]) |
|
* file-name after we've written all of our data. |
* file-name after we've written all of our data. |
*/ |
*/ |
|
|
if (0 == (sz = strlen(f)) || sz + 5 >= MAXPATHLEN) { |
fbuf[0] = fbuf[MAXPATHLEN - 2] = |
|
fbbuf[0] = fbbuf[MAXPATHLEN - 1] = '\0'; |
|
|
|
strlcat(fbuf, dir, MAXPATHLEN); |
|
strlcat(fbuf, MANDOC_DB, MAXPATHLEN); |
|
strlcat(fbbuf, fbuf, MAXPATHLEN); |
|
strlcat(fbbuf, "~", MAXPATHLEN); |
|
|
|
if ('\0' != fbuf[MAXPATHLEN - 2] || |
|
'\0' != fbbuf[MAXPATHLEN - 2]) { |
fprintf(stderr, "%s: Bad filename\n", progname); |
fprintf(stderr, "%s: Bad filename\n", progname); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
memcpy(fbuf, f, sz); |
|
memcpy(fbuf + (int)sz, ".bak", 4); |
|
fbuf[(int)sz + 4] = '\0'; |
|
|
|
/* |
/* |
* Open a BTREE database that allows duplicates. If the |
* Open a BTREE database that allows duplicates. If the |
* database already exists (it's a backup anyway), then blow it |
* database already exists (it's a backup anyway), then blow it |
Line 263 main(int argc, char *argv[]) |
|
Line 272 main(int argc, char *argv[]) |
|
memset(&info, 0, sizeof(BTREEINFO)); |
memset(&info, 0, sizeof(BTREEINFO)); |
info.flags = R_DUP; |
info.flags = R_DUP; |
|
|
db = dbopen(fbuf, O_CREAT|O_TRUNC|O_RDWR, |
db = dbopen(fbbuf, O_CREAT|O_TRUNC|O_RDWR, |
0644, DB_BTREE, &info); |
0644, DB_BTREE, &info); |
|
|
if (NULL == db) { |
if (NULL == db) { |
perror(f); |
perror(fbbuf); |
exit((int)MANDOCLEVEL_SYSERR); |
exit((int)MANDOCLEVEL_SYSERR); |
} |
} |
|
|
Line 286 main(int argc, char *argv[]) |
|
Line 295 main(int argc, char *argv[]) |
|
ksz = vsz = 0; |
ksz = vsz = 0; |
|
|
while (NULL != (fn = *argv++)) { |
while (NULL != (fn = *argv++)) { |
printf("Trying: %s\n", fn); |
|
mparse_reset(mp); |
mparse_reset(mp); |
if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) |
if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) |
continue; |
continue; |
mparse_result(mp, &mdoc, &man); |
mparse_result(mp, &mdoc, NULL); |
if (mdoc) |
if (mdoc) |
pmdoc(db, fbuf, &key, &ksz, |
pmdoc(db, fbbuf, &key, &ksz, |
&val, &vsz, fn, mdoc); |
&val, &vsz, fn, mdoc); |
} |
} |
|
|
Line 304 main(int argc, char *argv[]) |
|
Line 312 main(int argc, char *argv[]) |
|
|
|
/* Atomically replace the file with our temporary one. */ |
/* Atomically replace the file with our temporary one. */ |
|
|
if (-1 == rename(fbuf, f)) |
if (-1 == rename(fbbuf, fbuf)) |
perror(f); |
perror(fbuf); |
|
|
return((int)MANDOCLEVEL_OK); |
return((int)MANDOCLEVEL_OK); |
} |
} |
|
|
|
|
fprintf(stderr, "usage: %s " |
fprintf(stderr, "usage: %s " |
"[-V] " |
"[-V] " |
"[-f path] " |
"[-d path] " |
"[file...]\n", |
"[file...]\n", |
progname); |
progname); |
} |
} |