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

Annotation of mandoc/mdoc_action.c, Revision 1.22

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

CVSweb