[BACK]Return to mansearch.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mansearch.c, Revision 1.13

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

CVSweb