[BACK]Return to mdoc_action.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc_action.c, Revision 1.13

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