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

Annotation of mandoc/mdoc_action.c, Revision 1.23

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

CVSweb