[BACK]Return to mansearch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mansearch.c, Revision 1.38

1.38    ! schwarze    1: /*     $OpenBSD: mansearch.c,v 1.37 2014/11/18 01:14:40 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.2       schwarze  405:        size_t           i;
                    406:        int              c;
                    407:
1.16      schwarze  408:        mpage->file = NULL;
1.8       schwarze  409:        mpage->names = NULL;
1.13      schwarze  410:        prevsec = prevarch = NULL;
1.2       schwarze  411:        i = 1;
1.22      schwarze  412:        SQL_BIND_INT64(db, s, i, pageid);
1.2       schwarze  413:        while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.8       schwarze  414:
1.13      schwarze  415:                /* Decide whether we already have some names. */
1.8       schwarze  416:
                    417:                if (NULL == mpage->names) {
1.2       schwarze  418:                        oldnames = "";
                    419:                        sep1 = "";
                    420:                } else {
1.8       schwarze  421:                        oldnames = mpage->names;
1.2       schwarze  422:                        sep1 = ", ";
                    423:                }
1.13      schwarze  424:
                    425:                /* Fetch the next name. */
                    426:
1.30      schwarze  427:                sec = (const char *)sqlite3_column_text(s, 0);
                    428:                arch = (const char *)sqlite3_column_text(s, 1);
                    429:                name = (const char *)sqlite3_column_text(s, 2);
1.29      schwarze  430:
                    431:                /* Remember the first section found. */
                    432:
                    433:                if (9 < mpage->sec && '1' <= *sec && '9' >= *sec)
                    434:                        mpage->sec = (*sec - '1') + 1;
1.13      schwarze  435:
                    436:                /* If the section changed, append the old one. */
                    437:
                    438:                if (NULL != prevsec &&
                    439:                    (strcmp(sec, prevsec) ||
                    440:                     strcmp(arch, prevarch))) {
                    441:                        sep2 = '\0' == *prevarch ? "" : "/";
1.15      schwarze  442:                        mandoc_asprintf(&newnames, "%s(%s%s%s)",
                    443:                            oldnames, prevsec, sep2, prevarch);
1.13      schwarze  444:                        free(mpage->names);
                    445:                        oldnames = mpage->names = newnames;
                    446:                        free(prevsec);
                    447:                        free(prevarch);
                    448:                        prevsec = prevarch = NULL;
                    449:                }
                    450:
                    451:                /* Save the new section, to append it later. */
                    452:
                    453:                if (NULL == prevsec) {
                    454:                        prevsec = mandoc_strdup(sec);
                    455:                        prevarch = mandoc_strdup(arch);
                    456:                }
                    457:
                    458:                /* Append the new name. */
                    459:
1.15      schwarze  460:                mandoc_asprintf(&newnames, "%s%s%s",
                    461:                    oldnames, sep1, name);
1.8       schwarze  462:                free(mpage->names);
                    463:                mpage->names = newnames;
                    464:
                    465:                /* Also save the first file name encountered. */
                    466:
1.38    ! schwarze  467:                if (mpage->file != NULL)
1.8       schwarze  468:                        continue;
                    469:
1.35      schwarze  470:                if (form & FORM_SRC) {
1.10      schwarze  471:                        sep1 = "man";
                    472:                        fsec = sec;
                    473:                } else {
                    474:                        sep1 = "cat";
                    475:                        fsec = "0";
                    476:                }
1.38    ! schwarze  477:                sep2 = *arch == '\0' ? "" : "/";
        !           478:                mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
        !           479:                    path, sep1, sec, sep2, arch, name, fsec);
1.2       schwarze  480:        }
1.38    ! schwarze  481:        if (c != SQLITE_DONE)
1.2       schwarze  482:                fprintf(stderr, "%s\n", sqlite3_errmsg(db));
                    483:        sqlite3_reset(s);
1.13      schwarze  484:
                    485:        /* Append one final section to the names. */
                    486:
1.38    ! schwarze  487:        if (prevsec != NULL) {
        !           488:                sep2 = *prevarch == '\0' ? "" : "/";
1.15      schwarze  489:                mandoc_asprintf(&newnames, "%s(%s%s%s)",
                    490:                    mpage->names, prevsec, sep2, prevarch);
1.13      schwarze  491:                free(mpage->names);
                    492:                mpage->names = newnames;
                    493:                free(prevsec);
                    494:                free(prevarch);
                    495:        }
1.3       schwarze  496: }
                    497:
                    498: static char *
1.22      schwarze  499: buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
1.3       schwarze  500: {
                    501:        char            *output, *newoutput;
                    502:        const char      *oldoutput, *sep1, *data;
                    503:        size_t           i;
                    504:        int              c;
                    505:
                    506:        output = NULL;
                    507:        i = 1;
1.22      schwarze  508:        SQL_BIND_INT64(db, s, i, pageid);
1.3       schwarze  509:        SQL_BIND_INT64(db, s, i, outbit);
                    510:        while (SQLITE_ROW == (c = sqlite3_step(s))) {
                    511:                if (NULL == output) {
                    512:                        oldoutput = "";
                    513:                        sep1 = "";
                    514:                } else {
                    515:                        oldoutput = output;
                    516:                        sep1 = " # ";
                    517:                }
1.30      schwarze  518:                data = (const char *)sqlite3_column_text(s, 1);
1.15      schwarze  519:                mandoc_asprintf(&newoutput, "%s%s%s",
                    520:                    oldoutput, sep1, data);
1.3       schwarze  521:                free(output);
                    522:                output = newoutput;
                    523:        }
                    524:        if (SQLITE_DONE != c)
                    525:                fprintf(stderr, "%s\n", sqlite3_errmsg(db));
                    526:        sqlite3_reset(s);
                    527:        return(output);
1.1       schwarze  528: }
                    529:
                    530: /*
                    531:  * Implement substring match as an application-defined SQL function.
                    532:  * Using the SQL LIKE or GLOB operators instead would be a bad idea
                    533:  * because that would require escaping metacharacters in the string
                    534:  * being searched for.
                    535:  */
                    536: static void
                    537: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
                    538: {
                    539:
                    540:        assert(2 == argc);
                    541:        sqlite3_result_int(context, NULL != strcasestr(
                    542:            (const char *)sqlite3_value_text(argv[1]),
                    543:            (const char *)sqlite3_value_text(argv[0])));
                    544: }
                    545:
                    546: /*
                    547:  * Implement regular expression match
                    548:  * as an application-defined SQL function.
                    549:  */
                    550: static void
                    551: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
                    552: {
                    553:
                    554:        assert(2 == argc);
                    555:        sqlite3_result_int(context, !regexec(
                    556:            (regex_t *)sqlite3_value_blob(argv[0]),
                    557:            (const char *)sqlite3_value_text(argv[1]),
                    558:            0, NULL, 0));
                    559: }
                    560:
1.4       schwarze  561: static void
                    562: sql_append(char **sql, size_t *sz, const char *newstr, int count)
                    563: {
                    564:        size_t           newsz;
                    565:
                    566:        newsz = 1 < count ? (size_t)count : strlen(newstr);
                    567:        *sql = mandoc_realloc(*sql, *sz + newsz + 1);
                    568:        if (1 < count)
                    569:                memset(*sql + *sz, *newstr, (size_t)count);
                    570:        else
                    571:                memcpy(*sql + *sz, newstr, newsz);
                    572:        *sz += newsz;
                    573:        (*sql)[*sz] = '\0';
                    574: }
                    575:
1.1       schwarze  576: /*
                    577:  * Prepare the search SQL statement.
                    578:  */
                    579: static char *
1.6       schwarze  580: sql_statement(const struct expr *e)
1.1       schwarze  581: {
                    582:        char            *sql;
                    583:        size_t           sz;
1.4       schwarze  584:        int              needop;
1.1       schwarze  585:
1.37      schwarze  586:        sql = mandoc_strdup(e->equal ?
                    587:            "SELECT desc, form, pageid, bits "
                    588:                "FROM mpages NATURAL JOIN names WHERE " :
                    589:            "SELECT desc, form, pageid, 0 FROM mpages WHERE ");
1.1       schwarze  590:        sz = strlen(sql);
                    591:
1.4       schwarze  592:        for (needop = 0; NULL != e; e = e->next) {
                    593:                if (e->and)
                    594:                        sql_append(&sql, &sz, " AND ", 1);
                    595:                else if (needop)
                    596:                        sql_append(&sql, &sz, " OR ", 1);
                    597:                if (e->open)
                    598:                        sql_append(&sql, &sz, "(", e->open);
1.17      schwarze  599:                sql_append(&sql, &sz,
                    600:                    TYPE_Nd & e->bits
                    601:                    ? (NULL == e->substr
                    602:                        ? "desc REGEXP ?"
                    603:                        : "desc MATCH ?")
1.18      schwarze  604:                    : TYPE_Nm == e->bits
                    605:                    ? (NULL == e->substr
1.22      schwarze  606:                        ? "pageid IN (SELECT pageid FROM names "
1.18      schwarze  607:                          "WHERE name REGEXP ?)"
1.28      schwarze  608:                        : e->equal
1.37      schwarze  609:                        ? "name = ? "
1.22      schwarze  610:                        : "pageid IN (SELECT pageid FROM names "
1.18      schwarze  611:                          "WHERE name MATCH ?)")
1.17      schwarze  612:                    : (NULL == e->substr
1.22      schwarze  613:                        ? "pageid IN (SELECT pageid FROM keys "
1.17      schwarze  614:                          "WHERE key REGEXP ? AND bits & ?)"
1.22      schwarze  615:                        : "pageid IN (SELECT pageid FROM keys "
1.17      schwarze  616:                          "WHERE key MATCH ? AND bits & ?)"), 1);
1.4       schwarze  617:                if (e->close)
                    618:                        sql_append(&sql, &sz, ")", e->close);
                    619:                needop = 1;
1.1       schwarze  620:        }
                    621:
                    622:        return(sql);
                    623: }
                    624:
                    625: /*
                    626:  * Compile a set of string tokens into an expression.
                    627:  * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
                    628:  * "(", "foo=bar", etc.).
                    629:  */
                    630: static struct expr *
                    631: exprcomp(const struct mansearch *search, int argc, char *argv[])
                    632: {
1.18      schwarze  633:        uint64_t         mask;
1.4       schwarze  634:        int              i, toopen, logic, igncase, toclose;
1.18      schwarze  635:        struct expr     *first, *prev, *cur, *next;
1.1       schwarze  636:
                    637:        first = cur = NULL;
1.6       schwarze  638:        logic = igncase = toclose = 0;
1.23      schwarze  639:        toopen = NULL != search->sec || NULL != search->arch;
1.1       schwarze  640:
                    641:        for (i = 0; i < argc; i++) {
1.4       schwarze  642:                if (0 == strcmp("(", argv[i])) {
                    643:                        if (igncase)
                    644:                                goto fail;
                    645:                        toopen++;
                    646:                        toclose++;
                    647:                        continue;
                    648:                } else if (0 == strcmp(")", argv[i])) {
                    649:                        if (toopen || logic || igncase || NULL == cur)
                    650:                                goto fail;
                    651:                        cur->close++;
                    652:                        if (0 > --toclose)
                    653:                                goto fail;
                    654:                        continue;
                    655:                } else if (0 == strcmp("-a", argv[i])) {
                    656:                        if (toopen || logic || igncase || NULL == cur)
                    657:                                goto fail;
                    658:                        logic = 1;
                    659:                        continue;
                    660:                } else if (0 == strcmp("-o", argv[i])) {
                    661:                        if (toopen || logic || igncase || NULL == cur)
                    662:                                goto fail;
                    663:                        logic = 2;
                    664:                        continue;
                    665:                } else if (0 == strcmp("-i", argv[i])) {
                    666:                        if (igncase)
                    667:                                goto fail;
                    668:                        igncase = 1;
                    669:                        continue;
1.1       schwarze  670:                }
1.4       schwarze  671:                next = exprterm(search, argv[i], !igncase);
                    672:                if (NULL == next)
                    673:                        goto fail;
1.17      schwarze  674:                if (NULL == first)
                    675:                        first = next;
                    676:                else
1.1       schwarze  677:                        cur->next = next;
1.18      schwarze  678:                prev = cur = next;
1.17      schwarze  679:
                    680:                /*
                    681:                 * Searching for descriptions must be split out
                    682:                 * because they are stored in the mpages table,
                    683:                 * not in the keys table.
                    684:                 */
                    685:
1.18      schwarze  686:                for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
                    687:                        if (mask & cur->bits && ~mask & cur->bits) {
                    688:                                next = mandoc_calloc(1,
                    689:                                    sizeof(struct expr));
                    690:                                memcpy(next, cur, sizeof(struct expr));
                    691:                                prev->open = 1;
                    692:                                cur->bits = mask;
                    693:                                cur->next = next;
                    694:                                cur = next;
                    695:                                cur->bits &= ~mask;
                    696:                        }
                    697:                }
                    698:                prev->and = (1 == logic);
                    699:                prev->open += toopen;
                    700:                if (cur != prev)
1.17      schwarze  701:                        cur->close = 1;
1.18      schwarze  702:
1.4       schwarze  703:                toopen = logic = igncase = 0;
1.1       schwarze  704:        }
1.6       schwarze  705:        if (toopen || logic || igncase || toclose)
                    706:                goto fail;
                    707:
1.23      schwarze  708:        if (NULL != search->sec || NULL != search->arch)
                    709:                cur->close++;
                    710:        if (NULL != search->arch)
                    711:                cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
                    712:        if (NULL != search->sec)
                    713:                exprspec(cur, TYPE_sec, search->sec, "^%s$");
1.6       schwarze  714:
                    715:        return(first);
                    716:
1.4       schwarze  717: fail:
                    718:        if (NULL != first)
                    719:                exprfree(first);
                    720:        return(NULL);
1.1       schwarze  721: }
                    722:
                    723: static struct expr *
1.6       schwarze  724: exprspec(struct expr *cur, uint64_t key, const char *value,
                    725:                const char *format)
                    726: {
                    727:        char     errbuf[BUFSIZ];
                    728:        char    *cp;
                    729:        int      irc;
                    730:
1.15      schwarze  731:        mandoc_asprintf(&cp, format, value);
1.6       schwarze  732:        cur->next = mandoc_calloc(1, sizeof(struct expr));
                    733:        cur = cur->next;
                    734:        cur->and = 1;
                    735:        cur->bits = key;
                    736:        if (0 != (irc = regcomp(&cur->regexp, cp,
                    737:            REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
                    738:                regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
                    739:                fprintf(stderr, "regcomp: %s\n", errbuf);
                    740:                cur->substr = value;
                    741:        }
                    742:        free(cp);
                    743:        return(cur);
                    744: }
                    745:
                    746: static struct expr *
1.1       schwarze  747: exprterm(const struct mansearch *search, char *buf, int cs)
                    748: {
1.6       schwarze  749:        char             errbuf[BUFSIZ];
1.1       schwarze  750:        struct expr     *e;
1.28      schwarze  751:        char            *key, *val;
1.11      schwarze  752:        uint64_t         iterbit;
                    753:        int              i, irc;
1.1       schwarze  754:
                    755:        if ('\0' == *buf)
                    756:                return(NULL);
                    757:
                    758:        e = mandoc_calloc(1, sizeof(struct expr));
                    759:
1.33      schwarze  760:        if (search->argmode == ARG_NAME) {
                    761:                e->bits = TYPE_Nm;
1.1       schwarze  762:                e->substr = buf;
1.28      schwarze  763:                e->equal = 1;
1.1       schwarze  764:                return(e);
                    765:        }
                    766:
                    767:        /*
1.33      schwarze  768:         * Separate macro keys from search string.
                    769:         * If needed, request regular expression handling
                    770:         * by setting e->substr to NULL.
1.1       schwarze  771:         */
                    772:
1.33      schwarze  773:        if (search->argmode == ARG_WORD) {
                    774:                e->bits = TYPE_Nm;
                    775:                e->substr = NULL;
                    776:                mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", buf);
                    777:                cs = 0;
                    778:        } else if ((val = strpbrk(buf, "=~")) == NULL) {
                    779:                e->bits = TYPE_Nm | TYPE_Nd;
1.1       schwarze  780:                e->substr = buf;
1.28      schwarze  781:        } else {
                    782:                if (val == buf)
1.33      schwarze  783:                        e->bits = TYPE_Nm | TYPE_Nd;
1.28      schwarze  784:                if ('=' == *val)
                    785:                        e->substr = val + 1;
                    786:                *val++ = '\0';
1.12      schwarze  787:                if (NULL != strstr(buf, "arch"))
                    788:                        cs = 0;
1.28      schwarze  789:        }
                    790:
                    791:        /* Compile regular expressions. */
                    792:
                    793:        if (NULL == e->substr) {
                    794:                irc = regcomp(&e->regexp, val,
                    795:                    REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.33      schwarze  796:                if (search->argmode == ARG_WORD)
1.28      schwarze  797:                        free(val);
                    798:                if (irc) {
1.6       schwarze  799:                        regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
                    800:                        fprintf(stderr, "regcomp: %s\n", errbuf);
1.1       schwarze  801:                        free(e);
                    802:                        return(NULL);
                    803:                }
1.28      schwarze  804:        }
                    805:
                    806:        if (e->bits)
                    807:                return(e);
1.1       schwarze  808:
                    809:        /*
                    810:         * Parse out all possible fields.
                    811:         * If the field doesn't resolve, bail.
                    812:         */
                    813:
                    814:        while (NULL != (key = strsep(&buf, ","))) {
                    815:                if ('\0' == *key)
                    816:                        continue;
1.11      schwarze  817:                for (i = 0, iterbit = 1;
                    818:                     i < mansearch_keymax;
                    819:                     i++, iterbit <<= 1) {
                    820:                        if (0 == strcasecmp(key,
                    821:                            mansearch_keynames[i])) {
                    822:                                e->bits |= iterbit;
                    823:                                break;
                    824:                        }
                    825:                }
                    826:                if (i == mansearch_keymax) {
                    827:                        if (strcasecmp(key, "any")) {
                    828:                                free(e);
                    829:                                return(NULL);
                    830:                        }
                    831:                        e->bits |= ~0ULL;
1.1       schwarze  832:                }
                    833:        }
                    834:
                    835:        return(e);
                    836: }
                    837:
                    838: static void
                    839: exprfree(struct expr *p)
                    840: {
                    841:        struct expr     *pp;
                    842:
                    843:        while (NULL != p) {
                    844:                pp = p->next;
                    845:                free(p);
                    846:                p = pp;
                    847:        }
                    848: }
                    849:
                    850: static void *
1.27      espie     851: hash_calloc(size_t nmemb, size_t sz, void *arg)
1.1       schwarze  852: {
                    853:
1.27      espie     854:        return(mandoc_calloc(nmemb, sz));
1.1       schwarze  855: }
                    856:
                    857: static void *
                    858: hash_alloc(size_t sz, void *arg)
                    859: {
                    860:
                    861:        return(mandoc_malloc(sz));
                    862: }
                    863:
                    864: static void
1.27      espie     865: hash_free(void *p, void *arg)
1.1       schwarze  866: {
                    867:
                    868:        free(p);
                    869: }