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

Annotation of mandoc/apropos_db.c, Revision 1.6

1.6     ! kristaps    1: /*     $Id: apropos_db.c,v 1.5 2011/11/18 07:02:19 kristaps Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.3       schwarze    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #include <assert.h>
                     19: #include <fcntl.h>
                     20: #include <regex.h>
                     21: #include <stdarg.h>
1.6     ! kristaps   22: #include <stdint.h>
1.1       schwarze   23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
                     26: #ifdef __linux__
                     27: # include <db_185.h>
                     28: #else
                     29: # include <db.h>
                     30: #endif
                     31:
1.2       schwarze   32: #include "mandocdb.h"
1.1       schwarze   33: #include "apropos_db.h"
                     34: #include "mandoc.h"
                     35:
1.5       kristaps   36: struct rec {
                     37:        struct res       res; /* resulting record info */
                     38:        /*
                     39:         * Maintain a binary tree for checking the uniqueness of `rec'
                     40:         * when adding elements to the results array.
                     41:         * Since the results array is dynamic, use offset in the array
                     42:         * instead of a pointer to the structure.
                     43:         */
                     44:        int              lhs;
                     45:        int              rhs;
                     46:        int              matched; /* expression is true */
                     47:        int             *matches; /* partial truth evaluations */
                     48: };
                     49:
1.1       schwarze   50: struct expr {
1.5       kristaps   51:        int              regex; /* is regex? */
                     52:        int              index; /* index in match array */
1.6     ! kristaps   53:        uint64_t         mask; /* type-mask */
1.5       kristaps   54:        int              cs; /* is case-sensitive? */
                     55:        int              and; /* is rhs of logical AND? */
                     56:        char            *v; /* search value */
                     57:        regex_t          re; /* compiled re, if regex */
                     58:        struct expr     *next; /* next in sequence */
                     59:        struct expr     *subexpr;
1.1       schwarze   60: };
                     61:
                     62: struct type {
1.6     ! kristaps   63:        uint64_t         mask;
1.1       schwarze   64:        const char      *name;
                     65: };
                     66:
                     67: static const struct type types[] = {
1.6     ! kristaps   68:        { TYPE_An, "An" },
        !            69:        { TYPE_Ar, "Ar" },
        !            70:        { TYPE_At, "At" },
        !            71:        { TYPE_Bsx, "Bsx" },
        !            72:        { TYPE_Bx, "Bx" },
        !            73:        { TYPE_Cd, "Cd" },
        !            74:        { TYPE_Cm, "Cm" },
        !            75:        { TYPE_Dv, "Dv" },
        !            76:        { TYPE_Dx, "Dx" },
        !            77:        { TYPE_Em, "Em" },
        !            78:        { TYPE_Er, "Er" },
        !            79:        { TYPE_Ev, "Ev" },
        !            80:        { TYPE_Fa, "Fa" },
        !            81:        { TYPE_Fl, "Fl" },
        !            82:        { TYPE_Fn, "Fn" },
        !            83:        { TYPE_Fn, "Fo" },
        !            84:        { TYPE_Ft, "Ft" },
        !            85:        { TYPE_Fx, "Fx" },
        !            86:        { TYPE_Ic, "Ic" },
        !            87:        { TYPE_In, "In" },
        !            88:        { TYPE_Lb, "Lb" },
        !            89:        { TYPE_Li, "Li" },
        !            90:        { TYPE_Lk, "Lk" },
        !            91:        { TYPE_Ms, "Ms" },
        !            92:        { TYPE_Mt, "Mt" },
        !            93:        { TYPE_Nd, "Nd" },
        !            94:        { TYPE_Nm, "Nm" },
        !            95:        { TYPE_Nx, "Nx" },
        !            96:        { TYPE_Ox, "Ox" },
        !            97:        { TYPE_Pa, "Pa" },
        !            98:        { TYPE_Rs, "Rs" },
        !            99:        { TYPE_Sh, "Sh" },
        !           100:        { TYPE_Ss, "Ss" },
        !           101:        { TYPE_St, "St" },
        !           102:        { TYPE_Sy, "Sy" },
        !           103:        { TYPE_Tn, "Tn" },
        !           104:        { TYPE_Va, "Va" },
        !           105:        { TYPE_Va, "Vt" },
        !           106:        { TYPE_Xr, "Xr" },
1.2       schwarze  107:        { INT_MAX, "any" },
1.1       schwarze  108:        { 0, NULL }
                    109: };
                    110:
                    111: static DB      *btree_open(void);
1.5       kristaps  112: static int      btree_read(const DBT *,
                    113:                        const struct mchars *, char **);
                    114: static int      expreval(const struct expr *, int *);
                    115: static void     exprexec(const struct expr *,
1.6     ! kristaps  116:                        const char *, uint64_t, struct rec *);
1.5       kristaps  117: static int      exprmark(const struct expr *,
1.6     ! kristaps  118:                        const char *, uint64_t, int *);
1.5       kristaps  119: static struct expr *exprexpr(int, char *[], int *, int *, size_t *);
                    120: static struct expr *exprterm(char *, int);
1.1       schwarze  121: static DB      *index_open(void);
                    122: static int      index_read(const DBT *, const DBT *,
                    123:                        const struct mchars *, struct rec *);
                    124: static void     norm_string(const char *,
                    125:                        const struct mchars *, char **);
                    126: static size_t   norm_utf8(unsigned int, char[7]);
1.5       kristaps  127: static void     recfree(struct rec *);
1.1       schwarze  128:
                    129: /*
                    130:  * Open the keyword mandoc-db database.
                    131:  */
                    132: static DB *
                    133: btree_open(void)
                    134: {
                    135:        BTREEINFO        info;
                    136:        DB              *db;
                    137:
                    138:        memset(&info, 0, sizeof(BTREEINFO));
                    139:        info.flags = R_DUP;
                    140:
1.2       schwarze  141:        db = dbopen(MANDOC_DB, O_RDONLY, 0, DB_BTREE, &info);
1.1       schwarze  142:        if (NULL != db)
                    143:                return(db);
                    144:
                    145:        return(NULL);
                    146: }
                    147:
                    148: /*
                    149:  * Read a keyword from the database and normalise it.
                    150:  * Return 0 if the database is insane, else 1.
                    151:  */
                    152: static int
                    153: btree_read(const DBT *v, const struct mchars *mc, char **buf)
                    154: {
                    155:
                    156:        /* Sanity: are we nil-terminated? */
                    157:
                    158:        assert(v->size > 0);
1.6     ! kristaps  159:
1.1       schwarze  160:        if ('\0' != ((char *)v->data)[(int)v->size - 1])
                    161:                return(0);
                    162:
                    163:        norm_string((char *)v->data, mc, buf);
                    164:        return(1);
                    165: }
                    166:
                    167: /*
                    168:  * Take a Unicode codepoint and produce its UTF-8 encoding.
                    169:  * This isn't the best way to do this, but it works.
                    170:  * The magic numbers are from the UTF-8 packaging.
                    171:  * They're not as scary as they seem: read the UTF-8 spec for details.
                    172:  */
                    173: static size_t
                    174: norm_utf8(unsigned int cp, char out[7])
                    175: {
                    176:        size_t           rc;
                    177:
                    178:        rc = 0;
                    179:
                    180:        if (cp <= 0x0000007F) {
                    181:                rc = 1;
                    182:                out[0] = (char)cp;
                    183:        } else if (cp <= 0x000007FF) {
                    184:                rc = 2;
                    185:                out[0] = (cp >> 6  & 31) | 192;
                    186:                out[1] = (cp       & 63) | 128;
                    187:        } else if (cp <= 0x0000FFFF) {
                    188:                rc = 3;
                    189:                out[0] = (cp >> 12 & 15) | 224;
                    190:                out[1] = (cp >> 6  & 63) | 128;
                    191:                out[2] = (cp       & 63) | 128;
                    192:        } else if (cp <= 0x001FFFFF) {
                    193:                rc = 4;
                    194:                out[0] = (cp >> 18 & 7) | 240;
                    195:                out[1] = (cp >> 12 & 63) | 128;
                    196:                out[2] = (cp >> 6  & 63) | 128;
                    197:                out[3] = (cp       & 63) | 128;
                    198:        } else if (cp <= 0x03FFFFFF) {
                    199:                rc = 5;
                    200:                out[0] = (cp >> 24 & 3) | 248;
                    201:                out[1] = (cp >> 18 & 63) | 128;
                    202:                out[2] = (cp >> 12 & 63) | 128;
                    203:                out[3] = (cp >> 6  & 63) | 128;
                    204:                out[4] = (cp       & 63) | 128;
                    205:        } else if (cp <= 0x7FFFFFFF) {
                    206:                rc = 6;
                    207:                out[0] = (cp >> 30 & 1) | 252;
                    208:                out[1] = (cp >> 24 & 63) | 128;
                    209:                out[2] = (cp >> 18 & 63) | 128;
                    210:                out[3] = (cp >> 12 & 63) | 128;
                    211:                out[4] = (cp >> 6  & 63) | 128;
                    212:                out[5] = (cp       & 63) | 128;
                    213:        } else
                    214:                return(0);
                    215:
                    216:        out[rc] = '\0';
                    217:        return(rc);
                    218: }
                    219:
                    220: /*
                    221:  * Normalise strings from the index and database.
                    222:  * These strings are escaped as defined by mandoc_char(7) along with
                    223:  * other goop in mandoc.h (e.g., soft hyphens).
                    224:  * This function normalises these into a nice UTF-8 string.
                    225:  * Returns 0 if the database is fucked.
                    226:  */
                    227: static void
                    228: norm_string(const char *val, const struct mchars *mc, char **buf)
                    229: {
                    230:        size_t            sz, bsz;
                    231:        char              utfbuf[7];
                    232:        const char       *seq, *cpp;
                    233:        int               len, u, pos;
                    234:        enum mandoc_esc   esc;
                    235:        static const char res[] = { '\\', '\t',
                    236:                                ASCII_NBRSP, ASCII_HYPH, '\0' };
                    237:
                    238:        /* Pre-allocate by the length of the input */
                    239:
                    240:        bsz = strlen(val) + 1;
                    241:        *buf = mandoc_realloc(*buf, bsz);
                    242:        pos = 0;
                    243:
                    244:        while ('\0' != *val) {
                    245:                /*
                    246:                 * Halt on the first escape sequence.
                    247:                 * This also halts on the end of string, in which case
                    248:                 * we just copy, fallthrough, and exit the loop.
                    249:                 */
                    250:                if ((sz = strcspn(val, res)) > 0) {
                    251:                        memcpy(&(*buf)[pos], val, sz);
                    252:                        pos += (int)sz;
                    253:                        val += (int)sz;
                    254:                }
                    255:
                    256:                if (ASCII_HYPH == *val) {
                    257:                        (*buf)[pos++] = '-';
                    258:                        val++;
                    259:                        continue;
                    260:                } else if ('\t' == *val || ASCII_NBRSP == *val) {
                    261:                        (*buf)[pos++] = ' ';
                    262:                        val++;
                    263:                        continue;
                    264:                } else if ('\\' != *val)
                    265:                        break;
                    266:
                    267:                /* Read past the slash. */
                    268:
                    269:                val++;
                    270:                u = 0;
                    271:
                    272:                /*
                    273:                 * Parse the escape sequence and see if it's a
                    274:                 * predefined character or special character.
                    275:                 */
                    276:
                    277:                esc = mandoc_escape(&val, &seq, &len);
                    278:                if (ESCAPE_ERROR == esc)
                    279:                        break;
                    280:
                    281:                /*
                    282:                 * XXX - this just does UTF-8, but we need to know
                    283:                 * beforehand whether we should do text substitution.
                    284:                 */
                    285:
                    286:                switch (esc) {
                    287:                case (ESCAPE_SPECIAL):
                    288:                        if (0 != (u = mchars_spec2cp(mc, seq, len)))
                    289:                                break;
                    290:                        /* FALLTHROUGH */
                    291:                default:
                    292:                        continue;
                    293:                }
                    294:
                    295:                /*
                    296:                 * If we have a Unicode codepoint, try to convert that
                    297:                 * to a UTF-8 byte string.
                    298:                 */
                    299:
                    300:                cpp = utfbuf;
                    301:                if (0 == (sz = norm_utf8(u, utfbuf)))
                    302:                        continue;
                    303:
                    304:                /* Copy the rendered glyph into the stream. */
                    305:
                    306:                sz = strlen(cpp);
                    307:                bsz += sz;
                    308:
                    309:                *buf = mandoc_realloc(*buf, bsz);
                    310:
                    311:                memcpy(&(*buf)[pos], cpp, sz);
                    312:                pos += (int)sz;
                    313:        }
                    314:
                    315:        (*buf)[pos] = '\0';
                    316: }
                    317:
                    318: /*
                    319:  * Open the filename-index mandoc-db database.
                    320:  * Returns NULL if opening failed.
                    321:  */
                    322: static DB *
                    323: index_open(void)
                    324: {
                    325:        DB              *db;
                    326:
1.2       schwarze  327:        db = dbopen(MANDOC_IDX, O_RDONLY, 0, DB_RECNO, NULL);
1.1       schwarze  328:        if (NULL != db)
                    329:                return(db);
                    330:
                    331:        return(NULL);
                    332: }
                    333:
                    334: /*
                    335:  * Safely unpack from an index file record into the structure.
                    336:  * Returns 1 if an entry was unpacked, 0 if the database is insane.
                    337:  */
                    338: static int
                    339: index_read(const DBT *key, const DBT *val,
                    340:                const struct mchars *mc, struct rec *rec)
                    341: {
                    342:        size_t           left;
                    343:        char            *np, *cp;
                    344:
                    345: #define        INDEX_BREAD(_dst) \
                    346:        do { \
                    347:                if (NULL == (np = memchr(cp, '\0', left))) \
                    348:                        return(0); \
                    349:                norm_string(cp, mc, &(_dst)); \
                    350:                left -= (np - cp) + 1; \
                    351:                cp = np + 1; \
                    352:        } while (/* CONSTCOND */ 0)
                    353:
                    354:        left = val->size;
                    355:        cp = (char *)val->data;
                    356:
1.5       kristaps  357:        rec->res.rec = *(recno_t *)key->data;
1.1       schwarze  358:
1.5       kristaps  359:        INDEX_BREAD(rec->res.file);
                    360:        INDEX_BREAD(rec->res.cat);
                    361:        INDEX_BREAD(rec->res.title);
                    362:        INDEX_BREAD(rec->res.arch);
                    363:        INDEX_BREAD(rec->res.desc);
1.1       schwarze  364:        return(1);
                    365: }
                    366:
                    367: /*
                    368:  * Search the mandocdb database for the expression "expr".
                    369:  * Filter out by "opts".
                    370:  * Call "res" with the results, which may be zero.
1.5       kristaps  371:  * Return 0 if there was a database error, else return 1.
1.1       schwarze  372:  */
1.5       kristaps  373: int
1.1       schwarze  374: apropos_search(const struct opts *opts, const struct expr *expr,
1.5       kristaps  375:                size_t terms, void *arg,
                    376:                void (*res)(struct res *, size_t, void *))
1.1       schwarze  377: {
1.6     ! kristaps  378:        int              i, rsz, root, leaf, mlen, rc, ch;
        !           379:        uint64_t         mask;
1.1       schwarze  380:        DBT              key, val;
                    381:        DB              *btree, *idx;
                    382:        struct mchars   *mc;
                    383:        char            *buf;
                    384:        recno_t          rec;
1.5       kristaps  385:        struct rec      *rs;
                    386:        struct res      *ress;
                    387:        struct rec       r;
1.6     ! kristaps  388:        struct db_val   *vbuf;
1.1       schwarze  389:
1.5       kristaps  390:        rc      = 0;
1.1       schwarze  391:        root    = -1;
                    392:        leaf    = -1;
                    393:        btree   = NULL;
                    394:        idx     = NULL;
                    395:        mc      = NULL;
                    396:        buf     = NULL;
1.5       kristaps  397:        rs      = NULL;
                    398:        rsz     = 0;
1.1       schwarze  399:
1.5       kristaps  400:        memset(&r, 0, sizeof(struct rec));
1.1       schwarze  401:
                    402:        mc = mchars_alloc();
                    403:
                    404:        if (NULL == (btree = btree_open()))
                    405:                goto out;
                    406:        if (NULL == (idx = index_open()))
                    407:                goto out;
                    408:
                    409:        while (0 == (ch = (*btree->seq)(btree, &key, &val, R_NEXT))) {
1.6     ! kristaps  410:                if (key.size < 2 || sizeof(struct db_val) != val.size)
        !           411:                        break;
1.1       schwarze  412:                if ( ! btree_read(&key, mc, &buf))
                    413:                        break;
                    414:
1.6     ! kristaps  415:                vbuf = val.data;
        !           416:                rec = vbuf->rec;
        !           417:                mask = vbuf->mask;
1.5       kristaps  418:
                    419:                /*
                    420:                 * See if this keyword record matches any of the
                    421:                 * expressions we have stored.
                    422:                 */
                    423:                if ( ! exprmark(expr, buf, mask, NULL))
1.1       schwarze  424:                        continue;
                    425:
                    426:                /*
                    427:                 * O(log n) scan for prior records.  Since a record
                    428:                 * number is unbounded, this has decent performance over
                    429:                 * a complex hash function.
                    430:                 */
                    431:
                    432:                for (leaf = root; leaf >= 0; )
1.5       kristaps  433:                        if (rec > rs[leaf].res.rec &&
                    434:                                        rs[leaf].rhs >= 0)
                    435:                                leaf = rs[leaf].rhs;
                    436:                        else if (rec < rs[leaf].res.rec &&
                    437:                                        rs[leaf].lhs >= 0)
                    438:                                leaf = rs[leaf].lhs;
1.1       schwarze  439:                        else
                    440:                                break;
                    441:
1.5       kristaps  442:                /*
                    443:                 * If we find a record, see if it has already evaluated
                    444:                 * to true.  If it has, great, just keep going.  If not,
                    445:                 * try to evaluate it now and continue anyway.
                    446:                 */
                    447:
                    448:                if (leaf >= 0 && rs[leaf].res.rec == rec) {
                    449:                        if (0 == rs[leaf].matched)
                    450:                                exprexec(expr, buf, mask, &rs[leaf]);
1.1       schwarze  451:                        continue;
1.5       kristaps  452:                }
1.1       schwarze  453:
                    454:                /*
1.5       kristaps  455:                 * We have a new file to examine.
                    456:                 * Extract the manpage's metadata from the index
                    457:                 * database, then begin partial evaluation.
1.1       schwarze  458:                 */
                    459:
                    460:                key.data = &rec;
                    461:                key.size = sizeof(recno_t);
                    462:
                    463:                if (0 != (*idx->get)(idx, &key, &val, 0))
                    464:                        break;
                    465:
1.5       kristaps  466:                r.lhs = r.rhs = -1;
                    467:                if ( ! index_read(&key, &val, mc, &r))
1.1       schwarze  468:                        break;
                    469:
1.5       kristaps  470:                /* XXX: this should be elsewhere, I guess? */
                    471:
                    472:                if (opts->cat && strcasecmp(opts->cat, r.res.cat))
1.1       schwarze  473:                        continue;
1.5       kristaps  474:                if (opts->arch && strcasecmp(opts->arch, r.res.arch))
1.1       schwarze  475:                        continue;
                    476:
1.5       kristaps  477:                rs = mandoc_realloc
                    478:                        (rs, (rsz + 1) * sizeof(struct rec));
1.1       schwarze  479:
1.5       kristaps  480:                memcpy(&rs[rsz], &r, sizeof(struct rec));
                    481:                rs[rsz].matches = mandoc_calloc(terms, sizeof(int));
1.1       schwarze  482:
1.5       kristaps  483:                exprexec(expr, buf, mask, &rs[rsz]);
1.1       schwarze  484:                /* Append to our tree. */
                    485:
                    486:                if (leaf >= 0) {
1.5       kristaps  487:                        if (rec > rs[leaf].res.rec)
                    488:                                rs[leaf].rhs = rsz;
1.1       schwarze  489:                        else
1.5       kristaps  490:                                rs[leaf].lhs = rsz;
1.1       schwarze  491:                } else
1.5       kristaps  492:                        root = rsz;
1.1       schwarze  493:
1.5       kristaps  494:                memset(&r, 0, sizeof(struct rec));
                    495:                rsz++;
1.1       schwarze  496:        }
1.5       kristaps  497:
                    498:        /*
                    499:         * If we haven't encountered any database errors, then construct
                    500:         * an array of results and push them to the caller.
                    501:         */
1.1       schwarze  502:
1.5       kristaps  503:        if (1 == ch) {
                    504:                for (mlen = i = 0; i < rsz; i++)
                    505:                        if (rs[i].matched)
                    506:                                mlen++;
                    507:                ress = mandoc_malloc(mlen * sizeof(struct res));
                    508:                for (mlen = i = 0; i < rsz; i++)
                    509:                        if (rs[i].matched)
                    510:                                memcpy(&ress[mlen++], &rs[i].res,
                    511:                                                sizeof(struct res));
                    512:                (*res)(ress, mlen, arg);
                    513:                free(ress);
                    514:                rc = 1;
                    515:        }
1.1       schwarze  516:
                    517: out:
1.5       kristaps  518:        for (i = 0; i < rsz; i++)
                    519:                recfree(&rs[i]);
1.1       schwarze  520:
1.5       kristaps  521:        recfree(&r);
1.1       schwarze  522:
                    523:        if (mc)
                    524:                mchars_free(mc);
                    525:        if (btree)
                    526:                (*btree->close)(btree);
                    527:        if (idx)
                    528:                (*idx->close)(idx);
                    529:
                    530:        free(buf);
1.5       kristaps  531:        free(rs);
                    532:        return(rc);
                    533: }
                    534:
                    535: static void
                    536: recfree(struct rec *rec)
                    537: {
                    538:
                    539:        free(rec->res.file);
                    540:        free(rec->res.cat);
                    541:        free(rec->res.title);
                    542:        free(rec->res.arch);
                    543:        free(rec->res.desc);
                    544:
                    545:        free(rec->matches);
1.1       schwarze  546: }
                    547:
                    548: struct expr *
1.5       kristaps  549: exprcomp(int argc, char *argv[], size_t *tt)
1.1       schwarze  550: {
1.5       kristaps  551:        int              pos, lvl;
                    552:        struct expr     *e;
                    553:
                    554:        pos = lvl = 0;
                    555:        *tt = 0;
                    556:
                    557:        e = exprexpr(argc, argv, &pos, &lvl, tt);
                    558:
                    559:        if (0 == lvl && pos >= argc)
                    560:                return(e);
                    561:
                    562:        exprfree(e);
                    563:        return(NULL);
                    564: }
                    565:
                    566: /*
                    567:  * Compile an array of tokens into an expression.
                    568:  * An informal expression grammar is defined in apropos(1).
                    569:  * Return NULL if we fail doing so.  All memory will be cleaned up.
                    570:  * Return the root of the expression sequence if alright.
                    571:  */
                    572: static struct expr *
                    573: exprexpr(int argc, char *argv[], int *pos, int *lvl, size_t *tt)
                    574: {
                    575:        struct expr     *e, *first, *next;
                    576:        int              log;
                    577:
                    578:        first = next = NULL;
                    579:
                    580:        for ( ; *pos < argc; (*pos)++) {
                    581:                e = next;
                    582:
                    583:                /*
                    584:                 * Close out a subexpression.
                    585:                 */
                    586:
                    587:                if (NULL != e && 0 == strcmp(")", argv[*pos])) {
                    588:                        if (--(*lvl) < 0)
                    589:                                goto err;
                    590:                        break;
                    591:                }
                    592:
                    593:                /*
                    594:                 * Small note: if we're just starting, don't let "-a"
                    595:                 * and "-o" be considered logical operators: they're
                    596:                 * just tokens unless pairwise joining, in which case we
                    597:                 * record their existence (or assume "OR").
                    598:                 */
                    599:                log = 0;
                    600:
                    601:                if (NULL != e && 0 == strcmp("-a", argv[*pos]))
                    602:                        log = 1;
                    603:                else if (NULL != e && 0 == strcmp("-o", argv[*pos]))
                    604:                        log = 2;
                    605:
                    606:                if (log > 0 && ++(*pos) >= argc)
                    607:                        goto err;
                    608:
                    609:                /*
                    610:                 * Now we parse the term part.  This can begin with
                    611:                 * "-i", in which case the expression is case
                    612:                 * insensitive.
                    613:                 */
                    614:
                    615:                if (0 == strcmp("(", argv[*pos])) {
                    616:                        ++(*pos);
                    617:                        ++(*lvl);
                    618:                        next = mandoc_calloc(1, sizeof(struct expr));
                    619:                        next->cs = 1;
                    620:                        next->subexpr = exprexpr(argc, argv, pos, lvl, tt);
                    621:                        if (NULL == next->subexpr) {
                    622:                                free(next);
                    623:                                next = NULL;
                    624:                        }
                    625:                } else if (0 == strcmp("-i", argv[*pos])) {
                    626:                        if (++(*pos) >= argc)
                    627:                                goto err;
                    628:                        next = exprterm(argv[*pos], 0);
                    629:                } else
                    630:                        next = exprterm(argv[*pos], 1);
                    631:
                    632:                if (NULL == next)
                    633:                        goto err;
                    634:
                    635:                next->and = log == 1;
                    636:                next->index = (int)(*tt)++;
                    637:
                    638:                /* Append to our chain of expressions. */
                    639:
                    640:                if (NULL == first) {
                    641:                        assert(NULL == e);
                    642:                        first = next;
                    643:                } else {
                    644:                        assert(NULL != e);
                    645:                        e->next = next;
                    646:                }
                    647:        }
                    648:
                    649:        return(first);
                    650: err:
                    651:        exprfree(first);
                    652:        return(NULL);
                    653: }
                    654:
                    655: /*
                    656:  * Parse a terminal expression with the grammar as defined in
                    657:  * apropos(1).
                    658:  * Return NULL if we fail the parse.
                    659:  */
                    660: static struct expr *
                    661: exprterm(char *buf, int cs)
                    662: {
                    663:        struct expr      e;
1.1       schwarze  664:        struct expr     *p;
1.3       schwarze  665:        char            *key;
1.5       kristaps  666:        int              i;
                    667:
                    668:        memset(&e, 0, sizeof(struct expr));
1.1       schwarze  669:
1.5       kristaps  670:        e.cs = cs;
1.1       schwarze  671:
1.5       kristaps  672:        /* Choose regex or substring match. */
1.3       schwarze  673:
1.4       kristaps  674:        if (NULL == (e.v = strpbrk(buf, "=~"))) {
1.3       schwarze  675:                e.regex = 0;
1.4       kristaps  676:                e.v = buf;
1.3       schwarze  677:        } else {
                    678:                e.regex = '~' == *e.v;
                    679:                *e.v++ = '\0';
                    680:        }
1.1       schwarze  681:
1.5       kristaps  682:        /* Determine the record types to search for. */
1.3       schwarze  683:
                    684:        e.mask = 0;
1.4       kristaps  685:        if (buf < e.v) {
                    686:                while (NULL != (key = strsep(&buf, ","))) {
1.3       schwarze  687:                        i = 0;
                    688:                        while (types[i].mask &&
1.4       kristaps  689:                                        strcmp(types[i].name, key))
1.3       schwarze  690:                                i++;
                    691:                        e.mask |= types[i].mask;
                    692:                }
                    693:        }
                    694:        if (0 == e.mask)
                    695:                e.mask = TYPE_Nm | TYPE_Nd;
1.1       schwarze  696:
1.5       kristaps  697:        if (e.regex) {
                    698:                i = REG_EXTENDED | REG_NOSUB | cs ? 0 : REG_ICASE;
                    699:                if (regcomp(&e.re, e.v, i))
                    700:                        return(NULL);
                    701:        }
1.1       schwarze  702:
1.3       schwarze  703:        e.v = mandoc_strdup(e.v);
1.1       schwarze  704:
                    705:        p = mandoc_calloc(1, sizeof(struct expr));
                    706:        memcpy(p, &e, sizeof(struct expr));
                    707:        return(p);
                    708: }
                    709:
                    710: void
                    711: exprfree(struct expr *p)
                    712: {
1.5       kristaps  713:        struct expr     *pp;
                    714:
                    715:        while (NULL != p) {
                    716:                if (p->subexpr)
                    717:                        exprfree(p->subexpr);
                    718:                if (p->regex)
                    719:                        regfree(&p->re);
                    720:                free(p->v);
                    721:                pp = p->next;
                    722:                free(p);
                    723:                p = pp;
                    724:        }
                    725: }
1.1       schwarze  726:
1.5       kristaps  727: static int
1.6     ! kristaps  728: exprmark(const struct expr *p, const char *cp,
        !           729:                uint64_t mask, int *ms)
1.5       kristaps  730: {
                    731:
                    732:        for ( ; p; p = p->next) {
                    733:                if (p->subexpr) {
                    734:                        if (exprmark(p->subexpr, cp, mask, ms))
                    735:                                return(1);
                    736:                        continue;
                    737:                } else if ( ! (mask & p->mask))
                    738:                        continue;
1.1       schwarze  739:
1.5       kristaps  740:                if (p->regex) {
                    741:                        if (regexec(&p->re, cp, 0, NULL, 0))
                    742:                                continue;
                    743:                } else if (p->cs) {
                    744:                        if (NULL == strstr(cp, p->v))
                    745:                                continue;
                    746:                } else {
                    747:                        if (NULL == strcasestr(cp, p->v))
                    748:                                continue;
                    749:                }
                    750:
                    751:                if (NULL == ms)
                    752:                        return(1);
                    753:                else
                    754:                        ms[p->index] = 1;
                    755:        }
1.1       schwarze  756:
1.5       kristaps  757:        return(0);
1.1       schwarze  758: }
                    759:
                    760: static int
1.5       kristaps  761: expreval(const struct expr *p, int *ms)
1.1       schwarze  762: {
1.5       kristaps  763:        int              match;
1.1       schwarze  764:
1.5       kristaps  765:        /*
                    766:         * AND has precedence over OR.  Analysis is left-right, though
                    767:         * it doesn't matter because there are no side-effects.
                    768:         * Thus, step through pairwise ANDs and accumulate their Boolean
                    769:         * evaluation.  If we encounter a single true AND collection or
                    770:         * standalone term, the whole expression is true (by definition
                    771:         * of OR).
                    772:         */
                    773:
                    774:        for (match = 0; p && ! match; p = p->next) {
                    775:                /* Evaluate a subexpression, if applicable. */
                    776:                if (p->subexpr && ! ms[p->index])
                    777:                        ms[p->index] = expreval(p->subexpr, ms);
                    778:
                    779:                match = ms[p->index];
                    780:                for ( ; p->next && p->next->and; p = p->next) {
                    781:                        /* Evaluate a subexpression, if applicable. */
                    782:                        if (p->next->subexpr && ! ms[p->next->index])
                    783:                                ms[p->next->index] =
                    784:                                        expreval(p->next->subexpr, ms);
                    785:                        match = match && ms[p->next->index];
                    786:                }
                    787:        }
                    788:
                    789:        return(match);
                    790: }
                    791:
                    792: /*
                    793:  * First, update the array of terms for which this expression evaluates
                    794:  * to true.
                    795:  * Second, logically evaluate all terms over the updated array of truth
                    796:  * values.
                    797:  * If this evaluates to true, mark the expression as satisfied.
                    798:  */
                    799: static void
1.6     ! kristaps  800: exprexec(const struct expr *p, const char *cp,
        !           801:                uint64_t mask, struct rec *r)
1.5       kristaps  802: {
1.1       schwarze  803:
1.5       kristaps  804:        assert(0 == r->matched);
                    805:        exprmark(p, cp, mask, r->matches);
                    806:        r->matched = expreval(p, r->matches);
1.1       schwarze  807: }

CVSweb