[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.26

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