xref: /netbsd-src/usr.sbin/makemandb/run_query.3 (revision 9616dacfef448e70e3fbbd865bddf60d54b656c5)
1.\" $NetBSD: run_query.3,v 1.4 2016/05/24 18:06:42 abhinav Exp $
2.\"
3.\" Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
4.\" All rights reserved.
5.\"
6.\" This code was developed as part of Google's Summer of Code 2011 program.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\"
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in
16.\"    the documentation and/or other materials provided with the
17.\"    distribution.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd May 23, 2016
33.Dt RUN_QUERY 3
34.Os
35.Sh NAME
36.Nm run_query
37.Nd run a query against
38.Pa /var/db/man.db
39and process the results in a callback function.
40.Sh SYNOPSIS
41.In apropos-utils.h
42.Ft int
43.Fn run_query "sqlite3 *db" "query_format fmt" "query_args *args"
44.Sh DESCRIPTION
45The
46.Fn run_query
47function prepares the user supplied query in a form suitable to be run
48against
49.Pa /var/db/man.db
50and executes the query.
51For each row obtained in the result set,
52.Fn run_query
53will call the user supplied callback function, which should contain the
54logic for processing the data thus obtained.
55.Pp
56The
57.Fn run_query
58function takes following arguments:
59.Bl -tag -width 8n
60.It Fa sqlite3 *db
61Handle to the database connection which can be obtained by calling
62.Fn init_db .
63.It Fa query_format fmt
64An enum value
65indicating the output format.
66Currently you can specify following values:
67.Bl -hang -width compact
68.It Dv APROPOS_PAGER
69.It Dv APROPOS_TERM
70.It Dv APROPOS_HTML
71.It Dv APROPOS_NONE
72.El
73.It Fa query_args *args
74.Ft query_args
75is a struct which is defined in
76.Pa apropos-utils.h .
77It has the following fields:
78.Bl -tag -width 8n -offset indent
79.It Li const char *search_str
80This is the query as entered by the user.
81You may want to pre-process it to do sanitization etc.
82.It Li int *sec_nums
83This is an array of
84.Ft int
85wherein each index element represents the
86section number in which search should be performed.
87The sections whose corresponding index element in this array is set to
88.Dv NULL
89are not searched.
90If all the elements in the array are
91.Dv 0
92then all the sections are searched.
93Alternatively pass
94.Dv NULL
95as to search in all the sections.
96.It Li int nrec
97This specifies the number of matching rows to fetch from the database.
98Specifiy a negative value to fetch all the matching rows.
99.It Li int offset
100This specifies the offset within the result-set.
101Use it to specify the position
102from where to start processing the result-set.
103For example if the value of nrec is m and value of offset is n, then the first
104n records from the result-set will be omitted and rest m-n (or less) records will
105be available for processing inside the callback.
106Use a negative value if you don't wish to offset any records.
107.It Li int legacy
108If the output should be in legacy format (similary to classic apropos).
109.It Li const char *machine
110The machine architecture to which the searches should be restricted.
111Specify NULL if the search should not be restricted a particular machine architecture.
112.It Li int (*callback) (void *, const char *, const char *, const char *,\
113const char *, size_t)
114This is the callback function which will
115be called for each row retrieved from the database.
116The function should return a value of 0 on successful execution.
117A non-zero return value will indicate a failure and
118.Fn run_query
119will return.
120The interface of the callback function is described later in this section.
121.It Li void *callback_data
122Use this argument to pass any data to the callback function.
123It can be retrieved inside the callback function from its first argument.
124.It Li char **errmsg
125If an error occurs while fetching the data from the database,
126a human readable error message will be stored in
127.Fa errmsg .
128If no error occurs then
129.Fa errmsg
130will be set to
131.Dv NULL .
132In case
133.Fa errmsg
134is not
135.Dv NULL ,
136then the caller should make sure to free it.
137.El
138.El
139.Pp
140The interface of the callback function is
141.Ft int
142.Fn (*callback) "void *callback_data" "const char *section" "const char *name"\
143"const char *name_desc" "const char *snippet" "size_t snippet_length"
144.Pp
145Its arguments are:
146.Bl -column -offset indent "Function" "Argument Description"
147.It Li void *callback_data Ta This is the caller supplied data.
148.It Li const char *section Ta Ta \&It is the section number to which this man
149page belongs.
150.It Li const char *name Ta This is the name of the man page
151.It Li const char *name_desc Ta This is the one line description of this man
152page obtained from its NAME section.
153.It Li const char *snippet Ta This is a snippet of the matching text from this
154man page.
155.It Li size_t snippet_length Ta This is the length of the snippet.
156.El
157.Sh RETURN VALUES
158On successful execution the
159.Fn run_query
160function will return 0 and in case of an error \-1 will be returned.
161.Sh FILES
162.Bl -hang -width /var/db/man.db -compact
163.It Pa /var/db/man.db
164The Sqlite FTS database which contains an index of the manual pages.
165.El
166.Sh EXAMPLES
167Following is a code excerpt of how
168.Pa apropos.c
169uses
170.Fn run_query .
171.Bd -literal -offset indent
172#include <apropos-utils.h>
173query_args args;
174char *errmsg = NULL;
175int *sec_nums = {0, 1, 1, 0, 0, 0, 0, 0, 0};
176args.search_str = argv[1];
177args.sec_nums = sec_nums;
178args.nrec = 10;
179args.offset = -1;
180args.machine = NULL;
181args.callback = &query_callback;
182args.callback_data = NULL;
183args.errmsg = &errmsg;
184if (run_query(db, APROPOS_PAGER, &args) < 0)
185		errx(EXIT_FAILURE, "%s", errmsg);
186}
187
188free(query);
189free(errmsg);
190
191static int
192query_callback(void *data, const char *section, const char *name,
193	const char *name_desc, const char *snippet, size_t snippet_length )
194{
195	/* The user supplied data could be obtained as follows */
196	// my_data *buf = (my_data *) data;
197
198	fprintf(stdout, "%s(%s)\t%s\en%s\en\en", name, section, name_desc,
199		snippet);
200	return 0;
201}
202.Ed
203.Sh SEE ALSO
204.Xr apropos-utils 3 ,
205.Xr close_db 3 ,
206.Xr init_db 3
207.Sh AUTHORS
208.An Abhinav Upadhyay
209