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

Annotation of mandoc/mdoc_action.c, Revision 1.25

1.25    ! kristaps    1: /*     $Id: mdoc_action.c,v 1.24 2009/07/07 11:47:17 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:
                     35: static int       post_ar(POST_ARGS);
1.25    ! kristaps   36: static int       post_at(POST_ARGS);
1.1       kristaps   37: static int       post_bl(POST_ARGS);
1.14      kristaps   38: static int       post_bl_head(POST_ARGS);
1.25    ! kristaps   39: static int       post_bl_tagwidth(POST_ARGS);
1.1       kristaps   40: static int       post_bl_width(POST_ARGS);
                     41: static int       post_dd(POST_ARGS);
                     42: static int       post_display(POST_ARGS);
                     43: static int       post_dt(POST_ARGS);
1.10      kristaps   44: static int       post_lk(POST_ARGS);
1.1       kristaps   45: static int       post_nm(POST_ARGS);
                     46: static int       post_os(POST_ARGS);
                     47: static int       post_prol(POST_ARGS);
                     48: static int       post_sh(POST_ARGS);
                     49: static int       post_std(POST_ARGS);
                     50:
                     51: static int       pre_bd(PRE_ARGS);
                     52: static int       pre_dl(PRE_ARGS);
                     53:
                     54: const  struct actions mdoc_actions[MDOC_MAX] = {
1.12      kristaps   55:        { NULL, NULL }, /* Ap */
1.1       kristaps   56:        { NULL, post_dd }, /* Dd */
                     57:        { NULL, post_dt }, /* Dt */
                     58:        { NULL, post_os }, /* Os */
                     59:        { NULL, post_sh }, /* Sh */
                     60:        { NULL, NULL }, /* Ss */
                     61:        { NULL, NULL }, /* Pp */
                     62:        { NULL, NULL }, /* D1 */
                     63:        { pre_dl, post_display }, /* Dl */
                     64:        { pre_bd, post_display }, /* Bd */
                     65:        { NULL, NULL }, /* Ed */
                     66:        { NULL, post_bl }, /* Bl */
                     67:        { NULL, NULL }, /* El */
                     68:        { NULL, NULL }, /* It */
                     69:        { NULL, NULL }, /* Ad */
                     70:        { NULL, NULL }, /* An */
                     71:        { NULL, post_ar }, /* Ar */
1.19      kristaps   72:        { NULL, NULL }, /* Cd */ /* FIXME: tabs are accepted! */
1.1       kristaps   73:        { NULL, NULL }, /* Cm */
                     74:        { NULL, NULL }, /* Dv */
                     75:        { NULL, NULL }, /* Er */
                     76:        { NULL, NULL }, /* Ev */
                     77:        { NULL, post_std }, /* Ex */
                     78:        { NULL, NULL }, /* Fa */
                     79:        { NULL, NULL }, /* Fd */
                     80:        { NULL, NULL }, /* Fl */
                     81:        { NULL, NULL }, /* Fn */
                     82:        { NULL, NULL }, /* Ft */
                     83:        { NULL, NULL }, /* Ic */
                     84:        { NULL, NULL }, /* In */
                     85:        { NULL, NULL }, /* Li */
                     86:        { NULL, NULL }, /* Nd */
                     87:        { NULL, post_nm }, /* Nm */
                     88:        { NULL, NULL }, /* Op */
                     89:        { NULL, NULL }, /* Ot */
                     90:        { NULL, NULL }, /* Pa */
                     91:        { NULL, post_std }, /* Rv */
                     92:        { NULL, NULL }, /* St */
                     93:        { NULL, NULL }, /* Va */
                     94:        { NULL, NULL }, /* Vt */
                     95:        { NULL, NULL }, /* Xr */
                     96:        { NULL, NULL }, /* %A */
                     97:        { NULL, NULL }, /* %B */
                     98:        { NULL, NULL }, /* %D */
                     99:        { NULL, NULL }, /* %I */
                    100:        { NULL, NULL }, /* %J */
                    101:        { NULL, NULL }, /* %N */
                    102:        { NULL, NULL }, /* %O */
                    103:        { NULL, NULL }, /* %P */
                    104:        { NULL, NULL }, /* %R */
                    105:        { NULL, NULL }, /* %T */
                    106:        { NULL, NULL }, /* %V */
                    107:        { NULL, NULL }, /* Ac */
                    108:        { NULL, NULL }, /* Ao */
                    109:        { NULL, NULL }, /* Aq */
1.25    ! kristaps  110:        { NULL, post_at }, /* At */
1.1       kristaps  111:        { NULL, NULL }, /* Bc */
                    112:        { NULL, NULL }, /* Bf */
                    113:        { NULL, NULL }, /* Bo */
                    114:        { NULL, NULL }, /* Bq */
                    115:        { NULL, NULL }, /* Bsx */
                    116:        { NULL, NULL }, /* Bx */
                    117:        { NULL, NULL }, /* Db */
                    118:        { NULL, NULL }, /* Dc */
                    119:        { NULL, NULL }, /* Do */
                    120:        { NULL, NULL }, /* Dq */
                    121:        { NULL, NULL }, /* Ec */
                    122:        { NULL, NULL }, /* Ef */
                    123:        { NULL, NULL }, /* Em */
                    124:        { NULL, NULL }, /* Eo */
                    125:        { NULL, NULL }, /* Fx */
                    126:        { NULL, NULL }, /* Ms */
                    127:        { NULL, NULL }, /* No */
                    128:        { NULL, NULL }, /* Ns */
                    129:        { NULL, NULL }, /* Nx */
                    130:        { NULL, NULL }, /* Ox */
                    131:        { NULL, NULL }, /* Pc */
                    132:        { NULL, NULL }, /* Pf */
                    133:        { NULL, NULL }, /* Po */
                    134:        { NULL, NULL }, /* Pq */
                    135:        { NULL, NULL }, /* Qc */
                    136:        { NULL, NULL }, /* Ql */
                    137:        { NULL, NULL }, /* Qo */
                    138:        { NULL, NULL }, /* Qq */
                    139:        { NULL, NULL }, /* Re */
                    140:        { NULL, NULL }, /* Rs */
                    141:        { NULL, NULL }, /* Sc */
                    142:        { NULL, NULL }, /* So */
                    143:        { NULL, NULL }, /* Sq */
                    144:        { NULL, NULL }, /* Sm */
                    145:        { NULL, NULL }, /* Sx */
                    146:        { NULL, NULL }, /* Sy */
                    147:        { NULL, NULL }, /* Tn */
                    148:        { NULL, NULL }, /* Ux */
                    149:        { NULL, NULL }, /* Xc */
                    150:        { NULL, NULL }, /* Xo */
                    151:        { NULL, NULL }, /* Fo */
                    152:        { NULL, NULL }, /* Fc */
                    153:        { NULL, NULL }, /* Oo */
                    154:        { NULL, NULL }, /* Oc */
                    155:        { NULL, NULL }, /* Bk */
                    156:        { NULL, NULL }, /* Ek */
                    157:        { NULL, NULL }, /* Bt */
                    158:        { NULL, NULL }, /* Hf */
                    159:        { NULL, NULL }, /* Fr */
                    160:        { NULL, NULL }, /* Ud */
                    161:        { NULL, NULL }, /* Lb */
                    162:        { NULL, NULL }, /* Lp */
1.10      kristaps  163:        { NULL, post_lk }, /* Lk */
1.1       kristaps  164:        { NULL, NULL }, /* Mt */
                    165:        { NULL, NULL }, /* Brq */
                    166:        { NULL, NULL }, /* Bro */
                    167:        { NULL, NULL }, /* Brc */
                    168:        { NULL, NULL }, /* %C */
                    169:        { NULL, NULL }, /* Es */
                    170:        { NULL, NULL }, /* En */
                    171:        { NULL, NULL }, /* Dx */
                    172:        { NULL, NULL }, /* %Q */
                    173: };
                    174:
1.25    ! kristaps  175: static int       concat(struct mdoc *, const struct mdoc_node *,
        !           176:                        char *, size_t);
1.1       kristaps  177:
1.2       kristaps  178: #ifdef __linux__
1.25    ! kristaps  179: extern size_t    strlcat(char *, const char *, size_t);
1.2       kristaps  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
1.25    ! kristaps  293: post_at(POST_ARGS)
        !           294: {
        !           295:        struct mdoc_node *n;
        !           296:        const char       *p;
        !           297:
        !           298:        if (m->last->child) {
        !           299:                assert(MDOC_TEXT == m->last->child->type);
        !           300:                p = mdoc_a2att(m->last->child->string);
        !           301:                assert(p);
        !           302:                free(m->last->child->string);
        !           303:                m->last->child->string = strdup(p);
        !           304:                if (NULL == m->last->child->string)
        !           305:                        return(mdoc_nerr(m, m->last, EMALLOC));
        !           306:                return(1);
        !           307:        }
        !           308:
        !           309:        n = m->last;
        !           310:        m->next = MDOC_NEXT_CHILD;
        !           311:
        !           312:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "AT&T UNIX"))
        !           313:                return(0);
        !           314:
        !           315:        m->last = n;
        !           316:        m->next = MDOC_NEXT_SIBLING;
        !           317:        return(1);
        !           318: }
        !           319:
        !           320:
        !           321: static int
1.1       kristaps  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);
1.5       kristaps  335:
                    336:        buf[0] = 0;
1.2       kristaps  337:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    338:                return(0);
1.1       kristaps  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:
1.23      kristaps  354:                        return(mdoc_nwarn(m, m->last, EBADSEC));
1.1       kristaps  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)) {
1.2       kristaps  387:                if (NULL == (m->meta.title = strdup("unknown")))
1.23      kristaps  388:                        return(mdoc_nerr(m, m->last, EMALLOC));
1.2       kristaps  389:                if (NULL == (m->meta.vol = strdup("local")))
1.23      kristaps  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:
1.2       kristaps  398:        if (NULL == (m->meta.title = strdup(n->string)))
1.23      kristaps  399:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  400:
                    401:        if (NULL == (n = n->next)) {
1.2       kristaps  402:                if (NULL == (m->meta.vol = strdup("local")))
1.23      kristaps  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) {
1.2       kristaps  416:                if (NULL == (m->meta.vol = strdup(cp)))
1.23      kristaps  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;
1.2       kristaps  422:        } else if (NULL == (m->meta.vol = strdup(n->string)))
1.23      kristaps  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);
1.2       kristaps  438:                if (NULL == (m->meta.vol = strdup(cp)))
1.23      kristaps  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);
1.2       kristaps  445:                        if (NULL == (m->meta.vol = strdup(n->string)))
1.23      kristaps  446:                                return(mdoc_nerr(m, m->last, EMALLOC));
1.2       kristaps  447:                } else if (NULL == (m->meta.arch = strdup(cp)))
1.23      kristaps  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);
1.4       kristaps  465:
                    466:        buf[0] = 0;
1.2       kristaps  467:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    468:                return(0);
1.1       kristaps  469:
                    470:        if (0 == buf[0]) {
                    471:                if (-1 == uname(&utsname))
1.23      kristaps  472:                        return(mdoc_nerr(m, m->last, EUTSNAME));
1.4       kristaps  473:                if (strlcat(buf, utsname.sysname, 64) >= 64)
1.23      kristaps  474:                        return(mdoc_nerr(m, m->last, ETOOLONG));
1.2       kristaps  475:                if (strlcat(buf, " ", 64) >= 64)
1.23      kristaps  476:                        return(mdoc_nerr(m, m->last, ETOOLONG));
1.2       kristaps  477:                if (strlcat(buf, utsname.release, 64) >= 64)
1.23      kristaps  478:                        return(mdoc_nerr(m, m->last, ETOOLONG));
1.1       kristaps  479:        }
                    480:
1.2       kristaps  481:        if (NULL == (m->meta.os = strdup(buf)))
1.23      kristaps  482:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  483:
1.13      kristaps  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.11      kristaps  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)))
1.23      kristaps  517:                                if ( ! mdoc_nwarn(m, m->last, ENOWIDTH))
1.1       kristaps  518:                                        return(0);
                    519:                } else
                    520:                        sz = (int)strlen(n->string) + 1;
                    521:        }
                    522:
1.2       kristaps  523:        if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
1.23      kristaps  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);
1.2       kristaps  533:        sz = (int)(n->args->argc)++;
1.1       kristaps  534:
1.2       kristaps  535:        n->args->argv = realloc(n->args->argv,
1.1       kristaps  536:                        n->args->argc * sizeof(struct mdoc_argv));
                    537:
1.2       kristaps  538:        if (NULL == n->args->argv)
1.23      kristaps  539:                return(mdoc_nerr(m, m->last, EMALLOC));
1.2       kristaps  540:
1.1       kristaps  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 *));
1.2       kristaps  546:
1.1       kristaps  547:        if (NULL == n->args->argv[sz].value)
1.23      kristaps  548:                return(mdoc_nerr(m, m->last, EMALLOC));
1.2       kristaps  549:        if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
1.23      kristaps  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:
1.2       kristaps  580:        if (0 == strcmp(p, "Ds"))
1.18      kristaps  581:                width = 6;
1.3       kristaps  582:        else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
1.1       kristaps  583:                return(1);
                    584:        else if (0 == (width = mdoc_macro2len(tok)))
1.23      kristaps  585:                return(mdoc_nwarn(m, m->last, ENOWIDTH));
1.1       kristaps  586:
                    587:        /* The value already exists: free and reallocate it. */
                    588:
1.2       kristaps  589:        if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
1.23      kristaps  590:                return(mdoc_nerr(m, m->last, ENUMFMT));
1.1       kristaps  591:
                    592:        free(m->last->args->argv[i].value[0]);
1.2       kristaps  593:        m->last->args->argv[i].value[0] = strdup(buf);
                    594:        if (NULL == m->last->args->argv[i].value[0])
1.23      kristaps  595:                return(mdoc_nerr(m, m->last, EMALLOC));
1.1       kristaps  596:
                    597:        return(1);
                    598: }
                    599:
                    600:
                    601: static int
1.14      kristaps  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.17      kristaps  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.14      kristaps  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.17      kristaps  642:        m->last->nchild = 0;
1.14      kristaps  643:        m->last->child = NULL;
1.17      kristaps  644:
1.14      kristaps  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.14      kristaps  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:
                    683:        return(1);
                    684: }
                    685:
                    686:
                    687: static int
1.10      kristaps  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;
1.24      kristaps  697:
                    698:        /* XXX: this isn't documented anywhere! */
1.25    ! kristaps  699:        if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "~"))
1.10      kristaps  700:                return(0);
                    701:
                    702:        m->last = n;
                    703:        m->next = MDOC_NEXT_SIBLING;
                    704:        return(1);
                    705: }
                    706:
                    707:
                    708: static int
1.1       kristaps  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;
1.25    ! kristaps  718:        if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "file"))
1.1       kristaps  719:                return(0);
                    720:        m->next = MDOC_NEXT_SIBLING;
1.25    ! kristaps  721:        if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "..."))
1.1       kristaps  722:                return(0);
                    723:
                    724:        m->last = n;
                    725:        m->next = MDOC_NEXT_SIBLING;
                    726:        return(1);
                    727: }
                    728:
                    729:
                    730: static int
                    731: post_dd(POST_ARGS)
                    732: {
                    733:        char              buf[64];
                    734:
1.5       kristaps  735:        buf[0] = 0;
1.2       kristaps  736:        if ( ! concat(m, m->last->child, buf, sizeof(buf)))
                    737:                return(0);
1.1       kristaps  738:
                    739:        if (0 == (m->meta.date = mdoc_atotime(buf))) {
1.23      kristaps  740:                if ( ! mdoc_nwarn(m, m->last, EBADDATE))
1.1       kristaps  741:                        return(0);
                    742:                m->meta.date = time(NULL);
                    743:        }
                    744:
                    745:        return(post_prol(m));
                    746: }
                    747:
                    748:
                    749: static int
                    750: post_prol(POST_ARGS)
                    751: {
                    752:        struct mdoc_node *n;
                    753:
                    754:        /*
                    755:         * The end document shouldn't have the prologue macros as part
                    756:         * of the syntax tree (they encompass only meta-data).
                    757:         */
                    758:
                    759:        if (m->last->parent->child == m->last)
                    760:                m->last->parent->child = m->last->prev;
                    761:        if (m->last->prev)
                    762:                m->last->prev->next = NULL;
                    763:
                    764:        n = m->last;
                    765:        assert(NULL == m->last->next);
                    766:
                    767:        if (m->last->prev) {
                    768:                m->last = m->last->prev;
                    769:                m->next = MDOC_NEXT_SIBLING;
                    770:        } else {
                    771:                m->last = m->last->parent;
                    772:                m->next = MDOC_NEXT_CHILD;
                    773:        }
                    774:
                    775:        mdoc_node_freelist(n);
                    776:        return(1);
                    777: }
                    778:
                    779:
                    780: static int
                    781: pre_dl(PRE_ARGS)
                    782: {
                    783:
1.16      kristaps  784:        if (MDOC_BODY == n->type)
                    785:                m->flags |= MDOC_LITERAL;
1.1       kristaps  786:        return(1);
                    787: }
                    788:
                    789:
                    790: static int
                    791: pre_bd(PRE_ARGS)
                    792: {
                    793:        int              i;
                    794:
                    795:        if (MDOC_BODY != n->type)
                    796:                return(1);
                    797:
1.2       kristaps  798:        /* Enter literal context if `Bd -literal' or * -unfilled'. */
1.1       kristaps  799:
1.2       kristaps  800:        for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
1.1       kristaps  801:                if (MDOC_Literal == n->args->argv[i].arg)
                    802:                        break;
                    803:                else if (MDOC_Unfilled == n->args->argv[i].arg)
                    804:                        break;
                    805:
                    806:        if (i < (int)n->args->argc)
                    807:                m->flags |= MDOC_LITERAL;
                    808:
                    809:        return(1);
                    810: }
                    811:
                    812:
                    813: static int
                    814: post_display(POST_ARGS)
                    815: {
                    816:
                    817:        if (MDOC_BODY == m->last->type)
                    818:                m->flags &= ~MDOC_LITERAL;
                    819:        return(1);
                    820: }
                    821:
                    822:

CVSweb