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

Annotation of mandoc/action.c, Revision 1.51

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

CVSweb