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

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