Annotation of mandoc/mansearch.c, Revision 1.37
1.37 ! schwarze 1: /* $Id: mansearch.c,v 1.36 2014/04/23 21:06:41 schwarze Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.13 schwarze 4: * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.28 schwarze 22: #include <sys/mman.h>
1.1 kristaps 23: #include <assert.h>
24: #include <fcntl.h>
25: #include <getopt.h>
1.6 schwarze 26: #include <limits.h>
1.8 schwarze 27: #include <regex.h>
1.1 kristaps 28: #include <stdio.h>
29: #include <stdint.h>
30: #include <stddef.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34:
1.4 kristaps 35: #ifdef HAVE_OHASH
1.1 kristaps 36: #include <ohash.h>
1.4 kristaps 37: #else
38: #include "compat_ohash.h"
39: #endif
1.1 kristaps 40: #include <sqlite3.h>
41:
42: #include "mandoc.h"
1.23 schwarze 43: #include "mandoc_aux.h"
1.1 kristaps 44: #include "manpath.h"
45: #include "mansearch.h"
46:
1.20 schwarze 47: extern int mansearch_keymax;
48: extern const char *const mansearch_keynames[];
49:
1.3 kristaps 50: #define SQL_BIND_TEXT(_db, _s, _i, _v) \
1.8 schwarze 51: do { if (SQLITE_OK != sqlite3_bind_text \
1.2 kristaps 52: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
1.8 schwarze 53: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
54: } while (0)
1.3 kristaps 55: #define SQL_BIND_INT64(_db, _s, _i, _v) \
1.8 schwarze 56: do { if (SQLITE_OK != sqlite3_bind_int64 \
1.2 kristaps 57: ((_s), (_i)++, (_v))) \
1.8 schwarze 58: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
59: } while (0)
60: #define SQL_BIND_BLOB(_db, _s, _i, _v) \
61: do { if (SQLITE_OK != sqlite3_bind_blob \
62: ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
63: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
64: } while (0)
1.2 kristaps 65:
1.1 kristaps 66: struct expr {
1.34 schwarze 67: uint64_t bits; /* type-mask */
1.8 schwarze 68: const char *substr; /* to search for, if applicable */
69: regex_t regexp; /* compiled regexp, if applicable */
1.13 schwarze 70: int open; /* opening parentheses before */
71: int and; /* logical AND before */
72: int close; /* closing parentheses after */
1.8 schwarze 73: struct expr *next; /* next in sequence */
1.1 kristaps 74: };
75:
76: struct match {
1.32 schwarze 77: uint64_t pageid; /* identifier in database */
1.26 schwarze 78: char *desc; /* manual page description */
1.1 kristaps 79: int form; /* 0 == catpage */
80: };
81:
1.17 schwarze 82: static void buildnames(struct manpage *, sqlite3 *,
1.19 schwarze 83: sqlite3_stmt *, uint64_t,
84: const char *, int form);
1.12 schwarze 85: static char *buildoutput(sqlite3 *, sqlite3_stmt *,
86: uint64_t, uint64_t);
1.1 kristaps 87: static void *hash_alloc(size_t, void *);
1.37 ! schwarze 88: static void hash_free(void *, void *);
! 89: static void *hash_calloc(size_t, size_t, void *);
1.34 schwarze 90: static struct expr *exprcomp(const struct mansearch *,
1.5 kristaps 91: int, char *[]);
1.1 kristaps 92: static void exprfree(struct expr *);
1.15 schwarze 93: static struct expr *exprspec(struct expr *, uint64_t,
94: const char *, const char *);
1.8 schwarze 95: static struct expr *exprterm(const struct mansearch *, char *, int);
1.13 schwarze 96: static void sql_append(char **sql, size_t *sz,
97: const char *newstr, int count);
1.7 schwarze 98: static void sql_match(sqlite3_context *context,
99: int argc, sqlite3_value **argv);
1.8 schwarze 100: static void sql_regexp(sqlite3_context *context,
101: int argc, sqlite3_value **argv);
1.15 schwarze 102: static char *sql_statement(const struct expr *);
1.28 schwarze 103:
1.34 schwarze 104:
1.28 schwarze 105: int
106: mansearch_setup(int start)
107: {
108: static void *pagecache;
109: int c;
110:
111: #define PC_PAGESIZE 1280
112: #define PC_NUMPAGES 256
113:
114: if (start) {
115: if (NULL != pagecache) {
116: fprintf(stderr, "pagecache already enabled\n");
117: return((int)MANDOCLEVEL_BADARG);
118: }
119:
120: pagecache = mmap(NULL, PC_PAGESIZE * PC_NUMPAGES,
121: PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
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 kristaps 151:
152: int
1.5 kristaps 153: mansearch(const struct mansearch *search,
1.12 schwarze 154: const struct manpaths *paths,
155: int argc, char *argv[],
156: const char *outkey,
1.1 kristaps 157: struct manpage **res, size_t *sz)
158: {
1.20 schwarze 159: int fd, rc, c, indexbit;
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_info info;
170: struct ohash htab;
171: unsigned int idx;
172: size_t i, j, cur, maxres;
173:
1.37 ! schwarze 174: info.calloc = hash_calloc;
1.1 kristaps 175: info.alloc = hash_alloc;
1.37 ! schwarze 176: info.free = hash_free;
1.32 schwarze 177: info.key_offset = offsetof(struct match, pageid);
1.1 kristaps 178:
1.2 kristaps 179: *sz = cur = maxres = 0;
1.1 kristaps 180: sql = NULL;
181: *res = NULL;
182: fd = -1;
183: e = NULL;
1.2 kristaps 184: rc = 0;
1.1 kristaps 185:
186: if (0 == argc)
187: goto out;
1.5 kristaps 188: if (NULL == (e = exprcomp(search, argc, argv)))
1.1 kristaps 189: goto out;
190:
1.12 schwarze 191: outbit = 0;
192: if (NULL != outkey) {
1.20 schwarze 193: for (indexbit = 0, iterbit = 1;
194: indexbit < mansearch_keymax;
195: indexbit++, iterbit <<= 1) {
196: if (0 == strcasecmp(outkey,
197: mansearch_keynames[indexbit])) {
198: outbit = iterbit;
1.12 schwarze 199: break;
200: }
201: }
202: }
203:
1.1 kristaps 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:
1.6 schwarze 211: if (NULL == getcwd(buf, PATH_MAX)) {
1.30 schwarze 212: perror("getcwd");
1.1 kristaps 213: goto out;
214: } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
215: perror(buf);
216: goto out;
217: }
218:
1.15 schwarze 219: sql = sql_statement(e);
1.1 kristaps 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.34 schwarze 237: }
1.1 kristaps 238:
1.34 schwarze 239: c = sqlite3_open_v2(MANDOC_DB, &db,
240: SQLITE_OPEN_READONLY, NULL);
1.1 kristaps 241:
1.2 kristaps 242: if (SQLITE_OK != c) {
1.1 kristaps 243: perror(MANDOC_DB);
244: sqlite3_close(db);
245: continue;
246: }
247:
1.8 schwarze 248: /*
249: * Define the SQL functions for substring
250: * and regular expression matching.
251: */
1.7 schwarze 252:
253: c = sqlite3_create_function(db, "match", 2,
1.31 schwarze 254: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
255: NULL, sql_match, NULL, NULL);
1.8 schwarze 256: assert(SQLITE_OK == c);
257: c = sqlite3_create_function(db, "regexp", 2,
1.31 schwarze 258: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
259: NULL, sql_regexp, NULL, NULL);
1.8 schwarze 260: assert(SQLITE_OK == c);
1.7 schwarze 261:
1.1 kristaps 262: j = 1;
1.2 kristaps 263: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
264: if (SQLITE_OK != c)
265: fprintf(stderr, "%s\n", 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:
276: memset(&htab, 0, sizeof(struct ohash));
277: ohash_init(&htab, 4, &info);
278:
279: /*
280: * Hash each entry on its [unique] document identifier.
281: * This is a uint64_t.
282: * Instead of using a hash function, simply convert the
283: * uint64_t to a uint32_t, the hash value's type.
284: * This gives good performance and preserves the
285: * distribution of buckets in the table.
286: */
1.2 kristaps 287: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.32 schwarze 288: pageid = sqlite3_column_int64(s, 2);
1.34 schwarze 289: idx = ohash_lookup_memory(&htab,
290: (char *)&pageid, sizeof(uint64_t),
291: (uint32_t)pageid);
1.1 kristaps 292:
293: if (NULL != ohash_find(&htab, idx))
294: continue;
295:
296: mp = mandoc_calloc(1, sizeof(struct match));
1.32 schwarze 297: mp->pageid = pageid;
1.26 schwarze 298: mp->form = sqlite3_column_int(s, 1);
299: if (TYPE_Nd == outbit)
300: mp->desc = mandoc_strdup(
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)
306: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
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)
315: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 316:
1.12 schwarze 317: c = sqlite3_prepare_v2(db,
1.35 schwarze 318: "SELECT bits, key, pageid FROM keys "
319: "WHERE pageid=? AND bits & ?",
1.12 schwarze 320: -1, &s2, NULL);
321: if (SQLITE_OK != c)
322: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
323:
1.1 kristaps 324: for (mp = ohash_first(&htab, &idx);
325: NULL != mp;
326: mp = ohash_next(&htab, &idx)) {
327: if (cur + 1 > maxres) {
328: maxres += 1024;
1.36 schwarze 329: *res = mandoc_reallocarray(*res,
330: maxres, sizeof(struct manpage));
1.1 kristaps 331: }
1.10 schwarze 332: mpage = *res + cur;
333: mpage->form = mp->form;
1.32 schwarze 334: buildnames(mpage, db, s, mp->pageid,
1.19 schwarze 335: paths->paths[i], mp->form);
1.26 schwarze 336: mpage->output = TYPE_Nd & outbit ?
337: mp->desc : outbit ?
1.32 schwarze 338: buildoutput(db, s2, mp->pageid, outbit) : NULL;
1.10 schwarze 339:
1.1 kristaps 340: free(mp);
341: cur++;
342: }
1.10 schwarze 343:
344: sqlite3_finalize(s);
1.12 schwarze 345: sqlite3_finalize(s2);
1.10 schwarze 346: sqlite3_close(db);
1.1 kristaps 347: ohash_delete(&htab);
348: }
1.2 kristaps 349: rc = 1;
1.1 kristaps 350: out:
1.30 schwarze 351: if (-1 != fd) {
352: if (-1 == fchdir(fd))
353: perror(buf);
354: close(fd);
355: }
1.1 kristaps 356: exprfree(e);
357: free(sql);
358: *sz = cur;
1.2 kristaps 359: return(rc);
1.11 schwarze 360: }
361:
1.17 schwarze 362: static void
363: buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
1.32 schwarze 364: uint64_t pageid, const char *path, int form)
1.11 schwarze 365: {
1.22 schwarze 366: char *newnames, *prevsec, *prevarch;
1.19 schwarze 367: const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
1.11 schwarze 368: size_t i;
369: int c;
370:
1.25 schwarze 371: mpage->file = NULL;
1.17 schwarze 372: mpage->names = NULL;
1.22 schwarze 373: prevsec = prevarch = NULL;
1.11 schwarze 374: i = 1;
1.32 schwarze 375: SQL_BIND_INT64(db, s, i, pageid);
1.11 schwarze 376: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.17 schwarze 377:
1.22 schwarze 378: /* Decide whether we already have some names. */
1.17 schwarze 379:
380: if (NULL == mpage->names) {
1.11 schwarze 381: oldnames = "";
382: sep1 = "";
383: } else {
1.17 schwarze 384: oldnames = mpage->names;
1.11 schwarze 385: sep1 = ", ";
386: }
1.22 schwarze 387:
388: /* Fetch the next name. */
389:
1.19 schwarze 390: sec = sqlite3_column_text(s, 0);
391: arch = sqlite3_column_text(s, 1);
392: name = sqlite3_column_text(s, 2);
1.22 schwarze 393:
394: /* If the section changed, append the old one. */
395:
396: if (NULL != prevsec &&
397: (strcmp(sec, prevsec) ||
398: strcmp(arch, prevarch))) {
399: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 400: mandoc_asprintf(&newnames, "%s(%s%s%s)",
401: oldnames, prevsec, sep2, prevarch);
1.22 schwarze 402: free(mpage->names);
403: oldnames = mpage->names = newnames;
404: free(prevsec);
405: free(prevarch);
406: prevsec = prevarch = NULL;
407: }
408:
409: /* Save the new section, to append it later. */
410:
411: if (NULL == prevsec) {
412: prevsec = mandoc_strdup(sec);
413: prevarch = mandoc_strdup(arch);
414: }
415:
416: /* Append the new name. */
417:
1.24 schwarze 418: mandoc_asprintf(&newnames, "%s%s%s",
419: oldnames, sep1, name);
1.17 schwarze 420: free(mpage->names);
421: mpage->names = newnames;
422:
423: /* Also save the first file name encountered. */
424:
425: if (NULL != mpage->file)
426: continue;
427:
1.19 schwarze 428: if (form) {
429: sep1 = "man";
430: fsec = sec;
431: } else {
432: sep1 = "cat";
433: fsec = "0";
434: }
1.22 schwarze 435: sep2 = '\0' == *arch ? "" : "/";
1.24 schwarze 436: mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
437: path, sep1, sec, sep2, arch, name, fsec);
1.11 schwarze 438: }
439: if (SQLITE_DONE != c)
440: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
441: sqlite3_reset(s);
1.22 schwarze 442:
443: /* Append one final section to the names. */
444:
445: if (NULL != prevsec) {
446: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 447: mandoc_asprintf(&newnames, "%s(%s%s%s)",
448: mpage->names, prevsec, sep2, prevarch);
1.22 schwarze 449: free(mpage->names);
450: mpage->names = newnames;
451: free(prevsec);
452: free(prevarch);
453: }
1.12 schwarze 454: }
455:
456: static char *
1.32 schwarze 457: buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
1.12 schwarze 458: {
459: char *output, *newoutput;
460: const char *oldoutput, *sep1, *data;
461: size_t i;
462: int c;
463:
464: output = NULL;
465: i = 1;
1.32 schwarze 466: SQL_BIND_INT64(db, s, i, pageid);
1.12 schwarze 467: SQL_BIND_INT64(db, s, i, outbit);
468: while (SQLITE_ROW == (c = sqlite3_step(s))) {
469: if (NULL == output) {
470: oldoutput = "";
471: sep1 = "";
472: } else {
473: oldoutput = output;
474: sep1 = " # ";
475: }
476: data = sqlite3_column_text(s, 1);
1.24 schwarze 477: mandoc_asprintf(&newoutput, "%s%s%s",
478: oldoutput, sep1, data);
1.12 schwarze 479: free(output);
480: output = newoutput;
481: }
482: if (SQLITE_DONE != c)
483: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
484: sqlite3_reset(s);
485: return(output);
1.1 kristaps 486: }
487:
488: /*
1.7 schwarze 489: * Implement substring match as an application-defined SQL function.
490: * Using the SQL LIKE or GLOB operators instead would be a bad idea
491: * because that would require escaping metacharacters in the string
492: * being searched for.
493: */
494: static void
495: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
496: {
497:
498: assert(2 == argc);
499: sqlite3_result_int(context, NULL != strcasestr(
500: (const char *)sqlite3_value_text(argv[1]),
501: (const char *)sqlite3_value_text(argv[0])));
502: }
503:
504: /*
1.8 schwarze 505: * Implement regular expression match
506: * as an application-defined SQL function.
507: */
508: static void
509: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
510: {
511:
512: assert(2 == argc);
513: sqlite3_result_int(context, !regexec(
514: (regex_t *)sqlite3_value_blob(argv[0]),
515: (const char *)sqlite3_value_text(argv[1]),
516: 0, NULL, 0));
517: }
518:
1.13 schwarze 519: static void
520: sql_append(char **sql, size_t *sz, const char *newstr, int count)
521: {
522: size_t newsz;
523:
524: newsz = 1 < count ? (size_t)count : strlen(newstr);
525: *sql = mandoc_realloc(*sql, *sz + newsz + 1);
526: if (1 < count)
527: memset(*sql + *sz, *newstr, (size_t)count);
528: else
529: memcpy(*sql + *sz, newstr, newsz);
530: *sz += newsz;
531: (*sql)[*sz] = '\0';
532: }
533:
1.8 schwarze 534: /*
1.1 kristaps 535: * Prepare the search SQL statement.
536: */
537: static char *
1.15 schwarze 538: sql_statement(const struct expr *e)
1.1 kristaps 539: {
540: char *sql;
541: size_t sz;
1.13 schwarze 542: int needop;
1.1 kristaps 543:
1.35 schwarze 544: sql = mandoc_strdup(
545: "SELECT desc, form, pageid FROM mpages WHERE ");
1.1 kristaps 546: sz = strlen(sql);
1.2 kristaps 547:
1.13 schwarze 548: for (needop = 0; NULL != e; e = e->next) {
549: if (e->and)
550: sql_append(&sql, &sz, " AND ", 1);
551: else if (needop)
552: sql_append(&sql, &sz, " OR ", 1);
553: if (e->open)
554: sql_append(&sql, &sz, "(", e->open);
1.26 schwarze 555: sql_append(&sql, &sz,
556: TYPE_Nd & e->bits
557: ? (NULL == e->substr
558: ? "desc REGEXP ?"
559: : "desc MATCH ?")
1.27 schwarze 560: : TYPE_Nm == e->bits
561: ? (NULL == e->substr
1.32 schwarze 562: ? "pageid IN (SELECT pageid FROM names "
1.27 schwarze 563: "WHERE name REGEXP ?)"
1.32 schwarze 564: : "pageid IN (SELECT pageid FROM names "
1.27 schwarze 565: "WHERE name MATCH ?)")
1.26 schwarze 566: : (NULL == e->substr
1.32 schwarze 567: ? "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 568: "WHERE key REGEXP ? AND bits & ?)"
1.32 schwarze 569: : "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 570: "WHERE key MATCH ? AND bits & ?)"), 1);
1.13 schwarze 571: if (e->close)
572: sql_append(&sql, &sz, ")", e->close);
573: needop = 1;
1.1 kristaps 574: }
575:
576: return(sql);
577: }
578:
579: /*
580: * Compile a set of string tokens into an expression.
581: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
582: * "(", "foo=bar", etc.).
583: */
584: static struct expr *
1.5 kristaps 585: exprcomp(const struct mansearch *search, int argc, char *argv[])
1.1 kristaps 586: {
1.27 schwarze 587: uint64_t mask;
1.13 schwarze 588: int i, toopen, logic, igncase, toclose;
1.27 schwarze 589: struct expr *first, *prev, *cur, *next;
1.1 kristaps 590:
591: first = cur = NULL;
1.15 schwarze 592: logic = igncase = toclose = 0;
1.33 schwarze 593: toopen = NULL != search->sec || NULL != search->arch;
1.1 kristaps 594:
595: for (i = 0; i < argc; i++) {
1.13 schwarze 596: if (0 == strcmp("(", argv[i])) {
597: if (igncase)
598: goto fail;
599: toopen++;
600: toclose++;
601: continue;
602: } else if (0 == strcmp(")", argv[i])) {
603: if (toopen || logic || igncase || NULL == cur)
604: goto fail;
605: cur->close++;
606: if (0 > --toclose)
607: goto fail;
608: continue;
609: } else if (0 == strcmp("-a", argv[i])) {
610: if (toopen || logic || igncase || NULL == cur)
611: goto fail;
612: logic = 1;
613: continue;
614: } else if (0 == strcmp("-o", argv[i])) {
615: if (toopen || logic || igncase || NULL == cur)
616: goto fail;
617: logic = 2;
618: continue;
619: } else if (0 == strcmp("-i", argv[i])) {
620: if (igncase)
621: goto fail;
622: igncase = 1;
623: continue;
1.1 kristaps 624: }
1.13 schwarze 625: next = exprterm(search, argv[i], !igncase);
626: if (NULL == next)
627: goto fail;
1.26 schwarze 628: if (NULL == first)
629: first = next;
630: else
1.1 kristaps 631: cur->next = next;
1.27 schwarze 632: prev = cur = next;
1.26 schwarze 633:
634: /*
635: * Searching for descriptions must be split out
636: * because they are stored in the mpages table,
637: * not in the keys table.
638: */
639:
1.27 schwarze 640: for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
641: if (mask & cur->bits && ~mask & cur->bits) {
642: next = mandoc_calloc(1,
643: sizeof(struct expr));
644: memcpy(next, cur, sizeof(struct expr));
645: prev->open = 1;
646: cur->bits = mask;
647: cur->next = next;
648: cur = next;
649: cur->bits &= ~mask;
650: }
651: }
652: prev->and = (1 == logic);
653: prev->open += toopen;
654: if (cur != prev)
1.26 schwarze 655: cur->close = 1;
1.27 schwarze 656:
1.13 schwarze 657: toopen = logic = igncase = 0;
1.1 kristaps 658: }
1.15 schwarze 659: if (toopen || logic || igncase || toclose)
660: goto fail;
661:
1.33 schwarze 662: if (NULL != search->sec || NULL != search->arch)
663: cur->close++;
664: if (NULL != search->arch)
665: cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
666: if (NULL != search->sec)
667: exprspec(cur, TYPE_sec, search->sec, "^%s$");
1.15 schwarze 668:
669: return(first);
670:
1.13 schwarze 671: fail:
672: if (NULL != first)
673: exprfree(first);
674: return(NULL);
1.1 kristaps 675: }
676:
677: static struct expr *
1.15 schwarze 678: exprspec(struct expr *cur, uint64_t key, const char *value,
679: const char *format)
680: {
681: char errbuf[BUFSIZ];
682: char *cp;
683: int irc;
684:
1.24 schwarze 685: mandoc_asprintf(&cp, format, value);
1.15 schwarze 686: cur->next = mandoc_calloc(1, sizeof(struct expr));
687: cur = cur->next;
688: cur->and = 1;
689: cur->bits = key;
690: if (0 != (irc = regcomp(&cur->regexp, cp,
691: REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
692: regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
693: fprintf(stderr, "regcomp: %s\n", errbuf);
694: cur->substr = value;
695: }
696: free(cp);
697: return(cur);
698: }
699:
700: static struct expr *
1.8 schwarze 701: exprterm(const struct mansearch *search, char *buf, int cs)
1.1 kristaps 702: {
1.15 schwarze 703: char errbuf[BUFSIZ];
1.1 kristaps 704: struct expr *e;
705: char *key, *v;
1.20 schwarze 706: uint64_t iterbit;
707: int i, irc;
1.1 kristaps 708:
709: if ('\0' == *buf)
710: return(NULL);
711:
712: e = mandoc_calloc(1, sizeof(struct expr));
713:
1.5 kristaps 714: /*"whatis" mode uses an opaque string and default fields. */
715:
716: if (MANSEARCH_WHATIS & search->flags) {
1.8 schwarze 717: e->substr = buf;
1.5 kristaps 718: e->bits = search->deftype;
719: return(e);
720: }
721:
1.1 kristaps 722: /*
723: * If no =~ is specified, search with equality over names and
724: * descriptions.
725: * If =~ begins the phrase, use name and description fields.
726: */
727:
728: if (NULL == (v = strpbrk(buf, "=~"))) {
1.8 schwarze 729: e->substr = buf;
1.5 kristaps 730: e->bits = search->deftype;
1.1 kristaps 731: return(e);
732: } else if (v == buf)
1.5 kristaps 733: e->bits = search->deftype;
1.1 kristaps 734:
1.8 schwarze 735: if ('~' == *v++) {
1.21 schwarze 736: if (NULL != strstr(buf, "arch"))
737: cs = 0;
1.15 schwarze 738: if (0 != (irc = regcomp(&e->regexp, v,
739: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE)))) {
740: regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
741: fprintf(stderr, "regcomp: %s\n", errbuf);
1.8 schwarze 742: free(e);
743: return(NULL);
744: }
745: } else
746: e->substr = v;
747: v[-1] = '\0';
1.1 kristaps 748:
749: /*
750: * Parse out all possible fields.
751: * If the field doesn't resolve, bail.
752: */
753:
754: while (NULL != (key = strsep(&buf, ","))) {
755: if ('\0' == *key)
756: continue;
1.20 schwarze 757: for (i = 0, iterbit = 1;
758: i < mansearch_keymax;
759: i++, iterbit <<= 1) {
760: if (0 == strcasecmp(key,
761: mansearch_keynames[i])) {
762: e->bits |= iterbit;
763: break;
764: }
765: }
766: if (i == mansearch_keymax) {
767: if (strcasecmp(key, "any")) {
768: free(e);
769: return(NULL);
770: }
771: e->bits |= ~0ULL;
1.1 kristaps 772: }
773: }
774:
775: return(e);
776: }
777:
778: static void
779: exprfree(struct expr *p)
780: {
781: struct expr *pp;
782:
783: while (NULL != p) {
784: pp = p->next;
785: free(p);
786: p = pp;
787: }
788: }
789:
790: static void *
1.37 ! schwarze 791: hash_calloc(size_t nmemb, size_t sz, void *arg)
1.1 kristaps 792: {
793:
1.37 ! schwarze 794: return(mandoc_calloc(nmemb, sz));
1.1 kristaps 795: }
796:
797: static void *
798: hash_alloc(size_t sz, void *arg)
799: {
800:
801: return(mandoc_malloc(sz));
802: }
803:
804: static void
1.37 ! schwarze 805: hash_free(void *p, void *arg)
1.1 kristaps 806: {
807:
808: free(p);
809: }
CVSweb