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

Annotation of mandoc/mansearch.c, Revision 1.79

1.79    ! schwarze    1: /*     $Id: mansearch.c,v 1.78 2018/11/19 19:27:37 schwarze Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.78      schwarze    4:  * Copyright (c) 2013-2018 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.56      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.56      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.67      schwarze   18: #include "config.h"
1.1       kristaps   19:
1.28      schwarze   20: #include <sys/mman.h>
1.43      schwarze   21: #include <sys/types.h>
                     22:
1.1       kristaps   23: #include <assert.h>
1.68      schwarze   24: #if HAVE_ERR
1.59      schwarze   25: #include <err.h>
1.68      schwarze   26: #endif
1.52      schwarze   27: #include <errno.h>
1.1       kristaps   28: #include <fcntl.h>
1.54      schwarze   29: #include <glob.h>
1.6       schwarze   30: #include <limits.h>
1.8       schwarze   31: #include <regex.h>
1.1       kristaps   32: #include <stdio.h>
                     33: #include <stdint.h>
                     34: #include <stddef.h>
                     35: #include <stdlib.h>
                     36: #include <string.h>
                     37: #include <unistd.h>
                     38:
                     39: #include "mandoc.h"
1.23      schwarze   40: #include "mandoc_aux.h"
1.60      schwarze   41: #include "mandoc_ohash.h"
1.56      schwarze   42: #include "manconf.h"
1.1       kristaps   43: #include "mansearch.h"
1.66      schwarze   44: #include "dbm.h"
1.2       kristaps   45:
1.1       kristaps   46: struct expr {
1.66      schwarze   47:        /* Used for terms: */
                     48:        struct dbm_match match;   /* Match type and expression. */
                     49:        uint64_t         bits;    /* Type mask. */
                     50:        /* Used for OR and AND groups: */
                     51:        struct expr     *next;    /* Next child in the parent group. */
                     52:        struct expr     *child;   /* First child in this group. */
                     53:        enum { EXPR_TERM, EXPR_OR, EXPR_AND } type;
1.1       kristaps   54: };
                     55:
1.66      schwarze   56: const char *const mansearch_keynames[KEY_MAX] = {
                     57:        "arch", "sec",  "Xr",   "Ar",   "Fa",   "Fl",   "Dv",   "Fn",
                     58:        "Ic",   "Pa",   "Cm",   "Li",   "Em",   "Cd",   "Va",   "Ft",
                     59:        "Tn",   "Er",   "Ev",   "Sy",   "Sh",   "In",   "Ss",   "Ox",
                     60:        "An",   "Mt",   "St",   "Bx",   "At",   "Nx",   "Fx",   "Lk",
                     61:        "Ms",   "Bsx",  "Dx",   "Rs",   "Vt",   "Lb",   "Nm",   "Nd"
1.1       kristaps   62: };
                     63:
1.66      schwarze   64:
                     65: static struct ohash    *manmerge(struct expr *, struct ohash *);
                     66: static struct ohash    *manmerge_term(struct expr *, struct ohash *);
                     67: static struct ohash    *manmerge_or(struct expr *, struct ohash *);
                     68: static struct ohash    *manmerge_and(struct expr *, struct ohash *);
                     69: static char            *buildnames(const struct dbm_page *);
1.70      schwarze   70: static char            *buildoutput(size_t, struct dbm_page *);
                     71: static size_t           lstlen(const char *, size_t);
                     72: static void             lstcat(char *, size_t *, const char *, const char *);
1.66      schwarze   73: static int              lstmatch(const char *, const char *);
1.34      schwarze   74: static struct expr     *exprcomp(const struct mansearch *,
1.66      schwarze   75:                                int, char *[], int *);
                     76: static struct expr     *expr_and(const struct mansearch *,
                     77:                                int, char *[], int *);
                     78: static struct expr     *exprterm(const struct mansearch *,
                     79:                                int, char *[], int *);
1.1       kristaps   80: static void             exprfree(struct expr *);
1.39      schwarze   81: static int              manpage_compare(const void *, const void *);
1.28      schwarze   82:
1.1       kristaps   83:
                     84: int
1.5       kristaps   85: mansearch(const struct mansearch *search,
1.12      schwarze   86:                const struct manpaths *paths,
                     87:                int argc, char *argv[],
1.1       kristaps   88:                struct manpage **res, size_t *sz)
                     89: {
1.6       schwarze   90:        char             buf[PATH_MAX];
1.66      schwarze   91:        struct dbm_res  *rp;
                     92:        struct expr     *e;
                     93:        struct dbm_page *page;
1.10      schwarze   94:        struct manpage  *mpage;
1.66      schwarze   95:        struct ohash    *htab;
                     96:        size_t           cur, i, maxres, outkey;
                     97:        unsigned int     slot;
                     98:        int              argi, chdir_status, getcwd_status, im;
1.57      schwarze   99:
1.66      schwarze  100:        argi = 0;
                    101:        if ((e = exprcomp(search, argc, argv, &argi)) == NULL) {
1.57      schwarze  102:                *sz = 0;
1.58      schwarze  103:                return 0;
1.57      schwarze  104:        }
1.1       kristaps  105:
1.57      schwarze  106:        cur = maxres = 0;
1.74      schwarze  107:        if (res != NULL)
                    108:                *res = NULL;
1.1       kristaps  109:
1.66      schwarze  110:        outkey = KEY_Nd;
                    111:        if (search->outkey != NULL)
                    112:                for (im = 0; im < KEY_MAX; im++)
1.45      schwarze  113:                        if (0 == strcasecmp(search->outkey,
1.66      schwarze  114:                            mansearch_keynames[im])) {
                    115:                                outkey = im;
1.12      schwarze  116:                                break;
                    117:                        }
                    118:
1.1       kristaps  119:        /*
1.57      schwarze  120:         * Remember the original working directory, if possible.
                    121:         * This will be needed if the second or a later directory
                    122:         * is given as a relative path.
                    123:         * Do not error out if the current directory is not
                    124:         * searchable: Maybe it won't be needed after all.
1.1       kristaps  125:         */
                    126:
1.57      schwarze  127:        if (getcwd(buf, PATH_MAX) == NULL) {
                    128:                getcwd_status = 0;
                    129:                (void)strlcpy(buf, strerror(errno), sizeof(buf));
                    130:        } else
                    131:                getcwd_status = 1;
1.1       kristaps  132:
                    133:        /*
                    134:         * Loop over the directories (containing databases) for us to
                    135:         * search.
                    136:         * Don't let missing/bad databases/directories phase us.
                    137:         * In each, try to open the resident database and, if it opens,
                    138:         * scan it for our match expression.
                    139:         */
                    140:
1.57      schwarze  141:        chdir_status = 0;
1.1       kristaps  142:        for (i = 0; i < paths->sz; i++) {
1.57      schwarze  143:                if (chdir_status && paths->paths[i][0] != '/') {
                    144:                        if ( ! getcwd_status) {
1.59      schwarze  145:                                warnx("%s: getcwd: %s", paths->paths[i], buf);
1.57      schwarze  146:                                continue;
                    147:                        } else if (chdir(buf) == -1) {
1.64      schwarze  148:                                warn("%s", buf);
1.57      schwarze  149:                                continue;
                    150:                        }
                    151:                }
                    152:                if (chdir(paths->paths[i]) == -1) {
1.64      schwarze  153:                        warn("%s", paths->paths[i]);
1.1       kristaps  154:                        continue;
1.34      schwarze  155:                }
1.57      schwarze  156:                chdir_status = 1;
1.1       kristaps  157:
1.66      schwarze  158:                if (dbm_open(MANDOC_DB) == -1) {
1.73      schwarze  159:                        if (errno != ENOENT)
                    160:                                warn("%s/%s", paths->paths[i], MANDOC_DB);
1.1       kristaps  161:                        continue;
                    162:                }
                    163:
1.66      schwarze  164:                if ((htab = manmerge(e, NULL)) == NULL) {
                    165:                        dbm_close();
                    166:                        continue;
1.1       kristaps  167:                }
                    168:
1.66      schwarze  169:                for (rp = ohash_first(htab, &slot); rp != NULL;
                    170:                    rp = ohash_next(htab, &slot)) {
                    171:                        page = dbm_page_get(rp->page);
1.1       kristaps  172:
1.66      schwarze  173:                        if (lstmatch(search->sec, page->sect) == 0 ||
1.76      schwarze  174:                            lstmatch(search->arch, page->arch) == 0 ||
                    175:                            (search->argmode == ARG_NAME &&
                    176:                             rp->bits <= (int32_t)(NAME_SYN & NAME_MASK)))
1.1       kristaps  177:                                continue;
                    178:
1.74      schwarze  179:                        if (res == NULL) {
                    180:                                cur = 1;
                    181:                                break;
                    182:                        }
1.1       kristaps  183:                        if (cur + 1 > maxres) {
                    184:                                maxres += 1024;
1.36      schwarze  185:                                *res = mandoc_reallocarray(*res,
1.66      schwarze  186:                                    maxres, sizeof(**res));
1.1       kristaps  187:                        }
1.10      schwarze  188:                        mpage = *res + cur;
1.66      schwarze  189:                        mandoc_asprintf(&mpage->file, "%s/%s",
                    190:                            paths->paths[i], page->file + 1);
1.77      schwarze  191:                        if (access(chdir_status ? page->file + 1 :
                    192:                            mpage->file, R_OK) == -1) {
                    193:                                warn("%s", mpage->file);
                    194:                                warnx("outdated mandoc.db contains "
                    195:                                    "bogus %s entry, run makewhatis %s",
                    196:                                    page->file + 1, paths->paths[i]);
                    197:                                free(mpage->file);
                    198:                                free(rp);
                    199:                                continue;
                    200:                        }
1.66      schwarze  201:                        mpage->names = buildnames(page);
1.70      schwarze  202:                        mpage->output = buildoutput(outkey, page);
1.47      schwarze  203:                        mpage->ipath = i;
1.66      schwarze  204:                        mpage->sec = *page->sect - '0';
                    205:                        if (mpage->sec < 0 || mpage->sec > 9)
                    206:                                mpage->sec = 10;
                    207:                        mpage->form = *page->file;
                    208:                        free(rp);
                    209:                        cur++;
                    210:                }
                    211:                ohash_delete(htab);
                    212:                free(htab);
                    213:                dbm_close();
1.49      schwarze  214:
                    215:                /*
                    216:                 * In man(1) mode, prefer matches in earlier trees
                    217:                 * over matches in later trees.
                    218:                 */
                    219:
                    220:                if (cur && search->firstmatch)
                    221:                        break;
1.1       kristaps  222:        }
1.74      schwarze  223:        if (res != NULL)
                    224:                qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.57      schwarze  225:        if (chdir_status && getcwd_status && chdir(buf) == -1)
1.64      schwarze  226:                warn("%s", buf);
1.1       kristaps  227:        exprfree(e);
                    228:        *sz = cur;
1.74      schwarze  229:        return res != NULL || cur;
1.11      schwarze  230: }
                    231:
1.66      schwarze  232: /*
                    233:  * Merge the results for the expression tree rooted at e
                    234:  * into the the result list htab.
                    235:  */
                    236: static struct ohash *
                    237: manmerge(struct expr *e, struct ohash *htab)
1.45      schwarze  238: {
1.66      schwarze  239:        switch (e->type) {
                    240:        case EXPR_TERM:
                    241:                return manmerge_term(e, htab);
                    242:        case EXPR_OR:
                    243:                return manmerge_or(e->child, htab);
                    244:        case EXPR_AND:
                    245:                return manmerge_and(e->child, htab);
                    246:        default:
                    247:                abort();
1.45      schwarze  248:        }
                    249: }
                    250:
1.66      schwarze  251: static struct ohash *
                    252: manmerge_term(struct expr *e, struct ohash *htab)
1.39      schwarze  253: {
1.66      schwarze  254:        struct dbm_res   res, *rp;
                    255:        uint64_t         ib;
                    256:        unsigned int     slot;
                    257:        int              im;
                    258:
                    259:        if (htab == NULL) {
                    260:                htab = mandoc_malloc(sizeof(*htab));
                    261:                mandoc_ohash_init(htab, 4, offsetof(struct dbm_res, page));
                    262:        }
                    263:
                    264:        for (im = 0, ib = 1; im < KEY_MAX; im++, ib <<= 1) {
                    265:                if ((e->bits & ib) == 0)
                    266:                        continue;
                    267:
                    268:                switch (ib) {
                    269:                case TYPE_arch:
                    270:                        dbm_page_byarch(&e->match);
                    271:                        break;
                    272:                case TYPE_sec:
                    273:                        dbm_page_bysect(&e->match);
                    274:                        break;
                    275:                case TYPE_Nm:
                    276:                        dbm_page_byname(&e->match);
                    277:                        break;
                    278:                case TYPE_Nd:
                    279:                        dbm_page_bydesc(&e->match);
                    280:                        break;
                    281:                default:
                    282:                        dbm_page_bymacro(im - 2, &e->match);
                    283:                        break;
                    284:                }
                    285:
                    286:                /*
                    287:                 * When hashing for deduplication, use the unique
                    288:                 * page ID itself instead of a hash function;
                    289:                 * that is quite efficient.
                    290:                 */
1.39      schwarze  291:
1.66      schwarze  292:                for (;;) {
                    293:                        res = dbm_page_next();
                    294:                        if (res.page == -1)
                    295:                                break;
                    296:                        slot = ohash_lookup_memory(htab,
                    297:                            (char *)&res, sizeof(res.page), res.page);
1.79    ! schwarze  298:                        if ((rp = ohash_find(htab, slot)) != NULL)
1.66      schwarze  299:                                continue;
                    300:                        rp = mandoc_malloc(sizeof(*rp));
                    301:                        *rp = res;
                    302:                        ohash_insert(htab, slot, rp);
                    303:                }
                    304:        }
                    305:        return htab;
1.39      schwarze  306: }
                    307:
1.66      schwarze  308: static struct ohash *
                    309: manmerge_or(struct expr *e, struct ohash *htab)
                    310: {
                    311:        while (e != NULL) {
                    312:                htab = manmerge(e, htab);
                    313:                e = e->next;
                    314:        }
                    315:        return htab;
                    316: }
1.22      schwarze  317:
1.66      schwarze  318: static struct ohash *
                    319: manmerge_and(struct expr *e, struct ohash *htab)
                    320: {
                    321:        struct ohash    *hand, *h1, *h2;
                    322:        struct dbm_res  *res;
                    323:        unsigned int     slot1, slot2;
1.22      schwarze  324:
1.66      schwarze  325:        /* Evaluate the first term of the AND clause. */
1.39      schwarze  326:
1.66      schwarze  327:        hand = manmerge(e, NULL);
1.39      schwarze  328:
1.66      schwarze  329:        while ((e = e->next) != NULL) {
1.22      schwarze  330:
1.66      schwarze  331:                /* Evaluate the next term and prepare for ANDing. */
1.22      schwarze  332:
1.66      schwarze  333:                h2 = manmerge(e, NULL);
                    334:                if (ohash_entries(h2) < ohash_entries(hand)) {
                    335:                        h1 = h2;
                    336:                        h2 = hand;
                    337:                } else
                    338:                        h1 = hand;
                    339:                hand = mandoc_malloc(sizeof(*hand));
                    340:                mandoc_ohash_init(hand, 4, offsetof(struct dbm_res, page));
1.22      schwarze  341:
1.66      schwarze  342:                /* Keep all pages that are in both result sets. */
1.22      schwarze  343:
1.66      schwarze  344:                for (res = ohash_first(h1, &slot1); res != NULL;
                    345:                    res = ohash_next(h1, &slot1)) {
                    346:                        if (ohash_find(h2, ohash_lookup_memory(h2,
                    347:                            (char *)res, sizeof(res->page),
                    348:                            res->page)) == NULL)
                    349:                                free(res);
                    350:                        else
                    351:                                ohash_insert(hand, ohash_lookup_memory(hand,
                    352:                                    (char *)res, sizeof(res->page),
                    353:                                    res->page), res);
1.22      schwarze  354:                }
                    355:
1.66      schwarze  356:                /* Discard the merged results. */
1.22      schwarze  357:
1.66      schwarze  358:                for (res = ohash_first(h2, &slot2); res != NULL;
                    359:                    res = ohash_next(h2, &slot2))
                    360:                        free(res);
                    361:                ohash_delete(h2);
                    362:                free(h2);
                    363:                ohash_delete(h1);
                    364:                free(h1);
                    365:        }
1.17      schwarze  366:
1.66      schwarze  367:        /* Merge the result of the AND into htab. */
1.17      schwarze  368:
1.66      schwarze  369:        if (htab == NULL)
                    370:                return hand;
                    371:
                    372:        for (res = ohash_first(hand, &slot1); res != NULL;
                    373:            res = ohash_next(hand, &slot1)) {
                    374:                slot2 = ohash_lookup_memory(htab,
                    375:                    (char *)res, sizeof(res->page), res->page);
                    376:                if (ohash_find(htab, slot2) == NULL)
                    377:                        ohash_insert(htab, slot2, res);
                    378:                else
                    379:                        free(res);
                    380:        }
                    381:
                    382:        /* Discard the merged result. */
1.17      schwarze  383:
1.66      schwarze  384:        ohash_delete(hand);
                    385:        free(hand);
                    386:        return htab;
                    387: }
1.54      schwarze  388:
1.66      schwarze  389: void
                    390: mansearch_free(struct manpage *res, size_t sz)
                    391: {
                    392:        size_t   i;
1.54      schwarze  393:
1.66      schwarze  394:        for (i = 0; i < sz; i++) {
                    395:                free(res[i].file);
                    396:                free(res[i].names);
                    397:                free(res[i].output);
1.22      schwarze  398:        }
1.66      schwarze  399:        free(res);
                    400: }
                    401:
                    402: static int
                    403: manpage_compare(const void *vp1, const void *vp2)
                    404: {
                    405:        const struct manpage    *mp1, *mp2;
1.75      schwarze  406:        const char              *cp1, *cp2;
                    407:        size_t                   sz1, sz2;
1.66      schwarze  408:        int                      diff;
                    409:
                    410:        mp1 = vp1;
                    411:        mp2 = vp2;
1.79    ! schwarze  412:        if ((diff = mp1->sec - mp2->sec))
1.75      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.12      schwarze  428: }
                    429:
                    430: static char *
1.66      schwarze  431: buildnames(const struct dbm_page *page)
1.12      schwarze  432: {
1.66      schwarze  433:        char    *buf;
                    434:        size_t   i, sz;
1.12      schwarze  435:
1.70      schwarze  436:        sz = lstlen(page->name, 2) + 1 + lstlen(page->sect, 2) +
                    437:            (page->arch == NULL ? 0 : 1 + lstlen(page->arch, 2)) + 2;
1.66      schwarze  438:        buf = mandoc_malloc(sz);
                    439:        i = 0;
1.70      schwarze  440:        lstcat(buf, &i, page->name, ", ");
1.66      schwarze  441:        buf[i++] = '(';
1.70      schwarze  442:        lstcat(buf, &i, page->sect, ", ");
1.66      schwarze  443:        if (page->arch != NULL) {
                    444:                buf[i++] = '/';
1.70      schwarze  445:                lstcat(buf, &i, page->arch, ", ");
1.66      schwarze  446:        }
                    447:        buf[i++] = ')';
                    448:        buf[i++] = '\0';
                    449:        assert(i == sz);
                    450:        return buf;
1.1       kristaps  451: }
                    452:
                    453: /*
1.66      schwarze  454:  * Count the buffer space needed to print the NUL-terminated
1.70      schwarze  455:  * list of NUL-terminated strings, when printing sep separator
1.66      schwarze  456:  * characters between strings.
1.7       schwarze  457:  */
1.66      schwarze  458: static size_t
1.70      schwarze  459: lstlen(const char *cp, size_t sep)
1.7       schwarze  460: {
1.66      schwarze  461:        size_t   sz;
1.7       schwarze  462:
1.76      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.66      schwarze  485:        }
                    486:        return sz;
1.7       schwarze  487: }
                    488:
                    489: /*
1.66      schwarze  490:  * Print the NUL-terminated list of NUL-terminated strings
1.70      schwarze  491:  * into the buffer, seperating strings with sep.
1.8       schwarze  492:  */
                    493: static void
1.70      schwarze  494: lstcat(char *buf, size_t *i, const char *cp, const char *sep)
1.8       schwarze  495: {
1.76      schwarze  496:        const char      *s;
                    497:        size_t           i_start;
1.70      schwarze  498:
1.76      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.70      schwarze  514:                        s = sep;
                    515:                        while (*s != '\0')
                    516:                                buf[(*i)++] = *s++;
1.76      schwarze  517:                }
                    518:
                    519:                /* Copy one string. */
                    520:                while (*cp != '\0')
                    521:                        buf[(*i)++] = *cp++;
1.66      schwarze  522:        }
1.76      schwarze  523:
1.8       schwarze  524: }
                    525:
1.66      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.13      schwarze  534: {
1.66      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.13      schwarze  543: }
                    544:
1.8       schwarze  545: /*
1.70      schwarze  546:  * Build a list of values taken by the macro im in the manual page.
1.1       kristaps  547:  */
                    548: static char *
1.70      schwarze  549: buildoutput(size_t im, struct dbm_page *page)
1.1       kristaps  550: {
1.70      schwarze  551:        const char      *oldoutput, *sep, *input;
1.66      schwarze  552:        char            *output, *newoutput, *value;
1.70      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.71      schwarze  579:                output[i++] = '\0';
                    580:                assert(i == sz);
1.70      schwarze  581:                return output;
                    582:        }
1.66      schwarze  583:
                    584:        output = NULL;
1.70      schwarze  585:        dbm_macro_bypage(im - 2, page->addr);
1.66      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       kristaps  597:        }
1.66      schwarze  598:        return output;
1.1       kristaps  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.66      schwarze  607: exprcomp(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       kristaps  608: {
1.66      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.13      schwarze  626:                        continue;
1.66      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.13      schwarze  638:                        continue;
1.66      schwarze  639:                }
                    640:                needterm = 0;
                    641:                if (child == NULL) {
                    642:                        child = expr_and(search, argc, argv, argi);
1.13      schwarze  643:                        continue;
1.1       kristaps  644:                }
1.66      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.26      schwarze  658:
1.66      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.26      schwarze  664:
1.66      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.27      schwarze  684:                        }
1.66      schwarze  685:                        needterm = 1;
                    686:                        ++*argi;
                    687:                        continue;
1.27      schwarze  688:                }
1.66      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.15      schwarze  713: }
                    714:
                    715: static struct expr *
1.66      schwarze  716: exprterm(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       kristaps  717: {
1.15      schwarze  718:        char             errbuf[BUFSIZ];
1.1       kristaps  719:        struct expr     *e;
1.38      schwarze  720:        char            *key, *val;
1.20      schwarze  721:        uint64_t         iterbit;
1.66      schwarze  722:        int              cs, i, irc;
1.1       kristaps  723:
1.66      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       kristaps  734:
1.72      schwarze  735:        if (strcmp("-i", argv[*argi]) == 0 && *argi + 1 < argc) {
                    736:                cs = 0;
                    737:                ++*argi;
                    738:        } else
                    739:                cs = 1;
                    740:
1.66      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       kristaps  746:
1.45      schwarze  747:        if (search->argmode == ARG_NAME) {
                    748:                e->bits = TYPE_Nm;
1.66      schwarze  749:                e->match.type = DBM_EXACT;
                    750:                e->match.str = argv[(*argi)++];
1.58      schwarze  751:                return e;
1.5       kristaps  752:        }
                    753:
1.1       kristaps  754:        /*
1.45      schwarze  755:         * Separate macro keys from search string.
1.66      schwarze  756:         * If needed, request regular expression handling.
1.1       kristaps  757:         */
                    758:
1.45      schwarze  759:        if (search->argmode == ARG_WORD) {
                    760:                e->bits = TYPE_Nm;
1.66      schwarze  761:                e->match.type = DBM_REGEX;
1.61      schwarze  762: #if HAVE_REWB_BSD
1.66      schwarze  763:                mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", argv[*argi]);
1.61      schwarze  764: #elif HAVE_REWB_SYSV
1.66      schwarze  765:                mandoc_asprintf(&val, "\\<%s\\>", argv[*argi]);
1.61      schwarze  766: #else
                    767:                mandoc_asprintf(&val,
1.66      schwarze  768:                    "(^|[^a-zA-Z01-9_])%s([^a-zA-Z01-9_]|$)", argv[*argi]);
1.61      schwarze  769: #endif
1.46      schwarze  770:                cs = 0;
1.66      schwarze  771:        } else if ((val = strpbrk(argv[*argi], "=~")) == NULL) {
1.45      schwarze  772:                e->bits = TYPE_Nm | TYPE_Nd;
1.78      schwarze  773:                e->match.type = DBM_REGEX;
                    774:                val = argv[*argi];
                    775:                cs = 0;
1.38      schwarze  776:        } else {
1.66      schwarze  777:                if (val == argv[*argi])
1.45      schwarze  778:                        e->bits = TYPE_Nm | TYPE_Nd;
1.66      schwarze  779:                if (*val == '=') {
                    780:                        e->match.type = DBM_SUB;
                    781:                        e->match.str = val + 1;
                    782:                } else
                    783:                        e->match.type = DBM_REGEX;
1.38      schwarze  784:                *val++ = '\0';
1.66      schwarze  785:                if (strstr(argv[*argi], "arch") != NULL)
1.21      schwarze  786:                        cs = 0;
1.38      schwarze  787:        }
                    788:
                    789:        /* Compile regular expressions. */
                    790:
1.66      schwarze  791:        if (e->match.type == DBM_REGEX) {
                    792:                e->match.re = mandoc_malloc(sizeof(*e->match.re));
                    793:                irc = regcomp(e->match.re, val,
1.38      schwarze  794:                    REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.66      schwarze  795:                if (irc) {
                    796:                        regerror(irc, e->match.re, errbuf, sizeof(errbuf));
                    797:                        warnx("regcomp /%s/: %s", val, errbuf);
                    798:                }
1.45      schwarze  799:                if (search->argmode == ARG_WORD)
1.38      schwarze  800:                        free(val);
                    801:                if (irc) {
1.66      schwarze  802:                        free(e->match.re);
1.8       schwarze  803:                        free(e);
1.66      schwarze  804:                        ++*argi;
1.58      schwarze  805:                        return NULL;
1.8       schwarze  806:                }
1.38      schwarze  807:        }
                    808:
1.66      schwarze  809:        if (e->bits) {
                    810:                ++*argi;
1.58      schwarze  811:                return e;
1.66      schwarze  812:        }
1.1       kristaps  813:
                    814:        /*
                    815:         * Parse out all possible fields.
                    816:         * If the field doesn't resolve, bail.
                    817:         */
                    818:
1.66      schwarze  819:        while (NULL != (key = strsep(&argv[*argi], ","))) {
1.1       kristaps  820:                if ('\0' == *key)
                    821:                        continue;
1.66      schwarze  822:                for (i = 0, iterbit = 1; i < KEY_MAX; i++, iterbit <<= 1) {
                    823:                        if (0 == strcasecmp(key, mansearch_keynames[i])) {
1.20      schwarze  824:                                e->bits |= iterbit;
                    825:                                break;
                    826:                        }
                    827:                }
1.66      schwarze  828:                if (i == KEY_MAX) {
                    829:                        if (strcasecmp(key, "any"))
                    830:                                warnx("treating unknown key "
                    831:                                    "\"%s\" as \"any\"", key);
1.20      schwarze  832:                        e->bits |= ~0ULL;
1.1       kristaps  833:                }
                    834:        }
                    835:
1.66      schwarze  836:        ++*argi;
1.58      schwarze  837:        return e;
1.1       kristaps  838: }
                    839:
                    840: static void
1.66      schwarze  841: exprfree(struct expr *e)
1.1       kristaps  842: {
1.66      schwarze  843:        if (e->next != NULL)
                    844:                exprfree(e->next);
                    845:        if (e->child != NULL)
                    846:                exprfree(e->child);
                    847:        free(e);
1.1       kristaps  848: }

CVSweb