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

Annotation of src/usr.bin/mandoc/mdoc_action.c, Revision 1.40

1.40    ! schwarze    1: /*     $Id: mdoc_action.c,v 1.39 2010/06/06 18:08:41 schwarze Exp $ */
1.1       kristaps    2: /*
1.2       schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.2       schwarze    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.2       schwarze    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.
1.1       kristaps   16:  */
1.24      schwarze   17: #ifndef        OSNAME
1.1       kristaps   18: #include <sys/utsname.h>
1.24      schwarze   19: #endif
1.1       kristaps   20:
                     21: #include <assert.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
1.24      schwarze   25: #include <time.h>
1.1       kristaps   26:
1.36      schwarze   27: #include "mandoc.h"
1.1       kristaps   28: #include "libmdoc.h"
1.25      schwarze   29: #include "libmandoc.h"
1.1       kristaps   30:
1.20      schwarze   31: #define        POST_ARGS struct mdoc *m, struct mdoc_node *n
1.37      schwarze   32: #define        PRE_ARGS  struct mdoc *m, struct mdoc_node *n
1.1       kristaps   33:
1.25      schwarze   34: #define        NUMSIZ    32
                     35: #define        DATESIZ   32
                     36:
1.1       kristaps   37: struct actions {
                     38:        int     (*pre)(PRE_ARGS);
                     39:        int     (*post)(POST_ARGS);
                     40: };
                     41:
1.25      schwarze   42: static int       concat(struct mdoc *, char *,
                     43:                        const struct mdoc_node *, size_t);
1.31      schwarze   44: static inline int order_rs(enum mdoct);
1.23      schwarze   45:
1.1       kristaps   46: static int       post_ar(POST_ARGS);
1.15      schwarze   47: static int       post_at(POST_ARGS);
1.1       kristaps   48: static int       post_bl(POST_ARGS);
1.6       schwarze   49: static int       post_bl_head(POST_ARGS);
1.15      schwarze   50: static int       post_bl_tagwidth(POST_ARGS);
1.1       kristaps   51: static int       post_bl_width(POST_ARGS);
                     52: static int       post_dd(POST_ARGS);
                     53: static int       post_display(POST_ARGS);
                     54: static int       post_dt(POST_ARGS);
1.15      schwarze   55: static int       post_lb(POST_ARGS);
1.39      schwarze   56: static int       post_li(POST_ARGS);
1.1       kristaps   57: static int       post_nm(POST_ARGS);
                     58: static int       post_os(POST_ARGS);
1.25      schwarze   59: static int       post_pa(POST_ARGS);
1.1       kristaps   60: static int       post_prol(POST_ARGS);
1.23      schwarze   61: static int       post_rs(POST_ARGS);
1.1       kristaps   62: static int       post_sh(POST_ARGS);
1.15      schwarze   63: static int       post_st(POST_ARGS);
1.1       kristaps   64: static int       post_std(POST_ARGS);
                     65:
                     66: static int       pre_bd(PRE_ARGS);
1.22      schwarze   67: static int       pre_bl(PRE_ARGS);
1.1       kristaps   68: static int       pre_dl(PRE_ARGS);
1.22      schwarze   69: static int       pre_offset(PRE_ARGS);
1.1       kristaps   70:
1.21      schwarze   71: static const struct actions mdoc_actions[MDOC_MAX] = {
1.4       schwarze   72:        { NULL, NULL }, /* Ap */
1.1       kristaps   73:        { NULL, post_dd }, /* Dd */
                     74:        { NULL, post_dt }, /* Dt */
                     75:        { NULL, post_os }, /* Os */
                     76:        { NULL, post_sh }, /* Sh */
                     77:        { NULL, NULL }, /* Ss */
                     78:        { NULL, NULL }, /* Pp */
                     79:        { NULL, NULL }, /* D1 */
                     80:        { pre_dl, post_display }, /* Dl */
                     81:        { pre_bd, post_display }, /* Bd */
                     82:        { NULL, NULL }, /* Ed */
1.22      schwarze   83:        { pre_bl, post_bl }, /* Bl */
1.1       kristaps   84:        { NULL, NULL }, /* El */
                     85:        { NULL, NULL }, /* It */
                     86:        { NULL, NULL }, /* Ad */
                     87:        { NULL, NULL }, /* An */
                     88:        { NULL, post_ar }, /* Ar */
1.20      schwarze   89:        { NULL, NULL }, /* Cd */
1.1       kristaps   90:        { NULL, NULL }, /* Cm */
                     91:        { NULL, NULL }, /* Dv */
                     92:        { NULL, NULL }, /* Er */
                     93:        { NULL, NULL }, /* Ev */
                     94:        { NULL, post_std }, /* Ex */
                     95:        { NULL, NULL }, /* Fa */
                     96:        { NULL, NULL }, /* Fd */
                     97:        { NULL, NULL }, /* Fl */
                     98:        { NULL, NULL }, /* Fn */
                     99:        { NULL, NULL }, /* Ft */
                    100:        { NULL, NULL }, /* Ic */
                    101:        { NULL, NULL }, /* In */
1.39      schwarze  102:        { NULL, post_li }, /* Li */
1.1       kristaps  103:        { NULL, NULL }, /* Nd */
                    104:        { NULL, post_nm }, /* Nm */
                    105:        { NULL, NULL }, /* Op */
                    106:        { NULL, NULL }, /* Ot */
1.25      schwarze  107:        { NULL, post_pa }, /* Pa */
1.1       kristaps  108:        { NULL, post_std }, /* Rv */
1.15      schwarze  109:        { NULL, post_st }, /* St */
1.1       kristaps  110:        { NULL, NULL }, /* Va */
                    111:        { NULL, NULL }, /* Vt */
                    112:        { NULL, NULL }, /* Xr */
                    113:        { NULL, NULL }, /* %A */
                    114:        { NULL, NULL }, /* %B */
                    115:        { NULL, NULL }, /* %D */
                    116:        { NULL, NULL }, /* %I */
                    117:        { NULL, NULL }, /* %J */
                    118:        { NULL, NULL }, /* %N */
                    119:        { NULL, NULL }, /* %O */
                    120:        { NULL, NULL }, /* %P */
                    121:        { NULL, NULL }, /* %R */
                    122:        { NULL, NULL }, /* %T */
                    123:        { NULL, NULL }, /* %V */
                    124:        { NULL, NULL }, /* Ac */
                    125:        { NULL, NULL }, /* Ao */
                    126:        { NULL, NULL }, /* Aq */
1.15      schwarze  127:        { NULL, post_at }, /* At */
1.1       kristaps  128:        { NULL, NULL }, /* Bc */
                    129:        { NULL, NULL }, /* Bf */
                    130:        { NULL, NULL }, /* Bo */
                    131:        { NULL, NULL }, /* Bq */
                    132:        { NULL, NULL }, /* Bsx */
                    133:        { NULL, NULL }, /* Bx */
                    134:        { NULL, NULL }, /* Db */
                    135:        { NULL, NULL }, /* Dc */
                    136:        { NULL, NULL }, /* Do */
                    137:        { NULL, NULL }, /* Dq */
                    138:        { NULL, NULL }, /* Ec */
                    139:        { NULL, NULL }, /* Ef */
                    140:        { NULL, NULL }, /* Em */
                    141:        { NULL, NULL }, /* Eo */
                    142:        { NULL, NULL }, /* Fx */
                    143:        { NULL, NULL }, /* Ms */
                    144:        { NULL, NULL }, /* No */
                    145:        { NULL, NULL }, /* Ns */
                    146:        { NULL, NULL }, /* Nx */
                    147:        { NULL, NULL }, /* Ox */
                    148:        { NULL, NULL }, /* Pc */
                    149:        { NULL, NULL }, /* Pf */
                    150:        { NULL, NULL }, /* Po */
                    151:        { NULL, NULL }, /* Pq */
                    152:        { NULL, NULL }, /* Qc */
                    153:        { NULL, NULL }, /* Ql */
                    154:        { NULL, NULL }, /* Qo */
                    155:        { NULL, NULL }, /* Qq */
                    156:        { NULL, NULL }, /* Re */
1.23      schwarze  157:        { NULL, post_rs }, /* Rs */
1.1       kristaps  158:        { NULL, NULL }, /* Sc */
                    159:        { NULL, NULL }, /* So */
                    160:        { NULL, NULL }, /* Sq */
                    161:        { NULL, NULL }, /* Sm */
                    162:        { NULL, NULL }, /* Sx */
                    163:        { NULL, NULL }, /* Sy */
                    164:        { NULL, NULL }, /* Tn */
                    165:        { NULL, NULL }, /* Ux */
                    166:        { NULL, NULL }, /* Xc */
                    167:        { NULL, NULL }, /* Xo */
                    168:        { NULL, NULL }, /* Fo */
                    169:        { NULL, NULL }, /* Fc */
                    170:        { NULL, NULL }, /* Oo */
                    171:        { NULL, NULL }, /* Oc */
                    172:        { NULL, NULL }, /* Bk */
                    173:        { NULL, NULL }, /* Ek */
                    174:        { NULL, NULL }, /* Bt */
                    175:        { NULL, NULL }, /* Hf */
                    176:        { NULL, NULL }, /* Fr */
                    177:        { NULL, NULL }, /* Ud */
1.15      schwarze  178:        { NULL, post_lb }, /* Lb */
1.1       kristaps  179:        { NULL, NULL }, /* Lp */
1.24      schwarze  180:        { NULL, NULL }, /* Lk */
1.1       kristaps  181:        { NULL, NULL }, /* Mt */
                    182:        { NULL, NULL }, /* Brq */
                    183:        { NULL, NULL }, /* Bro */
                    184:        { NULL, NULL }, /* Brc */
                    185:        { NULL, NULL }, /* %C */
                    186:        { NULL, NULL }, /* Es */
                    187:        { NULL, NULL }, /* En */
                    188:        { NULL, NULL }, /* Dx */
                    189:        { NULL, NULL }, /* %Q */
1.16      schwarze  190:        { NULL, NULL }, /* br */
                    191:        { NULL, NULL }, /* sp */
1.24      schwarze  192:        { NULL, NULL }, /* %U */
1.40    ! schwarze  193:        { NULL, NULL }, /* Ta */
1.1       kristaps  194: };
                    195:
1.24      schwarze  196: #define        RSORD_MAX 14
1.23      schwarze  197:
1.31      schwarze  198: static const enum mdoct rsord[RSORD_MAX] = {
1.23      schwarze  199:        MDOC__A,
                    200:        MDOC__T,
                    201:        MDOC__B,
                    202:        MDOC__I,
                    203:        MDOC__J,
                    204:        MDOC__R,
                    205:        MDOC__N,
                    206:        MDOC__V,
                    207:        MDOC__P,
                    208:        MDOC__Q,
                    209:        MDOC__D,
                    210:        MDOC__O,
1.24      schwarze  211:        MDOC__C,
                    212:        MDOC__U
1.23      schwarze  213: };
1.15      schwarze  214:
1.1       kristaps  215:
                    216: int
1.37      schwarze  217: mdoc_action_pre(struct mdoc *m, struct mdoc_node *n)
1.1       kristaps  218: {
                    219:
                    220:        switch (n->type) {
                    221:        case (MDOC_ROOT):
                    222:                /* FALLTHROUGH */
                    223:        case (MDOC_TEXT):
                    224:                return(1);
                    225:        default:
                    226:                break;
                    227:        }
                    228:
                    229:        if (NULL == mdoc_actions[n->tok].pre)
                    230:                return(1);
                    231:        return((*mdoc_actions[n->tok].pre)(m, n));
                    232: }
                    233:
                    234:
                    235: int
                    236: mdoc_action_post(struct mdoc *m)
                    237: {
                    238:
                    239:        if (MDOC_ACTED & m->last->flags)
                    240:                return(1);
                    241:        m->last->flags |= MDOC_ACTED;
                    242:
                    243:        switch (m->last->type) {
                    244:        case (MDOC_TEXT):
                    245:                /* FALLTHROUGH */
                    246:        case (MDOC_ROOT):
                    247:                return(1);
                    248:        default:
                    249:                break;
                    250:        }
                    251:
                    252:        if (NULL == mdoc_actions[m->last->tok].post)
                    253:                return(1);
1.20      schwarze  254:        return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.1       kristaps  255: }
                    256:
                    257:
1.25      schwarze  258: /*
                    259:  * Concatenate sibling nodes together.  All siblings must be of type
                    260:  * MDOC_TEXT or an assertion is raised.  Concatenation is separated by a
                    261:  * single whitespace.
                    262:  */
1.1       kristaps  263: static int
1.25      schwarze  264: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
1.1       kristaps  265: {
                    266:
1.25      schwarze  267:        assert(sz);
                    268:        p[0] = '\0';
1.1       kristaps  269:        for ( ; n; n = n->next) {
                    270:                assert(MDOC_TEXT == n->type);
1.36      schwarze  271:                /*
                    272:                 * XXX: yes, these can technically be resized, but it's
                    273:                 * highly unlikely that we're going to get here, so let
                    274:                 * it slip for now.
                    275:                 */
                    276:                if (strlcat(p, n->string, sz) >= sz) {
                    277:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    278:                        return(0);
                    279:                }
1.1       kristaps  280:                if (NULL == n->next)
                    281:                        continue;
1.36      schwarze  282:                if (strlcat(p, " ", sz) >= sz) {
                    283:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    284:                        return(0);
                    285:                }
1.1       kristaps  286:        }
                    287:
                    288:        return(1);
                    289: }
                    290:
                    291:
1.25      schwarze  292: /*
                    293:  * Macros accepting `-std' as an argument have the name of the current
                    294:  * document (`Nm') filled in as the argument if it's not provided.
                    295:  */
1.1       kristaps  296: static int
                    297: post_std(POST_ARGS)
                    298: {
1.36      schwarze  299:        struct mdoc_node *nn;
1.1       kristaps  300:
1.20      schwarze  301:        if (n->child)
1.1       kristaps  302:                return(1);
1.36      schwarze  303:        if (NULL == m->meta.name)
                    304:                return(1);
1.20      schwarze  305:
                    306:        nn = n;
                    307:        m->next = MDOC_NEXT_CHILD;
1.36      schwarze  308:
1.20      schwarze  309:        if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
                    310:                return(0);
                    311:        m->last = nn;
1.1       kristaps  312:        return(1);
                    313: }
                    314:
                    315:
1.25      schwarze  316: /*
                    317:  * The `Nm' macro's first use sets the name of the document.  See also
                    318:  * post_std(), etc.
                    319:  */
1.1       kristaps  320: static int
                    321: post_nm(POST_ARGS)
                    322: {
1.25      schwarze  323:        char             buf[BUFSIZ];
1.1       kristaps  324:
                    325:        if (m->meta.name)
                    326:                return(1);
1.25      schwarze  327:        if ( ! concat(m, buf, n->child, BUFSIZ))
1.1       kristaps  328:                return(0);
1.25      schwarze  329:        m->meta.name = mandoc_strdup(buf);
1.1       kristaps  330:        return(1);
                    331: }
                    332:
                    333:
1.25      schwarze  334: /*
                    335:  * Look up the value of `Lb' for matching predefined strings.  If it has
                    336:  * one, then substitute the current value for the formatted value.  Note
                    337:  * that the lookup may fail (we can provide arbitrary strings).
                    338:  */
                    339: /* ARGSUSED */
1.1       kristaps  340: static int
1.15      schwarze  341: post_lb(POST_ARGS)
                    342: {
                    343:        const char      *p;
                    344:        char            *buf;
                    345:        size_t           sz;
                    346:
1.20      schwarze  347:        assert(MDOC_TEXT == n->child->type);
                    348:        p = mdoc_a2lib(n->child->string);
1.25      schwarze  349:
                    350:        if (p) {
1.20      schwarze  351:                free(n->child->string);
1.25      schwarze  352:                n->child->string = mandoc_strdup(p);
1.15      schwarze  353:                return(1);
                    354:        }
                    355:
1.25      schwarze  356:        sz = strlen(n->child->string) +
                    357:                2 + strlen("\\(lqlibrary\\(rq");
                    358:        buf = mandoc_malloc(sz);
                    359:        snprintf(buf, sz, "library \\(lq%s\\(rq", n->child->string);
1.20      schwarze  360:        free(n->child->string);
1.25      schwarze  361:        n->child->string = buf;
1.15      schwarze  362:        return(1);
                    363: }
                    364:
                    365:
1.25      schwarze  366: /*
                    367:  * Substitute the value of `St' for the corresponding formatted string.
                    368:  * We're guaranteed that this exists (it's been verified during the
                    369:  * validation phase).
                    370:  */
                    371: /* ARGSUSED */
1.15      schwarze  372: static int
                    373: post_st(POST_ARGS)
                    374: {
                    375:        const char      *p;
                    376:
1.20      schwarze  377:        assert(MDOC_TEXT == n->child->type);
                    378:        p = mdoc_a2st(n->child->string);
1.34      schwarze  379:        if (p != NULL) {
                    380:                free(n->child->string);
                    381:                n->child->string = mandoc_strdup(p);
                    382:        }
1.15      schwarze  383:        return(1);
                    384: }
                    385:
                    386:
1.25      schwarze  387: /*
                    388:  * Look up the standard string in a table.  We know that it exists from
                    389:  * the validation phase, so assert on failure.  If a standard key wasn't
                    390:  * supplied, supply the default ``AT&T UNIX''.
                    391:  */
1.15      schwarze  392: static int
                    393: post_at(POST_ARGS)
                    394: {
1.34      schwarze  395:        struct mdoc_node *nn;
                    396:        const char       *p, *q;
                    397:        char             *buf;
                    398:        size_t            sz;
1.15      schwarze  399:
1.20      schwarze  400:        if (n->child) {
                    401:                assert(MDOC_TEXT == n->child->type);
                    402:                p = mdoc_a2att(n->child->string);
1.34      schwarze  403:                if (p) {
                    404:                        free(n->child->string);
                    405:                        n->child->string = mandoc_strdup(p);
                    406:                } else {
                    407:                        p = "AT&T UNIX ";
                    408:                        q = n->child->string;
                    409:                        sz = strlen(p) + strlen(q) + 1;
                    410:                        buf = mandoc_malloc(sz);
                    411:                        strlcpy(buf, p, sz);
                    412:                        strlcat(buf, q, sz);
                    413:                        free(n->child->string);
                    414:                        n->child->string = buf;
                    415:                }
1.15      schwarze  416:                return(1);
                    417:        }
                    418:
1.20      schwarze  419:        nn = n;
1.15      schwarze  420:        m->next = MDOC_NEXT_CHILD;
1.20      schwarze  421:        if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
1.15      schwarze  422:                return(0);
1.20      schwarze  423:        m->last = nn;
1.15      schwarze  424:        return(1);
                    425: }
                    426:
                    427:
1.25      schwarze  428: /*
                    429:  * Mark the current section.  The ``named'' section (lastnamed) is set
                    430:  * whenever the current section isn't a custom section--we use this to
                    431:  * keep track of section ordering.  Also check that the section is
                    432:  * allowed within the document's manual section.
                    433:  */
1.15      schwarze  434: static int
1.1       kristaps  435: post_sh(POST_ARGS)
                    436: {
                    437:        enum mdoc_sec    sec;
1.25      schwarze  438:        char             buf[BUFSIZ];
1.1       kristaps  439:
1.20      schwarze  440:        if (MDOC_HEAD != n->type)
1.1       kristaps  441:                return(1);
                    442:
1.25      schwarze  443:        if ( ! concat(m, buf, n->child, BUFSIZ))
1.1       kristaps  444:                return(0);
1.32      schwarze  445:        sec = mdoc_str2sec(buf);
1.30      schwarze  446:        /*
                    447:         * The first section should always make us move into a non-new
                    448:         * state.
                    449:         */
1.29      schwarze  450:        if (SEC_NONE == m->lastnamed || SEC_CUSTOM != sec)
1.1       kristaps  451:                m->lastnamed = sec;
                    452:
1.25      schwarze  453:        /* Some sections only live in certain manual sections. */
                    454:
1.1       kristaps  455:        switch ((m->lastsec = sec)) {
                    456:        case (SEC_RETURN_VALUES):
                    457:                /* FALLTHROUGH */
                    458:        case (SEC_ERRORS):
1.35      schwarze  459:                assert(m->meta.msec);
                    460:                if (*m->meta.msec == '2')
1.1       kristaps  461:                        break;
1.35      schwarze  462:                if (*m->meta.msec == '3')
                    463:                        break;
                    464:                if (*m->meta.msec == '9')
                    465:                        break;
1.36      schwarze  466:                return(mdoc_nmsg(m, n, MANDOCERR_SECMSEC));
1.1       kristaps  467:        default:
                    468:                break;
                    469:        }
                    470:        return(1);
                    471: }
                    472:
                    473:
1.25      schwarze  474: /*
                    475:  * Parse out the contents of `Dt'.  See in-line documentation for how we
                    476:  * handle the various fields of this macro.
                    477:  */
1.1       kristaps  478: static int
                    479: post_dt(POST_ARGS)
                    480: {
1.20      schwarze  481:        struct mdoc_node *nn;
1.1       kristaps  482:        const char       *cp;
                    483:
                    484:        if (m->meta.title)
                    485:                free(m->meta.title);
                    486:        if (m->meta.vol)
                    487:                free(m->meta.vol);
                    488:        if (m->meta.arch)
                    489:                free(m->meta.arch);
                    490:
                    491:        m->meta.title = m->meta.vol = m->meta.arch = NULL;
                    492:        /* Handles: `.Dt'
                    493:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    494:         */
                    495:
1.20      schwarze  496:        if (NULL == (nn = n->child)) {
1.25      schwarze  497:                /* XXX: make these macro values. */
1.35      schwarze  498:                /* FIXME: warn about missing values. */
1.40    ! schwarze  499:                m->meta.title = mandoc_strdup("UNKNOWN");
        !           500:                m->meta.vol = mandoc_strdup("LOCAL");
1.35      schwarze  501:                m->meta.msec = mandoc_strdup("1");
1.20      schwarze  502:                return(post_prol(m, n));
1.1       kristaps  503:        }
                    504:
                    505:        /* Handles: `.Dt TITLE'
                    506:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                    507:         */
                    508:
1.40    ! schwarze  509:        m->meta.title = mandoc_strdup
        !           510:                ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
1.1       kristaps  511:
1.20      schwarze  512:        if (NULL == (nn = nn->next)) {
1.35      schwarze  513:                /* FIXME: warn about missing msec. */
1.25      schwarze  514:                /* XXX: make this a macro value. */
1.40    ! schwarze  515:                m->meta.vol = mandoc_strdup("LOCAL");
1.35      schwarze  516:                m->meta.msec = mandoc_strdup("1");
1.20      schwarze  517:                return(post_prol(m, n));
1.1       kristaps  518:        }
                    519:
                    520:        /* Handles: `.Dt TITLE SEC'
                    521:         *   --> title = TITLE, volume = SEC is msec ?
                    522:         *           format(msec) : SEC,
                    523:         *       msec = SEC is msec ? atoi(msec) : 0,
                    524:         *       arch = NULL
                    525:         */
                    526:
1.20      schwarze  527:        cp = mdoc_a2msec(nn->string);
1.1       kristaps  528:        if (cp) {
1.25      schwarze  529:                m->meta.vol = mandoc_strdup(cp);
1.35      schwarze  530:                m->meta.msec = mandoc_strdup(nn->string);
1.36      schwarze  531:        } else if (mdoc_nmsg(m, n, MANDOCERR_BADMSEC)) {
1.25      schwarze  532:                m->meta.vol = mandoc_strdup(nn->string);
1.35      schwarze  533:                m->meta.msec = mandoc_strdup(nn->string);
                    534:        } else
                    535:                return(0);
1.1       kristaps  536:
1.20      schwarze  537:        if (NULL == (nn = nn->next))
                    538:                return(post_prol(m, n));
1.1       kristaps  539:
                    540:        /* Handles: `.Dt TITLE SEC VOL'
                    541:         *   --> title = TITLE, volume = VOL is vol ?
                    542:         *       format(VOL) :
                    543:         *           VOL is arch ? format(arch) :
                    544:         *               VOL
                    545:         */
                    546:
1.20      schwarze  547:        cp = mdoc_a2vol(nn->string);
1.1       kristaps  548:        if (cp) {
                    549:                free(m->meta.vol);
1.25      schwarze  550:                m->meta.vol = mandoc_strdup(cp);
1.1       kristaps  551:        } else {
1.35      schwarze  552:                /* FIXME: warn about bad arch. */
1.20      schwarze  553:                cp = mdoc_a2arch(nn->string);
1.1       kristaps  554:                if (NULL == cp) {
                    555:                        free(m->meta.vol);
1.25      schwarze  556:                        m->meta.vol = mandoc_strdup(nn->string);
                    557:                } else
                    558:                        m->meta.arch = mandoc_strdup(cp);
1.1       kristaps  559:        }
                    560:
                    561:        /* Ignore any subsequent parameters... */
1.25      schwarze  562:        /* FIXME: warn about subsequent parameters. */
1.1       kristaps  563:
1.20      schwarze  564:        return(post_prol(m, n));
1.1       kristaps  565: }
                    566:
                    567:
1.25      schwarze  568: /*
                    569:  * Set the operating system by way of the `Os' macro.  Note that if an
                    570:  * argument isn't provided and -DOSNAME="\"foo\"" is provided during
                    571:  * compilation, this value will be used instead of filling in "sysname
                    572:  * release" from uname().
                    573:  */
1.1       kristaps  574: static int
                    575: post_os(POST_ARGS)
                    576: {
1.25      schwarze  577:        char              buf[BUFSIZ];
                    578: #ifndef OSNAME
1.1       kristaps  579:        struct utsname    utsname;
1.24      schwarze  580: #endif
                    581:
1.1       kristaps  582:        if (m->meta.os)
                    583:                free(m->meta.os);
                    584:
1.25      schwarze  585:        if ( ! concat(m, buf, n->child, BUFSIZ))
1.1       kristaps  586:                return(0);
                    587:
1.36      schwarze  588:        /* XXX: yes, these can all be dynamically-adjusted buffers, but
                    589:         * it's really not worth the extra hackery.
                    590:         */
                    591:
1.25      schwarze  592:        if ('\0' == buf[0]) {
                    593: #ifdef OSNAME
1.36      schwarze  594:                if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
                    595:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    596:                        return(0);
                    597:                }
1.25      schwarze  598: #else /*!OSNAME */
1.1       kristaps  599:                if (-1 == uname(&utsname))
1.36      schwarze  600:                        return(mdoc_nmsg(m, n, MANDOCERR_UTSNAME));
                    601:
                    602:                if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
                    603:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    604:                        return(0);
                    605:                }
                    606:                if (strlcat(buf, " ", 64) >= BUFSIZ) {
                    607:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    608:                        return(0);
                    609:                }
                    610:                if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
                    611:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    612:                        return(0);
                    613:                }
1.25      schwarze  614: #endif /*!OSNAME*/
1.1       kristaps  615:        }
                    616:
1.25      schwarze  617:        m->meta.os = mandoc_strdup(buf);
1.20      schwarze  618:        return(post_prol(m, n));
1.1       kristaps  619: }
                    620:
                    621:
                    622: /*
                    623:  * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
1.25      schwarze  624:  * Uses the first head macro.  NOTE AGAIN: this is ONLY if the -width
                    625:  * argument has NOT been provided.  See post_bl_width() for converting
                    626:  * the -width string.
1.1       kristaps  627:  */
                    628: static int
1.20      schwarze  629: post_bl_tagwidth(POST_ARGS)
1.1       kristaps  630: {
1.25      schwarze  631:        struct mdoc_node *nn;
                    632:        size_t            sz;
                    633:        int               i;
                    634:        char              buf[NUMSIZ];
1.1       kristaps  635:
1.25      schwarze  636:        /* Defaults to ten ens. */
1.1       kristaps  637:
1.25      schwarze  638:        sz = 10; /* XXX: make this a macro value. */
1.38      schwarze  639:
                    640:        for (nn = n->body->child; nn; nn = nn->next) {
                    641:                if (MDOC_It == nn->tok)
                    642:                        break;
                    643:        }
1.25      schwarze  644:
1.20      schwarze  645:        if (nn) {
                    646:                assert(MDOC_BLOCK == nn->type);
                    647:                nn = nn->head->child;
                    648:                if (MDOC_TEXT != nn->type) {
1.25      schwarze  649:                        sz = mdoc_macro2len(nn->tok);
                    650:                        if (sz == 0) {
1.36      schwarze  651:                                if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
1.1       kristaps  652:                                        return(0);
1.25      schwarze  653:                                sz = 10;
                    654:                        }
1.1       kristaps  655:                } else
1.25      schwarze  656:                        sz = strlen(nn->string) + 1;
1.1       kristaps  657:        }
                    658:
1.25      schwarze  659:        snprintf(buf, NUMSIZ, "%zun", sz);
1.1       kristaps  660:
                    661:        /*
                    662:         * We have to dynamically add this to the macro's argument list.
                    663:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    664:         */
                    665:
1.20      schwarze  666:        nn = n;
                    667:        assert(nn->args);
1.25      schwarze  668:        i = (int)(nn->args->argc)++;
1.1       kristaps  669:
1.25      schwarze  670:        nn->args->argv = mandoc_realloc(nn->args->argv,
1.20      schwarze  671:                        nn->args->argc * sizeof(struct mdoc_argv));
1.1       kristaps  672:
1.25      schwarze  673:        nn->args->argv[i].arg = MDOC_Width;
                    674:        nn->args->argv[i].line = n->line;
                    675:        nn->args->argv[i].pos = n->pos;
                    676:        nn->args->argv[i].sz = 1;
                    677:        nn->args->argv[i].value = mandoc_malloc(sizeof(char *));
                    678:        nn->args->argv[i].value[0] = mandoc_strdup(buf);
1.1       kristaps  679:        return(1);
                    680: }
                    681:
                    682:
1.25      schwarze  683: /*
                    684:  * Calculate the real width of a list from the -width string, which may
                    685:  * contain a macro (with a known default width), a literal string, or a
                    686:  * scaling width.
                    687:  */
1.1       kristaps  688: static int
1.20      schwarze  689: post_bl_width(POST_ARGS)
1.1       kristaps  690: {
                    691:        size_t            width;
1.28      schwarze  692:        int               i;
                    693:        enum mdoct        tok;
1.25      schwarze  694:        char              buf[NUMSIZ];
1.1       kristaps  695:        char             *p;
                    696:
1.20      schwarze  697:        if (NULL == n->args)
1.1       kristaps  698:                return(1);
                    699:
1.20      schwarze  700:        for (i = 0; i < (int)n->args->argc; i++)
                    701:                if (MDOC_Width == n->args->argv[i].arg)
1.1       kristaps  702:                        break;
                    703:
1.20      schwarze  704:        if (i == (int)n->args->argc)
1.1       kristaps  705:                return(1);
1.20      schwarze  706:        p = n->args->argv[i].value[0];
1.1       kristaps  707:
                    708:        /*
                    709:         * If the value to -width is a macro, then we re-write it to be
                    710:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    711:         */
                    712:
                    713:        if (0 == strcmp(p, "Ds"))
1.9       schwarze  714:                width = 6;
1.21      schwarze  715:        else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
1.1       kristaps  716:                return(1);
                    717:        else if (0 == (width = mdoc_macro2len(tok)))
1.36      schwarze  718:                return(mdoc_nmsg(m, n, MANDOCERR_BADWIDTH));
1.1       kristaps  719:
                    720:        /* The value already exists: free and reallocate it. */
                    721:
1.25      schwarze  722:        snprintf(buf, NUMSIZ, "%zun", width);
1.20      schwarze  723:        free(n->args->argv[i].value[0]);
1.25      schwarze  724:        n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1       kristaps  725:        return(1);
                    726: }
                    727:
                    728:
1.25      schwarze  729: /*
                    730:  * Do processing for -column lists, which can have two distinct styles
                    731:  * of invocation.  Merge this two styles into a consistent form.
                    732:  */
1.20      schwarze  733: /* ARGSUSED */
1.1       kristaps  734: static int
1.6       schwarze  735: post_bl_head(POST_ARGS)
                    736: {
                    737:        int                      i, c;
1.20      schwarze  738:        struct mdoc_node        *np, *nn, *nnp;
1.6       schwarze  739:
1.20      schwarze  740:        if (NULL == n->child)
1.6       schwarze  741:                return(1);
                    742:
1.20      schwarze  743:        np = n->parent;
                    744:        assert(np->args);
1.6       schwarze  745:
1.20      schwarze  746:        for (c = 0; c < (int)np->args->argc; c++)
                    747:                if (MDOC_Column == np->args->argv[c].arg)
1.6       schwarze  748:                        break;
                    749:
1.20      schwarze  750:        if (c == (int)np->args->argc)
1.6       schwarze  751:                return(1);
1.20      schwarze  752:        assert(0 == np->args->argv[c].sz);
1.6       schwarze  753:
                    754:        /*
                    755:         * Accomodate for new-style groff column syntax.  Shuffle the
                    756:         * child nodes, all of which must be TEXT, as arguments for the
                    757:         * column field.  Then, delete the head children.
                    758:         */
                    759:
1.20      schwarze  760:        np->args->argv[c].sz = (size_t)n->nchild;
1.25      schwarze  761:        np->args->argv[c].value = mandoc_malloc
1.20      schwarze  762:                ((size_t)n->nchild * sizeof(char *));
1.6       schwarze  763:
1.20      schwarze  764:        for (i = 0, nn = n->child; nn; i++) {
                    765:                np->args->argv[c].value[i] = nn->string;
1.6       schwarze  766:                nn->string = NULL;
                    767:                nnp = nn;
                    768:                nn = nn->next;
1.30      schwarze  769:                mdoc_node_delete(NULL, nnp);
1.6       schwarze  770:        }
                    771:
1.20      schwarze  772:        n->nchild = 0;
                    773:        n->child = NULL;
1.6       schwarze  774:        return(1);
                    775: }
                    776:
                    777:
                    778: static int
1.1       kristaps  779: post_bl(POST_ARGS)
                    780: {
                    781:        int               i, r, len;
                    782:
1.20      schwarze  783:        if (MDOC_HEAD == n->type)
                    784:                return(post_bl_head(m, n));
                    785:        if (MDOC_BLOCK != n->type)
1.1       kristaps  786:                return(1);
                    787:
                    788:        /*
                    789:         * These are fairly complicated, so we've broken them into two
                    790:         * functions.  post_bl_tagwidth() is called when a -tag is
                    791:         * specified, but no -width (it must be guessed).  The second
                    792:         * when a -width is specified (macro indicators must be
                    793:         * rewritten into real lengths).
                    794:         */
                    795:
1.20      schwarze  796:        len = (int)(n->args ? n->args->argc : 0);
1.1       kristaps  797:
                    798:        for (r = i = 0; i < len; i++) {
1.20      schwarze  799:                if (MDOC_Tag == n->args->argv[i].arg)
1.1       kristaps  800:                        r |= 1 << 0;
1.20      schwarze  801:                if (MDOC_Width == n->args->argv[i].arg)
1.1       kristaps  802:                        r |= 1 << 1;
                    803:        }
                    804:
                    805:        if (r & (1 << 0) && ! (r & (1 << 1))) {
1.20      schwarze  806:                if ( ! post_bl_tagwidth(m, n))
1.1       kristaps  807:                        return(0);
                    808:        } else if (r & (1 << 1))
1.20      schwarze  809:                if ( ! post_bl_width(m, n))
1.1       kristaps  810:                        return(0);
                    811:
1.3       schwarze  812:        return(1);
                    813: }
                    814:
                    815:
1.25      schwarze  816: /*
                    817:  * The `Pa' macro defaults to a tilde if no value is provided as an
                    818:  * argument.
                    819:  */
1.3       schwarze  820: static int
1.25      schwarze  821: post_pa(POST_ARGS)
1.3       schwarze  822: {
1.20      schwarze  823:        struct mdoc_node *np;
1.3       schwarze  824:
1.20      schwarze  825:        if (n->child)
1.3       schwarze  826:                return(1);
                    827:
1.20      schwarze  828:        np = n;
1.3       schwarze  829:        m->next = MDOC_NEXT_CHILD;
1.25      schwarze  830:        /* XXX: make into macro value. */
1.20      schwarze  831:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.39      schwarze  832:                return(0);
                    833:        m->last = np;
                    834:        return(1);
                    835: }
                    836:
                    837:
                    838: /*
                    839:  * Empty `Li' macros get an empty string to make front-ends add an extra
                    840:  * space.
                    841:  */
                    842: static int
                    843: post_li(POST_ARGS)
                    844: {
                    845:        struct mdoc_node *np;
                    846:
                    847:        if (n->child)
                    848:                return(1);
                    849:
                    850:        np = n;
                    851:        m->next = MDOC_NEXT_CHILD;
                    852:        if ( ! mdoc_word_alloc(m, n->line, n->pos, ""))
1.3       schwarze  853:                return(0);
1.20      schwarze  854:        m->last = np;
1.1       kristaps  855:        return(1);
                    856: }
                    857:
                    858:
1.25      schwarze  859: /*
                    860:  * The `Ar' macro defaults to two strings "file ..." if no value is
                    861:  * provided as an argument.
                    862:  */
1.1       kristaps  863: static int
                    864: post_ar(POST_ARGS)
                    865: {
1.20      schwarze  866:        struct mdoc_node *np;
1.1       kristaps  867:
1.20      schwarze  868:        if (n->child)
1.1       kristaps  869:                return(1);
                    870:
1.20      schwarze  871:        np = n;
1.1       kristaps  872:        m->next = MDOC_NEXT_CHILD;
1.25      schwarze  873:        /* XXX: make into macro values. */
1.20      schwarze  874:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1       kristaps  875:                return(0);
1.20      schwarze  876:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1       kristaps  877:                return(0);
1.20      schwarze  878:        m->last = np;
1.1       kristaps  879:        return(1);
                    880: }
                    881:
                    882:
1.25      schwarze  883: /*
1.26      schwarze  884:  * Parse the date field in `Dd'.
1.25      schwarze  885:  */
1.1       kristaps  886: static int
                    887: post_dd(POST_ARGS)
                    888: {
1.25      schwarze  889:        char            buf[DATESIZ];
1.1       kristaps  890:
1.25      schwarze  891:        if ( ! concat(m, buf, n->child, DATESIZ))
1.1       kristaps  892:                return(0);
                    893:
1.26      schwarze  894:        m->meta.date = mandoc_a2time
                    895:                (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
                    896:
1.25      schwarze  897:        if (0 == m->meta.date) {
1.36      schwarze  898:                if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
1.1       kristaps  899:                        return(0);
                    900:                m->meta.date = time(NULL);
                    901:        }
                    902:
1.20      schwarze  903:        return(post_prol(m, n));
1.1       kristaps  904: }
                    905:
                    906:
1.25      schwarze  907: /*
                    908:  * Remove prologue macros from the document after they're processed.
                    909:  * The final document uses mdoc_meta for these values and discards the
                    910:  * originals.
                    911:  */
1.1       kristaps  912: static int
                    913: post_prol(POST_ARGS)
                    914: {
1.18      schwarze  915:
1.30      schwarze  916:        mdoc_node_delete(m, n);
1.18      schwarze  917:        if (m->meta.title && m->meta.date && m->meta.os)
                    918:                m->flags |= MDOC_PBODY;
1.1       kristaps  919:        return(1);
                    920: }
                    921:
                    922:
1.25      schwarze  923: /*
                    924:  * Trigger a literal context.
                    925:  */
1.1       kristaps  926: static int
                    927: pre_dl(PRE_ARGS)
                    928: {
                    929:
1.7       schwarze  930:        if (MDOC_BODY == n->type)
                    931:                m->flags |= MDOC_LITERAL;
1.1       kristaps  932:        return(1);
                    933: }
                    934:
                    935:
1.25      schwarze  936: /* ARGSUSED */
1.1       kristaps  937: static int
1.22      schwarze  938: pre_offset(PRE_ARGS)
                    939: {
                    940:        int              i;
                    941:
                    942:        /*
                    943:         * Make sure that an empty offset produces an 8n length space as
                    944:         * stipulated by mdoc.samples.
                    945:         */
                    946:
1.40    ! schwarze  947:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
1.22      schwarze  948:                if (MDOC_Offset != n->args->argv[i].arg)
                    949:                        continue;
                    950:                if (n->args->argv[i].sz)
                    951:                        break;
                    952:                assert(1 == n->args->refcnt);
                    953:                /* If no value set, length of <string>. */
                    954:                n->args->argv[i].sz++;
1.25      schwarze  955:                n->args->argv[i].value = mandoc_malloc(sizeof(char *));
                    956:                n->args->argv[i].value[0] = mandoc_strdup("8n");
1.22      schwarze  957:                break;
                    958:        }
                    959:
                    960:        return(1);
                    961: }
                    962:
                    963:
                    964: static int
                    965: pre_bl(PRE_ARGS)
                    966: {
                    967:
1.40    ! schwarze  968:        if (MDOC_BLOCK == n->type)
        !           969:                return(pre_offset(m, n));
        !           970:        return(1);
1.22      schwarze  971: }
                    972:
                    973:
                    974: static int
1.1       kristaps  975: pre_bd(PRE_ARGS)
                    976: {
                    977:        int              i;
                    978:
1.22      schwarze  979:        if (MDOC_BLOCK == n->type)
                    980:                return(pre_offset(m, n));
1.1       kristaps  981:        if (MDOC_BODY != n->type)
                    982:                return(1);
                    983:
1.19      schwarze  984:        /* Enter literal context if `Bd -literal' or `-unfilled'. */
                    985:
1.1       kristaps  986:        for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
                    987:                if (MDOC_Literal == n->args->argv[i].arg)
1.22      schwarze  988:                        m->flags |= MDOC_LITERAL;
1.1       kristaps  989:                else if (MDOC_Unfilled == n->args->argv[i].arg)
1.22      schwarze  990:                        m->flags |= MDOC_LITERAL;
1.1       kristaps  991:
                    992:        return(1);
                    993: }
                    994:
                    995:
                    996: static int
                    997: post_display(POST_ARGS)
                    998: {
                    999:
1.20      schwarze 1000:        if (MDOC_BODY == n->type)
1.1       kristaps 1001:                m->flags &= ~MDOC_LITERAL;
                   1002:        return(1);
                   1003: }
                   1004:
                   1005:
1.23      schwarze 1006: static inline int
1.31      schwarze 1007: order_rs(enum mdoct t)
1.23      schwarze 1008: {
                   1009:        int             i;
                   1010:
1.31      schwarze 1011:        for (i = 0; i < (int)RSORD_MAX; i++)
1.23      schwarze 1012:                if (rsord[i] == t)
                   1013:                        return(i);
                   1014:
                   1015:        abort();
                   1016:        /* NOTREACHED */
                   1017: }
                   1018:
                   1019:
                   1020: /* ARGSUSED */
                   1021: static int
                   1022: post_rs(POST_ARGS)
                   1023: {
                   1024:        struct mdoc_node        *nn, *next, *prev;
                   1025:        int                      o;
                   1026:
                   1027:        if (MDOC_BLOCK != n->type)
                   1028:                return(1);
                   1029:
                   1030:        assert(n->body->child);
                   1031:        for (next = NULL, nn = n->body->child->next; nn; nn = next) {
                   1032:                o = order_rs(nn->tok);
                   1033:
                   1034:                /* Remove `nn' from the chain. */
                   1035:                next = nn->next;
                   1036:                if (next)
                   1037:                        next->prev = nn->prev;
                   1038:
                   1039:                prev = nn->prev;
                   1040:                if (prev)
                   1041:                        prev->next = nn->next;
                   1042:
                   1043:                nn->prev = nn->next = NULL;
                   1044:
                   1045:                /*
                   1046:                 * Scan back until we reach a node that's ordered before
                   1047:                 * us, then set ourselves as being the next.
                   1048:                 */
                   1049:                for ( ; prev; prev = prev->prev)
                   1050:                        if (order_rs(prev->tok) <= o)
                   1051:                                break;
                   1052:
                   1053:                nn->prev = prev;
                   1054:                if (prev) {
                   1055:                        if (prev->next)
                   1056:                                prev->next->prev = nn;
                   1057:                        nn->next = prev->next;
                   1058:                        prev->next = nn;
                   1059:                        continue;
                   1060:                }
                   1061:
                   1062:                n->body->child->prev = nn;
                   1063:                nn->next = n->body->child;
                   1064:                n->body->child = nn;
                   1065:        }
                   1066:        return(1);
                   1067: }