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

Annotation of mandoc/mansearch.c, Revision 1.15

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

CVSweb