apropos-utils.c revision 1.4.2.5 1 1.4.2.5 yamt /* $NetBSD: apropos-utils.c,v 1.4.2.5 2013/01/23 00:06:43 yamt Exp $ */
2 1.4.2.2 yamt /*-
3 1.4.2.2 yamt * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay (at) gmail.com>
4 1.4.2.2 yamt * All rights reserved.
5 1.4.2.2 yamt *
6 1.4.2.2 yamt * This code was developed as part of Google's Summer of Code 2011 program.
7 1.4.2.2 yamt *
8 1.4.2.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.4.2.2 yamt * modification, are permitted provided that the following conditions
10 1.4.2.2 yamt * are met:
11 1.4.2.2 yamt *
12 1.4.2.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.4.2.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.4.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.4.2.2 yamt * notice, this list of conditions and the following disclaimer in
16 1.4.2.2 yamt * the documentation and/or other materials provided with the
17 1.4.2.2 yamt * distribution.
18 1.4.2.2 yamt *
19 1.4.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.4.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.4.2.2 yamt * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 1.4.2.2 yamt * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 1.4.2.2 yamt * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.4.2.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 1.4.2.2 yamt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.4.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 1.4.2.2 yamt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 1.4.2.2 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 1.4.2.2 yamt * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.4.2.2 yamt * SUCH DAMAGE.
31 1.4.2.2 yamt */
32 1.4.2.2 yamt
33 1.4.2.2 yamt #include <sys/cdefs.h>
34 1.4.2.5 yamt __RCSID("$NetBSD: apropos-utils.c,v 1.4.2.5 2013/01/23 00:06:43 yamt Exp $");
35 1.4.2.2 yamt
36 1.4.2.4 yamt #include <sys/queue.h>
37 1.4.2.2 yamt #include <sys/stat.h>
38 1.4.2.2 yamt
39 1.4.2.2 yamt #include <assert.h>
40 1.4.2.2 yamt #include <ctype.h>
41 1.4.2.2 yamt #include <err.h>
42 1.4.2.2 yamt #include <math.h>
43 1.4.2.2 yamt #include <stdio.h>
44 1.4.2.2 yamt #include <stdlib.h>
45 1.4.2.2 yamt #include <string.h>
46 1.4.2.2 yamt #include <util.h>
47 1.4.2.2 yamt #include <zlib.h>
48 1.4.2.5 yamt #include <term.h>
49 1.4.2.5 yamt #undef tab // XXX: manconf.h
50 1.4.2.2 yamt
51 1.4.2.2 yamt #include "apropos-utils.h"
52 1.4.2.4 yamt #include "manconf.h"
53 1.4.2.5 yamt #include "dist/mandoc.h"
54 1.4.2.2 yamt #include "sqlite3.h"
55 1.4.2.2 yamt
56 1.4.2.2 yamt typedef struct orig_callback_data {
57 1.4.2.2 yamt void *data;
58 1.4.2.2 yamt int (*callback) (void *, const char *, const char *, const char *,
59 1.4.2.2 yamt const char *, size_t);
60 1.4.2.2 yamt } orig_callback_data;
61 1.4.2.2 yamt
62 1.4.2.2 yamt typedef struct inverse_document_frequency {
63 1.4.2.2 yamt double value;
64 1.4.2.2 yamt int status;
65 1.4.2.2 yamt } inverse_document_frequency;
66 1.4.2.2 yamt
67 1.4.2.2 yamt /* weights for individual columns */
68 1.4.2.2 yamt static const double col_weights[] = {
69 1.4.2.2 yamt 2.0, // NAME
70 1.4.2.2 yamt 2.00, // Name-description
71 1.4.2.2 yamt 0.55, // DESCRIPTION
72 1.4.2.2 yamt 0.10, // LIBRARY
73 1.4.2.2 yamt 0.001, //RETURN VALUES
74 1.4.2.2 yamt 0.20, //ENVIRONMENT
75 1.4.2.2 yamt 0.01, //FILES
76 1.4.2.2 yamt 0.001, //EXIT STATUS
77 1.4.2.2 yamt 2.00, //DIAGNOSTICS
78 1.4.2.2 yamt 0.05, //ERRORS
79 1.4.2.2 yamt 0.00, //md5_hash
80 1.4.2.2 yamt 1.00 //machine
81 1.4.2.2 yamt };
82 1.4.2.2 yamt
83 1.4.2.2 yamt /*
84 1.4.2.2 yamt * lower --
85 1.4.2.2 yamt * Converts the string str to lower case
86 1.4.2.2 yamt */
87 1.4.2.2 yamt char *
88 1.4.2.2 yamt lower(char *str)
89 1.4.2.2 yamt {
90 1.4.2.2 yamt assert(str);
91 1.4.2.2 yamt int i = 0;
92 1.4.2.2 yamt char c;
93 1.4.2.2 yamt while (str[i] != '\0') {
94 1.4.2.2 yamt c = tolower((unsigned char) str[i]);
95 1.4.2.2 yamt str[i++] = c;
96 1.4.2.2 yamt }
97 1.4.2.2 yamt return str;
98 1.4.2.2 yamt }
99 1.4.2.2 yamt
100 1.4.2.2 yamt /*
101 1.4.2.2 yamt * concat--
102 1.4.2.2 yamt * Utility function. Concatenates together: dst, a space character and src.
103 1.4.2.2 yamt * dst + " " + src
104 1.4.2.2 yamt */
105 1.4.2.2 yamt void
106 1.4.2.2 yamt concat(char **dst, const char *src)
107 1.4.2.2 yamt {
108 1.4.2.2 yamt concat2(dst, src, strlen(src));
109 1.4.2.2 yamt }
110 1.4.2.2 yamt
111 1.4.2.2 yamt void
112 1.4.2.2 yamt concat2(char **dst, const char *src, size_t srclen)
113 1.4.2.2 yamt {
114 1.4.2.2 yamt size_t total_len, dst_len;
115 1.4.2.2 yamt assert(src != NULL);
116 1.4.2.2 yamt
117 1.4.2.2 yamt /* If destination buffer dst is NULL, then simply strdup the source buffer */
118 1.4.2.2 yamt if (*dst == NULL) {
119 1.4.2.2 yamt *dst = estrdup(src);
120 1.4.2.2 yamt return;
121 1.4.2.2 yamt }
122 1.4.2.2 yamt
123 1.4.2.2 yamt dst_len = strlen(*dst);
124 1.4.2.2 yamt /*
125 1.4.2.2 yamt * NUL Byte and separator space
126 1.4.2.2 yamt */
127 1.4.2.2 yamt total_len = dst_len + srclen + 2;
128 1.4.2.2 yamt
129 1.4.2.2 yamt *dst = erealloc(*dst, total_len);
130 1.4.2.2 yamt
131 1.4.2.2 yamt /* Append a space at the end of dst */
132 1.4.2.2 yamt (*dst)[dst_len++] = ' ';
133 1.4.2.2 yamt
134 1.4.2.2 yamt /* Now, copy src at the end of dst */
135 1.4.2.2 yamt memcpy(*dst + dst_len, src, srclen + 1);
136 1.4.2.2 yamt }
137 1.4.2.2 yamt
138 1.4.2.2 yamt void
139 1.4.2.2 yamt close_db(sqlite3 *db)
140 1.4.2.2 yamt {
141 1.4.2.2 yamt sqlite3_close(db);
142 1.4.2.2 yamt sqlite3_shutdown();
143 1.4.2.2 yamt }
144 1.4.2.2 yamt
145 1.4.2.2 yamt /*
146 1.4.2.2 yamt * create_db --
147 1.4.2.2 yamt * Creates the database schema.
148 1.4.2.2 yamt */
149 1.4.2.2 yamt static int
150 1.4.2.2 yamt create_db(sqlite3 *db)
151 1.4.2.2 yamt {
152 1.4.2.2 yamt const char *sqlstr = NULL;
153 1.4.2.2 yamt char *schemasql;
154 1.4.2.2 yamt char *errmsg = NULL;
155 1.4.2.2 yamt
156 1.4.2.2 yamt /*------------------------ Create the tables------------------------------*/
157 1.4.2.2 yamt
158 1.4.2.2 yamt #if NOTYET
159 1.4.2.2 yamt sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, NULL);
160 1.4.2.2 yamt #else
161 1.4.2.2 yamt sqlite3_exec(db, "PRAGMA journal_mode = DELETE", NULL, NULL, NULL);
162 1.4.2.2 yamt #endif
163 1.4.2.2 yamt
164 1.4.2.2 yamt schemasql = sqlite3_mprintf("PRAGMA user_version = %d",
165 1.4.2.2 yamt APROPOS_SCHEMA_VERSION);
166 1.4.2.2 yamt sqlite3_exec(db, schemasql, NULL, NULL, &errmsg);
167 1.4.2.2 yamt if (errmsg != NULL)
168 1.4.2.2 yamt goto out;
169 1.4.2.2 yamt sqlite3_free(schemasql);
170 1.4.2.2 yamt
171 1.4.2.2 yamt sqlstr = "CREATE VIRTUAL TABLE mandb USING fts4(section, name, "
172 1.4.2.2 yamt "name_desc, desc, lib, return_vals, env, files, "
173 1.4.2.2 yamt "exit_status, diagnostics, errors, md5_hash UNIQUE, machine, "
174 1.4.2.2 yamt "compress=zip, uncompress=unzip, tokenize=porter); " //mandb
175 1.4.2.2 yamt "CREATE TABLE IF NOT EXISTS mandb_meta(device, inode, mtime, "
176 1.4.2.2 yamt "file UNIQUE, md5_hash UNIQUE, id INTEGER PRIMARY KEY); "
177 1.4.2.2 yamt //mandb_meta
178 1.4.2.2 yamt "CREATE TABLE IF NOT EXISTS mandb_links(link, target, section, "
179 1.4.2.3 yamt "machine, md5_hash); "; //mandb_links
180 1.4.2.2 yamt
181 1.4.2.2 yamt sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
182 1.4.2.2 yamt if (errmsg != NULL)
183 1.4.2.2 yamt goto out;
184 1.4.2.2 yamt
185 1.4.2.2 yamt sqlstr = "CREATE INDEX IF NOT EXISTS index_mandb_links ON mandb_links "
186 1.4.2.2 yamt "(link); "
187 1.4.2.2 yamt "CREATE INDEX IF NOT EXISTS index_mandb_meta_dev ON mandb_meta "
188 1.4.2.3 yamt "(device, inode); "
189 1.4.2.3 yamt "CREATE INDEX IF NOT EXISTS index_mandb_links_md5 ON mandb_links "
190 1.4.2.3 yamt "(md5_hash);";
191 1.4.2.2 yamt sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
192 1.4.2.2 yamt if (errmsg != NULL)
193 1.4.2.2 yamt goto out;
194 1.4.2.2 yamt return 0;
195 1.4.2.2 yamt
196 1.4.2.2 yamt out:
197 1.4.2.2 yamt warnx("%s", errmsg);
198 1.4.2.2 yamt free(errmsg);
199 1.4.2.2 yamt sqlite3_close(db);
200 1.4.2.2 yamt sqlite3_shutdown();
201 1.4.2.2 yamt return -1;
202 1.4.2.2 yamt }
203 1.4.2.2 yamt
204 1.4.2.2 yamt /*
205 1.4.2.2 yamt * zip --
206 1.4.2.2 yamt * User defined Sqlite function to compress the FTS table
207 1.4.2.2 yamt */
208 1.4.2.2 yamt static void
209 1.4.2.2 yamt zip(sqlite3_context *pctx, int nval, sqlite3_value **apval)
210 1.4.2.2 yamt {
211 1.4.2.2 yamt int nin;
212 1.4.2.2 yamt long int nout;
213 1.4.2.2 yamt const unsigned char * inbuf;
214 1.4.2.2 yamt unsigned char *outbuf;
215 1.4.2.2 yamt
216 1.4.2.2 yamt assert(nval == 1);
217 1.4.2.2 yamt nin = sqlite3_value_bytes(apval[0]);
218 1.4.2.2 yamt inbuf = (const unsigned char *) sqlite3_value_blob(apval[0]);
219 1.4.2.2 yamt nout = nin + 13 + (nin + 999) / 1000;
220 1.4.2.2 yamt outbuf = emalloc(nout);
221 1.4.2.2 yamt compress(outbuf, (unsigned long *) &nout, inbuf, nin);
222 1.4.2.2 yamt sqlite3_result_blob(pctx, outbuf, nout, free);
223 1.4.2.2 yamt }
224 1.4.2.2 yamt
225 1.4.2.2 yamt /*
226 1.4.2.2 yamt * unzip --
227 1.4.2.2 yamt * User defined Sqlite function to uncompress the FTS table.
228 1.4.2.2 yamt */
229 1.4.2.2 yamt static void
230 1.4.2.2 yamt unzip(sqlite3_context *pctx, int nval, sqlite3_value **apval)
231 1.4.2.2 yamt {
232 1.4.2.2 yamt unsigned int rc;
233 1.4.2.2 yamt unsigned char *outbuf;
234 1.4.2.2 yamt z_stream stream;
235 1.4.2.2 yamt
236 1.4.2.2 yamt assert(nval == 1);
237 1.4.2.2 yamt stream.next_in = __UNCONST(sqlite3_value_blob(apval[0]));
238 1.4.2.2 yamt stream.avail_in = sqlite3_value_bytes(apval[0]);
239 1.4.2.2 yamt stream.avail_out = stream.avail_in * 2 + 100;
240 1.4.2.2 yamt stream.next_out = outbuf = emalloc(stream.avail_out);
241 1.4.2.2 yamt stream.zalloc = NULL;
242 1.4.2.2 yamt stream.zfree = NULL;
243 1.4.2.2 yamt
244 1.4.2.2 yamt if (inflateInit(&stream) != Z_OK) {
245 1.4.2.2 yamt free(outbuf);
246 1.4.2.2 yamt return;
247 1.4.2.2 yamt }
248 1.4.2.2 yamt
249 1.4.2.2 yamt while ((rc = inflate(&stream, Z_SYNC_FLUSH)) != Z_STREAM_END) {
250 1.4.2.2 yamt if (rc != Z_OK ||
251 1.4.2.2 yamt (stream.avail_out != 0 && stream.avail_in == 0)) {
252 1.4.2.2 yamt free(outbuf);
253 1.4.2.2 yamt return;
254 1.4.2.2 yamt }
255 1.4.2.2 yamt outbuf = erealloc(outbuf, stream.total_out * 2);
256 1.4.2.2 yamt stream.next_out = outbuf + stream.total_out;
257 1.4.2.2 yamt stream.avail_out = stream.total_out;
258 1.4.2.2 yamt }
259 1.4.2.2 yamt if (inflateEnd(&stream) != Z_OK) {
260 1.4.2.2 yamt free(outbuf);
261 1.4.2.2 yamt return;
262 1.4.2.2 yamt }
263 1.4.2.2 yamt outbuf = erealloc(outbuf, stream.total_out);
264 1.4.2.2 yamt sqlite3_result_text(pctx, (const char *) outbuf, stream.total_out, free);
265 1.4.2.2 yamt }
266 1.4.2.2 yamt
267 1.4.2.4 yamt /*
268 1.4.2.4 yamt * get_dbpath --
269 1.4.2.4 yamt * Read the path of the database from man.conf and return.
270 1.4.2.4 yamt */
271 1.4.2.4 yamt char *
272 1.4.2.4 yamt get_dbpath(const char *manconf)
273 1.4.2.4 yamt {
274 1.4.2.4 yamt TAG *tp;
275 1.4.2.4 yamt char *dbpath;
276 1.4.2.4 yamt
277 1.4.2.4 yamt config(manconf);
278 1.4.2.4 yamt tp = gettag("_mandb", 1);
279 1.4.2.4 yamt if (!tp)
280 1.4.2.4 yamt return NULL;
281 1.4.2.4 yamt
282 1.4.2.4 yamt if (TAILQ_EMPTY(&tp->entrylist))
283 1.4.2.4 yamt return NULL;
284 1.4.2.4 yamt
285 1.4.2.4 yamt dbpath = TAILQ_LAST(&tp->entrylist, tqh)->s;
286 1.4.2.4 yamt return dbpath;
287 1.4.2.4 yamt }
288 1.4.2.4 yamt
289 1.4.2.2 yamt /* init_db --
290 1.4.2.2 yamt * Prepare the database. Register the compress/uncompress functions and the
291 1.4.2.2 yamt * stopword tokenizer.
292 1.4.2.2 yamt * db_flag specifies the mode in which to open the database. 3 options are
293 1.4.2.2 yamt * available:
294 1.4.2.2 yamt * 1. DB_READONLY: Open in READONLY mode. An error if db does not exist.
295 1.4.2.2 yamt * 2. DB_READWRITE: Open in read-write mode. An error if db does not exist.
296 1.4.2.2 yamt * 3. DB_CREATE: Open in read-write mode. It will try to create the db if
297 1.4.2.2 yamt * it does not exist already.
298 1.4.2.2 yamt * RETURN VALUES:
299 1.4.2.2 yamt * The function will return NULL in case the db does not exist and DB_CREATE
300 1.4.2.2 yamt * was not specified. And in case DB_CREATE was specified and yet NULL is
301 1.4.2.2 yamt * returned, then there was some other error.
302 1.4.2.2 yamt * In normal cases the function should return a handle to the db.
303 1.4.2.2 yamt */
304 1.4.2.2 yamt sqlite3 *
305 1.4.2.4 yamt init_db(int db_flag, const char *manconf)
306 1.4.2.2 yamt {
307 1.4.2.2 yamt sqlite3 *db = NULL;
308 1.4.2.2 yamt sqlite3_stmt *stmt;
309 1.4.2.2 yamt struct stat sb;
310 1.4.2.2 yamt int rc;
311 1.4.2.2 yamt int create_db_flag = 0;
312 1.4.2.2 yamt
313 1.4.2.4 yamt char *dbpath = get_dbpath(manconf);
314 1.4.2.4 yamt if (dbpath == NULL)
315 1.4.2.4 yamt errx(EXIT_FAILURE, "_mandb entry not found in man.conf");
316 1.4.2.2 yamt /* Check if the database exists or not */
317 1.4.2.4 yamt if (!(stat(dbpath, &sb) == 0 && S_ISREG(sb.st_mode))) {
318 1.4.2.2 yamt /* Database does not exist, check if DB_CREATE was specified, and set
319 1.4.2.2 yamt * flag to create the database schema
320 1.4.2.2 yamt */
321 1.4.2.2 yamt if (db_flag != (MANDB_CREATE)) {
322 1.4.2.2 yamt warnx("Missing apropos database. "
323 1.4.2.2 yamt "Please run makemandb to create it.");
324 1.4.2.2 yamt return NULL;
325 1.4.2.2 yamt }
326 1.4.2.2 yamt create_db_flag = 1;
327 1.4.2.2 yamt }
328 1.4.2.2 yamt
329 1.4.2.2 yamt /* Now initialize the database connection */
330 1.4.2.2 yamt sqlite3_initialize();
331 1.4.2.4 yamt rc = sqlite3_open_v2(dbpath, &db, db_flag, NULL);
332 1.4.2.2 yamt
333 1.4.2.2 yamt if (rc != SQLITE_OK) {
334 1.4.2.2 yamt warnx("%s", sqlite3_errmsg(db));
335 1.4.2.2 yamt sqlite3_shutdown();
336 1.4.2.2 yamt return NULL;
337 1.4.2.2 yamt }
338 1.4.2.2 yamt
339 1.4.2.2 yamt if (create_db_flag && create_db(db) < 0) {
340 1.4.2.2 yamt warnx("%s", "Unable to create database schema");
341 1.4.2.2 yamt goto error;
342 1.4.2.2 yamt }
343 1.4.2.2 yamt
344 1.4.2.2 yamt rc = sqlite3_prepare_v2(db, "PRAGMA user_version", -1, &stmt, NULL);
345 1.4.2.2 yamt if (rc != SQLITE_OK) {
346 1.4.2.2 yamt warnx("Unable to query schema version: %s",
347 1.4.2.2 yamt sqlite3_errmsg(db));
348 1.4.2.2 yamt goto error;
349 1.4.2.2 yamt }
350 1.4.2.2 yamt if (sqlite3_step(stmt) != SQLITE_ROW) {
351 1.4.2.2 yamt sqlite3_finalize(stmt);
352 1.4.2.2 yamt warnx("Unable to query schema version: %s",
353 1.4.2.2 yamt sqlite3_errmsg(db));
354 1.4.2.2 yamt goto error;
355 1.4.2.2 yamt }
356 1.4.2.2 yamt if (sqlite3_column_int(stmt, 0) != APROPOS_SCHEMA_VERSION) {
357 1.4.2.2 yamt sqlite3_finalize(stmt);
358 1.4.2.2 yamt warnx("Incorrect schema version found. "
359 1.4.2.2 yamt "Please run makemandb -f.");
360 1.4.2.2 yamt goto error;
361 1.4.2.2 yamt }
362 1.4.2.2 yamt sqlite3_finalize(stmt);
363 1.4.2.2 yamt
364 1.4.2.2 yamt sqlite3_extended_result_codes(db, 1);
365 1.4.2.2 yamt
366 1.4.2.2 yamt /* Register the zip and unzip functions for FTS compression */
367 1.4.2.2 yamt rc = sqlite3_create_function(db, "zip", 1, SQLITE_ANY, NULL, zip, NULL, NULL);
368 1.4.2.2 yamt if (rc != SQLITE_OK) {
369 1.4.2.2 yamt warnx("Unable to register function: compress: %s",
370 1.4.2.2 yamt sqlite3_errmsg(db));
371 1.4.2.2 yamt goto error;
372 1.4.2.2 yamt }
373 1.4.2.2 yamt
374 1.4.2.2 yamt rc = sqlite3_create_function(db, "unzip", 1, SQLITE_ANY, NULL,
375 1.4.2.2 yamt unzip, NULL, NULL);
376 1.4.2.2 yamt if (rc != SQLITE_OK) {
377 1.4.2.2 yamt warnx("Unable to register function: uncompress: %s",
378 1.4.2.2 yamt sqlite3_errmsg(db));
379 1.4.2.2 yamt goto error;
380 1.4.2.2 yamt }
381 1.4.2.2 yamt return db;
382 1.4.2.4 yamt
383 1.4.2.2 yamt error:
384 1.4.2.2 yamt sqlite3_close(db);
385 1.4.2.2 yamt sqlite3_shutdown();
386 1.4.2.2 yamt return NULL;
387 1.4.2.2 yamt }
388 1.4.2.2 yamt
389 1.4.2.2 yamt /*
390 1.4.2.2 yamt * rank_func --
391 1.4.2.2 yamt * Sqlite user defined function for ranking the documents.
392 1.4.2.2 yamt * For each phrase of the query, it computes the tf and idf and adds them over.
393 1.4.2.2 yamt * It computes the final rank, by multiplying tf and idf together.
394 1.4.2.2 yamt * Weight of term t for document d = (term frequency of t in d *
395 1.4.2.2 yamt * inverse document frequency of t)
396 1.4.2.2 yamt *
397 1.4.2.2 yamt * Term Frequency of term t in document d = Number of times t occurs in d /
398 1.4.2.2 yamt * Number of times t appears in all
399 1.4.2.2 yamt * documents
400 1.4.2.2 yamt *
401 1.4.2.2 yamt * Inverse document frequency of t = log(Total number of documents /
402 1.4.2.2 yamt * Number of documents in which t occurs)
403 1.4.2.2 yamt */
404 1.4.2.2 yamt static void
405 1.4.2.2 yamt rank_func(sqlite3_context *pctx, int nval, sqlite3_value **apval)
406 1.4.2.2 yamt {
407 1.4.2.2 yamt inverse_document_frequency *idf = sqlite3_user_data(pctx);
408 1.4.2.2 yamt double tf = 0.0;
409 1.4.2.2 yamt const unsigned int *matchinfo;
410 1.4.2.2 yamt int ncol;
411 1.4.2.2 yamt int nphrase;
412 1.4.2.2 yamt int iphrase;
413 1.4.2.2 yamt int ndoc;
414 1.4.2.2 yamt int doclen = 0;
415 1.4.2.2 yamt const double k = 3.75;
416 1.4.2.2 yamt /* Check that the number of arguments passed to this function is correct. */
417 1.4.2.2 yamt assert(nval == 1);
418 1.4.2.2 yamt
419 1.4.2.2 yamt matchinfo = (const unsigned int *) sqlite3_value_blob(apval[0]);
420 1.4.2.2 yamt nphrase = matchinfo[0];
421 1.4.2.2 yamt ncol = matchinfo[1];
422 1.4.2.2 yamt ndoc = matchinfo[2 + 3 * ncol * nphrase + ncol];
423 1.4.2.2 yamt for (iphrase = 0; iphrase < nphrase; iphrase++) {
424 1.4.2.2 yamt int icol;
425 1.4.2.2 yamt const unsigned int *phraseinfo = &matchinfo[2 + ncol+ iphrase * ncol * 3];
426 1.4.2.2 yamt for(icol = 1; icol < ncol; icol++) {
427 1.4.2.2 yamt
428 1.4.2.2 yamt /* nhitcount: number of times the current phrase occurs in the current
429 1.4.2.2 yamt * column in the current document.
430 1.4.2.2 yamt * nglobalhitcount: number of times current phrase occurs in the current
431 1.4.2.2 yamt * column in all documents.
432 1.4.2.2 yamt * ndocshitcount: number of documents in which the current phrase
433 1.4.2.2 yamt * occurs in the current column at least once.
434 1.4.2.2 yamt */
435 1.4.2.2 yamt int nhitcount = phraseinfo[3 * icol];
436 1.4.2.2 yamt int nglobalhitcount = phraseinfo[3 * icol + 1];
437 1.4.2.2 yamt int ndocshitcount = phraseinfo[3 * icol + 2];
438 1.4.2.2 yamt doclen = matchinfo[2 + icol ];
439 1.4.2.2 yamt double weight = col_weights[icol - 1];
440 1.4.2.2 yamt if (idf->status == 0 && ndocshitcount)
441 1.4.2.2 yamt idf->value += log(((double)ndoc / ndocshitcount))* weight;
442 1.4.2.2 yamt
443 1.4.2.2 yamt /* Dividing the tf by document length to normalize the effect of
444 1.4.2.2 yamt * longer documents.
445 1.4.2.2 yamt */
446 1.4.2.2 yamt if (nglobalhitcount > 0 && nhitcount)
447 1.4.2.2 yamt tf += (((double)nhitcount * weight) / (nglobalhitcount * doclen));
448 1.4.2.2 yamt }
449 1.4.2.2 yamt }
450 1.4.2.2 yamt idf->status = 1;
451 1.4.2.2 yamt
452 1.4.2.2 yamt /* Final score = (tf * idf)/ ( k + tf)
453 1.4.2.2 yamt * Dividing by k+ tf further normalizes the weight leading to better
454 1.4.2.2 yamt * results.
455 1.4.2.2 yamt * The value of k is experimental
456 1.4.2.2 yamt */
457 1.4.2.2 yamt double score = (tf * idf->value/ ( k + tf)) ;
458 1.4.2.2 yamt sqlite3_result_double(pctx, score);
459 1.4.2.2 yamt return;
460 1.4.2.2 yamt }
461 1.4.2.2 yamt
462 1.4.2.2 yamt /*
463 1.4.2.2 yamt * run_query --
464 1.4.2.2 yamt * Performs the searches for the keywords entered by the user.
465 1.4.2.2 yamt * The 2nd param: snippet_args is an array of strings providing values for the
466 1.4.2.2 yamt * last three parameters to the snippet function of sqlite. (Look at the docs).
467 1.4.2.2 yamt * The 3rd param: args contains rest of the search parameters. Look at
468 1.4.2.2 yamt * arpopos-utils.h for the description of individual fields.
469 1.4.2.2 yamt *
470 1.4.2.2 yamt */
471 1.4.2.2 yamt int
472 1.4.2.2 yamt run_query(sqlite3 *db, const char *snippet_args[3], query_args *args)
473 1.4.2.2 yamt {
474 1.4.2.2 yamt const char *default_snippet_args[3];
475 1.4.2.2 yamt char *section_clause = NULL;
476 1.4.2.2 yamt char *limit_clause = NULL;
477 1.4.2.2 yamt char *machine_clause = NULL;
478 1.4.2.2 yamt char *query;
479 1.4.2.2 yamt const char *section;
480 1.4.2.2 yamt char *name;
481 1.4.2.2 yamt const char *name_desc;
482 1.4.2.2 yamt const char *machine;
483 1.4.2.2 yamt const char *snippet;
484 1.4.2.2 yamt const char *name_temp;
485 1.4.2.2 yamt char *slash_ptr;
486 1.4.2.2 yamt char *m = NULL;
487 1.4.2.2 yamt int rc;
488 1.4.2.2 yamt inverse_document_frequency idf = {0, 0};
489 1.4.2.2 yamt sqlite3_stmt *stmt;
490 1.4.2.2 yamt
491 1.4.2.2 yamt if (args->machine)
492 1.4.2.2 yamt easprintf(&machine_clause, "AND machine = \'%s\' ", args->machine);
493 1.4.2.2 yamt
494 1.4.2.2 yamt /* Register the rank function */
495 1.4.2.2 yamt rc = sqlite3_create_function(db, "rank_func", 1, SQLITE_ANY, (void *)&idf,
496 1.4.2.2 yamt rank_func, NULL, NULL);
497 1.4.2.2 yamt if (rc != SQLITE_OK) {
498 1.4.2.2 yamt warnx("Unable to register the ranking function: %s",
499 1.4.2.2 yamt sqlite3_errmsg(db));
500 1.4.2.2 yamt sqlite3_close(db);
501 1.4.2.2 yamt sqlite3_shutdown();
502 1.4.2.2 yamt exit(EXIT_FAILURE);
503 1.4.2.2 yamt }
504 1.4.2.2 yamt
505 1.4.2.2 yamt /* We want to build a query of the form: "select x,y,z from mandb where
506 1.4.2.2 yamt * mandb match :query [AND (section LIKE '1' OR section LIKE '2' OR...)]
507 1.4.2.2 yamt * ORDER BY rank DESC..."
508 1.4.2.2 yamt * NOTES: 1. The portion in square brackets is optional, it will be there
509 1.4.2.2 yamt * only if the user has specified an option on the command line to search in
510 1.4.2.2 yamt * one or more specific sections.
511 1.4.2.2 yamt * 2. I am using LIKE operator because '=' or IN operators do not seem to be
512 1.4.2.2 yamt * working with the compression option enabled.
513 1.4.2.2 yamt */
514 1.4.2.2 yamt
515 1.4.2.2 yamt if (args->sec_nums) {
516 1.4.2.2 yamt char *temp;
517 1.4.2.2 yamt int i;
518 1.4.2.2 yamt
519 1.4.2.2 yamt for (i = 0; i < SECMAX; i++) {
520 1.4.2.2 yamt if (args->sec_nums[i] == 0)
521 1.4.2.2 yamt continue;
522 1.4.2.2 yamt easprintf(&temp, " OR section = \'%d\'", i + 1);
523 1.4.2.2 yamt if (section_clause) {
524 1.4.2.2 yamt concat(§ion_clause, temp);
525 1.4.2.2 yamt free(temp);
526 1.4.2.2 yamt } else {
527 1.4.2.2 yamt section_clause = temp;
528 1.4.2.2 yamt }
529 1.4.2.2 yamt }
530 1.4.2.2 yamt if (section_clause) {
531 1.4.2.2 yamt /*
532 1.4.2.2 yamt * At least one section requested, add glue for query.
533 1.4.2.2 yamt */
534 1.4.2.2 yamt temp = section_clause;
535 1.4.2.2 yamt /* Skip " OR " before first term. */
536 1.4.2.2 yamt easprintf(§ion_clause, " AND (%s)", temp + 4);
537 1.4.2.2 yamt free(temp);
538 1.4.2.2 yamt }
539 1.4.2.2 yamt }
540 1.4.2.2 yamt if (args->nrec >= 0) {
541 1.4.2.2 yamt /* Use the provided number of records and offset */
542 1.4.2.2 yamt easprintf(&limit_clause, " LIMIT %d OFFSET %d",
543 1.4.2.2 yamt args->nrec, args->offset);
544 1.4.2.2 yamt }
545 1.4.2.2 yamt
546 1.4.2.2 yamt if (snippet_args == NULL) {
547 1.4.2.2 yamt default_snippet_args[0] = "";
548 1.4.2.2 yamt default_snippet_args[1] = "";
549 1.4.2.2 yamt default_snippet_args[2] = "...";
550 1.4.2.2 yamt snippet_args = default_snippet_args;
551 1.4.2.2 yamt }
552 1.4.2.2 yamt query = sqlite3_mprintf("SELECT section, name, name_desc, machine,"
553 1.4.2.2 yamt " snippet(mandb, %Q, %Q, %Q, -1, 40 ),"
554 1.4.2.2 yamt " rank_func(matchinfo(mandb, \"pclxn\")) AS rank"
555 1.4.2.2 yamt " FROM mandb"
556 1.4.2.2 yamt " WHERE mandb MATCH %Q %s "
557 1.4.2.2 yamt "%s"
558 1.4.2.2 yamt " ORDER BY rank DESC"
559 1.4.2.2 yamt "%s",
560 1.4.2.2 yamt snippet_args[0], snippet_args[1], snippet_args[2], args->search_str,
561 1.4.2.2 yamt machine_clause ? machine_clause : "",
562 1.4.2.2 yamt section_clause ? section_clause : "",
563 1.4.2.2 yamt limit_clause ? limit_clause : "");
564 1.4.2.2 yamt
565 1.4.2.2 yamt free(machine_clause);
566 1.4.2.2 yamt free(section_clause);
567 1.4.2.2 yamt free(limit_clause);
568 1.4.2.2 yamt
569 1.4.2.2 yamt if (query == NULL) {
570 1.4.2.2 yamt *args->errmsg = estrdup("malloc failed");
571 1.4.2.2 yamt return -1;
572 1.4.2.2 yamt }
573 1.4.2.2 yamt rc = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
574 1.4.2.2 yamt if (rc == SQLITE_IOERR) {
575 1.4.2.2 yamt warnx("Corrupt database. Please rerun makemandb");
576 1.4.2.2 yamt sqlite3_free(query);
577 1.4.2.2 yamt return -1;
578 1.4.2.2 yamt } else if (rc != SQLITE_OK) {
579 1.4.2.2 yamt warnx("%s", sqlite3_errmsg(db));
580 1.4.2.2 yamt sqlite3_free(query);
581 1.4.2.2 yamt return -1;
582 1.4.2.2 yamt }
583 1.4.2.2 yamt
584 1.4.2.2 yamt while (sqlite3_step(stmt) == SQLITE_ROW) {
585 1.4.2.2 yamt section = (const char *) sqlite3_column_text(stmt, 0);
586 1.4.2.2 yamt name_temp = (const char *) sqlite3_column_text(stmt, 1);
587 1.4.2.2 yamt name_desc = (const char *) sqlite3_column_text(stmt, 2);
588 1.4.2.2 yamt machine = (const char *) sqlite3_column_text(stmt, 3);
589 1.4.2.2 yamt snippet = (const char *) sqlite3_column_text(stmt, 4);
590 1.4.2.2 yamt if ((slash_ptr = strrchr(name_temp, '/')) != NULL)
591 1.4.2.2 yamt name_temp = slash_ptr + 1;
592 1.4.2.2 yamt if (machine && machine[0]) {
593 1.4.2.2 yamt m = estrdup(machine);
594 1.4.2.2 yamt easprintf(&name, "%s/%s", lower(m),
595 1.4.2.2 yamt name_temp);
596 1.4.2.2 yamt free(m);
597 1.4.2.2 yamt } else {
598 1.4.2.2 yamt name = estrdup((const char *) sqlite3_column_text(stmt, 1));
599 1.4.2.2 yamt }
600 1.4.2.2 yamt
601 1.4.2.2 yamt (args->callback)(args->callback_data, section, name, name_desc, snippet,
602 1.4.2.2 yamt strlen(snippet));
603 1.4.2.2 yamt
604 1.4.2.2 yamt free(name);
605 1.4.2.2 yamt }
606 1.4.2.2 yamt
607 1.4.2.2 yamt sqlite3_finalize(stmt);
608 1.4.2.2 yamt sqlite3_free(query);
609 1.4.2.2 yamt return *(args->errmsg) == NULL ? 0 : -1;
610 1.4.2.2 yamt }
611 1.4.2.2 yamt
612 1.4.2.2 yamt /*
613 1.4.2.2 yamt * callback_html --
614 1.4.2.2 yamt * Callback function for run_query_html. It builds the html output and then
615 1.4.2.2 yamt * calls the actual user supplied callback function.
616 1.4.2.2 yamt */
617 1.4.2.2 yamt static int
618 1.4.2.2 yamt callback_html(void *data, const char *section, const char *name,
619 1.4.2.2 yamt const char *name_desc, const char *snippet, size_t snippet_length)
620 1.4.2.2 yamt {
621 1.4.2.2 yamt const char *temp = snippet;
622 1.4.2.2 yamt int i = 0;
623 1.4.2.2 yamt size_t sz = 0;
624 1.4.2.2 yamt int count = 0;
625 1.4.2.2 yamt struct orig_callback_data *orig_data = (struct orig_callback_data *) data;
626 1.4.2.2 yamt int (*callback) (void *, const char *, const char *, const char *,
627 1.4.2.2 yamt const char *, size_t) = orig_data->callback;
628 1.4.2.2 yamt
629 1.4.2.2 yamt /* First scan the snippet to find out the number of occurrences of {'>', '<'
630 1.4.2.2 yamt * '"', '&'}.
631 1.4.2.2 yamt * Then allocate a new buffer with sufficient space to be able to store the
632 1.4.2.2 yamt * quoted versions of the special characters {>, <, ", &}.
633 1.4.2.2 yamt * Copy over the characters from the original snippet to this buffer while
634 1.4.2.2 yamt * replacing the special characters with their quoted versions.
635 1.4.2.2 yamt */
636 1.4.2.2 yamt
637 1.4.2.2 yamt while (*temp) {
638 1.4.2.2 yamt sz = strcspn(temp, "<>\"&\002\003");
639 1.4.2.2 yamt temp += sz + 1;
640 1.4.2.2 yamt count++;
641 1.4.2.2 yamt }
642 1.4.2.2 yamt size_t qsnippet_length = snippet_length + count * 5;
643 1.4.2.2 yamt char *qsnippet = emalloc(qsnippet_length + 1);
644 1.4.2.2 yamt sz = 0;
645 1.4.2.2 yamt while (*snippet) {
646 1.4.2.2 yamt sz = strcspn(snippet, "<>\"&\002\003");
647 1.4.2.2 yamt if (sz) {
648 1.4.2.2 yamt memcpy(&qsnippet[i], snippet, sz);
649 1.4.2.2 yamt snippet += sz;
650 1.4.2.2 yamt i += sz;
651 1.4.2.2 yamt }
652 1.4.2.2 yamt
653 1.4.2.2 yamt switch (*snippet++) {
654 1.4.2.2 yamt case '<':
655 1.4.2.2 yamt memcpy(&qsnippet[i], "<", 4);
656 1.4.2.2 yamt i += 4;
657 1.4.2.2 yamt break;
658 1.4.2.2 yamt case '>':
659 1.4.2.2 yamt memcpy(&qsnippet[i], ">", 4);
660 1.4.2.2 yamt i += 4;
661 1.4.2.2 yamt break;
662 1.4.2.2 yamt case '\"':
663 1.4.2.2 yamt memcpy(&qsnippet[i], """, 6);
664 1.4.2.2 yamt i += 6;
665 1.4.2.2 yamt break;
666 1.4.2.2 yamt case '&':
667 1.4.2.2 yamt /* Don't perform the quoting if this & is part of an mdoc escape
668 1.4.2.2 yamt * sequence, e.g. \&
669 1.4.2.2 yamt */
670 1.4.2.2 yamt if (i && *(snippet - 2) != '\\') {
671 1.4.2.2 yamt memcpy(&qsnippet[i], "&", 5);
672 1.4.2.2 yamt i += 5;
673 1.4.2.2 yamt } else {
674 1.4.2.2 yamt qsnippet[i++] = '&';
675 1.4.2.2 yamt }
676 1.4.2.2 yamt break;
677 1.4.2.2 yamt case '\002':
678 1.4.2.2 yamt memcpy(&qsnippet[i], "<b>", 3);
679 1.4.2.2 yamt i += 3;
680 1.4.2.2 yamt break;
681 1.4.2.2 yamt case '\003':
682 1.4.2.2 yamt memcpy(&qsnippet[i], "</b>", 4);
683 1.4.2.2 yamt i += 4;
684 1.4.2.2 yamt break;
685 1.4.2.2 yamt default:
686 1.4.2.2 yamt break;
687 1.4.2.2 yamt }
688 1.4.2.2 yamt }
689 1.4.2.2 yamt qsnippet[++i] = 0;
690 1.4.2.2 yamt (*callback)(orig_data->data, section, name, name_desc,
691 1.4.2.2 yamt (const char *)qsnippet, qsnippet_length);
692 1.4.2.2 yamt free(qsnippet);
693 1.4.2.2 yamt return 0;
694 1.4.2.2 yamt }
695 1.4.2.2 yamt
696 1.4.2.2 yamt /*
697 1.4.2.2 yamt * run_query_html --
698 1.4.2.2 yamt * Utility function to output query result in HTML format.
699 1.4.2.2 yamt * It internally calls run_query only, but it first passes the output to it's
700 1.4.2.2 yamt * own custom callback function, which preprocess the snippet for quoting
701 1.4.2.2 yamt * inline HTML fragments.
702 1.4.2.2 yamt * After that it delegates the call the actual user supplied callback function.
703 1.4.2.2 yamt */
704 1.4.2.2 yamt int
705 1.4.2.2 yamt run_query_html(sqlite3 *db, query_args *args)
706 1.4.2.2 yamt {
707 1.4.2.2 yamt struct orig_callback_data orig_data;
708 1.4.2.2 yamt orig_data.callback = args->callback;
709 1.4.2.2 yamt orig_data.data = args->callback_data;
710 1.4.2.2 yamt const char *snippet_args[] = {"\002", "\003", "..."};
711 1.4.2.2 yamt args->callback = &callback_html;
712 1.4.2.2 yamt args->callback_data = (void *) &orig_data;
713 1.4.2.2 yamt return run_query(db, snippet_args, args);
714 1.4.2.2 yamt }
715 1.4.2.2 yamt
716 1.4.2.2 yamt /*
717 1.4.2.5 yamt * underline a string, pager style.
718 1.4.2.5 yamt */
719 1.4.2.5 yamt static char *
720 1.4.2.5 yamt ul_pager(const char *s)
721 1.4.2.5 yamt {
722 1.4.2.5 yamt size_t len;
723 1.4.2.5 yamt char *dst, *d;
724 1.4.2.5 yamt
725 1.4.2.5 yamt // a -> _\ba
726 1.4.2.5 yamt len = strlen(s) * 3 + 1;
727 1.4.2.5 yamt
728 1.4.2.5 yamt d = dst = emalloc(len);
729 1.4.2.5 yamt while (*s) {
730 1.4.2.5 yamt *d++ = '_';
731 1.4.2.5 yamt *d++ = '\b';
732 1.4.2.5 yamt *d++ = *s++;
733 1.4.2.5 yamt }
734 1.4.2.5 yamt *d = '\0';
735 1.4.2.5 yamt return dst;
736 1.4.2.5 yamt }
737 1.4.2.5 yamt
738 1.4.2.5 yamt /*
739 1.4.2.2 yamt * callback_pager --
740 1.4.2.2 yamt * A callback similar to callback_html. It overstrikes the matching text in
741 1.4.2.2 yamt * the snippet so that it appears emboldened when viewed using a pager like
742 1.4.2.2 yamt * more or less.
743 1.4.2.2 yamt */
744 1.4.2.2 yamt static int
745 1.4.2.2 yamt callback_pager(void *data, const char *section, const char *name,
746 1.4.2.2 yamt const char *name_desc, const char *snippet, size_t snippet_length)
747 1.4.2.2 yamt {
748 1.4.2.2 yamt struct orig_callback_data *orig_data = (struct orig_callback_data *) data;
749 1.4.2.2 yamt char *psnippet;
750 1.4.2.2 yamt const char *temp = snippet;
751 1.4.2.2 yamt int count = 0;
752 1.4.2.2 yamt int i = 0;
753 1.4.2.2 yamt size_t sz = 0;
754 1.4.2.2 yamt size_t psnippet_length;
755 1.4.2.2 yamt
756 1.4.2.2 yamt /* Count the number of bytes of matching text. For each of these bytes we
757 1.4.2.2 yamt * will use 2 extra bytes to overstrike it so that it appears bold when
758 1.4.2.2 yamt * viewed using a pager.
759 1.4.2.2 yamt */
760 1.4.2.2 yamt while (*temp) {
761 1.4.2.2 yamt sz = strcspn(temp, "\002\003");
762 1.4.2.2 yamt temp += sz;
763 1.4.2.2 yamt if (*temp == '\003') {
764 1.4.2.2 yamt count += 2 * (sz);
765 1.4.2.2 yamt }
766 1.4.2.2 yamt temp++;
767 1.4.2.2 yamt }
768 1.4.2.2 yamt
769 1.4.2.2 yamt psnippet_length = snippet_length + count;
770 1.4.2.2 yamt psnippet = emalloc(psnippet_length + 1);
771 1.4.2.2 yamt
772 1.4.2.2 yamt /* Copy the bytes from snippet to psnippet:
773 1.4.2.2 yamt * 1. Copy the bytes before \002 as it is.
774 1.4.2.2 yamt * 2. The bytes after \002 need to be overstriked till we encounter \003.
775 1.4.2.2 yamt * 3. To overstrike a byte 'A' we need to write 'A\bA'
776 1.4.2.2 yamt */
777 1.4.2.2 yamt while (*snippet) {
778 1.4.2.2 yamt sz = strcspn(snippet, "\002");
779 1.4.2.2 yamt memcpy(&psnippet[i], snippet, sz);
780 1.4.2.2 yamt snippet += sz;
781 1.4.2.2 yamt i += sz;
782 1.4.2.2 yamt
783 1.4.2.2 yamt /* Don't change this. Advancing the pointer without reading the byte
784 1.4.2.2 yamt * is causing strange behavior.
785 1.4.2.2 yamt */
786 1.4.2.2 yamt if (*snippet == '\002')
787 1.4.2.2 yamt snippet++;
788 1.4.2.2 yamt while (*snippet && *snippet != '\003') {
789 1.4.2.2 yamt psnippet[i++] = *snippet;
790 1.4.2.2 yamt psnippet[i++] = '\b';
791 1.4.2.2 yamt psnippet[i++] = *snippet++;
792 1.4.2.2 yamt }
793 1.4.2.2 yamt if (*snippet)
794 1.4.2.2 yamt snippet++;
795 1.4.2.2 yamt }
796 1.4.2.2 yamt
797 1.4.2.2 yamt psnippet[i] = 0;
798 1.4.2.5 yamt char *ul_section = ul_pager(section);
799 1.4.2.5 yamt char *ul_name = ul_pager(name);
800 1.4.2.5 yamt char *ul_name_desc = ul_pager(name_desc);
801 1.4.2.5 yamt (orig_data->callback)(orig_data->data, ul_section, ul_name,
802 1.4.2.5 yamt ul_name_desc, psnippet, psnippet_length);
803 1.4.2.5 yamt free(ul_section);
804 1.4.2.5 yamt free(ul_name);
805 1.4.2.5 yamt free(ul_name_desc);
806 1.4.2.2 yamt free(psnippet);
807 1.4.2.2 yamt return 0;
808 1.4.2.2 yamt }
809 1.4.2.2 yamt
810 1.4.2.5 yamt struct term_args {
811 1.4.2.5 yamt struct orig_callback_data *orig_data;
812 1.4.2.5 yamt const char *smul;
813 1.4.2.5 yamt const char *rmul;
814 1.4.2.5 yamt };
815 1.4.2.5 yamt
816 1.4.2.5 yamt /*
817 1.4.2.5 yamt * underline a string, pager style.
818 1.4.2.5 yamt */
819 1.4.2.5 yamt static char *
820 1.4.2.5 yamt ul_term(const char *s, const struct term_args *ta)
821 1.4.2.5 yamt {
822 1.4.2.5 yamt char *dst;
823 1.4.2.5 yamt
824 1.4.2.5 yamt easprintf(&dst, "%s%s%s", ta->smul, s, ta->rmul);
825 1.4.2.5 yamt return dst;
826 1.4.2.5 yamt }
827 1.4.2.5 yamt
828 1.4.2.5 yamt /*
829 1.4.2.5 yamt * callback_term --
830 1.4.2.5 yamt * A callback similar to callback_html. It overstrikes the matching text in
831 1.4.2.5 yamt * the snippet so that it appears emboldened when viewed using a pager like
832 1.4.2.5 yamt * more or less.
833 1.4.2.5 yamt */
834 1.4.2.5 yamt static int
835 1.4.2.5 yamt callback_term(void *data, const char *section, const char *name,
836 1.4.2.5 yamt const char *name_desc, const char *snippet, size_t snippet_length)
837 1.4.2.5 yamt {
838 1.4.2.5 yamt struct term_args *ta = data;
839 1.4.2.5 yamt struct orig_callback_data *orig_data = ta->orig_data;
840 1.4.2.5 yamt
841 1.4.2.5 yamt char *ul_section = ul_term(section, ta);
842 1.4.2.5 yamt char *ul_name = ul_term(name, ta);
843 1.4.2.5 yamt char *ul_name_desc = ul_term(name_desc, ta);
844 1.4.2.5 yamt (orig_data->callback)(orig_data->data, ul_section, ul_name,
845 1.4.2.5 yamt ul_name_desc, snippet, snippet_length);
846 1.4.2.5 yamt free(ul_section);
847 1.4.2.5 yamt free(ul_name);
848 1.4.2.5 yamt free(ul_name_desc);
849 1.4.2.5 yamt return 0;
850 1.4.2.5 yamt }
851 1.4.2.5 yamt
852 1.4.2.2 yamt /*
853 1.4.2.2 yamt * run_query_pager --
854 1.4.2.2 yamt * Utility function similar to run_query_html. This function tries to
855 1.4.2.2 yamt * pre-process the result assuming it will be piped to a pager.
856 1.4.2.2 yamt * For this purpose it first calls it's own callback function callback_pager
857 1.4.2.2 yamt * which then delegates the call to the user supplied callback.
858 1.4.2.2 yamt */
859 1.4.2.3 yamt int
860 1.4.2.3 yamt run_query_pager(sqlite3 *db, query_args *args)
861 1.4.2.2 yamt {
862 1.4.2.2 yamt struct orig_callback_data orig_data;
863 1.4.2.2 yamt orig_data.callback = args->callback;
864 1.4.2.2 yamt orig_data.data = args->callback_data;
865 1.4.2.2 yamt const char *snippet_args[] = {"\002", "\003", "..."};
866 1.4.2.2 yamt args->callback = &callback_pager;
867 1.4.2.2 yamt args->callback_data = (void *) &orig_data;
868 1.4.2.2 yamt return run_query(db, snippet_args, args);
869 1.4.2.2 yamt }
870 1.4.2.5 yamt
871 1.4.2.5 yamt static void
872 1.4.2.5 yamt term_init(int fd, const char *sa[5])
873 1.4.2.5 yamt {
874 1.4.2.5 yamt TERMINAL *ti;
875 1.4.2.5 yamt int error;
876 1.4.2.5 yamt const char *bold, *sgr0, *smso, *rmso, *smul, *rmul;
877 1.4.2.5 yamt
878 1.4.2.5 yamt if (ti_setupterm(&ti, NULL, fd, &error) == -1) {
879 1.4.2.5 yamt bold = sgr0 = NULL;
880 1.4.2.5 yamt smso = rmso = smul = rmul = "";
881 1.4.2.5 yamt ti = NULL;
882 1.4.2.5 yamt } else {
883 1.4.2.5 yamt bold = ti_getstr(ti, "bold");
884 1.4.2.5 yamt sgr0 = ti_getstr(ti, "sgr0");
885 1.4.2.5 yamt if (bold == NULL || sgr0 == NULL) {
886 1.4.2.5 yamt smso = ti_getstr(ti, "smso");
887 1.4.2.5 yamt
888 1.4.2.5 yamt if (smso == NULL ||
889 1.4.2.5 yamt (rmso = ti_getstr(ti, "rmso")) == NULL)
890 1.4.2.5 yamt smso = rmso = "";
891 1.4.2.5 yamt bold = sgr0 = NULL;
892 1.4.2.5 yamt } else
893 1.4.2.5 yamt smso = rmso = "";
894 1.4.2.5 yamt
895 1.4.2.5 yamt smul = ti_getstr(ti, "smul");
896 1.4.2.5 yamt if (smul == NULL || (rmul = ti_getstr(ti, "rmul")) == NULL)
897 1.4.2.5 yamt smul = rmul = "";
898 1.4.2.5 yamt }
899 1.4.2.5 yamt
900 1.4.2.5 yamt sa[0] = estrdup(bold ? bold : smso);
901 1.4.2.5 yamt sa[1] = estrdup(sgr0 ? sgr0 : rmso);
902 1.4.2.5 yamt sa[2] = estrdup("...");
903 1.4.2.5 yamt sa[3] = estrdup(smul);
904 1.4.2.5 yamt sa[4] = estrdup(rmul);
905 1.4.2.5 yamt if (ti)
906 1.4.2.5 yamt del_curterm(ti);
907 1.4.2.5 yamt }
908 1.4.2.5 yamt
909 1.4.2.5 yamt /*
910 1.4.2.5 yamt * run_query_term --
911 1.4.2.5 yamt * Utility function similar to run_query_html. This function tries to
912 1.4.2.5 yamt * pre-process the result assuming it will be displayed on a terminal
913 1.4.2.5 yamt * For this purpose it first calls it's own callback function callback_pager
914 1.4.2.5 yamt * which then delegates the call to the user supplied callback.
915 1.4.2.5 yamt */
916 1.4.2.5 yamt int
917 1.4.2.5 yamt run_query_term(sqlite3 *db, query_args *args)
918 1.4.2.5 yamt {
919 1.4.2.5 yamt struct orig_callback_data orig_data;
920 1.4.2.5 yamt struct term_args ta;
921 1.4.2.5 yamt orig_data.callback = args->callback;
922 1.4.2.5 yamt orig_data.data = args->callback_data;
923 1.4.2.5 yamt const char *snippet_args[5];
924 1.4.2.5 yamt term_init(STDOUT_FILENO, snippet_args);
925 1.4.2.5 yamt ta.smul = snippet_args[3];
926 1.4.2.5 yamt ta.rmul = snippet_args[4];
927 1.4.2.5 yamt ta.orig_data = (void *) &orig_data;
928 1.4.2.5 yamt
929 1.4.2.5 yamt args->callback = &callback_term;
930 1.4.2.5 yamt args->callback_data = &ta;
931 1.4.2.5 yamt return run_query(db, snippet_args, args);
932 1.4.2.5 yamt }
933