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