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

Annotation of mandoc/mdoc_action.c, Revision 1.26

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

CVSweb