]> git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
After careful gprof(1)ing of the new apropos(1), move the descriptions
[mandoc.git] / mansearch.c
1 /* $Id: mansearch.c,v 1.26 2014/04/09 21:50:08 schwarze Exp $ */
2 /*
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <assert.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <limits.h>
26 #include <regex.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #ifdef HAVE_OHASH
35 #include <ohash.h>
36 #else
37 #include "compat_ohash.h"
38 #endif
39 #include <sqlite3.h>
40
41 #include "mandoc.h"
42 #include "mandoc_aux.h"
43 #include "manpath.h"
44 #include "mansearch.h"
45
46 extern int mansearch_keymax;
47 extern const char *const mansearch_keynames[];
48
49 #define SQL_BIND_TEXT(_db, _s, _i, _v) \
50 do { if (SQLITE_OK != sqlite3_bind_text \
51 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
52 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
53 } while (0)
54 #define SQL_BIND_INT64(_db, _s, _i, _v) \
55 do { if (SQLITE_OK != sqlite3_bind_int64 \
56 ((_s), (_i)++, (_v))) \
57 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
58 } while (0)
59 #define SQL_BIND_BLOB(_db, _s, _i, _v) \
60 do { if (SQLITE_OK != sqlite3_bind_blob \
61 ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
62 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
63 } while (0)
64
65 struct expr {
66 uint64_t bits; /* type-mask */
67 const char *substr; /* to search for, if applicable */
68 regex_t regexp; /* compiled regexp, if applicable */
69 int open; /* opening parentheses before */
70 int and; /* logical AND before */
71 int close; /* closing parentheses after */
72 struct expr *next; /* next in sequence */
73 };
74
75 struct match {
76 uint64_t id; /* identifier in database */
77 char *desc; /* manual page description */
78 int form; /* 0 == catpage */
79 };
80
81 static void buildnames(struct manpage *, sqlite3 *,
82 sqlite3_stmt *, uint64_t,
83 const char *, int form);
84 static char *buildoutput(sqlite3 *, sqlite3_stmt *,
85 uint64_t, uint64_t);
86 static void *hash_alloc(size_t, void *);
87 static void hash_free(void *, size_t, void *);
88 static void *hash_halloc(size_t, void *);
89 static struct expr *exprcomp(const struct mansearch *,
90 int, char *[]);
91 static void exprfree(struct expr *);
92 static struct expr *exprspec(struct expr *, uint64_t,
93 const char *, const char *);
94 static struct expr *exprterm(const struct mansearch *, char *, int);
95 static void sql_append(char **sql, size_t *sz,
96 const char *newstr, int count);
97 static void sql_match(sqlite3_context *context,
98 int argc, sqlite3_value **argv);
99 static void sql_regexp(sqlite3_context *context,
100 int argc, sqlite3_value **argv);
101 static char *sql_statement(const struct expr *);
102
103 int
104 mansearch(const struct mansearch *search,
105 const struct manpaths *paths,
106 int argc, char *argv[],
107 const char *outkey,
108 struct manpage **res, size_t *sz)
109 {
110 int fd, rc, c, indexbit;
111 int64_t id;
112 uint64_t outbit, iterbit;
113 char buf[PATH_MAX];
114 char *sql;
115 struct manpage *mpage;
116 struct expr *e, *ep;
117 sqlite3 *db;
118 sqlite3_stmt *s, *s2;
119 struct match *mp;
120 struct ohash_info info;
121 struct ohash htab;
122 unsigned int idx;
123 size_t i, j, cur, maxres;
124
125 memset(&info, 0, sizeof(struct ohash_info));
126
127 info.halloc = hash_halloc;
128 info.alloc = hash_alloc;
129 info.hfree = hash_free;
130 info.key_offset = offsetof(struct match, id);
131
132 *sz = cur = maxres = 0;
133 sql = NULL;
134 *res = NULL;
135 fd = -1;
136 e = NULL;
137 rc = 0;
138
139 if (0 == argc)
140 goto out;
141 if (NULL == (e = exprcomp(search, argc, argv)))
142 goto out;
143
144 outbit = 0;
145 if (NULL != outkey) {
146 for (indexbit = 0, iterbit = 1;
147 indexbit < mansearch_keymax;
148 indexbit++, iterbit <<= 1) {
149 if (0 == strcasecmp(outkey,
150 mansearch_keynames[indexbit])) {
151 outbit = iterbit;
152 break;
153 }
154 }
155 }
156
157 /*
158 * Save a descriptor to the current working directory.
159 * Since pathnames in the "paths" variable might be relative,
160 * and we'll be chdir()ing into them, we need to keep a handle
161 * on our current directory from which to start the chdir().
162 */
163
164 if (NULL == getcwd(buf, PATH_MAX)) {
165 perror(NULL);
166 goto out;
167 } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
168 perror(buf);
169 goto out;
170 }
171
172 sql = sql_statement(e);
173
174 /*
175 * Loop over the directories (containing databases) for us to
176 * search.
177 * Don't let missing/bad databases/directories phase us.
178 * In each, try to open the resident database and, if it opens,
179 * scan it for our match expression.
180 */
181
182 for (i = 0; i < paths->sz; i++) {
183 if (-1 == fchdir(fd)) {
184 perror(buf);
185 free(*res);
186 break;
187 } else if (-1 == chdir(paths->paths[i])) {
188 perror(paths->paths[i]);
189 continue;
190 }
191
192 c = sqlite3_open_v2
193 (MANDOC_DB, &db,
194 SQLITE_OPEN_READONLY, NULL);
195
196 if (SQLITE_OK != c) {
197 perror(MANDOC_DB);
198 sqlite3_close(db);
199 continue;
200 }
201
202 /*
203 * Define the SQL functions for substring
204 * and regular expression matching.
205 */
206
207 c = sqlite3_create_function(db, "match", 2,
208 SQLITE_ANY, NULL, sql_match, NULL, NULL);
209 assert(SQLITE_OK == c);
210 c = sqlite3_create_function(db, "regexp", 2,
211 SQLITE_ANY, NULL, sql_regexp, NULL, NULL);
212 assert(SQLITE_OK == c);
213
214 j = 1;
215 c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
216 if (SQLITE_OK != c)
217 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
218
219 for (ep = e; NULL != ep; ep = ep->next) {
220 if (NULL == ep->substr) {
221 SQL_BIND_BLOB(db, s, j, ep->regexp);
222 } else
223 SQL_BIND_TEXT(db, s, j, ep->substr);
224 if (0 == (TYPE_Nd & ep->bits))
225 SQL_BIND_INT64(db, s, j, ep->bits);
226 }
227
228 memset(&htab, 0, sizeof(struct ohash));
229 ohash_init(&htab, 4, &info);
230
231 /*
232 * Hash each entry on its [unique] document identifier.
233 * This is a uint64_t.
234 * Instead of using a hash function, simply convert the
235 * uint64_t to a uint32_t, the hash value's type.
236 * This gives good performance and preserves the
237 * distribution of buckets in the table.
238 */
239 while (SQLITE_ROW == (c = sqlite3_step(s))) {
240 id = sqlite3_column_int64(s, 2);
241 idx = ohash_lookup_memory
242 (&htab, (char *)&id,
243 sizeof(uint64_t), (uint32_t)id);
244
245 if (NULL != ohash_find(&htab, idx))
246 continue;
247
248 mp = mandoc_calloc(1, sizeof(struct match));
249 mp->id = id;
250 mp->form = sqlite3_column_int(s, 1);
251 if (TYPE_Nd == outbit)
252 mp->desc = mandoc_strdup(
253 sqlite3_column_text(s, 0));
254 ohash_insert(&htab, idx, mp);
255 }
256
257 if (SQLITE_DONE != c)
258 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
259
260 sqlite3_finalize(s);
261
262 c = sqlite3_prepare_v2(db,
263 "SELECT * FROM mlinks WHERE pageid=?"
264 " ORDER BY sec, arch, name",
265 -1, &s, NULL);
266 if (SQLITE_OK != c)
267 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
268
269 c = sqlite3_prepare_v2(db,
270 "SELECT * FROM keys WHERE pageid=? AND bits & ?",
271 -1, &s2, NULL);
272 if (SQLITE_OK != c)
273 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
274
275 for (mp = ohash_first(&htab, &idx);
276 NULL != mp;
277 mp = ohash_next(&htab, &idx)) {
278 if (cur + 1 > maxres) {
279 maxres += 1024;
280 *res = mandoc_realloc
281 (*res, maxres * sizeof(struct manpage));
282 }
283 mpage = *res + cur;
284 mpage->form = mp->form;
285 buildnames(mpage, db, s, mp->id,
286 paths->paths[i], mp->form);
287 mpage->output = TYPE_Nd & outbit ?
288 mp->desc : outbit ?
289 buildoutput(db, s2, mp->id, outbit) : NULL;
290
291 free(mp);
292 cur++;
293 }
294
295 sqlite3_finalize(s);
296 sqlite3_finalize(s2);
297 sqlite3_close(db);
298 ohash_delete(&htab);
299 }
300 rc = 1;
301 out:
302 exprfree(e);
303 if (-1 != fd)
304 close(fd);
305 free(sql);
306 *sz = cur;
307 return(rc);
308 }
309
310 static void
311 buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
312 uint64_t id, const char *path, int form)
313 {
314 char *newnames, *prevsec, *prevarch;
315 const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
316 size_t i;
317 int c;
318
319 mpage->file = NULL;
320 mpage->names = NULL;
321 prevsec = prevarch = NULL;
322 i = 1;
323 SQL_BIND_INT64(db, s, i, id);
324 while (SQLITE_ROW == (c = sqlite3_step(s))) {
325
326 /* Decide whether we already have some names. */
327
328 if (NULL == mpage->names) {
329 oldnames = "";
330 sep1 = "";
331 } else {
332 oldnames = mpage->names;
333 sep1 = ", ";
334 }
335
336 /* Fetch the next name. */
337
338 sec = sqlite3_column_text(s, 0);
339 arch = sqlite3_column_text(s, 1);
340 name = sqlite3_column_text(s, 2);
341
342 /* If the section changed, append the old one. */
343
344 if (NULL != prevsec &&
345 (strcmp(sec, prevsec) ||
346 strcmp(arch, prevarch))) {
347 sep2 = '\0' == *prevarch ? "" : "/";
348 mandoc_asprintf(&newnames, "%s(%s%s%s)",
349 oldnames, prevsec, sep2, prevarch);
350 free(mpage->names);
351 oldnames = mpage->names = newnames;
352 free(prevsec);
353 free(prevarch);
354 prevsec = prevarch = NULL;
355 }
356
357 /* Save the new section, to append it later. */
358
359 if (NULL == prevsec) {
360 prevsec = mandoc_strdup(sec);
361 prevarch = mandoc_strdup(arch);
362 }
363
364 /* Append the new name. */
365
366 mandoc_asprintf(&newnames, "%s%s%s",
367 oldnames, sep1, name);
368 free(mpage->names);
369 mpage->names = newnames;
370
371 /* Also save the first file name encountered. */
372
373 if (NULL != mpage->file)
374 continue;
375
376 if (form) {
377 sep1 = "man";
378 fsec = sec;
379 } else {
380 sep1 = "cat";
381 fsec = "0";
382 }
383 sep2 = '\0' == *arch ? "" : "/";
384 mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
385 path, sep1, sec, sep2, arch, name, fsec);
386 }
387 if (SQLITE_DONE != c)
388 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
389 sqlite3_reset(s);
390
391 /* Append one final section to the names. */
392
393 if (NULL != prevsec) {
394 sep2 = '\0' == *prevarch ? "" : "/";
395 mandoc_asprintf(&newnames, "%s(%s%s%s)",
396 mpage->names, prevsec, sep2, prevarch);
397 free(mpage->names);
398 mpage->names = newnames;
399 free(prevsec);
400 free(prevarch);
401 }
402 }
403
404 static char *
405 buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t id, uint64_t outbit)
406 {
407 char *output, *newoutput;
408 const char *oldoutput, *sep1, *data;
409 size_t i;
410 int c;
411
412 output = NULL;
413 i = 1;
414 SQL_BIND_INT64(db, s, i, id);
415 SQL_BIND_INT64(db, s, i, outbit);
416 while (SQLITE_ROW == (c = sqlite3_step(s))) {
417 if (NULL == output) {
418 oldoutput = "";
419 sep1 = "";
420 } else {
421 oldoutput = output;
422 sep1 = " # ";
423 }
424 data = sqlite3_column_text(s, 1);
425 mandoc_asprintf(&newoutput, "%s%s%s",
426 oldoutput, sep1, data);
427 free(output);
428 output = newoutput;
429 }
430 if (SQLITE_DONE != c)
431 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
432 sqlite3_reset(s);
433 return(output);
434 }
435
436 /*
437 * Implement substring match as an application-defined SQL function.
438 * Using the SQL LIKE or GLOB operators instead would be a bad idea
439 * because that would require escaping metacharacters in the string
440 * being searched for.
441 */
442 static void
443 sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
444 {
445
446 assert(2 == argc);
447 sqlite3_result_int(context, NULL != strcasestr(
448 (const char *)sqlite3_value_text(argv[1]),
449 (const char *)sqlite3_value_text(argv[0])));
450 }
451
452 /*
453 * Implement regular expression match
454 * as an application-defined SQL function.
455 */
456 static void
457 sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
458 {
459
460 assert(2 == argc);
461 sqlite3_result_int(context, !regexec(
462 (regex_t *)sqlite3_value_blob(argv[0]),
463 (const char *)sqlite3_value_text(argv[1]),
464 0, NULL, 0));
465 }
466
467 static void
468 sql_append(char **sql, size_t *sz, const char *newstr, int count)
469 {
470 size_t newsz;
471
472 newsz = 1 < count ? (size_t)count : strlen(newstr);
473 *sql = mandoc_realloc(*sql, *sz + newsz + 1);
474 if (1 < count)
475 memset(*sql + *sz, *newstr, (size_t)count);
476 else
477 memcpy(*sql + *sz, newstr, newsz);
478 *sz += newsz;
479 (*sql)[*sz] = '\0';
480 }
481
482 /*
483 * Prepare the search SQL statement.
484 */
485 static char *
486 sql_statement(const struct expr *e)
487 {
488 char *sql;
489 size_t sz;
490 int needop;
491
492 sql = mandoc_strdup("SELECT * FROM mpages WHERE ");
493 sz = strlen(sql);
494
495 for (needop = 0; NULL != e; e = e->next) {
496 if (e->and)
497 sql_append(&sql, &sz, " AND ", 1);
498 else if (needop)
499 sql_append(&sql, &sz, " OR ", 1);
500 if (e->open)
501 sql_append(&sql, &sz, "(", e->open);
502 sql_append(&sql, &sz,
503 TYPE_Nd & e->bits
504 ? (NULL == e->substr
505 ? "desc REGEXP ?"
506 : "desc MATCH ?")
507 : (NULL == e->substr
508 ? "id IN (SELECT pageid FROM keys "
509 "WHERE key REGEXP ? AND bits & ?)"
510 : "id IN (SELECT pageid FROM keys "
511 "WHERE key MATCH ? AND bits & ?)"), 1);
512 if (e->close)
513 sql_append(&sql, &sz, ")", e->close);
514 needop = 1;
515 }
516
517 return(sql);
518 }
519
520 /*
521 * Compile a set of string tokens into an expression.
522 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
523 * "(", "foo=bar", etc.).
524 */
525 static struct expr *
526 exprcomp(const struct mansearch *search, int argc, char *argv[])
527 {
528 int i, toopen, logic, igncase, toclose;
529 struct expr *first, *next, *cur;
530
531 first = cur = NULL;
532 logic = igncase = toclose = 0;
533 toopen = 1;
534
535 for (i = 0; i < argc; i++) {
536 if (0 == strcmp("(", argv[i])) {
537 if (igncase)
538 goto fail;
539 toopen++;
540 toclose++;
541 continue;
542 } else if (0 == strcmp(")", argv[i])) {
543 if (toopen || logic || igncase || NULL == cur)
544 goto fail;
545 cur->close++;
546 if (0 > --toclose)
547 goto fail;
548 continue;
549 } else if (0 == strcmp("-a", argv[i])) {
550 if (toopen || logic || igncase || NULL == cur)
551 goto fail;
552 logic = 1;
553 continue;
554 } else if (0 == strcmp("-o", argv[i])) {
555 if (toopen || logic || igncase || NULL == cur)
556 goto fail;
557 logic = 2;
558 continue;
559 } else if (0 == strcmp("-i", argv[i])) {
560 if (igncase)
561 goto fail;
562 igncase = 1;
563 continue;
564 }
565 next = exprterm(search, argv[i], !igncase);
566 if (NULL == next)
567 goto fail;
568 if (NULL == first)
569 first = next;
570 else
571 cur->next = next;
572
573 /*
574 * Searching for descriptions must be split out
575 * because they are stored in the mpages table,
576 * not in the keys table.
577 */
578
579 if (TYPE_Nd & next->bits && ~TYPE_Nd & next->bits) {
580 cur = mandoc_calloc(1, sizeof(struct expr));
581 memcpy(cur, next, sizeof(struct expr));
582 next->open = 1;
583 next->bits = TYPE_Nd;
584 next->next = cur;
585 cur->bits &= ~TYPE_Nd;
586 cur->close = 1;
587 } else
588 cur = next;
589 next->and = (1 == logic);
590 next->open += toopen;
591 toopen = logic = igncase = 0;
592 }
593 if (toopen || logic || igncase || toclose)
594 goto fail;
595
596 cur->close++;
597 cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
598 exprspec(cur, TYPE_sec, search->sec, "^%s$");
599
600 return(first);
601
602 fail:
603 if (NULL != first)
604 exprfree(first);
605 return(NULL);
606 }
607
608 static struct expr *
609 exprspec(struct expr *cur, uint64_t key, const char *value,
610 const char *format)
611 {
612 char errbuf[BUFSIZ];
613 char *cp;
614 int irc;
615
616 if (NULL == value)
617 return(cur);
618
619 mandoc_asprintf(&cp, format, value);
620 cur->next = mandoc_calloc(1, sizeof(struct expr));
621 cur = cur->next;
622 cur->and = 1;
623 cur->bits = key;
624 if (0 != (irc = regcomp(&cur->regexp, cp,
625 REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
626 regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
627 fprintf(stderr, "regcomp: %s\n", errbuf);
628 cur->substr = value;
629 }
630 free(cp);
631 return(cur);
632 }
633
634 static struct expr *
635 exprterm(const struct mansearch *search, char *buf, int cs)
636 {
637 char errbuf[BUFSIZ];
638 struct expr *e;
639 char *key, *v;
640 uint64_t iterbit;
641 int i, irc;
642
643 if ('\0' == *buf)
644 return(NULL);
645
646 e = mandoc_calloc(1, sizeof(struct expr));
647
648 /*"whatis" mode uses an opaque string and default fields. */
649
650 if (MANSEARCH_WHATIS & search->flags) {
651 e->substr = buf;
652 e->bits = search->deftype;
653 return(e);
654 }
655
656 /*
657 * If no =~ is specified, search with equality over names and
658 * descriptions.
659 * If =~ begins the phrase, use name and description fields.
660 */
661
662 if (NULL == (v = strpbrk(buf, "=~"))) {
663 e->substr = buf;
664 e->bits = search->deftype;
665 return(e);
666 } else if (v == buf)
667 e->bits = search->deftype;
668
669 if ('~' == *v++) {
670 if (NULL != strstr(buf, "arch"))
671 cs = 0;
672 if (0 != (irc = regcomp(&e->regexp, v,
673 REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE)))) {
674 regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
675 fprintf(stderr, "regcomp: %s\n", errbuf);
676 free(e);
677 return(NULL);
678 }
679 } else
680 e->substr = v;
681 v[-1] = '\0';
682
683 /*
684 * Parse out all possible fields.
685 * If the field doesn't resolve, bail.
686 */
687
688 while (NULL != (key = strsep(&buf, ","))) {
689 if ('\0' == *key)
690 continue;
691 for (i = 0, iterbit = 1;
692 i < mansearch_keymax;
693 i++, iterbit <<= 1) {
694 if (0 == strcasecmp(key,
695 mansearch_keynames[i])) {
696 e->bits |= iterbit;
697 break;
698 }
699 }
700 if (i == mansearch_keymax) {
701 if (strcasecmp(key, "any")) {
702 free(e);
703 return(NULL);
704 }
705 e->bits |= ~0ULL;
706 }
707 }
708
709 return(e);
710 }
711
712 static void
713 exprfree(struct expr *p)
714 {
715 struct expr *pp;
716
717 while (NULL != p) {
718 pp = p->next;
719 free(p);
720 p = pp;
721 }
722 }
723
724 static void *
725 hash_halloc(size_t sz, void *arg)
726 {
727
728 return(mandoc_calloc(sz, 1));
729 }
730
731 static void *
732 hash_alloc(size_t sz, void *arg)
733 {
734
735 return(mandoc_malloc(sz));
736 }
737
738 static void
739 hash_free(void *p, size_t sz, void *arg)
740 {
741
742 free(p);
743 }