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

Annotation of mandoc/mdoc_action.c, Revision 1.7

1.7     ! kristaps    1: /* $Id: mdoc_action.c,v 1.6 2009/04/06 08:53:12 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  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;
                    276:        case (EMALLOC):
                    277:                p = "memory exhausted";
                    278:                break;
                    279:        }
                    280:        assert(p);
                    281:        return(mdoc_perr(m, line, pos, p));
                    282: }
                    283:
                    284:
                    285: static int
1.1       kristaps  286: pwarn(struct mdoc *m, int line, int pos, enum mwarn type)
                    287: {
                    288:        char            *p;
                    289:        int              c;
                    290:
                    291:        p = NULL;
                    292:        c = WARN_SYNTAX;
                    293:        switch (type) {
                    294:        case (WBADSEC):
                    295:                p = "inappropriate document section in manual section";
                    296:                c = WARN_COMPAT;
                    297:                break;
                    298:        case (WNOWIDTH):
                    299:                p = "cannot determine default width";
                    300:                break;
                    301:        case (WBADDATE):
                    302:                p = "malformed date syntax";
                    303:                break;
                    304:        }
                    305:        assert(p);
                    306:        return(mdoc_pwarn(m, line, pos, c, p));
                    307: }
                    308:
                    309:
                    310: static int
                    311: post_std(POST_ARGS)
                    312: {
                    313:
                    314:        /*
                    315:         * If '-std' is invoked without an argument, fill it in with our
                    316:         * name (if it's been set).
                    317:         */
                    318:
                    319:        if (NULL == m->last->args)
                    320:                return(1);
                    321:        if (m->last->args->argv[0].sz)
                    322:                return(1);
                    323:
                    324:        assert(m->meta.name);
                    325:
                    326:        m->last->args->argv[0].value = calloc(1, sizeof(char *));
                    327:        if (NULL == m->last->args->argv[0].value)
1.2       kristaps  328:                return(verr(m, EMALLOC));
1.1       kristaps  329:
                    330:        m->last->args->argv[0].sz = 1;
1.2       kristaps  331:        m->last->args->argv[0].value[0] = strdup(m->meta.name);
                    332:        if (NULL == m->last->args->argv[0].value[0])
                    333:                return(verr(m, EMALLOC));
                    334:
1.1       kristaps  335:        return(1);
                    336: }
                    337:
                    338:
                    339: static int
                    340: post_nm(POST_ARGS)
                    341: {
                    342:        char             buf[64];
                    343:
                    344:        if (m->meta.name)
                    345:                return(1);
1.5       kristaps  346:
                    347:        buf[0] = 0;
1.2       kristaps  348:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    349:                return(0);
1.6       kristaps  350:
1.2       kristaps  351:        if (NULL == (m->meta.name = strdup(buf)))
                    352:                return(verr(m, EMALLOC));
1.1       kristaps  353:
                    354:        return(1);
                    355: }
                    356:
                    357:
                    358: static int
                    359: post_sh(POST_ARGS)
                    360: {
                    361:        enum mdoc_sec    sec;
                    362:        char             buf[64];
                    363:
                    364:        /*
                    365:         * We keep track of the current section /and/ the "named"
                    366:         * section, which is one of the conventional ones, in order to
                    367:         * check ordering.
                    368:         */
                    369:
                    370:        if (MDOC_HEAD != m->last->type)
                    371:                return(1);
1.5       kristaps  372:
                    373:        buf[0] = 0;
1.2       kristaps  374:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    375:                return(0);
1.1       kristaps  376:        if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
                    377:                m->lastnamed = sec;
                    378:
                    379:        switch ((m->lastsec = sec)) {
                    380:        case (SEC_RETURN_VALUES):
                    381:                /* FALLTHROUGH */
                    382:        case (SEC_ERRORS):
                    383:                switch (m->meta.msec) {
                    384:                case (2):
                    385:                        /* FALLTHROUGH */
                    386:                case (3):
                    387:                        /* FALLTHROUGH */
                    388:                case (9):
                    389:                        break;
                    390:                default:
1.2       kristaps  391:                        return(vwarn(m, WBADSEC));
1.1       kristaps  392:                }
                    393:                break;
                    394:        default:
                    395:                break;
                    396:        }
                    397:        return(1);
                    398: }
                    399:
                    400:
                    401: static int
                    402: post_dt(POST_ARGS)
                    403: {
                    404:        struct mdoc_node *n;
                    405:        const char       *cp;
                    406:        char             *ep;
                    407:        long              lval;
                    408:
                    409:        if (m->meta.title)
                    410:                free(m->meta.title);
                    411:        if (m->meta.vol)
                    412:                free(m->meta.vol);
                    413:        if (m->meta.arch)
                    414:                free(m->meta.arch);
                    415:
                    416:        m->meta.title = m->meta.vol = m->meta.arch = NULL;
                    417:        m->meta.msec = 0;
                    418:
                    419:        /* Handles: `.Dt'
                    420:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    421:         */
                    422:
                    423:        if (NULL == (n = m->last->child)) {
1.2       kristaps  424:                if (NULL == (m->meta.title = strdup("unknown")))
                    425:                        return(verr(m, EMALLOC));
                    426:                if (NULL == (m->meta.vol = strdup("local")))
                    427:                        return(verr(m, EMALLOC));
1.1       kristaps  428:                return(post_prol(m));
                    429:        }
                    430:
                    431:        /* Handles: `.Dt TITLE'
                    432:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                    433:         */
                    434:
1.2       kristaps  435:        if (NULL == (m->meta.title = strdup(n->string)))
                    436:                return(verr(m, EMALLOC));
1.1       kristaps  437:
                    438:        if (NULL == (n = n->next)) {
1.2       kristaps  439:                if (NULL == (m->meta.vol = strdup("local")))
                    440:                        return(verr(m, EMALLOC));
1.1       kristaps  441:                return(post_prol(m));
                    442:        }
                    443:
                    444:        /* Handles: `.Dt TITLE SEC'
                    445:         *   --> title = TITLE, volume = SEC is msec ?
                    446:         *           format(msec) : SEC,
                    447:         *       msec = SEC is msec ? atoi(msec) : 0,
                    448:         *       arch = NULL
                    449:         */
                    450:
                    451:        cp = mdoc_a2msec(n->string);
                    452:        if (cp) {
1.2       kristaps  453:                if (NULL == (m->meta.vol = strdup(cp)))
                    454:                        return(verr(m, EMALLOC));
1.1       kristaps  455:                errno = 0;
                    456:                lval = strtol(n->string, &ep, 10);
                    457:                if (n->string[0] != '\0' && *ep == '\0')
                    458:                        m->meta.msec = (int)lval;
1.2       kristaps  459:        } else if (NULL == (m->meta.vol = strdup(n->string)))
                    460:                return(verr(m, EMALLOC));
1.1       kristaps  461:
                    462:        if (NULL == (n = n->next))
                    463:                return(post_prol(m));
                    464:
                    465:        /* Handles: `.Dt TITLE SEC VOL'
                    466:         *   --> title = TITLE, volume = VOL is vol ?
                    467:         *       format(VOL) :
                    468:         *           VOL is arch ? format(arch) :
                    469:         *               VOL
                    470:         */
                    471:
                    472:        cp = mdoc_a2vol(n->string);
                    473:        if (cp) {
                    474:                free(m->meta.vol);
1.2       kristaps  475:                if (NULL == (m->meta.vol = strdup(cp)))
                    476:                        return(verr(m, EMALLOC));
1.1       kristaps  477:                n = n->next;
                    478:        } else {
                    479:                cp = mdoc_a2arch(n->string);
                    480:                if (NULL == cp) {
                    481:                        free(m->meta.vol);
1.2       kristaps  482:                        if (NULL == (m->meta.vol = strdup(n->string)))
                    483:                                return(verr(m, EMALLOC));
                    484:                } else if (NULL == (m->meta.arch = strdup(cp)))
                    485:                        return(verr(m, EMALLOC));
1.1       kristaps  486:        }
                    487:
                    488:        /* Ignore any subsequent parameters... */
                    489:
                    490:        return(post_prol(m));
                    491: }
                    492:
                    493:
                    494: static int
                    495: post_os(POST_ARGS)
                    496: {
                    497:        char              buf[64];
                    498:        struct utsname    utsname;
                    499:
                    500:        if (m->meta.os)
                    501:                free(m->meta.os);
1.4       kristaps  502:
                    503:        buf[0] = 0;
1.2       kristaps  504:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    505:                return(0);
1.1       kristaps  506:
                    507:        if (0 == buf[0]) {
                    508:                if (-1 == uname(&utsname))
                    509:                        return(mdoc_err(m, "utsname"));
1.4       kristaps  510:                if (strlcat(buf, utsname.sysname, 64) >= 64)
1.2       kristaps  511:                        return(verr(m, ETOOLONG));
                    512:                if (strlcat(buf, " ", 64) >= 64)
                    513:                        return(verr(m, ETOOLONG));
                    514:                if (strlcat(buf, utsname.release, 64) >= 64)
                    515:                        return(verr(m, ETOOLONG));
1.1       kristaps  516:        }
                    517:
1.2       kristaps  518:        if (NULL == (m->meta.os = strdup(buf)))
                    519:                return(verr(m, EMALLOC));
1.1       kristaps  520:        m->lastnamed = m->lastsec = SEC_BODY;
                    521:
                    522:        return(post_prol(m));
                    523: }
                    524:
                    525:
                    526: /*
                    527:  * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
                    528:  * Uses the first head macro.
                    529:  */
                    530: static int
                    531: post_bl_tagwidth(struct mdoc *m)
                    532: {
                    533:        struct mdoc_node  *n;
                    534:        int                sz;
                    535:        char               buf[32];
                    536:
                    537:        /*
                    538:         * Use the text width, if a text node, or the default macro
                    539:         * width if a macro.
                    540:         */
                    541:
                    542:        if ((n = m->last->body->child)) {
                    543:                assert(MDOC_BLOCK == n->type);
                    544:                assert(MDOC_It == n->tok);
                    545:                n = n->head->child;
                    546:        }
                    547:
                    548:        sz = 10; /* Default size. */
                    549:
                    550:        if (n) {
                    551:                if (MDOC_TEXT != n->type) {
                    552:                        if (0 == (sz = (int)mdoc_macro2len(n->tok)))
1.2       kristaps  553:                                if ( ! vwarn(m, WNOWIDTH))
1.1       kristaps  554:                                        return(0);
                    555:                } else
                    556:                        sz = (int)strlen(n->string) + 1;
                    557:        }
                    558:
1.2       kristaps  559:        if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
                    560:                return(verr(m, ENUMFMT));
1.1       kristaps  561:
                    562:        /*
                    563:         * We have to dynamically add this to the macro's argument list.
                    564:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    565:         */
                    566:
                    567:        n = m->last;
                    568:        assert(n->args);
1.2       kristaps  569:        sz = (int)(n->args->argc)++;
1.1       kristaps  570:
1.2       kristaps  571:        n->args->argv = realloc(n->args->argv,
1.1       kristaps  572:                        n->args->argc * sizeof(struct mdoc_argv));
                    573:
1.2       kristaps  574:        if (NULL == n->args->argv)
                    575:                return(verr(m, EMALLOC));
                    576:
1.1       kristaps  577:        n->args->argv[sz].arg = MDOC_Width;
                    578:        n->args->argv[sz].line = m->last->line;
                    579:        n->args->argv[sz].pos = m->last->pos;
                    580:        n->args->argv[sz].sz = 1;
                    581:        n->args->argv[sz].value = calloc(1, sizeof(char *));
1.2       kristaps  582:
1.1       kristaps  583:        if (NULL == n->args->argv[sz].value)
1.2       kristaps  584:                return(verr(m, EMALLOC));
                    585:        if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
                    586:                return(verr(m, EMALLOC));
1.1       kristaps  587:
                    588:        return(1);
                    589: }
                    590:
                    591:
                    592: static int
                    593: post_bl_width(struct mdoc *m)
                    594: {
                    595:        size_t            width;
                    596:        int               i, tok;
                    597:        char              buf[32];
                    598:        char             *p;
                    599:
                    600:        if (NULL == m->last->args)
                    601:                return(1);
                    602:
                    603:        for (i = 0; i < (int)m->last->args->argc; i++)
                    604:                if (MDOC_Width == m->last->args->argv[i].arg)
                    605:                        break;
                    606:
                    607:        if (i == (int)m->last->args->argc)
                    608:                return(1);
                    609:        p = m->last->args->argv[i].value[0];
                    610:
                    611:        /*
                    612:         * If the value to -width is a macro, then we re-write it to be
                    613:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    614:         */
                    615:
1.2       kristaps  616:        if (0 == strcmp(p, "Ds"))
1.1       kristaps  617:                width = 8;
1.3       kristaps  618:        else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
1.1       kristaps  619:                return(1);
                    620:        else if (0 == (width = mdoc_macro2len(tok)))
1.2       kristaps  621:                return(vwarn(m, WNOWIDTH));
1.1       kristaps  622:
                    623:        /* The value already exists: free and reallocate it. */
                    624:
1.2       kristaps  625:        if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
                    626:                return(verr(m, ENUMFMT));
1.1       kristaps  627:
                    628:        free(m->last->args->argv[i].value[0]);
1.2       kristaps  629:        m->last->args->argv[i].value[0] = strdup(buf);
                    630:        if (NULL == m->last->args->argv[i].value[0])
                    631:                return(verr(m, EMALLOC));
1.1       kristaps  632:
                    633:        return(1);
                    634: }
                    635:
                    636:
                    637: static int
                    638: post_bl(POST_ARGS)
                    639: {
                    640:        int               i, r, len;
                    641:
                    642:        if (MDOC_BLOCK != m->last->type)
                    643:                return(1);
                    644:
                    645:        /*
                    646:         * These are fairly complicated, so we've broken them into two
                    647:         * functions.  post_bl_tagwidth() is called when a -tag is
                    648:         * specified, but no -width (it must be guessed).  The second
                    649:         * when a -width is specified (macro indicators must be
                    650:         * rewritten into real lengths).
                    651:         */
                    652:
                    653:        len = (int)(m->last->args ? m->last->args->argc : 0);
                    654:
                    655:        for (r = i = 0; i < len; i++) {
                    656:                if (MDOC_Tag == m->last->args->argv[i].arg)
                    657:                        r |= 1 << 0;
                    658:                if (MDOC_Width == m->last->args->argv[i].arg)
                    659:                        r |= 1 << 1;
                    660:        }
                    661:
                    662:        if (r & (1 << 0) && ! (r & (1 << 1))) {
                    663:                if ( ! post_bl_tagwidth(m))
                    664:                        return(0);
                    665:        } else if (r & (1 << 1))
                    666:                if ( ! post_bl_width(m))
                    667:                        return(0);
                    668:
                    669:        return(1);
                    670: }
                    671:
                    672:
                    673: static int
                    674: post_ar(POST_ARGS)
                    675: {
                    676:        struct mdoc_node *n;
                    677:
                    678:        if (m->last->child)
                    679:                return(1);
                    680:
                    681:        n = m->last;
                    682:        m->next = MDOC_NEXT_CHILD;
                    683:        if ( ! mdoc_word_alloc(m, m->last->line,
                    684:                                m->last->pos, "file"))
                    685:                return(0);
                    686:        m->next = MDOC_NEXT_SIBLING;
                    687:        if ( ! mdoc_word_alloc(m, m->last->line,
                    688:                                m->last->pos, "..."))
                    689:                return(0);
                    690:
                    691:        m->last = n;
                    692:        m->next = MDOC_NEXT_SIBLING;
                    693:        return(1);
                    694: }
                    695:
                    696:
                    697: static int
                    698: post_dd(POST_ARGS)
                    699: {
                    700:        char              buf[64];
                    701:
1.5       kristaps  702:        buf[0] = 0;
1.2       kristaps  703:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    704:                return(0);
1.1       kristaps  705:
                    706:        if (0 == (m->meta.date = mdoc_atotime(buf))) {
1.2       kristaps  707:                if ( ! vwarn(m, WBADDATE))
1.1       kristaps  708:                        return(0);
                    709:                m->meta.date = time(NULL);
                    710:        }
                    711:
                    712:        return(post_prol(m));
                    713: }
                    714:
                    715:
                    716: static int
                    717: post_prol(POST_ARGS)
                    718: {
                    719:        struct mdoc_node *n;
                    720:
                    721:        /*
                    722:         * The end document shouldn't have the prologue macros as part
                    723:         * of the syntax tree (they encompass only meta-data).
                    724:         */
                    725:
                    726:        if (m->last->parent->child == m->last)
                    727:                m->last->parent->child = m->last->prev;
                    728:        if (m->last->prev)
                    729:                m->last->prev->next = NULL;
                    730:
                    731:        n = m->last;
                    732:        assert(NULL == m->last->next);
                    733:
                    734:        if (m->last->prev) {
                    735:                m->last = m->last->prev;
                    736:                m->next = MDOC_NEXT_SIBLING;
                    737:        } else {
                    738:                m->last = m->last->parent;
                    739:                m->next = MDOC_NEXT_CHILD;
                    740:        }
                    741:
                    742:        mdoc_node_freelist(n);
                    743:        return(1);
                    744: }
                    745:
                    746:
                    747: static int
                    748: pre_dl(PRE_ARGS)
                    749: {
                    750:
                    751:        if (MDOC_BODY != n->type)
                    752:                return(1);
                    753:        m->flags |= MDOC_LITERAL;
                    754:        return(1);
                    755: }
                    756:
                    757:
                    758: static int
                    759: pre_bd(PRE_ARGS)
                    760: {
                    761:        int              i;
                    762:
                    763:        if (MDOC_BODY != n->type)
                    764:                return(1);
                    765:
1.2       kristaps  766:        /* Enter literal context if `Bd -literal' or * -unfilled'. */
1.1       kristaps  767:
1.2       kristaps  768:        for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
1.1       kristaps  769:                if (MDOC_Literal == n->args->argv[i].arg)
                    770:                        break;
                    771:                else if (MDOC_Unfilled == n->args->argv[i].arg)
                    772:                        break;
                    773:
                    774:        if (i < (int)n->args->argc)
                    775:                m->flags |= MDOC_LITERAL;
                    776:
                    777:        return(1);
                    778: }
                    779:
                    780:
                    781: static int
                    782: post_display(POST_ARGS)
                    783: {
                    784:
                    785:        if (MDOC_BODY == m->last->type)
                    786:                m->flags &= ~MDOC_LITERAL;
                    787:        return(1);
                    788: }
                    789:
                    790:

CVSweb