source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Sort result pages first by section number, then by name.
@ 2014-07-24 20:30 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2014-07-24 20:30 UTC (permalink / raw)
  To: source

Log Message:
-----------
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@.

Modified Files:
--------------
    mdocml:
        cgi.c
        mansearch.c
        mansearch.h

Revision Data
-------------
Index: mansearch.h
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mansearch.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -Lmansearch.h -Lmansearch.h -u -p -r1.14 -r1.15
--- mansearch.h
+++ mansearch.h
@@ -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 */
 };
 
Index: mansearch.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mansearch.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -Lmansearch.c -Lmansearch.c -u -p -r1.38 -r1.39
--- mansearch.c
+++ mansearch.c
@@ -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)
@@ -391,6 +406,11 @@ buildnames(struct manpage *mpage, sqlite
 		sec = sqlite3_column_text(s, 0);
 		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. */
 
Index: cgi.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/cgi.c,v
retrieving revision 1.81
retrieving revision 1.82
diff -Lcgi.c -Lcgi.c -u -p -r1.81 -r1.82
--- cgi.c
+++ cgi.c
@@ -53,7 +53,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 *);
@@ -593,8 +592,6 @@ pg_searchres(const struct req *req, stru
 		return;
 	}
 
-	qsort(r, sz, sizeof(struct manpage), cmp);
-
 	resp_begin_html(200, NULL);
 	resp_searchform(req);
 	puts("<DIV CLASS=\"results\">");
@@ -1057,14 +1054,6 @@ main(void)
 		free(req.p[i]);
 	free(req.p);
 	return(EXIT_SUCCESS);
-}
-
-static int
-cmp(const void *p1, const void *p2)
-{
-
-	return(strcasecmp(((const struct manpage *)p1)->names,
-	    ((const struct manpage *)p2)->names));
 }
 
 /*
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-07-24 20:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-24 20:30 mdocml: Sort result pages first by section number, then by name schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).