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