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

Annotation of mandoc/mansearch.c, Revision 1.56

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

CVSweb