]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $Id: mansearch.c,v 1.38 2014/07/12 14:00:25 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 regex_t regexp
; /* compiled regexp, if applicable */
68 const char *substr
; /* to search for, if applicable */
69 struct expr
*next
; /* next in sequence */
70 uint64_t bits
; /* type-mask */
71 int equal
; /* equality, not subsring match */
72 int open
; /* opening parentheses before */
73 int and; /* logical AND before */
74 int close
; /* closing parentheses after */
78 uint64_t pageid
; /* identifier in database */
79 char *desc
; /* manual page description */
80 int form
; /* 0 == catpage */
83 static void buildnames(struct manpage
*, sqlite3
*,
84 sqlite3_stmt
*, uint64_t,
85 const char *, int form
);
86 static char *buildoutput(sqlite3
*, sqlite3_stmt
*,
88 static void *hash_alloc(size_t, void *);
89 static void hash_free(void *, void *);
90 static void *hash_calloc(size_t, size_t, void *);
91 static struct expr
*exprcomp(const struct mansearch
*,
93 static void exprfree(struct expr
*);
94 static struct expr
*exprspec(struct expr
*, uint64_t,
95 const char *, const char *);
96 static struct expr
*exprterm(const struct mansearch
*, char *, int);
97 static void sql_append(char **sql
, size_t *sz
,
98 const char *newstr
, int count
);
99 static void sql_match(sqlite3_context
*context
,
100 int argc
, sqlite3_value
**argv
);
101 static void sql_regexp(sqlite3_context
*context
,
102 int argc
, sqlite3_value
**argv
);
103 static char *sql_statement(const struct expr
*);
107 mansearch_setup(int start
)
109 static void *pagecache
;
112 #define PC_PAGESIZE 1280
113 #define PC_NUMPAGES 256
116 if (NULL
!= pagecache
) {
117 fprintf(stderr
, "pagecache already enabled\n");
118 return((int)MANDOCLEVEL_BADARG
);
121 pagecache
= mmap(NULL
, PC_PAGESIZE
* PC_NUMPAGES
,
122 PROT_READ
| PROT_WRITE
, MAP_ANON
, -1, 0);
124 if (MAP_FAILED
== pagecache
) {
127 return((int)MANDOCLEVEL_SYSERR
);
130 c
= sqlite3_config(SQLITE_CONFIG_PAGECACHE
,
131 pagecache
, PC_PAGESIZE
, PC_NUMPAGES
);
134 return((int)MANDOCLEVEL_OK
);
136 fprintf(stderr
, "pagecache: %s\n", sqlite3_errstr(c
));
138 } else if (NULL
== pagecache
) {
139 fprintf(stderr
, "pagecache missing\n");
140 return((int)MANDOCLEVEL_BADARG
);
143 if (-1 == munmap(pagecache
, PC_PAGESIZE
* PC_NUMPAGES
)) {
146 return((int)MANDOCLEVEL_SYSERR
);
150 return((int)MANDOCLEVEL_OK
);
154 mansearch(const struct mansearch
*search
,
155 const struct manpaths
*paths
,
156 int argc
, char *argv
[],
158 struct manpage
**res
, size_t *sz
)
160 int fd
, rc
, c
, indexbit
;
162 uint64_t outbit
, iterbit
;
165 struct manpage
*mpage
;
168 sqlite3_stmt
*s
, *s2
;
170 struct ohash_info info
;
173 size_t i
, j
, cur
, maxres
;
175 info
.calloc
= hash_calloc
;
176 info
.alloc
= hash_alloc
;
177 info
.free
= hash_free
;
178 info
.key_offset
= offsetof(struct match
, pageid
);
180 *sz
= cur
= maxres
= 0;
189 if (NULL
== (e
= exprcomp(search
, argc
, argv
)))
193 if (NULL
!= outkey
) {
194 for (indexbit
= 0, iterbit
= 1;
195 indexbit
< mansearch_keymax
;
196 indexbit
++, iterbit
<<= 1) {
197 if (0 == strcasecmp(outkey
,
198 mansearch_keynames
[indexbit
])) {
206 * Save a descriptor to the current working directory.
207 * Since pathnames in the "paths" variable might be relative,
208 * and we'll be chdir()ing into them, we need to keep a handle
209 * on our current directory from which to start the chdir().
212 if (NULL
== getcwd(buf
, PATH_MAX
)) {
215 } else if (-1 == (fd
= open(buf
, O_RDONLY
, 0))) {
220 sql
= sql_statement(e
);
223 * Loop over the directories (containing databases) for us to
225 * Don't let missing/bad databases/directories phase us.
226 * In each, try to open the resident database and, if it opens,
227 * scan it for our match expression.
230 for (i
= 0; i
< paths
->sz
; i
++) {
231 if (-1 == fchdir(fd
)) {
235 } else if (-1 == chdir(paths
->paths
[i
])) {
236 perror(paths
->paths
[i
]);
240 c
= sqlite3_open_v2(MANDOC_DB
, &db
,
241 SQLITE_OPEN_READONLY
, NULL
);
243 if (SQLITE_OK
!= c
) {
250 * Define the SQL functions for substring
251 * and regular expression matching.
254 c
= sqlite3_create_function(db
, "match", 2,
255 SQLITE_UTF8
| SQLITE_DETERMINISTIC
,
256 NULL
, sql_match
, NULL
, NULL
);
257 assert(SQLITE_OK
== c
);
258 c
= sqlite3_create_function(db
, "regexp", 2,
259 SQLITE_UTF8
| SQLITE_DETERMINISTIC
,
260 NULL
, sql_regexp
, NULL
, NULL
);
261 assert(SQLITE_OK
== c
);
264 c
= sqlite3_prepare_v2(db
, sql
, -1, &s
, NULL
);
266 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
268 for (ep
= e
; NULL
!= ep
; ep
= ep
->next
) {
269 if (NULL
== ep
->substr
) {
270 SQL_BIND_BLOB(db
, s
, j
, ep
->regexp
);
272 SQL_BIND_TEXT(db
, s
, j
, ep
->substr
);
273 if (0 == ((TYPE_Nd
| TYPE_Nm
) & ep
->bits
))
274 SQL_BIND_INT64(db
, s
, j
, ep
->bits
);
277 memset(&htab
, 0, sizeof(struct ohash
));
278 ohash_init(&htab
, 4, &info
);
281 * Hash each entry on its [unique] document identifier.
282 * This is a uint64_t.
283 * Instead of using a hash function, simply convert the
284 * uint64_t to a uint32_t, the hash value's type.
285 * This gives good performance and preserves the
286 * distribution of buckets in the table.
288 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
289 pageid
= sqlite3_column_int64(s
, 2);
290 idx
= ohash_lookup_memory(&htab
,
291 (char *)&pageid
, sizeof(uint64_t),
294 if (NULL
!= ohash_find(&htab
, idx
))
297 mp
= mandoc_calloc(1, sizeof(struct match
));
299 mp
->form
= sqlite3_column_int(s
, 1);
300 if (TYPE_Nd
== outbit
)
301 mp
->desc
= mandoc_strdup(
302 sqlite3_column_text(s
, 0));
303 ohash_insert(&htab
, idx
, mp
);
306 if (SQLITE_DONE
!= c
)
307 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
311 c
= sqlite3_prepare_v2(db
,
312 "SELECT sec, arch, name, pageid FROM mlinks "
313 "WHERE pageid=? ORDER BY sec, arch, name",
316 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
318 c
= sqlite3_prepare_v2(db
,
319 "SELECT bits, key, pageid FROM keys "
320 "WHERE pageid=? AND bits & ?",
323 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
325 for (mp
= ohash_first(&htab
, &idx
);
327 mp
= ohash_next(&htab
, &idx
)) {
328 if (cur
+ 1 > maxres
) {
330 *res
= mandoc_reallocarray(*res
,
331 maxres
, sizeof(struct manpage
));
334 mpage
->form
= mp
->form
;
335 buildnames(mpage
, db
, s
, mp
->pageid
,
336 paths
->paths
[i
], mp
->form
);
337 mpage
->output
= TYPE_Nd
& outbit
?
339 buildoutput(db
, s2
, mp
->pageid
, outbit
) : NULL
;
346 sqlite3_finalize(s2
);
353 if (-1 == fchdir(fd
))
364 buildnames(struct manpage
*mpage
, sqlite3
*db
, sqlite3_stmt
*s
,
365 uint64_t pageid
, const char *path
, int form
)
367 char *newnames
, *prevsec
, *prevarch
;
368 const char *oldnames
, *sep1
, *name
, *sec
, *sep2
, *arch
, *fsec
;
374 prevsec
= prevarch
= NULL
;
376 SQL_BIND_INT64(db
, s
, i
, pageid
);
377 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
379 /* Decide whether we already have some names. */
381 if (NULL
== mpage
->names
) {
385 oldnames
= mpage
->names
;
389 /* Fetch the next name. */
391 sec
= sqlite3_column_text(s
, 0);
392 arch
= sqlite3_column_text(s
, 1);
393 name
= sqlite3_column_text(s
, 2);
395 /* If the section changed, append the old one. */
397 if (NULL
!= prevsec
&&
398 (strcmp(sec
, prevsec
) ||
399 strcmp(arch
, prevarch
))) {
400 sep2
= '\0' == *prevarch
? "" : "/";
401 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
402 oldnames
, prevsec
, sep2
, prevarch
);
404 oldnames
= mpage
->names
= newnames
;
407 prevsec
= prevarch
= NULL
;
410 /* Save the new section, to append it later. */
412 if (NULL
== prevsec
) {
413 prevsec
= mandoc_strdup(sec
);
414 prevarch
= mandoc_strdup(arch
);
417 /* Append the new name. */
419 mandoc_asprintf(&newnames
, "%s%s%s",
420 oldnames
, sep1
, name
);
422 mpage
->names
= newnames
;
424 /* Also save the first file name encountered. */
426 if (NULL
!= mpage
->file
)
436 sep2
= '\0' == *arch
? "" : "/";
437 mandoc_asprintf(&mpage
->file
, "%s/%s%s%s%s/%s.%s",
438 path
, sep1
, sec
, sep2
, arch
, name
, fsec
);
440 if (SQLITE_DONE
!= c
)
441 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
444 /* Append one final section to the names. */
446 if (NULL
!= prevsec
) {
447 sep2
= '\0' == *prevarch
? "" : "/";
448 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
449 mpage
->names
, prevsec
, sep2
, prevarch
);
451 mpage
->names
= newnames
;
458 buildoutput(sqlite3
*db
, sqlite3_stmt
*s
, uint64_t pageid
, uint64_t outbit
)
460 char *output
, *newoutput
;
461 const char *oldoutput
, *sep1
, *data
;
467 SQL_BIND_INT64(db
, s
, i
, pageid
);
468 SQL_BIND_INT64(db
, s
, i
, outbit
);
469 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
470 if (NULL
== output
) {
477 data
= sqlite3_column_text(s
, 1);
478 mandoc_asprintf(&newoutput
, "%s%s%s",
479 oldoutput
, sep1
, data
);
483 if (SQLITE_DONE
!= c
)
484 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
490 * Implement substring match as an application-defined SQL function.
491 * Using the SQL LIKE or GLOB operators instead would be a bad idea
492 * because that would require escaping metacharacters in the string
493 * being searched for.
496 sql_match(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
500 sqlite3_result_int(context
, NULL
!= strcasestr(
501 (const char *)sqlite3_value_text(argv
[1]),
502 (const char *)sqlite3_value_text(argv
[0])));
506 * Implement regular expression match
507 * as an application-defined SQL function.
510 sql_regexp(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
514 sqlite3_result_int(context
, !regexec(
515 (regex_t
*)sqlite3_value_blob(argv
[0]),
516 (const char *)sqlite3_value_text(argv
[1]),
521 sql_append(char **sql
, size_t *sz
, const char *newstr
, int count
)
525 newsz
= 1 < count
? (size_t)count
: strlen(newstr
);
526 *sql
= mandoc_realloc(*sql
, *sz
+ newsz
+ 1);
528 memset(*sql
+ *sz
, *newstr
, (size_t)count
);
530 memcpy(*sql
+ *sz
, newstr
, newsz
);
536 * Prepare the search SQL statement.
539 sql_statement(const struct expr
*e
)
546 "SELECT desc, form, pageid FROM mpages WHERE ");
549 for (needop
= 0; NULL
!= e
; e
= e
->next
) {
551 sql_append(&sql
, &sz
, " AND ", 1);
553 sql_append(&sql
, &sz
, " OR ", 1);
555 sql_append(&sql
, &sz
, "(", e
->open
);
556 sql_append(&sql
, &sz
,
563 ? "pageid IN (SELECT pageid FROM names "
564 "WHERE name REGEXP ?)"
566 ? "pageid IN (SELECT pageid FROM names "
568 : "pageid IN (SELECT pageid FROM names "
569 "WHERE name MATCH ?)")
571 ? "pageid IN (SELECT pageid FROM keys "
572 "WHERE key REGEXP ? AND bits & ?)"
573 : "pageid IN (SELECT pageid FROM keys "
574 "WHERE key MATCH ? AND bits & ?)"), 1);
576 sql_append(&sql
, &sz
, ")", e
->close
);
584 * Compile a set of string tokens into an expression.
585 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
586 * "(", "foo=bar", etc.).
589 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[])
592 int i
, toopen
, logic
, igncase
, toclose
;
593 struct expr
*first
, *prev
, *cur
, *next
;
596 logic
= igncase
= toclose
= 0;
597 toopen
= NULL
!= search
->sec
|| NULL
!= search
->arch
;
599 for (i
= 0; i
< argc
; i
++) {
600 if (0 == strcmp("(", argv
[i
])) {
606 } else if (0 == strcmp(")", argv
[i
])) {
607 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
613 } else if (0 == strcmp("-a", argv
[i
])) {
614 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
618 } else if (0 == strcmp("-o", argv
[i
])) {
619 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
623 } else if (0 == strcmp("-i", argv
[i
])) {
629 next
= exprterm(search
, argv
[i
], !igncase
);
639 * Searching for descriptions must be split out
640 * because they are stored in the mpages table,
641 * not in the keys table.
644 for (mask
= TYPE_Nm
; mask
<= TYPE_Nd
; mask
<<= 1) {
645 if (mask
& cur
->bits
&& ~mask
& cur
->bits
) {
646 next
= mandoc_calloc(1,
647 sizeof(struct expr
));
648 memcpy(next
, cur
, sizeof(struct expr
));
656 prev
->and = (1 == logic
);
657 prev
->open
+= toopen
;
661 toopen
= logic
= igncase
= 0;
663 if (toopen
|| logic
|| igncase
|| toclose
)
666 if (NULL
!= search
->sec
|| NULL
!= search
->arch
)
668 if (NULL
!= search
->arch
)
669 cur
= exprspec(cur
, TYPE_arch
, search
->arch
, "^(%s|any)$");
670 if (NULL
!= search
->sec
)
671 exprspec(cur
, TYPE_sec
, search
->sec
, "^%s$");
682 exprspec(struct expr
*cur
, uint64_t key
, const char *value
,
689 mandoc_asprintf(&cp
, format
, value
);
690 cur
->next
= mandoc_calloc(1, sizeof(struct expr
));
694 if (0 != (irc
= regcomp(&cur
->regexp
, cp
,
695 REG_EXTENDED
| REG_NOSUB
| REG_ICASE
))) {
696 regerror(irc
, &cur
->regexp
, errbuf
, sizeof(errbuf
));
697 fprintf(stderr
, "regcomp: %s\n", errbuf
);
705 exprterm(const struct mansearch
*search
, char *buf
, int cs
)
716 e
= mandoc_calloc(1, sizeof(struct expr
));
718 if (MANSEARCH_MAN
& search
->flags
) {
719 e
->bits
= search
->deftype
;
726 * Look for an '=' or '~' operator,
727 * unless forced to some fixed macro keys.
730 if (MANSEARCH_WHATIS
& search
->flags
)
733 val
= strpbrk(buf
, "=~");
736 e
->bits
= search
->deftype
;
741 * Regexp search is requested by !e->substr.
746 e
->bits
= search
->deftype
;
750 if (NULL
!= strstr(buf
, "arch"))
754 /* Compile regular expressions. */
756 if (MANSEARCH_WHATIS
& search
->flags
) {
758 mandoc_asprintf(&val
, "[[:<:]]%s[[:>:]]", buf
);
761 if (NULL
== e
->substr
) {
762 irc
= regcomp(&e
->regexp
, val
,
763 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
));
764 if (MANSEARCH_WHATIS
& search
->flags
)
767 regerror(irc
, &e
->regexp
, errbuf
, sizeof(errbuf
));
768 fprintf(stderr
, "regcomp: %s\n", errbuf
);
778 * Parse out all possible fields.
779 * If the field doesn't resolve, bail.
782 while (NULL
!= (key
= strsep(&buf
, ","))) {
785 for (i
= 0, iterbit
= 1;
786 i
< mansearch_keymax
;
787 i
++, iterbit
<<= 1) {
788 if (0 == strcasecmp(key
,
789 mansearch_keynames
[i
])) {
794 if (i
== mansearch_keymax
) {
795 if (strcasecmp(key
, "any")) {
807 exprfree(struct expr
*p
)
819 hash_calloc(size_t nmemb
, size_t sz
, void *arg
)
822 return(mandoc_calloc(nmemb
, sz
));
826 hash_alloc(size_t sz
, void *arg
)
829 return(mandoc_malloc(sz
));
833 hash_free(void *p
, void *arg
)