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

Annotation of mandoc/mansearch.c, Revision 1.26

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

CVSweb