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

Annotation of mandoc/mansearch.c, Revision 1.4

1.4     ! kristaps    1: /*     $Id: mansearch.c,v 1.3 2012/06/08 15:06:28 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <sys/param.h>
                     22:
                     23: #include <assert.h>
                     24: #include <fcntl.h>
                     25: #include <getopt.h>
                     26: #include <stdio.h>
                     27: #include <stdint.h>
                     28: #include <stddef.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <unistd.h>
                     32:
1.4     ! kristaps   33: #ifdef HAVE_OHASH
1.1       kristaps   34: #include <ohash.h>
1.4     ! kristaps   35: #else
        !            36: #include "compat_ohash.h"
        !            37: #endif
1.1       kristaps   38: #include <sqlite3.h>
                     39:
                     40: #include "mandoc.h"
                     41: #include "manpath.h"
                     42: #include "mandocdb.h"
                     43: #include "mansearch.h"
                     44:
1.3       kristaps   45: #define        SQL_BIND_TEXT(_db, _s, _i, _v) \
1.2       kristaps   46:        if (SQLITE_OK != sqlite3_bind_text \
                     47:                ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
                     48:                fprintf(stderr, "%s\n", sqlite3_errmsg((_db)))
1.3       kristaps   49: #define        SQL_BIND_INT64(_db, _s, _i, _v) \
1.2       kristaps   50:        if (SQLITE_OK != sqlite3_bind_int64 \
                     51:                ((_s), (_i)++, (_v))) \
                     52:                fprintf(stderr, "%s\n", sqlite3_errmsg((_db)))
                     53:
1.1       kristaps   54: struct expr {
                     55:        int              glob; /* is glob? */
                     56:        uint64_t         bits; /* type-mask */
                     57:        const char      *v; /* search value */
                     58:        struct expr     *next; /* next in sequence */
                     59: };
                     60:
                     61: struct match {
                     62:        uint64_t         id; /* identifier in database */
                     63:        char            *file; /* relative filepath of manpage */
                     64:        char            *desc; /* description of manpage */
                     65:        int              form; /* 0 == catpage */
                     66: };
                     67:
                     68: struct type {
                     69:        uint64_t         bits;
                     70:        const char      *name;
                     71: };
                     72:
                     73: static const struct type types[] = {
                     74:        { TYPE_An,  "An" },
                     75:        { TYPE_Ar,  "Ar" },
                     76:        { TYPE_At,  "At" },
                     77:        { TYPE_Bsx, "Bsx" },
                     78:        { TYPE_Bx,  "Bx" },
                     79:        { TYPE_Cd,  "Cd" },
                     80:        { TYPE_Cm,  "Cm" },
                     81:        { TYPE_Dv,  "Dv" },
                     82:        { TYPE_Dx,  "Dx" },
                     83:        { TYPE_Em,  "Em" },
                     84:        { TYPE_Er,  "Er" },
                     85:        { TYPE_Ev,  "Ev" },
                     86:        { TYPE_Fa,  "Fa" },
                     87:        { TYPE_Fl,  "Fl" },
                     88:        { TYPE_Fn,  "Fn" },
                     89:        { TYPE_Fn,  "Fo" },
                     90:        { TYPE_Ft,  "Ft" },
                     91:        { TYPE_Fx,  "Fx" },
                     92:        { TYPE_Ic,  "Ic" },
                     93:        { TYPE_In,  "In" },
                     94:        { TYPE_Lb,  "Lb" },
                     95:        { TYPE_Li,  "Li" },
                     96:        { TYPE_Lk,  "Lk" },
                     97:        { TYPE_Ms,  "Ms" },
                     98:        { TYPE_Mt,  "Mt" },
                     99:        { TYPE_Nd,  "Nd" },
                    100:        { TYPE_Nm,  "Nm" },
                    101:        { TYPE_Nx,  "Nx" },
                    102:        { TYPE_Ox,  "Ox" },
                    103:        { TYPE_Pa,  "Pa" },
                    104:        { TYPE_Rs,  "Rs" },
                    105:        { TYPE_Sh,  "Sh" },
                    106:        { TYPE_Ss,  "Ss" },
                    107:        { TYPE_St,  "St" },
                    108:        { TYPE_Sy,  "Sy" },
                    109:        { TYPE_Tn,  "Tn" },
                    110:        { TYPE_Va,  "Va" },
                    111:        { TYPE_Va,  "Vt" },
                    112:        { TYPE_Xr,  "Xr" },
                    113:        { ~0ULL,    "any" },
                    114:        { 0ULL, NULL }
                    115: };
                    116:
                    117: static void            *hash_alloc(size_t, void *);
                    118: static void             hash_free(void *, size_t, void *);
                    119: static void            *hash_halloc(size_t, void *);
                    120: static struct expr     *exprcomp(int, char *[]);
                    121: static void             exprfree(struct expr *);
                    122: static struct expr     *exprterm(char *);
                    123: static char            *sql_statement(const struct expr *,
                    124:                                const char *, const char *);
                    125:
                    126: int
                    127: mansearch(const struct manpaths *paths,
                    128:                const char *arch, const char *sec,
                    129:                int argc, char *argv[],
                    130:                struct manpage **res, size_t *sz)
                    131: {
1.2       kristaps  132:        int              fd, rc, c;
1.1       kristaps  133:        int64_t          id;
                    134:        char             buf[MAXPATHLEN];
                    135:        char            *sql;
                    136:        struct expr     *e, *ep;
                    137:        sqlite3         *db;
                    138:        sqlite3_stmt    *s;
                    139:        struct match    *mp;
                    140:        struct ohash_info info;
                    141:        struct ohash     htab;
                    142:        unsigned int     idx;
                    143:        size_t           i, j, cur, maxres;
                    144:
                    145:        memset(&info, 0, sizeof(struct ohash_info));
                    146:
                    147:        info.halloc = hash_halloc;
                    148:        info.alloc = hash_alloc;
                    149:        info.hfree = hash_free;
                    150:        info.key_offset = offsetof(struct match, id);
                    151:
1.2       kristaps  152:        *sz = cur = maxres = 0;
1.1       kristaps  153:        sql = NULL;
                    154:        *res = NULL;
                    155:        fd = -1;
                    156:        e = NULL;
1.2       kristaps  157:        rc = 0;
1.1       kristaps  158:
                    159:        if (0 == argc)
                    160:                goto out;
                    161:        if (NULL == (e = exprcomp(argc, argv)))
                    162:                goto out;
                    163:
                    164:        /*
                    165:         * Save a descriptor to the current working directory.
                    166:         * Since pathnames in the "paths" variable might be relative,
                    167:         * and we'll be chdir()ing into them, we need to keep a handle
                    168:         * on our current directory from which to start the chdir().
                    169:         */
                    170:
                    171:        if (NULL == getcwd(buf, MAXPATHLEN)) {
                    172:                perror(NULL);
                    173:                goto out;
                    174:        } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
                    175:                perror(buf);
                    176:                goto out;
                    177:        }
                    178:
                    179:        sql = sql_statement(e, arch, sec);
                    180:
                    181:        /*
                    182:         * Loop over the directories (containing databases) for us to
                    183:         * search.
                    184:         * Don't let missing/bad databases/directories phase us.
                    185:         * In each, try to open the resident database and, if it opens,
                    186:         * scan it for our match expression.
                    187:         */
                    188:
                    189:        for (i = 0; i < paths->sz; i++) {
                    190:                if (-1 == fchdir(fd)) {
                    191:                        perror(buf);
                    192:                        free(*res);
                    193:                        break;
                    194:                } else if (-1 == chdir(paths->paths[i])) {
                    195:                        perror(paths->paths[i]);
                    196:                        continue;
                    197:                }
                    198:
1.2       kristaps  199:                c =  sqlite3_open_v2
                    200:                        (MANDOC_DB, &db,
                    201:                         SQLITE_OPEN_READONLY, NULL);
1.1       kristaps  202:
1.2       kristaps  203:                if (SQLITE_OK != c) {
1.1       kristaps  204:                        perror(MANDOC_DB);
                    205:                        sqlite3_close(db);
                    206:                        continue;
                    207:                }
                    208:
                    209:                j = 1;
1.2       kristaps  210:                c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
                    211:                if (SQLITE_OK != c)
                    212:                        fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1       kristaps  213:
                    214:                if (NULL != arch)
1.3       kristaps  215:                        SQL_BIND_TEXT(db, s, j, arch);
1.1       kristaps  216:                if (NULL != sec)
1.3       kristaps  217:                        SQL_BIND_TEXT(db, s, j, arch);
1.1       kristaps  218:
                    219:                for (ep = e; NULL != ep; ep = ep->next) {
1.3       kristaps  220:                        SQL_BIND_TEXT(db, s, j, ep->v);
                    221:                        SQL_BIND_INT64(db, s, j, ep->bits);
1.1       kristaps  222:                }
                    223:
                    224:                memset(&htab, 0, sizeof(struct ohash));
                    225:                ohash_init(&htab, 4, &info);
                    226:
                    227:                /*
                    228:                 * Hash each entry on its [unique] document identifier.
                    229:                 * This is a uint64_t.
                    230:                 * Instead of using a hash function, simply convert the
                    231:                 * uint64_t to a uint32_t, the hash value's type.
                    232:                 * This gives good performance and preserves the
                    233:                 * distribution of buckets in the table.
                    234:                 */
1.2       kristaps  235:                while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.1       kristaps  236:                        id = sqlite3_column_int64(s, 0);
                    237:                        idx = ohash_lookup_memory
                    238:                                (&htab, (char *)&id,
                    239:                                 sizeof(uint64_t), (uint32_t)id);
                    240:
                    241:                        if (NULL != ohash_find(&htab, idx))
                    242:                                continue;
                    243:
                    244:                        mp = mandoc_calloc(1, sizeof(struct match));
                    245:                        mp->id = id;
                    246:                        mp->file = mandoc_strdup
                    247:                                ((char *)sqlite3_column_text(s, 3));
                    248:                        mp->desc = mandoc_strdup
                    249:                                ((char *)sqlite3_column_text(s, 4));
                    250:                        mp->form = sqlite3_column_int(s, 5);
                    251:                        ohash_insert(&htab, idx, mp);
                    252:                }
                    253:
1.2       kristaps  254:                if (SQLITE_DONE != c)
                    255:                        fprintf(stderr, "%s\n", sqlite3_errmsg(db));
                    256:
1.1       kristaps  257:                sqlite3_finalize(s);
                    258:                sqlite3_close(db);
                    259:
                    260:                for (mp = ohash_first(&htab, &idx);
                    261:                                NULL != mp;
                    262:                                mp = ohash_next(&htab, &idx)) {
                    263:                        if (cur + 1 > maxres) {
                    264:                                maxres += 1024;
                    265:                                *res = mandoc_realloc
                    266:                                        (*res, maxres * sizeof(struct manpage));
                    267:                        }
                    268:                        strlcpy((*res)[cur].file,
                    269:                                paths->paths[i], MAXPATHLEN);
                    270:                        strlcat((*res)[cur].file, "/", MAXPATHLEN);
                    271:                        strlcat((*res)[cur].file, mp->file, MAXPATHLEN);
                    272:                        (*res)[cur].desc = mp->desc;
                    273:                        (*res)[cur].form = mp->form;
                    274:                        free(mp->file);
                    275:                        free(mp);
                    276:                        cur++;
                    277:                }
                    278:                ohash_delete(&htab);
                    279:        }
1.2       kristaps  280:        rc = 1;
1.1       kristaps  281: out:
                    282:        exprfree(e);
                    283:        if (-1 != fd)
                    284:                close(fd);
                    285:        free(sql);
                    286:        *sz = cur;
1.2       kristaps  287:        return(rc);
1.1       kristaps  288: }
                    289:
                    290: /*
                    291:  * Prepare the search SQL statement.
                    292:  * We search for any of the words specified in our match expression.
                    293:  * We filter the per-doc AND expressions when collecting results.
                    294:  */
                    295: static char *
                    296: sql_statement(const struct expr *e, const char *arch, const char *sec)
                    297: {
                    298:        char            *sql;
                    299:        const char      *glob = "(key GLOB ? AND bits & ?)";
                    300:        const char      *eq = "(key = ? AND bits & ?)";
                    301:        const char      *andarch = "arch = ? AND ";
                    302:        const char      *andsec = "sec = ? AND ";
1.2       kristaps  303:        size_t           globsz;
                    304:        size_t           eqsz;
1.1       kristaps  305:        size_t           sz;
                    306:
                    307:        sql = mandoc_strdup
                    308:                ("SELECT docid,bits,key,file,desc,form,sec,arch "
                    309:                 "FROM keys "
                    310:                 "INNER JOIN docs ON docs.id=keys.docid "
                    311:                 "WHERE ");
                    312:        sz = strlen(sql);
1.2       kristaps  313:        globsz = strlen(glob);
                    314:        eqsz = strlen(eq);
1.1       kristaps  315:
                    316:        if (NULL != arch) {
                    317:                sz += strlen(andarch) + 1;
                    318:                sql = mandoc_realloc(sql, sz);
                    319:                strlcat(sql, andarch, sz);
                    320:        }
1.2       kristaps  321:
1.1       kristaps  322:        if (NULL != sec) {
                    323:                sz += strlen(andsec) + 1;
                    324:                sql = mandoc_realloc(sql, sz);
                    325:                strlcat(sql, andsec, sz);
                    326:        }
                    327:
                    328:        sz += 2;
                    329:        sql = mandoc_realloc(sql, sz);
                    330:        strlcat(sql, "(", sz);
                    331:
                    332:        for ( ; NULL != e; e = e->next) {
                    333:                sz += (e->glob ? globsz : eqsz) +
                    334:                        (NULL == e->next ? 3 : 5);
                    335:                sql = mandoc_realloc(sql, sz);
                    336:                strlcat(sql, e->glob ? glob : eq, sz);
                    337:                strlcat(sql, NULL == e->next ? ");" : " OR ", sz);
                    338:        }
                    339:
                    340:        return(sql);
                    341: }
                    342:
                    343: /*
                    344:  * Compile a set of string tokens into an expression.
                    345:  * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
                    346:  * "(", "foo=bar", etc.).
                    347:  */
                    348: static struct expr *
                    349: exprcomp(int argc, char *argv[])
                    350: {
                    351:        int              i;
                    352:        struct expr     *first, *next, *cur;
                    353:
                    354:        first = cur = NULL;
                    355:
                    356:        for (i = 0; i < argc; i++) {
                    357:                next = exprterm(argv[i]);
                    358:                if (NULL == next) {
                    359:                        exprfree(first);
                    360:                        return(NULL);
                    361:                }
                    362:                if (NULL != first) {
                    363:                        cur->next = next;
                    364:                        cur = next;
                    365:                } else
                    366:                        cur = first = next;
                    367:        }
                    368:
                    369:        return(first);
                    370: }
                    371:
                    372: static struct expr *
                    373: exprterm(char *buf)
                    374: {
                    375:        struct expr     *e;
                    376:        char            *key, *v;
                    377:        size_t           i;
                    378:
                    379:        if ('\0' == *buf)
                    380:                return(NULL);
                    381:
                    382:        e = mandoc_calloc(1, sizeof(struct expr));
                    383:
                    384:        /*
                    385:         * If no =~ is specified, search with equality over names and
                    386:         * descriptions.
                    387:         * If =~ begins the phrase, use name and description fields.
                    388:         */
                    389:
                    390:        if (NULL == (v = strpbrk(buf, "=~"))) {
                    391:                e->v = buf;
                    392:                e->bits = TYPE_Nm | TYPE_Nd;
                    393:                return(e);
                    394:        } else if (v == buf)
                    395:                e->bits = TYPE_Nm | TYPE_Nd;
                    396:
                    397:        e->glob = '~' == *v;
                    398:        *v++ = '\0';
                    399:        e->v = v;
                    400:
                    401:        /*
                    402:         * Parse out all possible fields.
                    403:         * If the field doesn't resolve, bail.
                    404:         */
                    405:
                    406:        while (NULL != (key = strsep(&buf, ","))) {
                    407:                if ('\0' == *key)
                    408:                        continue;
                    409:                i = 0;
                    410:                while (types[i].bits &&
                    411:                        strcasecmp(types[i].name, key))
                    412:                        i++;
                    413:                if (0 == types[i].bits) {
                    414:                        free(e);
                    415:                        return(NULL);
                    416:                }
                    417:                e->bits |= types[i].bits;
                    418:        }
                    419:
                    420:        return(e);
                    421: }
                    422:
                    423: static void
                    424: exprfree(struct expr *p)
                    425: {
                    426:        struct expr     *pp;
                    427:
                    428:        while (NULL != p) {
                    429:                pp = p->next;
                    430:                free(p);
                    431:                p = pp;
                    432:        }
                    433: }
                    434:
                    435: static void *
                    436: hash_halloc(size_t sz, void *arg)
                    437: {
                    438:
                    439:        return(mandoc_calloc(sz, 1));
                    440: }
                    441:
                    442: static void *
                    443: hash_alloc(size_t sz, void *arg)
                    444: {
                    445:
                    446:        return(mandoc_malloc(sz));
                    447: }
                    448:
                    449: static void
                    450: hash_free(void *p, size_t sz, void *arg)
                    451: {
                    452:
                    453:        free(p);
                    454: }

CVSweb