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

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