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