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