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

Annotation of mandoc/mdoc_action.c, Revision 1.38

1.38    ! kristaps    1: /*     $Id: mdoc_action.c,v 1.37 2009/09/16 20:49:06 kristaps Exp $ */
1.1       kristaps    2: /*
1.9       kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.8       kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.8       kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
                     17: #include <sys/utsname.h>
                     18:
                     19: #include <assert.h>
                     20: #include <errno.h>
                     21: #include <stdio.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "libmdoc.h"
                     26:
1.35      kristaps   27: #define        POST_ARGS struct mdoc *m, struct mdoc_node *n
1.1       kristaps   28: #define        PRE_ARGS  struct mdoc *m, const struct mdoc_node *n
                     29:
                     30: struct actions {
                     31:        int     (*pre)(PRE_ARGS);
                     32:        int     (*post)(POST_ARGS);
                     33: };
                     34:
                     35: static int       post_ar(POST_ARGS);
1.25      kristaps   36: static int       post_at(POST_ARGS);
1.1       kristaps   37: static int       post_bl(POST_ARGS);
1.14      kristaps   38: static int       post_bl_head(POST_ARGS);
1.25      kristaps   39: static int       post_bl_tagwidth(POST_ARGS);
1.1       kristaps   40: static int       post_bl_width(POST_ARGS);
                     41: static int       post_dd(POST_ARGS);
                     42: static int       post_display(POST_ARGS);
                     43: static int       post_dt(POST_ARGS);
1.27      kristaps   44: static int       post_lb(POST_ARGS);
1.1       kristaps   45: static int       post_nm(POST_ARGS);
                     46: static int       post_os(POST_ARGS);
                     47: static int       post_prol(POST_ARGS);
                     48: static int       post_sh(POST_ARGS);
1.26      kristaps   49: static int       post_st(POST_ARGS);
1.1       kristaps   50: static int       post_std(POST_ARGS);
1.31      kristaps   51: static int       post_tilde(POST_ARGS);
1.1       kristaps   52:
                     53: static int       pre_bd(PRE_ARGS);
1.38    ! kristaps   54: static int       pre_bl(PRE_ARGS);
1.1       kristaps   55: static int       pre_dl(PRE_ARGS);
1.38    ! kristaps   56: static int       pre_offset(PRE_ARGS);
1.1       kristaps   57:
1.37      kristaps   58: static const struct actions mdoc_actions[MDOC_MAX] = {
1.12      kristaps   59:        { NULL, NULL }, /* Ap */
1.1       kristaps   60:        { NULL, post_dd }, /* Dd */
                     61:        { NULL, post_dt }, /* Dt */
                     62:        { NULL, post_os }, /* Os */
                     63:        { NULL, post_sh }, /* Sh */
                     64:        { NULL, NULL }, /* Ss */
                     65:        { NULL, NULL }, /* Pp */
                     66:        { NULL, NULL }, /* D1 */
                     67:        { pre_dl, post_display }, /* Dl */
                     68:        { pre_bd, post_display }, /* Bd */
                     69:        { NULL, NULL }, /* Ed */
1.38    ! kristaps   70:        { pre_bl, post_bl }, /* Bl */
1.1       kristaps   71:        { NULL, NULL }, /* El */
                     72:        { NULL, NULL }, /* It */
                     73:        { NULL, NULL }, /* Ad */
                     74:        { NULL, NULL }, /* An */
                     75:        { NULL, post_ar }, /* Ar */
1.35      kristaps   76:        { NULL, NULL }, /* Cd */
1.1       kristaps   77:        { NULL, NULL }, /* Cm */
                     78:        { NULL, NULL }, /* Dv */
                     79:        { NULL, NULL }, /* Er */
                     80:        { NULL, NULL }, /* Ev */
                     81:        { NULL, post_std }, /* Ex */
                     82:        { NULL, NULL }, /* Fa */
                     83:        { NULL, NULL }, /* Fd */
                     84:        { NULL, NULL }, /* Fl */
                     85:        { NULL, NULL }, /* Fn */
                     86:        { NULL, NULL }, /* Ft */
                     87:        { NULL, NULL }, /* Ic */
                     88:        { NULL, NULL }, /* In */
                     89:        { NULL, NULL }, /* Li */
                     90:        { NULL, NULL }, /* Nd */
                     91:        { NULL, post_nm }, /* Nm */
                     92:        { NULL, NULL }, /* Op */
                     93:        { NULL, NULL }, /* Ot */
1.31      kristaps   94:        { NULL, post_tilde }, /* Pa */
1.1       kristaps   95:        { NULL, post_std }, /* Rv */
1.26      kristaps   96:        { NULL, post_st }, /* St */
1.1       kristaps   97:        { NULL, NULL }, /* Va */
                     98:        { NULL, NULL }, /* Vt */
                     99:        { NULL, NULL }, /* Xr */
                    100:        { NULL, NULL }, /* %A */
                    101:        { NULL, NULL }, /* %B */
                    102:        { NULL, NULL }, /* %D */
                    103:        { NULL, NULL }, /* %I */
                    104:        { NULL, NULL }, /* %J */
                    105:        { NULL, NULL }, /* %N */
                    106:        { NULL, NULL }, /* %O */
                    107:        { NULL, NULL }, /* %P */
                    108:        { NULL, NULL }, /* %R */
                    109:        { NULL, NULL }, /* %T */
                    110:        { NULL, NULL }, /* %V */
                    111:        { NULL, NULL }, /* Ac */
                    112:        { NULL, NULL }, /* Ao */
                    113:        { NULL, NULL }, /* Aq */
1.25      kristaps  114:        { NULL, post_at }, /* At */
1.1       kristaps  115:        { NULL, NULL }, /* Bc */
                    116:        { NULL, NULL }, /* Bf */
                    117:        { NULL, NULL }, /* Bo */
                    118:        { NULL, NULL }, /* Bq */
                    119:        { NULL, NULL }, /* Bsx */
                    120:        { NULL, NULL }, /* Bx */
                    121:        { NULL, NULL }, /* Db */
                    122:        { NULL, NULL }, /* Dc */
                    123:        { NULL, NULL }, /* Do */
                    124:        { NULL, NULL }, /* Dq */
                    125:        { NULL, NULL }, /* Ec */
                    126:        { NULL, NULL }, /* Ef */
                    127:        { NULL, NULL }, /* Em */
                    128:        { NULL, NULL }, /* Eo */
                    129:        { NULL, NULL }, /* Fx */
                    130:        { NULL, NULL }, /* Ms */
                    131:        { NULL, NULL }, /* No */
                    132:        { NULL, NULL }, /* Ns */
                    133:        { NULL, NULL }, /* Nx */
                    134:        { NULL, NULL }, /* Ox */
                    135:        { NULL, NULL }, /* Pc */
                    136:        { NULL, NULL }, /* Pf */
                    137:        { NULL, NULL }, /* Po */
                    138:        { NULL, NULL }, /* Pq */
                    139:        { NULL, NULL }, /* Qc */
                    140:        { NULL, NULL }, /* Ql */
                    141:        { NULL, NULL }, /* Qo */
                    142:        { NULL, NULL }, /* Qq */
                    143:        { NULL, NULL }, /* Re */
                    144:        { NULL, NULL }, /* Rs */
                    145:        { NULL, NULL }, /* Sc */
                    146:        { NULL, NULL }, /* So */
                    147:        { NULL, NULL }, /* Sq */
                    148:        { NULL, NULL }, /* Sm */
                    149:        { NULL, NULL }, /* Sx */
                    150:        { NULL, NULL }, /* Sy */
                    151:        { NULL, NULL }, /* Tn */
                    152:        { NULL, NULL }, /* Ux */
                    153:        { NULL, NULL }, /* Xc */
                    154:        { NULL, NULL }, /* Xo */
                    155:        { NULL, NULL }, /* Fo */
                    156:        { NULL, NULL }, /* Fc */
                    157:        { NULL, NULL }, /* Oo */
                    158:        { NULL, NULL }, /* Oc */
                    159:        { NULL, NULL }, /* Bk */
                    160:        { NULL, NULL }, /* Ek */
                    161:        { NULL, NULL }, /* Bt */
                    162:        { NULL, NULL }, /* Hf */
                    163:        { NULL, NULL }, /* Fr */
                    164:        { NULL, NULL }, /* Ud */
1.27      kristaps  165:        { NULL, post_lb }, /* Lb */
1.1       kristaps  166:        { NULL, NULL }, /* Lp */
1.31      kristaps  167:        { NULL, post_tilde }, /* Lk */
1.1       kristaps  168:        { NULL, NULL }, /* Mt */
                    169:        { NULL, NULL }, /* Brq */
                    170:        { NULL, NULL }, /* Bro */
                    171:        { NULL, NULL }, /* Brc */
                    172:        { NULL, NULL }, /* %C */
                    173:        { NULL, NULL }, /* Es */
                    174:        { NULL, NULL }, /* En */
                    175:        { NULL, NULL }, /* Dx */
                    176:        { NULL, NULL }, /* %Q */
1.30      kristaps  177:        { NULL, NULL }, /* br */
                    178:        { NULL, NULL }, /* sp */
1.1       kristaps  179: };
                    180:
1.25      kristaps  181: static int       concat(struct mdoc *, const struct mdoc_node *,
                    182:                        char *, size_t);
1.1       kristaps  183:
1.2       kristaps  184: #ifdef __linux__
1.25      kristaps  185: extern size_t    strlcat(char *, const char *, size_t);
1.2       kristaps  186: #endif
                    187:
                    188:
1.1       kristaps  189: int
                    190: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
                    191: {
                    192:
                    193:        switch (n->type) {
                    194:        case (MDOC_ROOT):
1.2       kristaps  195:                /* FALLTHROUGH */
1.1       kristaps  196:        case (MDOC_TEXT):
1.2       kristaps  197:                return(1);
                    198:        default:
1.1       kristaps  199:                break;
                    200:        }
1.2       kristaps  201:
1.6       kristaps  202:        if (NULL == mdoc_actions[n->tok].pre)
1.2       kristaps  203:                return(1);
1.6       kristaps  204:        return((*mdoc_actions[n->tok].pre)(m, n));
1.1       kristaps  205: }
                    206:
                    207:
                    208: int
                    209: mdoc_action_post(struct mdoc *m)
                    210: {
                    211:
                    212:        if (MDOC_ACTED & m->last->flags)
                    213:                return(1);
                    214:        m->last->flags |= MDOC_ACTED;
                    215:
                    216:        switch (m->last->type) {
                    217:        case (MDOC_TEXT):
1.2       kristaps  218:                /* FALLTHROUGH */
1.1       kristaps  219:        case (MDOC_ROOT):
1.2       kristaps  220:                return(1);
                    221:        default:
1.1       kristaps  222:                break;
                    223:        }
1.2       kristaps  224:
                    225:        if (NULL == mdoc_actions[m->last->tok].post)
                    226:                return(1);
1.35      kristaps  227:        return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.2       kristaps  228: }
                    229:
                    230:
                    231: static int
                    232: concat(struct mdoc *m, const struct mdoc_node *n,
                    233:                char *buf, size_t sz)
                    234: {
                    235:
                    236:        for ( ; n; n = n->next) {
                    237:                assert(MDOC_TEXT == n->type);
                    238:                if (strlcat(buf, n->string, sz) >= sz)
1.23      kristaps  239:                        return(mdoc_nerr(m, n, ETOOLONG));
1.2       kristaps  240:                if (NULL == n->next)
                    241:                        continue;
                    242:                if (strlcat(buf, " ", sz) >= sz)
1.23      kristaps  243:                        return(mdoc_nerr(m, n, ETOOLONG));
1.2       kristaps  244:        }
                    245:
1.1       kristaps  246:        return(1);
                    247: }
                    248:
                    249:
                    250: static int
                    251: post_std(POST_ARGS)
                    252: {
1.35      kristaps  253:        struct mdoc_node        *nn;
1.1       kristaps  254:
1.35      kristaps  255:        if (n->child)
1.1       kristaps  256:                return(1);
1.35      kristaps  257:
                    258:        nn = n;
                    259:        m->next = MDOC_NEXT_CHILD;
1.1       kristaps  260:        assert(m->meta.name);
1.35      kristaps  261:        if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
                    262:                return(0);
                    263:        m->last = nn;
1.2       kristaps  264:
1.1       kristaps  265:        return(1);
                    266: }
                    267:
                    268:
                    269: static int
                    270: post_nm(POST_ARGS)
                    271: {
                    272:        char             buf[64];
                    273:
                    274:        if (m->meta.name)
                    275:                return(1);
1.5       kristaps  276:
                    277:        buf[0] = 0;
1.35      kristaps  278:        if ( ! concat(m, n->child, buf, sizeof(buf)))
1.2       kristaps  279:                return(0);
                    280:        if (NULL == (m->meta.name = strdup(buf)))
1.35      kristaps  281:                return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  282:
                    283:        return(1);
                    284: }
                    285:
                    286:
                    287: static int
1.27      kristaps  288: post_lb(POST_ARGS)
                    289: {
                    290:        const char      *p;
                    291:        char            *buf;
                    292:        size_t           sz;
                    293:
1.35      kristaps  294:        assert(MDOC_TEXT == n->child->type);
                    295:        p = mdoc_a2lib(n->child->string);
1.27      kristaps  296:        if (NULL == p) {
1.35      kristaps  297:                sz = strlen(n->child->string) +
1.27      kristaps  298:                        2 + strlen("\\(lqlibrary\\(rq");
                    299:                buf = malloc(sz);
                    300:                if (NULL == buf)
1.35      kristaps  301:                        return(mdoc_nerr(m, n, EMALLOC));
1.27      kristaps  302:                (void)snprintf(buf, sz, "library \\(lq%s\\(rq",
1.35      kristaps  303:                                n->child->string);
                    304:                free(n->child->string);
                    305:                n->child->string = buf;
1.27      kristaps  306:                return(1);
                    307:        }
                    308:
1.35      kristaps  309:        free(n->child->string);
                    310:        n->child->string = strdup(p);
                    311:        if (NULL == n->child->string)
                    312:                return(mdoc_nerr(m, n, EMALLOC));
                    313:
1.27      kristaps  314:        return(1);
                    315: }
                    316:
                    317:
                    318: static int
1.26      kristaps  319: post_st(POST_ARGS)
                    320: {
                    321:        const char      *p;
                    322:
1.35      kristaps  323:        assert(MDOC_TEXT == n->child->type);
                    324:        p = mdoc_a2st(n->child->string);
1.26      kristaps  325:        assert(p);
1.35      kristaps  326:        free(n->child->string);
                    327:        n->child->string = strdup(p);
                    328:        if (NULL == n->child->string)
                    329:                return(mdoc_nerr(m, n, EMALLOC));
                    330:
1.26      kristaps  331:        return(1);
                    332: }
                    333:
                    334:
                    335: static int
1.25      kristaps  336: post_at(POST_ARGS)
                    337: {
1.35      kristaps  338:        struct mdoc_node        *nn;
                    339:        const char              *p;
1.25      kristaps  340:
1.35      kristaps  341:        if (n->child) {
                    342:                assert(MDOC_TEXT == n->child->type);
                    343:                p = mdoc_a2att(n->child->string);
1.25      kristaps  344:                assert(p);
1.35      kristaps  345:                free(n->child->string);
                    346:                n->child->string = strdup(p);
                    347:                if (NULL == n->child->string)
                    348:                        return(mdoc_nerr(m, n, EMALLOC));
1.25      kristaps  349:                return(1);
                    350:        }
                    351:
1.35      kristaps  352:        nn = n;
1.25      kristaps  353:        m->next = MDOC_NEXT_CHILD;
                    354:
1.35      kristaps  355:        if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
1.25      kristaps  356:                return(0);
1.35      kristaps  357:        m->last = nn;
1.25      kristaps  358:
                    359:        return(1);
                    360: }
                    361:
                    362:
                    363: static int
1.1       kristaps  364: post_sh(POST_ARGS)
                    365: {
                    366:        enum mdoc_sec    sec;
                    367:        char             buf[64];
                    368:
                    369:        /*
                    370:         * We keep track of the current section /and/ the "named"
                    371:         * section, which is one of the conventional ones, in order to
                    372:         * check ordering.
                    373:         */
                    374:
1.35      kristaps  375:        if (MDOC_HEAD != n->type)
1.1       kristaps  376:                return(1);
1.5       kristaps  377:
                    378:        buf[0] = 0;
1.35      kristaps  379:        if ( ! concat(m, n->child, buf, sizeof(buf)))
1.2       kristaps  380:                return(0);
1.1       kristaps  381:        if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
                    382:                m->lastnamed = sec;
                    383:
                    384:        switch ((m->lastsec = sec)) {
                    385:        case (SEC_RETURN_VALUES):
                    386:                /* FALLTHROUGH */
                    387:        case (SEC_ERRORS):
                    388:                switch (m->meta.msec) {
                    389:                case (2):
                    390:                        /* FALLTHROUGH */
                    391:                case (3):
                    392:                        /* FALLTHROUGH */
                    393:                case (9):
                    394:                        break;
                    395:                default:
1.35      kristaps  396:                        return(mdoc_nwarn(m, n, EBADSEC));
1.1       kristaps  397:                }
                    398:                break;
                    399:        default:
                    400:                break;
                    401:        }
                    402:        return(1);
                    403: }
                    404:
                    405:
                    406: static int
                    407: post_dt(POST_ARGS)
                    408: {
1.35      kristaps  409:        struct mdoc_node *nn;
1.1       kristaps  410:        const char       *cp;
                    411:        char             *ep;
                    412:        long              lval;
                    413:
                    414:        if (m->meta.title)
                    415:                free(m->meta.title);
                    416:        if (m->meta.vol)
                    417:                free(m->meta.vol);
                    418:        if (m->meta.arch)
                    419:                free(m->meta.arch);
                    420:
                    421:        m->meta.title = m->meta.vol = m->meta.arch = NULL;
                    422:        m->meta.msec = 0;
                    423:
                    424:        /* Handles: `.Dt'
                    425:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    426:         */
                    427:
1.35      kristaps  428:        if (NULL == (nn = n->child)) {
1.2       kristaps  429:                if (NULL == (m->meta.title = strdup("unknown")))
1.35      kristaps  430:                        return(mdoc_nerr(m, n, EMALLOC));
1.2       kristaps  431:                if (NULL == (m->meta.vol = strdup("local")))
1.35      kristaps  432:                        return(mdoc_nerr(m, n, EMALLOC));
                    433:                return(post_prol(m, n));
1.1       kristaps  434:        }
                    435:
                    436:        /* Handles: `.Dt TITLE'
                    437:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                    438:         */
                    439:
1.35      kristaps  440:        if (NULL == (m->meta.title = strdup(nn->string)))
                    441:                return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  442:
1.35      kristaps  443:        if (NULL == (nn = nn->next)) {
1.2       kristaps  444:                if (NULL == (m->meta.vol = strdup("local")))
1.35      kristaps  445:                        return(mdoc_nerr(m, n, EMALLOC));
                    446:                return(post_prol(m, n));
1.1       kristaps  447:        }
                    448:
                    449:        /* Handles: `.Dt TITLE SEC'
                    450:         *   --> title = TITLE, volume = SEC is msec ?
                    451:         *           format(msec) : SEC,
                    452:         *       msec = SEC is msec ? atoi(msec) : 0,
                    453:         *       arch = NULL
                    454:         */
                    455:
1.35      kristaps  456:        cp = mdoc_a2msec(nn->string);
1.1       kristaps  457:        if (cp) {
1.2       kristaps  458:                if (NULL == (m->meta.vol = strdup(cp)))
1.35      kristaps  459:                        return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  460:                errno = 0;
1.35      kristaps  461:                lval = strtol(nn->string, &ep, 10);
                    462:                if (nn->string[0] != '\0' && *ep == '\0')
1.1       kristaps  463:                        m->meta.msec = (int)lval;
1.35      kristaps  464:        } else if (NULL == (m->meta.vol = strdup(nn->string)))
                    465:                return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  466:
1.35      kristaps  467:        if (NULL == (nn = nn->next))
                    468:                return(post_prol(m, n));
1.1       kristaps  469:
                    470:        /* Handles: `.Dt TITLE SEC VOL'
                    471:         *   --> title = TITLE, volume = VOL is vol ?
                    472:         *       format(VOL) :
                    473:         *           VOL is arch ? format(arch) :
                    474:         *               VOL
                    475:         */
                    476:
1.35      kristaps  477:        cp = mdoc_a2vol(nn->string);
1.1       kristaps  478:        if (cp) {
                    479:                free(m->meta.vol);
1.2       kristaps  480:                if (NULL == (m->meta.vol = strdup(cp)))
1.35      kristaps  481:                        return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  482:        } else {
1.35      kristaps  483:                cp = mdoc_a2arch(nn->string);
1.1       kristaps  484:                if (NULL == cp) {
                    485:                        free(m->meta.vol);
1.35      kristaps  486:                        if (NULL == (m->meta.vol = strdup(nn->string)))
                    487:                                return(mdoc_nerr(m, n, EMALLOC));
1.2       kristaps  488:                } else if (NULL == (m->meta.arch = strdup(cp)))
1.35      kristaps  489:                        return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  490:        }
                    491:
                    492:        /* Ignore any subsequent parameters... */
                    493:
1.35      kristaps  494:        return(post_prol(m, n));
1.1       kristaps  495: }
                    496:
                    497:
                    498: static int
                    499: post_os(POST_ARGS)
                    500: {
                    501:        char              buf[64];
                    502:        struct utsname    utsname;
                    503:
                    504:        if (m->meta.os)
                    505:                free(m->meta.os);
1.4       kristaps  506:
                    507:        buf[0] = 0;
1.35      kristaps  508:        if ( ! concat(m, n->child, buf, sizeof(buf)))
1.2       kristaps  509:                return(0);
1.1       kristaps  510:
                    511:        if (0 == buf[0]) {
                    512:                if (-1 == uname(&utsname))
1.35      kristaps  513:                        return(mdoc_nerr(m, n, EUTSNAME));
1.4       kristaps  514:                if (strlcat(buf, utsname.sysname, 64) >= 64)
1.35      kristaps  515:                        return(mdoc_nerr(m, n, ETOOLONG));
1.2       kristaps  516:                if (strlcat(buf, " ", 64) >= 64)
1.35      kristaps  517:                        return(mdoc_nerr(m, n, ETOOLONG));
1.2       kristaps  518:                if (strlcat(buf, utsname.release, 64) >= 64)
1.35      kristaps  519:                        return(mdoc_nerr(m, n, ETOOLONG));
1.1       kristaps  520:        }
                    521:
1.2       kristaps  522:        if (NULL == (m->meta.os = strdup(buf)))
1.35      kristaps  523:                return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  524:
1.35      kristaps  525:        return(post_prol(m, n));
1.1       kristaps  526: }
                    527:
                    528:
                    529: /*
                    530:  * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
                    531:  * Uses the first head macro.
                    532:  */
                    533: static int
1.35      kristaps  534: post_bl_tagwidth(POST_ARGS)
1.1       kristaps  535: {
1.35      kristaps  536:        struct mdoc_node  *nn;
1.1       kristaps  537:        int                sz;
                    538:        char               buf[32];
                    539:
                    540:        /*
                    541:         * Use the text width, if a text node, or the default macro
                    542:         * width if a macro.
                    543:         */
                    544:
1.35      kristaps  545:        nn = n->body->child;
                    546:        if (nn) {
                    547:                assert(MDOC_BLOCK == nn->type);
                    548:                assert(MDOC_It == nn->tok);
                    549:                nn = nn->head->child;
1.1       kristaps  550:        }
                    551:
                    552:        sz = 10; /* Default size. */
                    553:
1.35      kristaps  554:        if (nn) {
                    555:                if (MDOC_TEXT != nn->type) {
                    556:                        if (0 == (sz = (int)mdoc_macro2len(nn->tok)))
                    557:                                if ( ! mdoc_nwarn(m, n, ENOWIDTH))
1.1       kristaps  558:                                        return(0);
                    559:                } else
1.35      kristaps  560:                        sz = (int)strlen(nn->string) + 1;
1.1       kristaps  561:        }
                    562:
1.2       kristaps  563:        if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
1.35      kristaps  564:                return(mdoc_nerr(m, n, ENUMFMT));
1.1       kristaps  565:
                    566:        /*
                    567:         * We have to dynamically add this to the macro's argument list.
                    568:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    569:         */
                    570:
1.35      kristaps  571:        nn = n;
                    572:        assert(nn->args);
                    573:        sz = (int)(nn->args->argc)++;
1.1       kristaps  574:
1.35      kristaps  575:        nn->args->argv = realloc(nn->args->argv,
                    576:                        nn->args->argc * sizeof(struct mdoc_argv));
1.1       kristaps  577:
1.35      kristaps  578:        if (NULL == nn->args->argv)
                    579:                return(mdoc_nerr(m, n, EMALLOC));
1.2       kristaps  580:
1.35      kristaps  581:        nn->args->argv[sz].arg = MDOC_Width;
                    582:        nn->args->argv[sz].line = n->line;
                    583:        nn->args->argv[sz].pos = n->pos;
                    584:        nn->args->argv[sz].sz = 1;
                    585:        nn->args->argv[sz].value = calloc(1, sizeof(char *));
1.2       kristaps  586:
1.35      kristaps  587:        if (NULL == nn->args->argv[sz].value)
                    588:                return(mdoc_nerr(m, n, EMALLOC));
                    589:        if (NULL == (nn->args->argv[sz].value[0] = strdup(buf)))
                    590:                return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  591:
                    592:        return(1);
                    593: }
                    594:
                    595:
                    596: static int
1.35      kristaps  597: post_bl_width(POST_ARGS)
1.1       kristaps  598: {
                    599:        size_t            width;
                    600:        int               i, tok;
                    601:        char              buf[32];
                    602:        char             *p;
                    603:
1.35      kristaps  604:        if (NULL == n->args)
1.1       kristaps  605:                return(1);
                    606:
1.35      kristaps  607:        for (i = 0; i < (int)n->args->argc; i++)
                    608:                if (MDOC_Width == n->args->argv[i].arg)
1.1       kristaps  609:                        break;
                    610:
1.35      kristaps  611:        if (i == (int)n->args->argc)
1.1       kristaps  612:                return(1);
1.35      kristaps  613:        p = n->args->argv[i].value[0];
1.1       kristaps  614:
                    615:        /*
                    616:         * If the value to -width is a macro, then we re-write it to be
                    617:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    618:         */
                    619:
1.2       kristaps  620:        if (0 == strcmp(p, "Ds"))
1.18      kristaps  621:                width = 6;
1.36      kristaps  622:        else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
1.1       kristaps  623:                return(1);
                    624:        else if (0 == (width = mdoc_macro2len(tok)))
1.35      kristaps  625:                return(mdoc_nwarn(m, n, ENOWIDTH));
1.1       kristaps  626:
                    627:        /* The value already exists: free and reallocate it. */
                    628:
1.2       kristaps  629:        if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
1.35      kristaps  630:                return(mdoc_nerr(m, n, ENUMFMT));
1.1       kristaps  631:
1.35      kristaps  632:        free(n->args->argv[i].value[0]);
                    633:        n->args->argv[i].value[0] = strdup(buf);
                    634:        if (NULL == n->args->argv[i].value[0])
                    635:                return(mdoc_nerr(m, n, EMALLOC));
1.1       kristaps  636:
                    637:        return(1);
                    638: }
                    639:
                    640:
1.35      kristaps  641: /* ARGSUSED */
1.1       kristaps  642: static int
1.14      kristaps  643: post_bl_head(POST_ARGS)
                    644: {
                    645:        int                      i, c;
1.35      kristaps  646:        struct mdoc_node        *np, *nn, *nnp;
1.14      kristaps  647:
1.35      kristaps  648:        if (NULL == n->child)
1.14      kristaps  649:                return(1);
                    650:
1.35      kristaps  651:        np = n->parent;
                    652:        assert(np->args);
1.14      kristaps  653:
1.35      kristaps  654:        for (c = 0; c < (int)np->args->argc; c++)
                    655:                if (MDOC_Column == np->args->argv[c].arg)
1.14      kristaps  656:                        break;
                    657:
                    658:        /* Only process -column. */
                    659:
1.35      kristaps  660:        if (c == (int)np->args->argc)
1.14      kristaps  661:                return(1);
                    662:
1.35      kristaps  663:        assert(0 == np->args->argv[c].sz);
1.14      kristaps  664:
                    665:        /*
                    666:         * Accomodate for new-style groff column syntax.  Shuffle the
                    667:         * child nodes, all of which must be TEXT, as arguments for the
                    668:         * column field.  Then, delete the head children.
                    669:         */
                    670:
1.35      kristaps  671:        np->args->argv[c].sz = (size_t)n->nchild;
                    672:        np->args->argv[c].value = malloc
                    673:                ((size_t)n->nchild * sizeof(char *));
1.14      kristaps  674:
1.35      kristaps  675:        for (i = 0, nn = n->child; nn; i++) {
                    676:                np->args->argv[c].value[i] = nn->string;
1.14      kristaps  677:                nn->string = NULL;
                    678:                nnp = nn;
                    679:                nn = nn->next;
                    680:                mdoc_node_free(nnp);
                    681:        }
                    682:
1.35      kristaps  683:        n->nchild = 0;
                    684:        n->child = NULL;
1.17      kristaps  685:
1.14      kristaps  686:        return(1);
                    687: }
                    688:
                    689:
                    690: static int
1.1       kristaps  691: post_bl(POST_ARGS)
                    692: {
                    693:        int               i, r, len;
                    694:
1.35      kristaps  695:        if (MDOC_HEAD == n->type)
                    696:                return(post_bl_head(m, n));
                    697:        if (MDOC_BLOCK != n->type)
1.1       kristaps  698:                return(1);
                    699:
                    700:        /*
                    701:         * These are fairly complicated, so we've broken them into two
                    702:         * functions.  post_bl_tagwidth() is called when a -tag is
                    703:         * specified, but no -width (it must be guessed).  The second
                    704:         * when a -width is specified (macro indicators must be
                    705:         * rewritten into real lengths).
                    706:         */
                    707:
1.35      kristaps  708:        len = (int)(n->args ? n->args->argc : 0);
1.1       kristaps  709:
                    710:        for (r = i = 0; i < len; i++) {
1.35      kristaps  711:                if (MDOC_Tag == n->args->argv[i].arg)
1.1       kristaps  712:                        r |= 1 << 0;
1.35      kristaps  713:                if (MDOC_Width == n->args->argv[i].arg)
1.1       kristaps  714:                        r |= 1 << 1;
                    715:        }
                    716:
                    717:        if (r & (1 << 0) && ! (r & (1 << 1))) {
1.35      kristaps  718:                if ( ! post_bl_tagwidth(m, n))
1.1       kristaps  719:                        return(0);
                    720:        } else if (r & (1 << 1))
1.35      kristaps  721:                if ( ! post_bl_width(m, n))
1.1       kristaps  722:                        return(0);
                    723:
                    724:        return(1);
                    725: }
                    726:
                    727:
                    728: static int
1.31      kristaps  729: post_tilde(POST_ARGS)
1.10      kristaps  730: {
1.35      kristaps  731:        struct mdoc_node *np;
1.10      kristaps  732:
1.35      kristaps  733:        if (n->child)
1.10      kristaps  734:                return(1);
                    735:
1.35      kristaps  736:        np = n;
1.10      kristaps  737:        m->next = MDOC_NEXT_CHILD;
1.24      kristaps  738:
1.31      kristaps  739:        /* XXX: not documented for `Lk'. */
1.35      kristaps  740:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.10      kristaps  741:                return(0);
1.35      kristaps  742:        m->last = np;
1.10      kristaps  743:
                    744:        return(1);
                    745: }
                    746:
                    747:
                    748: static int
1.1       kristaps  749: post_ar(POST_ARGS)
                    750: {
1.35      kristaps  751:        struct mdoc_node *np;
1.1       kristaps  752:
1.35      kristaps  753:        if (n->child)
1.1       kristaps  754:                return(1);
                    755:
1.35      kristaps  756:        np = n;
1.1       kristaps  757:        m->next = MDOC_NEXT_CHILD;
1.35      kristaps  758:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1       kristaps  759:                return(0);
1.35      kristaps  760:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1       kristaps  761:                return(0);
1.35      kristaps  762:        m->last = np;
1.1       kristaps  763:
                    764:        return(1);
                    765: }
                    766:
                    767:
                    768: static int
                    769: post_dd(POST_ARGS)
                    770: {
                    771:        char              buf[64];
                    772:
1.5       kristaps  773:        buf[0] = 0;
1.35      kristaps  774:        if ( ! concat(m, n->child, buf, sizeof(buf)))
1.2       kristaps  775:                return(0);
1.1       kristaps  776:
                    777:        if (0 == (m->meta.date = mdoc_atotime(buf))) {
1.35      kristaps  778:                if ( ! mdoc_nwarn(m, n, EBADDATE))
1.1       kristaps  779:                        return(0);
                    780:                m->meta.date = time(NULL);
                    781:        }
                    782:
1.35      kristaps  783:        return(post_prol(m, n));
1.1       kristaps  784: }
                    785:
                    786:
                    787: static int
                    788: post_prol(POST_ARGS)
                    789: {
1.35      kristaps  790:        struct mdoc_node *np;
1.1       kristaps  791:
1.35      kristaps  792:        /* Remove prologue macros from AST.  */
1.1       kristaps  793:
1.35      kristaps  794:        if (n->parent->child == n)
                    795:                n->parent->child = n->prev;
                    796:        if (n->prev)
                    797:                n->prev->next = NULL;
1.1       kristaps  798:
1.35      kristaps  799:        np = n;
                    800:        assert(NULL == n->next);
1.1       kristaps  801:
1.35      kristaps  802:        if (n->prev) {
                    803:                m->last = n->prev;
1.1       kristaps  804:                m->next = MDOC_NEXT_SIBLING;
                    805:        } else {
1.35      kristaps  806:                m->last = n->parent;
1.1       kristaps  807:                m->next = MDOC_NEXT_CHILD;
                    808:        }
                    809:
1.35      kristaps  810:        mdoc_node_freelist(np);
1.34      kristaps  811:
                    812:        if (m->meta.title && m->meta.date && m->meta.os)
                    813:                m->flags |= MDOC_PBODY;
1.35      kristaps  814:
1.1       kristaps  815:        return(1);
                    816: }
                    817:
                    818:
                    819: static int
                    820: pre_dl(PRE_ARGS)
                    821: {
                    822:
1.16      kristaps  823:        if (MDOC_BODY == n->type)
                    824:                m->flags |= MDOC_LITERAL;
1.1       kristaps  825:        return(1);
                    826: }
                    827:
                    828:
                    829: static int
1.38    ! kristaps  830: pre_offset(PRE_ARGS)
        !           831: {
        !           832:        int              i;
        !           833:
        !           834:        /*
        !           835:         * Make sure that an empty offset produces an 8n length space as
        !           836:         * stipulated by mdoc.samples.
        !           837:         */
        !           838:
        !           839:        assert(n->args);
        !           840:        for (i = 0; i < (int)n->args->argc; i++) {
        !           841:                if (MDOC_Offset != n->args->argv[i].arg)
        !           842:                        continue;
        !           843:                if (n->args->argv[i].sz)
        !           844:                        break;
        !           845:                assert(1 == n->args->refcnt);
        !           846:                /* If no value set, length of <string>. */
        !           847:                n->args->argv[i].value =
        !           848:                calloc(1, sizeof(char *));
        !           849:                if (NULL == n->args->argv[i].value)
        !           850:                        return(mdoc_nerr(m, n, EMALLOC));
        !           851:                n->args->argv[i].sz++;
        !           852:                n->args->argv[i].value[0] = strdup("8n");
        !           853:                if (NULL == n->args->argv[i].value[0])
        !           854:                        return(mdoc_nerr(m, n, EMALLOC));
        !           855:                break;
        !           856:        }
        !           857:
        !           858:        return(1);
        !           859: }
        !           860:
        !           861:
        !           862: static int
        !           863: pre_bl(PRE_ARGS)
        !           864: {
        !           865:
        !           866:        return(MDOC_BLOCK == n->type ? pre_offset(m, n) : 1);
        !           867: }
        !           868:
        !           869:
        !           870: static int
1.1       kristaps  871: pre_bd(PRE_ARGS)
                    872: {
                    873:        int              i;
                    874:
1.38    ! kristaps  875:        if (MDOC_BLOCK == n->type)
        !           876:                return(pre_offset(m, n));
1.1       kristaps  877:        if (MDOC_BODY != n->type)
                    878:                return(1);
                    879:
1.32      kristaps  880:        /* Enter literal context if `Bd -literal' or `-unfilled'. */
                    881:
1.2       kristaps  882:        for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
1.1       kristaps  883:                if (MDOC_Literal == n->args->argv[i].arg)
1.38    ! kristaps  884:                        m->flags |= MDOC_LITERAL;
1.1       kristaps  885:                else if (MDOC_Unfilled == n->args->argv[i].arg)
1.38    ! kristaps  886:                        m->flags |= MDOC_LITERAL;
1.1       kristaps  887:
                    888:        return(1);
                    889: }
                    890:
                    891:
                    892: static int
                    893: post_display(POST_ARGS)
                    894: {
                    895:
1.35      kristaps  896:        if (MDOC_BODY == n->type)
1.1       kristaps  897:                m->flags &= ~MDOC_LITERAL;
                    898:        return(1);
                    899: }
                    900:
                    901:

CVSweb