From 923fed8aa9a44ce109722816b3ea0749ba63cf56 Mon Sep 17 00:00:00 2001 From: schwarze Date: Thu, 24 Jul 2014 20:30:38 +0000 Subject: [PATCH] Sort result pages first by section number, then by name. By moving the sort from cgi.c to mansearch.c, we get two advantages: Easier access to the data needed for sorting, in particular the section number, and the apropos(1) command line utility profits as well. Feature requested by deraadt@. --- usr.bin/mandoc/cgi.c | 13 +------------ usr.bin/mandoc/mansearch.c | 22 +++++++++++++++++++++- usr.bin/mandoc/mansearch.h | 3 ++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/usr.bin/mandoc/cgi.c b/usr.bin/mandoc/cgi.c index bc474a6b930..f0168e841c2 100644 --- a/usr.bin/mandoc/cgi.c +++ b/usr.bin/mandoc/cgi.c @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.21 2014/07/24 08:25:45 schwarze Exp $ */ +/* $Id: cgi.c,v 1.22 2014/07/24 20:30:38 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -49,7 +49,6 @@ struct req { }; static void catman(const struct req *, const char *); -static int cmp(const void *, const void *); static void format(const struct req *, const char *); static void html_print(const char *); static void html_printquery(const struct req *); @@ -589,8 +588,6 @@ pg_searchres(const struct req *req, struct manpage *r, size_t sz) return; } - qsort(r, sz, sizeof(struct manpage), cmp); - resp_begin_html(200, NULL); resp_searchform(req); puts("
"); @@ -1055,14 +1052,6 @@ main(void) return(EXIT_SUCCESS); } -static int -cmp(const void *p1, const void *p2) -{ - - return(strcasecmp(((const struct manpage *)p1)->names, - ((const struct manpage *)p2)->names)); -} - /* * Scan for indexable paths. */ diff --git a/usr.bin/mandoc/mansearch.c b/usr.bin/mandoc/mansearch.c index 443c3a812f9..d58813531bc 100644 --- a/usr.bin/mandoc/mansearch.c +++ b/usr.bin/mandoc/mansearch.c @@ -1,4 +1,4 @@ -/* $Id: mansearch.c,v 1.28 2014/07/12 13:59:54 schwarze Exp $ */ +/* $Id: mansearch.c,v 1.29 2014/07/24 20:30:38 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014 Ingo Schwarze @@ -86,6 +86,7 @@ static void exprfree(struct expr *); static struct expr *exprspec(struct expr *, uint64_t, const char *, const char *); static struct expr *exprterm(const struct mansearch *, char *, int); +static int manpage_compare(const void *, const void *); static void sql_append(char **sql, size_t *sz, const char *newstr, int count); static void sql_match(sqlite3_context *context, @@ -323,6 +324,7 @@ mansearch(const struct mansearch *search, maxres, sizeof(struct manpage)); } mpage = *res + cur; + mpage->sec = 10; mpage->form = mp->form; buildnames(mpage, db, s, mp->pageid, paths->paths[i], mp->form); @@ -339,6 +341,7 @@ mansearch(const struct mansearch *search, sqlite3_close(db); ohash_delete(&htab); } + qsort(*res, cur, sizeof(struct manpage), manpage_compare); rc = 1; out: if (-1 != fd) { @@ -352,6 +355,18 @@ out: return(rc); } +static int +manpage_compare(const void *vp1, const void *vp2) +{ + const struct manpage *mp1, *mp2; + int diff; + + mp1 = vp1; + mp2 = vp2; + diff = mp1->sec - mp2->sec; + return(diff ? diff : strcasecmp(mp1->names, mp2->names)); +} + static void buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, const char *path, int form) @@ -384,6 +399,11 @@ buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s, arch = sqlite3_column_text(s, 1); name = sqlite3_column_text(s, 2); + /* Remember the first section found. */ + + if (9 < mpage->sec && '1' <= *sec && '9' >= *sec) + mpage->sec = (*sec - '1') + 1; + /* If the section changed, append the old one. */ if (NULL != prevsec && diff --git a/usr.bin/mandoc/mansearch.h b/usr.bin/mandoc/mansearch.h index 4eee09c202d..18d76362db0 100644 --- a/usr.bin/mandoc/mansearch.h +++ b/usr.bin/mandoc/mansearch.h @@ -1,4 +1,4 @@ -/* $Id: mansearch.h,v 1.10 2014/07/12 13:59:54 schwarze Exp $ */ +/* $Id: mansearch.h,v 1.11 2014/07/24 20:30:38 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014 Ingo Schwarze @@ -74,6 +74,7 @@ struct manpage { char *file; /* to be prefixed by manpath */ char *names; /* a list of names with sections */ char *output; /* user-defined additional output */ + int sec; /* section number, 10 means invalid */ int form; /* 0 == catpage */ }; -- 2.20.1