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

Annotation of mandoc/mansearch.c, Revision 1.64

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

CVSweb