=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mandoc/mandocdb.c,v retrieving revision 1.179 retrieving revision 1.180 diff -c -r1.179 -r1.180 *** src/usr.bin/mandoc/mandocdb.c 2016/09/02 14:03:24 1.179 --- src/usr.bin/mandoc/mandocdb.c 2016/10/18 14:13:46 1.180 *************** *** 1,7 **** ! /* $OpenBSD: mandocdb.c,v 1.179 2016/09/02 14:03:24 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * 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 --- 1,8 ---- ! /* $OpenBSD: mandocdb.c,v 1.180 2016/10/18 14:13:46 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011-2016 Ingo Schwarze + * Copyright (c) 2016 Ed Maste * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above *************** *** 73,78 **** --- 74,80 ---- char *arch; /* architecture from file content */ char *title; /* title from file content */ char *desc; /* description from file content */ + struct mpage *next; /* singly linked list */ struct mlink *mlinks; /* singly linked list */ int name_head_done; enum form form; /* format from file content */ *************** *** 107,117 **** static void dbprune(struct dba *); static void dbwrite(struct dba *); static void filescan(const char *); static void mlink_add(struct mlink *, const struct stat *); 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); --- 109,119 ---- static void dbprune(struct dba *); static void dbwrite(struct dba *); static void filescan(const char *); + static int fts_compare(const FTSENT **, const FTSENT **); static void mlink_add(struct mlink *, const struct stat *); static void mlink_check(struct mpage *, struct mlink *); static void mlink_free(struct mlink *); static void mlinks_undupe(struct mpage *); static void mpages_free(void); static void mpages_merge(struct dba *, struct mparse *); static void parse_cat(struct mpage *, int); *************** *** 158,163 **** --- 160,166 ---- static int exitcode; /* to be returned by main */ static enum op op; /* operational mode */ static char basedir[PATH_MAX]; /* current base directory */ + static struct mpage *mpage_head; /* list of distinct manual pages */ static struct ohash mpages; /* table of distinct manual pages */ static struct ohash mlinks; /* table of directory entries */ static struct ohash names; /* table of all names */ *************** *** 517,522 **** --- 520,535 ---- } /* + * To get a singly linked list in alpha order while inserting entries + * at the beginning, process directory entries in reverse alpha order. + */ + static int + fts_compare(const FTSENT **a, const FTSENT **b) + { + return -strcmp((*a)->fts_name, (*b)->fts_name); + } + + /* * Scan a directory tree rooted at "basedir" for manpages. * We use fts(), scanning directory parts along the way for clues to our * section and architecture. *************** *** 546,553 **** argv[0] = "."; argv[1] = (char *)NULL; ! f = fts_open((char * const *)argv, ! FTS_PHYSICAL | FTS_NOCHDIR, NULL); if (f == NULL) { exitcode = (int)MANDOCLEVEL_SYSERR; say("", "&fts_open"); --- 559,566 ---- argv[0] = "."; argv[1] = (char *)NULL; ! f = fts_open((char * const *)argv, FTS_PHYSICAL | FTS_NOCHDIR, ! fts_compare); if (f == NULL) { exitcode = (int)MANDOCLEVEL_SYSERR; say("", "&fts_open"); *************** *** 905,910 **** --- 918,925 ---- mpage->inodev.st_ino = inodev.st_ino; mpage->inodev.st_dev = inodev.st_dev; mpage->form = FORM_NONE; + mpage->next = mpage_head; + mpage_head = mpage; ohash_insert(&mpages, slot, mpage); } else mlink->next = mpage->mlinks; *************** *** 928,947 **** { struct mpage *mpage; struct mlink *mlink; - unsigned int slot; ! mpage = ohash_first(&mpages, &slot); ! while (NULL != mpage) { ! while (NULL != (mlink = mpage->mlinks)) { mpage->mlinks = mlink->next; mlink_free(mlink); } free(mpage->sec); free(mpage->arch); free(mpage->title); free(mpage->desc); free(mpage); - mpage = ohash_next(&mpages, &slot); } } --- 943,960 ---- { struct mpage *mpage; struct mlink *mlink; ! while ((mpage = mpage_head) != NULL) { ! while ((mlink = mpage->mlinks) != NULL) { mpage->mlinks = mlink->next; mlink_free(mlink); } + mpage_head = mpage->next; free(mpage->sec); free(mpage->arch); free(mpage->title); free(mpage->desc); free(mpage); } } *************** *** 1055,1084 **** static void mpages_merge(struct dba *dba, struct mparse *mp) { ! struct mpage **mplist, *mpage, *mpage_dest; struct mlink *mlink, *mlink_dest; struct roff_man *man; char *sodest; char *cp; int fd; - 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 (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)); --- 1068,1085 ---- static void mpages_merge(struct dba *dba, struct mparse *mp) { ! struct mpage *mpage, *mpage_dest; struct mlink *mlink, *mlink_dest; struct roff_man *man; char *sodest; char *cp; int fd; ! for (mpage = mpage_head; mpage != NULL; mpage = mpage->next) { mlinks_undupe(mpage); ! if ((mlink = mpage->mlinks) == NULL) ! continue; name_mask = NAME_MASK; mandoc_ohash_init(&names, 4, offsetof(struct str, key)); mandoc_ohash_init(&strings, 6, offsetof(struct str, key)); *************** *** 1187,1203 **** ohash_delete(&strings); ohash_delete(&names); } - 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 --- 1188,1193 ----