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

Annotation of mandoc/action.c, Revision 1.46

1.46    ! kristaps    1: /* $Id: action.c,v 1.45 2009/03/20 16:43:28 kristaps Exp $ */
1.1       kristaps    2: /*
1.43      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.31      kristaps   19: #include <sys/utsname.h>
                     20:
1.1       kristaps   21: #include <assert.h>
1.31      kristaps   22: #include <errno.h>
1.20      kristaps   23: #include <stdio.h>
1.1       kristaps   24: #include <stdlib.h>
1.20      kristaps   25: #include <string.h>
1.1       kristaps   26:
                     27: #include "private.h"
                     28:
1.14      kristaps   29: /*
                     30:  * Actions are executed on macros after they've been post-validated: in
                     31:  * other words, a macro will not be "acted upon" until all of its
                     32:  * children have been filled in (post-fix order).
                     33:  */
1.1       kristaps   34:
1.33      kristaps   35: enum   merr {
                     36:        ENOWIDTH
                     37: };
                     38:
                     39: enum   mwarn {
                     40:        WBADSEC,
                     41:        WNOWIDTH,
                     42:        WBADDATE
                     43: };
                     44:
1.44      kristaps   45: #define        PRE_ARGS  struct mdoc *m, const struct mdoc_node *n
                     46: #define        POST_ARGS struct mdoc *m
                     47:
1.1       kristaps   48: struct actions {
1.44      kristaps   49:        int     (*pre)(PRE_ARGS);
                     50:        int     (*post)(POST_ARGS);
1.1       kristaps   51: };
                     52:
1.44      kristaps   53: static int       pwarn(struct mdoc *, int, int, enum mwarn);
                     54: static int       perr(struct mdoc *, int, int, enum merr);
                     55:
                     56: static int       post_ar(POST_ARGS);
                     57: static int       post_bl(POST_ARGS);
                     58: static int       post_bl_width(POST_ARGS);
                     59: static int       post_bl_tagwidth(POST_ARGS);
                     60: static int       post_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.1       kristaps  194: };
                    195:
                    196:
1.44      kristaps  197: int
                    198: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
                    199: {
                    200:
                    201:        switch (n->type) {
                    202:        case (MDOC_ROOT):
                    203:                break;
                    204:        case (MDOC_TEXT):
                    205:                break;
                    206:        default:
                    207:                if (NULL == mdoc_actions[m->last->tok].pre)
                    208:                        break;
                    209:                return((*mdoc_actions[m->last->tok].pre)(m, n));
                    210:        }
                    211:        return(1);
                    212: }
                    213:
                    214:
                    215: int
                    216: mdoc_action_post(struct mdoc *m)
                    217: {
                    218:
                    219:        if (MDOC_ACTED & m->last->flags)
                    220:                return(1);
                    221:        m->last->flags |= MDOC_ACTED;
                    222:
                    223:        switch (m->last->type) {
                    224:        case (MDOC_TEXT):
                    225:                break;
                    226:        case (MDOC_ROOT):
                    227:                break;
                    228:        default:
                    229:                if (NULL == mdoc_actions[m->last->tok].post)
                    230:                        break;
                    231:                return((*mdoc_actions[m->last->tok].post)(m));
                    232:        }
                    233:        return(1);
                    234: }
                    235:
                    236:
1.33      kristaps  237: static int
1.44      kristaps  238: perr(struct mdoc *m, int line, int pos, enum merr type)
1.33      kristaps  239: {
                    240:        char            *p;
                    241:
                    242:        p = NULL;
                    243:
                    244:        switch (type) {
                    245:        case (ENOWIDTH):
                    246:                p = "missing width argument";
                    247:                break;
                    248:        }
                    249:
                    250:        assert(p);
1.44      kristaps  251:        return(mdoc_perr(m, line, pos, p));
1.33      kristaps  252: }
                    253:
                    254:
                    255: static int
1.44      kristaps  256: pwarn(struct mdoc *m, int line, int pos, enum mwarn type)
1.33      kristaps  257: {
                    258:        char            *p;
                    259:        int              c;
                    260:
                    261:        p = NULL;
                    262:        c = WARN_SYNTAX;
                    263:
                    264:        switch (type) {
                    265:        case (WBADSEC):
                    266:                p = "inappropriate document section in manual section";
                    267:                c = WARN_COMPAT;
                    268:                break;
                    269:        case (WNOWIDTH):
                    270:                p = "cannot determine default width";
                    271:                break;
                    272:        case (WBADDATE):
                    273:                p = "malformed date syntax";
                    274:                break;
                    275:        }
                    276:
                    277:        assert(p);
1.44      kristaps  278:        return(mdoc_pwarn(m, line, pos, c, p));
1.33      kristaps  279: }
                    280:
                    281:
1.3       kristaps  282: static int
1.44      kristaps  283: post_std(POST_ARGS)
1.29      kristaps  284: {
                    285:
                    286:        /*
1.41      kristaps  287:         * If '-std' is invoked without an argument, fill it in with our
                    288:         * name (if it's been set).
1.29      kristaps  289:         */
                    290:
1.44      kristaps  291:        if (NULL == m->last->args)
1.29      kristaps  292:                return(1);
1.44      kristaps  293:        if (m->last->args->argv[0].sz)
1.29      kristaps  294:                return(1);
                    295:
1.44      kristaps  296:        assert(m->meta.name);
1.29      kristaps  297:
1.44      kristaps  298:        m->last->args->argv[0].value = xcalloc(1, sizeof(char *));
                    299:        m->last->args->argv[0].sz = 1;
                    300:        m->last->args->argv[0].value[0] = xstrdup(m->meta.name);
1.29      kristaps  301:        return(1);
                    302: }
                    303:
                    304:
                    305: static int
1.44      kristaps  306: post_nm(POST_ARGS)
1.10      kristaps  307: {
                    308:        char             buf[64];
                    309:
1.44      kristaps  310:        if (m->meta.name)
1.10      kristaps  311:                return(1);
                    312:
1.44      kristaps  313:        (void)xstrlcpys(buf, m->last->child, sizeof(buf));
                    314:        m->meta.name = xstrdup(buf);
1.10      kristaps  315:
1.31      kristaps  316:        return(1);
1.10      kristaps  317: }
                    318:
                    319:
                    320: static int
1.44      kristaps  321: post_sh(POST_ARGS)
1.1       kristaps  322: {
1.10      kristaps  323:        enum mdoc_sec    sec;
                    324:        char             buf[64];
1.3       kristaps  325:
1.23      kristaps  326:        /*
                    327:         * We keep track of the current section /and/ the "named"
                    328:         * section, which is one of the conventional ones, in order to
                    329:         * check ordering.
                    330:         */
                    331:
1.44      kristaps  332:        if (MDOC_HEAD != m->last->type)
1.3       kristaps  333:                return(1);
1.31      kristaps  334:
1.44      kristaps  335:        (void)xstrlcpys(buf, m->last->child, sizeof(buf));
1.31      kristaps  336:        if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
1.44      kristaps  337:                m->lastnamed = sec;
1.31      kristaps  338:
1.44      kristaps  339:        switch ((m->lastsec = sec)) {
1.23      kristaps  340:        case (SEC_RETURN_VALUES):
                    341:                /* FALLTHROUGH */
                    342:        case (SEC_ERRORS):
1.44      kristaps  343:                switch (m->meta.msec) {
1.31      kristaps  344:                case (2):
1.23      kristaps  345:                        /* FALLTHROUGH */
1.31      kristaps  346:                case (3):
1.23      kristaps  347:                        /* FALLTHROUGH */
1.31      kristaps  348:                case (9):
1.23      kristaps  349:                        break;
                    350:                default:
1.44      kristaps  351:                        return(mwarn(m, WBADSEC));
1.23      kristaps  352:                }
                    353:                break;
                    354:        default:
                    355:                break;
1.11      kristaps  356:        }
1.23      kristaps  357:        return(1);
1.1       kristaps  358: }
                    359:
1.3       kristaps  360:
1.4       kristaps  361: static int
1.44      kristaps  362: post_dt(POST_ARGS)
1.4       kristaps  363: {
1.5       kristaps  364:        struct mdoc_node *n;
1.31      kristaps  365:        const char       *cp;
                    366:        char             *ep;
                    367:        long              lval;
                    368:
1.44      kristaps  369:        if (m->meta.title)
                    370:                free(m->meta.title);
                    371:        if (m->meta.vol)
                    372:                free(m->meta.vol);
                    373:        if (m->meta.arch)
                    374:                free(m->meta.arch);
1.31      kristaps  375:
1.44      kristaps  376:        m->meta.title = m->meta.vol = m->meta.arch = NULL;
                    377:        m->meta.msec = 0;
1.31      kristaps  378:
                    379:        /* Handles: `.Dt'
                    380:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    381:         */
                    382:
1.44      kristaps  383:        if (NULL == (n = m->last->child)) {
                    384:                m->meta.title = xstrdup("unknown");
                    385:                m->meta.vol = xstrdup("local");
                    386:                return(post_prol(m));
1.31      kristaps  387:        }
1.5       kristaps  388:
1.31      kristaps  389:        /* Handles: `.Dt TITLE'
                    390:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
1.23      kristaps  391:         */
                    392:
1.44      kristaps  393:        m->meta.title = xstrdup(n->string);
1.31      kristaps  394:
                    395:        if (NULL == (n = n->next)) {
1.44      kristaps  396:                m->meta.vol = xstrdup("local");
                    397:                return(post_prol(m));
1.31      kristaps  398:        }
1.5       kristaps  399:
1.31      kristaps  400:        /* Handles: `.Dt TITLE SEC'
                    401:         *   --> title = TITLE, volume = SEC is msec ?
                    402:         *           format(msec) : SEC,
                    403:         *       msec = SEC is msec ? atoi(msec) : 0,
                    404:         *       arch = NULL
                    405:         */
                    406:
1.33      kristaps  407:        if ((cp = mdoc_a2msec(n->string))) {
1.44      kristaps  408:                m->meta.vol = xstrdup(cp);
1.31      kristaps  409:                errno = 0;
1.33      kristaps  410:                lval = strtol(n->string, &ep, 10);
                    411:                if (n->string[0] != '\0' && *ep == '\0')
1.44      kristaps  412:                        m->meta.msec = (int)lval;
1.31      kristaps  413:        } else
1.44      kristaps  414:                m->meta.vol = xstrdup(n->string);
1.31      kristaps  415:
1.44      kristaps  416:        if (NULL == (n = n->next))
                    417:                return(post_prol(m));
1.4       kristaps  418:
1.31      kristaps  419:        /* Handles: `.Dt TITLE SEC VOL'
                    420:         *   --> title = TITLE, volume = VOL is vol ?
                    421:         *       format(VOL) :
                    422:         *           VOL is arch ? format(arch) :
                    423:         *               VOL
                    424:         */
1.4       kristaps  425:
1.33      kristaps  426:        if ((cp = mdoc_a2vol(n->string))) {
1.44      kristaps  427:                free(m->meta.vol);
                    428:                m->meta.vol = xstrdup(cp);
1.31      kristaps  429:                n = n->next;
                    430:        } else {
1.33      kristaps  431:                cp = mdoc_a2arch(n->string);
1.31      kristaps  432:                if (NULL == cp) {
1.44      kristaps  433:                        free(m->meta.vol);
                    434:                        m->meta.vol = xstrdup(n->string);
1.31      kristaps  435:                } else
1.44      kristaps  436:                        m->meta.arch = xstrdup(cp);
1.31      kristaps  437:        }
1.4       kristaps  438:
1.31      kristaps  439:        /* Ignore any subsequent parameters... */
1.15      kristaps  440:
1.44      kristaps  441:        return(post_prol(m));
1.4       kristaps  442: }
                    443:
                    444:
                    445: static int
1.44      kristaps  446: post_os(POST_ARGS)
1.4       kristaps  447: {
1.10      kristaps  448:        char              buf[64];
1.31      kristaps  449:        struct utsname    utsname;
1.5       kristaps  450:
1.44      kristaps  451:        if (m->meta.os)
                    452:                free(m->meta.os);
1.23      kristaps  453:
1.44      kristaps  454:        (void)xstrlcpys(buf, m->last->child, sizeof(buf));
1.5       kristaps  455:
1.31      kristaps  456:        if (0 == buf[0]) {
                    457:                if (-1 == uname(&utsname))
1.44      kristaps  458:                        return(mdoc_err(m, "utsname"));
1.31      kristaps  459:                (void)xstrlcpy(buf, utsname.sysname, sizeof(buf));
                    460:                (void)xstrlcat(buf, " ", sizeof(buf));
                    461:                (void)xstrlcat(buf, utsname.release, sizeof(buf));
                    462:        }
1.6       kristaps  463:
1.44      kristaps  464:        m->meta.os = xstrdup(buf);
                    465:        m->lastnamed = m->lastsec = SEC_BODY;
1.31      kristaps  466:
1.44      kristaps  467:        return(post_prol(m));
1.4       kristaps  468: }
                    469:
                    470:
1.20      kristaps  471: static int
1.44      kristaps  472: post_bl_tagwidth(struct mdoc *m)
1.20      kristaps  473: {
1.23      kristaps  474:        struct mdoc_node  *n;
                    475:        int                sz;
1.20      kristaps  476:        char               buf[32];
                    477:
1.23      kristaps  478:        /*
                    479:         * If -tag has been specified and -width has not been, then try
                    480:         * to intuit our width from the first body element.
                    481:         */
                    482:
1.44      kristaps  483:        if (NULL == (n = m->last->body->child))
1.23      kristaps  484:                return(1);
                    485:
                    486:        /*
                    487:         * Use the text width, if a text node, or the default macro
                    488:         * width if a macro.
                    489:         */
                    490:
1.33      kristaps  491:        if ((n = n->head->child)) {
1.23      kristaps  492:                if (MDOC_TEXT != n->type) {
1.33      kristaps  493:                        if (0 == (sz = (int)mdoc_macro2len(n->tok)))
1.23      kristaps  494:                                sz = -1;
                    495:                } else
1.33      kristaps  496:                        sz = (int)strlen(n->string) + 1;
1.23      kristaps  497:        } else
                    498:                sz = -1;
                    499:
                    500:        if (-1 == sz) {
1.44      kristaps  501:                if ( ! mwarn(m, WNOWIDTH))
1.23      kristaps  502:                        return(0);
                    503:                sz = 10;
                    504:        }
                    505:
                    506:        (void)snprintf(buf, sizeof(buf), "%dn", sz);
                    507:
                    508:        /*
                    509:         * We have to dynamically add this to the macro's argument list.
                    510:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    511:         */
                    512:
1.44      kristaps  513:        if (NULL == m->last->args) {
                    514:                m->last->args = xcalloc
1.33      kristaps  515:                        (1, sizeof(struct mdoc_arg));
1.44      kristaps  516:                m->last->args->refcnt = 1;
1.33      kristaps  517:        }
                    518:
1.44      kristaps  519:        n = m->last;
1.33      kristaps  520:        sz = (int)n->args->argc;
                    521:
                    522:        (n->args->argc)++;
                    523:
                    524:        n->args->argv = xrealloc(n->args->argv,
                    525:                        n->args->argc * sizeof(struct mdoc_arg));
1.23      kristaps  526:
1.33      kristaps  527:        n->args->argv[sz - 1].arg = MDOC_Width;
1.44      kristaps  528:        n->args->argv[sz - 1].line = m->last->line;
                    529:        n->args->argv[sz - 1].pos = m->last->pos;
1.33      kristaps  530:        n->args->argv[sz - 1].sz = 1;
                    531:        n->args->argv[sz - 1].value = xcalloc(1, sizeof(char *));
                    532:        n->args->argv[sz - 1].value[0] = xstrdup(buf);
1.23      kristaps  533:
                    534:        return(1);
                    535: }
1.20      kristaps  536:
                    537:
1.23      kristaps  538: static int
1.33      kristaps  539: post_bl_width(struct mdoc *m)
1.23      kristaps  540: {
                    541:        size_t            width;
                    542:        int               i, tok;
                    543:        char              buf[32];
1.34      kristaps  544:        char             *p;
1.23      kristaps  545:
1.33      kristaps  546:        if (NULL == m->last->args)
                    547:                return(merr(m, ENOWIDTH));
                    548:
                    549:        for (i = 0; i < (int)m->last->args->argc; i++)
                    550:                if (MDOC_Width == m->last->args->argv[i].arg)
1.20      kristaps  551:                        break;
                    552:
1.33      kristaps  553:        if (i == (int)m->last->args->argc)
                    554:                return(merr(m, ENOWIDTH));
                    555:
1.34      kristaps  556:        p = m->last->args->argv[i].value[0];
1.23      kristaps  557:
                    558:        /*
                    559:         * If the value to -width is a macro, then we re-write it to be
                    560:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    561:         */
1.20      kristaps  562:
1.34      kristaps  563:        if (xstrcmp(p, "Ds"))
1.26      kristaps  564:                width = 8;
1.34      kristaps  565:        else if (MDOC_MAX == (tok = mdoc_tokhash_find(m->htab, p)))
1.20      kristaps  566:                return(1);
1.24      kristaps  567:        else if (0 == (width = mdoc_macro2len(tok)))
1.33      kristaps  568:                return(mwarn(m, WNOWIDTH));
1.20      kristaps  569:
1.23      kristaps  570:        /* The value already exists: free and reallocate it. */
1.20      kristaps  571:
                    572:        (void)snprintf(buf, sizeof(buf), "%zun", width);
                    573:
1.34      kristaps  574:        free(m->last->args->argv[i].value[0]);
                    575:        m->last->args->argv[i].value[0] = xstrdup(buf);
1.23      kristaps  576:
                    577:        return(1);
                    578: }
                    579:
                    580:
                    581: static int
1.44      kristaps  582: post_bl(POST_ARGS)
1.23      kristaps  583: {
1.33      kristaps  584:        int               i, r, len;
1.23      kristaps  585:
1.44      kristaps  586:        if (MDOC_BLOCK != m->last->type)
1.23      kristaps  587:                return(1);
                    588:
                    589:        /*
                    590:         * These are fairly complicated, so we've broken them into two
                    591:         * functions.  post_bl_tagwidth() is called when a -tag is
                    592:         * specified, but no -width (it must be guessed).  The second
                    593:         * when a -width is specified (macro indicators must be
                    594:         * rewritten into real lengths).
                    595:         */
                    596:
1.44      kristaps  597:        len = (int)(m->last->args ? m->last->args->argc : 0);
1.33      kristaps  598:
                    599:        for (r = i = 0; i < len; i++) {
1.44      kristaps  600:                if (MDOC_Tag == m->last->args->argv[i].arg)
1.23      kristaps  601:                        r |= 1 << 0;
1.44      kristaps  602:                if (MDOC_Width == m->last->args->argv[i].arg)
1.23      kristaps  603:                        r |= 1 << 1;
                    604:        }
                    605:
                    606:        if (r & (1 << 0) && ! (r & (1 << 1))) {
1.44      kristaps  607:                if ( ! post_bl_tagwidth(m))
1.23      kristaps  608:                        return(0);
                    609:        } else if (r & (1 << 1))
1.44      kristaps  610:                if ( ! post_bl_width(m))
1.23      kristaps  611:                        return(0);
1.20      kristaps  612:
                    613:        return(1);
                    614: }
                    615:
                    616:
1.4       kristaps  617: static int
1.44      kristaps  618: post_ar(POST_ARGS)
1.30      kristaps  619: {
                    620:        struct mdoc_node *n;
                    621:
1.44      kristaps  622:        if (m->last->child)
1.30      kristaps  623:                return(1);
                    624:
1.44      kristaps  625:        n = m->last;
                    626:        m->next = MDOC_NEXT_CHILD;
                    627:        if ( ! mdoc_word_alloc(m, m->last->line,
                    628:                                m->last->pos, "file"))
1.32      kristaps  629:                return(0);
1.44      kristaps  630:        m->next = MDOC_NEXT_SIBLING;
                    631:        if ( ! mdoc_word_alloc(m, m->last->line,
                    632:                                m->last->pos, "..."))
1.32      kristaps  633:                return(0);
1.30      kristaps  634:
1.44      kristaps  635:        m->last = n;
                    636:        m->next = MDOC_NEXT_SIBLING;
1.30      kristaps  637:        return(1);
                    638: }
                    639:
                    640:
                    641: static int
1.44      kristaps  642: post_dd(POST_ARGS)
1.4       kristaps  643: {
1.15      kristaps  644:        char              buf[64];
1.4       kristaps  645:
1.44      kristaps  646:        (void)xstrlcpys(buf, m->last->child, sizeof(buf));
1.5       kristaps  647:
1.44      kristaps  648:        if (0 == (m->meta.date = mdoc_atotime(buf))) {
                    649:                if ( ! mwarn(m, WBADDATE))
1.33      kristaps  650:                        return(0);
1.44      kristaps  651:                m->meta.date = time(NULL);
1.33      kristaps  652:        }
1.5       kristaps  653:
1.44      kristaps  654:        return(post_prol(m));
1.4       kristaps  655: }
                    656:
                    657:
1.13      kristaps  658: static int
1.44      kristaps  659: post_prol(POST_ARGS)
1.13      kristaps  660: {
                    661:        struct mdoc_node *n;
                    662:
1.23      kristaps  663:        /*
                    664:         * The end document shouldn't have the prologue macros as part
                    665:         * of the syntax tree (they encompass only meta-data).
                    666:         */
                    667:
1.44      kristaps  668:        if (m->last->parent->child == m->last)
                    669:                m->last->parent->child = m->last->prev;
                    670:        if (m->last->prev)
                    671:                m->last->prev->next = NULL;
                    672:
                    673:        n = m->last;
                    674:        assert(NULL == m->last->next);
                    675:
                    676:        if (m->last->prev) {
                    677:                m->last = m->last->prev;
                    678:                m->next = MDOC_NEXT_SIBLING;
1.13      kristaps  679:        } else {
1.44      kristaps  680:                m->last = m->last->parent;
                    681:                m->next = MDOC_NEXT_CHILD;
1.13      kristaps  682:        }
                    683:
                    684:        mdoc_node_freelist(n);
                    685:        return(1);
                    686: }
                    687:
                    688:
1.44      kristaps  689: static int
1.46    ! kristaps  690: pre_dl(PRE_ARGS)
        !           691: {
        !           692:
        !           693:        if (MDOC_BODY != n->type)
        !           694:                return(1);
        !           695:        m->flags |= MDOC_LITERAL;
        !           696:        return(1);
        !           697: }
        !           698:
        !           699:
        !           700: static int
1.45      kristaps  701: pre_bd(PRE_ARGS)
1.3       kristaps  702: {
1.44      kristaps  703:        int              i;
1.3       kristaps  704:
1.44      kristaps  705:        if (MDOC_BODY != n->type)
1.12      kristaps  706:                return(1);
                    707:
1.44      kristaps  708:        /*
                    709:         * We ONLY enter a literal context if `Bd -literal' or `Bd
                    710:         * -unfilled'.
                    711:         */
                    712:
                    713:        n = n->parent;
1.45      kristaps  714:
1.44      kristaps  715:        for (i = 0; i < (int)n->args->argc; i++)
                    716:                if (MDOC_Literal == n->args->argv[i].arg)
                    717:                        break;
                    718:                else if (MDOC_Unfilled == n->args->argv[i].arg)
                    719:                        break;
                    720:
                    721:        if (i < (int)n->args->argc)
                    722:                m->flags |= MDOC_LITERAL;
                    723:
                    724:        return(1);
                    725: }
                    726:
                    727:
                    728: static int
1.46    ! kristaps  729: post_display(POST_ARGS)
1.44      kristaps  730: {
                    731:
                    732:        if (MDOC_BODY == m->last->type)
                    733:                m->flags &= ~MDOC_LITERAL;
                    734:        return(1);
1.3       kristaps  735: }
1.44      kristaps  736:
                    737:

CVSweb