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

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