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

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