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