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