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

Annotation of mandoc/action.c, Revision 1.50

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

CVSweb