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

Annotation of mandoc/mansearch.c, Revision 1.48

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

CVSweb