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

Annotation of mandoc/action.c, Revision 1.48

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

CVSweb