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

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

1.65    ! schwarze    1: /*     $OpenBSD: mansearch.c,v 1.64 2019/04/30 18:48:26 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.61      schwarze    4:  * Copyright (c) 2013-2018 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.43      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.43      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.33      schwarze   18:
1.19      schwarze   19: #include <sys/mman.h>
1.33      schwarze   20: #include <sys/types.h>
                     21:
1.1       schwarze   22: #include <assert.h>
1.46      schwarze   23: #include <err.h>
1.39      schwarze   24: #include <errno.h>
1.1       schwarze   25: #include <fcntl.h>
1.41      schwarze   26: #include <glob.h>
1.1       schwarze   27: #include <limits.h>
                     28: #include <regex.h>
                     29: #include <stdio.h>
                     30: #include <stdint.h>
                     31: #include <stddef.h>
                     32: #include <stdlib.h>
                     33: #include <string.h>
                     34: #include <unistd.h>
                     35:
1.14      schwarze   36: #include "mandoc_aux.h"
1.47      schwarze   37: #include "mandoc_ohash.h"
1.43      schwarze   38: #include "manconf.h"
1.1       schwarze   39: #include "mansearch.h"
1.51      schwarze   40: #include "dbm.h"
1.1       schwarze   41:
                     42: struct expr {
1.51      schwarze   43:        /* Used for terms: */
                     44:        struct dbm_match match;   /* Match type and expression. */
                     45:        uint64_t         bits;    /* Type mask. */
                     46:        /* Used for OR and AND groups: */
                     47:        struct expr     *next;    /* Next child in the parent group. */
                     48:        struct expr     *child;   /* First child in this group. */
                     49:        enum { EXPR_TERM, EXPR_OR, EXPR_AND } type;
1.1       schwarze   50: };
                     51:
1.51      schwarze   52: const char *const mansearch_keynames[KEY_MAX] = {
                     53:        "arch", "sec",  "Xr",   "Ar",   "Fa",   "Fl",   "Dv",   "Fn",
                     54:        "Ic",   "Pa",   "Cm",   "Li",   "Em",   "Cd",   "Va",   "Ft",
                     55:        "Tn",   "Er",   "Ev",   "Sy",   "Sh",   "In",   "Ss",   "Ox",
                     56:        "An",   "Mt",   "St",   "Bx",   "At",   "Nx",   "Fx",   "Lk",
                     57:        "Ms",   "Bsx",  "Dx",   "Rs",   "Vt",   "Lb",   "Nm",   "Nd"
1.1       schwarze   58: };
                     59:
1.51      schwarze   60:
                     61: static struct ohash    *manmerge(struct expr *, struct ohash *);
                     62: static struct ohash    *manmerge_term(struct expr *, struct ohash *);
                     63: static struct ohash    *manmerge_or(struct expr *, struct ohash *);
                     64: static struct ohash    *manmerge_and(struct expr *, struct ohash *);
                     65: static char            *buildnames(const struct dbm_page *);
1.53      schwarze   66: static char            *buildoutput(size_t, struct dbm_page *);
                     67: static size_t           lstlen(const char *, size_t);
                     68: static void             lstcat(char *, size_t *, const char *, const char *);
1.51      schwarze   69: static int              lstmatch(const char *, const char *);
1.24      schwarze   70: static struct expr     *exprcomp(const struct mansearch *,
1.51      schwarze   71:                                int, char *[], int *);
                     72: static struct expr     *expr_and(const struct mansearch *,
                     73:                                int, char *[], int *);
                     74: static struct expr     *exprterm(const struct mansearch *,
                     75:                                int, char *[], int *);
1.1       schwarze   76: static void             exprfree(struct expr *);
1.29      schwarze   77: static int              manpage_compare(const void *, const void *);
1.19      schwarze   78:
1.24      schwarze   79:
1.19      schwarze   80: int
1.1       schwarze   81: mansearch(const struct mansearch *search,
1.3       schwarze   82:                const struct manpaths *paths,
                     83:                int argc, char *argv[],
1.1       schwarze   84:                struct manpage **res, size_t *sz)
                     85: {
                     86:        char             buf[PATH_MAX];
1.51      schwarze   87:        struct dbm_res  *rp;
                     88:        struct expr     *e;
                     89:        struct dbm_page *page;
1.1       schwarze   90:        struct manpage  *mpage;
1.51      schwarze   91:        struct ohash    *htab;
                     92:        size_t           cur, i, maxres, outkey;
                     93:        unsigned int     slot;
                     94:        int              argi, chdir_status, getcwd_status, im;
1.44      schwarze   95:
1.51      schwarze   96:        argi = 0;
                     97:        if ((e = exprcomp(search, argc, argv, &argi)) == NULL) {
1.44      schwarze   98:                *sz = 0;
1.45      schwarze   99:                return 0;
1.44      schwarze  100:        }
1.1       schwarze  101:
1.44      schwarze  102:        cur = maxres = 0;
1.57      schwarze  103:        if (res != NULL)
                    104:                *res = NULL;
1.1       schwarze  105:
1.51      schwarze  106:        outkey = KEY_Nd;
                    107:        if (search->outkey != NULL)
                    108:                for (im = 0; im < KEY_MAX; im++)
1.33      schwarze  109:                        if (0 == strcasecmp(search->outkey,
1.51      schwarze  110:                            mansearch_keynames[im])) {
                    111:                                outkey = im;
1.3       schwarze  112:                                break;
                    113:                        }
                    114:
1.1       schwarze  115:        /*
1.44      schwarze  116:         * Remember the original working directory, if possible.
                    117:         * This will be needed if the second or a later directory
                    118:         * is given as a relative path.
                    119:         * Do not error out if the current directory is not
                    120:         * searchable: Maybe it won't be needed after all.
1.1       schwarze  121:         */
                    122:
1.44      schwarze  123:        if (getcwd(buf, PATH_MAX) == NULL) {
                    124:                getcwd_status = 0;
                    125:                (void)strlcpy(buf, strerror(errno), sizeof(buf));
                    126:        } else
                    127:                getcwd_status = 1;
1.1       schwarze  128:
                    129:        /*
                    130:         * Loop over the directories (containing databases) for us to
                    131:         * search.
                    132:         * Don't let missing/bad databases/directories phase us.
                    133:         * In each, try to open the resident database and, if it opens,
                    134:         * scan it for our match expression.
                    135:         */
                    136:
1.44      schwarze  137:        chdir_status = 0;
1.1       schwarze  138:        for (i = 0; i < paths->sz; i++) {
1.44      schwarze  139:                if (chdir_status && paths->paths[i][0] != '/') {
                    140:                        if ( ! getcwd_status) {
1.46      schwarze  141:                                warnx("%s: getcwd: %s", paths->paths[i], buf);
1.44      schwarze  142:                                continue;
                    143:                        } else if (chdir(buf) == -1) {
1.49      schwarze  144:                                warn("%s", buf);
1.44      schwarze  145:                                continue;
                    146:                        }
                    147:                }
                    148:                if (chdir(paths->paths[i]) == -1) {
1.49      schwarze  149:                        warn("%s", paths->paths[i]);
1.1       schwarze  150:                        continue;
1.24      schwarze  151:                }
1.44      schwarze  152:                chdir_status = 1;
1.1       schwarze  153:
1.51      schwarze  154:                if (dbm_open(MANDOC_DB) == -1) {
1.56      schwarze  155:                        if (errno != ENOENT)
                    156:                                warn("%s/%s", paths->paths[i], MANDOC_DB);
1.1       schwarze  157:                        continue;
                    158:                }
                    159:
1.51      schwarze  160:                if ((htab = manmerge(e, NULL)) == NULL) {
                    161:                        dbm_close();
                    162:                        continue;
1.1       schwarze  163:                }
                    164:
1.51      schwarze  165:                for (rp = ohash_first(htab, &slot); rp != NULL;
                    166:                    rp = ohash_next(htab, &slot)) {
                    167:                        page = dbm_page_get(rp->page);
1.1       schwarze  168:
1.51      schwarze  169:                        if (lstmatch(search->sec, page->sect) == 0 ||
1.59      schwarze  170:                            lstmatch(search->arch, page->arch) == 0 ||
                    171:                            (search->argmode == ARG_NAME &&
                    172:                             rp->bits <= (int32_t)(NAME_SYN & NAME_MASK)))
1.1       schwarze  173:                                continue;
                    174:
1.57      schwarze  175:                        if (res == NULL) {
                    176:                                cur = 1;
                    177:                                break;
                    178:                        }
1.1       schwarze  179:                        if (cur + 1 > maxres) {
                    180:                                maxres += 1024;
1.26      schwarze  181:                                *res = mandoc_reallocarray(*res,
1.51      schwarze  182:                                    maxres, sizeof(**res));
1.1       schwarze  183:                        }
                    184:                        mpage = *res + cur;
1.51      schwarze  185:                        mandoc_asprintf(&mpage->file, "%s/%s",
                    186:                            paths->paths[i], page->file + 1);
1.60      schwarze  187:                        if (access(chdir_status ? page->file + 1 :
                    188:                            mpage->file, R_OK) == -1) {
                    189:                                warn("%s", mpage->file);
                    190:                                warnx("outdated mandoc.db contains "
1.65    ! schwarze  191:                                    "bogus %s entry, run makewhatis %s",
1.60      schwarze  192:                                    page->file + 1, paths->paths[i]);
                    193:                                free(mpage->file);
                    194:                                free(rp);
                    195:                                continue;
                    196:                        }
1.51      schwarze  197:                        mpage->names = buildnames(page);
1.53      schwarze  198:                        mpage->output = buildoutput(outkey, page);
1.64      schwarze  199:                        mpage->bits = search->firstmatch ? rp->bits : 0;
1.34      schwarze  200:                        mpage->ipath = i;
1.51      schwarze  201:                        mpage->sec = *page->sect - '0';
                    202:                        if (mpage->sec < 0 || mpage->sec > 9)
                    203:                                mpage->sec = 10;
                    204:                        mpage->form = *page->file;
                    205:                        free(rp);
                    206:                        cur++;
                    207:                }
                    208:                ohash_delete(htab);
                    209:                free(htab);
                    210:                dbm_close();
1.36      schwarze  211:
                    212:                /*
                    213:                 * In man(1) mode, prefer matches in earlier trees
                    214:                 * over matches in later trees.
                    215:                 */
                    216:
                    217:                if (cur && search->firstmatch)
                    218:                        break;
1.1       schwarze  219:        }
1.57      schwarze  220:        if (res != NULL)
                    221:                qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.44      schwarze  222:        if (chdir_status && getcwd_status && chdir(buf) == -1)
1.49      schwarze  223:                warn("%s", buf);
1.1       schwarze  224:        exprfree(e);
                    225:        *sz = cur;
1.57      schwarze  226:        return res != NULL || cur;
1.2       schwarze  227: }
                    228:
1.51      schwarze  229: /*
                    230:  * Merge the results for the expression tree rooted at e
                    231:  * into the the result list htab.
                    232:  */
                    233: static struct ohash *
                    234: manmerge(struct expr *e, struct ohash *htab)
1.33      schwarze  235: {
1.51      schwarze  236:        switch (e->type) {
                    237:        case EXPR_TERM:
                    238:                return manmerge_term(e, htab);
                    239:        case EXPR_OR:
                    240:                return manmerge_or(e->child, htab);
                    241:        case EXPR_AND:
                    242:                return manmerge_and(e->child, htab);
                    243:        default:
                    244:                abort();
1.33      schwarze  245:        }
                    246: }
                    247:
1.51      schwarze  248: static struct ohash *
                    249: manmerge_term(struct expr *e, struct ohash *htab)
1.29      schwarze  250: {
1.51      schwarze  251:        struct dbm_res   res, *rp;
                    252:        uint64_t         ib;
                    253:        unsigned int     slot;
                    254:        int              im;
                    255:
                    256:        if (htab == NULL) {
                    257:                htab = mandoc_malloc(sizeof(*htab));
                    258:                mandoc_ohash_init(htab, 4, offsetof(struct dbm_res, page));
                    259:        }
                    260:
                    261:        for (im = 0, ib = 1; im < KEY_MAX; im++, ib <<= 1) {
                    262:                if ((e->bits & ib) == 0)
                    263:                        continue;
                    264:
                    265:                switch (ib) {
                    266:                case TYPE_arch:
                    267:                        dbm_page_byarch(&e->match);
                    268:                        break;
                    269:                case TYPE_sec:
                    270:                        dbm_page_bysect(&e->match);
                    271:                        break;
                    272:                case TYPE_Nm:
                    273:                        dbm_page_byname(&e->match);
                    274:                        break;
                    275:                case TYPE_Nd:
                    276:                        dbm_page_bydesc(&e->match);
                    277:                        break;
                    278:                default:
                    279:                        dbm_page_bymacro(im - 2, &e->match);
                    280:                        break;
                    281:                }
                    282:
                    283:                /*
                    284:                 * When hashing for deduplication, use the unique
                    285:                 * page ID itself instead of a hash function;
                    286:                 * that is quite efficient.
                    287:                 */
1.29      schwarze  288:
1.51      schwarze  289:                for (;;) {
                    290:                        res = dbm_page_next();
                    291:                        if (res.page == -1)
                    292:                                break;
                    293:                        slot = ohash_lookup_memory(htab,
                    294:                            (char *)&res, sizeof(res.page), res.page);
1.64      schwarze  295:                        if ((rp = ohash_find(htab, slot)) != NULL) {
                    296:                                rp->bits |= res.bits;
1.51      schwarze  297:                                continue;
1.64      schwarze  298:                        }
1.51      schwarze  299:                        rp = mandoc_malloc(sizeof(*rp));
                    300:                        *rp = res;
                    301:                        ohash_insert(htab, slot, rp);
                    302:                }
                    303:        }
                    304:        return htab;
1.29      schwarze  305: }
                    306:
1.51      schwarze  307: static struct ohash *
                    308: manmerge_or(struct expr *e, struct ohash *htab)
                    309: {
                    310:        while (e != NULL) {
                    311:                htab = manmerge(e, htab);
                    312:                e = e->next;
                    313:        }
                    314:        return htab;
                    315: }
1.13      schwarze  316:
1.51      schwarze  317: static struct ohash *
                    318: manmerge_and(struct expr *e, struct ohash *htab)
                    319: {
                    320:        struct ohash    *hand, *h1, *h2;
                    321:        struct dbm_res  *res;
                    322:        unsigned int     slot1, slot2;
1.13      schwarze  323:
1.51      schwarze  324:        /* Evaluate the first term of the AND clause. */
1.29      schwarze  325:
1.51      schwarze  326:        hand = manmerge(e, NULL);
1.29      schwarze  327:
1.51      schwarze  328:        while ((e = e->next) != NULL) {
1.13      schwarze  329:
1.51      schwarze  330:                /* Evaluate the next term and prepare for ANDing. */
1.13      schwarze  331:
1.51      schwarze  332:                h2 = manmerge(e, NULL);
                    333:                if (ohash_entries(h2) < ohash_entries(hand)) {
                    334:                        h1 = h2;
                    335:                        h2 = hand;
                    336:                } else
                    337:                        h1 = hand;
                    338:                hand = mandoc_malloc(sizeof(*hand));
                    339:                mandoc_ohash_init(hand, 4, offsetof(struct dbm_res, page));
1.13      schwarze  340:
1.51      schwarze  341:                /* Keep all pages that are in both result sets. */
1.13      schwarze  342:
1.51      schwarze  343:                for (res = ohash_first(h1, &slot1); res != NULL;
                    344:                    res = ohash_next(h1, &slot1)) {
                    345:                        if (ohash_find(h2, ohash_lookup_memory(h2,
                    346:                            (char *)res, sizeof(res->page),
                    347:                            res->page)) == NULL)
                    348:                                free(res);
                    349:                        else
                    350:                                ohash_insert(hand, ohash_lookup_memory(hand,
                    351:                                    (char *)res, sizeof(res->page),
                    352:                                    res->page), res);
1.13      schwarze  353:                }
                    354:
1.51      schwarze  355:                /* Discard the merged results. */
                    356:
                    357:                for (res = ohash_first(h2, &slot2); res != NULL;
                    358:                    res = ohash_next(h2, &slot2))
                    359:                        free(res);
                    360:                ohash_delete(h2);
                    361:                free(h2);
                    362:                ohash_delete(h1);
                    363:                free(h1);
                    364:        }
1.13      schwarze  365:
1.51      schwarze  366:        /* Merge the result of the AND into htab. */
1.8       schwarze  367:
1.51      schwarze  368:        if (htab == NULL)
                    369:                return hand;
1.8       schwarze  370:
1.51      schwarze  371:        for (res = ohash_first(hand, &slot1); res != NULL;
                    372:            res = ohash_next(hand, &slot1)) {
                    373:                slot2 = ohash_lookup_memory(htab,
                    374:                    (char *)res, sizeof(res->page), res->page);
                    375:                if (ohash_find(htab, slot2) == NULL)
                    376:                        ohash_insert(htab, slot2, res);
                    377:                else
                    378:                        free(res);
                    379:        }
                    380:
                    381:        /* Discard the merged result. */
1.8       schwarze  382:
1.51      schwarze  383:        ohash_delete(hand);
                    384:        free(hand);
                    385:        return htab;
                    386: }
1.41      schwarze  387:
1.51      schwarze  388: void
                    389: mansearch_free(struct manpage *res, size_t sz)
                    390: {
                    391:        size_t   i;
1.41      schwarze  392:
1.51      schwarze  393:        for (i = 0; i < sz; i++) {
                    394:                free(res[i].file);
                    395:                free(res[i].names);
                    396:                free(res[i].output);
1.13      schwarze  397:        }
1.51      schwarze  398:        free(res);
                    399: }
                    400:
                    401: static int
                    402: manpage_compare(const void *vp1, const void *vp2)
                    403: {
                    404:        const struct manpage    *mp1, *mp2;
1.58      schwarze  405:        const char              *cp1, *cp2;
                    406:        size_t                   sz1, sz2;
1.51      schwarze  407:        int                      diff;
                    408:
                    409:        mp1 = vp1;
                    410:        mp2 = vp2;
1.64      schwarze  411:        if ((diff = mp2->bits - mp1->bits) ||
                    412:            (diff = mp1->sec - mp2->sec))
1.58      schwarze  413:                return diff;
                    414:
                    415:        /* Fall back to alphabetic ordering of names. */
                    416:        sz1 = strcspn(mp1->names, "(");
                    417:        sz2 = strcspn(mp2->names, "(");
                    418:        if (sz1 < sz2)
                    419:                sz1 = sz2;
                    420:        if ((diff = strncasecmp(mp1->names, mp2->names, sz1)))
                    421:                return diff;
                    422:
                    423:        /* For identical names and sections, prefer arch-dependent. */
                    424:        cp1 = strchr(mp1->names + sz1, '/');
                    425:        cp2 = strchr(mp2->names + sz2, '/');
                    426:        return cp1 != NULL && cp2 != NULL ? strcasecmp(cp1, cp2) :
                    427:            cp1 != NULL ? -1 : cp2 != NULL ? 1 : 0;
1.3       schwarze  428: }
                    429:
                    430: static char *
1.51      schwarze  431: buildnames(const struct dbm_page *page)
1.3       schwarze  432: {
1.51      schwarze  433:        char    *buf;
                    434:        size_t   i, sz;
1.3       schwarze  435:
1.53      schwarze  436:        sz = lstlen(page->name, 2) + 1 + lstlen(page->sect, 2) +
                    437:            (page->arch == NULL ? 0 : 1 + lstlen(page->arch, 2)) + 2;
1.51      schwarze  438:        buf = mandoc_malloc(sz);
                    439:        i = 0;
1.53      schwarze  440:        lstcat(buf, &i, page->name, ", ");
1.51      schwarze  441:        buf[i++] = '(';
1.53      schwarze  442:        lstcat(buf, &i, page->sect, ", ");
1.51      schwarze  443:        if (page->arch != NULL) {
                    444:                buf[i++] = '/';
1.53      schwarze  445:                lstcat(buf, &i, page->arch, ", ");
1.51      schwarze  446:        }
                    447:        buf[i++] = ')';
                    448:        buf[i++] = '\0';
                    449:        assert(i == sz);
                    450:        return buf;
1.1       schwarze  451: }
                    452:
                    453: /*
1.51      schwarze  454:  * Count the buffer space needed to print the NUL-terminated
1.53      schwarze  455:  * list of NUL-terminated strings, when printing sep separator
1.51      schwarze  456:  * characters between strings.
1.1       schwarze  457:  */
1.51      schwarze  458: static size_t
1.53      schwarze  459: lstlen(const char *cp, size_t sep)
1.1       schwarze  460: {
1.51      schwarze  461:        size_t   sz;
1.1       schwarze  462:
1.59      schwarze  463:        for (sz = 0; *cp != '\0'; cp++) {
                    464:
                    465:                /* Skip names appearing only in the SYNOPSIS. */
                    466:                if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
                    467:                        while (*cp != '\0')
                    468:                                cp++;
                    469:                        continue;
                    470:                }
                    471:
                    472:                /* Skip name class markers. */
                    473:                if (*cp < ' ')
                    474:                        cp++;
                    475:
                    476:                /* Print a separator before each but the first string. */
                    477:                if (sz)
                    478:                        sz += sep;
                    479:
                    480:                /* Copy one string. */
                    481:                while (*cp != '\0') {
                    482:                        sz++;
                    483:                        cp++;
                    484:                }
1.51      schwarze  485:        }
                    486:        return sz;
1.1       schwarze  487: }
                    488:
                    489: /*
1.51      schwarze  490:  * Print the NUL-terminated list of NUL-terminated strings
1.53      schwarze  491:  * into the buffer, seperating strings with sep.
1.1       schwarze  492:  */
                    493: static void
1.53      schwarze  494: lstcat(char *buf, size_t *i, const char *cp, const char *sep)
1.1       schwarze  495: {
1.59      schwarze  496:        const char      *s;
                    497:        size_t           i_start;
1.53      schwarze  498:
1.59      schwarze  499:        for (i_start = *i; *cp != '\0'; cp++) {
                    500:
                    501:                /* Skip names appearing only in the SYNOPSIS. */
                    502:                if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
                    503:                        while (*cp != '\0')
                    504:                                cp++;
                    505:                        continue;
                    506:                }
                    507:
                    508:                /* Skip name class markers. */
                    509:                if (*cp < ' ')
                    510:                        cp++;
                    511:
                    512:                /* Print a separator before each but the first string. */
                    513:                if (*i > i_start) {
1.53      schwarze  514:                        s = sep;
                    515:                        while (*s != '\0')
                    516:                                buf[(*i)++] = *s++;
1.59      schwarze  517:                }
                    518:
                    519:                /* Copy one string. */
                    520:                while (*cp != '\0')
                    521:                        buf[(*i)++] = *cp++;
1.51      schwarze  522:        }
1.59      schwarze  523:
1.1       schwarze  524: }
                    525:
1.51      schwarze  526: /*
                    527:  * Return 1 if the string *want occurs in any of the strings
                    528:  * in the NUL-terminated string list *have, or 0 otherwise.
                    529:  * If either argument is NULL or empty, assume no filtering
                    530:  * is desired and return 1.
                    531:  */
                    532: static int
                    533: lstmatch(const char *want, const char *have)
1.4       schwarze  534: {
1.51      schwarze  535:         if (want == NULL || have == NULL || *have == '\0')
                    536:                 return 1;
                    537:         while (*have != '\0') {
                    538:                 if (strcasestr(have, want) != NULL)
                    539:                         return 1;
                    540:                 have = strchr(have, '\0') + 1;
                    541:         }
                    542:         return 0;
1.4       schwarze  543: }
                    544:
1.1       schwarze  545: /*
1.53      schwarze  546:  * Build a list of values taken by the macro im in the manual page.
1.1       schwarze  547:  */
                    548: static char *
1.53      schwarze  549: buildoutput(size_t im, struct dbm_page *page)
1.1       schwarze  550: {
1.53      schwarze  551:        const char      *oldoutput, *sep, *input;
1.51      schwarze  552:        char            *output, *newoutput, *value;
1.53      schwarze  553:        size_t           sz, i;
                    554:
                    555:        switch (im) {
                    556:        case KEY_Nd:
                    557:                return mandoc_strdup(page->desc);
                    558:        case KEY_Nm:
                    559:                input = page->name;
                    560:                break;
                    561:        case KEY_sec:
                    562:                input = page->sect;
                    563:                break;
                    564:        case KEY_arch:
                    565:                input = page->arch;
                    566:                if (input == NULL)
                    567:                        input = "all\0";
                    568:                break;
                    569:        default:
                    570:                input = NULL;
                    571:                break;
                    572:        }
                    573:
                    574:        if (input != NULL) {
                    575:                sz = lstlen(input, 3) + 1;
                    576:                output = mandoc_malloc(sz);
                    577:                i = 0;
                    578:                lstcat(output, &i, input, " # ");
1.54      schwarze  579:                output[i++] = '\0';
                    580:                assert(i == sz);
1.53      schwarze  581:                return output;
                    582:        }
1.51      schwarze  583:
                    584:        output = NULL;
1.53      schwarze  585:        dbm_macro_bypage(im - 2, page->addr);
1.51      schwarze  586:        while ((value = dbm_macro_next()) != NULL) {
                    587:                if (output == NULL) {
                    588:                        oldoutput = "";
                    589:                        sep = "";
                    590:                } else {
                    591:                        oldoutput = output;
                    592:                        sep = " # ";
                    593:                }
                    594:                mandoc_asprintf(&newoutput, "%s%s%s", oldoutput, sep, value);
                    595:                free(output);
                    596:                output = newoutput;
1.1       schwarze  597:        }
1.51      schwarze  598:        return output;
1.1       schwarze  599: }
                    600:
                    601: /*
                    602:  * Compile a set of string tokens into an expression.
                    603:  * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
                    604:  * "(", "foo=bar", etc.).
                    605:  */
                    606: static struct expr *
1.51      schwarze  607: exprcomp(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       schwarze  608: {
1.51      schwarze  609:        struct expr     *parent, *child;
                    610:        int              needterm, nested;
                    611:
                    612:        if ((nested = *argi) == argc)
                    613:                return NULL;
                    614:        needterm = 1;
                    615:        parent = child = NULL;
                    616:        while (*argi < argc) {
                    617:                if (strcmp(")", argv[*argi]) == 0) {
                    618:                        if (needterm)
                    619:                                warnx("missing term "
                    620:                                    "before closing parenthesis");
                    621:                        needterm = 0;
                    622:                        if (nested)
                    623:                                break;
                    624:                        warnx("ignoring unmatched right parenthesis");
                    625:                        ++*argi;
1.4       schwarze  626:                        continue;
1.51      schwarze  627:                }
                    628:                if (strcmp("-o", argv[*argi]) == 0) {
                    629:                        if (needterm) {
                    630:                                if (*argi > 0)
                    631:                                        warnx("ignoring -o after %s",
                    632:                                            argv[*argi - 1]);
                    633:                                else
                    634:                                        warnx("ignoring initial -o");
                    635:                        }
                    636:                        needterm = 1;
                    637:                        ++*argi;
1.4       schwarze  638:                        continue;
1.51      schwarze  639:                }
                    640:                needterm = 0;
                    641:                if (child == NULL) {
                    642:                        child = expr_and(search, argc, argv, argi);
1.4       schwarze  643:                        continue;
1.1       schwarze  644:                }
1.51      schwarze  645:                if (parent == NULL) {
                    646:                        parent = mandoc_calloc(1, sizeof(*parent));
                    647:                        parent->type = EXPR_OR;
                    648:                        parent->next = NULL;
                    649:                        parent->child = child;
                    650:                }
                    651:                child->next = expr_and(search, argc, argv, argi);
                    652:                child = child->next;
                    653:        }
                    654:        if (needterm && *argi)
                    655:                warnx("ignoring trailing %s", argv[*argi - 1]);
                    656:        return parent == NULL ? child : parent;
                    657: }
1.17      schwarze  658:
1.51      schwarze  659: static struct expr *
                    660: expr_and(const struct mansearch *search, int argc, char *argv[], int *argi)
                    661: {
                    662:        struct expr     *parent, *child;
                    663:        int              needterm;
1.17      schwarze  664:
1.51      schwarze  665:        needterm = 1;
                    666:        parent = child = NULL;
                    667:        while (*argi < argc) {
                    668:                if (strcmp(")", argv[*argi]) == 0) {
                    669:                        if (needterm)
                    670:                                warnx("missing term "
                    671:                                    "before closing parenthesis");
                    672:                        needterm = 0;
                    673:                        break;
                    674:                }
                    675:                if (strcmp("-o", argv[*argi]) == 0)
                    676:                        break;
                    677:                if (strcmp("-a", argv[*argi]) == 0) {
                    678:                        if (needterm) {
                    679:                                if (*argi > 0)
                    680:                                        warnx("ignoring -a after %s",
                    681:                                            argv[*argi - 1]);
                    682:                                else
                    683:                                        warnx("ignoring initial -a");
1.18      schwarze  684:                        }
1.51      schwarze  685:                        needterm = 1;
                    686:                        ++*argi;
                    687:                        continue;
1.18      schwarze  688:                }
1.51      schwarze  689:                if (needterm == 0)
                    690:                        break;
                    691:                if (child == NULL) {
                    692:                        child = exprterm(search, argc, argv, argi);
                    693:                        if (child != NULL)
                    694:                                needterm = 0;
                    695:                        continue;
                    696:                }
                    697:                needterm = 0;
                    698:                if (parent == NULL) {
                    699:                        parent = mandoc_calloc(1, sizeof(*parent));
                    700:                        parent->type = EXPR_AND;
                    701:                        parent->next = NULL;
                    702:                        parent->child = child;
                    703:                }
                    704:                child->next = exprterm(search, argc, argv, argi);
                    705:                if (child->next != NULL) {
                    706:                        child = child->next;
                    707:                        needterm = 0;
                    708:                }
                    709:        }
                    710:        if (needterm && *argi)
                    711:                warnx("ignoring trailing %s", argv[*argi - 1]);
                    712:        return parent == NULL ? child : parent;
1.6       schwarze  713: }
                    714:
                    715: static struct expr *
1.51      schwarze  716: exprterm(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       schwarze  717: {
1.6       schwarze  718:        char             errbuf[BUFSIZ];
1.1       schwarze  719:        struct expr     *e;
1.28      schwarze  720:        char            *key, *val;
1.11      schwarze  721:        uint64_t         iterbit;
1.51      schwarze  722:        int              cs, i, irc;
1.1       schwarze  723:
1.51      schwarze  724:        if (strcmp("(", argv[*argi]) == 0) {
                    725:                ++*argi;
                    726:                e = exprcomp(search, argc, argv, argi);
                    727:                if (*argi < argc) {
                    728:                        assert(strcmp(")", argv[*argi]) == 0);
                    729:                        ++*argi;
                    730:                } else
                    731:                        warnx("unclosed parenthesis");
                    732:                return e;
                    733:        }
1.1       schwarze  734:
1.55      schwarze  735:        if (strcmp("-i", argv[*argi]) == 0 && *argi + 1 < argc) {
                    736:                cs = 0;
                    737:                ++*argi;
                    738:        } else
                    739:                cs = 1;
                    740:
1.51      schwarze  741:        e = mandoc_calloc(1, sizeof(*e));
                    742:        e->type = EXPR_TERM;
                    743:        e->bits = 0;
                    744:        e->next = NULL;
                    745:        e->child = NULL;
1.1       schwarze  746:
1.33      schwarze  747:        if (search->argmode == ARG_NAME) {
                    748:                e->bits = TYPE_Nm;
1.51      schwarze  749:                e->match.type = DBM_EXACT;
                    750:                e->match.str = argv[(*argi)++];
1.45      schwarze  751:                return e;
1.1       schwarze  752:        }
                    753:
                    754:        /*
1.33      schwarze  755:         * Separate macro keys from search string.
1.51      schwarze  756:         * If needed, request regular expression handling.
1.1       schwarze  757:         */
                    758:
1.33      schwarze  759:        if (search->argmode == ARG_WORD) {
                    760:                e->bits = TYPE_Nm;
1.51      schwarze  761:                e->match.type = DBM_REGEX;
                    762:                mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", argv[*argi]);
1.33      schwarze  763:                cs = 0;
1.51      schwarze  764:        } else if ((val = strpbrk(argv[*argi], "=~")) == NULL) {
1.33      schwarze  765:                e->bits = TYPE_Nm | TYPE_Nd;
1.61      schwarze  766:                e->match.type = DBM_REGEX;
                    767:                val = argv[*argi];
                    768:                cs = 0;
1.28      schwarze  769:        } else {
1.51      schwarze  770:                if (val == argv[*argi])
1.33      schwarze  771:                        e->bits = TYPE_Nm | TYPE_Nd;
1.51      schwarze  772:                if (*val == '=') {
                    773:                        e->match.type = DBM_SUB;
                    774:                        e->match.str = val + 1;
                    775:                } else
                    776:                        e->match.type = DBM_REGEX;
1.28      schwarze  777:                *val++ = '\0';
1.51      schwarze  778:                if (strstr(argv[*argi], "arch") != NULL)
1.12      schwarze  779:                        cs = 0;
1.28      schwarze  780:        }
                    781:
                    782:        /* Compile regular expressions. */
                    783:
1.51      schwarze  784:        if (e->match.type == DBM_REGEX) {
                    785:                e->match.re = mandoc_malloc(sizeof(*e->match.re));
                    786:                irc = regcomp(e->match.re, val,
1.28      schwarze  787:                    REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.51      schwarze  788:                if (irc) {
                    789:                        regerror(irc, e->match.re, errbuf, sizeof(errbuf));
                    790:                        warnx("regcomp /%s/: %s", val, errbuf);
                    791:                }
1.33      schwarze  792:                if (search->argmode == ARG_WORD)
1.28      schwarze  793:                        free(val);
                    794:                if (irc) {
1.51      schwarze  795:                        free(e->match.re);
1.1       schwarze  796:                        free(e);
1.51      schwarze  797:                        ++*argi;
1.45      schwarze  798:                        return NULL;
1.1       schwarze  799:                }
1.28      schwarze  800:        }
                    801:
1.51      schwarze  802:        if (e->bits) {
                    803:                ++*argi;
1.45      schwarze  804:                return e;
1.51      schwarze  805:        }
1.1       schwarze  806:
                    807:        /*
                    808:         * Parse out all possible fields.
                    809:         * If the field doesn't resolve, bail.
                    810:         */
                    811:
1.51      schwarze  812:        while (NULL != (key = strsep(&argv[*argi], ","))) {
1.1       schwarze  813:                if ('\0' == *key)
                    814:                        continue;
1.51      schwarze  815:                for (i = 0, iterbit = 1; i < KEY_MAX; i++, iterbit <<= 1) {
                    816:                        if (0 == strcasecmp(key, mansearch_keynames[i])) {
1.11      schwarze  817:                                e->bits |= iterbit;
                    818:                                break;
                    819:                        }
                    820:                }
1.51      schwarze  821:                if (i == KEY_MAX) {
                    822:                        if (strcasecmp(key, "any"))
                    823:                                warnx("treating unknown key "
                    824:                                    "\"%s\" as \"any\"", key);
1.11      schwarze  825:                        e->bits |= ~0ULL;
1.1       schwarze  826:                }
                    827:        }
                    828:
1.51      schwarze  829:        ++*argi;
1.45      schwarze  830:        return e;
1.1       schwarze  831: }
                    832:
                    833: static void
1.51      schwarze  834: exprfree(struct expr *e)
1.1       schwarze  835: {
1.51      schwarze  836:        if (e->next != NULL)
                    837:                exprfree(e->next);
                    838:        if (e->child != NULL)
                    839:                exprfree(e->child);
                    840:        free(e);
1.1       schwarze  841: }