[BACK]Return to mansearch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mansearch.c, Revision 1.50

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