=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.206 retrieving revision 1.218 diff -u -p -r1.206 -r1.218 --- mandoc/mandocdb.c 2015/11/06 16:30:33 1.206 +++ mandoc/mandocdb.c 2016/07/12 05:18:38 1.218 @@ -1,7 +1,7 @@ -/* $Id: mandocdb.c,v 1.206 2015/11/06 16:30:33 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.218 2016/07/12 05:18:38 kristaps Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons - * Copyright (c) 2011-2015 Ingo Schwarze + * Copyright (c) 2011-2016 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,7 +23,9 @@ #include #include +#if HAVE_ERR #include +#endif #include #include #if HAVE_FTS @@ -31,8 +33,10 @@ #else #include "compat_fts.h" #endif -#include #include +#if HAVE_SANDBOX_INIT +#include +#endif #include #include #include @@ -154,8 +158,6 @@ static void parse_man(struct mpage *, const struct ro const struct roff_node *); static void parse_mdoc(struct mpage *, const struct roff_meta *, 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 *, const struct roff_node *); static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *, @@ -171,6 +173,8 @@ static int parse_mdoc_Nm(struct mpage *, const struct const struct roff_node *); static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *, 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 *, const struct roff_node *); static void putkey(const struct mpage *, char *, uint64_t); @@ -240,8 +244,8 @@ static const struct mdoc_handler mdocs[MDOC_MAX] = { { NULL, TYPE_Pa }, /* Pa */ { NULL, 0 }, /* Rv */ { NULL, TYPE_St }, /* St */ - { NULL, TYPE_Va }, /* Va */ - { parse_mdoc_body, TYPE_Va }, /* Vt */ + { parse_mdoc_Va, TYPE_Va }, /* Va */ + { parse_mdoc_Va, TYPE_Vt }, /* Vt */ { parse_mdoc_Xr, 0 }, /* Xr */ { NULL, 0 }, /* %A */ { NULL, 0 }, /* %B */ @@ -337,6 +341,20 @@ mandocdb(int argc, char *argv[]) size_t j, sz; int ch, i; +#if HAVE_PLEDGE + if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) { + warn("pledge"); + return (int)MANDOCLEVEL_SYSERR; + } +#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(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *)); @@ -410,6 +428,15 @@ mandocdb(int argc, char *argv[]) argc -= optind; argv += optind; +#if HAVE_PLEDGE + if (nodb) { + if (pledge("stdio rpath", NULL) == -1) { + warn("pledge"); + return (int)MANDOCLEVEL_SYSERR; + } + } +#endif + if (OP_CONFFILE == op && argc > 0) { warnx("-C: Too many arguments"); goto usage; @@ -435,6 +462,15 @@ mandocdb(int argc, char *argv[]) * The existing database is usable. Process * all files specified on the command-line. */ +#if HAVE_PLEDGE + if (!nodb) { + if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) { + warn("pledge"); + exitcode = (int)MANDOCLEVEL_SYSERR; + goto out; + } + } +#endif use_all = 1; for (i = 0; i < argc; i++) filescan(argv[i]); @@ -547,7 +583,7 @@ usage: * or * [./]cat
[/]/.0 * - * TODO: accomodate for multi-language directories. + * TODO: accommodate for multi-language directories. */ static int treescan(void) @@ -1107,8 +1143,7 @@ mpages_merge(struct mparse *mp) man = NULL; sodest = NULL; - mparse_open(mp, &fd, mlink->file); - if (fd == -1) { + if ((fd = mparse_open(mp, mlink->file)) == -1) { say(mlink->file, "&open"); goto nextpage; } @@ -1119,6 +1154,7 @@ mpages_merge(struct mparse *mp) */ if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) { mparse_readfd(mp, fd, mlink->file); + close(fd); mparse_result(mp, &man, &sodest); } @@ -1267,7 +1303,9 @@ parse_cat(struct mpage *mpage, int fd) { FILE *stream; char *line, *p, *title; - size_t len, plen, titlesz; + size_t linesz, plen, titlesz; + ssize_t len; + int offs; stream = (-1 == fd) ? fopen(mpage->mlinks->file, "r") : @@ -1280,10 +1318,13 @@ parse_cat(struct mpage *mpage, int fd) return; } + line = NULL; + linesz = 0; + /* Skip to first blank line. */ - while (NULL != (line = fgetln(stream, &len))) - if ('\n' == *line) + while (getline(&line, &linesz, stream) != -1) + if (*line == '\n') break; /* @@ -1291,8 +1332,8 @@ parse_cat(struct mpage *mpage, int fd) * is the first section header. Skip to it. */ - while (NULL != (line = fgetln(stream, &len))) - if ('\n' != *line && ' ' != *line) + while (getline(&line, &linesz, stream) != -1) + if (*line != '\n' && *line != ' ') break; /* @@ -1305,20 +1346,20 @@ parse_cat(struct mpage *mpage, int fd) titlesz = 0; title = NULL; - while (NULL != (line = fgetln(stream, &len))) { - if (' ' != *line || '\n' != line[len - 1]) + while ((len = getline(&line, &linesz, stream)) != -1) { + if (*line != ' ') break; - while (len > 0 && isspace((unsigned char)*line)) { - line++; - len--; - } - if (1 == len) + offs = 0; + while (isspace((unsigned char)line[offs])) + offs++; + if (line[offs] == '\0') continue; - title = mandoc_realloc(title, titlesz + len); - memcpy(title + titlesz, line, len); - titlesz += len; + title = mandoc_realloc(title, titlesz + len - offs); + memcpy(title + titlesz, line + offs, len - offs); + titlesz += len - offs; title[titlesz - 1] = ' '; } + free(line); /* * If no page content can be found, or the input line @@ -1336,8 +1377,7 @@ parse_cat(struct mpage *mpage, int fd) return; } - title = mandoc_realloc(title, titlesz + 1); - title[titlesz] = '\0'; + title[titlesz - 1] = '\0'; /* * Skip to the first dash. @@ -1415,7 +1455,7 @@ parse_man(struct mpage *mpage, const struct roff_meta char byte; size_t sz; - if (NULL == n) + if (n == NULL) return; /* @@ -1427,13 +1467,12 @@ parse_man(struct mpage *mpage, const struct roff_meta if (n->type == ROFFT_BODY && n->tok == MAN_SH) { body = n; - assert(body->parent); - if (NULL != (head = body->parent->head) && - 1 == head->nchild && - NULL != (head = (head->child)) && + if ((head = body->parent->head) != NULL && + (head = head->child) != NULL && + head->next == NULL && head->type == ROFFT_TEXT && - 0 == strcmp(head->string, "NAME") && - NULL != body->child) { + strcmp(head->string, "NAME") == 0 && + body->child != NULL) { /* * Suck the entire NAME section into memory. @@ -1658,6 +1697,31 @@ parse_mdoc_Fo(struct mpage *mpage, const struct roff_m } 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, const struct roff_node *n) { @@ -1723,14 +1787,6 @@ parse_mdoc_head(struct mpage *mpage, const struct roff { 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; } /*