1 .\" $Id: mansearch.3,v 1.1 2014/04/15 20:18:26 schwarze Exp $
3 .\" Copyright (c) 2014 Ingo Schwarze <schwarze@openbsd.org>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 .Dd $Mdocdate: April 15 2014 $
23 .Nd search manual page databases
33 .Fa "const struct mansearch *search"
34 .Fa "const struct manpaths *paths"
37 .Fa "const char *outkey"
38 .Fa "struct manpage **res"
44 function returns information about manuals matching a search query from a
48 The query arguments are as follows:
50 .It Fa "const struct mansearch *search"
51 Search options, defined in
53 .It Fa "const struct manpaths *paths"
54 Directories to be searched, defined in
56 .It Fa "int argc" , "char *argv[]"
57 Search criteria, usually taken from the command line.
61 .Fa "const char *outkey"
62 selects which data to return in the
67 It takes any of the macro keys defined in
72 The output arguments are as follows:
74 .It Fa "struct manpage **res"
75 Returns a pointer to an array of result structures defined in
77 The user is expected to call
84 fields of all structures, as well as the
88 Returns the number of result structures contained in
92 To speed up searches, the
94 function can optionally be called with a
98 to set up an SQLite3 pagecache.
99 If it was called, it has to be called again with a
101 argument of 0 after the last call to
103 to release the memory used for the pagecache.
104 .Sh IMPLEMENTATION NOTES
105 For each manual page tree, the search is done in two steps.
106 In the first step, a list of pages matching the search criteria is built.
107 In the second step, the requested information about these pages is
108 retrieved from the database and assembled into the
112 All function mentioned here are defined in the file
118 build any SQL code, and no functions except
125 The query is built using the following grammar:
126 .Bd -literal -offset indent
127 <query> ::= "SELECT * FROM mpages WHERE" <condition>
128 <condition> ::= "(" <condition> ")" |
129 <condition> "OR" <condition> |
130 <condition> "AND" <condition> |
131 "desc" <operator> "?" |
132 "id IN (SELECT pageid FROM" <subquery> ")"
133 <subquery> ::= "names WHERE name" <operator> "?" |
134 "keys WHERE key" <operator> "? AND bits & ?"
135 <operator> ::= "MATCH" | "REGEXP"
138 The MATCH and REGEXP operators are implemented by the functions
143 This is required because SQLite3 natively neither supports
144 case-insensitive substring matching nor regular expression matching,
145 but only string identity, shell globbing, and the weird home-brewed
148 Command line parsing is done by the function
150 building a singly linked list of
152 structures, using the helper functions
156 The resulting SQL statement is assembled by the function
158 and evaluated in the main loop of the
161 .Ss Assembling the results
162 The names, sections, and architectures of the manuals found
163 are assembled into the
165 field of the result structure by the function
167 using the following query:
169 .Dl "SELECT * FROM mlinks WHERE pageid=? ORDER BY sec, arch, name"
175 the requested output data is assembled into the
177 field of the result structure by the function
179 using the following query:
181 .Dl "SELECT * FROM keys WHERE pageid=? AND bits & ?"
183 .Bl -tag -width mandoc.db -compact
185 The manual page database.
188 The simplest invocation
192 results in the following SQL query:
194 SELECT * FROM mpages WHERE (
195 id IN (SELECT pageid FROM names WHERE name MATCH 'keyword') OR
200 A more complicated request like
202 .Dl apropos -s 2 Nm,Xr=getuid
206 SELECT * FROM mpages WHERE (
207 id IN (SELECT pageid FROM names WHERE name MATCH 'getuid') OR
208 id IN (SELECT pageid FROM keys WHERE key MATCH 'getuid' AND bits & 4)
209 ) AND id IN (SELECT pageid FROM keys WHERE key REGEXP '^2$' AND bits & 2);
218 subsystem first appeared in
222 A module to search manual page databases was first written by
223 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv
224 in 2011, at first using the Berkeley DB;
225 he rewrote it for SQLite3 in 2012.
226 The current version received major changes from
227 .An Ingo Schwarze Aq Mt schwarze@openbsd.org .