Module Name: src Committed By: abhinav Date: Sun Apr 23 13:52:57 UTC 2017
Modified Files: src/usr.sbin/makemandb: apropos-utils.c Log Message: Better handle MLINKS in apropos(1). apropos(1) only indexes the first .Nm entry from the NAME section in the full text index. Rest of the .Nm entries are stored in a separate table: mandb_links. Till now apropos(1) did not use the mandb_links table. So whenever a query was being made for one of the man page links, such as realloc(3), it was showing malloc(3) in the results but not as the first result. And, also the result would show up as malloc(3), rather than realloc(3) (which can be confusing). With this change, for single keyword queries, apropos(1) would now utilise the mandb_links table as well. If the query is for one of the links of a man page, it would show as the first result. Also, the result would show up as the name of the link rather than the original man page name. For example, if the query was for realloc, the output would be realloc(3), rather than malloc(3). Following are some example queries showing difference in the output before this change and after this change: #Before changes $ apropos -n 5 -M realloc reallocarr (3) reallocate array reallocarray (3) reallocate memory for an array of elements checking for overflow fgetwln (3) get a line of wide characters from a stream fgetln (3) get a line from a stream posix_memalign (3) aligned memory allocation #After changes $ ./apropos -n 5 -M realloc realloc (3) general memory allocation operations realloc (3) general purpose memory allocation functions realloc (9) general-purpose kernel memory allocator reallocarr (3) reallocate array reallocarray (3) reallocate memory for an array of elements checking for overflow #Before changes $ apropos -n 5 -M TAILQ_REMOVE SLIST_HEAD (3) implementations of singly-linked lists, lists, simple queues, tail queues, and singly-linked tail queues #After changes $ ./apropos -n 5 -M TAILQ_REMOVE TAILQ_REMOVE (3) implementations of singly-linked lists, lists, simple queues, tail queues, and singly-linked tail queues #Before changes $ apropos -n 5 -M falloc filedesc (9) file descriptor tables and operations file (9) operations on file entries #After changes $ ./apropos -n 5 -M falloc falloc (9) file descriptor tables and operations file (9) operations on file entries ok christos@ To generate a diff of this commit: cvs rdiff -u -r1.30 -r1.31 src/usr.sbin/makemandb/apropos-utils.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.sbin/makemandb/apropos-utils.c diff -u src/usr.sbin/makemandb/apropos-utils.c:1.30 src/usr.sbin/makemandb/apropos-utils.c:1.31 --- src/usr.sbin/makemandb/apropos-utils.c:1.30 Tue Jan 10 04:34:07 2017 +++ src/usr.sbin/makemandb/apropos-utils.c Sun Apr 23 13:52:57 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: apropos-utils.c,v 1.30 2017/01/10 04:34:07 kamil Exp $ */ +/* $NetBSD: apropos-utils.c,v 1.31 2017/04/23 13:52:57 abhinav Exp $ */ /*- * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com> * All rights reserved. @@ -31,7 +31,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: apropos-utils.c,v 1.30 2017/01/10 04:34:07 kamil Exp $"); +__RCSID("$NetBSD: apropos-utils.c,v 1.31 2017/04/23 13:52:57 abhinav Exp $"); #include <sys/queue.h> #include <sys/stat.h> @@ -182,7 +182,7 @@ create_db(sqlite3 *db) "CREATE TABLE IF NOT EXISTS mandb_meta(device, inode, mtime, " "file UNIQUE, md5_hash UNIQUE, id INTEGER PRIMARY KEY); " //mandb_links - "CREATE TABLE IF NOT EXISTS mandb_links(link, target, section, " + "CREATE TABLE IF NOT EXISTS mandb_links(link COLLATE NOCASE, target, section, " "machine, md5_hash); "; sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg); @@ -504,7 +504,7 @@ generate_search_query(query_args *args, char *query; if (args->machine) - easprintf(&machine_clause, "AND machine = \'%s\' ", + easprintf(&machine_clause, "AND mandb.machine = \'%s\' ", args->machine); @@ -547,7 +547,7 @@ generate_search_query(query_args *args, if (section_clause[section_clause_len - 1] == ',') section_clause[section_clause_len - 1] = 0; temp = section_clause; - easprintf(§ion_clause, " AND section IN (%s)", temp); + easprintf(§ion_clause, " AND mandb.section IN (%s)", temp); free(temp); } } @@ -577,6 +577,35 @@ generate_search_query(query_args *args, section_clause ? section_clause : "", limit_clause ? limit_clause : ""); free(wild); + } else if (strchr(args->search_str, ' ') == NULL) { + /* + * If it's a single word query, we want to search in the + * links table as well. If the link table contains an entry + * for the queried keyword, we want to use that as the name of + * the man page. + * For example, for `apropos realloc` the output should be + * realloc(3) and not malloc(3). + */ + query = sqlite3_mprintf( + "SELECT section, name, name_desc, machine," + " snippet(mandb, %Q, %Q, %Q, -1, 40 )," + " rank_func(matchinfo(mandb, \"pclxn\")) AS rank" + " FROM mandb WHERE name NOT IN (" + " SELECT target FROM mandb_links WHERE link=%Q AND" + " mandb_links.section=mandb.section) AND mandb MATCH %Q %s %s" + " UNION" + " SELECT mandb.section, mandb_links.link AS name, mandb.name_desc," + " mandb.machine, '' AS snippet, 100.00 AS rank" + " FROM mandb JOIN mandb_links ON mandb.name=mandb_links.target and" + " mandb.section=mandb_links.section WHERE mandb_links.link=%Q" + " %s %s" + " ORDER BY rank DESC %s", + snippet_args[0], snippet_args[1], snippet_args[2], + args->search_str, args->search_str, section_clause ? section_clause : "", + machine_clause ? machine_clause : "", args->search_str, + machine_clause ? machine_clause : "", + section_clause ? section_clause : "", + limit_clause ? limit_clause : ""); } else { query = sqlite3_mprintf("SELECT section, name, name_desc, machine," " snippet(mandb, %Q, %Q, %Q, -1, 40 ),"