[BACK]Return to mdoc_action.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/mdoc_action.c, Revision 1.8

1.8     ! kristaps    1: /*     $Id: mdoc_action.c,v 1.7 2009/04/06 09:48:35 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.8     ! kristaps    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.8     ! kristaps    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:
1.2       kristaps   33: enum   merr {
                     34:        ETOOLONG,
                     35:        EMALLOC,
                     36:        ENUMFMT
                     37: };
                     38:
1.1       kristaps   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);
1.2       kristaps   48: static int       perr(struct mdoc *, int, int, enum merr);
                     49: static int       concat(struct mdoc *, const struct mdoc_node *,
                     50:                        char *, size_t);
1.1       kristaps   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);
                     59: static int       post_nm(POST_ARGS);
                     60: static int       post_os(POST_ARGS);
                     61: static int       post_prol(POST_ARGS);
                     62: static int       post_sh(POST_ARGS);
                     63: static int       post_std(POST_ARGS);
                     64:
                     65: static int       pre_bd(PRE_ARGS);
                     66: static int       pre_dl(PRE_ARGS);
                     67:
1.2       kristaps   68: #define        vwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
                     69: #define        verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
                     70: #define        nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
1.1       kristaps   71:
                     72: const  struct actions mdoc_actions[MDOC_MAX] = {
                     73:        { NULL, NULL }, /* \" */
                     74:        { NULL, post_dd }, /* Dd */
                     75:        { NULL, post_dt }, /* Dt */
                     76:        { NULL, post_os }, /* Os */
                     77:        { NULL, post_sh }, /* Sh */
                     78:        { NULL, NULL }, /* Ss */
                     79:        { NULL, NULL }, /* Pp */
                     80:        { NULL, NULL }, /* D1 */
                     81:        { pre_dl, post_display }, /* Dl */
                     82:        { pre_bd, post_display }, /* Bd */
                     83:        { NULL, NULL }, /* Ed */
                     84:        { NULL, post_bl }, /* Bl */
                     85:        { NULL, NULL }, /* El */
                     86:        { NULL, NULL }, /* It */
                     87:        { NULL, NULL }, /* Ad */
                     88:        { NULL, NULL }, /* An */
                     89:        { NULL, post_ar }, /* Ar */
                     90:        { NULL, NULL }, /* Cd */
                     91:        { NULL, NULL }, /* Cm */
                     92:        { NULL, NULL }, /* Dv */
                     93:        { NULL, NULL }, /* Er */
                     94:        { NULL, NULL }, /* Ev */
                     95:        { NULL, post_std }, /* Ex */
                     96:        { NULL, NULL }, /* Fa */
                     97:        { NULL, NULL }, /* Fd */
                     98:        { NULL, NULL }, /* Fl */
                     99:        { NULL, NULL }, /* Fn */
                    100:        { NULL, NULL }, /* Ft */
                    101:        { NULL, NULL }, /* Ic */
                    102:        { NULL, NULL }, /* In */
                    103:        { NULL, NULL }, /* Li */
                    104:        { NULL, NULL }, /* Nd */
                    105:        { NULL, post_nm }, /* Nm */
                    106:        { NULL, NULL }, /* Op */
                    107:        { NULL, NULL }, /* Ot */
                    108:        { NULL, NULL }, /* Pa */
                    109:        { NULL, post_std }, /* Rv */
                    110:        { NULL, NULL }, /* St */
                    111:        { NULL, NULL }, /* Va */
                    112:        { NULL, NULL }, /* Vt */
                    113:        { NULL, NULL }, /* Xr */
                    114:        { NULL, NULL }, /* %A */
                    115:        { NULL, NULL }, /* %B */
                    116:        { NULL, NULL }, /* %D */
                    117:        { NULL, NULL }, /* %I */
                    118:        { NULL, NULL }, /* %J */
                    119:        { NULL, NULL }, /* %N */
                    120:        { NULL, NULL }, /* %O */
                    121:        { NULL, NULL }, /* %P */
                    122:        { NULL, NULL }, /* %R */
                    123:        { NULL, NULL }, /* %T */
                    124:        { NULL, NULL }, /* %V */
                    125:        { NULL, NULL }, /* Ac */
                    126:        { NULL, NULL }, /* Ao */
                    127:        { NULL, NULL }, /* Aq */
                    128:        { NULL, NULL }, /* At */
                    129:        { NULL, NULL }, /* Bc */
                    130:        { NULL, NULL }, /* Bf */
                    131:        { NULL, NULL }, /* Bo */
                    132:        { NULL, NULL }, /* Bq */
                    133:        { NULL, NULL }, /* Bsx */
                    134:        { NULL, NULL }, /* Bx */
                    135:        { NULL, NULL }, /* Db */
                    136:        { NULL, NULL }, /* Dc */
                    137:        { NULL, NULL }, /* Do */
                    138:        { NULL, NULL }, /* Dq */
                    139:        { NULL, NULL }, /* Ec */
                    140:        { NULL, NULL }, /* Ef */
                    141:        { NULL, NULL }, /* Em */
                    142:        { NULL, NULL }, /* Eo */
                    143:        { NULL, NULL }, /* Fx */
                    144:        { NULL, NULL }, /* Ms */
                    145:        { NULL, NULL }, /* No */
                    146:        { NULL, NULL }, /* Ns */
                    147:        { NULL, NULL }, /* Nx */
                    148:        { NULL, NULL }, /* Ox */
                    149:        { NULL, NULL }, /* Pc */
                    150:        { NULL, NULL }, /* Pf */
                    151:        { NULL, NULL }, /* Po */
                    152:        { NULL, NULL }, /* Pq */
                    153:        { NULL, NULL }, /* Qc */
                    154:        { NULL, NULL }, /* Ql */
                    155:        { NULL, NULL }, /* Qo */
                    156:        { NULL, NULL }, /* Qq */
                    157:        { NULL, NULL }, /* Re */
                    158:        { NULL, NULL }, /* Rs */
                    159:        { NULL, NULL }, /* Sc */
                    160:        { NULL, NULL }, /* So */
                    161:        { NULL, NULL }, /* Sq */
                    162:        { NULL, NULL }, /* Sm */
                    163:        { NULL, NULL }, /* Sx */
                    164:        { NULL, NULL }, /* Sy */
                    165:        { NULL, NULL }, /* Tn */
                    166:        { NULL, NULL }, /* Ux */
                    167:        { NULL, NULL }, /* Xc */
                    168:        { NULL, NULL }, /* Xo */
                    169:        { NULL, NULL }, /* Fo */
                    170:        { NULL, NULL }, /* Fc */
                    171:        { NULL, NULL }, /* Oo */
                    172:        { NULL, NULL }, /* Oc */
                    173:        { NULL, NULL }, /* Bk */
                    174:        { NULL, NULL }, /* Ek */
                    175:        { NULL, NULL }, /* Bt */
                    176:        { NULL, NULL }, /* Hf */
                    177:        { NULL, NULL }, /* Fr */
                    178:        { NULL, NULL }, /* Ud */
                    179:        { NULL, NULL }, /* Lb */
                    180:        { NULL, NULL }, /* Ap */
                    181:        { NULL, NULL }, /* Lp */
                    182:        { NULL, NULL }, /* Lk */
                    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:
1.2       kristaps  195: #ifdef __linux__
                    196: extern size_t  strlcat(char *, const char *, size_t);
                    197: #endif
                    198:
                    199:
1.1       kristaps  200: int
                    201: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
                    202: {
                    203:
                    204:        switch (n->type) {
                    205:        case (MDOC_ROOT):
1.2       kristaps  206:                /* FALLTHROUGH */
1.1       kristaps  207:        case (MDOC_TEXT):
1.2       kristaps  208:                return(1);
                    209:        default:
1.1       kristaps  210:                break;
                    211:        }
1.2       kristaps  212:
1.6       kristaps  213:        if (NULL == mdoc_actions[n->tok].pre)
1.2       kristaps  214:                return(1);
1.6       kristaps  215:        return((*mdoc_actions[n->tok].pre)(m, n));
1.1       kristaps  216: }
                    217:
                    218:
                    219: int
                    220: mdoc_action_post(struct mdoc *m)
                    221: {
                    222:
                    223:        if (MDOC_ACTED & m->last->flags)
                    224:                return(1);
                    225:        m->last->flags |= MDOC_ACTED;
                    226:
                    227:        switch (m->last->type) {
                    228:        case (MDOC_TEXT):
1.2       kristaps  229:                /* FALLTHROUGH */
1.1       kristaps  230:        case (MDOC_ROOT):
1.2       kristaps  231:                return(1);
                    232:        default:
1.1       kristaps  233:                break;
                    234:        }
1.2       kristaps  235:
                    236:        if (NULL == mdoc_actions[m->last->tok].post)
                    237:                return(1);
                    238:        return((*mdoc_actions[m->last->tok].post)(m));
                    239: }
                    240:
                    241:
                    242: static int
                    243: concat(struct mdoc *m, const struct mdoc_node *n,
                    244:                char *buf, size_t sz)
                    245: {
                    246:
                    247:        for ( ; n; n = n->next) {
                    248:                assert(MDOC_TEXT == n->type);
                    249:                if (strlcat(buf, n->string, sz) >= sz)
                    250:                        return(nerr(m, n, ETOOLONG));
                    251:                if (NULL == n->next)
                    252:                        continue;
                    253:                if (strlcat(buf, " ", sz) >= sz)
                    254:                        return(nerr(m, n, ETOOLONG));
                    255:        }
                    256:
1.1       kristaps  257:        return(1);
                    258: }
                    259:
                    260:
                    261: static int
1.2       kristaps  262: perr(struct mdoc *m, int line, int pos, enum merr type)
                    263: {
                    264:        char            *p;
                    265:
                    266:        p = NULL;
                    267:        switch (type) {
                    268:        case (ENUMFMT):
                    269:                p = "bad number format";
                    270:                break;
                    271:        case (ETOOLONG):
                    272:                p = "argument text too long";
                    273:                break;
                    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
1.1       kristaps  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)
1.2       kristaps  326:                return(verr(m, EMALLOC));
1.1       kristaps  327:
                    328:        m->last->args->argv[0].sz = 1;
1.2       kristaps  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:
1.1       kristaps  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);
1.5       kristaps  344:
                    345:        buf[0] = 0;
1.2       kristaps  346:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    347:                return(0);
1.6       kristaps  348:
1.2       kristaps  349:        if (NULL == (m->meta.name = strdup(buf)))
                    350:                return(verr(m, EMALLOC));
1.1       kristaps  351:
                    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);
1.5       kristaps  370:
                    371:        buf[0] = 0;
1.2       kristaps  372:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    373:                return(0);
1.1       kristaps  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:
1.2       kristaps  389:                        return(vwarn(m, WBADSEC));
1.1       kristaps  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)) {
1.2       kristaps  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));
1.1       kristaps  426:                return(post_prol(m));
                    427:        }
                    428:
                    429:        /* Handles: `.Dt TITLE'
                    430:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                    431:         */
                    432:
1.2       kristaps  433:        if (NULL == (m->meta.title = strdup(n->string)))
                    434:                return(verr(m, EMALLOC));
1.1       kristaps  435:
                    436:        if (NULL == (n = n->next)) {
1.2       kristaps  437:                if (NULL == (m->meta.vol = strdup("local")))
                    438:                        return(verr(m, EMALLOC));
1.1       kristaps  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) {
1.2       kristaps  451:                if (NULL == (m->meta.vol = strdup(cp)))
                    452:                        return(verr(m, EMALLOC));
1.1       kristaps  453:                errno = 0;
                    454:                lval = strtol(n->string, &ep, 10);
                    455:                if (n->string[0] != '\0' && *ep == '\0')
                    456:                        m->meta.msec = (int)lval;
1.2       kristaps  457:        } else if (NULL == (m->meta.vol = strdup(n->string)))
                    458:                return(verr(m, EMALLOC));
1.1       kristaps  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);
1.2       kristaps  473:                if (NULL == (m->meta.vol = strdup(cp)))
                    474:                        return(verr(m, EMALLOC));
1.1       kristaps  475:                n = n->next;
                    476:        } else {
                    477:                cp = mdoc_a2arch(n->string);
                    478:                if (NULL == cp) {
                    479:                        free(m->meta.vol);
1.2       kristaps  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));
1.1       kristaps  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);
1.4       kristaps  500:
                    501:        buf[0] = 0;
1.2       kristaps  502:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    503:                return(0);
1.1       kristaps  504:
                    505:        if (0 == buf[0]) {
                    506:                if (-1 == uname(&utsname))
                    507:                        return(mdoc_err(m, "utsname"));
1.4       kristaps  508:                if (strlcat(buf, utsname.sysname, 64) >= 64)
1.2       kristaps  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));
1.1       kristaps  514:        }
                    515:
1.2       kristaps  516:        if (NULL == (m->meta.os = strdup(buf)))
                    517:                return(verr(m, EMALLOC));
1.1       kristaps  518:        m->lastnamed = m->lastsec = SEC_BODY;
                    519:
                    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:
                    540:        if ((n = m->last->body->child)) {
                    541:                assert(MDOC_BLOCK == n->type);
                    542:                assert(MDOC_It == n->tok);
                    543:                n = n->head->child;
                    544:        }
                    545:
                    546:        sz = 10; /* Default size. */
                    547:
                    548:        if (n) {
                    549:                if (MDOC_TEXT != n->type) {
                    550:                        if (0 == (sz = (int)mdoc_macro2len(n->tok)))
1.2       kristaps  551:                                if ( ! vwarn(m, WNOWIDTH))
1.1       kristaps  552:                                        return(0);
                    553:                } else
                    554:                        sz = (int)strlen(n->string) + 1;
                    555:        }
                    556:
1.2       kristaps  557:        if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
                    558:                return(verr(m, ENUMFMT));
1.1       kristaps  559:
                    560:        /*
                    561:         * We have to dynamically add this to the macro's argument list.
                    562:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    563:         */
                    564:
                    565:        n = m->last;
                    566:        assert(n->args);
1.2       kristaps  567:        sz = (int)(n->args->argc)++;
1.1       kristaps  568:
1.2       kristaps  569:        n->args->argv = realloc(n->args->argv,
1.1       kristaps  570:                        n->args->argc * sizeof(struct mdoc_argv));
                    571:
1.2       kristaps  572:        if (NULL == n->args->argv)
                    573:                return(verr(m, EMALLOC));
                    574:
1.1       kristaps  575:        n->args->argv[sz].arg = MDOC_Width;
                    576:        n->args->argv[sz].line = m->last->line;
                    577:        n->args->argv[sz].pos = m->last->pos;
                    578:        n->args->argv[sz].sz = 1;
                    579:        n->args->argv[sz].value = calloc(1, sizeof(char *));
1.2       kristaps  580:
1.1       kristaps  581:        if (NULL == n->args->argv[sz].value)
1.2       kristaps  582:                return(verr(m, EMALLOC));
                    583:        if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
                    584:                return(verr(m, EMALLOC));
1.1       kristaps  585:
                    586:        return(1);
                    587: }
                    588:
                    589:
                    590: static int
                    591: post_bl_width(struct mdoc *m)
                    592: {
                    593:        size_t            width;
                    594:        int               i, tok;
                    595:        char              buf[32];
                    596:        char             *p;
                    597:
                    598:        if (NULL == m->last->args)
                    599:                return(1);
                    600:
                    601:        for (i = 0; i < (int)m->last->args->argc; i++)
                    602:                if (MDOC_Width == m->last->args->argv[i].arg)
                    603:                        break;
                    604:
                    605:        if (i == (int)m->last->args->argc)
                    606:                return(1);
                    607:        p = m->last->args->argv[i].value[0];
                    608:
                    609:        /*
                    610:         * If the value to -width is a macro, then we re-write it to be
                    611:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    612:         */
                    613:
1.2       kristaps  614:        if (0 == strcmp(p, "Ds"))
1.1       kristaps  615:                width = 8;
1.3       kristaps  616:        else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
1.1       kristaps  617:                return(1);
                    618:        else if (0 == (width = mdoc_macro2len(tok)))
1.2       kristaps  619:                return(vwarn(m, WNOWIDTH));
1.1       kristaps  620:
                    621:        /* The value already exists: free and reallocate it. */
                    622:
1.2       kristaps  623:        if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
                    624:                return(verr(m, ENUMFMT));
1.1       kristaps  625:
                    626:        free(m->last->args->argv[i].value[0]);
1.2       kristaps  627:        m->last->args->argv[i].value[0] = strdup(buf);
                    628:        if (NULL == m->last->args->argv[i].value[0])
                    629:                return(verr(m, EMALLOC));
1.1       kristaps  630:
                    631:        return(1);
                    632: }
                    633:
                    634:
                    635: static int
                    636: post_bl(POST_ARGS)
                    637: {
                    638:        int               i, r, len;
                    639:
                    640:        if (MDOC_BLOCK != m->last->type)
                    641:                return(1);
                    642:
                    643:        /*
                    644:         * These are fairly complicated, so we've broken them into two
                    645:         * functions.  post_bl_tagwidth() is called when a -tag is
                    646:         * specified, but no -width (it must be guessed).  The second
                    647:         * when a -width is specified (macro indicators must be
                    648:         * rewritten into real lengths).
                    649:         */
                    650:
                    651:        len = (int)(m->last->args ? m->last->args->argc : 0);
                    652:
                    653:        for (r = i = 0; i < len; i++) {
                    654:                if (MDOC_Tag == m->last->args->argv[i].arg)
                    655:                        r |= 1 << 0;
                    656:                if (MDOC_Width == m->last->args->argv[i].arg)
                    657:                        r |= 1 << 1;
                    658:        }
                    659:
                    660:        if (r & (1 << 0) && ! (r & (1 << 1))) {
                    661:                if ( ! post_bl_tagwidth(m))
                    662:                        return(0);
                    663:        } else if (r & (1 << 1))
                    664:                if ( ! post_bl_width(m))
                    665:                        return(0);
                    666:
                    667:        return(1);
                    668: }
                    669:
                    670:
                    671: static int
                    672: post_ar(POST_ARGS)
                    673: {
                    674:        struct mdoc_node *n;
                    675:
                    676:        if (m->last->child)
                    677:                return(1);
                    678:
                    679:        n = m->last;
                    680:        m->next = MDOC_NEXT_CHILD;
                    681:        if ( ! mdoc_word_alloc(m, m->last->line,
                    682:                                m->last->pos, "file"))
                    683:                return(0);
                    684:        m->next = MDOC_NEXT_SIBLING;
                    685:        if ( ! mdoc_word_alloc(m, m->last->line,
                    686:                                m->last->pos, "..."))
                    687:                return(0);
                    688:
                    689:        m->last = n;
                    690:        m->next = MDOC_NEXT_SIBLING;
                    691:        return(1);
                    692: }
                    693:
                    694:
                    695: static int
                    696: post_dd(POST_ARGS)
                    697: {
                    698:        char              buf[64];
                    699:
1.5       kristaps  700:        buf[0] = 0;
1.2       kristaps  701:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    702:                return(0);
1.1       kristaps  703:
                    704:        if (0 == (m->meta.date = mdoc_atotime(buf))) {
1.2       kristaps  705:                if ( ! vwarn(m, WBADDATE))
1.1       kristaps  706:                        return(0);
                    707:                m->meta.date = time(NULL);
                    708:        }
                    709:
                    710:        return(post_prol(m));
                    711: }
                    712:
                    713:
                    714: static int
                    715: post_prol(POST_ARGS)
                    716: {
                    717:        struct mdoc_node *n;
                    718:
                    719:        /*
                    720:         * The end document shouldn't have the prologue macros as part
                    721:         * of the syntax tree (they encompass only meta-data).
                    722:         */
                    723:
                    724:        if (m->last->parent->child == m->last)
                    725:                m->last->parent->child = m->last->prev;
                    726:        if (m->last->prev)
                    727:                m->last->prev->next = NULL;
                    728:
                    729:        n = m->last;
                    730:        assert(NULL == m->last->next);
                    731:
                    732:        if (m->last->prev) {
                    733:                m->last = m->last->prev;
                    734:                m->next = MDOC_NEXT_SIBLING;
                    735:        } else {
                    736:                m->last = m->last->parent;
                    737:                m->next = MDOC_NEXT_CHILD;
                    738:        }
                    739:
                    740:        mdoc_node_freelist(n);
                    741:        return(1);
                    742: }
                    743:
                    744:
                    745: static int
                    746: pre_dl(PRE_ARGS)
                    747: {
                    748:
                    749:        if (MDOC_BODY != n->type)
                    750:                return(1);
                    751:        m->flags |= MDOC_LITERAL;
                    752:        return(1);
                    753: }
                    754:
                    755:
                    756: static int
                    757: pre_bd(PRE_ARGS)
                    758: {
                    759:        int              i;
                    760:
                    761:        if (MDOC_BODY != n->type)
                    762:                return(1);
                    763:
1.2       kristaps  764:        /* Enter literal context if `Bd -literal' or * -unfilled'. */
1.1       kristaps  765:
1.2       kristaps  766:        for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
1.1       kristaps  767:                if (MDOC_Literal == n->args->argv[i].arg)
                    768:                        break;
                    769:                else if (MDOC_Unfilled == n->args->argv[i].arg)
                    770:                        break;
                    771:
                    772:        if (i < (int)n->args->argc)
                    773:                m->flags |= MDOC_LITERAL;
                    774:
                    775:        return(1);
                    776: }
                    777:
                    778:
                    779: static int
                    780: post_display(POST_ARGS)
                    781: {
                    782:
                    783:        if (MDOC_BODY == m->last->type)
                    784:                m->flags &= ~MDOC_LITERAL;
                    785:        return(1);
                    786: }
                    787:
                    788:

CVSweb