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