]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $Id: mansearch.c,v 1.21 2014/01/19 23:09:30 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=?",
260 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
262 c
= sqlite3_prepare_v2(db
,
263 "SELECT * FROM keys WHERE pageid=? AND bits & ?",
266 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
268 for (mp
= ohash_first(&htab
, &idx
);
270 mp
= ohash_next(&htab
, &idx
)) {
271 if (cur
+ 1 > maxres
) {
273 *res
= mandoc_realloc
274 (*res
, maxres
* sizeof(struct manpage
));
277 mpage
->form
= mp
->form
;
278 buildnames(mpage
, db
, s
, mp
->id
,
279 paths
->paths
[i
], mp
->form
);
280 mpage
->output
= outbit
?
281 buildoutput(db
, s2
, mp
->id
, outbit
) : NULL
;
288 sqlite3_finalize(s2
);
303 buildnames(struct manpage
*mpage
, sqlite3
*db
, sqlite3_stmt
*s
,
304 uint64_t id
, const char *path
, int form
)
307 const char *oldnames
, *sep1
, *name
, *sec
, *sep2
, *arch
, *fsec
;
313 SQL_BIND_INT64(db
, s
, i
, id
);
314 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
316 /* Assemble the list of names. */
318 if (NULL
== mpage
->names
) {
322 oldnames
= mpage
->names
;
325 sec
= sqlite3_column_text(s
, 0);
326 arch
= sqlite3_column_text(s
, 1);
327 name
= sqlite3_column_text(s
, 2);
328 sep2
= '\0' == *arch
? "" : "/";
329 if (-1 == asprintf(&newnames
, "%s%s%s(%s%s%s)",
330 oldnames
, sep1
, name
, sec
, sep2
, arch
)) {
332 exit((int)MANDOCLEVEL_SYSERR
);
335 mpage
->names
= newnames
;
337 /* Also save the first file name encountered. */
339 if (NULL
!= mpage
->file
)
349 if (-1 == asprintf(&mpage
->file
, "%s/%s%s%s%s/%s.%s",
350 path
, sep1
, sec
, sep2
, arch
, name
, fsec
)) {
352 exit((int)MANDOCLEVEL_SYSERR
);
355 if (SQLITE_DONE
!= c
)
356 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
361 buildoutput(sqlite3
*db
, sqlite3_stmt
*s
, uint64_t id
, uint64_t outbit
)
363 char *output
, *newoutput
;
364 const char *oldoutput
, *sep1
, *data
;
370 SQL_BIND_INT64(db
, s
, i
, id
);
371 SQL_BIND_INT64(db
, s
, i
, outbit
);
372 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
373 if (NULL
== output
) {
380 data
= sqlite3_column_text(s
, 1);
381 if (-1 == asprintf(&newoutput
, "%s%s%s",
382 oldoutput
, sep1
, data
)) {
384 exit((int)MANDOCLEVEL_SYSERR
);
389 if (SQLITE_DONE
!= c
)
390 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
396 * Implement substring match as an application-defined SQL function.
397 * Using the SQL LIKE or GLOB operators instead would be a bad idea
398 * because that would require escaping metacharacters in the string
399 * being searched for.
402 sql_match(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
406 sqlite3_result_int(context
, NULL
!= strcasestr(
407 (const char *)sqlite3_value_text(argv
[1]),
408 (const char *)sqlite3_value_text(argv
[0])));
412 * Implement regular expression match
413 * as an application-defined SQL function.
416 sql_regexp(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
420 sqlite3_result_int(context
, !regexec(
421 (regex_t
*)sqlite3_value_blob(argv
[0]),
422 (const char *)sqlite3_value_text(argv
[1]),
427 sql_append(char **sql
, size_t *sz
, const char *newstr
, int count
)
431 newsz
= 1 < count
? (size_t)count
: strlen(newstr
);
432 *sql
= mandoc_realloc(*sql
, *sz
+ newsz
+ 1);
434 memset(*sql
+ *sz
, *newstr
, (size_t)count
);
436 memcpy(*sql
+ *sz
, newstr
, newsz
);
442 * Prepare the search SQL statement.
445 sql_statement(const struct expr
*e
)
451 sql
= mandoc_strdup("SELECT * FROM mpages WHERE ");
454 for (needop
= 0; NULL
!= e
; e
= e
->next
) {
456 sql_append(&sql
, &sz
, " AND ", 1);
458 sql_append(&sql
, &sz
, " OR ", 1);
460 sql_append(&sql
, &sz
, "(", e
->open
);
461 sql_append(&sql
, &sz
, NULL
== e
->substr
?
462 "id IN (SELECT pageid FROM keys "
463 "WHERE key REGEXP ? AND bits & ?)" :
464 "id IN (SELECT pageid FROM keys "
465 "WHERE key MATCH ? AND bits & ?)", 1);
467 sql_append(&sql
, &sz
, ")", e
->close
);
475 * Compile a set of string tokens into an expression.
476 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
477 * "(", "foo=bar", etc.).
480 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[])
482 int i
, toopen
, logic
, igncase
, toclose
;
483 struct expr
*first
, *next
, *cur
;
486 logic
= igncase
= toclose
= 0;
489 for (i
= 0; i
< argc
; i
++) {
490 if (0 == strcmp("(", argv
[i
])) {
496 } else if (0 == strcmp(")", argv
[i
])) {
497 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
503 } else if (0 == strcmp("-a", argv
[i
])) {
504 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
508 } else if (0 == strcmp("-o", argv
[i
])) {
509 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
513 } else if (0 == strcmp("-i", argv
[i
])) {
519 next
= exprterm(search
, argv
[i
], !igncase
);
523 next
->and = (1 == logic
);
529 toopen
= logic
= igncase
= 0;
531 if (toopen
|| logic
|| igncase
|| toclose
)
535 cur
= exprspec(cur
, TYPE_arch
, search
->arch
, "^(%s|any)$");
536 exprspec(cur
, TYPE_sec
, search
->sec
, "^%s$");
547 exprspec(struct expr
*cur
, uint64_t key
, const char *value
,
557 if (-1 == asprintf(&cp
, format
, value
)) {
559 exit((int)MANDOCLEVEL_SYSERR
);
561 cur
->next
= mandoc_calloc(1, sizeof(struct expr
));
565 if (0 != (irc
= regcomp(&cur
->regexp
, cp
,
566 REG_EXTENDED
| REG_NOSUB
| REG_ICASE
))) {
567 regerror(irc
, &cur
->regexp
, errbuf
, sizeof(errbuf
));
568 fprintf(stderr
, "regcomp: %s\n", errbuf
);
576 exprterm(const struct mansearch
*search
, char *buf
, int cs
)
587 e
= mandoc_calloc(1, sizeof(struct expr
));
589 /*"whatis" mode uses an opaque string and default fields. */
591 if (MANSEARCH_WHATIS
& search
->flags
) {
593 e
->bits
= search
->deftype
;
598 * If no =~ is specified, search with equality over names and
600 * If =~ begins the phrase, use name and description fields.
603 if (NULL
== (v
= strpbrk(buf
, "=~"))) {
605 e
->bits
= search
->deftype
;
608 e
->bits
= search
->deftype
;
611 if (NULL
!= strstr(buf
, "arch"))
613 if (0 != (irc
= regcomp(&e
->regexp
, v
,
614 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
)))) {
615 regerror(irc
, &e
->regexp
, errbuf
, sizeof(errbuf
));
616 fprintf(stderr
, "regcomp: %s\n", errbuf
);
625 * Parse out all possible fields.
626 * If the field doesn't resolve, bail.
629 while (NULL
!= (key
= strsep(&buf
, ","))) {
632 for (i
= 0, iterbit
= 1;
633 i
< mansearch_keymax
;
634 i
++, iterbit
<<= 1) {
635 if (0 == strcasecmp(key
,
636 mansearch_keynames
[i
])) {
641 if (i
== mansearch_keymax
) {
642 if (strcasecmp(key
, "any")) {
654 exprfree(struct expr
*p
)
666 hash_halloc(size_t sz
, void *arg
)
669 return(mandoc_calloc(sz
, 1));
673 hash_alloc(size_t sz
, void *arg
)
676 return(mandoc_malloc(sz
));
680 hash_free(void *p
, size_t sz
, void *arg
)