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

Annotation of mandoc/mdoc_action.c, Revision 1.17

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

CVSweb