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

Annotation of mandoc/makewhatis.c, Revision 1.14

1.14    ! kristaps    1: /*     $Id: makewhatis.c,v 1.13 2011/07/01 10:17:24 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <sys/param.h>
                     22:
                     23: #include <assert.h>
                     24: #include <fcntl.h>
                     25: #include <getopt.h>
                     26: #include <stdio.h>
                     27: #include <stdint.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30:
1.10      kristaps   31: #ifdef __linux__
                     32: # include <db_185.h>
                     33: #else
                     34: # include <db.h>
                     35: #endif
                     36:
1.1       kristaps   37: #include "man.h"
                     38: #include "mdoc.h"
                     39: #include "mandoc.h"
                     40:
                     41: #define        MANDOC_DB        "mandoc.db"
                     42: #define        MANDOC_IDX       "mandoc.index"
                     43: #define        MANDOC_BUFSZ      BUFSIZ
                     44: #define        MANDOC_FLAGS      O_CREAT|O_TRUNC|O_RDWR
                     45:
1.5       kristaps   46: /* Bit-fields.  See makewhatis.1. */
                     47:
1.10      kristaps   48: #define TYPE_NAME        0x01
                     49: #define TYPE_FUNCTION    0x02
                     50: #define TYPE_UTILITY     0x04
                     51: #define TYPE_INCLUDES    0x08
                     52: #define TYPE_VARIABLE    0x10
                     53: #define TYPE_STANDARD    0x20
                     54: #define TYPE_AUTHOR      0x40
                     55: #define TYPE_CONFIG      0x80
                     56: #define TYPE_DESC        0x100
1.11      kristaps   57: #define TYPE_XREF        0x200
1.12      kristaps   58: #define TYPE_PATH        0x400
1.3       kristaps   59:
1.5       kristaps   60: /* Buffer for storing growable data. */
                     61:
1.3       kristaps   62: struct buf {
                     63:        char             *cp;
                     64:        size_t            len;
                     65:        size_t            size;
1.1       kristaps   66: };
                     67:
1.3       kristaps   68: #define        MAN_ARGS          DB *hash, \
                     69:                          struct buf *buf, \
1.5       kristaps   70:                          struct buf *dbuf, \
1.1       kristaps   71:                          const struct man_node *n
1.3       kristaps   72: #define        MDOC_ARGS         DB *hash, \
                     73:                          struct buf *buf, \
1.5       kristaps   74:                          struct buf *dbuf, \
1.3       kristaps   75:                          const struct mdoc_node *n, \
                     76:                          const struct mdoc_meta *m
1.1       kristaps   77:
1.13      kristaps   78: static void              buf_appendmdoc(struct buf *,
1.14    ! kristaps   79:                                const struct mdoc_node *, int);
1.5       kristaps   80: static void              buf_append(struct buf *, const char *);
                     81: static void              buf_appendb(struct buf *,
1.1       kristaps   82:                                const void *, size_t);
                     83: static void              dbt_put(DB *, const char *, DBT *, DBT *);
1.3       kristaps   84: static void              hash_put(DB *, const struct buf *, int);
1.1       kristaps   85: static int               pman_node(MAN_ARGS);
                     86: static void              pmdoc_node(MDOC_ARGS);
                     87: static void              pmdoc_An(MDOC_ARGS);
                     88: static void              pmdoc_Cd(MDOC_ARGS);
                     89: static void              pmdoc_Fd(MDOC_ARGS);
                     90: static void              pmdoc_In(MDOC_ARGS);
                     91: static void              pmdoc_Fn(MDOC_ARGS);
                     92: static void              pmdoc_Fo(MDOC_ARGS);
                     93: static void              pmdoc_Nd(MDOC_ARGS);
                     94: static void              pmdoc_Nm(MDOC_ARGS);
1.12      kristaps   95: static void              pmdoc_Pa(MDOC_ARGS);
1.1       kristaps   96: static void              pmdoc_St(MDOC_ARGS);
                     97: static void              pmdoc_Vt(MDOC_ARGS);
1.11      kristaps   98: static void              pmdoc_Xr(MDOC_ARGS);
1.5       kristaps   99: static void              usage(void);
1.1       kristaps  100:
                    101: typedef        void            (*pmdoc_nf)(MDOC_ARGS);
                    102:
                    103: static const pmdoc_nf    mdocs[MDOC_MAX] = {
                    104:        NULL, /* Ap */
                    105:        NULL, /* Dd */
                    106:        NULL, /* Dt */
                    107:        NULL, /* Os */
                    108:        NULL, /* Sh */
                    109:        NULL, /* Ss */
                    110:        NULL, /* Pp */
                    111:        NULL, /* D1 */
                    112:        NULL, /* Dl */
                    113:        NULL, /* Bd */
                    114:        NULL, /* Ed */
                    115:        NULL, /* Bl */
                    116:        NULL, /* El */
                    117:        NULL, /* It */
                    118:        NULL, /* Ad */
                    119:        pmdoc_An, /* An */
                    120:        NULL, /* Ar */
                    121:        pmdoc_Cd, /* Cd */
                    122:        NULL, /* Cm */
                    123:        NULL, /* Dv */
                    124:        NULL, /* Er */
                    125:        NULL, /* Ev */
                    126:        NULL, /* Ex */
                    127:        NULL, /* Fa */
                    128:        pmdoc_Fd, /* Fd */
                    129:        NULL, /* Fl */
                    130:        pmdoc_Fn, /* Fn */
                    131:        NULL, /* Ft */
                    132:        NULL, /* Ic */
                    133:        pmdoc_In, /* In */
                    134:        NULL, /* Li */
                    135:        pmdoc_Nd, /* Nd */
                    136:        pmdoc_Nm, /* Nm */
                    137:        NULL, /* Op */
                    138:        NULL, /* Ot */
1.12      kristaps  139:        pmdoc_Pa, /* Pa */
1.1       kristaps  140:        NULL, /* Rv */
                    141:        pmdoc_St, /* St */
                    142:        pmdoc_Vt, /* Va */
                    143:        pmdoc_Vt, /* Vt */
1.11      kristaps  144:        pmdoc_Xr, /* Xr */
1.1       kristaps  145:        NULL, /* %A */
                    146:        NULL, /* %B */
                    147:        NULL, /* %D */
                    148:        NULL, /* %I */
                    149:        NULL, /* %J */
                    150:        NULL, /* %N */
                    151:        NULL, /* %O */
                    152:        NULL, /* %P */
                    153:        NULL, /* %R */
                    154:        NULL, /* %T */
                    155:        NULL, /* %V */
                    156:        NULL, /* Ac */
                    157:        NULL, /* Ao */
                    158:        NULL, /* Aq */
                    159:        NULL, /* At */
                    160:        NULL, /* Bc */
                    161:        NULL, /* Bf */
                    162:        NULL, /* Bo */
                    163:        NULL, /* Bq */
                    164:        NULL, /* Bsx */
                    165:        NULL, /* Bx */
                    166:        NULL, /* Db */
                    167:        NULL, /* Dc */
                    168:        NULL, /* Do */
                    169:        NULL, /* Dq */
                    170:        NULL, /* Ec */
                    171:        NULL, /* Ef */
                    172:        NULL, /* Em */
                    173:        NULL, /* Eo */
                    174:        NULL, /* Fx */
                    175:        NULL, /* Ms */
                    176:        NULL, /* No */
                    177:        NULL, /* Ns */
                    178:        NULL, /* Nx */
                    179:        NULL, /* Ox */
                    180:        NULL, /* Pc */
                    181:        NULL, /* Pf */
                    182:        NULL, /* Po */
                    183:        NULL, /* Pq */
                    184:        NULL, /* Qc */
                    185:        NULL, /* Ql */
                    186:        NULL, /* Qo */
                    187:        NULL, /* Qq */
                    188:        NULL, /* Re */
                    189:        NULL, /* Rs */
                    190:        NULL, /* Sc */
                    191:        NULL, /* So */
                    192:        NULL, /* Sq */
                    193:        NULL, /* Sm */
                    194:        NULL, /* Sx */
                    195:        NULL, /* Sy */
                    196:        NULL, /* Tn */
                    197:        NULL, /* Ux */
                    198:        NULL, /* Xc */
                    199:        NULL, /* Xo */
                    200:        pmdoc_Fo, /* Fo */
                    201:        NULL, /* Fc */
                    202:        NULL, /* Oo */
                    203:        NULL, /* Oc */
                    204:        NULL, /* Bk */
                    205:        NULL, /* Ek */
                    206:        NULL, /* Bt */
                    207:        NULL, /* Hf */
                    208:        NULL, /* Fr */
                    209:        NULL, /* Ud */
                    210:        NULL, /* Lb */
                    211:        NULL, /* Lp */
                    212:        NULL, /* Lk */
                    213:        NULL, /* Mt */
                    214:        NULL, /* Brq */
                    215:        NULL, /* Bro */
                    216:        NULL, /* Brc */
                    217:        NULL, /* %C */
                    218:        NULL, /* Es */
                    219:        NULL, /* En */
                    220:        NULL, /* Dx */
                    221:        NULL, /* %Q */
                    222:        NULL, /* br */
                    223:        NULL, /* sp */
                    224:        NULL, /* %U */
                    225:        NULL, /* Ta */
                    226: };
                    227:
1.5       kristaps  228: static const char       *progname;
                    229:
1.1       kristaps  230: int
                    231: main(int argc, char *argv[])
                    232: {
                    233:        struct mparse   *mp; /* parse sequence */
                    234:        struct mdoc     *mdoc; /* resulting mdoc */
                    235:        struct man      *man; /* resulting man */
                    236:        char            *fn; /* current file being parsed */
                    237:        const char      *msec, /* manual section */
                    238:                        *mtitle, /* manual title */
                    239:                        *arch, /* manual architecture */
                    240:                        *dir; /* result dir (default: cwd) */
                    241:        char             ibuf[MAXPATHLEN], /* index fname */
                    242:                         ibbuf[MAXPATHLEN], /* index backup fname */
                    243:                         fbuf[MAXPATHLEN],  /* btree fname */
1.3       kristaps  244:                         fbbuf[MAXPATHLEN], /* btree backup fname */
                    245:                         vbuf[8]; /* stringified record number */
1.10      kristaps  246:        int              ch, seq, verb;
1.1       kristaps  247:        DB              *idx, /* index database */
1.3       kristaps  248:                        *db, /* keyword database */
                    249:                        *hash; /* temporary keyword hashtable */
1.5       kristaps  250:        DBT              key, val;
1.9       kristaps  251:        size_t           sv;
1.1       kristaps  252:        BTREEINFO        info; /* btree configuration */
                    253:        recno_t          rec; /* current record number */
1.5       kristaps  254:        struct buf       buf, /* keyword buffer */
                    255:                         dbuf; /* description buffer */
1.1       kristaps  256:        extern int       optind;
                    257:        extern char     *optarg;
                    258:
                    259:        progname = strrchr(argv[0], '/');
                    260:        if (progname == NULL)
                    261:                progname = argv[0];
                    262:        else
                    263:                ++progname;
                    264:
                    265:        dir = "";
1.10      kristaps  266:        verb = 0;
1.1       kristaps  267:
1.10      kristaps  268:        while (-1 != (ch = getopt(argc, argv, "d:v")))
1.1       kristaps  269:                switch (ch) {
                    270:                case ('d'):
                    271:                        dir = optarg;
                    272:                        break;
1.10      kristaps  273:                case ('v'):
                    274:                        verb++;
                    275:                        break;
1.1       kristaps  276:                default:
                    277:                        usage();
                    278:                        return((int)MANDOCLEVEL_BADARG);
                    279:                }
                    280:
                    281:        argc -= optind;
                    282:        argv += optind;
                    283:
                    284:        /*
                    285:         * Set up temporary file-names into which we're going to write
                    286:         * all of our data (both for the index and database).  These
                    287:         * will be securely renamed to the real file-names after we've
                    288:         * written all of our data.
                    289:         */
                    290:
                    291:        ibuf[0] = ibuf[MAXPATHLEN - 2] =
                    292:                ibbuf[0] = ibbuf[MAXPATHLEN - 2] =
                    293:                fbuf[0] = fbuf[MAXPATHLEN - 2] =
                    294:                fbbuf[0] = fbbuf[MAXPATHLEN - 2] = '\0';
                    295:
                    296:        strlcat(fbuf, dir, MAXPATHLEN);
                    297:        strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
                    298:
                    299:        strlcat(fbbuf, fbuf, MAXPATHLEN);
                    300:        strlcat(fbbuf, "~", MAXPATHLEN);
                    301:
                    302:        strlcat(ibuf, dir, MAXPATHLEN);
                    303:        strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
                    304:
                    305:        strlcat(ibbuf, ibuf, MAXPATHLEN);
                    306:        strlcat(ibbuf, "~", MAXPATHLEN);
                    307:
                    308:        if ('\0' != fbuf[MAXPATHLEN - 2] ||
                    309:                        '\0' != fbbuf[MAXPATHLEN - 2] ||
                    310:                        '\0' != ibuf[MAXPATHLEN - 2] ||
                    311:                        '\0' != ibbuf[MAXPATHLEN - 2]) {
1.10      kristaps  312:                fprintf(stderr, "%s: Path too long\n", dir);
1.1       kristaps  313:                exit((int)MANDOCLEVEL_SYSERR);
                    314:        }
                    315:
                    316:        /*
                    317:         * For the keyword database, open a BTREE database that allows
1.3       kristaps  318:         * duplicates.
                    319:         * For the index database, use a standard RECNO database type.
1.1       kristaps  320:         */
                    321:
                    322:        memset(&info, 0, sizeof(BTREEINFO));
                    323:        info.flags = R_DUP;
                    324:        db = dbopen(fbbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
                    325:
                    326:        if (NULL == db) {
                    327:                perror(fbbuf);
                    328:                exit((int)MANDOCLEVEL_SYSERR);
                    329:        }
                    330:
                    331:        idx = dbopen(ibbuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);
                    332:
                    333:        if (NULL == db) {
                    334:                perror(ibbuf);
                    335:                (*db->close)(db);
                    336:                exit((int)MANDOCLEVEL_SYSERR);
                    337:        }
                    338:
                    339:        /*
1.10      kristaps  340:         * Try parsing each manual given on the command line.
                    341:         * If we fail, then emit an error and keep on going.
                    342:         * Take resulting trees and push them down into the database code.
1.1       kristaps  343:         * Use the auto-parser and don't report any errors.
                    344:         */
                    345:
                    346:        mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
                    347:
                    348:        rec = 1;
1.9       kristaps  349:        hash = NULL;
1.3       kristaps  350:
                    351:        memset(&buf, 0, sizeof(struct buf));
1.5       kristaps  352:        memset(&dbuf, 0, sizeof(struct buf));
                    353:
                    354:        buf.size = dbuf.size = MANDOC_BUFSZ;
1.3       kristaps  355:
                    356:        buf.cp = mandoc_malloc(buf.size);
1.5       kristaps  357:        dbuf.cp = mandoc_malloc(dbuf.size);
1.1       kristaps  358:
                    359:        while (NULL != (fn = *argv++)) {
                    360:                mparse_reset(mp);
                    361:
1.10      kristaps  362:                /* Initialise the in-memory hash of keywords. */
                    363:
1.8       kristaps  364:                if (hash)
                    365:                        (*hash->close)(hash);
                    366:
                    367:                hash = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL);
                    368:
                    369:                if (NULL == hash) {
                    370:                        perror("hash");
                    371:                        exit((int)MANDOCLEVEL_SYSERR);
                    372:                }
                    373:
1.1       kristaps  374:                /* Parse and get (non-empty) AST. */
                    375:
                    376:                if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {
                    377:                        fprintf(stderr, "%s: Parse failure\n", fn);
                    378:                        continue;
                    379:                }
1.10      kristaps  380:
1.1       kristaps  381:                mparse_result(mp, &mdoc, &man);
1.10      kristaps  382:
1.1       kristaps  383:                if (NULL == mdoc && NULL == man)
                    384:                        continue;
                    385:
                    386:                msec = NULL != mdoc ?
                    387:                        mdoc_meta(mdoc)->msec :
                    388:                        man_meta(man)->msec;
                    389:                mtitle = NULL != mdoc ?
                    390:                        mdoc_meta(mdoc)->title :
                    391:                        man_meta(man)->title;
                    392:                arch = NULL != mdoc ? mdoc_meta(mdoc)->arch : NULL;
                    393:
                    394:                /*
                    395:                 * The index record value consists of a nil-terminated
                    396:                 * filename, a nil-terminated manual section, and a
                    397:                 * nil-terminated description.  Since the description
                    398:                 * may not be set, we set a sentinel to see if we're
                    399:                 * going to write a nil byte in its place.
                    400:                 */
                    401:
1.5       kristaps  402:                dbuf.len = 0;
                    403:                buf_appendb(&dbuf, fn, strlen(fn) + 1);
                    404:                buf_appendb(&dbuf, msec, strlen(msec) + 1);
                    405:                buf_appendb(&dbuf, mtitle, strlen(mtitle) + 1);
                    406:                buf_appendb(&dbuf, arch ? arch : "",
1.1       kristaps  407:                                arch ? strlen(arch) + 1 : 1);
                    408:
1.5       kristaps  409:                sv = dbuf.len;
1.1       kristaps  410:
                    411:                /* Fix the record number in the btree value. */
                    412:
                    413:                if (mdoc)
1.5       kristaps  414:                        pmdoc_node(hash, &buf, &dbuf,
                    415:                                mdoc_node(mdoc), mdoc_meta(mdoc));
1.1       kristaps  416:                else
1.5       kristaps  417:                        pman_node(hash, &buf, &dbuf, man_node(man));
1.3       kristaps  418:
                    419:                /*
                    420:                 * Copy from the in-memory hashtable of pending keywords
                    421:                 * into the database.
                    422:                 */
                    423:
                    424:                memset(vbuf, 0, sizeof(uint32_t));
                    425:                memcpy(vbuf + 4, &rec, sizeof(uint32_t));
                    426:
                    427:                seq = R_FIRST;
                    428:                while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
1.7       kristaps  429:                        seq = R_NEXT;
                    430:
1.3       kristaps  431:                        memcpy(vbuf, val.data, sizeof(uint32_t));
                    432:                        val.size = sizeof(vbuf);
                    433:                        val.data = vbuf;
1.7       kristaps  434:
1.10      kristaps  435:                        if (verb > 1)
                    436:                                printf("%s: Keyword %s (%zu): 0x%x\n",
                    437:                                        fn, (char *)key.data, key.size,
1.8       kristaps  438:                                        *(int *)val.data);
                    439:
1.3       kristaps  440:                        dbt_put(db, fbbuf, &key, &val);
1.5       kristaps  441:
1.3       kristaps  442:                }
                    443:
                    444:                if (ch < 0) {
                    445:                        perror("hash");
                    446:                        exit((int)MANDOCLEVEL_SYSERR);
                    447:                }
1.1       kristaps  448:
                    449:                /*
1.3       kristaps  450:                 * Apply to the index.  If we haven't had a description
                    451:                 * set, put an empty one in now.
1.1       kristaps  452:                 */
                    453:
1.5       kristaps  454:                if (dbuf.len == sv)
                    455:                        buf_appendb(&dbuf, "", 1);
                    456:
                    457:                key.data = &rec;
                    458:                key.size = sizeof(recno_t);
1.1       kristaps  459:
1.5       kristaps  460:                val.data = dbuf.cp;
                    461:                val.size = dbuf.len;
1.1       kristaps  462:
1.10      kristaps  463:                if (verb > 0)
                    464:                        printf("%s: Indexed\n", fn);
1.8       kristaps  465:
1.5       kristaps  466:                dbt_put(idx, ibbuf, &key, &val);
1.1       kristaps  467:                rec++;
                    468:        }
                    469:
                    470:        (*db->close)(db);
                    471:        (*idx->close)(idx);
1.10      kristaps  472:
1.8       kristaps  473:        if (hash)
                    474:                (*hash->close)(hash);
1.1       kristaps  475:
                    476:        mparse_free(mp);
                    477:
1.3       kristaps  478:        free(buf.cp);
1.5       kristaps  479:        free(dbuf.cp);
1.1       kristaps  480:
                    481:        /* Atomically replace the file with our temporary one. */
                    482:
                    483:        if (-1 == rename(fbbuf, fbuf))
                    484:                perror(fbuf);
                    485:        if (-1 == rename(ibbuf, ibuf))
                    486:                perror(fbuf);
                    487:
                    488:        return((int)MANDOCLEVEL_OK);
                    489: }
                    490:
                    491: /*
1.5       kristaps  492:  * Grow the buffer (if necessary) and copy in a binary string.
1.1       kristaps  493:  */
                    494: static void
1.3       kristaps  495: buf_appendb(struct buf *buf, const void *cp, size_t sz)
                    496: {
                    497:
                    498:        /* Overshoot by MANDOC_BUFSZ. */
                    499:
                    500:        while (buf->len + sz >= buf->size) {
                    501:                buf->size = buf->len + sz + MANDOC_BUFSZ;
                    502:                buf->cp = mandoc_realloc(buf->cp, buf->size);
                    503:        }
                    504:
                    505:        memcpy(buf->cp + (int)buf->len, cp, sz);
                    506:        buf->len += sz;
                    507: }
                    508:
1.1       kristaps  509: /*
1.5       kristaps  510:  * Append a nil-terminated string to the buffer.
                    511:  * This can be invoked multiple times.
                    512:  * The buffer string will be nil-terminated.
                    513:  * If invoked multiple times, a space is put between strings.
1.1       kristaps  514:  */
                    515: static void
1.3       kristaps  516: buf_append(struct buf *buf, const char *cp)
                    517: {
                    518:        size_t           sz;
                    519:
                    520:        if (0 == (sz = strlen(cp)))
                    521:                return;
                    522:
                    523:        if (buf->len)
                    524:                buf->cp[(int)buf->len - 1] = ' ';
                    525:
                    526:        buf_appendb(buf, cp, sz + 1);
                    527: }
                    528:
1.13      kristaps  529: /*
                    530:  * Recursively add all text from a given node.
                    531:  * This is optimised for general mdoc nodes in this context, which do
                    532:  * not consist of subexpressions and having a recursive call for n->next
                    533:  * would be wasteful.
1.14    ! kristaps  534:  * The "f" variable should be 0 unless called from pmdoc_Nd for the
        !           535:  * description buffer, which does not start at the beginning of the
        !           536:  * buffer.
1.13      kristaps  537:  */
                    538: static void
1.14    ! kristaps  539: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
1.13      kristaps  540: {
                    541:
                    542:        for ( ; n; n = n->next) {
                    543:                if (n->child)
1.14    ! kristaps  544:                        buf_appendmdoc(buf, n->child, f);
        !           545:
        !           546:                if (MDOC_TEXT == n->type && f) {
        !           547:                        f = 0;
        !           548:                        buf_appendb(buf, n->string,
        !           549:                                        strlen(n->string) + 1);
        !           550:                } else if (MDOC_TEXT == n->type)
1.13      kristaps  551:                        buf_append(buf, n->string);
1.14    ! kristaps  552:
1.13      kristaps  553:        }
                    554: }
                    555:
1.1       kristaps  556: /* ARGSUSED */
                    557: static void
                    558: pmdoc_An(MDOC_ARGS)
                    559: {
                    560:
                    561:        if (SEC_AUTHORS != n->sec)
                    562:                return;
                    563:
1.14    ! kristaps  564:        buf_appendmdoc(buf, n->child, 0);
1.3       kristaps  565:        hash_put(hash, buf, TYPE_AUTHOR);
1.1       kristaps  566: }
                    567:
                    568: /* ARGSUSED */
                    569: static void
                    570: pmdoc_Fd(MDOC_ARGS)
                    571: {
                    572:        const char      *start, *end;
                    573:        size_t           sz;
                    574:
                    575:        if (SEC_SYNOPSIS != n->sec)
                    576:                return;
                    577:        if (NULL == (n = n->child) || MDOC_TEXT != n->type)
                    578:                return;
                    579:
                    580:        /*
                    581:         * Only consider those `Fd' macro fields that begin with an
                    582:         * "inclusion" token (versus, e.g., #define).
                    583:         */
                    584:        if (strcmp("#include", n->string))
                    585:                return;
                    586:
                    587:        if (NULL == (n = n->next) || MDOC_TEXT != n->type)
                    588:                return;
                    589:
                    590:        /*
                    591:         * Strip away the enclosing angle brackets and make sure we're
                    592:         * not zero-length.
                    593:         */
                    594:
                    595:        start = n->string;
                    596:        if ('<' == *start || '"' == *start)
                    597:                start++;
                    598:
                    599:        if (0 == (sz = strlen(start)))
                    600:                return;
                    601:
                    602:        end = &start[(int)sz - 1];
                    603:        if ('>' == *end || '"' == *end)
                    604:                end--;
                    605:
                    606:        assert(end >= start);
                    607:
1.3       kristaps  608:        buf_appendb(buf, start, (size_t)(end - start + 1));
                    609:        buf_appendb(buf, "", 1);
                    610:
                    611:        hash_put(hash, buf, TYPE_INCLUDES);
1.1       kristaps  612: }
                    613:
                    614: /* ARGSUSED */
                    615: static void
                    616: pmdoc_Cd(MDOC_ARGS)
                    617: {
                    618:
                    619:        if (SEC_SYNOPSIS != n->sec)
                    620:                return;
                    621:
1.14    ! kristaps  622:        buf_appendmdoc(buf, n->child, 0);
1.3       kristaps  623:        hash_put(hash, buf, TYPE_CONFIG);
1.1       kristaps  624: }
                    625:
                    626: /* ARGSUSED */
                    627: static void
                    628: pmdoc_In(MDOC_ARGS)
                    629: {
                    630:
                    631:        if (SEC_SYNOPSIS != n->sec)
                    632:                return;
                    633:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    634:                return;
                    635:
1.3       kristaps  636:        buf_append(buf, n->child->string);
                    637:        hash_put(hash, buf, TYPE_INCLUDES);
1.1       kristaps  638: }
                    639:
                    640: /* ARGSUSED */
                    641: static void
                    642: pmdoc_Fn(MDOC_ARGS)
                    643: {
                    644:        const char      *cp;
                    645:
                    646:        if (SEC_SYNOPSIS != n->sec)
                    647:                return;
                    648:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    649:                return;
                    650:
                    651:        /* .Fn "struct type *arg" "foo" */
                    652:
                    653:        cp = strrchr(n->child->string, ' ');
                    654:        if (NULL == cp)
                    655:                cp = n->child->string;
                    656:
                    657:        /* Strip away pointer symbol. */
                    658:
                    659:        while ('*' == *cp)
                    660:                cp++;
                    661:
1.3       kristaps  662:        buf_append(buf, cp);
                    663:        hash_put(hash, buf, TYPE_FUNCTION);
1.1       kristaps  664: }
                    665:
                    666: /* ARGSUSED */
                    667: static void
                    668: pmdoc_St(MDOC_ARGS)
                    669: {
                    670:
                    671:        if (SEC_STANDARDS != n->sec)
                    672:                return;
                    673:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    674:                return;
                    675:
1.3       kristaps  676:        buf_append(buf, n->child->string);
                    677:        hash_put(hash, buf, TYPE_STANDARD);
1.11      kristaps  678: }
                    679:
                    680: /* ARGSUSED */
                    681: static void
                    682: pmdoc_Xr(MDOC_ARGS)
                    683: {
                    684:
                    685:        if (NULL == (n = n->child))
                    686:                return;
                    687:
                    688:        buf_appendb(buf, n->string, strlen(n->string));
                    689:
                    690:        if (NULL != (n = n->next)) {
                    691:                buf_appendb(buf, ".", 1);
                    692:                buf_appendb(buf, n->string, strlen(n->string) + 1);
                    693:        } else
                    694:                buf_appendb(buf, ".", 2);
                    695:
                    696:        hash_put(hash, buf, TYPE_XREF);
1.1       kristaps  697: }
                    698:
                    699: /* ARGSUSED */
                    700: static void
                    701: pmdoc_Vt(MDOC_ARGS)
                    702: {
                    703:        const char      *start;
                    704:        size_t           sz;
                    705:
                    706:        if (SEC_SYNOPSIS != n->sec)
                    707:                return;
                    708:        if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
                    709:                return;
                    710:        if (NULL == n->last || MDOC_TEXT != n->last->type)
                    711:                return;
                    712:
                    713:        /*
                    714:         * Strip away leading pointer symbol '*' and trailing ';'.
                    715:         */
                    716:
                    717:        start = n->last->string;
                    718:
                    719:        while ('*' == *start)
                    720:                start++;
                    721:
                    722:        if (0 == (sz = strlen(start)))
                    723:                return;
                    724:
                    725:        if (';' == start[(int)sz - 1])
                    726:                sz--;
                    727:
                    728:        if (0 == sz)
                    729:                return;
                    730:
1.3       kristaps  731:        buf_appendb(buf, start, sz);
                    732:        buf_appendb(buf, "", 1);
                    733:        hash_put(hash, buf, TYPE_VARIABLE);
1.1       kristaps  734: }
                    735:
                    736: /* ARGSUSED */
                    737: static void
                    738: pmdoc_Fo(MDOC_ARGS)
                    739: {
                    740:
                    741:        if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                    742:                return;
                    743:        if (NULL == n->child || MDOC_TEXT != n->child->type)
                    744:                return;
                    745:
1.3       kristaps  746:        buf_append(buf, n->child->string);
                    747:        hash_put(hash, buf, TYPE_FUNCTION);
1.1       kristaps  748: }
                    749:
                    750:
                    751: /* ARGSUSED */
                    752: static void
                    753: pmdoc_Nd(MDOC_ARGS)
                    754: {
1.6       kristaps  755:
1.13      kristaps  756:        if (MDOC_BODY != n->type)
                    757:                return;
1.6       kristaps  758:
1.14    ! kristaps  759:        buf_appendmdoc(dbuf, n->child, 1);
        !           760:        buf_appendmdoc(buf, n->child, 0);
1.6       kristaps  761:
                    762:        hash_put(hash, buf, TYPE_DESC);
1.12      kristaps  763: }
                    764:
                    765: /* ARGSUSED */
                    766: static void
                    767: pmdoc_Pa(MDOC_ARGS)
                    768: {
                    769:
                    770:        if (SEC_FILES != n->sec)
                    771:                return;
                    772:
1.14    ! kristaps  773:        buf_appendmdoc(buf, n->child, 0);
1.12      kristaps  774:        hash_put(hash, buf, TYPE_PATH);
1.1       kristaps  775: }
                    776:
                    777: /* ARGSUSED */
                    778: static void
                    779: pmdoc_Nm(MDOC_ARGS)
                    780: {
                    781:
                    782:        if (SEC_NAME == n->sec) {
1.14    ! kristaps  783:                buf_appendmdoc(buf, n->child, 0);
1.3       kristaps  784:                hash_put(hash, buf, TYPE_NAME);
1.1       kristaps  785:                return;
                    786:        } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
                    787:                return;
                    788:
1.3       kristaps  789:        if (NULL == n->child)
                    790:                buf_append(buf, m->name);
                    791:
1.14    ! kristaps  792:        buf_appendmdoc(buf, n->child, 0);
1.3       kristaps  793:        hash_put(hash, buf, TYPE_UTILITY);
                    794: }
                    795:
                    796: static void
                    797: hash_put(DB *db, const struct buf *buf, int mask)
                    798: {
                    799:        DBT              key, val;
                    800:        int              rc;
                    801:
1.7       kristaps  802:        if (buf->len < 2)
                    803:                return;
                    804:
1.3       kristaps  805:        key.data = buf->cp;
1.7       kristaps  806:        key.size = buf->len;
1.3       kristaps  807:
                    808:        if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                    809:                perror("hash");
                    810:                exit((int)MANDOCLEVEL_SYSERR);
                    811:        } else if (0 == rc)
                    812:                mask |= *(int *)val.data;
                    813:
                    814:        val.data = &mask;
                    815:        val.size = sizeof(int);
1.1       kristaps  816:
1.3       kristaps  817:        if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                    818:                perror("hash");
                    819:                exit((int)MANDOCLEVEL_SYSERR);
                    820:        }
1.1       kristaps  821: }
                    822:
                    823: static void
                    824: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
                    825: {
                    826:
1.5       kristaps  827:        assert(key->size);
1.1       kristaps  828:        assert(val->size);
                    829:
                    830:        if (0 == (*db->put)(db, key, val, 0))
                    831:                return;
                    832:
                    833:        perror(dbn);
                    834:        exit((int)MANDOCLEVEL_SYSERR);
                    835:        /* NOTREACHED */
                    836: }
                    837:
                    838: /*
                    839:  * Call out to per-macro handlers after clearing the persistent database
                    840:  * key.  If the macro sets the database key, flush it to the database.
                    841:  */
                    842: static void
                    843: pmdoc_node(MDOC_ARGS)
                    844: {
                    845:
                    846:        if (NULL == n)
                    847:                return;
                    848:
                    849:        switch (n->type) {
                    850:        case (MDOC_HEAD):
                    851:                /* FALLTHROUGH */
                    852:        case (MDOC_BODY):
                    853:                /* FALLTHROUGH */
                    854:        case (MDOC_TAIL):
                    855:                /* FALLTHROUGH */
                    856:        case (MDOC_BLOCK):
                    857:                /* FALLTHROUGH */
                    858:        case (MDOC_ELEM):
                    859:                if (NULL == mdocs[n->tok])
                    860:                        break;
                    861:
1.3       kristaps  862:                buf->len = 0;
1.5       kristaps  863:                (*mdocs[n->tok])(hash, buf, dbuf, n, m);
1.1       kristaps  864:                break;
                    865:        default:
                    866:                break;
                    867:        }
                    868:
1.5       kristaps  869:        pmdoc_node(hash, buf, dbuf, n->child, m);
                    870:        pmdoc_node(hash, buf, dbuf, n->next, m);
1.1       kristaps  871: }
                    872:
                    873: static int
                    874: pman_node(MAN_ARGS)
                    875: {
                    876:        const struct man_node *head, *body;
                    877:        const char      *start, *sv;
                    878:        size_t           sz;
                    879:
                    880:        if (NULL == n)
                    881:                return(0);
                    882:
                    883:        /*
                    884:         * We're only searching for one thing: the first text child in
                    885:         * the BODY of a NAME section.  Since we don't keep track of
                    886:         * sections in -man, run some hoops to find out whether we're in
                    887:         * the correct section or not.
                    888:         */
                    889:
                    890:        if (MAN_BODY == n->type && MAN_SH == n->tok) {
                    891:                body = n;
                    892:                assert(body->parent);
                    893:                if (NULL != (head = body->parent->head) &&
                    894:                                1 == head->nchild &&
                    895:                                NULL != (head = (head->child)) &&
                    896:                                MAN_TEXT == head->type &&
                    897:                                0 == strcmp(head->string, "NAME") &&
                    898:                                NULL != (body = body->child) &&
                    899:                                MAN_TEXT == body->type) {
                    900:
                    901:                        assert(body->string);
                    902:                        start = sv = body->string;
                    903:
                    904:                        /*
                    905:                         * Go through a special heuristic dance here.
                    906:                         * This is why -man manuals are great!
                    907:                         * (I'm being sarcastic: my eyes are bleeding.)
                    908:                         * Conventionally, one or more manual names are
                    909:                         * comma-specified prior to a whitespace, then a
                    910:                         * dash, then a description.  Try to puzzle out
                    911:                         * the name parts here.
                    912:                         */
                    913:
                    914:                        for ( ;; ) {
                    915:                                sz = strcspn(start, " ,");
                    916:                                if ('\0' == start[(int)sz])
                    917:                                        break;
                    918:
1.3       kristaps  919:                                buf->len = 0;
                    920:                                buf_appendb(buf, start, sz);
                    921:                                buf_appendb(buf, "", 1);
1.1       kristaps  922:
1.3       kristaps  923:                                hash_put(hash, buf, TYPE_NAME);
1.1       kristaps  924:
                    925:                                if (' ' == start[(int)sz]) {
                    926:                                        start += (int)sz + 1;
                    927:                                        break;
                    928:                                }
                    929:
                    930:                                assert(',' == start[(int)sz]);
                    931:                                start += (int)sz + 1;
                    932:                                while (' ' == *start)
                    933:                                        start++;
                    934:                        }
                    935:
                    936:                        if (sv == start) {
1.3       kristaps  937:                                buf->len = 0;
                    938:                                buf_append(buf, start);
1.1       kristaps  939:                                return(1);
                    940:                        }
                    941:
                    942:                        while (' ' == *start)
                    943:                                start++;
                    944:
                    945:                        if (0 == strncmp(start, "-", 1))
                    946:                                start += 1;
                    947:                        else if (0 == strncmp(start, "\\-", 2))
                    948:                                start += 2;
                    949:                        else if (0 == strncmp(start, "\\(en", 4))
                    950:                                start += 4;
                    951:                        else if (0 == strncmp(start, "\\(em", 4))
                    952:                                start += 4;
                    953:
                    954:                        while (' ' == *start)
                    955:                                start++;
                    956:
1.6       kristaps  957:                        sz = strlen(start) + 1;
                    958:                        buf_appendb(dbuf, start, sz);
                    959:                        buf_appendb(buf, start, sz);
1.1       kristaps  960:                }
                    961:        }
                    962:
1.5       kristaps  963:        if (pman_node(hash, buf, dbuf, n->child))
1.1       kristaps  964:                return(1);
1.5       kristaps  965:        if (pman_node(hash, buf, dbuf, n->next))
1.1       kristaps  966:                return(1);
                    967:
                    968:        return(0);
                    969: }
                    970:
                    971: static void
                    972: usage(void)
                    973: {
                    974:
1.10      kristaps  975:        fprintf(stderr, "usage: %s [-v] [-d path] [file...]\n",
                    976:                        progname);
1.1       kristaps  977: }

CVSweb