Annotation of mandoc/mansearch.c, Revision 1.10
1.10 ! schwarze 1: /* $Id: mansearch.c,v 1.9 2013/12/27 01:16:54 schwarze Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.7 schwarze 4: * Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
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.
9: *
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.
17: */
18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
22: #include <assert.h>
23: #include <fcntl.h>
24: #include <getopt.h>
1.6 schwarze 25: #include <limits.h>
1.8 schwarze 26: #include <regex.h>
1.1 kristaps 27: #include <stdio.h>
28: #include <stdint.h>
29: #include <stddef.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
33:
1.4 kristaps 34: #ifdef HAVE_OHASH
1.1 kristaps 35: #include <ohash.h>
1.4 kristaps 36: #else
37: #include "compat_ohash.h"
38: #endif
1.1 kristaps 39: #include <sqlite3.h>
40:
41: #include "mandoc.h"
42: #include "manpath.h"
43: #include "mansearch.h"
44:
1.3 kristaps 45: #define SQL_BIND_TEXT(_db, _s, _i, _v) \
1.8 schwarze 46: do { if (SQLITE_OK != sqlite3_bind_text \
1.2 kristaps 47: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
1.8 schwarze 48: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
49: } while (0)
1.3 kristaps 50: #define SQL_BIND_INT64(_db, _s, _i, _v) \
1.8 schwarze 51: do { if (SQLITE_OK != sqlite3_bind_int64 \
1.2 kristaps 52: ((_s), (_i)++, (_v))) \
1.8 schwarze 53: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
54: } while (0)
55: #define SQL_BIND_BLOB(_db, _s, _i, _v) \
56: do { if (SQLITE_OK != sqlite3_bind_blob \
57: ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
58: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
59: } while (0)
1.2 kristaps 60:
1.1 kristaps 61: struct expr {
1.8 schwarze 62: uint64_t bits; /* type-mask */
63: const char *substr; /* to search for, if applicable */
64: regex_t regexp; /* compiled regexp, if applicable */
65: struct expr *next; /* next in sequence */
1.1 kristaps 66: };
67:
68: struct match {
69: uint64_t id; /* identifier in database */
70: char *file; /* relative filepath of manpage */
71: char *desc; /* description of manpage */
72: int form; /* 0 == catpage */
73: };
74:
75: struct type {
76: uint64_t bits;
77: const char *name;
78: };
79:
80: static const struct type types[] = {
81: { TYPE_An, "An" },
82: { TYPE_Ar, "Ar" },
83: { TYPE_At, "At" },
84: { TYPE_Bsx, "Bsx" },
85: { TYPE_Bx, "Bx" },
86: { TYPE_Cd, "Cd" },
87: { TYPE_Cm, "Cm" },
88: { TYPE_Dv, "Dv" },
89: { TYPE_Dx, "Dx" },
90: { TYPE_Em, "Em" },
91: { TYPE_Er, "Er" },
92: { TYPE_Ev, "Ev" },
93: { TYPE_Fa, "Fa" },
94: { TYPE_Fl, "Fl" },
95: { TYPE_Fn, "Fn" },
96: { TYPE_Fn, "Fo" },
97: { TYPE_Ft, "Ft" },
98: { TYPE_Fx, "Fx" },
99: { TYPE_Ic, "Ic" },
100: { TYPE_In, "In" },
101: { TYPE_Lb, "Lb" },
102: { TYPE_Li, "Li" },
103: { TYPE_Lk, "Lk" },
104: { TYPE_Ms, "Ms" },
105: { TYPE_Mt, "Mt" },
106: { TYPE_Nd, "Nd" },
107: { TYPE_Nm, "Nm" },
108: { TYPE_Nx, "Nx" },
109: { TYPE_Ox, "Ox" },
110: { TYPE_Pa, "Pa" },
111: { TYPE_Rs, "Rs" },
112: { TYPE_Sh, "Sh" },
113: { TYPE_Ss, "Ss" },
114: { TYPE_St, "St" },
115: { TYPE_Sy, "Sy" },
116: { TYPE_Tn, "Tn" },
117: { TYPE_Va, "Va" },
118: { TYPE_Va, "Vt" },
119: { TYPE_Xr, "Xr" },
120: { ~0ULL, "any" },
121: { 0ULL, NULL }
122: };
123:
124: static void *hash_alloc(size_t, void *);
125: static void hash_free(void *, size_t, void *);
126: static void *hash_halloc(size_t, void *);
1.5 kristaps 127: static struct expr *exprcomp(const struct mansearch *,
128: int, char *[]);
1.1 kristaps 129: static void exprfree(struct expr *);
1.8 schwarze 130: static struct expr *exprterm(const struct mansearch *, char *, int);
1.7 schwarze 131: static void sql_match(sqlite3_context *context,
132: int argc, sqlite3_value **argv);
1.8 schwarze 133: static void sql_regexp(sqlite3_context *context,
134: int argc, sqlite3_value **argv);
1.1 kristaps 135: static char *sql_statement(const struct expr *,
136: const char *, const char *);
137:
138: int
1.5 kristaps 139: mansearch(const struct mansearch *search,
140: const struct manpaths *paths,
1.1 kristaps 141: int argc, char *argv[],
142: struct manpage **res, size_t *sz)
143: {
1.2 kristaps 144: int fd, rc, c;
1.1 kristaps 145: int64_t id;
1.6 schwarze 146: char buf[PATH_MAX];
1.10 ! schwarze 147: char *sql, *newnames;
! 148: const char *oldnames, *sep1, *name, *sec, *sep2, *arch;
! 149: struct manpage *mpage;
1.1 kristaps 150: struct expr *e, *ep;
151: sqlite3 *db;
152: sqlite3_stmt *s;
153: struct match *mp;
154: struct ohash_info info;
155: struct ohash htab;
156: unsigned int idx;
157: size_t i, j, cur, maxres;
158:
159: memset(&info, 0, sizeof(struct ohash_info));
160:
161: info.halloc = hash_halloc;
162: info.alloc = hash_alloc;
163: info.hfree = hash_free;
164: info.key_offset = offsetof(struct match, id);
165:
1.2 kristaps 166: *sz = cur = maxres = 0;
1.1 kristaps 167: sql = NULL;
168: *res = NULL;
169: fd = -1;
170: e = NULL;
1.2 kristaps 171: rc = 0;
1.1 kristaps 172:
173: if (0 == argc)
174: goto out;
1.5 kristaps 175: if (NULL == (e = exprcomp(search, argc, argv)))
1.1 kristaps 176: goto out;
177:
178: /*
179: * Save a descriptor to the current working directory.
180: * Since pathnames in the "paths" variable might be relative,
181: * and we'll be chdir()ing into them, we need to keep a handle
182: * on our current directory from which to start the chdir().
183: */
184:
1.6 schwarze 185: if (NULL == getcwd(buf, PATH_MAX)) {
1.1 kristaps 186: perror(NULL);
187: goto out;
188: } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
189: perror(buf);
190: goto out;
191: }
192:
1.5 kristaps 193: sql = sql_statement(e, search->arch, search->sec);
1.1 kristaps 194:
195: /*
196: * Loop over the directories (containing databases) for us to
197: * search.
198: * Don't let missing/bad databases/directories phase us.
199: * In each, try to open the resident database and, if it opens,
200: * scan it for our match expression.
201: */
202:
203: for (i = 0; i < paths->sz; i++) {
204: if (-1 == fchdir(fd)) {
205: perror(buf);
206: free(*res);
207: break;
208: } else if (-1 == chdir(paths->paths[i])) {
209: perror(paths->paths[i]);
210: continue;
211: }
212:
1.2 kristaps 213: c = sqlite3_open_v2
214: (MANDOC_DB, &db,
215: SQLITE_OPEN_READONLY, NULL);
1.1 kristaps 216:
1.2 kristaps 217: if (SQLITE_OK != c) {
1.1 kristaps 218: perror(MANDOC_DB);
219: sqlite3_close(db);
220: continue;
221: }
222:
1.8 schwarze 223: /*
224: * Define the SQL functions for substring
225: * and regular expression matching.
226: */
1.7 schwarze 227:
228: c = sqlite3_create_function(db, "match", 2,
229: SQLITE_ANY, NULL, sql_match, NULL, NULL);
1.8 schwarze 230: assert(SQLITE_OK == c);
231: c = sqlite3_create_function(db, "regexp", 2,
232: SQLITE_ANY, NULL, sql_regexp, NULL, NULL);
233: assert(SQLITE_OK == c);
1.7 schwarze 234:
1.1 kristaps 235: j = 1;
1.2 kristaps 236: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
237: if (SQLITE_OK != c)
238: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 239:
1.5 kristaps 240: if (NULL != search->arch)
241: SQL_BIND_TEXT(db, s, j, search->arch);
242: if (NULL != search->sec)
243: SQL_BIND_TEXT(db, s, j, search->sec);
1.1 kristaps 244:
245: for (ep = e; NULL != ep; ep = ep->next) {
1.8 schwarze 246: if (NULL == ep->substr) {
247: SQL_BIND_BLOB(db, s, j, ep->regexp);
248: } else
249: SQL_BIND_TEXT(db, s, j, ep->substr);
1.3 kristaps 250: SQL_BIND_INT64(db, s, j, ep->bits);
1.1 kristaps 251: }
252:
253: memset(&htab, 0, sizeof(struct ohash));
254: ohash_init(&htab, 4, &info);
255:
256: /*
257: * Hash each entry on its [unique] document identifier.
258: * This is a uint64_t.
259: * Instead of using a hash function, simply convert the
260: * uint64_t to a uint32_t, the hash value's type.
261: * This gives good performance and preserves the
262: * distribution of buckets in the table.
263: */
1.2 kristaps 264: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.1 kristaps 265: id = sqlite3_column_int64(s, 0);
266: idx = ohash_lookup_memory
267: (&htab, (char *)&id,
268: sizeof(uint64_t), (uint32_t)id);
269:
270: if (NULL != ohash_find(&htab, idx))
271: continue;
272:
273: mp = mandoc_calloc(1, sizeof(struct match));
274: mp->id = id;
275: mp->file = mandoc_strdup
276: ((char *)sqlite3_column_text(s, 3));
277: mp->desc = mandoc_strdup
278: ((char *)sqlite3_column_text(s, 4));
279: mp->form = sqlite3_column_int(s, 5);
280: ohash_insert(&htab, idx, mp);
281: }
282:
1.2 kristaps 283: if (SQLITE_DONE != c)
284: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
285:
1.1 kristaps 286: sqlite3_finalize(s);
1.10 ! schwarze 287:
! 288: c = sqlite3_prepare_v2(db,
! 289: "SELECT * FROM mlinks WHERE pageid=?",
! 290: -1, &s, NULL);
! 291: if (SQLITE_OK != c)
! 292: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 293:
294: for (mp = ohash_first(&htab, &idx);
295: NULL != mp;
296: mp = ohash_next(&htab, &idx)) {
297: if (cur + 1 > maxres) {
298: maxres += 1024;
299: *res = mandoc_realloc
300: (*res, maxres * sizeof(struct manpage));
301: }
1.10 ! schwarze 302: mpage = *res + cur;
! 303: if (-1 == asprintf(&mpage->file, "%s/%s",
! 304: paths->paths[i], mp->file)) {
! 305: perror(0);
! 306: exit((int)MANDOCLEVEL_SYSERR);
! 307: }
! 308: mpage->names = NULL;
! 309: mpage->desc = mp->desc;
! 310: mpage->form = mp->form;
! 311:
! 312: j = 1;
! 313: SQL_BIND_INT64(db, s, j, mp->id);
! 314: while (SQLITE_ROW == (c = sqlite3_step(s))) {
! 315: if (NULL == mpage->names) {
! 316: oldnames = "";
! 317: sep1 = "";
! 318: } else {
! 319: oldnames = mpage->names;
! 320: sep1 = ", ";
! 321: }
! 322: sec = sqlite3_column_text(s, 1);
! 323: arch = sqlite3_column_text(s, 2);
! 324: name = sqlite3_column_text(s, 3);
! 325: sep2 = '\0' == *arch ? "" : "/";
! 326: if (-1 == asprintf(&newnames,
! 327: "%s%s%s(%s%s%s)", oldnames, sep1,
! 328: name, sec, sep2, arch)) {
! 329: perror(0);
! 330: exit((int)MANDOCLEVEL_SYSERR);
! 331: }
! 332: free(mpage->names);
! 333: mpage->names = newnames;
! 334: }
! 335: if (SQLITE_DONE != c)
! 336: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
! 337: sqlite3_reset(s);
! 338:
1.1 kristaps 339: free(mp->file);
340: free(mp);
341: cur++;
342: }
1.10 ! schwarze 343:
! 344: sqlite3_finalize(s);
! 345: sqlite3_close(db);
1.1 kristaps 346: ohash_delete(&htab);
347: }
1.2 kristaps 348: rc = 1;
1.1 kristaps 349: out:
350: exprfree(e);
351: if (-1 != fd)
352: close(fd);
353: free(sql);
354: *sz = cur;
1.2 kristaps 355: return(rc);
1.1 kristaps 356: }
357:
358: /*
1.7 schwarze 359: * Implement substring match as an application-defined SQL function.
360: * Using the SQL LIKE or GLOB operators instead would be a bad idea
361: * because that would require escaping metacharacters in the string
362: * being searched for.
363: */
364: static void
365: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
366: {
367:
368: assert(2 == argc);
369: sqlite3_result_int(context, NULL != strcasestr(
370: (const char *)sqlite3_value_text(argv[1]),
371: (const char *)sqlite3_value_text(argv[0])));
372: }
373:
374: /*
1.8 schwarze 375: * Implement regular expression match
376: * as an application-defined SQL function.
377: */
378: static void
379: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
380: {
381:
382: assert(2 == argc);
383: sqlite3_result_int(context, !regexec(
384: (regex_t *)sqlite3_value_blob(argv[0]),
385: (const char *)sqlite3_value_text(argv[1]),
386: 0, NULL, 0));
387: }
388:
389: /*
1.1 kristaps 390: * Prepare the search SQL statement.
391: * We search for any of the words specified in our match expression.
392: * We filter the per-doc AND expressions when collecting results.
393: */
394: static char *
395: sql_statement(const struct expr *e, const char *arch, const char *sec)
396: {
397: char *sql;
1.7 schwarze 398: const char *substr = "(key MATCH ? AND bits & ?)";
1.8 schwarze 399: const char *regexp = "(key REGEXP ? AND bits & ?)";
1.1 kristaps 400: const char *andarch = "arch = ? AND ";
401: const char *andsec = "sec = ? AND ";
1.7 schwarze 402: size_t substrsz;
1.8 schwarze 403: size_t regexpsz;
1.1 kristaps 404: size_t sz;
405:
406: sql = mandoc_strdup
1.9 schwarze 407: ("SELECT pageid,bits,key,file,desc,form,sec,arch "
1.1 kristaps 408: "FROM keys "
1.9 schwarze 409: "INNER JOIN mpages ON mpages.id=keys.pageid "
1.1 kristaps 410: "WHERE ");
411: sz = strlen(sql);
1.7 schwarze 412: substrsz = strlen(substr);
1.8 schwarze 413: regexpsz = strlen(regexp);
1.1 kristaps 414:
415: if (NULL != arch) {
416: sz += strlen(andarch) + 1;
417: sql = mandoc_realloc(sql, sz);
418: strlcat(sql, andarch, sz);
419: }
1.2 kristaps 420:
1.1 kristaps 421: if (NULL != sec) {
422: sz += strlen(andsec) + 1;
423: sql = mandoc_realloc(sql, sz);
424: strlcat(sql, andsec, sz);
425: }
426:
427: sz += 2;
428: sql = mandoc_realloc(sql, sz);
429: strlcat(sql, "(", sz);
430:
431: for ( ; NULL != e; e = e->next) {
1.8 schwarze 432: sz += (NULL == e->substr ? regexpsz : substrsz) +
1.1 kristaps 433: (NULL == e->next ? 3 : 5);
434: sql = mandoc_realloc(sql, sz);
1.8 schwarze 435: strlcat(sql, NULL == e->substr ? regexp : substr, sz);
1.1 kristaps 436: strlcat(sql, NULL == e->next ? ");" : " OR ", sz);
437: }
438:
439: return(sql);
440: }
441:
442: /*
443: * Compile a set of string tokens into an expression.
444: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
445: * "(", "foo=bar", etc.).
446: */
447: static struct expr *
1.5 kristaps 448: exprcomp(const struct mansearch *search, int argc, char *argv[])
1.1 kristaps 449: {
1.8 schwarze 450: int i, cs;
1.1 kristaps 451: struct expr *first, *next, *cur;
452:
453: first = cur = NULL;
454:
455: for (i = 0; i < argc; i++) {
1.8 schwarze 456: if (0 == strcmp("-i", argv[i])) {
457: if (++i >= argc)
458: return(NULL);
459: cs = 0;
460: } else
461: cs = 1;
462: next = exprterm(search, argv[i], cs);
1.1 kristaps 463: if (NULL == next) {
464: exprfree(first);
465: return(NULL);
466: }
467: if (NULL != first) {
468: cur->next = next;
469: cur = next;
470: } else
471: cur = first = next;
472: }
473:
474: return(first);
475: }
476:
477: static struct expr *
1.8 schwarze 478: exprterm(const struct mansearch *search, char *buf, int cs)
1.1 kristaps 479: {
480: struct expr *e;
481: char *key, *v;
482: size_t i;
483:
484: if ('\0' == *buf)
485: return(NULL);
486:
487: e = mandoc_calloc(1, sizeof(struct expr));
488:
1.5 kristaps 489: /*"whatis" mode uses an opaque string and default fields. */
490:
491: if (MANSEARCH_WHATIS & search->flags) {
1.8 schwarze 492: e->substr = buf;
1.5 kristaps 493: e->bits = search->deftype;
494: return(e);
495: }
496:
1.1 kristaps 497: /*
498: * If no =~ is specified, search with equality over names and
499: * descriptions.
500: * If =~ begins the phrase, use name and description fields.
501: */
502:
503: if (NULL == (v = strpbrk(buf, "=~"))) {
1.8 schwarze 504: e->substr = buf;
1.5 kristaps 505: e->bits = search->deftype;
1.1 kristaps 506: return(e);
507: } else if (v == buf)
1.5 kristaps 508: e->bits = search->deftype;
1.1 kristaps 509:
1.8 schwarze 510: if ('~' == *v++) {
511: if (regcomp(&e->regexp, v,
512: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE))) {
513: free(e);
514: return(NULL);
515: }
516: } else
517: e->substr = v;
518: v[-1] = '\0';
1.1 kristaps 519:
520: /*
521: * Parse out all possible fields.
522: * If the field doesn't resolve, bail.
523: */
524:
525: while (NULL != (key = strsep(&buf, ","))) {
526: if ('\0' == *key)
527: continue;
528: i = 0;
529: while (types[i].bits &&
530: strcasecmp(types[i].name, key))
531: i++;
532: if (0 == types[i].bits) {
533: free(e);
534: return(NULL);
535: }
536: e->bits |= types[i].bits;
537: }
538:
539: return(e);
540: }
541:
542: static void
543: exprfree(struct expr *p)
544: {
545: struct expr *pp;
546:
547: while (NULL != p) {
548: pp = p->next;
549: free(p);
550: p = pp;
551: }
552: }
553:
554: static void *
555: hash_halloc(size_t sz, void *arg)
556: {
557:
558: return(mandoc_calloc(sz, 1));
559: }
560:
561: static void *
562: hash_alloc(size_t sz, void *arg)
563: {
564:
565: return(mandoc_malloc(sz));
566: }
567:
568: static void
569: hash_free(void *p, size_t sz, void *arg)
570: {
571:
572: free(p);
573: }
CVSweb