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

Annotation of mandoc/mansearch.c, Revision 1.18

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

CVSweb