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

Annotation of mandoc/mdoc_action.c, Revision 1.2

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

CVSweb