/var/db/man.db
and process the results in a callback function.
int
run_query(sqlite3 *db, const char **snippet_args, query_args *args)
)
function prepares the user supplied query in a form suitable to be run
against
/var/db/man.db
and executes the query.
For each row obtained in the result set,
run_query()
will call the user supplied callback function, which should contain the
logic for processing the data thus obtained.
The
run_query()
function takes following arguments:
sqlite3 *db).
const char *snippet_argsNULL
value for it, in which case, a default value of
{"", "", "..."}
will be used.
The 3 members of the array specify the following values:
const char **snippet_args = {
"",
"",
"..."
};
query_args *argsquery_args
is a struct which is defined in
apropos-utils.h.
It has the following fields:
const char *search_strconst char **sec_numschar *
wherein each index element represents the
section number in which search should be performed.
The sections whose corresponding index element in this array is set to
NULL
are not searched.
If all the elements in the array are
NULL
then all the sections are searched.
const char *nrecint (*callback) (void *, int, char **, char **))
will return.
The interface of the callback function is described later in this section.
void *callback_datachar **errmsgerrmsg.
If no error occurs then
errmsg
will be set to
NULL.
In case
errmsg
is not
NULL,
then the caller should make sure to free it.
The interface of the callback function is
int
(*callback)(void *callback_data, int ncol, char **col_values, char **col_names).
Its arguments are:
void *callback_data |
)
results in a 5 column result set.
Those are as follows:
section |
)
function will return 0 and in case of an error -1 will be returned.
/var/db/man.dbapropos.c
uses
run_query().
#include
query_args args;
char *errmsg = NULL;
const char **sec_nums = {NULL, "2", "3", NULL, NULL, NULL, NULL, NULL, NULL};
args.search_str = argv[1];
args.sec_nums = sec_nums;
args.nrec = "10";
args.callback = &query_callback;
args.callback_data = NULL;
args.errmsg = &errmsg;
if (run_query(db, NULL, &args) < 0)
errx(EXIT_FAILURE, "%s", errmsg);
}
free(query);
free(errmsg);
static int
query_callback(void *data, int ncol, char **col_values, char **col_names)
{
char *section = col_values[0];
char *name = col_values[1];
char *snippet = col_values[2];
char *name_desc = col_values[3];
/* The user supplied data could be obtained as follows */
// my_data *buf = (my_data *) data;
fprintf(stdout, "%s(%s)%s\n%s\n\n", name, section, name_desc,
snippet);
return 0;
}
|
|
|