1 .\" $Id: mansearch.3,v 1.2 2014/08/05 15:29:30 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: August 5 2014 $
23 .Nd search manual page databases
34 .Fa "const struct mansearch *search"
35 .Fa "const struct manpaths *paths"
38 .Fa "const char *outkey"
39 .Fa "struct manpage **res"
45 function returns information about manuals matching a search query from a
49 The query arguments are as follows:
51 .It Fa "const struct mansearch *search"
52 Search options, defined in
54 .It Fa "const struct manpaths *paths"
55 Directories to be searched, defined in
57 .It Fa "int argc" , "char *argv[]"
58 Search criteria, usually taken from the command line.
62 .Fa "const char *outkey"
63 selects which data to return in the
68 It takes any of the macro keys defined in
73 The output arguments are as follows:
75 .It Fa "struct manpage **res"
76 Returns a pointer to an array of result structures defined in
78 The user is expected to call
85 fields of all structures, as well as the
89 Returns the number of result structures contained in
93 To speed up searches, the
95 function can optionally be called with a
99 to set up an SQLite3 pagecache.
100 If it was called, it has to be called again with a
102 argument of 0 after the last call to
104 to release the memory used for the pagecache.
105 .Sh IMPLEMENTATION NOTES
106 For each manual page tree, the search is done in two steps.
107 In the first step, a list of pages matching the search criteria is built.
108 In the second step, the requested information about these pages is
109 retrieved from the database and assembled into the
113 All function mentioned here are defined in the file
119 build any SQL code, and no functions except
126 The query is built using the following grammar:
127 .Bd -literal -offset indent
128 <query> ::= "SELECT * FROM mpages WHERE" <condition>
129 <condition> ::= "(" <condition> ")" |
130 <condition> "OR" <condition> |
131 <condition> "AND" <condition> |
132 "desc" <operator> "?" |
133 "id IN (SELECT pageid FROM" <subquery> ")"
134 <subquery> ::= "names WHERE name" <operator> "?" |
135 "keys WHERE key" <operator> "? AND bits & ?"
136 <operator> ::= "MATCH" | "REGEXP"
139 The MATCH and REGEXP operators are implemented by the functions
144 This is required because SQLite3 natively neither supports
145 case-insensitive substring matching nor regular expression matching,
146 but only string identity, shell globbing, and the weird home-brewed
149 Command line parsing is done by the function
151 building a singly linked list of
153 structures, using the helper functions
157 The resulting SQL statement is assembled by the function
159 and evaluated in the main loop of the
162 .Ss Assembling the results
163 The names, sections, and architectures of the manuals found
164 are assembled into the
166 field of the result structure by the function
168 using the following query:
170 .Dl "SELECT * FROM mlinks WHERE pageid=? ORDER BY sec, arch, name"
176 the requested output data is assembled into the
178 field of the result structure by the function
180 using the following query:
182 .Dl "SELECT * FROM keys WHERE pageid=? AND bits & ?"
184 .Bl -tag -width mandoc.db -compact
186 The manual page database.
189 The simplest invocation
193 results in the following SQL query:
195 SELECT * FROM mpages WHERE (
196 id IN (SELECT pageid FROM names WHERE name MATCH 'keyword') OR
201 A more complicated request like
203 .Dl apropos -s 2 Nm,Xr=getuid
207 SELECT * FROM mpages WHERE (
208 id IN (SELECT pageid FROM names WHERE name MATCH 'getuid') OR
209 id IN (SELECT pageid FROM keys WHERE key MATCH 'getuid' AND bits & 4)
210 ) AND id IN (SELECT pageid FROM keys WHERE key REGEXP '^2$' AND bits & 2);
219 subsystem first appeared in
223 A module to search manual page databases was first written by
224 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv
225 in 2011, at first using the Berkeley DB;
226 he rewrote it for SQLite3 in 2012.
227 The current version received major changes from
228 .An Ingo Schwarze Aq Mt schwarze@openbsd.org .