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

Annotation of mandoc/action.c, Revision 1.40

1.40    ! kristaps    1: /* $Id: action.c,v 1.39 2009/03/09 14:19:59 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.31      kristaps   19: #include <sys/utsname.h>
                     20:
1.1       kristaps   21: #include <assert.h>
1.31      kristaps   22: #include <errno.h>
1.20      kristaps   23: #include <stdio.h>
1.1       kristaps   24: #include <stdlib.h>
1.20      kristaps   25: #include <string.h>
1.1       kristaps   26:
                     27: #include "private.h"
                     28:
1.14      kristaps   29: /*
                     30:  * Actions are executed on macros after they've been post-validated: in
                     31:  * other words, a macro will not be "acted upon" until all of its
                     32:  * children have been filled in (post-fix order).
                     33:  */
1.1       kristaps   34:
1.33      kristaps   35: enum   merr {
                     36:        ENOWIDTH
                     37: };
                     38:
                     39: enum   mwarn {
                     40:        WBADSEC,
                     41:        WNOWIDTH,
                     42:        WBADDATE
                     43: };
                     44:
1.1       kristaps   45: struct actions {
1.10      kristaps   46:        int     (*post)(struct mdoc *);
1.1       kristaps   47: };
                     48:
1.33      kristaps   49: static int      nwarn(struct mdoc *,
                     50:                        const struct mdoc_node *, enum mwarn);
                     51: static int      nerr(struct mdoc *,
                     52:                        const struct mdoc_node *, enum merr);
1.30      kristaps   53: static int      post_ar(struct mdoc *);
1.20      kristaps   54: static int      post_bl(struct mdoc *);
1.23      kristaps   55: static int      post_bl_width(struct mdoc *);
                     56: static int      post_bl_tagwidth(struct mdoc *);
1.29      kristaps   57: static int      post_dd(struct mdoc *);
1.13      kristaps   58: static int      post_dt(struct mdoc *);
                     59: static int      post_nm(struct mdoc *);
1.29      kristaps   60: static int      post_os(struct mdoc *);
                     61: static int      post_sh(struct mdoc *);
1.30      kristaps   62: static int      post_ex(struct mdoc *);
1.13      kristaps   63: static int      post_prologue(struct mdoc *);
1.3       kristaps   64:
1.1       kristaps   65: const  struct actions mdoc_actions[MDOC_MAX] = {
1.10      kristaps   66:        { NULL }, /* \" */
                     67:        { post_dd }, /* Dd */
                     68:        { post_dt }, /* Dt */
                     69:        { post_os }, /* Os */
                     70:        { post_sh }, /* Sh */
                     71:        { NULL }, /* Ss */
                     72:        { NULL }, /* Pp */
                     73:        { NULL }, /* D1 */
                     74:        { NULL }, /* Dl */
                     75:        { NULL }, /* Bd */
                     76:        { NULL }, /* Ed */
1.20      kristaps   77:        { post_bl }, /* Bl */
1.10      kristaps   78:        { NULL }, /* El */
                     79:        { NULL }, /* It */
                     80:        { NULL }, /* Ad */
                     81:        { NULL }, /* An */
1.30      kristaps   82:        { post_ar }, /* Ar */
1.10      kristaps   83:        { NULL }, /* Cd */
                     84:        { NULL }, /* Cm */
                     85:        { NULL }, /* Dv */
                     86:        { NULL }, /* Er */
                     87:        { NULL }, /* Ev */
1.29      kristaps   88:        { post_ex }, /* Ex */
1.10      kristaps   89:        { NULL }, /* Fa */
                     90:        { NULL }, /* Fd */
                     91:        { NULL }, /* Fl */
                     92:        { NULL }, /* Fn */
                     93:        { NULL }, /* Ft */
                     94:        { NULL }, /* Ic */
                     95:        { NULL }, /* In */
                     96:        { NULL }, /* Li */
                     97:        { NULL }, /* Nd */
                     98:        { post_nm }, /* Nm */
                     99:        { NULL }, /* Op */
                    100:        { NULL }, /* Ot */
                    101:        { NULL }, /* Pa */
                    102:        { NULL }, /* Rv */
                    103:        { NULL }, /* St */
                    104:        { NULL }, /* Va */
                    105:        { NULL }, /* Vt */
                    106:        { NULL }, /* Xr */
                    107:        { NULL }, /* %A */
                    108:        { NULL }, /* %B */
                    109:        { NULL }, /* %D */
                    110:        { NULL }, /* %I */
                    111:        { NULL }, /* %J */
                    112:        { NULL }, /* %N */
                    113:        { NULL }, /* %O */
                    114:        { NULL }, /* %P */
                    115:        { NULL }, /* %R */
                    116:        { NULL }, /* %T */
                    117:        { NULL }, /* %V */
                    118:        { NULL }, /* Ac */
                    119:        { NULL }, /* Ao */
                    120:        { NULL }, /* Aq */
                    121:        { NULL }, /* At */
                    122:        { NULL }, /* Bc */
                    123:        { NULL }, /* Bf */
                    124:        { NULL }, /* Bo */
                    125:        { NULL }, /* Bq */
                    126:        { NULL }, /* Bsx */
                    127:        { NULL }, /* Bx */
                    128:        { NULL }, /* Db */
                    129:        { NULL }, /* Dc */
                    130:        { NULL }, /* Do */
                    131:        { NULL }, /* Dq */
                    132:        { NULL }, /* Ec */
                    133:        { NULL }, /* Ef */
                    134:        { NULL }, /* Em */
                    135:        { NULL }, /* Eo */
                    136:        { NULL }, /* Fx */
                    137:        { NULL }, /* Ms */
                    138:        { NULL }, /* No */
                    139:        { NULL }, /* Ns */
                    140:        { NULL }, /* Nx */
                    141:        { NULL }, /* Ox */
                    142:        { NULL }, /* Pc */
                    143:        { NULL }, /* Pf */
                    144:        { NULL }, /* Po */
                    145:        { NULL }, /* Pq */
                    146:        { NULL }, /* Qc */
                    147:        { NULL }, /* Ql */
                    148:        { NULL }, /* Qo */
                    149:        { NULL }, /* Qq */
                    150:        { NULL }, /* Re */
                    151:        { NULL }, /* Rs */
                    152:        { NULL }, /* Sc */
                    153:        { NULL }, /* So */
                    154:        { NULL }, /* Sq */
                    155:        { NULL }, /* Sm */
                    156:        { NULL }, /* Sx */
                    157:        { NULL }, /* Sy */
                    158:        { NULL }, /* Tn */
                    159:        { NULL }, /* Ux */
                    160:        { NULL }, /* Xc */
                    161:        { NULL }, /* Xo */
                    162:        { NULL }, /* Fo */
                    163:        { NULL }, /* Fc */
                    164:        { NULL }, /* Oo */
                    165:        { NULL }, /* Oc */
                    166:        { NULL }, /* Bk */
                    167:        { NULL }, /* Ek */
                    168:        { NULL }, /* Bt */
                    169:        { NULL }, /* Hf */
                    170:        { NULL }, /* Fr */
                    171:        { NULL }, /* Ud */
1.31      kristaps  172:        { NULL }, /* Lb */
1.36      kristaps  173:        { NULL }, /* Ap */
1.37      kristaps  174:        { NULL }, /* Lp */
1.39      kristaps  175:        { NULL }, /* Lk */
                    176:        { NULL }, /* Mt */
1.40    ! kristaps  177:        { NULL }, /* Brq */
        !           178:        { NULL }, /* Bro */
        !           179:        { NULL }, /* Brc */
1.1       kristaps  180: };
                    181:
                    182:
1.33      kristaps  183: #define        merr(m, t) nerr((m), (m)->last, (t))
                    184: static int
                    185: nerr(struct mdoc *m, const struct mdoc_node *n, enum merr type)
                    186: {
                    187:        char            *p;
                    188:
                    189:        p = NULL;
                    190:
                    191:        switch (type) {
                    192:        case (ENOWIDTH):
                    193:                p = "missing width argument";
                    194:                break;
                    195:        }
                    196:
                    197:        assert(p);
                    198:        return(mdoc_nerr(m, n, p));
                    199: }
                    200:
                    201:
                    202: #define        mwarn(m, t) nwarn((m), (m)->last, (t))
                    203: static int
                    204: nwarn(struct mdoc *m, const struct mdoc_node *n, enum mwarn type)
                    205: {
                    206:        char            *p;
                    207:        int              c;
                    208:
                    209:        p = NULL;
                    210:        c = WARN_SYNTAX;
                    211:
                    212:        switch (type) {
                    213:        case (WBADSEC):
                    214:                p = "inappropriate document section in manual section";
                    215:                c = WARN_COMPAT;
                    216:                break;
                    217:        case (WNOWIDTH):
                    218:                p = "cannot determine default width";
                    219:                break;
                    220:        case (WBADDATE):
                    221:                p = "malformed date syntax";
                    222:                break;
                    223:        }
                    224:
                    225:        assert(p);
                    226:        return(mdoc_nwarn(m, n, c, p));
                    227: }
                    228:
                    229:
1.3       kristaps  230: static int
1.29      kristaps  231: post_ex(struct mdoc *mdoc)
                    232: {
                    233:
                    234:        /*
                    235:         * If `.Ex -std' is invoked without an argument, fill it in with
                    236:         * our name (if it's been set).
                    237:         */
                    238:
1.33      kristaps  239:        if (NULL == mdoc->last->args)
1.29      kristaps  240:                return(1);
1.33      kristaps  241:        if (mdoc->last->args->argv[0].sz)
1.29      kristaps  242:                return(1);
                    243:
1.30      kristaps  244:        assert(mdoc->meta.name);
1.29      kristaps  245:
                    246:        mdoc_msg(mdoc, "writing %s argument: %s",
1.33      kristaps  247:                        mdoc_argnames[MDOC_Std],
                    248:                        mdoc->meta.name);
1.29      kristaps  249:
1.35      kristaps  250:        mdoc->last->args->argv[0].value = xcalloc(1, sizeof(char *));
1.33      kristaps  251:        mdoc->last->args->argv[0].sz = 1;
                    252:        mdoc->last->args->argv[0].value[0] = xstrdup(mdoc->meta.name);
1.29      kristaps  253:        return(1);
                    254: }
                    255:
                    256:
                    257: static int
1.10      kristaps  258: post_nm(struct mdoc *mdoc)
                    259: {
                    260:        char             buf[64];
                    261:
                    262:        if (mdoc->meta.name)
                    263:                return(1);
                    264:
1.31      kristaps  265:        (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
                    266:        mdoc->meta.name = xstrdup(buf);
                    267:        mdoc_msg(mdoc, "name: %s", mdoc->meta.name);
1.10      kristaps  268:
1.31      kristaps  269:        return(1);
1.10      kristaps  270: }
                    271:
                    272:
                    273: static int
1.4       kristaps  274: post_sh(struct mdoc *mdoc)
1.1       kristaps  275: {
1.10      kristaps  276:        enum mdoc_sec    sec;
                    277:        char             buf[64];
1.3       kristaps  278:
1.23      kristaps  279:        /*
                    280:         * We keep track of the current section /and/ the "named"
                    281:         * section, which is one of the conventional ones, in order to
                    282:         * check ordering.
                    283:         */
                    284:
1.3       kristaps  285:        if (MDOC_HEAD != mdoc->last->type)
                    286:                return(1);
1.31      kristaps  287:
                    288:        (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
                    289:        if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
                    290:                mdoc->lastnamed = sec;
                    291:
                    292:        mdoc->lastsec = sec;
1.23      kristaps  293:
                    294:        switch (mdoc->lastsec) {
                    295:        case (SEC_RETURN_VALUES):
                    296:                /* FALLTHROUGH */
                    297:        case (SEC_ERRORS):
                    298:                switch (mdoc->meta.msec) {
1.31      kristaps  299:                case (2):
1.23      kristaps  300:                        /* FALLTHROUGH */
1.31      kristaps  301:                case (3):
1.23      kristaps  302:                        /* FALLTHROUGH */
1.31      kristaps  303:                case (9):
1.23      kristaps  304:                        break;
                    305:                default:
1.33      kristaps  306:                        return(mwarn(mdoc, WBADSEC));
1.23      kristaps  307:                }
                    308:                break;
                    309:        default:
                    310:                break;
1.11      kristaps  311:        }
1.23      kristaps  312:        return(1);
1.1       kristaps  313: }
                    314:
1.3       kristaps  315:
1.4       kristaps  316: static int
                    317: post_dt(struct mdoc *mdoc)
                    318: {
1.5       kristaps  319:        struct mdoc_node *n;
1.31      kristaps  320:        const char       *cp;
                    321:        char             *ep;
                    322:        long              lval;
                    323:
                    324:        if (mdoc->meta.title)
                    325:                free(mdoc->meta.title);
                    326:        if (mdoc->meta.vol)
                    327:                free(mdoc->meta.vol);
                    328:        if (mdoc->meta.arch)
                    329:                free(mdoc->meta.arch);
                    330:
                    331:        mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
                    332:        mdoc->meta.msec = 0;
                    333:
                    334:        /* Handles: `.Dt'
                    335:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    336:         */
                    337:
                    338:        if (NULL == (n = mdoc->last->child)) {
                    339:                mdoc->meta.title = xstrdup("unknown");
                    340:                mdoc->meta.vol = xstrdup("local");
                    341:                mdoc_msg(mdoc, "title: %s", mdoc->meta.title);
                    342:                mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
                    343:                mdoc_msg(mdoc, "arch: <unset>");
                    344:                mdoc_msg(mdoc, "msec: <unset>");
                    345:                return(post_prologue(mdoc));
                    346:        }
1.5       kristaps  347:
1.31      kristaps  348:        /* Handles: `.Dt TITLE'
                    349:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
1.23      kristaps  350:         */
                    351:
1.33      kristaps  352:        mdoc->meta.title = xstrdup(n->string);
1.31      kristaps  353:        mdoc_msg(mdoc, "title: %s", mdoc->meta.title);
                    354:
                    355:        if (NULL == (n = n->next)) {
                    356:                mdoc->meta.vol = xstrdup("local");
                    357:                mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
                    358:                mdoc_msg(mdoc, "arch: <unset>");
                    359:                mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec);
                    360:                return(post_prologue(mdoc));
                    361:        }
1.5       kristaps  362:
1.31      kristaps  363:        /* Handles: `.Dt TITLE SEC'
                    364:         *   --> title = TITLE, volume = SEC is msec ?
                    365:         *           format(msec) : SEC,
                    366:         *       msec = SEC is msec ? atoi(msec) : 0,
                    367:         *       arch = NULL
                    368:         */
                    369:
1.33      kristaps  370:        if ((cp = mdoc_a2msec(n->string))) {
1.31      kristaps  371:                mdoc->meta.vol = xstrdup(cp);
                    372:                errno = 0;
1.33      kristaps  373:                lval = strtol(n->string, &ep, 10);
                    374:                if (n->string[0] != '\0' && *ep == '\0')
1.31      kristaps  375:                        mdoc->meta.msec = (int)lval;
                    376:        } else
1.33      kristaps  377:                mdoc->meta.vol = xstrdup(n->string);
1.31      kristaps  378:
                    379:        if (NULL == (n = n->next)) {
                    380:                mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
                    381:                mdoc_msg(mdoc, "arch: <unset>");
                    382:                mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec);
                    383:                return(post_prologue(mdoc));
                    384:        }
1.4       kristaps  385:
1.31      kristaps  386:        /* Handles: `.Dt TITLE SEC VOL'
                    387:         *   --> title = TITLE, volume = VOL is vol ?
                    388:         *       format(VOL) :
                    389:         *           VOL is arch ? format(arch) :
                    390:         *               VOL
                    391:         */
1.4       kristaps  392:
1.33      kristaps  393:        if ((cp = mdoc_a2vol(n->string))) {
1.31      kristaps  394:                free(mdoc->meta.vol);
                    395:                mdoc->meta.vol = xstrdup(cp);
                    396:                n = n->next;
                    397:        } else {
1.33      kristaps  398:                cp = mdoc_a2arch(n->string);
1.31      kristaps  399:                if (NULL == cp) {
                    400:                        free(mdoc->meta.vol);
1.33      kristaps  401:                        mdoc->meta.vol = xstrdup(n->string);
1.31      kristaps  402:                } else
                    403:                        mdoc->meta.arch = xstrdup(cp);
                    404:        }
1.4       kristaps  405:
1.31      kristaps  406:        mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
                    407:        mdoc_msg(mdoc, "arch: %s", mdoc->meta.arch ?
                    408:                        mdoc->meta.arch : "<unset>");
                    409:        mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec);
1.13      kristaps  410:
1.31      kristaps  411:        /* Ignore any subsequent parameters... */
1.15      kristaps  412:
1.13      kristaps  413:        return(post_prologue(mdoc));
1.4       kristaps  414: }
                    415:
                    416:
                    417: static int
                    418: post_os(struct mdoc *mdoc)
                    419: {
1.10      kristaps  420:        char              buf[64];
1.31      kristaps  421:        struct utsname    utsname;
1.5       kristaps  422:
1.31      kristaps  423:        if (mdoc->meta.os)
                    424:                free(mdoc->meta.os);
1.23      kristaps  425:
1.31      kristaps  426:        (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1.5       kristaps  427:
1.31      kristaps  428:        if (0 == buf[0]) {
                    429:                if (-1 == uname(&utsname))
                    430:                        return(mdoc_err(mdoc, "utsname"));
                    431:                (void)xstrlcpy(buf, utsname.sysname, sizeof(buf));
                    432:                (void)xstrlcat(buf, " ", sizeof(buf));
                    433:                (void)xstrlcat(buf, utsname.release, sizeof(buf));
                    434:        }
1.6       kristaps  435:
1.31      kristaps  436:        mdoc->meta.os = xstrdup(buf);
                    437:        mdoc_msg(mdoc, "system: %s", mdoc->meta.os);
                    438:
                    439:        mdoc->lastnamed = mdoc->lastsec = SEC_BODY;
1.13      kristaps  440:
                    441:        return(post_prologue(mdoc));
1.4       kristaps  442: }
                    443:
                    444:
1.20      kristaps  445: static int
1.23      kristaps  446: post_bl_tagwidth(struct mdoc *mdoc)
1.20      kristaps  447: {
1.23      kristaps  448:        struct mdoc_node  *n;
                    449:        int                sz;
1.20      kristaps  450:        char               buf[32];
                    451:
1.23      kristaps  452:        /*
                    453:         * If -tag has been specified and -width has not been, then try
                    454:         * to intuit our width from the first body element.
                    455:         */
                    456:
1.33      kristaps  457:        if (NULL == (n = mdoc->last->body->child))
1.23      kristaps  458:                return(1);
                    459:
                    460:        /*
                    461:         * Use the text width, if a text node, or the default macro
                    462:         * width if a macro.
                    463:         */
                    464:
1.33      kristaps  465:        if ((n = n->head->child)) {
1.23      kristaps  466:                if (MDOC_TEXT != n->type) {
1.33      kristaps  467:                        if (0 == (sz = (int)mdoc_macro2len(n->tok)))
1.23      kristaps  468:                                sz = -1;
                    469:                } else
1.33      kristaps  470:                        sz = (int)strlen(n->string) + 1;
1.23      kristaps  471:        } else
                    472:                sz = -1;
                    473:
                    474:        if (-1 == sz) {
1.33      kristaps  475:                if ( ! mwarn(mdoc, WNOWIDTH))
1.23      kristaps  476:                        return(0);
                    477:                sz = 10;
                    478:        }
                    479:
                    480:        (void)snprintf(buf, sizeof(buf), "%dn", sz);
                    481:
                    482:        /*
                    483:         * We have to dynamically add this to the macro's argument list.
                    484:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    485:         */
                    486:
1.33      kristaps  487:        if (NULL == mdoc->last->args) {
                    488:                mdoc->last->args = xcalloc
                    489:                        (1, sizeof(struct mdoc_arg));
                    490:                mdoc->last->args->refcnt = 1;
                    491:        }
                    492:
                    493:        n = mdoc->last;
                    494:        sz = (int)n->args->argc;
                    495:
                    496:        (n->args->argc)++;
                    497:
                    498:        n->args->argv = xrealloc(n->args->argv,
                    499:                        n->args->argc * sizeof(struct mdoc_arg));
1.23      kristaps  500:
1.33      kristaps  501:        n->args->argv[sz - 1].arg = MDOC_Width;
                    502:        n->args->argv[sz - 1].line = mdoc->last->line;
                    503:        n->args->argv[sz - 1].pos = mdoc->last->pos;
                    504:        n->args->argv[sz - 1].sz = 1;
                    505:        n->args->argv[sz - 1].value = xcalloc(1, sizeof(char *));
                    506:        n->args->argv[sz - 1].value[0] = xstrdup(buf);
1.23      kristaps  507:
1.33      kristaps  508:        mdoc_msg(mdoc, "adding %s argument: %s",
                    509:                        mdoc_argnames[MDOC_Width], buf);
1.23      kristaps  510:
                    511:        return(1);
                    512: }
1.20      kristaps  513:
                    514:
1.23      kristaps  515: static int
1.33      kristaps  516: post_bl_width(struct mdoc *m)
1.23      kristaps  517: {
                    518:        size_t            width;
                    519:        int               i, tok;
                    520:        char              buf[32];
1.34      kristaps  521:        char             *p;
1.23      kristaps  522:
1.33      kristaps  523:        if (NULL == m->last->args)
                    524:                return(merr(m, ENOWIDTH));
                    525:
                    526:        for (i = 0; i < (int)m->last->args->argc; i++)
                    527:                if (MDOC_Width == m->last->args->argv[i].arg)
1.20      kristaps  528:                        break;
                    529:
1.33      kristaps  530:        if (i == (int)m->last->args->argc)
                    531:                return(merr(m, ENOWIDTH));
                    532:
1.34      kristaps  533:        p = m->last->args->argv[i].value[0];
1.23      kristaps  534:
                    535:        /*
                    536:         * If the value to -width is a macro, then we re-write it to be
                    537:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    538:         */
1.20      kristaps  539:
1.34      kristaps  540:        if (xstrcmp(p, "Ds"))
1.26      kristaps  541:                width = 8;
1.34      kristaps  542:        else if (MDOC_MAX == (tok = mdoc_tokhash_find(m->htab, p)))
1.20      kristaps  543:                return(1);
1.24      kristaps  544:        else if (0 == (width = mdoc_macro2len(tok)))
1.33      kristaps  545:                return(mwarn(m, WNOWIDTH));
1.20      kristaps  546:
1.33      kristaps  547:        mdoc_msg(m, "re-writing %s argument: %s -> %zun",
1.34      kristaps  548:                        mdoc_argnames[MDOC_Width], p, width);
1.23      kristaps  549:
                    550:        /* The value already exists: free and reallocate it. */
1.20      kristaps  551:
                    552:        (void)snprintf(buf, sizeof(buf), "%zun", width);
                    553:
1.34      kristaps  554:        free(m->last->args->argv[i].value[0]);
                    555:        m->last->args->argv[i].value[0] = xstrdup(buf);
1.23      kristaps  556:
                    557:        return(1);
                    558: }
                    559:
                    560:
                    561: static int
                    562: post_bl(struct mdoc *mdoc)
                    563: {
1.33      kristaps  564:        int               i, r, len;
1.23      kristaps  565:
                    566:        if (MDOC_BLOCK != mdoc->last->type)
                    567:                return(1);
                    568:
                    569:        /*
                    570:         * These are fairly complicated, so we've broken them into two
                    571:         * functions.  post_bl_tagwidth() is called when a -tag is
                    572:         * specified, but no -width (it must be guessed).  The second
                    573:         * when a -width is specified (macro indicators must be
                    574:         * rewritten into real lengths).
                    575:         */
                    576:
1.33      kristaps  577:        len = (int)(mdoc->last->args ? mdoc->last->args->argc : 0);
                    578:
                    579:        for (r = i = 0; i < len; i++) {
                    580:                if (MDOC_Tag == mdoc->last->args->argv[i].arg)
1.23      kristaps  581:                        r |= 1 << 0;
1.33      kristaps  582:                if (MDOC_Width == mdoc->last->args->argv[i].arg)
1.23      kristaps  583:                        r |= 1 << 1;
                    584:        }
                    585:
                    586:        if (r & (1 << 0) && ! (r & (1 << 1))) {
                    587:                if ( ! post_bl_tagwidth(mdoc))
                    588:                        return(0);
                    589:        } else if (r & (1 << 1))
                    590:                if ( ! post_bl_width(mdoc))
                    591:                        return(0);
1.20      kristaps  592:
                    593:        return(1);
                    594: }
                    595:
                    596:
1.4       kristaps  597: static int
1.30      kristaps  598: post_ar(struct mdoc *mdoc)
                    599: {
                    600:        struct mdoc_node *n;
                    601:
                    602:        if (mdoc->last->child)
                    603:                return(1);
                    604:
                    605:        n = mdoc->last;
                    606:
                    607:        mdoc->next = MDOC_NEXT_CHILD;
1.32      kristaps  608:        if ( ! mdoc_word_alloc(mdoc, mdoc->last->line,
                    609:                                mdoc->last->pos, "file"))
                    610:                return(0);
1.30      kristaps  611:        mdoc->next = MDOC_NEXT_SIBLING;
1.32      kristaps  612:        if ( ! mdoc_word_alloc(mdoc, mdoc->last->line,
                    613:                                mdoc->last->pos, "..."))
                    614:                return(0);
1.30      kristaps  615:
                    616:        mdoc->last = n;
                    617:        mdoc->next = MDOC_NEXT_SIBLING;
                    618:        return(1);
                    619: }
                    620:
                    621:
                    622: static int
1.4       kristaps  623: post_dd(struct mdoc *mdoc)
                    624: {
1.15      kristaps  625:        char              buf[64];
1.4       kristaps  626:
1.31      kristaps  627:        (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1.5       kristaps  628:
1.33      kristaps  629:        if (0 == (mdoc->meta.date = mdoc_atotime(buf))) {
                    630:                if ( ! mwarn(mdoc, WBADDATE))
                    631:                        return(0);
                    632:                mdoc->meta.date = time(NULL);
                    633:        }
1.5       kristaps  634:
1.15      kristaps  635:        mdoc_msg(mdoc, "date: %u", mdoc->meta.date);
                    636:        return(post_prologue(mdoc));
1.4       kristaps  637: }
                    638:
                    639:
1.13      kristaps  640: static int
                    641: post_prologue(struct mdoc *mdoc)
                    642: {
                    643:        struct mdoc_node *n;
                    644:
1.23      kristaps  645:        /*
                    646:         * The end document shouldn't have the prologue macros as part
                    647:         * of the syntax tree (they encompass only meta-data).
                    648:         */
                    649:
1.13      kristaps  650:        if (mdoc->last->parent->child == mdoc->last)
                    651:                mdoc->last->parent->child = mdoc->last->prev;
                    652:        if (mdoc->last->prev)
                    653:                mdoc->last->prev->next = NULL;
                    654:
                    655:        n = mdoc->last;
                    656:        assert(NULL == mdoc->last->next);
                    657:
                    658:        if (mdoc->last->prev) {
                    659:                mdoc->last = mdoc->last->prev;
                    660:                mdoc->next = MDOC_NEXT_SIBLING;
                    661:        } else {
                    662:                mdoc->last = mdoc->last->parent;
                    663:                mdoc->next = MDOC_NEXT_CHILD;
                    664:        }
                    665:
                    666:        mdoc_node_freelist(n);
                    667:        return(1);
                    668: }
                    669:
                    670:
1.4       kristaps  671: int
                    672: mdoc_action_post(struct mdoc *mdoc)
1.3       kristaps  673: {
                    674:
1.12      kristaps  675:        if (MDOC_ACTED & mdoc->last->flags)
                    676:                return(1);
                    677:        mdoc->last->flags |= MDOC_ACTED;
                    678:
1.7       kristaps  679:        if (MDOC_TEXT == mdoc->last->type)
                    680:                return(1);
                    681:        if (MDOC_ROOT == mdoc->last->type)
1.3       kristaps  682:                return(1);
1.7       kristaps  683:        if (NULL == mdoc_actions[mdoc->last->tok].post)
1.3       kristaps  684:                return(1);
1.7       kristaps  685:        return((*mdoc_actions[mdoc->last->tok].post)(mdoc));
1.3       kristaps  686: }

CVSweb