Annotation of mandoc/mansearch.c, Revision 1.5
1.5 ! kristaps 1: /* $Id: mansearch.c,v 1.4 2012/06/09 11:00:13 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
21: #include <sys/param.h>
22:
23: #include <assert.h>
24: #include <fcntl.h>
25: #include <getopt.h>
26: #include <stdio.h>
27: #include <stdint.h>
28: #include <stddef.h>
29: #include <stdlib.h>
30: #include <string.h>
31: #include <unistd.h>
32:
1.4 kristaps 33: #ifdef HAVE_OHASH
1.1 kristaps 34: #include <ohash.h>
1.4 kristaps 35: #else
36: #include "compat_ohash.h"
37: #endif
1.1 kristaps 38: #include <sqlite3.h>
39:
40: #include "mandoc.h"
41: #include "manpath.h"
42: #include "mansearch.h"
43:
1.3 kristaps 44: #define SQL_BIND_TEXT(_db, _s, _i, _v) \
1.2 kristaps 45: if (SQLITE_OK != sqlite3_bind_text \
46: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
47: fprintf(stderr, "%s\n", sqlite3_errmsg((_db)))
1.3 kristaps 48: #define SQL_BIND_INT64(_db, _s, _i, _v) \
1.2 kristaps 49: if (SQLITE_OK != sqlite3_bind_int64 \
50: ((_s), (_i)++, (_v))) \
51: fprintf(stderr, "%s\n", sqlite3_errmsg((_db)))
52:
1.1 kristaps 53: struct expr {
54: int glob; /* is glob? */
55: uint64_t bits; /* type-mask */
56: const char *v; /* search value */
57: struct expr *next; /* next in sequence */
58: };
59:
60: struct match {
61: uint64_t id; /* identifier in database */
62: char *file; /* relative filepath of manpage */
63: char *desc; /* description of manpage */
64: int form; /* 0 == catpage */
65: };
66:
67: struct type {
68: uint64_t bits;
69: const char *name;
70: };
71:
72: static const struct type types[] = {
73: { TYPE_An, "An" },
74: { TYPE_Ar, "Ar" },
75: { TYPE_At, "At" },
76: { TYPE_Bsx, "Bsx" },
77: { TYPE_Bx, "Bx" },
78: { TYPE_Cd, "Cd" },
79: { TYPE_Cm, "Cm" },
80: { TYPE_Dv, "Dv" },
81: { TYPE_Dx, "Dx" },
82: { TYPE_Em, "Em" },
83: { TYPE_Er, "Er" },
84: { TYPE_Ev, "Ev" },
85: { TYPE_Fa, "Fa" },
86: { TYPE_Fl, "Fl" },
87: { TYPE_Fn, "Fn" },
88: { TYPE_Fn, "Fo" },
89: { TYPE_Ft, "Ft" },
90: { TYPE_Fx, "Fx" },
91: { TYPE_Ic, "Ic" },
92: { TYPE_In, "In" },
93: { TYPE_Lb, "Lb" },
94: { TYPE_Li, "Li" },
95: { TYPE_Lk, "Lk" },
96: { TYPE_Ms, "Ms" },
97: { TYPE_Mt, "Mt" },
98: { TYPE_Nd, "Nd" },
99: { TYPE_Nm, "Nm" },
100: { TYPE_Nx, "Nx" },
101: { TYPE_Ox, "Ox" },
102: { TYPE_Pa, "Pa" },
103: { TYPE_Rs, "Rs" },
104: { TYPE_Sh, "Sh" },
105: { TYPE_Ss, "Ss" },
106: { TYPE_St, "St" },
107: { TYPE_Sy, "Sy" },
108: { TYPE_Tn, "Tn" },
109: { TYPE_Va, "Va" },
110: { TYPE_Va, "Vt" },
111: { TYPE_Xr, "Xr" },
112: { ~0ULL, "any" },
113: { 0ULL, NULL }
114: };
115:
116: static void *hash_alloc(size_t, void *);
117: static void hash_free(void *, size_t, void *);
118: static void *hash_halloc(size_t, void *);
1.5 ! kristaps 119: static struct expr *exprcomp(const struct mansearch *,
! 120: int, char *[]);
1.1 kristaps 121: static void exprfree(struct expr *);
1.5 ! kristaps 122: static struct expr *exprterm(const struct mansearch *, char *);
1.1 kristaps 123: static char *sql_statement(const struct expr *,
124: const char *, const char *);
125:
126: int
1.5 ! kristaps 127: mansearch(const struct mansearch *search,
! 128: const struct manpaths *paths,
1.1 kristaps 129: int argc, char *argv[],
130: struct manpage **res, size_t *sz)
131: {
1.2 kristaps 132: int fd, rc, c;
1.1 kristaps 133: int64_t id;
134: char buf[MAXPATHLEN];
135: char *sql;
136: struct expr *e, *ep;
137: sqlite3 *db;
138: sqlite3_stmt *s;
139: struct match *mp;
140: struct ohash_info info;
141: struct ohash htab;
142: unsigned int idx;
143: size_t i, j, cur, maxres;
144:
145: memset(&info, 0, sizeof(struct ohash_info));
146:
147: info.halloc = hash_halloc;
148: info.alloc = hash_alloc;
149: info.hfree = hash_free;
150: info.key_offset = offsetof(struct match, id);
151:
1.2 kristaps 152: *sz = cur = maxres = 0;
1.1 kristaps 153: sql = NULL;
154: *res = NULL;
155: fd = -1;
156: e = NULL;
1.2 kristaps 157: rc = 0;
1.1 kristaps 158:
159: if (0 == argc)
160: goto out;
1.5 ! kristaps 161: if (NULL == (e = exprcomp(search, argc, argv)))
1.1 kristaps 162: goto out;
163:
164: /*
165: * Save a descriptor to the current working directory.
166: * Since pathnames in the "paths" variable might be relative,
167: * and we'll be chdir()ing into them, we need to keep a handle
168: * on our current directory from which to start the chdir().
169: */
170:
171: if (NULL == getcwd(buf, MAXPATHLEN)) {
172: perror(NULL);
173: goto out;
174: } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
175: perror(buf);
176: goto out;
177: }
178:
1.5 ! kristaps 179: sql = sql_statement(e, search->arch, search->sec);
1.1 kristaps 180:
181: /*
182: * Loop over the directories (containing databases) for us to
183: * search.
184: * Don't let missing/bad databases/directories phase us.
185: * In each, try to open the resident database and, if it opens,
186: * scan it for our match expression.
187: */
188:
189: for (i = 0; i < paths->sz; i++) {
190: if (-1 == fchdir(fd)) {
191: perror(buf);
192: free(*res);
193: break;
194: } else if (-1 == chdir(paths->paths[i])) {
195: perror(paths->paths[i]);
196: continue;
197: }
198:
1.2 kristaps 199: c = sqlite3_open_v2
200: (MANDOC_DB, &db,
201: SQLITE_OPEN_READONLY, NULL);
1.1 kristaps 202:
1.2 kristaps 203: if (SQLITE_OK != c) {
1.1 kristaps 204: perror(MANDOC_DB);
205: sqlite3_close(db);
206: continue;
207: }
208:
209: j = 1;
1.2 kristaps 210: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
211: if (SQLITE_OK != c)
212: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 213:
1.5 ! kristaps 214: if (NULL != search->arch)
! 215: SQL_BIND_TEXT(db, s, j, search->arch);
! 216: if (NULL != search->sec)
! 217: SQL_BIND_TEXT(db, s, j, search->sec);
1.1 kristaps 218:
219: for (ep = e; NULL != ep; ep = ep->next) {
1.3 kristaps 220: SQL_BIND_TEXT(db, s, j, ep->v);
221: SQL_BIND_INT64(db, s, j, ep->bits);
1.1 kristaps 222: }
223:
224: memset(&htab, 0, sizeof(struct ohash));
225: ohash_init(&htab, 4, &info);
226:
227: /*
228: * Hash each entry on its [unique] document identifier.
229: * This is a uint64_t.
230: * Instead of using a hash function, simply convert the
231: * uint64_t to a uint32_t, the hash value's type.
232: * This gives good performance and preserves the
233: * distribution of buckets in the table.
234: */
1.2 kristaps 235: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.1 kristaps 236: id = sqlite3_column_int64(s, 0);
237: idx = ohash_lookup_memory
238: (&htab, (char *)&id,
239: sizeof(uint64_t), (uint32_t)id);
240:
241: if (NULL != ohash_find(&htab, idx))
242: continue;
243:
244: mp = mandoc_calloc(1, sizeof(struct match));
245: mp->id = id;
246: mp->file = mandoc_strdup
247: ((char *)sqlite3_column_text(s, 3));
248: mp->desc = mandoc_strdup
249: ((char *)sqlite3_column_text(s, 4));
250: mp->form = sqlite3_column_int(s, 5);
251: ohash_insert(&htab, idx, mp);
252: }
253:
1.2 kristaps 254: if (SQLITE_DONE != c)
255: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
256:
1.1 kristaps 257: sqlite3_finalize(s);
258: sqlite3_close(db);
259:
260: for (mp = ohash_first(&htab, &idx);
261: NULL != mp;
262: mp = ohash_next(&htab, &idx)) {
263: if (cur + 1 > maxres) {
264: maxres += 1024;
265: *res = mandoc_realloc
266: (*res, maxres * sizeof(struct manpage));
267: }
268: strlcpy((*res)[cur].file,
269: paths->paths[i], MAXPATHLEN);
270: strlcat((*res)[cur].file, "/", MAXPATHLEN);
271: strlcat((*res)[cur].file, mp->file, MAXPATHLEN);
272: (*res)[cur].desc = mp->desc;
273: (*res)[cur].form = mp->form;
274: free(mp->file);
275: free(mp);
276: cur++;
277: }
278: ohash_delete(&htab);
279: }
1.2 kristaps 280: rc = 1;
1.1 kristaps 281: out:
282: exprfree(e);
283: if (-1 != fd)
284: close(fd);
285: free(sql);
286: *sz = cur;
1.2 kristaps 287: return(rc);
1.1 kristaps 288: }
289:
290: /*
291: * Prepare the search SQL statement.
292: * We search for any of the words specified in our match expression.
293: * We filter the per-doc AND expressions when collecting results.
294: */
295: static char *
296: sql_statement(const struct expr *e, const char *arch, const char *sec)
297: {
298: char *sql;
299: const char *glob = "(key GLOB ? AND bits & ?)";
300: const char *eq = "(key = ? AND bits & ?)";
301: const char *andarch = "arch = ? AND ";
302: const char *andsec = "sec = ? AND ";
1.2 kristaps 303: size_t globsz;
304: size_t eqsz;
1.1 kristaps 305: size_t sz;
306:
307: sql = mandoc_strdup
308: ("SELECT docid,bits,key,file,desc,form,sec,arch "
309: "FROM keys "
310: "INNER JOIN docs ON docs.id=keys.docid "
311: "WHERE ");
312: sz = strlen(sql);
1.2 kristaps 313: globsz = strlen(glob);
314: eqsz = strlen(eq);
1.1 kristaps 315:
316: if (NULL != arch) {
317: sz += strlen(andarch) + 1;
318: sql = mandoc_realloc(sql, sz);
319: strlcat(sql, andarch, sz);
320: }
1.2 kristaps 321:
1.1 kristaps 322: if (NULL != sec) {
323: sz += strlen(andsec) + 1;
324: sql = mandoc_realloc(sql, sz);
325: strlcat(sql, andsec, sz);
326: }
327:
328: sz += 2;
329: sql = mandoc_realloc(sql, sz);
330: strlcat(sql, "(", sz);
331:
332: for ( ; NULL != e; e = e->next) {
333: sz += (e->glob ? globsz : eqsz) +
334: (NULL == e->next ? 3 : 5);
335: sql = mandoc_realloc(sql, sz);
336: strlcat(sql, e->glob ? glob : eq, sz);
337: strlcat(sql, NULL == e->next ? ");" : " OR ", sz);
338: }
339:
340: return(sql);
341: }
342:
343: /*
344: * Compile a set of string tokens into an expression.
345: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
346: * "(", "foo=bar", etc.).
347: */
348: static struct expr *
1.5 ! kristaps 349: exprcomp(const struct mansearch *search, int argc, char *argv[])
1.1 kristaps 350: {
351: int i;
352: struct expr *first, *next, *cur;
353:
354: first = cur = NULL;
355:
356: for (i = 0; i < argc; i++) {
1.5 ! kristaps 357: next = exprterm(search, argv[i]);
1.1 kristaps 358: if (NULL == next) {
359: exprfree(first);
360: return(NULL);
361: }
362: if (NULL != first) {
363: cur->next = next;
364: cur = next;
365: } else
366: cur = first = next;
367: }
368:
369: return(first);
370: }
371:
372: static struct expr *
1.5 ! kristaps 373: exprterm(const struct mansearch *search, char *buf)
1.1 kristaps 374: {
375: struct expr *e;
376: char *key, *v;
377: size_t i;
378:
379: if ('\0' == *buf)
380: return(NULL);
381:
382: e = mandoc_calloc(1, sizeof(struct expr));
383:
1.5 ! kristaps 384: /*"whatis" mode uses an opaque string and default fields. */
! 385:
! 386: if (MANSEARCH_WHATIS & search->flags) {
! 387: e->v = buf;
! 388: e->bits = search->deftype;
! 389: return(e);
! 390: }
! 391:
1.1 kristaps 392: /*
393: * If no =~ is specified, search with equality over names and
394: * descriptions.
395: * If =~ begins the phrase, use name and description fields.
396: */
397:
398: if (NULL == (v = strpbrk(buf, "=~"))) {
399: e->v = buf;
1.5 ! kristaps 400: e->bits = search->deftype;
1.1 kristaps 401: return(e);
402: } else if (v == buf)
1.5 ! kristaps 403: e->bits = search->deftype;
1.1 kristaps 404:
405: e->glob = '~' == *v;
406: *v++ = '\0';
407: e->v = v;
408:
409: /*
410: * Parse out all possible fields.
411: * If the field doesn't resolve, bail.
412: */
413:
414: while (NULL != (key = strsep(&buf, ","))) {
415: if ('\0' == *key)
416: continue;
417: i = 0;
418: while (types[i].bits &&
419: strcasecmp(types[i].name, key))
420: i++;
421: if (0 == types[i].bits) {
422: free(e);
423: return(NULL);
424: }
425: e->bits |= types[i].bits;
426: }
427:
428: return(e);
429: }
430:
431: static void
432: exprfree(struct expr *p)
433: {
434: struct expr *pp;
435:
436: while (NULL != p) {
437: pp = p->next;
438: free(p);
439: p = pp;
440: }
441: }
442:
443: static void *
444: hash_halloc(size_t sz, void *arg)
445: {
446:
447: return(mandoc_calloc(sz, 1));
448: }
449:
450: static void *
451: hash_alloc(size_t sz, void *arg)
452: {
453:
454: return(mandoc_malloc(sz));
455: }
456:
457: static void
458: hash_free(void *p, size_t sz, void *arg)
459: {
460:
461: free(p);
462: }
CVSweb