version 1.202, 2015/10/13 15:53:05 |
version 1.211, 2016/01/04 14:44:57 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2011-2016 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 <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
|
#if HAVE_ERR |
#include <err.h> |
#include <err.h> |
|
#endif |
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#if HAVE_FTS |
#if HAVE_FTS |
Line 154 static void parse_man(struct mpage *, const struct ro |
|
Line 156 static void parse_man(struct mpage *, const struct ro |
|
const struct roff_node *); |
const struct roff_node *); |
static void parse_mdoc(struct mpage *, const struct roff_meta *, |
static void parse_mdoc(struct mpage *, const struct roff_meta *, |
const struct roff_node *); |
const struct roff_node *); |
static int parse_mdoc_body(struct mpage *, const struct roff_meta *, |
|
const struct roff_node *); |
|
static int parse_mdoc_head(struct mpage *, const struct roff_meta *, |
static int parse_mdoc_head(struct mpage *, const struct roff_meta *, |
const struct roff_node *); |
const struct roff_node *); |
static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *, |
static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *, |
Line 171 static int parse_mdoc_Nm(struct mpage *, const struct |
|
Line 171 static int parse_mdoc_Nm(struct mpage *, const struct |
|
const struct roff_node *); |
const struct roff_node *); |
static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *, |
static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *, |
const struct roff_node *); |
const struct roff_node *); |
|
static int parse_mdoc_Va(struct mpage *, const struct roff_meta *, |
|
const struct roff_node *); |
static int parse_mdoc_Xr(struct mpage *, const struct roff_meta *, |
static int parse_mdoc_Xr(struct mpage *, const struct roff_meta *, |
const struct roff_node *); |
const struct roff_node *); |
static void putkey(const struct mpage *, char *, uint64_t); |
static void putkey(const struct mpage *, char *, uint64_t); |
Line 183 static int set_basedir(const char *, int); |
|
Line 185 static int set_basedir(const char *, int); |
|
static int treescan(void); |
static int treescan(void); |
static size_t utf8(unsigned int, char [7]); |
static size_t utf8(unsigned int, char [7]); |
|
|
extern char *__progname; |
|
|
|
static char tempfilename[32]; |
static char tempfilename[32]; |
static int nodb; /* no database changes */ |
static int nodb; /* no database changes */ |
static int mparse_options; /* abort the parse early */ |
static int mparse_options; /* abort the parse early */ |
Line 195 static int write_utf8; /* write UTF-8 output; else A |
|
Line 195 static int write_utf8; /* write UTF-8 output; else A |
|
static int exitcode; /* to be returned by main */ |
static int exitcode; /* to be returned by main */ |
static enum op op; /* operational mode */ |
static enum op op; /* operational mode */ |
static char basedir[PATH_MAX]; /* current base directory */ |
static char basedir[PATH_MAX]; /* current base directory */ |
static struct mchars *mchars; /* table of named characters */ |
|
static struct ohash mpages; /* table of distinct manual pages */ |
static struct ohash mpages; /* table of distinct manual pages */ |
static struct ohash mlinks; /* table of directory entries */ |
static struct ohash mlinks; /* table of directory entries */ |
static struct ohash names; /* table of all names */ |
static struct ohash names; /* table of all names */ |
Line 243 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
Line 242 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
{ NULL, TYPE_Pa }, /* Pa */ |
{ NULL, TYPE_Pa }, /* Pa */ |
{ NULL, 0 }, /* Rv */ |
{ NULL, 0 }, /* Rv */ |
{ NULL, TYPE_St }, /* St */ |
{ NULL, TYPE_St }, /* St */ |
{ NULL, TYPE_Va }, /* Va */ |
{ parse_mdoc_Va, TYPE_Va }, /* Va */ |
{ parse_mdoc_body, TYPE_Va }, /* Vt */ |
{ parse_mdoc_Va, TYPE_Vt }, /* Vt */ |
{ parse_mdoc_Xr, 0 }, /* Xr */ |
{ parse_mdoc_Xr, 0 }, /* Xr */ |
{ NULL, 0 }, /* %A */ |
{ NULL, 0 }, /* %A */ |
{ NULL, 0 }, /* %B */ |
{ NULL, 0 }, /* %B */ |
Line 336 mandocdb(int argc, char *argv[]) |
|
Line 335 mandocdb(int argc, char *argv[]) |
|
{ |
{ |
struct manconf conf; |
struct manconf conf; |
struct mparse *mp; |
struct mparse *mp; |
const char *path_arg; |
const char *path_arg, *progname; |
size_t j, sz; |
size_t j, sz; |
int ch, i; |
int ch, i; |
|
|
|
#if HAVE_PLEDGE |
|
if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) { |
|
perror("pledge"); |
|
return (int)MANDOCLEVEL_SYSERR; |
|
} |
|
#endif |
|
|
memset(&conf, 0, sizeof(conf)); |
memset(&conf, 0, sizeof(conf)); |
memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *)); |
memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *)); |
|
|
Line 413 mandocdb(int argc, char *argv[]) |
|
Line 419 mandocdb(int argc, char *argv[]) |
|
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
|
#if HAVE_PLEDGE |
|
if (nodb) { |
|
if (pledge("stdio rpath", NULL) == -1) { |
|
perror("pledge"); |
|
return (int)MANDOCLEVEL_SYSERR; |
|
} |
|
} |
|
#endif |
|
|
if (OP_CONFFILE == op && argc > 0) { |
if (OP_CONFFILE == op && argc > 0) { |
warnx("-C: Too many arguments"); |
warnx("-C: Too many arguments"); |
goto usage; |
goto usage; |
} |
} |
|
|
exitcode = (int)MANDOCLEVEL_OK; |
exitcode = (int)MANDOCLEVEL_OK; |
mchars = mchars_alloc(); |
mchars_alloc(); |
mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, |
mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, NULL); |
mchars, NULL); |
|
mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev)); |
mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev)); |
mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file)); |
mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file)); |
|
|
Line 439 mandocdb(int argc, char *argv[]) |
|
Line 453 mandocdb(int argc, char *argv[]) |
|
* The existing database is usable. Process |
* The existing database is usable. Process |
* all files specified on the command-line. |
* all files specified on the command-line. |
*/ |
*/ |
|
#if HAVE_PLEDGE |
|
if (!nodb) { |
|
if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) { |
|
perror("pledge"); |
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
|
goto out; |
|
} |
|
} |
|
#endif |
use_all = 1; |
use_all = 1; |
for (i = 0; i < argc; i++) |
for (i = 0; i < argc; i++) |
filescan(argv[i]); |
filescan(argv[i]); |
Line 522 mandocdb(int argc, char *argv[]) |
|
Line 545 mandocdb(int argc, char *argv[]) |
|
out: |
out: |
manconf_free(&conf); |
manconf_free(&conf); |
mparse_free(mp); |
mparse_free(mp); |
mchars_free(mchars); |
mchars_free(); |
mpages_free(); |
mpages_free(); |
ohash_delete(&mpages); |
ohash_delete(&mpages); |
ohash_delete(&mlinks); |
ohash_delete(&mlinks); |
return exitcode; |
return exitcode; |
usage: |
usage: |
|
progname = getprogname(); |
fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n" |
fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n" |
" %s [-aDnpQ] [-Tutf8] dir ...\n" |
" %s [-aDnpQ] [-Tutf8] dir ...\n" |
" %s [-DnpQ] [-Tutf8] -d dir [file ...]\n" |
" %s [-DnpQ] [-Tutf8] -d dir [file ...]\n" |
" %s [-Dnp] -u dir [file ...]\n" |
" %s [-Dnp] -u dir [file ...]\n" |
" %s [-Q] -t file ...\n", |
" %s [-Q] -t file ...\n", |
__progname, __progname, __progname, |
progname, progname, progname, progname, progname); |
__progname, __progname); |
|
|
|
return (int)MANDOCLEVEL_BADARG; |
return (int)MANDOCLEVEL_BADARG; |
} |
} |
Line 1168 mpages_merge(struct mparse *mp) |
|
Line 1191 mpages_merge(struct mparse *mp) |
|
} |
} |
goto nextpage; |
goto nextpage; |
} else if (man != NULL && man->macroset == MACROSET_MDOC) { |
} else if (man != NULL && man->macroset == MACROSET_MDOC) { |
|
mdoc_validate(man); |
mpage->form = FORM_SRC; |
mpage->form = FORM_SRC; |
mpage->sec = man->meta.msec; |
mpage->sec = man->meta.msec; |
mpage->sec = mandoc_strdup( |
mpage->sec = mandoc_strdup( |
Line 1177 mpages_merge(struct mparse *mp) |
|
Line 1201 mpages_merge(struct mparse *mp) |
|
mpage->arch == NULL ? "" : mpage->arch); |
mpage->arch == NULL ? "" : mpage->arch); |
mpage->title = mandoc_strdup(man->meta.title); |
mpage->title = mandoc_strdup(man->meta.title); |
} else if (man != NULL && man->macroset == MACROSET_MAN) { |
} else if (man != NULL && man->macroset == MACROSET_MAN) { |
|
man_validate(man); |
mpage->form = FORM_SRC; |
mpage->form = FORM_SRC; |
mpage->sec = mandoc_strdup(man->meta.msec); |
mpage->sec = mandoc_strdup(man->meta.msec); |
mpage->arch = mandoc_strdup(mlink->arch); |
mpage->arch = mandoc_strdup(mlink->arch); |
Line 1269 parse_cat(struct mpage *mpage, int fd) |
|
Line 1294 parse_cat(struct mpage *mpage, int fd) |
|
{ |
{ |
FILE *stream; |
FILE *stream; |
char *line, *p, *title; |
char *line, *p, *title; |
size_t len, plen, titlesz; |
size_t linesz, plen, titlesz; |
|
ssize_t len; |
|
int offs; |
|
|
stream = (-1 == fd) ? |
stream = (-1 == fd) ? |
fopen(mpage->mlinks->file, "r") : |
fopen(mpage->mlinks->file, "r") : |
Line 1282 parse_cat(struct mpage *mpage, int fd) |
|
Line 1309 parse_cat(struct mpage *mpage, int fd) |
|
return; |
return; |
} |
} |
|
|
|
line = NULL; |
|
linesz = 0; |
|
|
/* Skip to first blank line. */ |
/* Skip to first blank line. */ |
|
|
while (NULL != (line = fgetln(stream, &len))) |
while (getline(&line, &linesz, stream) != -1) |
if ('\n' == *line) |
if (*line == '\n') |
break; |
break; |
|
|
/* |
/* |
Line 1293 parse_cat(struct mpage *mpage, int fd) |
|
Line 1323 parse_cat(struct mpage *mpage, int fd) |
|
* is the first section header. Skip to it. |
* is the first section header. Skip to it. |
*/ |
*/ |
|
|
while (NULL != (line = fgetln(stream, &len))) |
while (getline(&line, &linesz, stream) != -1) |
if ('\n' != *line && ' ' != *line) |
if (*line != '\n' && *line != ' ') |
break; |
break; |
|
|
/* |
/* |
Line 1307 parse_cat(struct mpage *mpage, int fd) |
|
Line 1337 parse_cat(struct mpage *mpage, int fd) |
|
titlesz = 0; |
titlesz = 0; |
title = NULL; |
title = NULL; |
|
|
while (NULL != (line = fgetln(stream, &len))) { |
while ((len = getline(&line, &linesz, stream)) != -1) { |
if (' ' != *line || '\n' != line[len - 1]) |
if (*line != ' ') |
break; |
break; |
while (len > 0 && isspace((unsigned char)*line)) { |
offs = 0; |
line++; |
while (isspace((unsigned char)line[offs])) |
len--; |
offs++; |
} |
if (line[offs] == '\0') |
if (1 == len) |
|
continue; |
continue; |
title = mandoc_realloc(title, titlesz + len); |
title = mandoc_realloc(title, titlesz + len - offs); |
memcpy(title + titlesz, line, len); |
memcpy(title + titlesz, line + offs, len - offs); |
titlesz += len; |
titlesz += len - offs; |
title[titlesz - 1] = ' '; |
title[titlesz - 1] = ' '; |
} |
} |
|
free(line); |
|
|
/* |
/* |
* If no page content can be found, or the input line |
* If no page content can be found, or the input line |
Line 1338 parse_cat(struct mpage *mpage, int fd) |
|
Line 1368 parse_cat(struct mpage *mpage, int fd) |
|
return; |
return; |
} |
} |
|
|
title = mandoc_realloc(title, titlesz + 1); |
title[titlesz - 1] = '\0'; |
title[titlesz] = '\0'; |
|
|
|
/* |
/* |
* Skip to the first dash. |
* Skip to the first dash. |
Line 1660 parse_mdoc_Fo(struct mpage *mpage, const struct roff_m |
|
Line 1689 parse_mdoc_Fo(struct mpage *mpage, const struct roff_m |
|
} |
} |
|
|
static int |
static int |
|
parse_mdoc_Va(struct mpage *mpage, const struct roff_meta *meta, |
|
const struct roff_node *n) |
|
{ |
|
char *cp; |
|
|
|
if (n->type != ROFFT_ELEM && n->type != ROFFT_BODY) |
|
return 0; |
|
|
|
if (n->nchild == 1 && n->child->type == ROFFT_TEXT) |
|
return 1; |
|
|
|
cp = NULL; |
|
deroff(&cp, n); |
|
if (cp != NULL) { |
|
putkey(mpage, cp, TYPE_Vt | (n->tok == MDOC_Va || |
|
n->type == ROFFT_BODY ? TYPE_Va : 0)); |
|
free(cp); |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
static int |
parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta, |
parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta, |
const struct roff_node *n) |
const struct roff_node *n) |
{ |
{ |
Line 1727 parse_mdoc_head(struct mpage *mpage, const struct roff |
|
Line 1779 parse_mdoc_head(struct mpage *mpage, const struct roff |
|
return n->type == ROFFT_HEAD; |
return n->type == ROFFT_HEAD; |
} |
} |
|
|
static int |
|
parse_mdoc_body(struct mpage *mpage, const struct roff_meta *meta, |
|
const struct roff_node *n) |
|
{ |
|
|
|
return n->type == ROFFT_BODY; |
|
} |
|
|
|
/* |
/* |
* Add a string to the hash table for the current manual. |
* Add a string to the hash table for the current manual. |
* Each string has a bitmask telling which macros it belongs to. |
* Each string has a bitmask telling which macros it belongs to. |
Line 1928 render_string(char **public, size_t *psz) |
|
Line 1972 render_string(char **public, size_t *psz) |
|
*/ |
*/ |
|
|
if (write_utf8) { |
if (write_utf8) { |
unicode = mchars_spec2cp(mchars, seq, seqlen); |
unicode = mchars_spec2cp(seq, seqlen); |
if (unicode <= 0) |
if (unicode <= 0) |
continue; |
continue; |
addsz = utf8(unicode, utfbuf); |
addsz = utf8(unicode, utfbuf); |
Line 1936 render_string(char **public, size_t *psz) |
|
Line 1980 render_string(char **public, size_t *psz) |
|
continue; |
continue; |
addcp = utfbuf; |
addcp = utfbuf; |
} else { |
} else { |
addcp = mchars_spec2str(mchars, seq, seqlen, &addsz); |
addcp = mchars_spec2str(seq, seqlen, &addsz); |
if (addcp == NULL) |
if (addcp == NULL) |
continue; |
continue; |
if (*addcp == ASCII_NBRSP) { |
if (*addcp == ASCII_NBRSP) { |