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

Annotation of mandoc/action.c, Revision 1.44

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

CVSweb