Annotation of mandoc/mansearch.c, Revision 1.36
1.36 ! schwarze 1: /* $Id: mansearch.c,v 1.35 2014/04/23 16:34:50 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 *);
88: static void hash_free(void *, size_t, void *);
89: static void *hash_halloc(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:
174: memset(&info, 0, sizeof(struct ohash_info));
175:
176: info.halloc = hash_halloc;
177: info.alloc = hash_alloc;
178: info.hfree = hash_free;
1.32 schwarze 179: info.key_offset = offsetof(struct match, pageid);
1.1 kristaps 180:
1.2 kristaps 181: *sz = cur = maxres = 0;
1.1 kristaps 182: sql = NULL;
183: *res = NULL;
184: fd = -1;
185: e = NULL;
1.2 kristaps 186: rc = 0;
1.1 kristaps 187:
188: if (0 == argc)
189: goto out;
1.5 kristaps 190: if (NULL == (e = exprcomp(search, argc, argv)))
1.1 kristaps 191: goto out;
192:
1.12 schwarze 193: outbit = 0;
194: if (NULL != outkey) {
1.20 schwarze 195: for (indexbit = 0, iterbit = 1;
196: indexbit < mansearch_keymax;
197: indexbit++, iterbit <<= 1) {
198: if (0 == strcasecmp(outkey,
199: mansearch_keynames[indexbit])) {
200: outbit = iterbit;
1.12 schwarze 201: break;
202: }
203: }
204: }
205:
1.1 kristaps 206: /*
207: * Save a descriptor to the current working directory.
208: * Since pathnames in the "paths" variable might be relative,
209: * and we'll be chdir()ing into them, we need to keep a handle
210: * on our current directory from which to start the chdir().
211: */
212:
1.6 schwarze 213: if (NULL == getcwd(buf, PATH_MAX)) {
1.30 schwarze 214: perror("getcwd");
1.1 kristaps 215: goto out;
216: } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
217: perror(buf);
218: goto out;
219: }
220:
1.15 schwarze 221: sql = sql_statement(e);
1.1 kristaps 222:
223: /*
224: * Loop over the directories (containing databases) for us to
225: * search.
226: * Don't let missing/bad databases/directories phase us.
227: * In each, try to open the resident database and, if it opens,
228: * scan it for our match expression.
229: */
230:
231: for (i = 0; i < paths->sz; i++) {
232: if (-1 == fchdir(fd)) {
233: perror(buf);
234: free(*res);
235: break;
236: } else if (-1 == chdir(paths->paths[i])) {
237: perror(paths->paths[i]);
238: continue;
1.34 schwarze 239: }
1.1 kristaps 240:
1.34 schwarze 241: c = sqlite3_open_v2(MANDOC_DB, &db,
242: SQLITE_OPEN_READONLY, NULL);
1.1 kristaps 243:
1.2 kristaps 244: if (SQLITE_OK != c) {
1.1 kristaps 245: perror(MANDOC_DB);
246: sqlite3_close(db);
247: continue;
248: }
249:
1.8 schwarze 250: /*
251: * Define the SQL functions for substring
252: * and regular expression matching.
253: */
1.7 schwarze 254:
255: c = sqlite3_create_function(db, "match", 2,
1.31 schwarze 256: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
257: NULL, sql_match, NULL, NULL);
1.8 schwarze 258: assert(SQLITE_OK == c);
259: c = sqlite3_create_function(db, "regexp", 2,
1.31 schwarze 260: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
261: NULL, sql_regexp, NULL, NULL);
1.8 schwarze 262: assert(SQLITE_OK == c);
1.7 schwarze 263:
1.1 kristaps 264: j = 1;
1.2 kristaps 265: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
266: if (SQLITE_OK != c)
267: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 268:
269: for (ep = e; NULL != ep; ep = ep->next) {
1.8 schwarze 270: if (NULL == ep->substr) {
271: SQL_BIND_BLOB(db, s, j, ep->regexp);
272: } else
273: SQL_BIND_TEXT(db, s, j, ep->substr);
1.27 schwarze 274: if (0 == ((TYPE_Nd | TYPE_Nm) & ep->bits))
1.26 schwarze 275: SQL_BIND_INT64(db, s, j, ep->bits);
1.1 kristaps 276: }
277:
278: memset(&htab, 0, sizeof(struct ohash));
279: ohash_init(&htab, 4, &info);
280:
281: /*
282: * Hash each entry on its [unique] document identifier.
283: * This is a uint64_t.
284: * Instead of using a hash function, simply convert the
285: * uint64_t to a uint32_t, the hash value's type.
286: * This gives good performance and preserves the
287: * distribution of buckets in the table.
288: */
1.2 kristaps 289: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.32 schwarze 290: pageid = sqlite3_column_int64(s, 2);
1.34 schwarze 291: idx = ohash_lookup_memory(&htab,
292: (char *)&pageid, sizeof(uint64_t),
293: (uint32_t)pageid);
1.1 kristaps 294:
295: if (NULL != ohash_find(&htab, idx))
296: continue;
297:
298: mp = mandoc_calloc(1, sizeof(struct match));
1.32 schwarze 299: mp->pageid = pageid;
1.26 schwarze 300: mp->form = sqlite3_column_int(s, 1);
301: if (TYPE_Nd == outbit)
302: mp->desc = mandoc_strdup(
303: sqlite3_column_text(s, 0));
1.1 kristaps 304: ohash_insert(&htab, idx, mp);
305: }
306:
1.2 kristaps 307: if (SQLITE_DONE != c)
308: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
309:
1.1 kristaps 310: sqlite3_finalize(s);
1.10 schwarze 311:
1.34 schwarze 312: c = sqlite3_prepare_v2(db,
1.35 schwarze 313: "SELECT sec, arch, name, pageid FROM mlinks "
314: "WHERE pageid=? ORDER BY sec, arch, name",
1.10 schwarze 315: -1, &s, NULL);
316: if (SQLITE_OK != c)
317: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 318:
1.12 schwarze 319: c = sqlite3_prepare_v2(db,
1.35 schwarze 320: "SELECT bits, key, pageid FROM keys "
321: "WHERE pageid=? AND bits & ?",
1.12 schwarze 322: -1, &s2, NULL);
323: if (SQLITE_OK != c)
324: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
325:
1.1 kristaps 326: for (mp = ohash_first(&htab, &idx);
327: NULL != mp;
328: mp = ohash_next(&htab, &idx)) {
329: if (cur + 1 > maxres) {
330: maxres += 1024;
1.36 ! schwarze 331: *res = mandoc_reallocarray(*res,
! 332: maxres, sizeof(struct manpage));
1.1 kristaps 333: }
1.10 schwarze 334: mpage = *res + cur;
335: mpage->form = mp->form;
1.32 schwarze 336: buildnames(mpage, db, s, mp->pageid,
1.19 schwarze 337: paths->paths[i], mp->form);
1.26 schwarze 338: mpage->output = TYPE_Nd & outbit ?
339: mp->desc : outbit ?
1.32 schwarze 340: buildoutput(db, s2, mp->pageid, outbit) : NULL;
1.10 schwarze 341:
1.1 kristaps 342: free(mp);
343: cur++;
344: }
1.10 schwarze 345:
346: sqlite3_finalize(s);
1.12 schwarze 347: sqlite3_finalize(s2);
1.10 schwarze 348: sqlite3_close(db);
1.1 kristaps 349: ohash_delete(&htab);
350: }
1.2 kristaps 351: rc = 1;
1.1 kristaps 352: out:
1.30 schwarze 353: if (-1 != fd) {
354: if (-1 == fchdir(fd))
355: perror(buf);
356: close(fd);
357: }
1.1 kristaps 358: exprfree(e);
359: free(sql);
360: *sz = cur;
1.2 kristaps 361: return(rc);
1.11 schwarze 362: }
363:
1.17 schwarze 364: static void
365: buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
1.32 schwarze 366: uint64_t pageid, const char *path, int form)
1.11 schwarze 367: {
1.22 schwarze 368: char *newnames, *prevsec, *prevarch;
1.19 schwarze 369: const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
1.11 schwarze 370: size_t i;
371: int c;
372:
1.25 schwarze 373: mpage->file = NULL;
1.17 schwarze 374: mpage->names = NULL;
1.22 schwarze 375: prevsec = prevarch = NULL;
1.11 schwarze 376: i = 1;
1.32 schwarze 377: SQL_BIND_INT64(db, s, i, pageid);
1.11 schwarze 378: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.17 schwarze 379:
1.22 schwarze 380: /* Decide whether we already have some names. */
1.17 schwarze 381:
382: if (NULL == mpage->names) {
1.11 schwarze 383: oldnames = "";
384: sep1 = "";
385: } else {
1.17 schwarze 386: oldnames = mpage->names;
1.11 schwarze 387: sep1 = ", ";
388: }
1.22 schwarze 389:
390: /* Fetch the next name. */
391:
1.19 schwarze 392: sec = sqlite3_column_text(s, 0);
393: arch = sqlite3_column_text(s, 1);
394: name = sqlite3_column_text(s, 2);
1.22 schwarze 395:
396: /* If the section changed, append the old one. */
397:
398: if (NULL != prevsec &&
399: (strcmp(sec, prevsec) ||
400: strcmp(arch, prevarch))) {
401: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 402: mandoc_asprintf(&newnames, "%s(%s%s%s)",
403: oldnames, prevsec, sep2, prevarch);
1.22 schwarze 404: free(mpage->names);
405: oldnames = mpage->names = newnames;
406: free(prevsec);
407: free(prevarch);
408: prevsec = prevarch = NULL;
409: }
410:
411: /* Save the new section, to append it later. */
412:
413: if (NULL == prevsec) {
414: prevsec = mandoc_strdup(sec);
415: prevarch = mandoc_strdup(arch);
416: }
417:
418: /* Append the new name. */
419:
1.24 schwarze 420: mandoc_asprintf(&newnames, "%s%s%s",
421: oldnames, sep1, name);
1.17 schwarze 422: free(mpage->names);
423: mpage->names = newnames;
424:
425: /* Also save the first file name encountered. */
426:
427: if (NULL != mpage->file)
428: continue;
429:
1.19 schwarze 430: if (form) {
431: sep1 = "man";
432: fsec = sec;
433: } else {
434: sep1 = "cat";
435: fsec = "0";
436: }
1.22 schwarze 437: sep2 = '\0' == *arch ? "" : "/";
1.24 schwarze 438: mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
439: path, sep1, sec, sep2, arch, name, fsec);
1.11 schwarze 440: }
441: if (SQLITE_DONE != c)
442: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
443: sqlite3_reset(s);
1.22 schwarze 444:
445: /* Append one final section to the names. */
446:
447: if (NULL != prevsec) {
448: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 449: mandoc_asprintf(&newnames, "%s(%s%s%s)",
450: mpage->names, prevsec, sep2, prevarch);
1.22 schwarze 451: free(mpage->names);
452: mpage->names = newnames;
453: free(prevsec);
454: free(prevarch);
455: }
1.12 schwarze 456: }
457:
458: static char *
1.32 schwarze 459: buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
1.12 schwarze 460: {
461: char *output, *newoutput;
462: const char *oldoutput, *sep1, *data;
463: size_t i;
464: int c;
465:
466: output = NULL;
467: i = 1;
1.32 schwarze 468: SQL_BIND_INT64(db, s, i, pageid);
1.12 schwarze 469: SQL_BIND_INT64(db, s, i, outbit);
470: while (SQLITE_ROW == (c = sqlite3_step(s))) {
471: if (NULL == output) {
472: oldoutput = "";
473: sep1 = "";
474: } else {
475: oldoutput = output;
476: sep1 = " # ";
477: }
478: data = sqlite3_column_text(s, 1);
1.24 schwarze 479: mandoc_asprintf(&newoutput, "%s%s%s",
480: oldoutput, sep1, data);
1.12 schwarze 481: free(output);
482: output = newoutput;
483: }
484: if (SQLITE_DONE != c)
485: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
486: sqlite3_reset(s);
487: return(output);
1.1 kristaps 488: }
489:
490: /*
1.7 schwarze 491: * Implement substring match as an application-defined SQL function.
492: * Using the SQL LIKE or GLOB operators instead would be a bad idea
493: * because that would require escaping metacharacters in the string
494: * being searched for.
495: */
496: static void
497: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
498: {
499:
500: assert(2 == argc);
501: sqlite3_result_int(context, NULL != strcasestr(
502: (const char *)sqlite3_value_text(argv[1]),
503: (const char *)sqlite3_value_text(argv[0])));
504: }
505:
506: /*
1.8 schwarze 507: * Implement regular expression match
508: * as an application-defined SQL function.
509: */
510: static void
511: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
512: {
513:
514: assert(2 == argc);
515: sqlite3_result_int(context, !regexec(
516: (regex_t *)sqlite3_value_blob(argv[0]),
517: (const char *)sqlite3_value_text(argv[1]),
518: 0, NULL, 0));
519: }
520:
1.13 schwarze 521: static void
522: sql_append(char **sql, size_t *sz, const char *newstr, int count)
523: {
524: size_t newsz;
525:
526: newsz = 1 < count ? (size_t)count : strlen(newstr);
527: *sql = mandoc_realloc(*sql, *sz + newsz + 1);
528: if (1 < count)
529: memset(*sql + *sz, *newstr, (size_t)count);
530: else
531: memcpy(*sql + *sz, newstr, newsz);
532: *sz += newsz;
533: (*sql)[*sz] = '\0';
534: }
535:
1.8 schwarze 536: /*
1.1 kristaps 537: * Prepare the search SQL statement.
538: */
539: static char *
1.15 schwarze 540: sql_statement(const struct expr *e)
1.1 kristaps 541: {
542: char *sql;
543: size_t sz;
1.13 schwarze 544: int needop;
1.1 kristaps 545:
1.35 schwarze 546: sql = mandoc_strdup(
547: "SELECT desc, form, pageid FROM mpages WHERE ");
1.1 kristaps 548: sz = strlen(sql);
1.2 kristaps 549:
1.13 schwarze 550: for (needop = 0; NULL != e; e = e->next) {
551: if (e->and)
552: sql_append(&sql, &sz, " AND ", 1);
553: else if (needop)
554: sql_append(&sql, &sz, " OR ", 1);
555: if (e->open)
556: sql_append(&sql, &sz, "(", e->open);
1.26 schwarze 557: sql_append(&sql, &sz,
558: TYPE_Nd & e->bits
559: ? (NULL == e->substr
560: ? "desc REGEXP ?"
561: : "desc MATCH ?")
1.27 schwarze 562: : TYPE_Nm == e->bits
563: ? (NULL == e->substr
1.32 schwarze 564: ? "pageid IN (SELECT pageid FROM names "
1.27 schwarze 565: "WHERE name REGEXP ?)"
1.32 schwarze 566: : "pageid IN (SELECT pageid FROM names "
1.27 schwarze 567: "WHERE name MATCH ?)")
1.26 schwarze 568: : (NULL == e->substr
1.32 schwarze 569: ? "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 570: "WHERE key REGEXP ? AND bits & ?)"
1.32 schwarze 571: : "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 572: "WHERE key MATCH ? AND bits & ?)"), 1);
1.13 schwarze 573: if (e->close)
574: sql_append(&sql, &sz, ")", e->close);
575: needop = 1;
1.1 kristaps 576: }
577:
578: return(sql);
579: }
580:
581: /*
582: * Compile a set of string tokens into an expression.
583: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
584: * "(", "foo=bar", etc.).
585: */
586: static struct expr *
1.5 kristaps 587: exprcomp(const struct mansearch *search, int argc, char *argv[])
1.1 kristaps 588: {
1.27 schwarze 589: uint64_t mask;
1.13 schwarze 590: int i, toopen, logic, igncase, toclose;
1.27 schwarze 591: struct expr *first, *prev, *cur, *next;
1.1 kristaps 592:
593: first = cur = NULL;
1.15 schwarze 594: logic = igncase = toclose = 0;
1.33 schwarze 595: toopen = NULL != search->sec || NULL != search->arch;
1.1 kristaps 596:
597: for (i = 0; i < argc; i++) {
1.13 schwarze 598: if (0 == strcmp("(", argv[i])) {
599: if (igncase)
600: goto fail;
601: toopen++;
602: toclose++;
603: continue;
604: } else if (0 == strcmp(")", argv[i])) {
605: if (toopen || logic || igncase || NULL == cur)
606: goto fail;
607: cur->close++;
608: if (0 > --toclose)
609: goto fail;
610: continue;
611: } else if (0 == strcmp("-a", argv[i])) {
612: if (toopen || logic || igncase || NULL == cur)
613: goto fail;
614: logic = 1;
615: continue;
616: } else if (0 == strcmp("-o", argv[i])) {
617: if (toopen || logic || igncase || NULL == cur)
618: goto fail;
619: logic = 2;
620: continue;
621: } else if (0 == strcmp("-i", argv[i])) {
622: if (igncase)
623: goto fail;
624: igncase = 1;
625: continue;
1.1 kristaps 626: }
1.13 schwarze 627: next = exprterm(search, argv[i], !igncase);
628: if (NULL == next)
629: goto fail;
1.26 schwarze 630: if (NULL == first)
631: first = next;
632: else
1.1 kristaps 633: cur->next = next;
1.27 schwarze 634: prev = cur = next;
1.26 schwarze 635:
636: /*
637: * Searching for descriptions must be split out
638: * because they are stored in the mpages table,
639: * not in the keys table.
640: */
641:
1.27 schwarze 642: for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
643: if (mask & cur->bits && ~mask & cur->bits) {
644: next = mandoc_calloc(1,
645: sizeof(struct expr));
646: memcpy(next, cur, sizeof(struct expr));
647: prev->open = 1;
648: cur->bits = mask;
649: cur->next = next;
650: cur = next;
651: cur->bits &= ~mask;
652: }
653: }
654: prev->and = (1 == logic);
655: prev->open += toopen;
656: if (cur != prev)
1.26 schwarze 657: cur->close = 1;
1.27 schwarze 658:
1.13 schwarze 659: toopen = logic = igncase = 0;
1.1 kristaps 660: }
1.15 schwarze 661: if (toopen || logic || igncase || toclose)
662: goto fail;
663:
1.33 schwarze 664: if (NULL != search->sec || NULL != search->arch)
665: cur->close++;
666: if (NULL != search->arch)
667: cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
668: if (NULL != search->sec)
669: exprspec(cur, TYPE_sec, search->sec, "^%s$");
1.15 schwarze 670:
671: return(first);
672:
1.13 schwarze 673: fail:
674: if (NULL != first)
675: exprfree(first);
676: return(NULL);
1.1 kristaps 677: }
678:
679: static struct expr *
1.15 schwarze 680: exprspec(struct expr *cur, uint64_t key, const char *value,
681: const char *format)
682: {
683: char errbuf[BUFSIZ];
684: char *cp;
685: int irc;
686:
1.24 schwarze 687: mandoc_asprintf(&cp, format, value);
1.15 schwarze 688: cur->next = mandoc_calloc(1, sizeof(struct expr));
689: cur = cur->next;
690: cur->and = 1;
691: cur->bits = key;
692: if (0 != (irc = regcomp(&cur->regexp, cp,
693: REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
694: regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
695: fprintf(stderr, "regcomp: %s\n", errbuf);
696: cur->substr = value;
697: }
698: free(cp);
699: return(cur);
700: }
701:
702: static struct expr *
1.8 schwarze 703: exprterm(const struct mansearch *search, char *buf, int cs)
1.1 kristaps 704: {
1.15 schwarze 705: char errbuf[BUFSIZ];
1.1 kristaps 706: struct expr *e;
707: char *key, *v;
1.20 schwarze 708: uint64_t iterbit;
709: int i, irc;
1.1 kristaps 710:
711: if ('\0' == *buf)
712: return(NULL);
713:
714: e = mandoc_calloc(1, sizeof(struct expr));
715:
1.5 kristaps 716: /*"whatis" mode uses an opaque string and default fields. */
717:
718: if (MANSEARCH_WHATIS & search->flags) {
1.8 schwarze 719: e->substr = buf;
1.5 kristaps 720: e->bits = search->deftype;
721: return(e);
722: }
723:
1.1 kristaps 724: /*
725: * If no =~ is specified, search with equality over names and
726: * descriptions.
727: * If =~ begins the phrase, use name and description fields.
728: */
729:
730: if (NULL == (v = strpbrk(buf, "=~"))) {
1.8 schwarze 731: e->substr = buf;
1.5 kristaps 732: e->bits = search->deftype;
1.1 kristaps 733: return(e);
734: } else if (v == buf)
1.5 kristaps 735: e->bits = search->deftype;
1.1 kristaps 736:
1.8 schwarze 737: if ('~' == *v++) {
1.21 schwarze 738: if (NULL != strstr(buf, "arch"))
739: cs = 0;
1.15 schwarze 740: if (0 != (irc = regcomp(&e->regexp, v,
741: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE)))) {
742: regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
743: fprintf(stderr, "regcomp: %s\n", errbuf);
1.8 schwarze 744: free(e);
745: return(NULL);
746: }
747: } else
748: e->substr = v;
749: v[-1] = '\0';
1.1 kristaps 750:
751: /*
752: * Parse out all possible fields.
753: * If the field doesn't resolve, bail.
754: */
755:
756: while (NULL != (key = strsep(&buf, ","))) {
757: if ('\0' == *key)
758: continue;
1.20 schwarze 759: for (i = 0, iterbit = 1;
760: i < mansearch_keymax;
761: i++, iterbit <<= 1) {
762: if (0 == strcasecmp(key,
763: mansearch_keynames[i])) {
764: e->bits |= iterbit;
765: break;
766: }
767: }
768: if (i == mansearch_keymax) {
769: if (strcasecmp(key, "any")) {
770: free(e);
771: return(NULL);
772: }
773: e->bits |= ~0ULL;
1.1 kristaps 774: }
775: }
776:
777: return(e);
778: }
779:
780: static void
781: exprfree(struct expr *p)
782: {
783: struct expr *pp;
784:
785: while (NULL != p) {
786: pp = p->next;
787: free(p);
788: p = pp;
789: }
790: }
791:
792: static void *
793: hash_halloc(size_t sz, void *arg)
794: {
795:
1.36 ! schwarze 796: return(mandoc_calloc(1, sz));
1.1 kristaps 797: }
798:
799: static void *
800: hash_alloc(size_t sz, void *arg)
801: {
802:
803: return(mandoc_malloc(sz));
804: }
805:
806: static void
807: hash_free(void *p, size_t sz, void *arg)
808: {
809:
810: free(p);
811: }
CVSweb