]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $Id: mansearch.c,v 1.26 2014/04/09 21:50:08 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 char *desc
; /* manual page description */
78 int form
; /* 0 == catpage */
81 static void buildnames(struct manpage
*, sqlite3
*,
82 sqlite3_stmt
*, uint64_t,
83 const char *, int form
);
84 static char *buildoutput(sqlite3
*, sqlite3_stmt
*,
86 static void *hash_alloc(size_t, void *);
87 static void hash_free(void *, size_t, void *);
88 static void *hash_halloc(size_t, void *);
89 static struct expr
*exprcomp(const struct mansearch
*,
91 static void exprfree(struct expr
*);
92 static struct expr
*exprspec(struct expr
*, uint64_t,
93 const char *, const char *);
94 static struct expr
*exprterm(const struct mansearch
*, char *, int);
95 static void sql_append(char **sql
, size_t *sz
,
96 const char *newstr
, int count
);
97 static void sql_match(sqlite3_context
*context
,
98 int argc
, sqlite3_value
**argv
);
99 static void sql_regexp(sqlite3_context
*context
,
100 int argc
, sqlite3_value
**argv
);
101 static char *sql_statement(const struct expr
*);
104 mansearch(const struct mansearch
*search
,
105 const struct manpaths
*paths
,
106 int argc
, char *argv
[],
108 struct manpage
**res
, size_t *sz
)
110 int fd
, rc
, c
, indexbit
;
112 uint64_t outbit
, iterbit
;
115 struct manpage
*mpage
;
118 sqlite3_stmt
*s
, *s2
;
120 struct ohash_info info
;
123 size_t i
, j
, cur
, maxres
;
125 memset(&info
, 0, sizeof(struct ohash_info
));
127 info
.halloc
= hash_halloc
;
128 info
.alloc
= hash_alloc
;
129 info
.hfree
= hash_free
;
130 info
.key_offset
= offsetof(struct match
, id
);
132 *sz
= cur
= maxres
= 0;
141 if (NULL
== (e
= exprcomp(search
, argc
, argv
)))
145 if (NULL
!= outkey
) {
146 for (indexbit
= 0, iterbit
= 1;
147 indexbit
< mansearch_keymax
;
148 indexbit
++, iterbit
<<= 1) {
149 if (0 == strcasecmp(outkey
,
150 mansearch_keynames
[indexbit
])) {
158 * Save a descriptor to the current working directory.
159 * Since pathnames in the "paths" variable might be relative,
160 * and we'll be chdir()ing into them, we need to keep a handle
161 * on our current directory from which to start the chdir().
164 if (NULL
== getcwd(buf
, PATH_MAX
)) {
167 } else if (-1 == (fd
= open(buf
, O_RDONLY
, 0))) {
172 sql
= sql_statement(e
);
175 * Loop over the directories (containing databases) for us to
177 * Don't let missing/bad databases/directories phase us.
178 * In each, try to open the resident database and, if it opens,
179 * scan it for our match expression.
182 for (i
= 0; i
< paths
->sz
; i
++) {
183 if (-1 == fchdir(fd
)) {
187 } else if (-1 == chdir(paths
->paths
[i
])) {
188 perror(paths
->paths
[i
]);
194 SQLITE_OPEN_READONLY
, NULL
);
196 if (SQLITE_OK
!= c
) {
203 * Define the SQL functions for substring
204 * and regular expression matching.
207 c
= sqlite3_create_function(db
, "match", 2,
208 SQLITE_ANY
, NULL
, sql_match
, NULL
, NULL
);
209 assert(SQLITE_OK
== c
);
210 c
= sqlite3_create_function(db
, "regexp", 2,
211 SQLITE_ANY
, NULL
, sql_regexp
, NULL
, NULL
);
212 assert(SQLITE_OK
== c
);
215 c
= sqlite3_prepare_v2(db
, sql
, -1, &s
, NULL
);
217 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
219 for (ep
= e
; NULL
!= ep
; ep
= ep
->next
) {
220 if (NULL
== ep
->substr
) {
221 SQL_BIND_BLOB(db
, s
, j
, ep
->regexp
);
223 SQL_BIND_TEXT(db
, s
, j
, ep
->substr
);
224 if (0 == (TYPE_Nd
& ep
->bits
))
225 SQL_BIND_INT64(db
, s
, j
, ep
->bits
);
228 memset(&htab
, 0, sizeof(struct ohash
));
229 ohash_init(&htab
, 4, &info
);
232 * Hash each entry on its [unique] document identifier.
233 * This is a uint64_t.
234 * Instead of using a hash function, simply convert the
235 * uint64_t to a uint32_t, the hash value's type.
236 * This gives good performance and preserves the
237 * distribution of buckets in the table.
239 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
240 id
= sqlite3_column_int64(s
, 2);
241 idx
= ohash_lookup_memory
243 sizeof(uint64_t), (uint32_t)id
);
245 if (NULL
!= ohash_find(&htab
, idx
))
248 mp
= mandoc_calloc(1, sizeof(struct match
));
250 mp
->form
= sqlite3_column_int(s
, 1);
251 if (TYPE_Nd
== outbit
)
252 mp
->desc
= mandoc_strdup(
253 sqlite3_column_text(s
, 0));
254 ohash_insert(&htab
, idx
, mp
);
257 if (SQLITE_DONE
!= c
)
258 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
262 c
= sqlite3_prepare_v2(db
,
263 "SELECT * FROM mlinks WHERE pageid=?"
264 " ORDER BY sec, arch, name",
267 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
269 c
= sqlite3_prepare_v2(db
,
270 "SELECT * FROM keys WHERE pageid=? AND bits & ?",
273 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
275 for (mp
= ohash_first(&htab
, &idx
);
277 mp
= ohash_next(&htab
, &idx
)) {
278 if (cur
+ 1 > maxres
) {
280 *res
= mandoc_realloc
281 (*res
, maxres
* sizeof(struct manpage
));
284 mpage
->form
= mp
->form
;
285 buildnames(mpage
, db
, s
, mp
->id
,
286 paths
->paths
[i
], mp
->form
);
287 mpage
->output
= TYPE_Nd
& outbit
?
289 buildoutput(db
, s2
, mp
->id
, outbit
) : NULL
;
296 sqlite3_finalize(s2
);
311 buildnames(struct manpage
*mpage
, sqlite3
*db
, sqlite3_stmt
*s
,
312 uint64_t id
, const char *path
, int form
)
314 char *newnames
, *prevsec
, *prevarch
;
315 const char *oldnames
, *sep1
, *name
, *sec
, *sep2
, *arch
, *fsec
;
321 prevsec
= prevarch
= NULL
;
323 SQL_BIND_INT64(db
, s
, i
, id
);
324 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
326 /* Decide whether we already have some names. */
328 if (NULL
== mpage
->names
) {
332 oldnames
= mpage
->names
;
336 /* Fetch the next name. */
338 sec
= sqlite3_column_text(s
, 0);
339 arch
= sqlite3_column_text(s
, 1);
340 name
= sqlite3_column_text(s
, 2);
342 /* If the section changed, append the old one. */
344 if (NULL
!= prevsec
&&
345 (strcmp(sec
, prevsec
) ||
346 strcmp(arch
, prevarch
))) {
347 sep2
= '\0' == *prevarch
? "" : "/";
348 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
349 oldnames
, prevsec
, sep2
, prevarch
);
351 oldnames
= mpage
->names
= newnames
;
354 prevsec
= prevarch
= NULL
;
357 /* Save the new section, to append it later. */
359 if (NULL
== prevsec
) {
360 prevsec
= mandoc_strdup(sec
);
361 prevarch
= mandoc_strdup(arch
);
364 /* Append the new name. */
366 mandoc_asprintf(&newnames
, "%s%s%s",
367 oldnames
, sep1
, name
);
369 mpage
->names
= newnames
;
371 /* Also save the first file name encountered. */
373 if (NULL
!= mpage
->file
)
383 sep2
= '\0' == *arch
? "" : "/";
384 mandoc_asprintf(&mpage
->file
, "%s/%s%s%s%s/%s.%s",
385 path
, sep1
, sec
, sep2
, arch
, name
, fsec
);
387 if (SQLITE_DONE
!= c
)
388 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
391 /* Append one final section to the names. */
393 if (NULL
!= prevsec
) {
394 sep2
= '\0' == *prevarch
? "" : "/";
395 mandoc_asprintf(&newnames
, "%s(%s%s%s)",
396 mpage
->names
, prevsec
, sep2
, prevarch
);
398 mpage
->names
= newnames
;
405 buildoutput(sqlite3
*db
, sqlite3_stmt
*s
, uint64_t id
, uint64_t outbit
)
407 char *output
, *newoutput
;
408 const char *oldoutput
, *sep1
, *data
;
414 SQL_BIND_INT64(db
, s
, i
, id
);
415 SQL_BIND_INT64(db
, s
, i
, outbit
);
416 while (SQLITE_ROW
== (c
= sqlite3_step(s
))) {
417 if (NULL
== output
) {
424 data
= sqlite3_column_text(s
, 1);
425 mandoc_asprintf(&newoutput
, "%s%s%s",
426 oldoutput
, sep1
, data
);
430 if (SQLITE_DONE
!= c
)
431 fprintf(stderr
, "%s\n", sqlite3_errmsg(db
));
437 * Implement substring match as an application-defined SQL function.
438 * Using the SQL LIKE or GLOB operators instead would be a bad idea
439 * because that would require escaping metacharacters in the string
440 * being searched for.
443 sql_match(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
447 sqlite3_result_int(context
, NULL
!= strcasestr(
448 (const char *)sqlite3_value_text(argv
[1]),
449 (const char *)sqlite3_value_text(argv
[0])));
453 * Implement regular expression match
454 * as an application-defined SQL function.
457 sql_regexp(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
461 sqlite3_result_int(context
, !regexec(
462 (regex_t
*)sqlite3_value_blob(argv
[0]),
463 (const char *)sqlite3_value_text(argv
[1]),
468 sql_append(char **sql
, size_t *sz
, const char *newstr
, int count
)
472 newsz
= 1 < count
? (size_t)count
: strlen(newstr
);
473 *sql
= mandoc_realloc(*sql
, *sz
+ newsz
+ 1);
475 memset(*sql
+ *sz
, *newstr
, (size_t)count
);
477 memcpy(*sql
+ *sz
, newstr
, newsz
);
483 * Prepare the search SQL statement.
486 sql_statement(const struct expr
*e
)
492 sql
= mandoc_strdup("SELECT * FROM mpages WHERE ");
495 for (needop
= 0; NULL
!= e
; e
= e
->next
) {
497 sql_append(&sql
, &sz
, " AND ", 1);
499 sql_append(&sql
, &sz
, " OR ", 1);
501 sql_append(&sql
, &sz
, "(", e
->open
);
502 sql_append(&sql
, &sz
,
508 ? "id IN (SELECT pageid FROM keys "
509 "WHERE key REGEXP ? AND bits & ?)"
510 : "id IN (SELECT pageid FROM keys "
511 "WHERE key MATCH ? AND bits & ?)"), 1);
513 sql_append(&sql
, &sz
, ")", e
->close
);
521 * Compile a set of string tokens into an expression.
522 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
523 * "(", "foo=bar", etc.).
526 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[])
528 int i
, toopen
, logic
, igncase
, toclose
;
529 struct expr
*first
, *next
, *cur
;
532 logic
= igncase
= toclose
= 0;
535 for (i
= 0; i
< argc
; i
++) {
536 if (0 == strcmp("(", argv
[i
])) {
542 } else if (0 == strcmp(")", argv
[i
])) {
543 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
549 } else if (0 == strcmp("-a", argv
[i
])) {
550 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
554 } else if (0 == strcmp("-o", argv
[i
])) {
555 if (toopen
|| logic
|| igncase
|| NULL
== cur
)
559 } else if (0 == strcmp("-i", argv
[i
])) {
565 next
= exprterm(search
, argv
[i
], !igncase
);
574 * Searching for descriptions must be split out
575 * because they are stored in the mpages table,
576 * not in the keys table.
579 if (TYPE_Nd
& next
->bits
&& ~TYPE_Nd
& next
->bits
) {
580 cur
= mandoc_calloc(1, sizeof(struct expr
));
581 memcpy(cur
, next
, sizeof(struct expr
));
583 next
->bits
= TYPE_Nd
;
585 cur
->bits
&= ~TYPE_Nd
;
589 next
->and = (1 == logic
);
590 next
->open
+= toopen
;
591 toopen
= logic
= igncase
= 0;
593 if (toopen
|| logic
|| igncase
|| toclose
)
597 cur
= exprspec(cur
, TYPE_arch
, search
->arch
, "^(%s|any)$");
598 exprspec(cur
, TYPE_sec
, search
->sec
, "^%s$");
609 exprspec(struct expr
*cur
, uint64_t key
, const char *value
,
619 mandoc_asprintf(&cp
, format
, value
);
620 cur
->next
= mandoc_calloc(1, sizeof(struct expr
));
624 if (0 != (irc
= regcomp(&cur
->regexp
, cp
,
625 REG_EXTENDED
| REG_NOSUB
| REG_ICASE
))) {
626 regerror(irc
, &cur
->regexp
, errbuf
, sizeof(errbuf
));
627 fprintf(stderr
, "regcomp: %s\n", errbuf
);
635 exprterm(const struct mansearch
*search
, char *buf
, int cs
)
646 e
= mandoc_calloc(1, sizeof(struct expr
));
648 /*"whatis" mode uses an opaque string and default fields. */
650 if (MANSEARCH_WHATIS
& search
->flags
) {
652 e
->bits
= search
->deftype
;
657 * If no =~ is specified, search with equality over names and
659 * If =~ begins the phrase, use name and description fields.
662 if (NULL
== (v
= strpbrk(buf
, "=~"))) {
664 e
->bits
= search
->deftype
;
667 e
->bits
= search
->deftype
;
670 if (NULL
!= strstr(buf
, "arch"))
672 if (0 != (irc
= regcomp(&e
->regexp
, v
,
673 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
)))) {
674 regerror(irc
, &e
->regexp
, errbuf
, sizeof(errbuf
));
675 fprintf(stderr
, "regcomp: %s\n", errbuf
);
684 * Parse out all possible fields.
685 * If the field doesn't resolve, bail.
688 while (NULL
!= (key
= strsep(&buf
, ","))) {
691 for (i
= 0, iterbit
= 1;
692 i
< mansearch_keymax
;
693 i
++, iterbit
<<= 1) {
694 if (0 == strcasecmp(key
,
695 mansearch_keynames
[i
])) {
700 if (i
== mansearch_keymax
) {
701 if (strcasecmp(key
, "any")) {
713 exprfree(struct expr
*p
)
725 hash_halloc(size_t sz
, void *arg
)
728 return(mandoc_calloc(sz
, 1));
732 hash_alloc(size_t sz
, void *arg
)
735 return(mandoc_malloc(sz
));
739 hash_free(void *p
, size_t sz
, void *arg
)