version 1.208, 2015/11/07 14:22:29 |
version 1.220, 2016/07/19 13:36:13 |
|
|
/* $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 |
|
|
#else |
#else |
#include "compat_fts.h" |
#include "compat_fts.h" |
#endif |
#endif |
#include <getopt.h> |
|
#include <limits.h> |
#include <limits.h> |
|
#if HAVE_SANDBOX_INIT |
|
#include <sandbox.h> |
|
#endif |
#include <stddef.h> |
#include <stddef.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdint.h> |
#include <stdint.h> |
Line 137 struct mdoc_handler { |
|
Line 139 struct mdoc_handler { |
|
uint64_t mask; /* set unless handler returns 0 */ |
uint64_t mask; /* set unless handler returns 0 */ |
}; |
}; |
|
|
|
|
|
int mandocdb(int, char *[]); |
|
|
static void dbclose(int); |
static void dbclose(int); |
static void dbadd(struct mpage *); |
static void dbadd(struct mpage *); |
static void dbadd_mlink(const struct mlink *mlink); |
static void dbadd_mlink(const struct mlink *mlink); |
Line 156 static void parse_man(struct mpage *, const struct ro |
|
Line 161 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 173 static int parse_mdoc_Nm(struct mpage *, const struct |
|
Line 176 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 180 static void putkeys(const struct mpage *, char *, siz |
|
Line 185 static void putkeys(const struct mpage *, char *, siz |
|
static void putmdockey(const struct mpage *, |
static void putmdockey(const struct mpage *, |
const struct roff_node *, uint64_t); |
const struct roff_node *, uint64_t); |
static int render_string(char **, size_t *); |
static int render_string(char **, size_t *); |
static void say(const char *, const char *, ...); |
static void say(const char *, const char *, ...) |
|
__attribute__((__format__ (printf, 2, 3))); |
static int set_basedir(const char *, int); |
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]); |
Line 242 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
Line 248 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 341 mandocdb(int argc, char *argv[]) |
|
Line 347 mandocdb(int argc, char *argv[]) |
|
|
|
#if HAVE_PLEDGE |
#if HAVE_PLEDGE |
if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) { |
if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) { |
perror("pledge"); |
warn("pledge"); |
return (int)MANDOCLEVEL_SYSERR; |
return (int)MANDOCLEVEL_SYSERR; |
} |
} |
#endif |
#endif |
|
|
|
#if HAVE_SANDBOX_INIT |
|
if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1) { |
|
warnx("sandbox_init"); |
|
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 420 mandocdb(int argc, char *argv[]) |
|
Line 433 mandocdb(int argc, char *argv[]) |
|
argv += optind; |
argv += optind; |
|
|
#if HAVE_PLEDGE |
#if HAVE_PLEDGE |
if (nodb && pledge("stdio rpath", NULL) == -1) { |
if (nodb) { |
perror("pledge"); |
if (pledge("stdio rpath", NULL) == -1) { |
return (int)MANDOCLEVEL_SYSERR; |
warn("pledge"); |
|
return (int)MANDOCLEVEL_SYSERR; |
|
} |
} |
} |
#endif |
#endif |
|
|
Line 452 mandocdb(int argc, char *argv[]) |
|
Line 467 mandocdb(int argc, char *argv[]) |
|
* all files specified on the command-line. |
* all files specified on the command-line. |
*/ |
*/ |
#if HAVE_PLEDGE |
#if HAVE_PLEDGE |
if (!nodb && pledge("stdio rpath wpath cpath fattr flock", |
if (!nodb) { |
NULL) == -1) { |
if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) { |
perror("pledge"); |
warn("pledge"); |
exitcode = (int)MANDOCLEVEL_SYSERR; |
exitcode = (int)MANDOCLEVEL_SYSERR; |
goto out; |
goto out; |
|
} |
} |
} |
#endif |
#endif |
use_all = 1; |
use_all = 1; |
|
|
* or |
* or |
* [./]cat<section>[/<arch>]/<name>.0 |
* [./]cat<section>[/<arch>]/<name>.0 |
* |
* |
* TODO: accomodate for multi-language directories. |
* TODO: accommodate for multi-language directories. |
*/ |
*/ |
static int |
static int |
treescan(void) |
treescan(void) |
Line 1131 mpages_merge(struct mparse *mp) |
|
Line 1147 mpages_merge(struct mparse *mp) |
|
man = NULL; |
man = NULL; |
sodest = NULL; |
sodest = NULL; |
|
|
mparse_open(mp, &fd, mlink->file); |
if ((fd = mparse_open(mp, mlink->file)) == -1) { |
if (fd == -1) { |
|
say(mlink->file, "&open"); |
say(mlink->file, "&open"); |
goto nextpage; |
goto nextpage; |
} |
} |
Line 1143 mpages_merge(struct mparse *mp) |
|
Line 1158 mpages_merge(struct mparse *mp) |
|
*/ |
*/ |
if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) { |
if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) { |
mparse_readfd(mp, fd, mlink->file); |
mparse_readfd(mp, fd, mlink->file); |
|
close(fd); |
mparse_result(mp, &man, &sodest); |
mparse_result(mp, &man, &sodest); |
} |
} |
|
|
Line 1291 parse_cat(struct mpage *mpage, int fd) |
|
Line 1307 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 1304 parse_cat(struct mpage *mpage, int fd) |
|
Line 1322 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 1315 parse_cat(struct mpage *mpage, int fd) |
|
Line 1336 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 1329 parse_cat(struct mpage *mpage, int fd) |
|
Line 1350 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 1360 parse_cat(struct mpage *mpage, int fd) |
|
Line 1381 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 1439 parse_man(struct mpage *mpage, const struct roff_meta |
|
Line 1459 parse_man(struct mpage *mpage, const struct roff_meta |
|
char byte; |
char byte; |
size_t sz; |
size_t sz; |
|
|
if (NULL == n) |
if (n == NULL) |
return; |
return; |
|
|
/* |
/* |
Line 1451 parse_man(struct mpage *mpage, const struct roff_meta |
|
Line 1471 parse_man(struct mpage *mpage, const struct roff_meta |
|
|
|
if (n->type == ROFFT_BODY && n->tok == MAN_SH) { |
if (n->type == ROFFT_BODY && n->tok == MAN_SH) { |
body = n; |
body = n; |
assert(body->parent); |
if ((head = body->parent->head) != NULL && |
if (NULL != (head = body->parent->head) && |
(head = head->child) != NULL && |
1 == head->nchild && |
head->next == NULL && |
NULL != (head = (head->child)) && |
|
head->type == ROFFT_TEXT && |
head->type == ROFFT_TEXT && |
0 == strcmp(head->string, "NAME") && |
strcmp(head->string, "NAME") == 0 && |
NULL != body->child) { |
body->child != NULL) { |
|
|
/* |
/* |
* Suck the entire NAME section into memory. |
* Suck the entire NAME section into memory. |
Line 1682 parse_mdoc_Fo(struct mpage *mpage, const struct roff_m |
|
Line 1701 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->child != NULL && |
|
n->child->next == NULL && |
|
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 1749 parse_mdoc_head(struct mpage *mpage, const struct roff |
|
Line 1793 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 1783 putkeys(const struct mpage *mpage, char *cp, size_t sz |
|
Line 1819 putkeys(const struct mpage *mpage, char *cp, size_t sz |
|
name_mask &= ~NAME_FIRST; |
name_mask &= ~NAME_FIRST; |
if (debug > 1) |
if (debug > 1) |
say(mpage->mlinks->file, |
say(mpage->mlinks->file, |
"Adding name %*s, bits=%d", sz, cp, v); |
"Adding name %*s, bits=0x%llu", (int)sz, cp, v); |
} else { |
} else { |
htab = &strings; |
htab = &strings; |
if (debug > 1) |
if (debug > 1) |
Line 1791 putkeys(const struct mpage *mpage, char *cp, size_t sz |
|
Line 1827 putkeys(const struct mpage *mpage, char *cp, size_t sz |
|
if ((uint64_t)1 << i & v) |
if ((uint64_t)1 << i & v) |
say(mpage->mlinks->file, |
say(mpage->mlinks->file, |
"Adding key %s=%*s", |
"Adding key %s=%*s", |
mansearch_keynames[i], sz, cp); |
mansearch_keynames[i], (int)sz, cp); |
} |
} |
|
|
end = cp + sz; |
end = cp + sz; |