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

1.5     ! schwarze    1: /*     $Id: mdoc_action.c,v 1.4 2009/06/18 23:34:53 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] = {
1.4       schwarze   74:        { NULL, NULL }, /* Ap */
1.1       kristaps   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 }, /* Lp */
1.3       schwarze  182:        { NULL, post_lk }, /* Lk */
1.1       kristaps  183:        { NULL, NULL }, /* Mt */
                    184:        { NULL, NULL }, /* Brq */
                    185:        { NULL, NULL }, /* Bro */
                    186:        { NULL, NULL }, /* Brc */
                    187:        { NULL, NULL }, /* %C */
                    188:        { NULL, NULL }, /* Es */
                    189:        { NULL, NULL }, /* En */
                    190:        { NULL, NULL }, /* Dx */
                    191:        { NULL, NULL }, /* %Q */
                    192: };
                    193:
                    194:
                    195: int
                    196: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
                    197: {
                    198:
                    199:        switch (n->type) {
                    200:        case (MDOC_ROOT):
                    201:                /* FALLTHROUGH */
                    202:        case (MDOC_TEXT):
                    203:                return(1);
                    204:        default:
                    205:                break;
                    206:        }
                    207:
                    208:        if (NULL == mdoc_actions[n->tok].pre)
                    209:                return(1);
                    210:        return((*mdoc_actions[n->tok].pre)(m, n));
                    211: }
                    212:
                    213:
                    214: int
                    215: mdoc_action_post(struct mdoc *m)
                    216: {
                    217:
                    218:        if (MDOC_ACTED & m->last->flags)
                    219:                return(1);
                    220:        m->last->flags |= MDOC_ACTED;
                    221:
                    222:        switch (m->last->type) {
                    223:        case (MDOC_TEXT):
                    224:                /* FALLTHROUGH */
                    225:        case (MDOC_ROOT):
                    226:                return(1);
                    227:        default:
                    228:                break;
                    229:        }
                    230:
                    231:        if (NULL == mdoc_actions[m->last->tok].post)
                    232:                return(1);
                    233:        return((*mdoc_actions[m->last->tok].post)(m));
                    234: }
                    235:
                    236:
                    237: static int
                    238: concat(struct mdoc *m, const struct mdoc_node *n,
                    239:                char *buf, size_t sz)
                    240: {
                    241:
                    242:        for ( ; n; n = n->next) {
                    243:                assert(MDOC_TEXT == n->type);
                    244:                if (strlcat(buf, n->string, sz) >= sz)
                    245:                        return(nerr(m, n, ETOOLONG));
                    246:                if (NULL == n->next)
                    247:                        continue;
                    248:                if (strlcat(buf, " ", sz) >= sz)
                    249:                        return(nerr(m, n, ETOOLONG));
                    250:        }
                    251:
                    252:        return(1);
                    253: }
                    254:
                    255:
                    256: static int
                    257: perr(struct mdoc *m, int line, int pos, enum merr type)
                    258: {
                    259:        char            *p;
                    260:
                    261:        p = NULL;
                    262:        switch (type) {
                    263:        case (ENUMFMT):
                    264:                p = "bad number format";
                    265:                break;
                    266:        case (ETOOLONG):
                    267:                p = "argument text too long";
                    268:                break;
                    269:        case (EMALLOC):
                    270:                p = "memory exhausted";
                    271:                break;
                    272:        }
                    273:        assert(p);
                    274:        return(mdoc_perr(m, line, pos, p));
                    275: }
                    276:
                    277:
                    278: static int
                    279: pwarn(struct mdoc *m, int line, int pos, enum mwarn type)
                    280: {
                    281:        char            *p;
                    282:        int              c;
                    283:
                    284:        p = NULL;
                    285:        c = WARN_SYNTAX;
                    286:        switch (type) {
                    287:        case (WBADSEC):
                    288:                p = "inappropriate document section in manual section";
                    289:                c = WARN_COMPAT;
                    290:                break;
                    291:        case (WNOWIDTH):
                    292:                p = "cannot determine default width";
                    293:                break;
                    294:        case (WBADDATE):
                    295:                p = "malformed date syntax";
                    296:                break;
                    297:        }
                    298:        assert(p);
                    299:        return(mdoc_pwarn(m, line, pos, c, p));
                    300: }
                    301:
                    302:
                    303: static int
                    304: post_std(POST_ARGS)
                    305: {
                    306:
                    307:        /*
                    308:         * If '-std' is invoked without an argument, fill it in with our
                    309:         * name (if it's been set).
                    310:         */
                    311:
                    312:        if (NULL == m->last->args)
                    313:                return(1);
                    314:        if (m->last->args->argv[0].sz)
                    315:                return(1);
                    316:
                    317:        assert(m->meta.name);
                    318:
                    319:        m->last->args->argv[0].value = calloc(1, sizeof(char *));
                    320:        if (NULL == m->last->args->argv[0].value)
                    321:                return(verr(m, EMALLOC));
                    322:
                    323:        m->last->args->argv[0].sz = 1;
                    324:        m->last->args->argv[0].value[0] = strdup(m->meta.name);
                    325:        if (NULL == m->last->args->argv[0].value[0])
                    326:                return(verr(m, EMALLOC));
                    327:
                    328:        return(1);
                    329: }
                    330:
                    331:
                    332: static int
                    333: post_nm(POST_ARGS)
                    334: {
                    335:        char             buf[64];
                    336:
                    337:        if (m->meta.name)
                    338:                return(1);
                    339:
                    340:        buf[0] = 0;
                    341:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    342:                return(0);
1.2       schwarze  343:
1.1       kristaps  344:        if (NULL == (m->meta.name = strdup(buf)))
                    345:                return(verr(m, EMALLOC));
1.2       schwarze  346:
1.1       kristaps  347:        return(1);
                    348: }
                    349:
                    350:
                    351: static int
                    352: post_sh(POST_ARGS)
                    353: {
                    354:        enum mdoc_sec    sec;
                    355:        char             buf[64];
                    356:
                    357:        /*
                    358:         * We keep track of the current section /and/ the "named"
                    359:         * section, which is one of the conventional ones, in order to
                    360:         * check ordering.
                    361:         */
                    362:
                    363:        if (MDOC_HEAD != m->last->type)
                    364:                return(1);
                    365:
                    366:        buf[0] = 0;
                    367:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    368:                return(0);
                    369:        if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
                    370:                m->lastnamed = sec;
                    371:
                    372:        switch ((m->lastsec = sec)) {
                    373:        case (SEC_RETURN_VALUES):
                    374:                /* FALLTHROUGH */
                    375:        case (SEC_ERRORS):
                    376:                switch (m->meta.msec) {
                    377:                case (2):
                    378:                        /* FALLTHROUGH */
                    379:                case (3):
                    380:                        /* FALLTHROUGH */
                    381:                case (9):
                    382:                        break;
                    383:                default:
                    384:                        return(vwarn(m, WBADSEC));
                    385:                }
                    386:                break;
                    387:        default:
                    388:                break;
                    389:        }
                    390:        return(1);
                    391: }
                    392:
                    393:
                    394: static int
                    395: post_dt(POST_ARGS)
                    396: {
                    397:        struct mdoc_node *n;
                    398:        const char       *cp;
                    399:        char             *ep;
                    400:        long              lval;
                    401:
                    402:        if (m->meta.title)
                    403:                free(m->meta.title);
                    404:        if (m->meta.vol)
                    405:                free(m->meta.vol);
                    406:        if (m->meta.arch)
                    407:                free(m->meta.arch);
                    408:
                    409:        m->meta.title = m->meta.vol = m->meta.arch = NULL;
                    410:        m->meta.msec = 0;
                    411:
                    412:        /* Handles: `.Dt'
                    413:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    414:         */
                    415:
                    416:        if (NULL == (n = m->last->child)) {
                    417:                if (NULL == (m->meta.title = strdup("unknown")))
                    418:                        return(verr(m, EMALLOC));
                    419:                if (NULL == (m->meta.vol = strdup("local")))
                    420:                        return(verr(m, EMALLOC));
                    421:                return(post_prol(m));
                    422:        }
                    423:
                    424:        /* Handles: `.Dt TITLE'
                    425:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                    426:         */
                    427:
                    428:        if (NULL == (m->meta.title = strdup(n->string)))
                    429:                return(verr(m, EMALLOC));
                    430:
                    431:        if (NULL == (n = n->next)) {
                    432:                if (NULL == (m->meta.vol = strdup("local")))
                    433:                        return(verr(m, EMALLOC));
                    434:                return(post_prol(m));
                    435:        }
                    436:
                    437:        /* Handles: `.Dt TITLE SEC'
                    438:         *   --> title = TITLE, volume = SEC is msec ?
                    439:         *           format(msec) : SEC,
                    440:         *       msec = SEC is msec ? atoi(msec) : 0,
                    441:         *       arch = NULL
                    442:         */
                    443:
                    444:        cp = mdoc_a2msec(n->string);
                    445:        if (cp) {
                    446:                if (NULL == (m->meta.vol = strdup(cp)))
                    447:                        return(verr(m, EMALLOC));
                    448:                errno = 0;
                    449:                lval = strtol(n->string, &ep, 10);
                    450:                if (n->string[0] != '\0' && *ep == '\0')
                    451:                        m->meta.msec = (int)lval;
                    452:        } else if (NULL == (m->meta.vol = strdup(n->string)))
                    453:                return(verr(m, EMALLOC));
                    454:
                    455:        if (NULL == (n = n->next))
                    456:                return(post_prol(m));
                    457:
                    458:        /* Handles: `.Dt TITLE SEC VOL'
                    459:         *   --> title = TITLE, volume = VOL is vol ?
                    460:         *       format(VOL) :
                    461:         *           VOL is arch ? format(arch) :
                    462:         *               VOL
                    463:         */
                    464:
                    465:        cp = mdoc_a2vol(n->string);
                    466:        if (cp) {
                    467:                free(m->meta.vol);
                    468:                if (NULL == (m->meta.vol = strdup(cp)))
                    469:                        return(verr(m, EMALLOC));
                    470:                n = n->next;
                    471:        } else {
                    472:                cp = mdoc_a2arch(n->string);
                    473:                if (NULL == cp) {
                    474:                        free(m->meta.vol);
                    475:                        if (NULL == (m->meta.vol = strdup(n->string)))
                    476:                                return(verr(m, EMALLOC));
                    477:                } else if (NULL == (m->meta.arch = strdup(cp)))
                    478:                        return(verr(m, EMALLOC));
                    479:        }
                    480:
                    481:        /* Ignore any subsequent parameters... */
                    482:
                    483:        return(post_prol(m));
                    484: }
                    485:
                    486:
                    487: static int
                    488: post_os(POST_ARGS)
                    489: {
                    490:        char              buf[64];
                    491:        struct utsname    utsname;
                    492:
                    493:        if (m->meta.os)
                    494:                free(m->meta.os);
                    495:
                    496:        buf[0] = 0;
                    497:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    498:                return(0);
                    499:
                    500:        if (0 == buf[0]) {
                    501:                if (-1 == uname(&utsname))
                    502:                        return(mdoc_err(m, "utsname"));
                    503:                if (strlcat(buf, utsname.sysname, 64) >= 64)
                    504:                        return(verr(m, ETOOLONG));
                    505:                if (strlcat(buf, " ", 64) >= 64)
                    506:                        return(verr(m, ETOOLONG));
                    507:                if (strlcat(buf, utsname.release, 64) >= 64)
                    508:                        return(verr(m, ETOOLONG));
                    509:        }
                    510:
                    511:        if (NULL == (m->meta.os = strdup(buf)))
                    512:                return(verr(m, EMALLOC));
                    513:
1.5     ! schwarze  514:        m->flags |= MDOC_PBODY;
1.1       kristaps  515:        return(post_prol(m));
                    516: }
                    517:
                    518:
                    519: /*
                    520:  * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
                    521:  * Uses the first head macro.
                    522:  */
                    523: static int
                    524: post_bl_tagwidth(struct mdoc *m)
                    525: {
                    526:        struct mdoc_node  *n;
                    527:        int                sz;
                    528:        char               buf[32];
                    529:
                    530:        /*
                    531:         * Use the text width, if a text node, or the default macro
                    532:         * width if a macro.
                    533:         */
                    534:
1.3       schwarze  535:        n = m->last->body->child;
                    536:        if (n) {
1.1       kristaps  537:                assert(MDOC_BLOCK == n->type);
                    538:                assert(MDOC_It == n->tok);
                    539:                n = n->head->child;
                    540:        }
                    541:
                    542:        sz = 10; /* Default size. */
                    543:
                    544:        if (n) {
                    545:                if (MDOC_TEXT != n->type) {
                    546:                        if (0 == (sz = (int)mdoc_macro2len(n->tok)))
                    547:                                if ( ! vwarn(m, WNOWIDTH))
                    548:                                        return(0);
                    549:                } else
                    550:                        sz = (int)strlen(n->string) + 1;
                    551:        }
                    552:
                    553:        if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
                    554:                return(verr(m, ENUMFMT));
                    555:
                    556:        /*
                    557:         * We have to dynamically add this to the macro's argument list.
                    558:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    559:         */
                    560:
                    561:        n = m->last;
                    562:        assert(n->args);
                    563:        sz = (int)(n->args->argc)++;
                    564:
                    565:        n->args->argv = realloc(n->args->argv,
                    566:                        n->args->argc * sizeof(struct mdoc_argv));
                    567:
                    568:        if (NULL == n->args->argv)
                    569:                return(verr(m, EMALLOC));
                    570:
                    571:        n->args->argv[sz].arg = MDOC_Width;
                    572:        n->args->argv[sz].line = m->last->line;
                    573:        n->args->argv[sz].pos = m->last->pos;
                    574:        n->args->argv[sz].sz = 1;
                    575:        n->args->argv[sz].value = calloc(1, sizeof(char *));
                    576:
                    577:        if (NULL == n->args->argv[sz].value)
                    578:                return(verr(m, EMALLOC));
                    579:        if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
                    580:                return(verr(m, EMALLOC));
                    581:
                    582:        return(1);
                    583: }
                    584:
                    585:
                    586: static int
                    587: post_bl_width(struct mdoc *m)
                    588: {
                    589:        size_t            width;
                    590:        int               i, tok;
                    591:        char              buf[32];
                    592:        char             *p;
                    593:
                    594:        if (NULL == m->last->args)
                    595:                return(1);
                    596:
                    597:        for (i = 0; i < (int)m->last->args->argc; i++)
                    598:                if (MDOC_Width == m->last->args->argv[i].arg)
                    599:                        break;
                    600:
                    601:        if (i == (int)m->last->args->argc)
                    602:                return(1);
                    603:        p = m->last->args->argv[i].value[0];
                    604:
                    605:        /*
                    606:         * If the value to -width is a macro, then we re-write it to be
                    607:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    608:         */
                    609:
                    610:        if (0 == strcmp(p, "Ds"))
                    611:                width = 8;
                    612:        else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
                    613:                return(1);
                    614:        else if (0 == (width = mdoc_macro2len(tok)))
                    615:                return(vwarn(m, WNOWIDTH));
                    616:
                    617:        /* The value already exists: free and reallocate it. */
                    618:
                    619:        if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
                    620:                return(verr(m, ENUMFMT));
                    621:
                    622:        free(m->last->args->argv[i].value[0]);
                    623:        m->last->args->argv[i].value[0] = strdup(buf);
                    624:        if (NULL == m->last->args->argv[i].value[0])
                    625:                return(verr(m, EMALLOC));
                    626:
                    627:        return(1);
                    628: }
                    629:
                    630:
                    631: static int
                    632: post_bl(POST_ARGS)
                    633: {
                    634:        int               i, r, len;
                    635:
                    636:        if (MDOC_BLOCK != m->last->type)
                    637:                return(1);
                    638:
                    639:        /*
                    640:         * These are fairly complicated, so we've broken them into two
                    641:         * functions.  post_bl_tagwidth() is called when a -tag is
                    642:         * specified, but no -width (it must be guessed).  The second
                    643:         * when a -width is specified (macro indicators must be
                    644:         * rewritten into real lengths).
                    645:         */
                    646:
                    647:        len = (int)(m->last->args ? m->last->args->argc : 0);
                    648:
                    649:        for (r = i = 0; i < len; i++) {
                    650:                if (MDOC_Tag == m->last->args->argv[i].arg)
                    651:                        r |= 1 << 0;
                    652:                if (MDOC_Width == m->last->args->argv[i].arg)
                    653:                        r |= 1 << 1;
                    654:        }
                    655:
                    656:        if (r & (1 << 0) && ! (r & (1 << 1))) {
                    657:                if ( ! post_bl_tagwidth(m))
                    658:                        return(0);
                    659:        } else if (r & (1 << 1))
                    660:                if ( ! post_bl_width(m))
                    661:                        return(0);
                    662:
1.3       schwarze  663:        return(1);
                    664: }
                    665:
                    666:
                    667: static int
                    668: post_lk(POST_ARGS)
                    669: {
                    670:        struct mdoc_node *n;
                    671:
                    672:        if (m->last->child)
                    673:                return(1);
                    674:
                    675:        n = m->last;
                    676:        m->next = MDOC_NEXT_CHILD;
                    677:        /* FIXME: this isn't documented anywhere! */
                    678:        if ( ! mdoc_word_alloc(m, m->last->line,
                    679:                                m->last->pos, "~"))
                    680:                return(0);
                    681:
                    682:        m->last = n;
                    683:        m->next = MDOC_NEXT_SIBLING;
1.1       kristaps  684:        return(1);
                    685: }
                    686:
                    687:
                    688: static int
                    689: post_ar(POST_ARGS)
                    690: {
                    691:        struct mdoc_node *n;
                    692:
                    693:        if (m->last->child)
                    694:                return(1);
                    695:
                    696:        n = m->last;
                    697:        m->next = MDOC_NEXT_CHILD;
                    698:        if ( ! mdoc_word_alloc(m, m->last->line,
                    699:                                m->last->pos, "file"))
                    700:                return(0);
                    701:        m->next = MDOC_NEXT_SIBLING;
                    702:        if ( ! mdoc_word_alloc(m, m->last->line,
                    703:                                m->last->pos, "..."))
                    704:                return(0);
                    705:
                    706:        m->last = n;
                    707:        m->next = MDOC_NEXT_SIBLING;
                    708:        return(1);
                    709: }
                    710:
                    711:
                    712: static int
                    713: post_dd(POST_ARGS)
                    714: {
                    715:        char              buf[64];
                    716:
                    717:        buf[0] = 0;
                    718:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    719:                return(0);
                    720:
                    721:        if (0 == (m->meta.date = mdoc_atotime(buf))) {
                    722:                if ( ! vwarn(m, WBADDATE))
                    723:                        return(0);
                    724:                m->meta.date = time(NULL);
                    725:        }
                    726:
                    727:        return(post_prol(m));
                    728: }
                    729:
                    730:
                    731: static int
                    732: post_prol(POST_ARGS)
                    733: {
                    734:        struct mdoc_node *n;
                    735:
                    736:        /*
                    737:         * The end document shouldn't have the prologue macros as part
                    738:         * of the syntax tree (they encompass only meta-data).
                    739:         */
                    740:
                    741:        if (m->last->parent->child == m->last)
                    742:                m->last->parent->child = m->last->prev;
                    743:        if (m->last->prev)
                    744:                m->last->prev->next = NULL;
                    745:
                    746:        n = m->last;
                    747:        assert(NULL == m->last->next);
                    748:
                    749:        if (m->last->prev) {
                    750:                m->last = m->last->prev;
                    751:                m->next = MDOC_NEXT_SIBLING;
                    752:        } else {
                    753:                m->last = m->last->parent;
                    754:                m->next = MDOC_NEXT_CHILD;
                    755:        }
                    756:
                    757:        mdoc_node_freelist(n);
                    758:        return(1);
                    759: }
                    760:
                    761:
                    762: static int
                    763: pre_dl(PRE_ARGS)
                    764: {
                    765:
                    766:        if (MDOC_BODY != n->type)
                    767:                return(1);
                    768:        m->flags |= MDOC_LITERAL;
                    769:        return(1);
                    770: }
                    771:
                    772:
                    773: static int
                    774: pre_bd(PRE_ARGS)
                    775: {
                    776:        int              i;
                    777:
                    778:        if (MDOC_BODY != n->type)
                    779:                return(1);
                    780:
                    781:        /* Enter literal context if `Bd -literal' or * -unfilled'. */
                    782:
                    783:        for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
                    784:                if (MDOC_Literal == n->args->argv[i].arg)
                    785:                        break;
                    786:                else if (MDOC_Unfilled == n->args->argv[i].arg)
                    787:                        break;
                    788:
                    789:        if (i < (int)n->args->argc)
                    790:                m->flags |= MDOC_LITERAL;
                    791:
                    792:        return(1);
                    793: }
                    794:
                    795:
                    796: static int
                    797: post_display(POST_ARGS)
                    798: {
                    799:
                    800:        if (MDOC_BODY == m->last->type)
                    801:                m->flags &= ~MDOC_LITERAL;
                    802:        return(1);
                    803: }
                    804:
                    805: