]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
f9892218fb6f3da292eef02c37c34ac7e917f5ae
1 /* $Id: mansearch.c,v 1.36 2014/04/23 21:06:41 schwarze Exp $ */
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
38 #include "compat_ohash.h"
43 #include "mandoc_aux.h"
45 #include "mansearch.h"
47 extern int mansearch_keymax
;
48 extern const char *const mansearch_keynames
[];
50 #define SQL_BIND_TEXT(_db, _s, _i, _v) \
51 do { if (SQLITE_OK != sqlite3_bind_text \
52 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
53 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
55 #define SQL_BIND_INT64(_db, _s, _i, _v) \
56 do { if (SQLITE_OK != sqlite3_bind_int64 \
57 ((_s), (_i)++, (_v))) \
58 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
60 #define SQL_BIND_BLOB(_db, _s, _i, _v) \
61 do { if (SQLITE_OK != sqlite3_bind_blob \
62 ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
63 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
67 uint64_t bits
; /* type-mask */
68 const char *substr
; /* to search for, if applicable */
69 regex_t regexp
; /* compiled regexp, if applicable */
70 int open
; /* opening parentheses before */
71 int and; /* logical AND before */
72 int close
; /* closing parentheses after */
73 struct expr
*next
; /* next in sequence */
77 uint64_t pageid
; /* identifier in database */
78 char *desc
; /* manual page description */
79 int form
; /* 0 == catpage */
82 static void buildnames(struct manpage
*, sqlite3
*,
83 sqlite3_stmt
*, uint64_t,
84 const char *, int form
);
85 static char *buildoutput(sqlite3
*, sqlite3_stmt
*,
87 static void *hash_alloc(size_t, void *);
88 static void hash_free(void *, size_t, void *);
89 static void *hash_halloc(size_t, void *);
90 static struct expr
*exprcomp(const struct mansearch
*,
92 static void exprfree(struct expr
*);
93 static struct expr
*exprspec(struct expr
*, uint64_t,
94 const char *, const char *);
95 static struct expr
*exprterm(const struct mansearch
*, char *, int);
96 static void sql_append(char **sql
, size_t *sz
,
97 const char *newstr
, int count
);
98 static void sql_match(sqlite3_context
*context
,
99 int argc
, sqlite3_value
**argv
);
100 static void sql_regexp(sqlite3_context
*context
,
101 int argc
, sqlite3_value
**argv
);
102 static char *sql_statement(const struct expr
*);
106 mansearch_setup(int start
)
108 static void *pagecache
;
111 #define PC_PAGESIZE 1280
112 #define PC_NUMPAGES 256
115 if (NULL
!= pagecache
) {
116 fprintf(stderr
, "pagecache already enabled\n");
117 return((int)MANDOCLEVEL_BADARG
);
120 pagecache
= mmap(NULL
, PC_PAGESIZE
* PC_NUMPAGES
,
121 PROT_READ
| PROT_WRITE
, MAP_ANON
, -1, 0);
123 if (MAP_FAILED
== pagecache
) {
126 return((int)MANDOCLEVEL_SYSERR
);
129 c
= sqlite3_config(SQLITE_CONFIG_PAGECACHE
,
130 pagecache
, PC_PAGESIZE
, PC_NUMPAGES
);
133 return((int)MANDOCLEVEL_OK
);
135 fprintf(stderr
, "pagecache: %s\n", sqlite3_errstr(c
));
137 } else if (NULL
== pagecache
) {
138 fprintf(stderr
, "pagecache missing\n");
139 return((int)MANDOCLEVEL_BADARG
);
142 if (-1 == munmap(pagecache
, PC_PAGESIZE
* PC_NUMPAGES
)) {
145 return((int)MANDOCLEVEL_SYSERR
);
149 return((int)MANDOCLEVEL_OK
);
153 mansearch(const struct mansearch
*search
,
154 const struct manpaths
*paths
,
155 int argc
, char *argv
[],
157 struct manpage
**res
, size_t *sz
)
159 int fd
, rc
, c
, indexbit
;
161 uint64_t outbit
, iterbit
;
164 struct manpage
*mpage
;
167 sqlite3_stmt
*s
, *s2
;
169 struct ohash_info info
;
172 size_t i
, j
, cur
, maxres
;
174 memset(&info
, 0, sizeof(struct ohash_info
));
176 info
.halloc
= hash_halloc
;
177 info
.alloc
= hash_alloc
;
178 info
.hfree
= hash_free
;
179 info
.key_offset
= offsetof(struct match
, pageid
);
181 *sz
= cur
= maxres
= 0;
190 if (NULL
== (e
= exprcomp(search
, argc
, argv
)))
194 if (NULL
!= outkey
) {
195 for (indexbit
= 0, iterbit
= 1;
196 indexbit
< mansearch_keymax
;
197 indexbit
++, iterbit
<<= 1) {
198 if (0 == strcasecmp(outkey
,
199 mansearch_keynames
[indexbit
])) {
207 * Save a descriptor to the current working directory.
208 * Since pathnames in the "paths" variable might be relative,
209 * and we'll be chdir()ing into them, we need to keep a handle
210 * on our current directory from which to start the chdir().
213 if (NULL
== getcwd(buf
, PATH_MAX
)) {
216 } else if (-1 == (fd
= open(buf
, O_RDONLY
, 0))) {
221 sql
= sql_statement(e
);
224 * Loop over the directories (containing databases) for us to
226 * Don't let missing/bad databases/directories phase us.
227 * In each, try to open the resident database and, if it opens,
228 * scan it for our match expression.
231 for (i
= 0; i
< paths
->sz
; i
++) {
232 if (-1 == fchdir(fd
)) {
236 } else if (-1 == chdir(paths
->paths
[i
])) {
237 perror(paths
->paths
[i
]);
241 c
= sqlite3_open_v2(MANDOC_DB
, &db
,
242 SQLITE_OPEN_READONLY
, NULL
);
244 if (SQLITE_OK
!= c
) {
251 * Define the SQL functions for substring
252 * and regular expression matching.
255 c
= sqlite3_create_function(db
, "match", 2,
256 SQLITE_UTF8
| SQLITE_DETERMINISTIC
,
257 NULL
, sql_match
, NULL
, NULL
);
258 assert(SQLITE_OK
== c
);
259 c
= sqlite3_create_function(db
, "regexp", 2,
260 SQLITE_UTF8
| SQLITE_DETERMINISTIC
,
261 NULL
, sql_regexp
, NULL
, NULL
);
262 assert(SQLITE_OK
== c
);
265 c
= sqlite3_prepare_v2(db
, sql
, -1, &s
, NULL
);
267 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
269 for (ep
= e
; NULL
!= ep
; ep
= ep
->next
) {
270 if (NULL
== ep
->substr
) {
271 SQL_BIND_BLOB(db
, s
, j
, ep
->regexp
);
273 SQL_BIND_TEXT(db
, s
, j
, ep
->substr
);
274 if (0 == ((TYPE_Nd
| TYPE_Nm
) & ep
->bits
))
275 SQL_BIND_INT64(db
, s
, j
, ep
->bits
);
278 memset(&htab
, 0, sizeof(struct ohash
));
279 ohash_init(&htab
, 4, &info
);
282 * Hash each entry on its [unique] document identifier.
283 * This is a uint64_t.
284 * Instead of using a hash function, simply convert the
285 * uint64_t to a uint32_t, the hash value's type.
286 * This gives good performance and preserves the
287 * distribution of buckets in the table.
289 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
290 pageid
= sqlite3_column_int64(s
, 2);
291 idx
= ohash_lookup_memory(&htab
,
292 (char *)&pageid
, sizeof(uint64_t),
295 if (NULL
!= ohash_find(&htab
, idx
))
298 mp
= mandoc_calloc(1, sizeof(struct match
));
300 mp
->form
= sqlite3_column_int(s
, 1);
301 if (TYPE_Nd
== outbit
)
302 mp
->desc
= mandoc_strdup(
303 sqlite3_column_text(s
, 0));
304 ohash_insert(&htab
, idx
, mp
);
307 if (SQLITE_DONE
!= c
)
308 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
312 c
= sqlite3_prepare_v2(db
,
313 "SELECT sec, arch, name, pageid FROM mlinks "
314 "WHERE pageid=? ORDER BY sec, arch, name",
317 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
319 c
= sqlite3_prepare_v2(db
,
320 "SELECT bits, key, pageid FROM keys "
321 "WHERE pageid=? AND bits & ?",
324 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
326 for (mp
= ohash_first(&htab
, &idx
);
328 mp
= ohash_next(&htab
, &idx
)) {
329 if (cur
+ 1 > maxres
) {
331 *res
= mandoc_reallocarray(*res
,
332 maxres
, sizeof(struct manpage
));
335 mpage
->form
= mp
->form
;
336 buildnames(mpage
, db
, s
, mp
->pageid
,
337 paths
->paths
[i
], mp
->form
);
338 mpage
->output
= TYPE_Nd
& outbit
?
340 buildoutput(db
, s2
, mp
->pageid
, outbit
) : NULL
;
347 sqlite3_finalize(s2
);
354 if (-1 == fchdir(fd
))
365 buildnames(struct manpage
*mpage
, sqlite3
*db
, sqlite3_stmt
*s
,
366 uint64_t pageid
, const char *path
, int form
)
368 char *newnames
, *prevsec
, *prevarch
;
369 const char *oldnames
, *sep1
, *name
, *sec
, *sep2
, *arch
, *fsec
;
375 prevsec
= prevarch
= NULL
;
377 SQL_BIND_INT64(db
, s
, i
, pageid
);
378 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
380 /* Decide whether we already have some names. */
382 if (NULL
== mpage
->names
) {
386 oldnames
= mpage
->names
;
390 /* Fetch the next name. */
392 sec
= sqlite3_column_text(s
, 0);
393 arch
= sqlite3_column_text(s
, 1);
394 name
= sqlite3_column_text(s
, 2);
396 /* If the section changed, append the old one. */
398 if (NULL
!= prevsec
&&
399 (strcmp(sec
, prevsec
) ||
400 strcmp(arch
, prevarch
))) {
401 sep2
= '\0' == *prevarch
? "" : "/";
402 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
403 oldnames
, prevsec
, sep2
, prevarch
);
405 oldnames
= mpage
->names
= newnames
;
408 prevsec
= prevarch
= NULL
;
411 /* Save the new section, to append it later. */
413 if (NULL
== prevsec
) {
414 prevsec
= mandoc_strdup(sec
);
415 prevarch
= mandoc_strdup(arch
);
418 /* Append the new name. */
420 mandoc_asprintf(&newnames
, "%s%s%s",
421 oldnames
, sep1
, name
);
423 mpage
->names
= newnames
;
425 /* Also save the first file name encountered. */
427 if (NULL
!= mpage
->file
)
437 sep2
= '\0' == *arch
? "" : "/";
438 mandoc_asprintf(&mpage
->file
, "%s/%s%s%s%s/%s.%s",
439 path
, sep1
, sec
, sep2
, arch
, name
, fsec
);
441 if (SQLITE_DONE
!= c
)
442 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
445 /* Append one final section to the names. */
447 if (NULL
!= prevsec
) {
448 sep2
= '\0' == *prevarch
? "" : "/";
449 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
450 mpage
->names
, prevsec
, sep2
, prevarch
);
452 mpage
->names
= newnames
;
459 buildoutput(sqlite3
*db
, sqlite3_stmt
*s
, uint64_t pageid
, uint64_t outbit
)
461 char *output
, *newoutput
;
462 const char *oldoutput
, *sep1
, *data
;
468 SQL_BIND_INT64(db
, s
, i
, pageid
);
469 SQL_BIND_INT64(db
, s
, i
, outbit
);
470 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
471 if (NULL
== output
) {
478 data
= sqlite3_column_text(s
, 1);
479 mandoc_asprintf(&newoutput
, "%s%s%s",
480 oldoutput
, sep1
, data
);
484 if (SQLITE_DONE
!= c
)
485 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
491 * Implement substring match as an application-defined SQL function.
492 * Using the SQL LIKE or GLOB operators instead would be a bad idea
493 * because that would require escaping metacharacters in the string
494 * being searched for.
497 sql_match(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
501 sqlite3_result_int(context
, NULL
!= strcasestr(
502 (const char *)sqlite3_value_text(argv
[1]),
503 (const char *)sqlite3_value_text(argv
[0])));
507 * Implement regular expression match
508 * as an application-defined SQL function.
511 sql_regexp(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
515 sqlite3_result_int(context
, !regexec(
516 (regex_t
*)sqlite3_value_blob(argv
[0]),
517 (const char *)sqlite3_value_text(argv
[1]),
522 sql_append(char **sql
, size_t *sz
, const char *newstr
, int count
)
526 newsz
= 1 < count
? (size_t)count
: strlen(newstr
);
527 *sql
= mandoc_realloc(*sql
, *sz
+ newsz
+ 1);
529 memset(*sql
+ *sz
, *newstr
, (size_t)count
);
531 memcpy(*sql
+ *sz
, newstr
, newsz
);
537 * Prepare the search SQL statement.
540 sql_statement(const struct expr
*e
)
547 "SELECT desc, form, pageid FROM mpages WHERE ");
550 for (needop
= 0; NULL
!= e
; e
= e
->next
) {
552 sql_append(&sql
, &sz
, " AND ", 1);
554 sql_append(&sql
, &sz
, " OR ", 1);
556 sql_append(&sql
, &sz
, "(", e
->open
);
557 sql_append(&sql
, &sz
,
564 ? "pageid IN (SELECT pageid FROM names "
565 "WHERE name REGEXP ?)"
566 : "pageid IN (SELECT pageid FROM names "
567 "WHERE name MATCH ?)")
569 ? "pageid IN (SELECT pageid FROM keys "
570 "WHERE key REGEXP ? AND bits & ?)"
571 : "pageid IN (SELECT pageid FROM keys "
572 "WHERE key MATCH ? AND bits & ?)"), 1);
574 sql_append(&sql
, &sz
, ")", e
->close
);
582 * Compile a set of string tokens into an expression.
583 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
584 * "(", "foo=bar", etc.).
587 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[])
590 int i
, toopen
, logic
, igncase
, toclose
;
591 struct expr
*first
, *prev
, *cur
, *next
;
594 logic
= igncase
= toclose
= 0;
595 toopen
= NULL
!= search
->sec
|| NULL
!= search
->arch
;
597 for (i
= 0; i
< argc
; i
++) {
598 if (0 == strcmp("(", argv
[i
])) {
604 } else if (0 == strcmp(")", argv
[i
])) {
605 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
611 } else if (0 == strcmp("-a", argv
[i
])) {
612 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
616 } else if (0 == strcmp("-o", argv
[i
])) {
617 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
621 } else if (0 == strcmp("-i", argv
[i
])) {
627 next
= exprterm(search
, argv
[i
], !igncase
);
637 * Searching for descriptions must be split out
638 * because they are stored in the mpages table,
639 * not in the keys table.
642 for (mask
= TYPE_Nm
; mask
<= TYPE_Nd
; mask
<<= 1) {
643 if (mask
& cur
->bits
&& ~mask
& cur
->bits
) {
644 next
= mandoc_calloc(1,
645 sizeof(struct expr
));
646 memcpy(next
, cur
, sizeof(struct expr
));
654 prev
->and = (1 == logic
);
655 prev
->open
+= toopen
;
659 toopen
= logic
= igncase
= 0;
661 if (toopen
|| logic
|| igncase
|| toclose
)
664 if (NULL
!= search
->sec
|| NULL
!= search
->arch
)
666 if (NULL
!= search
->arch
)
667 cur
= exprspec(cur
, TYPE_arch
, search
->arch
, "^(%s|any)$");
668 if (NULL
!= search
->sec
)
669 exprspec(cur
, TYPE_sec
, search
->sec
, "^%s$");
680 exprspec(struct expr
*cur
, uint64_t key
, const char *value
,
687 mandoc_asprintf(&cp
, format
, value
);
688 cur
->next
= mandoc_calloc(1, sizeof(struct expr
));
692 if (0 != (irc
= regcomp(&cur
->regexp
, cp
,
693 REG_EXTENDED
| REG_NOSUB
| REG_ICASE
))) {
694 regerror(irc
, &cur
->regexp
, errbuf
, sizeof(errbuf
));
695 fprintf(stderr
, "regcomp: %s\n", errbuf
);
703 exprterm(const struct mansearch
*search
, char *buf
, int cs
)
714 e
= mandoc_calloc(1, sizeof(struct expr
));
716 /*"whatis" mode uses an opaque string and default fields. */
718 if (MANSEARCH_WHATIS
& search
->flags
) {
720 e
->bits
= search
->deftype
;
725 * If no =~ is specified, search with equality over names and
727 * If =~ begins the phrase, use name and description fields.
730 if (NULL
== (v
= strpbrk(buf
, "=~"))) {
732 e
->bits
= search
->deftype
;
735 e
->bits
= search
->deftype
;
738 if (NULL
!= strstr(buf
, "arch"))
740 if (0 != (irc
= regcomp(&e
->regexp
, v
,
741 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
)))) {
742 regerror(irc
, &e
->regexp
, errbuf
, sizeof(errbuf
));
743 fprintf(stderr
, "regcomp: %s\n", errbuf
);
752 * Parse out all possible fields.
753 * If the field doesn't resolve, bail.
756 while (NULL
!= (key
= strsep(&buf
, ","))) {
759 for (i
= 0, iterbit
= 1;
760 i
< mansearch_keymax
;
761 i
++, iterbit
<<= 1) {
762 if (0 == strcasecmp(key
,
763 mansearch_keynames
[i
])) {
768 if (i
== mansearch_keymax
) {
769 if (strcasecmp(key
, "any")) {
781 exprfree(struct expr
*p
)
793 hash_halloc(size_t sz
, void *arg
)
796 return(mandoc_calloc(1, sz
));
800 hash_alloc(size_t sz
, void *arg
)
803 return(mandoc_malloc(sz
));
807 hash_free(void *p
, size_t sz
, void *arg
)