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

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