aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mansearch.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2014-07-24 20:30:45 +0000
committerIngo Schwarze <schwarze@openbsd.org>2014-07-24 20:30:45 +0000
commitf0ed82f19fa760aaaa0d02bcbbda9f34357649f2 (patch)
tree09519b3f84b2237ede119005fb5bacd9b9dc9373 /mansearch.c
parent896b390bda7935b1fbc5244e7655b49a2a089bf5 (diff)
downloadmandoc-f0ed82f19fa760aaaa0d02bcbbda9f34357649f2.tar.gz
mandoc-f0ed82f19fa760aaaa0d02bcbbda9f34357649f2.tar.zst
mandoc-f0ed82f19fa760aaaa0d02bcbbda9f34357649f2.zip
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@.
Diffstat (limited to 'mansearch.c')
-rw-r--r--mansearch.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/mansearch.c b/mansearch.c
index cf29eddc..259a1946 100644
--- a/mansearch.c
+++ b/mansearch.c
@@ -1,4 +1,4 @@
-/* $Id: mansearch.c,v 1.38 2014/07/12 14:00:25 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.39 2014/07/24 20:30:45 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -94,6 +94,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,
@@ -331,6 +332,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);
@@ -347,6 +349,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) {
@@ -360,6 +363,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)
@@ -392,6 +407,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 &&