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

Annotation of mandoc/mdoc_action.c, Revision 1.28

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

CVSweb