]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $Id: mansearch.c,v 1.22 2014/03/17 16:31:44 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.
37 #include "compat_ohash.h"
43 #include "mansearch.h"
45 extern int mansearch_keymax
;
46 extern const char *const mansearch_keynames
[];
48 #define SQL_BIND_TEXT(_db, _s, _i, _v) \
49 do { if (SQLITE_OK != sqlite3_bind_text \
50 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
51 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
53 #define SQL_BIND_INT64(_db, _s, _i, _v) \
54 do { if (SQLITE_OK != sqlite3_bind_int64 \
55 ((_s), (_i)++, (_v))) \
56 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
58 #define SQL_BIND_BLOB(_db, _s, _i, _v) \
59 do { if (SQLITE_OK != sqlite3_bind_blob \
60 ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
61 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
65 uint64_t bits
; /* type-mask */
66 const char *substr
; /* to search for, if applicable */
67 regex_t regexp
; /* compiled regexp, if applicable */
68 int open
; /* opening parentheses before */
69 int and; /* logical AND before */
70 int close
; /* closing parentheses after */
71 struct expr
*next
; /* next in sequence */
75 uint64_t id
; /* identifier in database */
76 int form
; /* 0 == catpage */
79 static void buildnames(struct manpage
*, sqlite3
*,
80 sqlite3_stmt
*, uint64_t,
81 const char *, int form
);
82 static char *buildoutput(sqlite3
*, sqlite3_stmt
*,
84 static void *hash_alloc(size_t, void *);
85 static void hash_free(void *, size_t, void *);
86 static void *hash_halloc(size_t, void *);
87 static struct expr
*exprcomp(const struct mansearch
*,
89 static void exprfree(struct expr
*);
90 static struct expr
*exprspec(struct expr
*, uint64_t,
91 const char *, const char *);
92 static struct expr
*exprterm(const struct mansearch
*, char *, int);
93 static void sql_append(char **sql
, size_t *sz
,
94 const char *newstr
, int count
);
95 static void sql_match(sqlite3_context
*context
,
96 int argc
, sqlite3_value
**argv
);
97 static void sql_regexp(sqlite3_context
*context
,
98 int argc
, sqlite3_value
**argv
);
99 static char *sql_statement(const struct expr
*);
102 mansearch(const struct mansearch
*search
,
103 const struct manpaths
*paths
,
104 int argc
, char *argv
[],
106 struct manpage
**res
, size_t *sz
)
108 int fd
, rc
, c
, indexbit
;
110 uint64_t outbit
, iterbit
;
113 struct manpage
*mpage
;
116 sqlite3_stmt
*s
, *s2
;
118 struct ohash_info info
;
121 size_t i
, j
, cur
, maxres
;
123 memset(&info
, 0, sizeof(struct ohash_info
));
125 info
.halloc
= hash_halloc
;
126 info
.alloc
= hash_alloc
;
127 info
.hfree
= hash_free
;
128 info
.key_offset
= offsetof(struct match
, id
);
130 *sz
= cur
= maxres
= 0;
139 if (NULL
== (e
= exprcomp(search
, argc
, argv
)))
143 if (NULL
!= outkey
) {
144 for (indexbit
= 0, iterbit
= 1;
145 indexbit
< mansearch_keymax
;
146 indexbit
++, iterbit
<<= 1) {
147 if (0 == strcasecmp(outkey
,
148 mansearch_keynames
[indexbit
])) {
156 * Save a descriptor to the current working directory.
157 * Since pathnames in the "paths" variable might be relative,
158 * and we'll be chdir()ing into them, we need to keep a handle
159 * on our current directory from which to start the chdir().
162 if (NULL
== getcwd(buf
, PATH_MAX
)) {
165 } else if (-1 == (fd
= open(buf
, O_RDONLY
, 0))) {
170 sql
= sql_statement(e
);
173 * Loop over the directories (containing databases) for us to
175 * Don't let missing/bad databases/directories phase us.
176 * In each, try to open the resident database and, if it opens,
177 * scan it for our match expression.
180 for (i
= 0; i
< paths
->sz
; i
++) {
181 if (-1 == fchdir(fd
)) {
185 } else if (-1 == chdir(paths
->paths
[i
])) {
186 perror(paths
->paths
[i
]);
192 SQLITE_OPEN_READONLY
, NULL
);
194 if (SQLITE_OK
!= c
) {
201 * Define the SQL functions for substring
202 * and regular expression matching.
205 c
= sqlite3_create_function(db
, "match", 2,
206 SQLITE_ANY
, NULL
, sql_match
, NULL
, NULL
);
207 assert(SQLITE_OK
== c
);
208 c
= sqlite3_create_function(db
, "regexp", 2,
209 SQLITE_ANY
, NULL
, sql_regexp
, NULL
, NULL
);
210 assert(SQLITE_OK
== c
);
213 c
= sqlite3_prepare_v2(db
, sql
, -1, &s
, NULL
);
215 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
217 for (ep
= e
; NULL
!= ep
; ep
= ep
->next
) {
218 if (NULL
== ep
->substr
) {
219 SQL_BIND_BLOB(db
, s
, j
, ep
->regexp
);
221 SQL_BIND_TEXT(db
, s
, j
, ep
->substr
);
222 SQL_BIND_INT64(db
, s
, j
, ep
->bits
);
225 memset(&htab
, 0, sizeof(struct ohash
));
226 ohash_init(&htab
, 4, &info
);
229 * Hash each entry on its [unique] document identifier.
230 * This is a uint64_t.
231 * Instead of using a hash function, simply convert the
232 * uint64_t to a uint32_t, the hash value's type.
233 * This gives good performance and preserves the
234 * distribution of buckets in the table.
236 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
237 id
= sqlite3_column_int64(s
, 1);
238 idx
= ohash_lookup_memory
240 sizeof(uint64_t), (uint32_t)id
);
242 if (NULL
!= ohash_find(&htab
, idx
))
245 mp
= mandoc_calloc(1, sizeof(struct match
));
247 mp
->form
= sqlite3_column_int(s
, 0);
248 ohash_insert(&htab
, idx
, mp
);
251 if (SQLITE_DONE
!= c
)
252 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
256 c
= sqlite3_prepare_v2(db
,
257 "SELECT * FROM mlinks WHERE pageid=?"
258 " ORDER BY sec, arch, name",
261 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
263 c
= sqlite3_prepare_v2(db
,
264 "SELECT * FROM keys WHERE pageid=? AND bits & ?",
267 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
269 for (mp
= ohash_first(&htab
, &idx
);
271 mp
= ohash_next(&htab
, &idx
)) {
272 if (cur
+ 1 > maxres
) {
274 *res
= mandoc_realloc
275 (*res
, maxres
* sizeof(struct manpage
));
278 mpage
->form
= mp
->form
;
279 buildnames(mpage
, db
, s
, mp
->id
,
280 paths
->paths
[i
], mp
->form
);
281 mpage
->output
= outbit
?
282 buildoutput(db
, s2
, mp
->id
, outbit
) : NULL
;
289 sqlite3_finalize(s2
);
304 buildnames(struct manpage
*mpage
, sqlite3
*db
, sqlite3_stmt
*s
,
305 uint64_t id
, const char *path
, int form
)
307 char *newnames
, *prevsec
, *prevarch
;
308 const char *oldnames
, *sep1
, *name
, *sec
, *sep2
, *arch
, *fsec
;
313 prevsec
= prevarch
= NULL
;
315 SQL_BIND_INT64(db
, s
, i
, id
);
316 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
318 /* Decide whether we already have some names. */
320 if (NULL
== mpage
->names
) {
324 oldnames
= mpage
->names
;
328 /* Fetch the next name. */
330 sec
= sqlite3_column_text(s
, 0);
331 arch
= sqlite3_column_text(s
, 1);
332 name
= sqlite3_column_text(s
, 2);
334 /* If the section changed, append the old one. */
336 if (NULL
!= prevsec
&&
337 (strcmp(sec
, prevsec
) ||
338 strcmp(arch
, prevarch
))) {
339 sep2
= '\0' == *prevarch
? "" : "/";
340 if (-1 == asprintf(&newnames
, "%s(%s%s%s)",
341 oldnames
, prevsec
, sep2
, prevarch
)) {
343 exit((int)MANDOCLEVEL_SYSERR
);
346 oldnames
= mpage
->names
= newnames
;
349 prevsec
= prevarch
= NULL
;
352 /* Save the new section, to append it later. */
354 if (NULL
== prevsec
) {
355 prevsec
= mandoc_strdup(sec
);
356 prevarch
= mandoc_strdup(arch
);
359 /* Append the new name. */
361 if (-1 == asprintf(&newnames
, "%s%s%s",
362 oldnames
, sep1
, name
)) {
364 exit((int)MANDOCLEVEL_SYSERR
);
367 mpage
->names
= newnames
;
369 /* Also save the first file name encountered. */
371 if (NULL
!= mpage
->file
)
381 sep2
= '\0' == *arch
? "" : "/";
382 if (-1 == asprintf(&mpage
->file
, "%s/%s%s%s%s/%s.%s",
383 path
, sep1
, sec
, sep2
, arch
, name
, fsec
)) {
385 exit((int)MANDOCLEVEL_SYSERR
);
388 if (SQLITE_DONE
!= c
)
389 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
392 /* Append one final section to the names. */
394 if (NULL
!= prevsec
) {
395 sep2
= '\0' == *prevarch
? "" : "/";
396 if (-1 == asprintf(&newnames
, "%s(%s%s%s)",
397 mpage
->names
, prevsec
, sep2
, prevarch
)) {
399 exit((int)MANDOCLEVEL_SYSERR
);
402 mpage
->names
= newnames
;
409 buildoutput(sqlite3
*db
, sqlite3_stmt
*s
, uint64_t id
, uint64_t outbit
)
411 char *output
, *newoutput
;
412 const char *oldoutput
, *sep1
, *data
;
418 SQL_BIND_INT64(db
, s
, i
, id
);
419 SQL_BIND_INT64(db
, s
, i
, outbit
);
420 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
421 if (NULL
== output
) {
428 data
= sqlite3_column_text(s
, 1);
429 if (-1 == asprintf(&newoutput
, "%s%s%s",
430 oldoutput
, sep1
, data
)) {
432 exit((int)MANDOCLEVEL_SYSERR
);
437 if (SQLITE_DONE
!= c
)
438 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
444 * Implement substring match as an application-defined SQL function.
445 * Using the SQL LIKE or GLOB operators instead would be a bad idea
446 * because that would require escaping metacharacters in the string
447 * being searched for.
450 sql_match(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
454 sqlite3_result_int(context
, NULL
!= strcasestr(
455 (const char *)sqlite3_value_text(argv
[1]),
456 (const char *)sqlite3_value_text(argv
[0])));
460 * Implement regular expression match
461 * as an application-defined SQL function.
464 sql_regexp(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
468 sqlite3_result_int(context
, !regexec(
469 (regex_t
*)sqlite3_value_blob(argv
[0]),
470 (const char *)sqlite3_value_text(argv
[1]),
475 sql_append(char **sql
, size_t *sz
, const char *newstr
, int count
)
479 newsz
= 1 < count
? (size_t)count
: strlen(newstr
);
480 *sql
= mandoc_realloc(*sql
, *sz
+ newsz
+ 1);
482 memset(*sql
+ *sz
, *newstr
, (size_t)count
);
484 memcpy(*sql
+ *sz
, newstr
, newsz
);
490 * Prepare the search SQL statement.
493 sql_statement(const struct expr
*e
)
499 sql
= mandoc_strdup("SELECT * FROM mpages WHERE ");
502 for (needop
= 0; NULL
!= e
; e
= e
->next
) {
504 sql_append(&sql
, &sz
, " AND ", 1);
506 sql_append(&sql
, &sz
, " OR ", 1);
508 sql_append(&sql
, &sz
, "(", e
->open
);
509 sql_append(&sql
, &sz
, NULL
== e
->substr
?
510 "id IN (SELECT pageid FROM keys "
511 "WHERE key REGEXP ? AND bits & ?)" :
512 "id IN (SELECT pageid FROM keys "
513 "WHERE key MATCH ? AND bits & ?)", 1);
515 sql_append(&sql
, &sz
, ")", e
->close
);
523 * Compile a set of string tokens into an expression.
524 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
525 * "(", "foo=bar", etc.).
528 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[])
530 int i
, toopen
, logic
, igncase
, toclose
;
531 struct expr
*first
, *next
, *cur
;
534 logic
= igncase
= toclose
= 0;
537 for (i
= 0; i
< argc
; i
++) {
538 if (0 == strcmp("(", argv
[i
])) {
544 } else if (0 == strcmp(")", argv
[i
])) {
545 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
551 } else if (0 == strcmp("-a", argv
[i
])) {
552 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
556 } else if (0 == strcmp("-o", argv
[i
])) {
557 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
561 } else if (0 == strcmp("-i", argv
[i
])) {
567 next
= exprterm(search
, argv
[i
], !igncase
);
571 next
->and = (1 == logic
);
577 toopen
= logic
= igncase
= 0;
579 if (toopen
|| logic
|| igncase
|| toclose
)
583 cur
= exprspec(cur
, TYPE_arch
, search
->arch
, "^(%s|any)$");
584 exprspec(cur
, TYPE_sec
, search
->sec
, "^%s$");
595 exprspec(struct expr
*cur
, uint64_t key
, const char *value
,
605 if (-1 == asprintf(&cp
, format
, value
)) {
607 exit((int)MANDOCLEVEL_SYSERR
);
609 cur
->next
= mandoc_calloc(1, sizeof(struct expr
));
613 if (0 != (irc
= regcomp(&cur
->regexp
, cp
,
614 REG_EXTENDED
| REG_NOSUB
| REG_ICASE
))) {
615 regerror(irc
, &cur
->regexp
, errbuf
, sizeof(errbuf
));
616 fprintf(stderr
, "regcomp: %s\n", errbuf
);
624 exprterm(const struct mansearch
*search
, char *buf
, int cs
)
635 e
= mandoc_calloc(1, sizeof(struct expr
));
637 /*"whatis" mode uses an opaque string and default fields. */
639 if (MANSEARCH_WHATIS
& search
->flags
) {
641 e
->bits
= search
->deftype
;
646 * If no =~ is specified, search with equality over names and
648 * If =~ begins the phrase, use name and description fields.
651 if (NULL
== (v
= strpbrk(buf
, "=~"))) {
653 e
->bits
= search
->deftype
;
656 e
->bits
= search
->deftype
;
659 if (NULL
!= strstr(buf
, "arch"))
661 if (0 != (irc
= regcomp(&e
->regexp
, v
,
662 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
)))) {
663 regerror(irc
, &e
->regexp
, errbuf
, sizeof(errbuf
));
664 fprintf(stderr
, "regcomp: %s\n", errbuf
);
673 * Parse out all possible fields.
674 * If the field doesn't resolve, bail.
677 while (NULL
!= (key
= strsep(&buf
, ","))) {
680 for (i
= 0, iterbit
= 1;
681 i
< mansearch_keymax
;
682 i
++, iterbit
<<= 1) {
683 if (0 == strcasecmp(key
,
684 mansearch_keynames
[i
])) {
689 if (i
== mansearch_keymax
) {
690 if (strcasecmp(key
, "any")) {
702 exprfree(struct expr
*p
)
714 hash_halloc(size_t sz
, void *arg
)
717 return(mandoc_calloc(sz
, 1));
721 hash_alloc(size_t sz
, void *arg
)
724 return(mandoc_malloc(sz
));
728 hash_free(void *p
, size_t sz
, void *arg
)