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

1.12    ! schwarze    1: /*     $Id: mdoc_action.c,v 1.11 2009/07/12 20:30:27 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:  */
                     17: #include <sys/utsname.h>
                     18:
                     19: #include <assert.h>
                     20: #include <errno.h>
                     21: #include <stdio.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "libmdoc.h"
                     26:
                     27: enum   mwarn {
                     28:        WBADSEC,
                     29:        WNOWIDTH,
                     30:        WBADDATE
                     31: };
                     32:
                     33: #define        PRE_ARGS  struct mdoc *m, const struct mdoc_node *n
                     34: #define        POST_ARGS struct mdoc *m
                     35:
                     36: struct actions {
                     37:        int     (*pre)(PRE_ARGS);
                     38:        int     (*post)(POST_ARGS);
                     39: };
                     40:
                     41: static int       pwarn(struct mdoc *, int, int, enum mwarn);
                     42: static int       perr(struct mdoc *, int, int, enum merr);
                     43: static int       concat(struct mdoc *, const struct mdoc_node *,
                     44:                        char *, size_t);
                     45:
                     46: static int       post_ar(POST_ARGS);
                     47: static int       post_bl(POST_ARGS);
1.6       schwarze   48: static int       post_bl_head(POST_ARGS);
1.1       kristaps   49: static int       post_bl_width(POST_ARGS);
                     50: static int       post_bl_tagwidth(POST_ARGS);
                     51: static int       post_dd(POST_ARGS);
                     52: static int       post_display(POST_ARGS);
                     53: static int       post_dt(POST_ARGS);
1.3       schwarze   54: static int       post_lk(POST_ARGS);
1.1       kristaps   55: static int       post_nm(POST_ARGS);
                     56: static int       post_os(POST_ARGS);
                     57: static int       post_prol(POST_ARGS);
                     58: static int       post_sh(POST_ARGS);
                     59: static int       post_std(POST_ARGS);
                     60:
                     61: static int       pre_bd(PRE_ARGS);
                     62: static int       pre_dl(PRE_ARGS);
                     63:
                     64: #define        vwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
                     65: #define        verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
                     66: #define        nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
                     67:
                     68: const  struct actions mdoc_actions[MDOC_MAX] = {
1.4       schwarze   69:        { NULL, NULL }, /* Ap */
1.1       kristaps   70:        { NULL, post_dd }, /* Dd */
                     71:        { NULL, post_dt }, /* Dt */
                     72:        { NULL, post_os }, /* Os */
                     73:        { NULL, post_sh }, /* Sh */
                     74:        { NULL, NULL }, /* Ss */
                     75:        { NULL, NULL }, /* Pp */
                     76:        { NULL, NULL }, /* D1 */
                     77:        { pre_dl, post_display }, /* Dl */
                     78:        { pre_bd, post_display }, /* Bd */
                     79:        { NULL, NULL }, /* Ed */
                     80:        { NULL, post_bl }, /* Bl */
                     81:        { NULL, NULL }, /* El */
                     82:        { NULL, NULL }, /* It */
                     83:        { NULL, NULL }, /* Ad */
                     84:        { NULL, NULL }, /* An */
                     85:        { NULL, post_ar }, /* Ar */
1.9       schwarze   86:        { NULL, NULL }, /* Cd */ /* FIXME: tabs are accepted! */
1.1       kristaps   87:        { NULL, NULL }, /* Cm */
                     88:        { NULL, NULL }, /* Dv */
                     89:        { NULL, NULL }, /* Er */
                     90:        { NULL, NULL }, /* Ev */
                     91:        { NULL, post_std }, /* Ex */
                     92:        { NULL, NULL }, /* Fa */
                     93:        { NULL, NULL }, /* Fd */
                     94:        { NULL, NULL }, /* Fl */
                     95:        { NULL, NULL }, /* Fn */
                     96:        { NULL, NULL }, /* Ft */
                     97:        { NULL, NULL }, /* Ic */
                     98:        { NULL, NULL }, /* In */
                     99:        { NULL, NULL }, /* Li */
                    100:        { NULL, NULL }, /* Nd */
                    101:        { NULL, post_nm }, /* Nm */
                    102:        { NULL, NULL }, /* Op */
                    103:        { NULL, NULL }, /* Ot */
                    104:        { NULL, NULL }, /* Pa */
                    105:        { NULL, post_std }, /* Rv */
                    106:        { NULL, NULL }, /* St */
                    107:        { NULL, NULL }, /* Va */
                    108:        { NULL, NULL }, /* Vt */
                    109:        { NULL, NULL }, /* Xr */
                    110:        { NULL, NULL }, /* %A */
                    111:        { NULL, NULL }, /* %B */
                    112:        { NULL, NULL }, /* %D */
                    113:        { NULL, NULL }, /* %I */
                    114:        { NULL, NULL }, /* %J */
                    115:        { NULL, NULL }, /* %N */
                    116:        { NULL, NULL }, /* %O */
                    117:        { NULL, NULL }, /* %P */
                    118:        { NULL, NULL }, /* %R */
                    119:        { NULL, NULL }, /* %T */
                    120:        { NULL, NULL }, /* %V */
                    121:        { NULL, NULL }, /* Ac */
                    122:        { NULL, NULL }, /* Ao */
                    123:        { NULL, NULL }, /* Aq */
                    124:        { NULL, NULL }, /* At */
                    125:        { NULL, NULL }, /* Bc */
                    126:        { NULL, NULL }, /* Bf */
                    127:        { NULL, NULL }, /* Bo */
                    128:        { NULL, NULL }, /* Bq */
                    129:        { NULL, NULL }, /* Bsx */
                    130:        { NULL, NULL }, /* Bx */
                    131:        { NULL, NULL }, /* Db */
                    132:        { NULL, NULL }, /* Dc */
                    133:        { NULL, NULL }, /* Do */
                    134:        { NULL, NULL }, /* Dq */
                    135:        { NULL, NULL }, /* Ec */
                    136:        { NULL, NULL }, /* Ef */
                    137:        { NULL, NULL }, /* Em */
                    138:        { NULL, NULL }, /* Eo */
                    139:        { NULL, NULL }, /* Fx */
                    140:        { NULL, NULL }, /* Ms */
                    141:        { NULL, NULL }, /* No */
                    142:        { NULL, NULL }, /* Ns */
                    143:        { NULL, NULL }, /* Nx */
                    144:        { NULL, NULL }, /* Ox */
                    145:        { NULL, NULL }, /* Pc */
                    146:        { NULL, NULL }, /* Pf */
                    147:        { NULL, NULL }, /* Po */
                    148:        { NULL, NULL }, /* Pq */
                    149:        { NULL, NULL }, /* Qc */
                    150:        { NULL, NULL }, /* Ql */
                    151:        { NULL, NULL }, /* Qo */
                    152:        { NULL, NULL }, /* Qq */
                    153:        { NULL, NULL }, /* Re */
                    154:        { NULL, NULL }, /* Rs */
                    155:        { NULL, NULL }, /* Sc */
                    156:        { NULL, NULL }, /* So */
                    157:        { NULL, NULL }, /* Sq */
                    158:        { NULL, NULL }, /* Sm */
                    159:        { NULL, NULL }, /* Sx */
                    160:        { NULL, NULL }, /* Sy */
                    161:        { NULL, NULL }, /* Tn */
                    162:        { NULL, NULL }, /* Ux */
                    163:        { NULL, NULL }, /* Xc */
                    164:        { NULL, NULL }, /* Xo */
                    165:        { NULL, NULL }, /* Fo */
                    166:        { NULL, NULL }, /* Fc */
                    167:        { NULL, NULL }, /* Oo */
                    168:        { NULL, NULL }, /* Oc */
                    169:        { NULL, NULL }, /* Bk */
                    170:        { NULL, NULL }, /* Ek */
                    171:        { NULL, NULL }, /* Bt */
                    172:        { NULL, NULL }, /* Hf */
                    173:        { NULL, NULL }, /* Fr */
                    174:        { NULL, NULL }, /* Ud */
                    175:        { NULL, NULL }, /* Lb */
                    176:        { NULL, NULL }, /* Lp */
1.3       schwarze  177:        { NULL, post_lk }, /* Lk */
1.1       kristaps  178:        { NULL, NULL }, /* Mt */
                    179:        { NULL, NULL }, /* Brq */
                    180:        { NULL, NULL }, /* Bro */
                    181:        { NULL, NULL }, /* Brc */
                    182:        { NULL, NULL }, /* %C */
                    183:        { NULL, NULL }, /* Es */
                    184:        { NULL, NULL }, /* En */
                    185:        { NULL, NULL }, /* Dx */
                    186:        { NULL, NULL }, /* %Q */
                    187: };
                    188:
                    189:
                    190: int
                    191: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
                    192: {
                    193:
                    194:        switch (n->type) {
                    195:        case (MDOC_ROOT):
                    196:                /* FALLTHROUGH */
                    197:        case (MDOC_TEXT):
                    198:                return(1);
                    199:        default:
                    200:                break;
                    201:        }
                    202:
                    203:        if (NULL == mdoc_actions[n->tok].pre)
                    204:                return(1);
                    205:        return((*mdoc_actions[n->tok].pre)(m, n));
                    206: }
                    207:
                    208:
                    209: int
                    210: mdoc_action_post(struct mdoc *m)
                    211: {
                    212:
                    213:        if (MDOC_ACTED & m->last->flags)
                    214:                return(1);
                    215:        m->last->flags |= MDOC_ACTED;
                    216:
                    217:        switch (m->last->type) {
                    218:        case (MDOC_TEXT):
                    219:                /* FALLTHROUGH */
                    220:        case (MDOC_ROOT):
                    221:                return(1);
                    222:        default:
                    223:                break;
                    224:        }
                    225:
                    226:        if (NULL == mdoc_actions[m->last->tok].post)
                    227:                return(1);
                    228:        return((*mdoc_actions[m->last->tok].post)(m));
                    229: }
                    230:
                    231:
                    232: static int
                    233: concat(struct mdoc *m, const struct mdoc_node *n,
                    234:                char *buf, size_t sz)
                    235: {
                    236:
                    237:        for ( ; n; n = n->next) {
                    238:                assert(MDOC_TEXT == n->type);
                    239:                if (strlcat(buf, n->string, sz) >= sz)
1.12    ! schwarze  240:                        return(mdoc_nerr(m, n, ETOOLONG));
1.1       kristaps  241:                if (NULL == n->next)
                    242:                        continue;
                    243:                if (strlcat(buf, " ", sz) >= sz)
1.12    ! schwarze  244:                        return(mdoc_nerr(m, n, ETOOLONG));
1.1       kristaps  245:        }
                    246:
                    247:        return(1);
                    248: }
                    249:
                    250:
                    251: static int
                    252: perr(struct mdoc *m, int line, int pos, enum merr type)
                    253: {
                    254:        char            *p;
                    255:
                    256:        p = NULL;
                    257:        switch (type) {
                    258:        case (ENUMFMT):
                    259:                p = "bad number format";
                    260:                break;
                    261:        case (ETOOLONG):
                    262:                p = "argument text too long";
                    263:                break;
1.10      schwarze  264:        case (EUTSNAME):
                    265:                p = "utsname";
                    266:                break;
1.1       kristaps  267:        case (EMALLOC):
                    268:                p = "memory exhausted";
                    269:                break;
                    270:        }
                    271:        assert(p);
                    272:        return(mdoc_perr(m, line, pos, p));
                    273: }
                    274:
                    275:
                    276: static int
                    277: pwarn(struct mdoc *m, int line, int pos, enum mwarn type)
                    278: {
                    279:        char            *p;
                    280:        int              c;
                    281:
                    282:        p = NULL;
                    283:        c = WARN_SYNTAX;
                    284:        switch (type) {
                    285:        case (WBADSEC):
                    286:                p = "inappropriate document section in manual section";
                    287:                c = WARN_COMPAT;
                    288:                break;
                    289:        case (WNOWIDTH):
                    290:                p = "cannot determine default width";
                    291:                break;
                    292:        case (WBADDATE):
                    293:                p = "malformed date syntax";
                    294:                break;
                    295:        }
                    296:        assert(p);
                    297:        return(mdoc_pwarn(m, line, pos, c, p));
                    298: }
                    299:
                    300:
                    301: static int
                    302: post_std(POST_ARGS)
                    303: {
                    304:
                    305:        /*
                    306:         * If '-std' is invoked without an argument, fill it in with our
                    307:         * name (if it's been set).
                    308:         */
                    309:
                    310:        if (NULL == m->last->args)
                    311:                return(1);
                    312:        if (m->last->args->argv[0].sz)
                    313:                return(1);
                    314:
                    315:        assert(m->meta.name);
                    316:
                    317:        m->last->args->argv[0].value = calloc(1, sizeof(char *));
                    318:        if (NULL == m->last->args->argv[0].value)
1.12    ! schwarze  319:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  320:
                    321:        m->last->args->argv[0].sz = 1;
                    322:        m->last->args->argv[0].value[0] = strdup(m->meta.name);
                    323:        if (NULL == m->last->args->argv[0].value[0])
1.12    ! schwarze  324:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  325:
                    326:        return(1);
                    327: }
                    328:
                    329:
                    330: static int
                    331: post_nm(POST_ARGS)
                    332: {
                    333:        char             buf[64];
                    334:
                    335:        if (m->meta.name)
                    336:                return(1);
                    337:
                    338:        buf[0] = 0;
                    339:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    340:                return(0);
1.2       schwarze  341:
1.1       kristaps  342:        if (NULL == (m->meta.name = strdup(buf)))
1.12    ! schwarze  343:                return(mdoc_nerr(m, m->last, EMALLOC));
1.2       schwarze  344:
1.1       kristaps  345:        return(1);
                    346: }
                    347:
                    348:
                    349: static int
                    350: post_sh(POST_ARGS)
                    351: {
                    352:        enum mdoc_sec    sec;
                    353:        char             buf[64];
                    354:
                    355:        /*
                    356:         * We keep track of the current section /and/ the "named"
                    357:         * section, which is one of the conventional ones, in order to
                    358:         * check ordering.
                    359:         */
                    360:
                    361:        if (MDOC_HEAD != m->last->type)
                    362:                return(1);
                    363:
                    364:        buf[0] = 0;
                    365:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    366:                return(0);
                    367:        if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
                    368:                m->lastnamed = sec;
                    369:
                    370:        switch ((m->lastsec = sec)) {
                    371:        case (SEC_RETURN_VALUES):
                    372:                /* FALLTHROUGH */
                    373:        case (SEC_ERRORS):
                    374:                switch (m->meta.msec) {
                    375:                case (2):
                    376:                        /* FALLTHROUGH */
                    377:                case (3):
                    378:                        /* FALLTHROUGH */
                    379:                case (9):
                    380:                        break;
                    381:                default:
                    382:                        return(vwarn(m, WBADSEC));
                    383:                }
                    384:                break;
                    385:        default:
                    386:                break;
                    387:        }
                    388:        return(1);
                    389: }
                    390:
                    391:
                    392: static int
                    393: post_dt(POST_ARGS)
                    394: {
                    395:        struct mdoc_node *n;
                    396:        const char       *cp;
                    397:        char             *ep;
                    398:        long              lval;
                    399:
                    400:        if (m->meta.title)
                    401:                free(m->meta.title);
                    402:        if (m->meta.vol)
                    403:                free(m->meta.vol);
                    404:        if (m->meta.arch)
                    405:                free(m->meta.arch);
                    406:
                    407:        m->meta.title = m->meta.vol = m->meta.arch = NULL;
                    408:        m->meta.msec = 0;
                    409:
                    410:        /* Handles: `.Dt'
                    411:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    412:         */
                    413:
                    414:        if (NULL == (n = m->last->child)) {
                    415:                if (NULL == (m->meta.title = strdup("unknown")))
1.12    ! schwarze  416:                        return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  417:                if (NULL == (m->meta.vol = strdup("local")))
1.12    ! schwarze  418:                        return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  419:                return(post_prol(m));
                    420:        }
                    421:
                    422:        /* Handles: `.Dt TITLE'
                    423:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                    424:         */
                    425:
                    426:        if (NULL == (m->meta.title = strdup(n->string)))
1.12    ! schwarze  427:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  428:
                    429:        if (NULL == (n = n->next)) {
                    430:                if (NULL == (m->meta.vol = strdup("local")))
1.12    ! schwarze  431:                        return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  432:                return(post_prol(m));
                    433:        }
                    434:
                    435:        /* Handles: `.Dt TITLE SEC'
                    436:         *   --> title = TITLE, volume = SEC is msec ?
                    437:         *           format(msec) : SEC,
                    438:         *       msec = SEC is msec ? atoi(msec) : 0,
                    439:         *       arch = NULL
                    440:         */
                    441:
                    442:        cp = mdoc_a2msec(n->string);
                    443:        if (cp) {
                    444:                if (NULL == (m->meta.vol = strdup(cp)))
1.12    ! schwarze  445:                        return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  446:                errno = 0;
                    447:                lval = strtol(n->string, &ep, 10);
                    448:                if (n->string[0] != '\0' && *ep == '\0')
                    449:                        m->meta.msec = (int)lval;
                    450:        } else if (NULL == (m->meta.vol = strdup(n->string)))
1.12    ! schwarze  451:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  452:
                    453:        if (NULL == (n = n->next))
                    454:                return(post_prol(m));
                    455:
                    456:        /* Handles: `.Dt TITLE SEC VOL'
                    457:         *   --> title = TITLE, volume = VOL is vol ?
                    458:         *       format(VOL) :
                    459:         *           VOL is arch ? format(arch) :
                    460:         *               VOL
                    461:         */
                    462:
                    463:        cp = mdoc_a2vol(n->string);
                    464:        if (cp) {
                    465:                free(m->meta.vol);
                    466:                if (NULL == (m->meta.vol = strdup(cp)))
1.12    ! schwarze  467:                        return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  468:                n = n->next;
                    469:        } else {
                    470:                cp = mdoc_a2arch(n->string);
                    471:                if (NULL == cp) {
                    472:                        free(m->meta.vol);
                    473:                        if (NULL == (m->meta.vol = strdup(n->string)))
1.12    ! schwarze  474:                                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  475:                } else if (NULL == (m->meta.arch = strdup(cp)))
1.12    ! schwarze  476:                        return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  477:        }
                    478:
                    479:        /* Ignore any subsequent parameters... */
                    480:
                    481:        return(post_prol(m));
                    482: }
                    483:
                    484:
                    485: static int
                    486: post_os(POST_ARGS)
                    487: {
                    488:        char              buf[64];
                    489:        struct utsname    utsname;
                    490:
                    491:        if (m->meta.os)
                    492:                free(m->meta.os);
                    493:
                    494:        buf[0] = 0;
                    495:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    496:                return(0);
                    497:
                    498:        if (0 == buf[0]) {
                    499:                if (-1 == uname(&utsname))
1.12    ! schwarze  500:                        return(mdoc_nerr(m, m->last, EUTSNAME));
1.1       kristaps  501:                if (strlcat(buf, utsname.sysname, 64) >= 64)
1.12    ! schwarze  502:                        return(mdoc_nerr(m, m->last, ETOOLONG));
1.1       kristaps  503:                if (strlcat(buf, " ", 64) >= 64)
1.12    ! schwarze  504:                        return(mdoc_nerr(m, m->last, ETOOLONG));
1.1       kristaps  505:                if (strlcat(buf, utsname.release, 64) >= 64)
1.12    ! schwarze  506:                        return(mdoc_nerr(m, m->last, ETOOLONG));
1.1       kristaps  507:        }
                    508:
                    509:        if (NULL == (m->meta.os = strdup(buf)))
1.12    ! schwarze  510:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  511:
1.5       schwarze  512:        m->flags |= MDOC_PBODY;
1.1       kristaps  513:        return(post_prol(m));
                    514: }
                    515:
                    516:
                    517: /*
                    518:  * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
                    519:  * Uses the first head macro.
                    520:  */
                    521: static int
                    522: post_bl_tagwidth(struct mdoc *m)
                    523: {
                    524:        struct mdoc_node  *n;
                    525:        int                sz;
                    526:        char               buf[32];
                    527:
                    528:        /*
                    529:         * Use the text width, if a text node, or the default macro
                    530:         * width if a macro.
                    531:         */
                    532:
1.3       schwarze  533:        n = m->last->body->child;
                    534:        if (n) {
1.1       kristaps  535:                assert(MDOC_BLOCK == n->type);
                    536:                assert(MDOC_It == n->tok);
                    537:                n = n->head->child;
                    538:        }
                    539:
                    540:        sz = 10; /* Default size. */
                    541:
                    542:        if (n) {
                    543:                if (MDOC_TEXT != n->type) {
                    544:                        if (0 == (sz = (int)mdoc_macro2len(n->tok)))
                    545:                                if ( ! vwarn(m, WNOWIDTH))
                    546:                                        return(0);
                    547:                } else
                    548:                        sz = (int)strlen(n->string) + 1;
                    549:        }
                    550:
                    551:        if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
1.12    ! schwarze  552:                return(mdoc_nerr(m, m->last, ENUMFMT));
1.1       kristaps  553:
                    554:        /*
                    555:         * We have to dynamically add this to the macro's argument list.
                    556:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    557:         */
                    558:
                    559:        n = m->last;
                    560:        assert(n->args);
                    561:        sz = (int)(n->args->argc)++;
                    562:
                    563:        n->args->argv = realloc(n->args->argv,
                    564:                        n->args->argc * sizeof(struct mdoc_argv));
                    565:
                    566:        if (NULL == n->args->argv)
1.12    ! schwarze  567:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  568:
                    569:        n->args->argv[sz].arg = MDOC_Width;
                    570:        n->args->argv[sz].line = m->last->line;
                    571:        n->args->argv[sz].pos = m->last->pos;
                    572:        n->args->argv[sz].sz = 1;
                    573:        n->args->argv[sz].value = calloc(1, sizeof(char *));
                    574:
                    575:        if (NULL == n->args->argv[sz].value)
1.12    ! schwarze  576:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  577:        if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
1.12    ! schwarze  578:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  579:
                    580:        return(1);
                    581: }
                    582:
                    583:
                    584: static int
                    585: post_bl_width(struct mdoc *m)
                    586: {
                    587:        size_t            width;
                    588:        int               i, tok;
                    589:        char              buf[32];
                    590:        char             *p;
                    591:
                    592:        if (NULL == m->last->args)
                    593:                return(1);
                    594:
                    595:        for (i = 0; i < (int)m->last->args->argc; i++)
                    596:                if (MDOC_Width == m->last->args->argv[i].arg)
                    597:                        break;
                    598:
                    599:        if (i == (int)m->last->args->argc)
                    600:                return(1);
                    601:        p = m->last->args->argv[i].value[0];
                    602:
                    603:        /*
                    604:         * If the value to -width is a macro, then we re-write it to be
                    605:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    606:         */
                    607:
                    608:        if (0 == strcmp(p, "Ds"))
1.9       schwarze  609:                width = 6;
1.1       kristaps  610:        else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
                    611:                return(1);
                    612:        else if (0 == (width = mdoc_macro2len(tok)))
                    613:                return(vwarn(m, WNOWIDTH));
                    614:
                    615:        /* The value already exists: free and reallocate it. */
                    616:
                    617:        if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
1.12    ! schwarze  618:                return(mdoc_nerr(m, m->last, ENUMFMT));
1.1       kristaps  619:
                    620:        free(m->last->args->argv[i].value[0]);
                    621:        m->last->args->argv[i].value[0] = strdup(buf);
                    622:        if (NULL == m->last->args->argv[i].value[0])
1.12    ! schwarze  623:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  624:
                    625:        return(1);
                    626: }
                    627:
                    628:
                    629: static int
1.6       schwarze  630: post_bl_head(POST_ARGS)
                    631: {
                    632:        int                      i, c;
                    633:        struct mdoc_node        *n, *nn, *nnp;
                    634:
                    635:        if (NULL == m->last->child)
                    636:                return(1);
                    637:
                    638:        n = m->last->parent;
                    639:        assert(n->args);
                    640:
                    641:        for (c = 0; c < (int)n->args->argc; c++)
                    642:                if (MDOC_Column == n->args->argv[c].arg)
                    643:                        break;
                    644:
                    645:        /* Only process -column. */
                    646:
                    647:        if (c == (int)n->args->argc)
                    648:                return(1);
                    649:
                    650:        assert(0 == n->args->argv[c].sz);
                    651:
                    652:        /*
                    653:         * Accomodate for new-style groff column syntax.  Shuffle the
                    654:         * child nodes, all of which must be TEXT, as arguments for the
                    655:         * column field.  Then, delete the head children.
                    656:         */
                    657:
1.8       schwarze  658:        n->args->argv[c].sz = (size_t)m->last->nchild;
                    659:        n->args->argv[c].value = malloc
                    660:                ((size_t)m->last->nchild * sizeof(char *));
1.6       schwarze  661:
                    662:        for (i = 0, nn = m->last->child; nn; i++) {
                    663:                n->args->argv[c].value[i] = nn->string;
                    664:                nn->string = NULL;
                    665:                nnp = nn;
                    666:                nn = nn->next;
                    667:                mdoc_node_free(nnp);
                    668:        }
                    669:
1.8       schwarze  670:        m->last->nchild = 0;
1.6       schwarze  671:        m->last->child = NULL;
1.8       schwarze  672:
1.6       schwarze  673:        return(1);
                    674: }
                    675:
                    676:
                    677: static int
1.1       kristaps  678: post_bl(POST_ARGS)
                    679: {
                    680:        int               i, r, len;
                    681:
1.6       schwarze  682:        if (MDOC_HEAD == m->last->type)
                    683:                return(post_bl_head(m));
1.1       kristaps  684:        if (MDOC_BLOCK != m->last->type)
                    685:                return(1);
                    686:
                    687:        /*
                    688:         * These are fairly complicated, so we've broken them into two
                    689:         * functions.  post_bl_tagwidth() is called when a -tag is
                    690:         * specified, but no -width (it must be guessed).  The second
                    691:         * when a -width is specified (macro indicators must be
                    692:         * rewritten into real lengths).
                    693:         */
                    694:
                    695:        len = (int)(m->last->args ? m->last->args->argc : 0);
                    696:
                    697:        for (r = i = 0; i < len; i++) {
                    698:                if (MDOC_Tag == m->last->args->argv[i].arg)
                    699:                        r |= 1 << 0;
                    700:                if (MDOC_Width == m->last->args->argv[i].arg)
                    701:                        r |= 1 << 1;
                    702:        }
                    703:
                    704:        if (r & (1 << 0) && ! (r & (1 << 1))) {
                    705:                if ( ! post_bl_tagwidth(m))
                    706:                        return(0);
                    707:        } else if (r & (1 << 1))
                    708:                if ( ! post_bl_width(m))
                    709:                        return(0);
                    710:
1.3       schwarze  711:        return(1);
                    712: }
                    713:
                    714:
                    715: static int
                    716: post_lk(POST_ARGS)
                    717: {
                    718:        struct mdoc_node *n;
                    719:
                    720:        if (m->last->child)
                    721:                return(1);
                    722:
                    723:        n = m->last;
                    724:        m->next = MDOC_NEXT_CHILD;
                    725:        /* FIXME: this isn't documented anywhere! */
                    726:        if ( ! mdoc_word_alloc(m, m->last->line,
                    727:                                m->last->pos, "~"))
                    728:                return(0);
                    729:
                    730:        m->last = n;
                    731:        m->next = MDOC_NEXT_SIBLING;
1.1       kristaps  732:        return(1);
                    733: }
                    734:
                    735:
                    736: static int
                    737: post_ar(POST_ARGS)
                    738: {
                    739:        struct mdoc_node *n;
                    740:
                    741:        if (m->last->child)
                    742:                return(1);
                    743:
                    744:        n = m->last;
                    745:        m->next = MDOC_NEXT_CHILD;
                    746:        if ( ! mdoc_word_alloc(m, m->last->line,
                    747:                                m->last->pos, "file"))
                    748:                return(0);
                    749:        m->next = MDOC_NEXT_SIBLING;
                    750:        if ( ! mdoc_word_alloc(m, m->last->line,
                    751:                                m->last->pos, "..."))
                    752:                return(0);
                    753:
                    754:        m->last = n;
                    755:        m->next = MDOC_NEXT_SIBLING;
                    756:        return(1);
                    757: }
                    758:
                    759:
                    760: static int
                    761: post_dd(POST_ARGS)
                    762: {
                    763:        char              buf[64];
                    764:
                    765:        buf[0] = 0;
                    766:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    767:                return(0);
                    768:
                    769:        if (0 == (m->meta.date = mdoc_atotime(buf))) {
                    770:                if ( ! vwarn(m, WBADDATE))
                    771:                        return(0);
                    772:                m->meta.date = time(NULL);
                    773:        }
                    774:
                    775:        return(post_prol(m));
                    776: }
                    777:
                    778:
                    779: static int
                    780: post_prol(POST_ARGS)
                    781: {
                    782:        struct mdoc_node *n;
                    783:
                    784:        /*
                    785:         * The end document shouldn't have the prologue macros as part
                    786:         * of the syntax tree (they encompass only meta-data).
                    787:         */
                    788:
                    789:        if (m->last->parent->child == m->last)
                    790:                m->last->parent->child = m->last->prev;
                    791:        if (m->last->prev)
                    792:                m->last->prev->next = NULL;
                    793:
                    794:        n = m->last;
                    795:        assert(NULL == m->last->next);
                    796:
                    797:        if (m->last->prev) {
                    798:                m->last = m->last->prev;
                    799:                m->next = MDOC_NEXT_SIBLING;
                    800:        } else {
                    801:                m->last = m->last->parent;
                    802:                m->next = MDOC_NEXT_CHILD;
                    803:        }
                    804:
                    805:        mdoc_node_freelist(n);
                    806:        return(1);
                    807: }
                    808:
                    809:
                    810: static int
                    811: pre_dl(PRE_ARGS)
                    812: {
                    813:
1.7       schwarze  814:        if (MDOC_BODY == n->type)
                    815:                m->flags |= MDOC_LITERAL;
1.1       kristaps  816:        return(1);
                    817: }
                    818:
                    819:
                    820: static int
                    821: pre_bd(PRE_ARGS)
                    822: {
                    823:        int              i;
                    824:
                    825:        if (MDOC_BODY != n->type)
                    826:                return(1);
                    827:
                    828:        /* Enter literal context if `Bd -literal' or * -unfilled'. */
                    829:
                    830:        for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
                    831:                if (MDOC_Literal == n->args->argv[i].arg)
                    832:                        break;
                    833:                else if (MDOC_Unfilled == n->args->argv[i].arg)
                    834:                        break;
                    835:
                    836:        if (i < (int)n->args->argc)
                    837:                m->flags |= MDOC_LITERAL;
                    838:
                    839:        return(1);
                    840: }
                    841:
                    842:
                    843: static int
                    844: post_display(POST_ARGS)
                    845: {
                    846:
                    847:        if (MDOC_BODY == m->last->type)
                    848:                m->flags &= ~MDOC_LITERAL;
                    849:        return(1);
                    850: }
                    851:
                    852: