Annotation of mandoc/mansearch.c, Revision 1.53
1.53 ! schwarze 1: /* $Id: mansearch.c,v 1.52 2014/12/06 01:23:24 schwarze Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.53 ! schwarze 4: * Copyright (c) 2013, 2014, 2015 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: #include "config.h"
19:
1.28 schwarze 20: #include <sys/mman.h>
1.43 schwarze 21: #include <sys/types.h>
22:
1.1 kristaps 23: #include <assert.h>
1.52 schwarze 24: #include <errno.h>
1.1 kristaps 25: #include <fcntl.h>
26: #include <getopt.h>
1.6 schwarze 27: #include <limits.h>
1.8 schwarze 28: #include <regex.h>
1.1 kristaps 29: #include <stdio.h>
30: #include <stdint.h>
31: #include <stddef.h>
32: #include <stdlib.h>
33: #include <string.h>
34: #include <unistd.h>
35:
1.44 schwarze 36: #if HAVE_OHASH
1.1 kristaps 37: #include <ohash.h>
1.4 kristaps 38: #else
39: #include "compat_ohash.h"
40: #endif
1.1 kristaps 41: #include <sqlite3.h>
1.40 schwarze 42: #ifndef SQLITE_DETERMINISTIC
43: #define SQLITE_DETERMINISTIC 0
44: #endif
1.1 kristaps 45:
46: #include "mandoc.h"
1.23 schwarze 47: #include "mandoc_aux.h"
1.1 kristaps 48: #include "manpath.h"
49: #include "mansearch.h"
50:
1.20 schwarze 51: extern int mansearch_keymax;
52: extern const char *const mansearch_keynames[];
53:
1.3 kristaps 54: #define SQL_BIND_TEXT(_db, _s, _i, _v) \
1.8 schwarze 55: do { if (SQLITE_OK != sqlite3_bind_text \
1.2 kristaps 56: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
1.8 schwarze 57: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
58: } while (0)
1.3 kristaps 59: #define SQL_BIND_INT64(_db, _s, _i, _v) \
1.8 schwarze 60: do { if (SQLITE_OK != sqlite3_bind_int64 \
1.2 kristaps 61: ((_s), (_i)++, (_v))) \
1.8 schwarze 62: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
63: } while (0)
64: #define SQL_BIND_BLOB(_db, _s, _i, _v) \
65: do { if (SQLITE_OK != sqlite3_bind_blob \
66: ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
67: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
68: } while (0)
1.2 kristaps 69:
1.1 kristaps 70: struct expr {
1.38 schwarze 71: regex_t regexp; /* compiled regexp, if applicable */
72: const char *substr; /* to search for, if applicable */
73: struct expr *next; /* next in sequence */
1.34 schwarze 74: uint64_t bits; /* type-mask */
1.38 schwarze 75: int equal; /* equality, not subsring match */
1.13 schwarze 76: int open; /* opening parentheses before */
77: int and; /* logical AND before */
78: int close; /* closing parentheses after */
1.1 kristaps 79: };
80:
81: struct match {
1.32 schwarze 82: uint64_t pageid; /* identifier in database */
1.50 schwarze 83: uint64_t bits; /* name type mask */
1.26 schwarze 84: char *desc; /* manual page description */
1.48 schwarze 85: int form; /* bit field: formatted, zipped? */
1.1 kristaps 86: };
87:
1.53 ! schwarze 88: static void buildnames(const struct mansearch *,
! 89: struct manpage *, sqlite3 *,
1.19 schwarze 90: sqlite3_stmt *, uint64_t,
91: const char *, int form);
1.12 schwarze 92: static char *buildoutput(sqlite3 *, sqlite3_stmt *,
93: uint64_t, uint64_t);
1.1 kristaps 94: static void *hash_alloc(size_t, void *);
1.37 schwarze 95: static void hash_free(void *, void *);
96: static void *hash_calloc(size_t, size_t, void *);
1.34 schwarze 97: static struct expr *exprcomp(const struct mansearch *,
1.5 kristaps 98: int, char *[]);
1.1 kristaps 99: static void exprfree(struct expr *);
1.8 schwarze 100: static struct expr *exprterm(const struct mansearch *, char *, int);
1.39 schwarze 101: static int manpage_compare(const void *, const void *);
1.13 schwarze 102: static void sql_append(char **sql, size_t *sz,
103: const char *newstr, int count);
1.7 schwarze 104: static void sql_match(sqlite3_context *context,
105: int argc, sqlite3_value **argv);
1.8 schwarze 106: static void sql_regexp(sqlite3_context *context,
107: int argc, sqlite3_value **argv);
1.15 schwarze 108: static char *sql_statement(const struct expr *);
1.28 schwarze 109:
1.34 schwarze 110:
1.28 schwarze 111: int
112: mansearch_setup(int start)
113: {
114: static void *pagecache;
115: int c;
116:
117: #define PC_PAGESIZE 1280
118: #define PC_NUMPAGES 256
119:
120: if (start) {
121: if (NULL != pagecache) {
122: fprintf(stderr, "pagecache already enabled\n");
123: return((int)MANDOCLEVEL_BADARG);
124: }
125:
126: pagecache = mmap(NULL, PC_PAGESIZE * PC_NUMPAGES,
1.42 schwarze 127: PROT_READ | PROT_WRITE,
128: MAP_SHARED | MAP_ANON, -1, 0);
1.28 schwarze 129:
130: if (MAP_FAILED == pagecache) {
131: perror("mmap");
132: pagecache = NULL;
133: return((int)MANDOCLEVEL_SYSERR);
134: }
135:
136: c = sqlite3_config(SQLITE_CONFIG_PAGECACHE,
137: pagecache, PC_PAGESIZE, PC_NUMPAGES);
138:
139: if (SQLITE_OK == c)
140: return((int)MANDOCLEVEL_OK);
141:
142: fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c));
143:
144: } else if (NULL == pagecache) {
145: fprintf(stderr, "pagecache missing\n");
146: return((int)MANDOCLEVEL_BADARG);
147: }
148:
149: if (-1 == munmap(pagecache, PC_PAGESIZE * PC_NUMPAGES)) {
150: perror("munmap");
151: pagecache = NULL;
152: return((int)MANDOCLEVEL_SYSERR);
153: }
154:
155: pagecache = NULL;
156: return((int)MANDOCLEVEL_OK);
157: }
1.1 kristaps 158:
159: int
1.5 kristaps 160: mansearch(const struct mansearch *search,
1.12 schwarze 161: const struct manpaths *paths,
162: int argc, char *argv[],
1.1 kristaps 163: struct manpage **res, size_t *sz)
164: {
1.20 schwarze 165: int fd, rc, c, indexbit;
1.32 schwarze 166: int64_t pageid;
1.20 schwarze 167: uint64_t outbit, iterbit;
1.6 schwarze 168: char buf[PATH_MAX];
1.11 schwarze 169: char *sql;
1.10 schwarze 170: struct manpage *mpage;
1.1 kristaps 171: struct expr *e, *ep;
172: sqlite3 *db;
1.12 schwarze 173: sqlite3_stmt *s, *s2;
1.1 kristaps 174: struct match *mp;
175: struct ohash_info info;
176: struct ohash htab;
177: unsigned int idx;
178: size_t i, j, cur, maxres;
179:
1.37 schwarze 180: info.calloc = hash_calloc;
1.1 kristaps 181: info.alloc = hash_alloc;
1.37 schwarze 182: info.free = hash_free;
1.32 schwarze 183: info.key_offset = offsetof(struct match, pageid);
1.1 kristaps 184:
1.2 kristaps 185: *sz = cur = maxres = 0;
1.1 kristaps 186: sql = NULL;
187: *res = NULL;
188: fd = -1;
189: e = NULL;
1.2 kristaps 190: rc = 0;
1.1 kristaps 191:
192: if (0 == argc)
193: goto out;
1.5 kristaps 194: if (NULL == (e = exprcomp(search, argc, argv)))
1.1 kristaps 195: goto out;
196:
1.12 schwarze 197: outbit = 0;
1.45 schwarze 198: if (NULL != search->outkey) {
1.20 schwarze 199: for (indexbit = 0, iterbit = 1;
200: indexbit < mansearch_keymax;
201: indexbit++, iterbit <<= 1) {
1.45 schwarze 202: if (0 == strcasecmp(search->outkey,
1.20 schwarze 203: mansearch_keynames[indexbit])) {
204: outbit = iterbit;
1.12 schwarze 205: break;
206: }
207: }
208: }
209:
1.1 kristaps 210: /*
211: * Save a descriptor to the current working directory.
212: * Since pathnames in the "paths" variable might be relative,
213: * and we'll be chdir()ing into them, we need to keep a handle
214: * on our current directory from which to start the chdir().
215: */
216:
1.6 schwarze 217: if (NULL == getcwd(buf, PATH_MAX)) {
1.30 schwarze 218: perror("getcwd");
1.1 kristaps 219: goto out;
220: } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
221: perror(buf);
222: goto out;
223: }
224:
1.15 schwarze 225: sql = sql_statement(e);
1.1 kristaps 226:
227: /*
228: * Loop over the directories (containing databases) for us to
229: * search.
230: * Don't let missing/bad databases/directories phase us.
231: * In each, try to open the resident database and, if it opens,
232: * scan it for our match expression.
233: */
234:
235: for (i = 0; i < paths->sz; i++) {
236: if (-1 == fchdir(fd)) {
237: perror(buf);
238: free(*res);
239: break;
240: } else if (-1 == chdir(paths->paths[i])) {
241: perror(paths->paths[i]);
242: continue;
1.34 schwarze 243: }
1.1 kristaps 244:
1.34 schwarze 245: c = sqlite3_open_v2(MANDOC_DB, &db,
246: SQLITE_OPEN_READONLY, NULL);
1.1 kristaps 247:
1.2 kristaps 248: if (SQLITE_OK != c) {
1.52 schwarze 249: fprintf(stderr, "%s/%s: %s\n",
250: paths->paths[i], MANDOC_DB, strerror(errno));
1.1 kristaps 251: sqlite3_close(db);
252: continue;
253: }
254:
1.8 schwarze 255: /*
256: * Define the SQL functions for substring
257: * and regular expression matching.
258: */
1.7 schwarze 259:
260: c = sqlite3_create_function(db, "match", 2,
1.31 schwarze 261: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
262: NULL, sql_match, NULL, NULL);
1.8 schwarze 263: assert(SQLITE_OK == c);
264: c = sqlite3_create_function(db, "regexp", 2,
1.31 schwarze 265: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
266: NULL, sql_regexp, NULL, NULL);
1.8 schwarze 267: assert(SQLITE_OK == c);
1.7 schwarze 268:
1.1 kristaps 269: j = 1;
1.2 kristaps 270: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
271: if (SQLITE_OK != c)
272: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 273:
274: for (ep = e; NULL != ep; ep = ep->next) {
1.8 schwarze 275: if (NULL == ep->substr) {
276: SQL_BIND_BLOB(db, s, j, ep->regexp);
277: } else
278: SQL_BIND_TEXT(db, s, j, ep->substr);
1.27 schwarze 279: if (0 == ((TYPE_Nd | TYPE_Nm) & ep->bits))
1.26 schwarze 280: SQL_BIND_INT64(db, s, j, ep->bits);
1.1 kristaps 281: }
282:
283: memset(&htab, 0, sizeof(struct ohash));
284: ohash_init(&htab, 4, &info);
285:
286: /*
287: * Hash each entry on its [unique] document identifier.
288: * This is a uint64_t.
289: * Instead of using a hash function, simply convert the
290: * uint64_t to a uint32_t, the hash value's type.
291: * This gives good performance and preserves the
292: * distribution of buckets in the table.
293: */
1.2 kristaps 294: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.32 schwarze 295: pageid = sqlite3_column_int64(s, 2);
1.34 schwarze 296: idx = ohash_lookup_memory(&htab,
297: (char *)&pageid, sizeof(uint64_t),
298: (uint32_t)pageid);
1.1 kristaps 299:
300: if (NULL != ohash_find(&htab, idx))
301: continue;
302:
303: mp = mandoc_calloc(1, sizeof(struct match));
1.32 schwarze 304: mp->pageid = pageid;
1.26 schwarze 305: mp->form = sqlite3_column_int(s, 1);
1.50 schwarze 306: mp->bits = sqlite3_column_int64(s, 3);
1.26 schwarze 307: if (TYPE_Nd == outbit)
1.41 schwarze 308: mp->desc = mandoc_strdup((const char *)
1.26 schwarze 309: sqlite3_column_text(s, 0));
1.1 kristaps 310: ohash_insert(&htab, idx, mp);
311: }
312:
1.2 kristaps 313: if (SQLITE_DONE != c)
314: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
315:
1.1 kristaps 316: sqlite3_finalize(s);
1.10 schwarze 317:
1.34 schwarze 318: c = sqlite3_prepare_v2(db,
1.35 schwarze 319: "SELECT sec, arch, name, pageid FROM mlinks "
320: "WHERE pageid=? ORDER BY sec, arch, name",
1.10 schwarze 321: -1, &s, NULL);
322: if (SQLITE_OK != c)
323: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 324:
1.12 schwarze 325: c = sqlite3_prepare_v2(db,
1.35 schwarze 326: "SELECT bits, key, pageid FROM keys "
327: "WHERE pageid=? AND bits & ?",
1.12 schwarze 328: -1, &s2, NULL);
329: if (SQLITE_OK != c)
330: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
331:
1.1 kristaps 332: for (mp = ohash_first(&htab, &idx);
333: NULL != mp;
334: mp = ohash_next(&htab, &idx)) {
335: if (cur + 1 > maxres) {
336: maxres += 1024;
1.36 schwarze 337: *res = mandoc_reallocarray(*res,
338: maxres, sizeof(struct manpage));
1.1 kristaps 339: }
1.10 schwarze 340: mpage = *res + cur;
1.47 schwarze 341: mpage->ipath = i;
1.50 schwarze 342: mpage->bits = mp->bits;
1.39 schwarze 343: mpage->sec = 10;
1.10 schwarze 344: mpage->form = mp->form;
1.53 ! schwarze 345: buildnames(search, mpage, db, s, mp->pageid,
1.19 schwarze 346: paths->paths[i], mp->form);
1.53 ! schwarze 347: if (mpage->names != NULL) {
! 348: mpage->output = TYPE_Nd & outbit ?
! 349: mp->desc : outbit ?
! 350: buildoutput(db, s2, mp->pageid, outbit) :
! 351: NULL;
! 352: cur++;
! 353: }
1.1 kristaps 354: free(mp);
355: }
1.10 schwarze 356:
357: sqlite3_finalize(s);
1.12 schwarze 358: sqlite3_finalize(s2);
1.10 schwarze 359: sqlite3_close(db);
1.1 kristaps 360: ohash_delete(&htab);
1.49 schwarze 361:
362: /*
363: * In man(1) mode, prefer matches in earlier trees
364: * over matches in later trees.
365: */
366:
367: if (cur && search->firstmatch)
368: break;
1.1 kristaps 369: }
1.39 schwarze 370: qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.2 kristaps 371: rc = 1;
1.1 kristaps 372: out:
1.30 schwarze 373: if (-1 != fd) {
374: if (-1 == fchdir(fd))
375: perror(buf);
376: close(fd);
377: }
1.1 kristaps 378: exprfree(e);
379: free(sql);
380: *sz = cur;
1.2 kristaps 381: return(rc);
1.11 schwarze 382: }
383:
1.45 schwarze 384: void
385: mansearch_free(struct manpage *res, size_t sz)
386: {
387: size_t i;
388:
389: for (i = 0; i < sz; i++) {
390: free(res[i].file);
391: free(res[i].names);
392: free(res[i].output);
393: }
394: free(res);
395: }
396:
1.39 schwarze 397: static int
398: manpage_compare(const void *vp1, const void *vp2)
399: {
400: const struct manpage *mp1, *mp2;
401: int diff;
402:
403: mp1 = vp1;
404: mp2 = vp2;
1.50 schwarze 405: return( (diff = mp2->bits - mp1->bits) ? diff :
406: (diff = mp1->sec - mp2->sec) ? diff :
407: strcasecmp(mp1->names, mp2->names));
1.39 schwarze 408: }
409:
1.17 schwarze 410: static void
1.53 ! schwarze 411: buildnames(const struct mansearch *search, struct manpage *mpage,
! 412: sqlite3 *db, sqlite3_stmt *s,
1.32 schwarze 413: uint64_t pageid, const char *path, int form)
1.11 schwarze 414: {
1.22 schwarze 415: char *newnames, *prevsec, *prevarch;
1.19 schwarze 416: const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
1.11 schwarze 417: size_t i;
418: int c;
419:
1.25 schwarze 420: mpage->file = NULL;
1.17 schwarze 421: mpage->names = NULL;
1.22 schwarze 422: prevsec = prevarch = NULL;
1.11 schwarze 423: i = 1;
1.32 schwarze 424: SQL_BIND_INT64(db, s, i, pageid);
1.11 schwarze 425: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.17 schwarze 426:
1.22 schwarze 427: /* Decide whether we already have some names. */
1.17 schwarze 428:
429: if (NULL == mpage->names) {
1.11 schwarze 430: oldnames = "";
431: sep1 = "";
432: } else {
1.17 schwarze 433: oldnames = mpage->names;
1.11 schwarze 434: sep1 = ", ";
435: }
1.22 schwarze 436:
1.53 ! schwarze 437: /* Fetch the next name, rejecting sec/arch mismatches. */
1.22 schwarze 438:
1.41 schwarze 439: sec = (const char *)sqlite3_column_text(s, 0);
1.53 ! schwarze 440: if (search->sec != NULL && strcasecmp(sec, search->sec))
! 441: continue;
1.41 schwarze 442: arch = (const char *)sqlite3_column_text(s, 1);
1.53 ! schwarze 443: if (search->arch != NULL && *arch != '\0' &&
! 444: strcasecmp(arch, search->arch))
! 445: continue;
1.41 schwarze 446: name = (const char *)sqlite3_column_text(s, 2);
1.39 schwarze 447:
448: /* Remember the first section found. */
449:
450: if (9 < mpage->sec && '1' <= *sec && '9' >= *sec)
451: mpage->sec = (*sec - '1') + 1;
1.22 schwarze 452:
453: /* If the section changed, append the old one. */
454:
455: if (NULL != prevsec &&
456: (strcmp(sec, prevsec) ||
457: strcmp(arch, prevarch))) {
458: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 459: mandoc_asprintf(&newnames, "%s(%s%s%s)",
460: oldnames, prevsec, sep2, prevarch);
1.22 schwarze 461: free(mpage->names);
462: oldnames = mpage->names = newnames;
463: free(prevsec);
464: free(prevarch);
465: prevsec = prevarch = NULL;
466: }
467:
468: /* Save the new section, to append it later. */
469:
470: if (NULL == prevsec) {
471: prevsec = mandoc_strdup(sec);
472: prevarch = mandoc_strdup(arch);
473: }
474:
475: /* Append the new name. */
476:
1.24 schwarze 477: mandoc_asprintf(&newnames, "%s%s%s",
478: oldnames, sep1, name);
1.17 schwarze 479: free(mpage->names);
480: mpage->names = newnames;
481:
482: /* Also save the first file name encountered. */
483:
1.51 schwarze 484: if (mpage->file != NULL)
1.17 schwarze 485: continue;
486:
1.48 schwarze 487: if (form & FORM_SRC) {
1.19 schwarze 488: sep1 = "man";
489: fsec = sec;
490: } else {
491: sep1 = "cat";
492: fsec = "0";
493: }
1.51 schwarze 494: sep2 = *arch == '\0' ? "" : "/";
495: mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
496: path, sep1, sec, sep2, arch, name, fsec);
1.11 schwarze 497: }
1.51 schwarze 498: if (c != SQLITE_DONE)
1.11 schwarze 499: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
500: sqlite3_reset(s);
1.22 schwarze 501:
502: /* Append one final section to the names. */
503:
1.51 schwarze 504: if (prevsec != NULL) {
505: sep2 = *prevarch == '\0' ? "" : "/";
1.24 schwarze 506: mandoc_asprintf(&newnames, "%s(%s%s%s)",
507: mpage->names, prevsec, sep2, prevarch);
1.22 schwarze 508: free(mpage->names);
509: mpage->names = newnames;
510: free(prevsec);
511: free(prevarch);
512: }
1.12 schwarze 513: }
514:
515: static char *
1.32 schwarze 516: buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
1.12 schwarze 517: {
518: char *output, *newoutput;
519: const char *oldoutput, *sep1, *data;
520: size_t i;
521: int c;
522:
523: output = NULL;
524: i = 1;
1.32 schwarze 525: SQL_BIND_INT64(db, s, i, pageid);
1.12 schwarze 526: SQL_BIND_INT64(db, s, i, outbit);
527: while (SQLITE_ROW == (c = sqlite3_step(s))) {
528: if (NULL == output) {
529: oldoutput = "";
530: sep1 = "";
531: } else {
532: oldoutput = output;
533: sep1 = " # ";
534: }
1.41 schwarze 535: data = (const char *)sqlite3_column_text(s, 1);
1.24 schwarze 536: mandoc_asprintf(&newoutput, "%s%s%s",
537: oldoutput, sep1, data);
1.12 schwarze 538: free(output);
539: output = newoutput;
540: }
541: if (SQLITE_DONE != c)
542: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
543: sqlite3_reset(s);
544: return(output);
1.1 kristaps 545: }
546:
547: /*
1.7 schwarze 548: * Implement substring match as an application-defined SQL function.
549: * Using the SQL LIKE or GLOB operators instead would be a bad idea
550: * because that would require escaping metacharacters in the string
551: * being searched for.
552: */
553: static void
554: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
555: {
556:
557: assert(2 == argc);
558: sqlite3_result_int(context, NULL != strcasestr(
559: (const char *)sqlite3_value_text(argv[1]),
560: (const char *)sqlite3_value_text(argv[0])));
561: }
562:
563: /*
1.8 schwarze 564: * Implement regular expression match
565: * as an application-defined SQL function.
566: */
567: static void
568: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
569: {
570:
571: assert(2 == argc);
572: sqlite3_result_int(context, !regexec(
573: (regex_t *)sqlite3_value_blob(argv[0]),
574: (const char *)sqlite3_value_text(argv[1]),
575: 0, NULL, 0));
576: }
577:
1.13 schwarze 578: static void
579: sql_append(char **sql, size_t *sz, const char *newstr, int count)
580: {
581: size_t newsz;
582:
583: newsz = 1 < count ? (size_t)count : strlen(newstr);
584: *sql = mandoc_realloc(*sql, *sz + newsz + 1);
585: if (1 < count)
586: memset(*sql + *sz, *newstr, (size_t)count);
587: else
588: memcpy(*sql + *sz, newstr, newsz);
589: *sz += newsz;
590: (*sql)[*sz] = '\0';
591: }
592:
1.8 schwarze 593: /*
1.1 kristaps 594: * Prepare the search SQL statement.
595: */
596: static char *
1.15 schwarze 597: sql_statement(const struct expr *e)
1.1 kristaps 598: {
599: char *sql;
600: size_t sz;
1.13 schwarze 601: int needop;
1.1 kristaps 602:
1.50 schwarze 603: sql = mandoc_strdup(e->equal ?
604: "SELECT desc, form, pageid, bits "
605: "FROM mpages NATURAL JOIN names WHERE " :
606: "SELECT desc, form, pageid, 0 FROM mpages WHERE ");
1.1 kristaps 607: sz = strlen(sql);
1.2 kristaps 608:
1.13 schwarze 609: for (needop = 0; NULL != e; e = e->next) {
610: if (e->and)
611: sql_append(&sql, &sz, " AND ", 1);
612: else if (needop)
613: sql_append(&sql, &sz, " OR ", 1);
614: if (e->open)
615: sql_append(&sql, &sz, "(", e->open);
1.26 schwarze 616: sql_append(&sql, &sz,
617: TYPE_Nd & e->bits
618: ? (NULL == e->substr
619: ? "desc REGEXP ?"
620: : "desc MATCH ?")
1.27 schwarze 621: : TYPE_Nm == e->bits
622: ? (NULL == e->substr
1.32 schwarze 623: ? "pageid IN (SELECT pageid FROM names "
1.27 schwarze 624: "WHERE name REGEXP ?)"
1.38 schwarze 625: : e->equal
1.50 schwarze 626: ? "name = ? "
1.32 schwarze 627: : "pageid IN (SELECT pageid FROM names "
1.27 schwarze 628: "WHERE name MATCH ?)")
1.26 schwarze 629: : (NULL == e->substr
1.32 schwarze 630: ? "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 631: "WHERE key REGEXP ? AND bits & ?)"
1.32 schwarze 632: : "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 633: "WHERE key MATCH ? AND bits & ?)"), 1);
1.13 schwarze 634: if (e->close)
635: sql_append(&sql, &sz, ")", e->close);
636: needop = 1;
1.1 kristaps 637: }
638:
639: return(sql);
640: }
641:
642: /*
643: * Compile a set of string tokens into an expression.
644: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
645: * "(", "foo=bar", etc.).
646: */
647: static struct expr *
1.5 kristaps 648: exprcomp(const struct mansearch *search, int argc, char *argv[])
1.1 kristaps 649: {
1.27 schwarze 650: uint64_t mask;
1.13 schwarze 651: int i, toopen, logic, igncase, toclose;
1.27 schwarze 652: struct expr *first, *prev, *cur, *next;
1.1 kristaps 653:
654: first = cur = NULL;
1.53 ! schwarze 655: logic = igncase = toopen = toclose = 0;
1.1 kristaps 656:
657: for (i = 0; i < argc; i++) {
1.13 schwarze 658: if (0 == strcmp("(", argv[i])) {
659: if (igncase)
660: goto fail;
661: toopen++;
662: toclose++;
663: continue;
664: } else if (0 == strcmp(")", argv[i])) {
665: if (toopen || logic || igncase || NULL == cur)
666: goto fail;
667: cur->close++;
668: if (0 > --toclose)
669: goto fail;
670: continue;
671: } else if (0 == strcmp("-a", argv[i])) {
672: if (toopen || logic || igncase || NULL == cur)
673: goto fail;
674: logic = 1;
675: continue;
676: } else if (0 == strcmp("-o", argv[i])) {
677: if (toopen || logic || igncase || NULL == cur)
678: goto fail;
679: logic = 2;
680: continue;
681: } else if (0 == strcmp("-i", argv[i])) {
682: if (igncase)
683: goto fail;
684: igncase = 1;
685: continue;
1.1 kristaps 686: }
1.13 schwarze 687: next = exprterm(search, argv[i], !igncase);
688: if (NULL == next)
689: goto fail;
1.26 schwarze 690: if (NULL == first)
691: first = next;
692: else
1.1 kristaps 693: cur->next = next;
1.27 schwarze 694: prev = cur = next;
1.26 schwarze 695:
696: /*
697: * Searching for descriptions must be split out
698: * because they are stored in the mpages table,
699: * not in the keys table.
700: */
701:
1.27 schwarze 702: for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
703: if (mask & cur->bits && ~mask & cur->bits) {
704: next = mandoc_calloc(1,
705: sizeof(struct expr));
706: memcpy(next, cur, sizeof(struct expr));
707: prev->open = 1;
708: cur->bits = mask;
709: cur->next = next;
710: cur = next;
711: cur->bits &= ~mask;
712: }
713: }
714: prev->and = (1 == logic);
715: prev->open += toopen;
716: if (cur != prev)
1.26 schwarze 717: cur->close = 1;
1.27 schwarze 718:
1.13 schwarze 719: toopen = logic = igncase = 0;
1.1 kristaps 720: }
1.53 ! schwarze 721: if ( ! (toopen || logic || igncase || toclose))
! 722: return(first);
1.15 schwarze 723:
1.13 schwarze 724: fail:
725: if (NULL != first)
726: exprfree(first);
727: return(NULL);
1.15 schwarze 728: }
729:
730: static struct expr *
1.8 schwarze 731: exprterm(const struct mansearch *search, char *buf, int cs)
1.1 kristaps 732: {
1.15 schwarze 733: char errbuf[BUFSIZ];
1.1 kristaps 734: struct expr *e;
1.38 schwarze 735: char *key, *val;
1.20 schwarze 736: uint64_t iterbit;
737: int i, irc;
1.1 kristaps 738:
739: if ('\0' == *buf)
740: return(NULL);
741:
742: e = mandoc_calloc(1, sizeof(struct expr));
743:
1.45 schwarze 744: if (search->argmode == ARG_NAME) {
745: e->bits = TYPE_Nm;
1.8 schwarze 746: e->substr = buf;
1.38 schwarze 747: e->equal = 1;
1.5 kristaps 748: return(e);
749: }
750:
1.1 kristaps 751: /*
1.45 schwarze 752: * Separate macro keys from search string.
753: * If needed, request regular expression handling
754: * by setting e->substr to NULL.
1.1 kristaps 755: */
756:
1.45 schwarze 757: if (search->argmode == ARG_WORD) {
758: e->bits = TYPE_Nm;
759: e->substr = NULL;
760: mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", buf);
1.46 schwarze 761: cs = 0;
1.45 schwarze 762: } else if ((val = strpbrk(buf, "=~")) == NULL) {
763: e->bits = TYPE_Nm | TYPE_Nd;
1.8 schwarze 764: e->substr = buf;
1.38 schwarze 765: } else {
766: if (val == buf)
1.45 schwarze 767: e->bits = TYPE_Nm | TYPE_Nd;
1.38 schwarze 768: if ('=' == *val)
769: e->substr = val + 1;
770: *val++ = '\0';
1.21 schwarze 771: if (NULL != strstr(buf, "arch"))
772: cs = 0;
1.38 schwarze 773: }
774:
775: /* Compile regular expressions. */
776:
777: if (NULL == e->substr) {
778: irc = regcomp(&e->regexp, val,
779: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.45 schwarze 780: if (search->argmode == ARG_WORD)
1.38 schwarze 781: free(val);
782: if (irc) {
1.15 schwarze 783: regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
784: fprintf(stderr, "regcomp: %s\n", errbuf);
1.8 schwarze 785: free(e);
786: return(NULL);
787: }
1.38 schwarze 788: }
789:
790: if (e->bits)
791: return(e);
1.1 kristaps 792:
793: /*
794: * Parse out all possible fields.
795: * If the field doesn't resolve, bail.
796: */
797:
798: while (NULL != (key = strsep(&buf, ","))) {
799: if ('\0' == *key)
800: continue;
1.20 schwarze 801: for (i = 0, iterbit = 1;
802: i < mansearch_keymax;
803: i++, iterbit <<= 1) {
804: if (0 == strcasecmp(key,
805: mansearch_keynames[i])) {
806: e->bits |= iterbit;
807: break;
808: }
809: }
810: if (i == mansearch_keymax) {
811: if (strcasecmp(key, "any")) {
812: free(e);
813: return(NULL);
814: }
815: e->bits |= ~0ULL;
1.1 kristaps 816: }
817: }
818:
819: return(e);
820: }
821:
822: static void
823: exprfree(struct expr *p)
824: {
825: struct expr *pp;
826:
827: while (NULL != p) {
828: pp = p->next;
829: free(p);
830: p = pp;
831: }
832: }
833:
834: static void *
1.37 schwarze 835: hash_calloc(size_t nmemb, size_t sz, void *arg)
1.1 kristaps 836: {
837:
1.37 schwarze 838: return(mandoc_calloc(nmemb, sz));
1.1 kristaps 839: }
840:
841: static void *
842: hash_alloc(size_t sz, void *arg)
843: {
844:
845: return(mandoc_malloc(sz));
846: }
847:
848: static void
1.37 schwarze 849: hash_free(void *p, void *arg)
1.1 kristaps 850: {
851:
852: free(p);
853: }
CVSweb