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