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

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