]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $Id: mansearch.c,v 1.25 2014/03/28 19:17:12 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"
42 #include "mandoc_aux.h"
44 #include "mansearch.h"
46 extern int mansearch_keymax
;
47 extern const char *const mansearch_keynames
[];
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))); \
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))); \
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))); \
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 */
76 uint64_t id
; /* identifier in database */
77 int form
; /* 0 == catpage */
80 static void buildnames(struct manpage
*, sqlite3
*,
81 sqlite3_stmt
*, uint64_t,
82 const char *, int form
);
83 static char *buildoutput(sqlite3
*, sqlite3_stmt
*,
85 static void *hash_alloc(size_t, void *);
86 static void hash_free(void *, size_t, void *);
87 static void *hash_halloc(size_t, void *);
88 static struct expr
*exprcomp(const struct mansearch
*,
90 static void exprfree(struct expr
*);
91 static struct expr
*exprspec(struct expr
*, uint64_t,
92 const char *, const char *);
93 static struct expr
*exprterm(const struct mansearch
*, char *, int);
94 static void sql_append(char **sql
, size_t *sz
,
95 const char *newstr
, int count
);
96 static void sql_match(sqlite3_context
*context
,
97 int argc
, sqlite3_value
**argv
);
98 static void sql_regexp(sqlite3_context
*context
,
99 int argc
, sqlite3_value
**argv
);
100 static char *sql_statement(const struct expr
*);
103 mansearch(const struct mansearch
*search
,
104 const struct manpaths
*paths
,
105 int argc
, char *argv
[],
107 struct manpage
**res
, size_t *sz
)
109 int fd
, rc
, c
, indexbit
;
111 uint64_t outbit
, iterbit
;
114 struct manpage
*mpage
;
117 sqlite3_stmt
*s
, *s2
;
119 struct ohash_info info
;
122 size_t i
, j
, cur
, maxres
;
124 memset(&info
, 0, sizeof(struct ohash_info
));
126 info
.halloc
= hash_halloc
;
127 info
.alloc
= hash_alloc
;
128 info
.hfree
= hash_free
;
129 info
.key_offset
= offsetof(struct match
, id
);
131 *sz
= cur
= maxres
= 0;
140 if (NULL
== (e
= exprcomp(search
, argc
, argv
)))
144 if (NULL
!= outkey
) {
145 for (indexbit
= 0, iterbit
= 1;
146 indexbit
< mansearch_keymax
;
147 indexbit
++, iterbit
<<= 1) {
148 if (0 == strcasecmp(outkey
,
149 mansearch_keynames
[indexbit
])) {
157 * Save a descriptor to the current working directory.
158 * Since pathnames in the "paths" variable might be relative,
159 * and we'll be chdir()ing into them, we need to keep a handle
160 * on our current directory from which to start the chdir().
163 if (NULL
== getcwd(buf
, PATH_MAX
)) {
166 } else if (-1 == (fd
= open(buf
, O_RDONLY
, 0))) {
171 sql
= sql_statement(e
);
174 * Loop over the directories (containing databases) for us to
176 * Don't let missing/bad databases/directories phase us.
177 * In each, try to open the resident database and, if it opens,
178 * scan it for our match expression.
181 for (i
= 0; i
< paths
->sz
; i
++) {
182 if (-1 == fchdir(fd
)) {
186 } else if (-1 == chdir(paths
->paths
[i
])) {
187 perror(paths
->paths
[i
]);
193 SQLITE_OPEN_READONLY
, NULL
);
195 if (SQLITE_OK
!= c
) {
202 * Define the SQL functions for substring
203 * and regular expression matching.
206 c
= sqlite3_create_function(db
, "match", 2,
207 SQLITE_ANY
, NULL
, sql_match
, NULL
, NULL
);
208 assert(SQLITE_OK
== c
);
209 c
= sqlite3_create_function(db
, "regexp", 2,
210 SQLITE_ANY
, NULL
, sql_regexp
, NULL
, NULL
);
211 assert(SQLITE_OK
== c
);
214 c
= sqlite3_prepare_v2(db
, sql
, -1, &s
, NULL
);
216 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
218 for (ep
= e
; NULL
!= ep
; ep
= ep
->next
) {
219 if (NULL
== ep
->substr
) {
220 SQL_BIND_BLOB(db
, s
, j
, ep
->regexp
);
222 SQL_BIND_TEXT(db
, s
, j
, ep
->substr
);
223 SQL_BIND_INT64(db
, s
, j
, ep
->bits
);
226 memset(&htab
, 0, sizeof(struct ohash
));
227 ohash_init(&htab
, 4, &info
);
230 * Hash each entry on its [unique] document identifier.
231 * This is a uint64_t.
232 * Instead of using a hash function, simply convert the
233 * uint64_t to a uint32_t, the hash value's type.
234 * This gives good performance and preserves the
235 * distribution of buckets in the table.
237 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
238 id
= sqlite3_column_int64(s
, 1);
239 idx
= ohash_lookup_memory
241 sizeof(uint64_t), (uint32_t)id
);
243 if (NULL
!= ohash_find(&htab
, idx
))
246 mp
= mandoc_calloc(1, sizeof(struct match
));
248 mp
->form
= sqlite3_column_int(s
, 0);
249 ohash_insert(&htab
, idx
, mp
);
252 if (SQLITE_DONE
!= c
)
253 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
257 c
= sqlite3_prepare_v2(db
,
258 "SELECT * FROM mlinks WHERE pageid=?"
259 " ORDER BY sec, arch, name",
262 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
264 c
= sqlite3_prepare_v2(db
,
265 "SELECT * FROM keys WHERE pageid=? AND bits & ?",
268 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
270 for (mp
= ohash_first(&htab
, &idx
);
272 mp
= ohash_next(&htab
, &idx
)) {
273 if (cur
+ 1 > maxres
) {
275 *res
= mandoc_realloc
276 (*res
, maxres
* sizeof(struct manpage
));
279 mpage
->form
= mp
->form
;
280 buildnames(mpage
, db
, s
, mp
->id
,
281 paths
->paths
[i
], mp
->form
);
282 mpage
->output
= outbit
?
283 buildoutput(db
, s2
, mp
->id
, outbit
) : NULL
;
290 sqlite3_finalize(s2
);
305 buildnames(struct manpage
*mpage
, sqlite3
*db
, sqlite3_stmt
*s
,
306 uint64_t id
, const char *path
, int form
)
308 char *newnames
, *prevsec
, *prevarch
;
309 const char *oldnames
, *sep1
, *name
, *sec
, *sep2
, *arch
, *fsec
;
315 prevsec
= prevarch
= NULL
;
317 SQL_BIND_INT64(db
, s
, i
, id
);
318 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
320 /* Decide whether we already have some names. */
322 if (NULL
== mpage
->names
) {
326 oldnames
= mpage
->names
;
330 /* Fetch the next name. */
332 sec
= sqlite3_column_text(s
, 0);
333 arch
= sqlite3_column_text(s
, 1);
334 name
= sqlite3_column_text(s
, 2);
336 /* If the section changed, append the old one. */
338 if (NULL
!= prevsec
&&
339 (strcmp(sec
, prevsec
) ||
340 strcmp(arch
, prevarch
))) {
341 sep2
= '\0' == *prevarch
? "" : "/";
342 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
343 oldnames
, prevsec
, sep2
, prevarch
);
345 oldnames
= mpage
->names
= newnames
;
348 prevsec
= prevarch
= NULL
;
351 /* Save the new section, to append it later. */
353 if (NULL
== prevsec
) {
354 prevsec
= mandoc_strdup(sec
);
355 prevarch
= mandoc_strdup(arch
);
358 /* Append the new name. */
360 mandoc_asprintf(&newnames
, "%s%s%s",
361 oldnames
, sep1
, name
);
363 mpage
->names
= newnames
;
365 /* Also save the first file name encountered. */
367 if (NULL
!= mpage
->file
)
377 sep2
= '\0' == *arch
? "" : "/";
378 mandoc_asprintf(&mpage
->file
, "%s/%s%s%s%s/%s.%s",
379 path
, sep1
, sec
, sep2
, arch
, name
, fsec
);
381 if (SQLITE_DONE
!= c
)
382 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
385 /* Append one final section to the names. */
387 if (NULL
!= prevsec
) {
388 sep2
= '\0' == *prevarch
? "" : "/";
389 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
390 mpage
->names
, prevsec
, sep2
, prevarch
);
392 mpage
->names
= newnames
;
399 buildoutput(sqlite3
*db
, sqlite3_stmt
*s
, uint64_t id
, uint64_t outbit
)
401 char *output
, *newoutput
;
402 const char *oldoutput
, *sep1
, *data
;
408 SQL_BIND_INT64(db
, s
, i
, id
);
409 SQL_BIND_INT64(db
, s
, i
, outbit
);
410 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
411 if (NULL
== output
) {
418 data
= sqlite3_column_text(s
, 1);
419 mandoc_asprintf(&newoutput
, "%s%s%s",
420 oldoutput
, sep1
, data
);
424 if (SQLITE_DONE
!= c
)
425 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
431 * Implement substring match as an application-defined SQL function.
432 * Using the SQL LIKE or GLOB operators instead would be a bad idea
433 * because that would require escaping metacharacters in the string
434 * being searched for.
437 sql_match(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
441 sqlite3_result_int(context
, NULL
!= strcasestr(
442 (const char *)sqlite3_value_text(argv
[1]),
443 (const char *)sqlite3_value_text(argv
[0])));
447 * Implement regular expression match
448 * as an application-defined SQL function.
451 sql_regexp(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
455 sqlite3_result_int(context
, !regexec(
456 (regex_t
*)sqlite3_value_blob(argv
[0]),
457 (const char *)sqlite3_value_text(argv
[1]),
462 sql_append(char **sql
, size_t *sz
, const char *newstr
, int count
)
466 newsz
= 1 < count
? (size_t)count
: strlen(newstr
);
467 *sql
= mandoc_realloc(*sql
, *sz
+ newsz
+ 1);
469 memset(*sql
+ *sz
, *newstr
, (size_t)count
);
471 memcpy(*sql
+ *sz
, newstr
, newsz
);
477 * Prepare the search SQL statement.
480 sql_statement(const struct expr
*e
)
486 sql
= mandoc_strdup("SELECT * FROM mpages WHERE ");
489 for (needop
= 0; NULL
!= e
; e
= e
->next
) {
491 sql_append(&sql
, &sz
, " AND ", 1);
493 sql_append(&sql
, &sz
, " OR ", 1);
495 sql_append(&sql
, &sz
, "(", e
->open
);
496 sql_append(&sql
, &sz
, NULL
== e
->substr
?
497 "id IN (SELECT pageid FROM keys "
498 "WHERE key REGEXP ? AND bits & ?)" :
499 "id IN (SELECT pageid FROM keys "
500 "WHERE key MATCH ? AND bits & ?)", 1);
502 sql_append(&sql
, &sz
, ")", e
->close
);
510 * Compile a set of string tokens into an expression.
511 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
512 * "(", "foo=bar", etc.).
515 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[])
517 int i
, toopen
, logic
, igncase
, toclose
;
518 struct expr
*first
, *next
, *cur
;
521 logic
= igncase
= toclose
= 0;
524 for (i
= 0; i
< argc
; i
++) {
525 if (0 == strcmp("(", argv
[i
])) {
531 } else if (0 == strcmp(")", argv
[i
])) {
532 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
538 } else if (0 == strcmp("-a", argv
[i
])) {
539 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
543 } else if (0 == strcmp("-o", argv
[i
])) {
544 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
548 } else if (0 == strcmp("-i", argv
[i
])) {
554 next
= exprterm(search
, argv
[i
], !igncase
);
558 next
->and = (1 == logic
);
564 toopen
= logic
= igncase
= 0;
566 if (toopen
|| logic
|| igncase
|| toclose
)
570 cur
= exprspec(cur
, TYPE_arch
, search
->arch
, "^(%s|any)$");
571 exprspec(cur
, TYPE_sec
, search
->sec
, "^%s$");
582 exprspec(struct expr
*cur
, uint64_t key
, const char *value
,
592 mandoc_asprintf(&cp
, format
, value
);
593 cur
->next
= mandoc_calloc(1, sizeof(struct expr
));
597 if (0 != (irc
= regcomp(&cur
->regexp
, cp
,
598 REG_EXTENDED
| REG_NOSUB
| REG_ICASE
))) {
599 regerror(irc
, &cur
->regexp
, errbuf
, sizeof(errbuf
));
600 fprintf(stderr
, "regcomp: %s\n", errbuf
);
608 exprterm(const struct mansearch
*search
, char *buf
, int cs
)
619 e
= mandoc_calloc(1, sizeof(struct expr
));
621 /*"whatis" mode uses an opaque string and default fields. */
623 if (MANSEARCH_WHATIS
& search
->flags
) {
625 e
->bits
= search
->deftype
;
630 * If no =~ is specified, search with equality over names and
632 * If =~ begins the phrase, use name and description fields.
635 if (NULL
== (v
= strpbrk(buf
, "=~"))) {
637 e
->bits
= search
->deftype
;
640 e
->bits
= search
->deftype
;
643 if (NULL
!= strstr(buf
, "arch"))
645 if (0 != (irc
= regcomp(&e
->regexp
, v
,
646 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
)))) {
647 regerror(irc
, &e
->regexp
, errbuf
, sizeof(errbuf
));
648 fprintf(stderr
, "regcomp: %s\n", errbuf
);
657 * Parse out all possible fields.
658 * If the field doesn't resolve, bail.
661 while (NULL
!= (key
= strsep(&buf
, ","))) {
664 for (i
= 0, iterbit
= 1;
665 i
< mansearch_keymax
;
666 i
++, iterbit
<<= 1) {
667 if (0 == strcasecmp(key
,
668 mansearch_keynames
[i
])) {
673 if (i
== mansearch_keymax
) {
674 if (strcasecmp(key
, "any")) {
686 exprfree(struct expr
*p
)
698 hash_halloc(size_t sz
, void *arg
)
701 return(mandoc_calloc(sz
, 1));
705 hash_alloc(size_t sz
, void *arg
)
708 return(mandoc_malloc(sz
));
712 hash_free(void *p
, size_t sz
, void *arg
)