[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.63

1.63    ! schwarze    1: /*     $OpenBSD: mansearch.c,v 1.62 2018/11/22 12:01:42 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 "
                    191:                                    "bogus %s entry, run makewhatis %s",
                    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.34      schwarze  199:                        mpage->ipath = i;
1.51      schwarze  200:                        mpage->sec = *page->sect - '0';
                    201:                        if (mpage->sec < 0 || mpage->sec > 9)
                    202:                                mpage->sec = 10;
                    203:                        mpage->form = *page->file;
                    204:                        free(rp);
                    205:                        cur++;
                    206:                }
                    207:                ohash_delete(htab);
                    208:                free(htab);
                    209:                dbm_close();
1.36      schwarze  210:
                    211:                /*
                    212:                 * In man(1) mode, prefer matches in earlier trees
                    213:                 * over matches in later trees.
                    214:                 */
                    215:
                    216:                if (cur && search->firstmatch)
                    217:                        break;
1.1       schwarze  218:        }
1.57      schwarze  219:        if (res != NULL)
                    220:                qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.44      schwarze  221:        if (chdir_status && getcwd_status && chdir(buf) == -1)
1.49      schwarze  222:                warn("%s", buf);
1.1       schwarze  223:        exprfree(e);
                    224:        *sz = cur;
1.57      schwarze  225:        return res != NULL || cur;
1.2       schwarze  226: }
                    227:
1.51      schwarze  228: /*
                    229:  * Merge the results for the expression tree rooted at e
                    230:  * into the the result list htab.
                    231:  */
                    232: static struct ohash *
                    233: manmerge(struct expr *e, struct ohash *htab)
1.33      schwarze  234: {
1.51      schwarze  235:        switch (e->type) {
                    236:        case EXPR_TERM:
                    237:                return manmerge_term(e, htab);
                    238:        case EXPR_OR:
                    239:                return manmerge_or(e->child, htab);
                    240:        case EXPR_AND:
                    241:                return manmerge_and(e->child, htab);
                    242:        default:
                    243:                abort();
1.33      schwarze  244:        }
                    245: }
                    246:
1.51      schwarze  247: static struct ohash *
                    248: manmerge_term(struct expr *e, struct ohash *htab)
1.29      schwarze  249: {
1.51      schwarze  250:        struct dbm_res   res, *rp;
                    251:        uint64_t         ib;
                    252:        unsigned int     slot;
                    253:        int              im;
                    254:
                    255:        if (htab == NULL) {
                    256:                htab = mandoc_malloc(sizeof(*htab));
                    257:                mandoc_ohash_init(htab, 4, offsetof(struct dbm_res, page));
                    258:        }
                    259:
                    260:        for (im = 0, ib = 1; im < KEY_MAX; im++, ib <<= 1) {
                    261:                if ((e->bits & ib) == 0)
                    262:                        continue;
                    263:
                    264:                switch (ib) {
                    265:                case TYPE_arch:
                    266:                        dbm_page_byarch(&e->match);
                    267:                        break;
                    268:                case TYPE_sec:
                    269:                        dbm_page_bysect(&e->match);
                    270:                        break;
                    271:                case TYPE_Nm:
                    272:                        dbm_page_byname(&e->match);
                    273:                        break;
                    274:                case TYPE_Nd:
                    275:                        dbm_page_bydesc(&e->match);
                    276:                        break;
                    277:                default:
                    278:                        dbm_page_bymacro(im - 2, &e->match);
                    279:                        break;
                    280:                }
                    281:
                    282:                /*
                    283:                 * When hashing for deduplication, use the unique
                    284:                 * page ID itself instead of a hash function;
                    285:                 * that is quite efficient.
                    286:                 */
1.29      schwarze  287:
1.51      schwarze  288:                for (;;) {
                    289:                        res = dbm_page_next();
                    290:                        if (res.page == -1)
                    291:                                break;
                    292:                        slot = ohash_lookup_memory(htab,
                    293:                            (char *)&res, sizeof(res.page), res.page);
1.62      schwarze  294:                        if ((rp = ohash_find(htab, slot)) != NULL)
1.51      schwarze  295:                                continue;
                    296:                        rp = mandoc_malloc(sizeof(*rp));
                    297:                        *rp = res;
                    298:                        ohash_insert(htab, slot, rp);
                    299:                }
                    300:        }
                    301:        return htab;
1.29      schwarze  302: }
                    303:
1.51      schwarze  304: static struct ohash *
                    305: manmerge_or(struct expr *e, struct ohash *htab)
                    306: {
                    307:        while (e != NULL) {
                    308:                htab = manmerge(e, htab);
                    309:                e = e->next;
                    310:        }
                    311:        return htab;
                    312: }
1.13      schwarze  313:
1.51      schwarze  314: static struct ohash *
                    315: manmerge_and(struct expr *e, struct ohash *htab)
                    316: {
                    317:        struct ohash    *hand, *h1, *h2;
                    318:        struct dbm_res  *res;
                    319:        unsigned int     slot1, slot2;
1.13      schwarze  320:
1.51      schwarze  321:        /* Evaluate the first term of the AND clause. */
1.29      schwarze  322:
1.51      schwarze  323:        hand = manmerge(e, NULL);
1.29      schwarze  324:
1.51      schwarze  325:        while ((e = e->next) != NULL) {
1.13      schwarze  326:
1.51      schwarze  327:                /* Evaluate the next term and prepare for ANDing. */
1.13      schwarze  328:
1.51      schwarze  329:                h2 = manmerge(e, NULL);
                    330:                if (ohash_entries(h2) < ohash_entries(hand)) {
                    331:                        h1 = h2;
                    332:                        h2 = hand;
                    333:                } else
                    334:                        h1 = hand;
                    335:                hand = mandoc_malloc(sizeof(*hand));
                    336:                mandoc_ohash_init(hand, 4, offsetof(struct dbm_res, page));
1.13      schwarze  337:
1.51      schwarze  338:                /* Keep all pages that are in both result sets. */
1.13      schwarze  339:
1.51      schwarze  340:                for (res = ohash_first(h1, &slot1); res != NULL;
                    341:                    res = ohash_next(h1, &slot1)) {
                    342:                        if (ohash_find(h2, ohash_lookup_memory(h2,
                    343:                            (char *)res, sizeof(res->page),
                    344:                            res->page)) == NULL)
                    345:                                free(res);
                    346:                        else
                    347:                                ohash_insert(hand, ohash_lookup_memory(hand,
                    348:                                    (char *)res, sizeof(res->page),
                    349:                                    res->page), res);
1.13      schwarze  350:                }
                    351:
1.51      schwarze  352:                /* Discard the merged results. */
                    353:
                    354:                for (res = ohash_first(h2, &slot2); res != NULL;
                    355:                    res = ohash_next(h2, &slot2))
                    356:                        free(res);
                    357:                ohash_delete(h2);
                    358:                free(h2);
                    359:                ohash_delete(h1);
                    360:                free(h1);
                    361:        }
1.13      schwarze  362:
1.51      schwarze  363:        /* Merge the result of the AND into htab. */
1.8       schwarze  364:
1.51      schwarze  365:        if (htab == NULL)
                    366:                return hand;
1.8       schwarze  367:
1.51      schwarze  368:        for (res = ohash_first(hand, &slot1); res != NULL;
                    369:            res = ohash_next(hand, &slot1)) {
                    370:                slot2 = ohash_lookup_memory(htab,
                    371:                    (char *)res, sizeof(res->page), res->page);
                    372:                if (ohash_find(htab, slot2) == NULL)
                    373:                        ohash_insert(htab, slot2, res);
                    374:                else
                    375:                        free(res);
                    376:        }
                    377:
                    378:        /* Discard the merged result. */
1.8       schwarze  379:
1.51      schwarze  380:        ohash_delete(hand);
                    381:        free(hand);
                    382:        return htab;
                    383: }
1.41      schwarze  384:
1.51      schwarze  385: void
                    386: mansearch_free(struct manpage *res, size_t sz)
                    387: {
                    388:        size_t   i;
1.41      schwarze  389:
1.51      schwarze  390:        for (i = 0; i < sz; i++) {
                    391:                free(res[i].file);
                    392:                free(res[i].names);
                    393:                free(res[i].output);
1.13      schwarze  394:        }
1.51      schwarze  395:        free(res);
                    396: }
                    397:
                    398: static int
                    399: manpage_compare(const void *vp1, const void *vp2)
                    400: {
                    401:        const struct manpage    *mp1, *mp2;
1.58      schwarze  402:        const char              *cp1, *cp2;
                    403:        size_t                   sz1, sz2;
1.51      schwarze  404:        int                      diff;
                    405:
                    406:        mp1 = vp1;
                    407:        mp2 = vp2;
1.62      schwarze  408:        if ((diff = mp1->sec - mp2->sec))
1.58      schwarze  409:                return diff;
                    410:
                    411:        /* Fall back to alphabetic ordering of names. */
                    412:        sz1 = strcspn(mp1->names, "(");
                    413:        sz2 = strcspn(mp2->names, "(");
                    414:        if (sz1 < sz2)
                    415:                sz1 = sz2;
                    416:        if ((diff = strncasecmp(mp1->names, mp2->names, sz1)))
                    417:                return diff;
                    418:
                    419:        /* For identical names and sections, prefer arch-dependent. */
                    420:        cp1 = strchr(mp1->names + sz1, '/');
                    421:        cp2 = strchr(mp2->names + sz2, '/');
                    422:        return cp1 != NULL && cp2 != NULL ? strcasecmp(cp1, cp2) :
                    423:            cp1 != NULL ? -1 : cp2 != NULL ? 1 : 0;
1.3       schwarze  424: }
                    425:
                    426: static char *
1.51      schwarze  427: buildnames(const struct dbm_page *page)
1.3       schwarze  428: {
1.51      schwarze  429:        char    *buf;
                    430:        size_t   i, sz;
1.3       schwarze  431:
1.53      schwarze  432:        sz = lstlen(page->name, 2) + 1 + lstlen(page->sect, 2) +
                    433:            (page->arch == NULL ? 0 : 1 + lstlen(page->arch, 2)) + 2;
1.51      schwarze  434:        buf = mandoc_malloc(sz);
                    435:        i = 0;
1.53      schwarze  436:        lstcat(buf, &i, page->name, ", ");
1.51      schwarze  437:        buf[i++] = '(';
1.53      schwarze  438:        lstcat(buf, &i, page->sect, ", ");
1.51      schwarze  439:        if (page->arch != NULL) {
                    440:                buf[i++] = '/';
1.53      schwarze  441:                lstcat(buf, &i, page->arch, ", ");
1.51      schwarze  442:        }
                    443:        buf[i++] = ')';
                    444:        buf[i++] = '\0';
                    445:        assert(i == sz);
                    446:        return buf;
1.1       schwarze  447: }
                    448:
                    449: /*
1.51      schwarze  450:  * Count the buffer space needed to print the NUL-terminated
1.53      schwarze  451:  * list of NUL-terminated strings, when printing sep separator
1.51      schwarze  452:  * characters between strings.
1.1       schwarze  453:  */
1.51      schwarze  454: static size_t
1.53      schwarze  455: lstlen(const char *cp, size_t sep)
1.1       schwarze  456: {
1.51      schwarze  457:        size_t   sz;
1.1       schwarze  458:
1.59      schwarze  459:        for (sz = 0; *cp != '\0'; cp++) {
                    460:
                    461:                /* Skip names appearing only in the SYNOPSIS. */
                    462:                if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
                    463:                        while (*cp != '\0')
                    464:                                cp++;
                    465:                        continue;
                    466:                }
                    467:
                    468:                /* Skip name class markers. */
                    469:                if (*cp < ' ')
                    470:                        cp++;
                    471:
                    472:                /* Print a separator before each but the first string. */
                    473:                if (sz)
                    474:                        sz += sep;
                    475:
                    476:                /* Copy one string. */
                    477:                while (*cp != '\0') {
                    478:                        sz++;
                    479:                        cp++;
                    480:                }
1.51      schwarze  481:        }
                    482:        return sz;
1.1       schwarze  483: }
                    484:
                    485: /*
1.51      schwarze  486:  * Print the NUL-terminated list of NUL-terminated strings
1.53      schwarze  487:  * into the buffer, seperating strings with sep.
1.1       schwarze  488:  */
                    489: static void
1.53      schwarze  490: lstcat(char *buf, size_t *i, const char *cp, const char *sep)
1.1       schwarze  491: {
1.59      schwarze  492:        const char      *s;
                    493:        size_t           i_start;
1.53      schwarze  494:
1.59      schwarze  495:        for (i_start = *i; *cp != '\0'; cp++) {
                    496:
                    497:                /* Skip names appearing only in the SYNOPSIS. */
                    498:                if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
                    499:                        while (*cp != '\0')
                    500:                                cp++;
                    501:                        continue;
                    502:                }
                    503:
                    504:                /* Skip name class markers. */
                    505:                if (*cp < ' ')
                    506:                        cp++;
                    507:
                    508:                /* Print a separator before each but the first string. */
                    509:                if (*i > i_start) {
1.53      schwarze  510:                        s = sep;
                    511:                        while (*s != '\0')
                    512:                                buf[(*i)++] = *s++;
1.59      schwarze  513:                }
                    514:
                    515:                /* Copy one string. */
                    516:                while (*cp != '\0')
                    517:                        buf[(*i)++] = *cp++;
1.51      schwarze  518:        }
1.59      schwarze  519:
1.1       schwarze  520: }
                    521:
1.51      schwarze  522: /*
                    523:  * Return 1 if the string *want occurs in any of the strings
                    524:  * in the NUL-terminated string list *have, or 0 otherwise.
                    525:  * If either argument is NULL or empty, assume no filtering
                    526:  * is desired and return 1.
                    527:  */
                    528: static int
                    529: lstmatch(const char *want, const char *have)
1.4       schwarze  530: {
1.51      schwarze  531:         if (want == NULL || have == NULL || *have == '\0')
                    532:                 return 1;
                    533:         while (*have != '\0') {
                    534:                 if (strcasestr(have, want) != NULL)
                    535:                         return 1;
                    536:                 have = strchr(have, '\0') + 1;
                    537:         }
                    538:         return 0;
1.4       schwarze  539: }
                    540:
1.1       schwarze  541: /*
1.53      schwarze  542:  * Build a list of values taken by the macro im in the manual page.
1.1       schwarze  543:  */
                    544: static char *
1.53      schwarze  545: buildoutput(size_t im, struct dbm_page *page)
1.1       schwarze  546: {
1.53      schwarze  547:        const char      *oldoutput, *sep, *input;
1.51      schwarze  548:        char            *output, *newoutput, *value;
1.53      schwarze  549:        size_t           sz, i;
                    550:
                    551:        switch (im) {
                    552:        case KEY_Nd:
                    553:                return mandoc_strdup(page->desc);
                    554:        case KEY_Nm:
                    555:                input = page->name;
                    556:                break;
                    557:        case KEY_sec:
                    558:                input = page->sect;
                    559:                break;
                    560:        case KEY_arch:
                    561:                input = page->arch;
                    562:                if (input == NULL)
                    563:                        input = "all\0";
                    564:                break;
                    565:        default:
                    566:                input = NULL;
                    567:                break;
                    568:        }
                    569:
                    570:        if (input != NULL) {
                    571:                sz = lstlen(input, 3) + 1;
                    572:                output = mandoc_malloc(sz);
                    573:                i = 0;
                    574:                lstcat(output, &i, input, " # ");
1.54      schwarze  575:                output[i++] = '\0';
                    576:                assert(i == sz);
1.53      schwarze  577:                return output;
                    578:        }
1.51      schwarze  579:
                    580:        output = NULL;
1.53      schwarze  581:        dbm_macro_bypage(im - 2, page->addr);
1.51      schwarze  582:        while ((value = dbm_macro_next()) != NULL) {
                    583:                if (output == NULL) {
                    584:                        oldoutput = "";
                    585:                        sep = "";
                    586:                } else {
                    587:                        oldoutput = output;
                    588:                        sep = " # ";
                    589:                }
                    590:                mandoc_asprintf(&newoutput, "%s%s%s", oldoutput, sep, value);
                    591:                free(output);
                    592:                output = newoutput;
1.1       schwarze  593:        }
1.51      schwarze  594:        return output;
1.1       schwarze  595: }
                    596:
                    597: /*
                    598:  * Compile a set of string tokens into an expression.
                    599:  * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
                    600:  * "(", "foo=bar", etc.).
                    601:  */
                    602: static struct expr *
1.51      schwarze  603: exprcomp(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       schwarze  604: {
1.51      schwarze  605:        struct expr     *parent, *child;
                    606:        int              needterm, nested;
                    607:
                    608:        if ((nested = *argi) == argc)
                    609:                return NULL;
                    610:        needterm = 1;
                    611:        parent = child = NULL;
                    612:        while (*argi < argc) {
                    613:                if (strcmp(")", argv[*argi]) == 0) {
                    614:                        if (needterm)
                    615:                                warnx("missing term "
                    616:                                    "before closing parenthesis");
                    617:                        needterm = 0;
                    618:                        if (nested)
                    619:                                break;
                    620:                        warnx("ignoring unmatched right parenthesis");
                    621:                        ++*argi;
1.4       schwarze  622:                        continue;
1.51      schwarze  623:                }
                    624:                if (strcmp("-o", argv[*argi]) == 0) {
                    625:                        if (needterm) {
                    626:                                if (*argi > 0)
                    627:                                        warnx("ignoring -o after %s",
                    628:                                            argv[*argi - 1]);
                    629:                                else
                    630:                                        warnx("ignoring initial -o");
                    631:                        }
                    632:                        needterm = 1;
                    633:                        ++*argi;
1.4       schwarze  634:                        continue;
1.51      schwarze  635:                }
                    636:                needterm = 0;
                    637:                if (child == NULL) {
                    638:                        child = expr_and(search, argc, argv, argi);
1.4       schwarze  639:                        continue;
1.1       schwarze  640:                }
1.51      schwarze  641:                if (parent == NULL) {
                    642:                        parent = mandoc_calloc(1, sizeof(*parent));
                    643:                        parent->type = EXPR_OR;
                    644:                        parent->next = NULL;
                    645:                        parent->child = child;
                    646:                }
                    647:                child->next = expr_and(search, argc, argv, argi);
                    648:                child = child->next;
                    649:        }
                    650:        if (needterm && *argi)
                    651:                warnx("ignoring trailing %s", argv[*argi - 1]);
                    652:        return parent == NULL ? child : parent;
                    653: }
1.17      schwarze  654:
1.51      schwarze  655: static struct expr *
                    656: expr_and(const struct mansearch *search, int argc, char *argv[], int *argi)
                    657: {
                    658:        struct expr     *parent, *child;
                    659:        int              needterm;
1.17      schwarze  660:
1.51      schwarze  661:        needterm = 1;
                    662:        parent = child = NULL;
                    663:        while (*argi < argc) {
                    664:                if (strcmp(")", argv[*argi]) == 0) {
                    665:                        if (needterm)
                    666:                                warnx("missing term "
                    667:                                    "before closing parenthesis");
                    668:                        needterm = 0;
                    669:                        break;
                    670:                }
                    671:                if (strcmp("-o", argv[*argi]) == 0)
                    672:                        break;
                    673:                if (strcmp("-a", argv[*argi]) == 0) {
                    674:                        if (needterm) {
                    675:                                if (*argi > 0)
                    676:                                        warnx("ignoring -a after %s",
                    677:                                            argv[*argi - 1]);
                    678:                                else
                    679:                                        warnx("ignoring initial -a");
1.18      schwarze  680:                        }
1.51      schwarze  681:                        needterm = 1;
                    682:                        ++*argi;
                    683:                        continue;
1.18      schwarze  684:                }
1.51      schwarze  685:                if (needterm == 0)
                    686:                        break;
                    687:                if (child == NULL) {
                    688:                        child = exprterm(search, argc, argv, argi);
                    689:                        if (child != NULL)
                    690:                                needterm = 0;
                    691:                        continue;
                    692:                }
                    693:                needterm = 0;
                    694:                if (parent == NULL) {
                    695:                        parent = mandoc_calloc(1, sizeof(*parent));
                    696:                        parent->type = EXPR_AND;
                    697:                        parent->next = NULL;
                    698:                        parent->child = child;
                    699:                }
                    700:                child->next = exprterm(search, argc, argv, argi);
                    701:                if (child->next != NULL) {
                    702:                        child = child->next;
                    703:                        needterm = 0;
                    704:                }
                    705:        }
                    706:        if (needterm && *argi)
                    707:                warnx("ignoring trailing %s", argv[*argi - 1]);
                    708:        return parent == NULL ? child : parent;
1.6       schwarze  709: }
                    710:
                    711: static struct expr *
1.51      schwarze  712: exprterm(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       schwarze  713: {
1.6       schwarze  714:        char             errbuf[BUFSIZ];
1.1       schwarze  715:        struct expr     *e;
1.28      schwarze  716:        char            *key, *val;
1.11      schwarze  717:        uint64_t         iterbit;
1.51      schwarze  718:        int              cs, i, irc;
1.1       schwarze  719:
1.51      schwarze  720:        if (strcmp("(", argv[*argi]) == 0) {
                    721:                ++*argi;
                    722:                e = exprcomp(search, argc, argv, argi);
                    723:                if (*argi < argc) {
                    724:                        assert(strcmp(")", argv[*argi]) == 0);
                    725:                        ++*argi;
                    726:                } else
                    727:                        warnx("unclosed parenthesis");
                    728:                return e;
                    729:        }
1.1       schwarze  730:
1.55      schwarze  731:        if (strcmp("-i", argv[*argi]) == 0 && *argi + 1 < argc) {
                    732:                cs = 0;
                    733:                ++*argi;
                    734:        } else
                    735:                cs = 1;
                    736:
1.51      schwarze  737:        e = mandoc_calloc(1, sizeof(*e));
                    738:        e->type = EXPR_TERM;
                    739:        e->bits = 0;
                    740:        e->next = NULL;
                    741:        e->child = NULL;
1.1       schwarze  742:
1.33      schwarze  743:        if (search->argmode == ARG_NAME) {
                    744:                e->bits = TYPE_Nm;
1.51      schwarze  745:                e->match.type = DBM_EXACT;
                    746:                e->match.str = argv[(*argi)++];
1.45      schwarze  747:                return e;
1.1       schwarze  748:        }
                    749:
                    750:        /*
1.33      schwarze  751:         * Separate macro keys from search string.
1.51      schwarze  752:         * If needed, request regular expression handling.
1.1       schwarze  753:         */
                    754:
1.33      schwarze  755:        if (search->argmode == ARG_WORD) {
                    756:                e->bits = TYPE_Nm;
1.51      schwarze  757:                e->match.type = DBM_REGEX;
                    758:                mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", argv[*argi]);
1.33      schwarze  759:                cs = 0;
1.51      schwarze  760:        } else if ((val = strpbrk(argv[*argi], "=~")) == NULL) {
1.33      schwarze  761:                e->bits = TYPE_Nm | TYPE_Nd;
1.61      schwarze  762:                e->match.type = DBM_REGEX;
                    763:                val = argv[*argi];
                    764:                cs = 0;
1.28      schwarze  765:        } else {
1.51      schwarze  766:                if (val == argv[*argi])
1.33      schwarze  767:                        e->bits = TYPE_Nm | TYPE_Nd;
1.51      schwarze  768:                if (*val == '=') {
                    769:                        e->match.type = DBM_SUB;
                    770:                        e->match.str = val + 1;
                    771:                } else
                    772:                        e->match.type = DBM_REGEX;
1.28      schwarze  773:                *val++ = '\0';
1.51      schwarze  774:                if (strstr(argv[*argi], "arch") != NULL)
1.12      schwarze  775:                        cs = 0;
1.28      schwarze  776:        }
                    777:
                    778:        /* Compile regular expressions. */
                    779:
1.51      schwarze  780:        if (e->match.type == DBM_REGEX) {
                    781:                e->match.re = mandoc_malloc(sizeof(*e->match.re));
                    782:                irc = regcomp(e->match.re, val,
1.28      schwarze  783:                    REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.51      schwarze  784:                if (irc) {
                    785:                        regerror(irc, e->match.re, errbuf, sizeof(errbuf));
                    786:                        warnx("regcomp /%s/: %s", val, errbuf);
                    787:                }
1.33      schwarze  788:                if (search->argmode == ARG_WORD)
1.28      schwarze  789:                        free(val);
                    790:                if (irc) {
1.51      schwarze  791:                        free(e->match.re);
1.1       schwarze  792:                        free(e);
1.51      schwarze  793:                        ++*argi;
1.45      schwarze  794:                        return NULL;
1.1       schwarze  795:                }
1.28      schwarze  796:        }
                    797:
1.51      schwarze  798:        if (e->bits) {
                    799:                ++*argi;
1.45      schwarze  800:                return e;
1.51      schwarze  801:        }
1.1       schwarze  802:
                    803:        /*
                    804:         * Parse out all possible fields.
                    805:         * If the field doesn't resolve, bail.
                    806:         */
                    807:
1.51      schwarze  808:        while (NULL != (key = strsep(&argv[*argi], ","))) {
1.1       schwarze  809:                if ('\0' == *key)
                    810:                        continue;
1.51      schwarze  811:                for (i = 0, iterbit = 1; i < KEY_MAX; i++, iterbit <<= 1) {
                    812:                        if (0 == strcasecmp(key, mansearch_keynames[i])) {
1.11      schwarze  813:                                e->bits |= iterbit;
                    814:                                break;
                    815:                        }
                    816:                }
1.51      schwarze  817:                if (i == KEY_MAX) {
                    818:                        if (strcasecmp(key, "any"))
                    819:                                warnx("treating unknown key "
                    820:                                    "\"%s\" as \"any\"", key);
1.11      schwarze  821:                        e->bits |= ~0ULL;
1.1       schwarze  822:                }
                    823:        }
                    824:
1.51      schwarze  825:        ++*argi;
1.45      schwarze  826:        return e;
1.1       schwarze  827: }
                    828:
                    829: static void
1.51      schwarze  830: exprfree(struct expr *e)
1.1       schwarze  831: {
1.51      schwarze  832:        if (e->next != NULL)
                    833:                exprfree(e->next);
                    834:        if (e->child != NULL)
                    835:                exprfree(e->child);
                    836:        free(e);
1.1       schwarze  837: }