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

Annotation of mandoc/mdoc_action.c, Revision 1.21

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

CVSweb