=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.242 retrieving revision 1.252 diff -u -p -r1.242 -r1.252 --- mandoc/mandocdb.c 2017/01/27 11:33:26 1.242 +++ mandoc/mandocdb.c 2017/07/15 13:17:49 1.252 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.242 2017/01/27 11:33:26 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.252 2017/07/15 13:17:49 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2017 Ingo Schwarze @@ -162,7 +162,7 @@ static void putmdockey(const struct mpage *, const struct roff_node *, uint64_t, int); static int render_string(char **, size_t *); static void say(const char *, const char *, ...) - __attribute__((__format__ (printf, 2, 3))); + __attribute__((__format__ (__printf__, 2, 3))); static int set_basedir(const char *, int); static int treescan(void); static size_t utf8(unsigned int, char [7]); @@ -183,8 +183,7 @@ static struct ohash names; /* table of all names */ static struct ohash strings; /* table of all strings */ static uint64_t name_mask; -static const struct mdoc_handler mdocs[MDOC_MAX] = { - { NULL, 0, 0 }, /* Ap */ +static const struct mdoc_handler __mdocs[MDOC_MAX - MDOC_Dd] = { { NULL, 0, NODE_NOPRT }, /* Dd */ { NULL, 0, NODE_NOPRT }, /* Dt */ { NULL, 0, NODE_NOPRT }, /* Os */ @@ -200,6 +199,7 @@ static const struct mdoc_handler mdocs[MDOC_MAX] = { { NULL, 0, 0 }, /* It */ { NULL, 0, 0 }, /* Ad */ { NULL, TYPE_An, 0 }, /* An */ + { NULL, 0, 0 }, /* Ap */ { NULL, TYPE_Ar, 0 }, /* Ar */ { NULL, TYPE_Cd, 0 }, /* Cd */ { NULL, TYPE_Cm, 0 }, /* Cm */ @@ -302,12 +302,10 @@ static const struct mdoc_handler mdocs[MDOC_MAX] = { { NULL, 0, 0 }, /* En */ { NULL, TYPE_Dx, NODE_NOSRC }, /* Dx */ { NULL, 0, 0 }, /* %Q */ - { NULL, 0, 0 }, /* br */ - { NULL, 0, 0 }, /* sp */ { NULL, 0, 0 }, /* %U */ { NULL, 0, 0 }, /* Ta */ - { NULL, 0, 0 }, /* ll */ }; +static const struct mdoc_handler *const mdocs = __mdocs - MDOC_Dd; int @@ -422,7 +420,8 @@ mandocdb(int argc, char *argv[]) exitcode = (int)MANDOCLEVEL_OK; mchars_alloc(); - mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, NULL); + mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, + MANDOC_OS_OTHER, NULL); mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev)); mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file)); @@ -589,7 +588,7 @@ treescan(void) const char *argv[2]; argv[0] = "."; - argv[1] = (char *)NULL; + argv[1] = NULL; f = fts_open((char * const *)argv, FTS_PHYSICAL | FTS_NOCHDIR, fts_compare); @@ -1211,7 +1210,7 @@ mpages_merge(struct dba *dba, struct mparse *mp) } else if (man != NULL && man->macroset == MACROSET_MAN) { man_validate(man); if (*man->meta.msec != '\0' || - *man->meta.msec != '\0') { + *man->meta.title != '\0') { mpage->form = FORM_SRC; mpage->sec = mandoc_strdup(man->meta.msec); mpage->arch = mandoc_strdup(mlink->arch); @@ -1545,25 +1544,26 @@ parse_mdoc(struct mpage *mpage, const struct roff_meta const struct roff_node *n) { - assert(NULL != n); - for (n = n->child; NULL != n; n = n->next) { - if (n->flags & mdocs[n->tok].taboo) + for (n = n->child; n != NULL; n = n->next) { + if (n->tok == TOKEN_NONE || + n->tok < ROFF_MAX || + n->flags & mdocs[n->tok].taboo) continue; + assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX); switch (n->type) { case ROFFT_ELEM: case ROFFT_BLOCK: case ROFFT_HEAD: case ROFFT_BODY: case ROFFT_TAIL: - if (NULL != mdocs[n->tok].fp) - if (0 == (*mdocs[n->tok].fp)(mpage, meta, n)) - break; + if (mdocs[n->tok].fp != NULL && + (*mdocs[n->tok].fp)(mpage, meta, n) == 0) + break; if (mdocs[n->tok].mask) putmdockey(mpage, n->child, mdocs[n->tok].mask, mdocs[n->tok].taboo); break; default: - assert(n->type != ROFFT_ROOT); continue; } if (NULL != n->child) @@ -2123,6 +2123,23 @@ dbwrite(struct dba *dba) int status; pid_t child; + /* + * Do not write empty databases, and delete existing ones + * when makewhatis -u causes them to become empty. + */ + + dba_array_start(dba->pages); + if (dba_array_next(dba->pages) == NULL) { + if (unlink(MANDOC_DB) == -1 && errno != ENOENT) + say(MANDOC_DB, "&unlink"); + return; + } + + /* + * Build the database in a temporary file, + * then atomically move it into place. + */ + if (dba_write(MANDOC_DB "~", dba) != -1) { if (rename(MANDOC_DB "~", MANDOC_DB) == -1) { exitcode = (int)MANDOCLEVEL_SYSERR; @@ -2131,6 +2148,11 @@ dbwrite(struct dba *dba) } return; } + + /* + * We lack write permission and cannot replace the database + * file, but let's at least check whether the data changed. + */ (void)strlcpy(tfn, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn)); if (mkdtemp(tfn) == NULL) {