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