Thu Mar 31 20:16:58 2016 UTC ()
PR/51025: Abhinav Upadhyay: Remove unused includes from apropos-utils.c


(christos)
diff -r1.21 -r1.22 src/usr.sbin/makemandb/apropos-utils.c

cvs diff -r1.21 -r1.22 src/usr.sbin/makemandb/apropos-utils.c (switch to unified diff)

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