=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.223 retrieving revision 1.230 diff -u -p -r1.223 -r1.230 --- mandoc/mandocdb.c 2016/07/29 15:23:57 1.223 +++ mandoc/mandocdb.c 2016/09/02 14:03:30 1.230 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.223 2016/07/29 15:23:57 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.230 2016/09/02 14:03:30 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2016 Ingo Schwarze @@ -122,6 +122,7 @@ static void mlink_add(struct mlink *, const struct st static void mlink_check(struct mpage *, struct mlink *); static void mlink_free(struct mlink *); static void mlinks_undupe(struct mpage *); +int mpages_compare(const void *, const void *); static void mpages_free(void); static void mpages_merge(struct dba *, struct mparse *); static void parse_cat(struct mpage *, int); @@ -426,7 +427,8 @@ mandocdb(int argc, char *argv[]) if (OP_TEST != op && 0 == set_basedir(path_arg, 1)) goto out; - if ((dba = dba_read(MANDOC_DB)) != NULL) { + dba = nodb ? dba_new(128) : dba_read(MANDOC_DB); + if (dba != NULL) { /* * The existing database is usable. Process * all files specified on the command-line. @@ -443,13 +445,13 @@ mandocdb(int argc, char *argv[]) use_all = 1; for (i = 0; i < argc; i++) filescan(argv[i]); - if (OP_TEST != op) + if (nodb == 0) dbprune(dba); } else { - /* - * Database missing or corrupt. - * Recreate from scratch. - */ + /* Database missing or corrupt. */ + if (op != OP_UPDATE || errno != ENOENT) + say(MANDOC_DB, "%s: Automatically recreating" + " from scratch", strerror(errno)); exitcode = (int)MANDOCLEVEL_OK; op = OP_DEFAULT; if (0 == treescan()) @@ -1047,7 +1049,7 @@ mlink_check(struct mpage *mpage, struct mlink *mlink) * architectures. * A few manuals are even shared across completely * different architectures, for example fdformat(1) - * on amd64, i386, sparc, and sparc64. + * on amd64, i386, and sparc64. */ if (strcasecmp(mpage->arch, mlink->arch)) @@ -1084,22 +1086,30 @@ mlink_check(struct mpage *mpage, struct mlink *mlink) static void mpages_merge(struct dba *dba, struct mparse *mp) { - struct mpage *mpage, *mpage_dest; + struct mpage **mplist, *mpage, *mpage_dest; struct mlink *mlink, *mlink_dest; struct roff_man *man; char *sodest; char *cp; int fd; - unsigned int pslot; + unsigned int ip, npages, pslot; + npages = ohash_entries(&mpages); + mplist = mandoc_reallocarray(NULL, npages, sizeof(*mplist)); + ip = 0; mpage = ohash_first(&mpages, &pslot); while (mpage != NULL) { mlinks_undupe(mpage); - if ((mlink = mpage->mlinks) == NULL) { - mpage = ohash_next(&mpages, &pslot); - continue; - } + if (mpage->mlinks != NULL) + mplist[ip++] = mpage; + mpage = ohash_next(&mpages, &pslot); + } + npages = ip; + qsort(mplist, npages, sizeof(*mplist), mpages_compare); + for (ip = 0; ip < npages; ip++) { + mpage = mplist[ip]; + mlink = mpage->mlinks; name_mask = NAME_MASK; mandoc_ohash_init(&names, 4, offsetof(struct str, key)); mandoc_ohash_init(&strings, 6, offsetof(struct str, key)); @@ -1207,10 +1217,20 @@ mpages_merge(struct dba *dba, struct mparse *mp) nextpage: ohash_delete(&strings); ohash_delete(&names); - mpage = ohash_next(&mpages, &pslot); } + free(mplist); } +int +mpages_compare(const void *vp1, const void *vp2) +{ + const struct mpage *mp1, *mp2; + + mp1 = *(const struct mpage **)vp1; + mp2 = *(const struct mpage **)vp2; + return strcmp(mp1->mlinks->file, mp2->mlinks->file); +} + static void parse_cat(struct mpage *mpage, int fd) { @@ -1673,7 +1693,7 @@ parse_mdoc_Nm(struct mpage *mpage, const struct roff_m if ( ! (mpage->name_head_done || n->child == NULL || n->child->string == NULL || strcasecmp(n->child->string, meta->title))) { - putkey(mpage, n->child->string, ROFFT_HEAD); + putkey(mpage, n->child->string, NAME_HEAD); mpage->name_head_done = 1; } return 0; @@ -1993,11 +2013,12 @@ dbadd(struct dba *dba, struct mpage *mpage) cp = mpage->desc; i = strlen(cp); mustfree = render_string(&cp, &i); - mpage->dba = dba_page_new(dba->pages, mlink->name, mpage->sec, + mpage->dba = dba_page_new(dba->pages, *mpage->arch == '\0' ? mlink->arch : mpage->arch, cp, mlink->file, mpage->form); if (mustfree) free(cp); + dba_page_add(mpage->dba, DBP_SECT, mpage->sec); while (mlink != NULL) { dbadd_mlink(mlink);