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

Annotation of mandoc/mdoc_action.c, Revision 1.20

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

CVSweb