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

1.56    ! schwarze    1: /*     $OpenBSD: mansearch.c,v 1.55 2017/04/18 15:04:35 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.53      schwarze    4:  * Copyright (c) 2013-2017 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:
                     36: #include "mandoc.h"
1.14      schwarze   37: #include "mandoc_aux.h"
1.47      schwarze   38: #include "mandoc_ohash.h"
1.43      schwarze   39: #include "manconf.h"
1.1       schwarze   40: #include "mansearch.h"
1.51      schwarze   41: #include "dbm.h"
1.1       schwarze   42:
                     43: struct expr {
1.51      schwarze   44:        /* Used for terms: */
                     45:        struct dbm_match match;   /* Match type and expression. */
                     46:        uint64_t         bits;    /* Type mask. */
                     47:        /* Used for OR and AND groups: */
                     48:        struct expr     *next;    /* Next child in the parent group. */
                     49:        struct expr     *child;   /* First child in this group. */
                     50:        enum { EXPR_TERM, EXPR_OR, EXPR_AND } type;
1.1       schwarze   51: };
                     52:
1.51      schwarze   53: const char *const mansearch_keynames[KEY_MAX] = {
                     54:        "arch", "sec",  "Xr",   "Ar",   "Fa",   "Fl",   "Dv",   "Fn",
                     55:        "Ic",   "Pa",   "Cm",   "Li",   "Em",   "Cd",   "Va",   "Ft",
                     56:        "Tn",   "Er",   "Ev",   "Sy",   "Sh",   "In",   "Ss",   "Ox",
                     57:        "An",   "Mt",   "St",   "Bx",   "At",   "Nx",   "Fx",   "Lk",
                     58:        "Ms",   "Bsx",  "Dx",   "Rs",   "Vt",   "Lb",   "Nm",   "Nd"
1.1       schwarze   59: };
                     60:
1.51      schwarze   61:
                     62: static struct ohash    *manmerge(struct expr *, struct ohash *);
                     63: static struct ohash    *manmerge_term(struct expr *, struct ohash *);
                     64: static struct ohash    *manmerge_or(struct expr *, struct ohash *);
                     65: static struct ohash    *manmerge_and(struct expr *, struct ohash *);
                     66: static char            *buildnames(const struct dbm_page *);
1.53      schwarze   67: static char            *buildoutput(size_t, struct dbm_page *);
                     68: static size_t           lstlen(const char *, size_t);
                     69: static void             lstcat(char *, size_t *, const char *, const char *);
1.51      schwarze   70: static int              lstmatch(const char *, const char *);
1.24      schwarze   71: static struct expr     *exprcomp(const struct mansearch *,
1.51      schwarze   72:                                int, char *[], int *);
                     73: static struct expr     *expr_and(const struct mansearch *,
                     74:                                int, char *[], int *);
                     75: static struct expr     *exprterm(const struct mansearch *,
                     76:                                int, char *[], int *);
1.1       schwarze   77: static void             exprfree(struct expr *);
1.29      schwarze   78: static int              manpage_compare(const void *, const void *);
1.19      schwarze   79:
1.24      schwarze   80:
1.19      schwarze   81: int
1.1       schwarze   82: mansearch(const struct mansearch *search,
1.3       schwarze   83:                const struct manpaths *paths,
                     84:                int argc, char *argv[],
1.1       schwarze   85:                struct manpage **res, size_t *sz)
                     86: {
                     87:        char             buf[PATH_MAX];
1.51      schwarze   88:        struct dbm_res  *rp;
                     89:        struct expr     *e;
                     90:        struct dbm_page *page;
1.1       schwarze   91:        struct manpage  *mpage;
1.51      schwarze   92:        struct ohash    *htab;
                     93:        size_t           cur, i, maxres, outkey;
                     94:        unsigned int     slot;
                     95:        int              argi, chdir_status, getcwd_status, im;
1.44      schwarze   96:
1.51      schwarze   97:        argi = 0;
                     98:        if ((e = exprcomp(search, argc, argv, &argi)) == NULL) {
1.44      schwarze   99:                *sz = 0;
1.45      schwarze  100:                return 0;
1.44      schwarze  101:        }
1.1       schwarze  102:
1.44      schwarze  103:        cur = maxres = 0;
1.1       schwarze  104:        *res = NULL;
                    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 ||
                    170:                            lstmatch(search->arch, page->arch) == 0)
1.1       schwarze  171:                                continue;
                    172:
                    173:                        if (cur + 1 > maxres) {
                    174:                                maxres += 1024;
1.26      schwarze  175:                                *res = mandoc_reallocarray(*res,
1.51      schwarze  176:                                    maxres, sizeof(**res));
1.1       schwarze  177:                        }
                    178:                        mpage = *res + cur;
1.51      schwarze  179:                        mandoc_asprintf(&mpage->file, "%s/%s",
                    180:                            paths->paths[i], page->file + 1);
                    181:                        mpage->names = buildnames(page);
1.53      schwarze  182:                        mpage->output = buildoutput(outkey, page);
1.34      schwarze  183:                        mpage->ipath = i;
1.51      schwarze  184:                        mpage->bits = rp->bits;
                    185:                        mpage->sec = *page->sect - '0';
                    186:                        if (mpage->sec < 0 || mpage->sec > 9)
                    187:                                mpage->sec = 10;
                    188:                        mpage->form = *page->file;
                    189:                        free(rp);
                    190:                        cur++;
                    191:                }
                    192:                ohash_delete(htab);
                    193:                free(htab);
                    194:                dbm_close();
1.36      schwarze  195:
                    196:                /*
                    197:                 * In man(1) mode, prefer matches in earlier trees
                    198:                 * over matches in later trees.
                    199:                 */
                    200:
                    201:                if (cur && search->firstmatch)
                    202:                        break;
1.1       schwarze  203:        }
1.29      schwarze  204:        qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.44      schwarze  205:        if (chdir_status && getcwd_status && chdir(buf) == -1)
1.49      schwarze  206:                warn("%s", buf);
1.1       schwarze  207:        exprfree(e);
                    208:        *sz = cur;
1.45      schwarze  209:        return 1;
1.2       schwarze  210: }
                    211:
1.51      schwarze  212: /*
                    213:  * Merge the results for the expression tree rooted at e
                    214:  * into the the result list htab.
                    215:  */
                    216: static struct ohash *
                    217: manmerge(struct expr *e, struct ohash *htab)
1.33      schwarze  218: {
1.51      schwarze  219:        switch (e->type) {
                    220:        case EXPR_TERM:
                    221:                return manmerge_term(e, htab);
                    222:        case EXPR_OR:
                    223:                return manmerge_or(e->child, htab);
                    224:        case EXPR_AND:
                    225:                return manmerge_and(e->child, htab);
                    226:        default:
                    227:                abort();
1.33      schwarze  228:        }
                    229: }
                    230:
1.51      schwarze  231: static struct ohash *
                    232: manmerge_term(struct expr *e, struct ohash *htab)
1.29      schwarze  233: {
1.51      schwarze  234:        struct dbm_res   res, *rp;
                    235:        uint64_t         ib;
                    236:        unsigned int     slot;
                    237:        int              im;
                    238:
                    239:        if (htab == NULL) {
                    240:                htab = mandoc_malloc(sizeof(*htab));
                    241:                mandoc_ohash_init(htab, 4, offsetof(struct dbm_res, page));
                    242:        }
                    243:
                    244:        for (im = 0, ib = 1; im < KEY_MAX; im++, ib <<= 1) {
                    245:                if ((e->bits & ib) == 0)
                    246:                        continue;
                    247:
                    248:                switch (ib) {
                    249:                case TYPE_arch:
                    250:                        dbm_page_byarch(&e->match);
                    251:                        break;
                    252:                case TYPE_sec:
                    253:                        dbm_page_bysect(&e->match);
                    254:                        break;
                    255:                case TYPE_Nm:
                    256:                        dbm_page_byname(&e->match);
                    257:                        break;
                    258:                case TYPE_Nd:
                    259:                        dbm_page_bydesc(&e->match);
                    260:                        break;
                    261:                default:
                    262:                        dbm_page_bymacro(im - 2, &e->match);
                    263:                        break;
                    264:                }
                    265:
                    266:                /*
                    267:                 * When hashing for deduplication, use the unique
                    268:                 * page ID itself instead of a hash function;
                    269:                 * that is quite efficient.
                    270:                 */
1.29      schwarze  271:
1.51      schwarze  272:                for (;;) {
                    273:                        res = dbm_page_next();
                    274:                        if (res.page == -1)
                    275:                                break;
                    276:                        slot = ohash_lookup_memory(htab,
                    277:                            (char *)&res, sizeof(res.page), res.page);
                    278:                        if ((rp = ohash_find(htab, slot)) != NULL) {
                    279:                                rp->bits |= res.bits;
                    280:                                continue;
                    281:                        }
                    282:                        rp = mandoc_malloc(sizeof(*rp));
                    283:                        *rp = res;
                    284:                        ohash_insert(htab, slot, rp);
                    285:                }
                    286:        }
                    287:        return htab;
1.29      schwarze  288: }
                    289:
1.51      schwarze  290: static struct ohash *
                    291: manmerge_or(struct expr *e, struct ohash *htab)
                    292: {
                    293:        while (e != NULL) {
                    294:                htab = manmerge(e, htab);
                    295:                e = e->next;
                    296:        }
                    297:        return htab;
                    298: }
1.13      schwarze  299:
1.51      schwarze  300: static struct ohash *
                    301: manmerge_and(struct expr *e, struct ohash *htab)
                    302: {
                    303:        struct ohash    *hand, *h1, *h2;
                    304:        struct dbm_res  *res;
                    305:        unsigned int     slot1, slot2;
1.13      schwarze  306:
1.51      schwarze  307:        /* Evaluate the first term of the AND clause. */
1.29      schwarze  308:
1.51      schwarze  309:        hand = manmerge(e, NULL);
1.29      schwarze  310:
1.51      schwarze  311:        while ((e = e->next) != NULL) {
1.13      schwarze  312:
1.51      schwarze  313:                /* Evaluate the next term and prepare for ANDing. */
1.13      schwarze  314:
1.51      schwarze  315:                h2 = manmerge(e, NULL);
                    316:                if (ohash_entries(h2) < ohash_entries(hand)) {
                    317:                        h1 = h2;
                    318:                        h2 = hand;
                    319:                } else
                    320:                        h1 = hand;
                    321:                hand = mandoc_malloc(sizeof(*hand));
                    322:                mandoc_ohash_init(hand, 4, offsetof(struct dbm_res, page));
1.13      schwarze  323:
1.51      schwarze  324:                /* Keep all pages that are in both result sets. */
1.13      schwarze  325:
1.51      schwarze  326:                for (res = ohash_first(h1, &slot1); res != NULL;
                    327:                    res = ohash_next(h1, &slot1)) {
                    328:                        if (ohash_find(h2, ohash_lookup_memory(h2,
                    329:                            (char *)res, sizeof(res->page),
                    330:                            res->page)) == NULL)
                    331:                                free(res);
                    332:                        else
                    333:                                ohash_insert(hand, ohash_lookup_memory(hand,
                    334:                                    (char *)res, sizeof(res->page),
                    335:                                    res->page), res);
1.13      schwarze  336:                }
                    337:
1.51      schwarze  338:                /* Discard the merged results. */
                    339:
                    340:                for (res = ohash_first(h2, &slot2); res != NULL;
                    341:                    res = ohash_next(h2, &slot2))
                    342:                        free(res);
                    343:                ohash_delete(h2);
                    344:                free(h2);
                    345:                ohash_delete(h1);
                    346:                free(h1);
                    347:        }
1.13      schwarze  348:
1.51      schwarze  349:        /* Merge the result of the AND into htab. */
1.8       schwarze  350:
1.51      schwarze  351:        if (htab == NULL)
                    352:                return hand;
1.8       schwarze  353:
1.51      schwarze  354:        for (res = ohash_first(hand, &slot1); res != NULL;
                    355:            res = ohash_next(hand, &slot1)) {
                    356:                slot2 = ohash_lookup_memory(htab,
                    357:                    (char *)res, sizeof(res->page), res->page);
                    358:                if (ohash_find(htab, slot2) == NULL)
                    359:                        ohash_insert(htab, slot2, res);
                    360:                else
                    361:                        free(res);
                    362:        }
                    363:
                    364:        /* Discard the merged result. */
1.8       schwarze  365:
1.51      schwarze  366:        ohash_delete(hand);
                    367:        free(hand);
                    368:        return htab;
                    369: }
1.41      schwarze  370:
1.51      schwarze  371: void
                    372: mansearch_free(struct manpage *res, size_t sz)
                    373: {
                    374:        size_t   i;
1.41      schwarze  375:
1.51      schwarze  376:        for (i = 0; i < sz; i++) {
                    377:                free(res[i].file);
                    378:                free(res[i].names);
                    379:                free(res[i].output);
1.13      schwarze  380:        }
1.51      schwarze  381:        free(res);
                    382: }
                    383:
                    384: static int
                    385: manpage_compare(const void *vp1, const void *vp2)
                    386: {
                    387:        const struct manpage    *mp1, *mp2;
                    388:        int                      diff;
                    389:
                    390:        mp1 = vp1;
                    391:        mp2 = vp2;
                    392:        return (diff = mp2->bits - mp1->bits) ? diff :
                    393:            (diff = mp1->sec - mp2->sec) ? diff :
                    394:            strcasecmp(mp1->names, mp2->names);
1.3       schwarze  395: }
                    396:
                    397: static char *
1.51      schwarze  398: buildnames(const struct dbm_page *page)
1.3       schwarze  399: {
1.51      schwarze  400:        char    *buf;
                    401:        size_t   i, sz;
1.3       schwarze  402:
1.53      schwarze  403:        sz = lstlen(page->name, 2) + 1 + lstlen(page->sect, 2) +
                    404:            (page->arch == NULL ? 0 : 1 + lstlen(page->arch, 2)) + 2;
1.51      schwarze  405:        buf = mandoc_malloc(sz);
                    406:        i = 0;
1.53      schwarze  407:        lstcat(buf, &i, page->name, ", ");
1.51      schwarze  408:        buf[i++] = '(';
1.53      schwarze  409:        lstcat(buf, &i, page->sect, ", ");
1.51      schwarze  410:        if (page->arch != NULL) {
                    411:                buf[i++] = '/';
1.53      schwarze  412:                lstcat(buf, &i, page->arch, ", ");
1.51      schwarze  413:        }
                    414:        buf[i++] = ')';
                    415:        buf[i++] = '\0';
                    416:        assert(i == sz);
                    417:        return buf;
1.1       schwarze  418: }
                    419:
                    420: /*
1.51      schwarze  421:  * Count the buffer space needed to print the NUL-terminated
1.53      schwarze  422:  * list of NUL-terminated strings, when printing sep separator
1.51      schwarze  423:  * characters between strings.
1.1       schwarze  424:  */
1.51      schwarze  425: static size_t
1.53      schwarze  426: lstlen(const char *cp, size_t sep)
1.1       schwarze  427: {
1.51      schwarze  428:        size_t   sz;
1.1       schwarze  429:
1.51      schwarze  430:        for (sz = 0;; sz++) {
                    431:                if (cp[0] == '\0') {
                    432:                        if (cp[1] == '\0')
                    433:                                break;
1.53      schwarze  434:                        sz += sep - 1;
1.51      schwarze  435:                } else if (cp[0] < ' ')
                    436:                        sz--;
                    437:                cp++;
                    438:        }
                    439:        return sz;
1.1       schwarze  440: }
                    441:
                    442: /*
1.51      schwarze  443:  * Print the NUL-terminated list of NUL-terminated strings
1.53      schwarze  444:  * into the buffer, seperating strings with sep.
1.1       schwarze  445:  */
                    446: static void
1.53      schwarze  447: lstcat(char *buf, size_t *i, const char *cp, const char *sep)
1.1       schwarze  448: {
1.53      schwarze  449:        const char *s;
                    450:
1.51      schwarze  451:        for (;;) {
                    452:                if (cp[0] == '\0') {
                    453:                        if (cp[1] == '\0')
                    454:                                break;
1.53      schwarze  455:                        s = sep;
                    456:                        while (*s != '\0')
                    457:                                buf[(*i)++] = *s++;
1.51      schwarze  458:                } else if (cp[0] >= ' ')
                    459:                        buf[(*i)++] = cp[0];
                    460:                cp++;
                    461:        }
1.1       schwarze  462: }
                    463:
1.51      schwarze  464: /*
                    465:  * Return 1 if the string *want occurs in any of the strings
                    466:  * in the NUL-terminated string list *have, or 0 otherwise.
                    467:  * If either argument is NULL or empty, assume no filtering
                    468:  * is desired and return 1.
                    469:  */
                    470: static int
                    471: lstmatch(const char *want, const char *have)
1.4       schwarze  472: {
1.51      schwarze  473:         if (want == NULL || have == NULL || *have == '\0')
                    474:                 return 1;
                    475:         while (*have != '\0') {
                    476:                 if (strcasestr(have, want) != NULL)
                    477:                         return 1;
                    478:                 have = strchr(have, '\0') + 1;
                    479:         }
                    480:         return 0;
1.4       schwarze  481: }
                    482:
1.1       schwarze  483: /*
1.53      schwarze  484:  * Build a list of values taken by the macro im in the manual page.
1.1       schwarze  485:  */
                    486: static char *
1.53      schwarze  487: buildoutput(size_t im, struct dbm_page *page)
1.1       schwarze  488: {
1.53      schwarze  489:        const char      *oldoutput, *sep, *input;
1.51      schwarze  490:        char            *output, *newoutput, *value;
1.53      schwarze  491:        size_t           sz, i;
                    492:
                    493:        switch (im) {
                    494:        case KEY_Nd:
                    495:                return mandoc_strdup(page->desc);
                    496:        case KEY_Nm:
                    497:                input = page->name;
                    498:                break;
                    499:        case KEY_sec:
                    500:                input = page->sect;
                    501:                break;
                    502:        case KEY_arch:
                    503:                input = page->arch;
                    504:                if (input == NULL)
                    505:                        input = "all\0";
                    506:                break;
                    507:        default:
                    508:                input = NULL;
                    509:                break;
                    510:        }
                    511:
                    512:        if (input != NULL) {
                    513:                sz = lstlen(input, 3) + 1;
                    514:                output = mandoc_malloc(sz);
                    515:                i = 0;
                    516:                lstcat(output, &i, input, " # ");
1.54      schwarze  517:                output[i++] = '\0';
                    518:                assert(i == sz);
1.53      schwarze  519:                return output;
                    520:        }
1.51      schwarze  521:
                    522:        output = NULL;
1.53      schwarze  523:        dbm_macro_bypage(im - 2, page->addr);
1.51      schwarze  524:        while ((value = dbm_macro_next()) != NULL) {
                    525:                if (output == NULL) {
                    526:                        oldoutput = "";
                    527:                        sep = "";
                    528:                } else {
                    529:                        oldoutput = output;
                    530:                        sep = " # ";
                    531:                }
                    532:                mandoc_asprintf(&newoutput, "%s%s%s", oldoutput, sep, value);
                    533:                free(output);
                    534:                output = newoutput;
1.1       schwarze  535:        }
1.51      schwarze  536:        return output;
1.1       schwarze  537: }
                    538:
                    539: /*
                    540:  * Compile a set of string tokens into an expression.
                    541:  * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
                    542:  * "(", "foo=bar", etc.).
                    543:  */
                    544: static struct expr *
1.51      schwarze  545: exprcomp(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       schwarze  546: {
1.51      schwarze  547:        struct expr     *parent, *child;
                    548:        int              needterm, nested;
                    549:
                    550:        if ((nested = *argi) == argc)
                    551:                return NULL;
                    552:        needterm = 1;
                    553:        parent = child = NULL;
                    554:        while (*argi < argc) {
                    555:                if (strcmp(")", argv[*argi]) == 0) {
                    556:                        if (needterm)
                    557:                                warnx("missing term "
                    558:                                    "before closing parenthesis");
                    559:                        needterm = 0;
                    560:                        if (nested)
                    561:                                break;
                    562:                        warnx("ignoring unmatched right parenthesis");
                    563:                        ++*argi;
1.4       schwarze  564:                        continue;
1.51      schwarze  565:                }
                    566:                if (strcmp("-o", argv[*argi]) == 0) {
                    567:                        if (needterm) {
                    568:                                if (*argi > 0)
                    569:                                        warnx("ignoring -o after %s",
                    570:                                            argv[*argi - 1]);
                    571:                                else
                    572:                                        warnx("ignoring initial -o");
                    573:                        }
                    574:                        needterm = 1;
                    575:                        ++*argi;
1.4       schwarze  576:                        continue;
1.51      schwarze  577:                }
                    578:                needterm = 0;
                    579:                if (child == NULL) {
                    580:                        child = expr_and(search, argc, argv, argi);
1.4       schwarze  581:                        continue;
1.1       schwarze  582:                }
1.51      schwarze  583:                if (parent == NULL) {
                    584:                        parent = mandoc_calloc(1, sizeof(*parent));
                    585:                        parent->type = EXPR_OR;
                    586:                        parent->next = NULL;
                    587:                        parent->child = child;
                    588:                }
                    589:                child->next = expr_and(search, argc, argv, argi);
                    590:                child = child->next;
                    591:        }
                    592:        if (needterm && *argi)
                    593:                warnx("ignoring trailing %s", argv[*argi - 1]);
                    594:        return parent == NULL ? child : parent;
                    595: }
1.17      schwarze  596:
1.51      schwarze  597: static struct expr *
                    598: expr_and(const struct mansearch *search, int argc, char *argv[], int *argi)
                    599: {
                    600:        struct expr     *parent, *child;
                    601:        int              needterm;
1.17      schwarze  602:
1.51      schwarze  603:        needterm = 1;
                    604:        parent = child = NULL;
                    605:        while (*argi < argc) {
                    606:                if (strcmp(")", argv[*argi]) == 0) {
                    607:                        if (needterm)
                    608:                                warnx("missing term "
                    609:                                    "before closing parenthesis");
                    610:                        needterm = 0;
                    611:                        break;
                    612:                }
                    613:                if (strcmp("-o", argv[*argi]) == 0)
                    614:                        break;
                    615:                if (strcmp("-a", argv[*argi]) == 0) {
                    616:                        if (needterm) {
                    617:                                if (*argi > 0)
                    618:                                        warnx("ignoring -a after %s",
                    619:                                            argv[*argi - 1]);
                    620:                                else
                    621:                                        warnx("ignoring initial -a");
1.18      schwarze  622:                        }
1.51      schwarze  623:                        needterm = 1;
                    624:                        ++*argi;
                    625:                        continue;
1.18      schwarze  626:                }
1.51      schwarze  627:                if (needterm == 0)
                    628:                        break;
                    629:                if (child == NULL) {
                    630:                        child = exprterm(search, argc, argv, argi);
                    631:                        if (child != NULL)
                    632:                                needterm = 0;
                    633:                        continue;
                    634:                }
                    635:                needterm = 0;
                    636:                if (parent == NULL) {
                    637:                        parent = mandoc_calloc(1, sizeof(*parent));
                    638:                        parent->type = EXPR_AND;
                    639:                        parent->next = NULL;
                    640:                        parent->child = child;
                    641:                }
                    642:                child->next = exprterm(search, argc, argv, argi);
                    643:                if (child->next != NULL) {
                    644:                        child = child->next;
                    645:                        needterm = 0;
                    646:                }
                    647:        }
                    648:        if (needterm && *argi)
                    649:                warnx("ignoring trailing %s", argv[*argi - 1]);
                    650:        return parent == NULL ? child : parent;
1.6       schwarze  651: }
                    652:
                    653: static struct expr *
1.51      schwarze  654: exprterm(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1       schwarze  655: {
1.6       schwarze  656:        char             errbuf[BUFSIZ];
1.1       schwarze  657:        struct expr     *e;
1.28      schwarze  658:        char            *key, *val;
1.11      schwarze  659:        uint64_t         iterbit;
1.51      schwarze  660:        int              cs, i, irc;
1.1       schwarze  661:
1.51      schwarze  662:        if (strcmp("(", argv[*argi]) == 0) {
                    663:                ++*argi;
                    664:                e = exprcomp(search, argc, argv, argi);
                    665:                if (*argi < argc) {
                    666:                        assert(strcmp(")", argv[*argi]) == 0);
                    667:                        ++*argi;
                    668:                } else
                    669:                        warnx("unclosed parenthesis");
                    670:                return e;
                    671:        }
1.1       schwarze  672:
1.55      schwarze  673:        if (strcmp("-i", argv[*argi]) == 0 && *argi + 1 < argc) {
                    674:                cs = 0;
                    675:                ++*argi;
                    676:        } else
                    677:                cs = 1;
                    678:
1.51      schwarze  679:        e = mandoc_calloc(1, sizeof(*e));
                    680:        e->type = EXPR_TERM;
                    681:        e->bits = 0;
                    682:        e->next = NULL;
                    683:        e->child = NULL;
1.1       schwarze  684:
1.33      schwarze  685:        if (search->argmode == ARG_NAME) {
                    686:                e->bits = TYPE_Nm;
1.51      schwarze  687:                e->match.type = DBM_EXACT;
                    688:                e->match.str = argv[(*argi)++];
1.45      schwarze  689:                return e;
1.1       schwarze  690:        }
                    691:
                    692:        /*
1.33      schwarze  693:         * Separate macro keys from search string.
1.51      schwarze  694:         * If needed, request regular expression handling.
1.1       schwarze  695:         */
                    696:
1.33      schwarze  697:        if (search->argmode == ARG_WORD) {
                    698:                e->bits = TYPE_Nm;
1.51      schwarze  699:                e->match.type = DBM_REGEX;
                    700:                mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", argv[*argi]);
1.33      schwarze  701:                cs = 0;
1.51      schwarze  702:        } else if ((val = strpbrk(argv[*argi], "=~")) == NULL) {
1.33      schwarze  703:                e->bits = TYPE_Nm | TYPE_Nd;
1.51      schwarze  704:                e->match.type = DBM_SUB;
                    705:                e->match.str = argv[*argi];
1.28      schwarze  706:        } else {
1.51      schwarze  707:                if (val == argv[*argi])
1.33      schwarze  708:                        e->bits = TYPE_Nm | TYPE_Nd;
1.51      schwarze  709:                if (*val == '=') {
                    710:                        e->match.type = DBM_SUB;
                    711:                        e->match.str = val + 1;
                    712:                } else
                    713:                        e->match.type = DBM_REGEX;
1.28      schwarze  714:                *val++ = '\0';
1.51      schwarze  715:                if (strstr(argv[*argi], "arch") != NULL)
1.12      schwarze  716:                        cs = 0;
1.28      schwarze  717:        }
                    718:
                    719:        /* Compile regular expressions. */
                    720:
1.51      schwarze  721:        if (e->match.type == DBM_REGEX) {
                    722:                e->match.re = mandoc_malloc(sizeof(*e->match.re));
                    723:                irc = regcomp(e->match.re, val,
1.28      schwarze  724:                    REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.51      schwarze  725:                if (irc) {
                    726:                        regerror(irc, e->match.re, errbuf, sizeof(errbuf));
                    727:                        warnx("regcomp /%s/: %s", val, errbuf);
                    728:                }
1.33      schwarze  729:                if (search->argmode == ARG_WORD)
1.28      schwarze  730:                        free(val);
                    731:                if (irc) {
1.51      schwarze  732:                        free(e->match.re);
1.1       schwarze  733:                        free(e);
1.51      schwarze  734:                        ++*argi;
1.45      schwarze  735:                        return NULL;
1.1       schwarze  736:                }
1.28      schwarze  737:        }
                    738:
1.51      schwarze  739:        if (e->bits) {
                    740:                ++*argi;
1.45      schwarze  741:                return e;
1.51      schwarze  742:        }
1.1       schwarze  743:
                    744:        /*
                    745:         * Parse out all possible fields.
                    746:         * If the field doesn't resolve, bail.
                    747:         */
                    748:
1.51      schwarze  749:        while (NULL != (key = strsep(&argv[*argi], ","))) {
1.1       schwarze  750:                if ('\0' == *key)
                    751:                        continue;
1.51      schwarze  752:                for (i = 0, iterbit = 1; i < KEY_MAX; i++, iterbit <<= 1) {
                    753:                        if (0 == strcasecmp(key, mansearch_keynames[i])) {
1.11      schwarze  754:                                e->bits |= iterbit;
                    755:                                break;
                    756:                        }
                    757:                }
1.51      schwarze  758:                if (i == KEY_MAX) {
                    759:                        if (strcasecmp(key, "any"))
                    760:                                warnx("treating unknown key "
                    761:                                    "\"%s\" as \"any\"", key);
1.11      schwarze  762:                        e->bits |= ~0ULL;
1.1       schwarze  763:                }
                    764:        }
                    765:
1.51      schwarze  766:        ++*argi;
1.45      schwarze  767:        return e;
1.1       schwarze  768: }
                    769:
                    770: static void
1.51      schwarze  771: exprfree(struct expr *e)
1.1       schwarze  772: {
1.51      schwarze  773:        if (e->next != NULL)
                    774:                exprfree(e->next);
                    775:        if (e->child != NULL)
                    776:                exprfree(e->child);
                    777:        free(e);
1.1       schwarze  778: }