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

Annotation of mandoc/mdoc_action.c, Revision 1.78

1.78    ! joerg       1: /*     $Id: mdoc_action.c,v 1.77 2010/07/26 13:45:49 kristaps Exp $ */
1.1       kristaps    2: /*
1.76      schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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:  */
1.50      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.44      kristaps   21: #ifndef        OSNAME
1.1       kristaps   22: #include <sys/utsname.h>
1.44      kristaps   23: #endif
1.1       kristaps   24:
                     25: #include <assert.h>
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
1.44      kristaps   29: #include <time.h>
1.1       kristaps   30:
1.59      kristaps   31: #include "mandoc.h"
1.1       kristaps   32: #include "libmdoc.h"
1.46      kristaps   33: #include "libmandoc.h"
1.72      kristaps   34:
                     35: /*
                     36:  * FIXME: this file is deprecated.  All future "actions" should be
                     37:  * pushed into mdoc_validate.c.
                     38:  */
1.1       kristaps   39:
1.35      kristaps   40: #define        POST_ARGS struct mdoc *m, struct mdoc_node *n
1.60      kristaps   41: #define        PRE_ARGS  struct mdoc *m, struct mdoc_node *n
1.1       kristaps   42:
1.46      kristaps   43: #define        NUMSIZ    32
                     44: #define        DATESIZ   32
                     45:
1.1       kristaps   46: struct actions {
                     47:        int     (*pre)(PRE_ARGS);
                     48:        int     (*post)(POST_ARGS);
                     49: };
                     50:
1.46      kristaps   51: static int       concat(struct mdoc *, char *,
                     52:                        const struct mdoc_node *, size_t);
1.54      kristaps   53: static inline int order_rs(enum mdoct);
1.39      kristaps   54:
1.1       kristaps   55: static int       post_ar(POST_ARGS);
1.25      kristaps   56: static int       post_at(POST_ARGS);
1.1       kristaps   57: static int       post_bl(POST_ARGS);
1.14      kristaps   58: static int       post_bl_head(POST_ARGS);
1.25      kristaps   59: static int       post_bl_tagwidth(POST_ARGS);
1.70      kristaps   60: static int       post_bl_width(POST_ARGS);
1.1       kristaps   61: static int       post_dd(POST_ARGS);
                     62: static int       post_display(POST_ARGS);
                     63: static int       post_dt(POST_ARGS);
1.27      kristaps   64: static int       post_lb(POST_ARGS);
1.62      kristaps   65: static int       post_li(POST_ARGS);
1.1       kristaps   66: static int       post_nm(POST_ARGS);
                     67: static int       post_os(POST_ARGS);
1.46      kristaps   68: static int       post_pa(POST_ARGS);
1.1       kristaps   69: static int       post_prol(POST_ARGS);
1.39      kristaps   70: static int       post_rs(POST_ARGS);
1.1       kristaps   71: static int       post_sh(POST_ARGS);
1.26      kristaps   72: static int       post_st(POST_ARGS);
1.1       kristaps   73: static int       post_std(POST_ARGS);
                     74:
                     75: static int       pre_bd(PRE_ARGS);
                     76: static int       pre_dl(PRE_ARGS);
                     77:
1.37      kristaps   78: static const struct actions mdoc_actions[MDOC_MAX] = {
1.12      kristaps   79:        { NULL, NULL }, /* Ap */
1.1       kristaps   80:        { NULL, post_dd }, /* Dd */
                     81:        { NULL, post_dt }, /* Dt */
                     82:        { NULL, post_os }, /* Os */
                     83:        { NULL, post_sh }, /* Sh */
                     84:        { NULL, NULL }, /* Ss */
                     85:        { NULL, NULL }, /* Pp */
                     86:        { NULL, NULL }, /* D1 */
                     87:        { pre_dl, post_display }, /* Dl */
                     88:        { pre_bd, post_display }, /* Bd */
                     89:        { NULL, NULL }, /* Ed */
1.68      kristaps   90:        { NULL, post_bl }, /* Bl */
1.1       kristaps   91:        { NULL, NULL }, /* El */
                     92:        { NULL, NULL }, /* It */
                     93:        { NULL, NULL }, /* Ad */
                     94:        { NULL, NULL }, /* An */
                     95:        { NULL, post_ar }, /* Ar */
1.35      kristaps   96:        { NULL, NULL }, /* Cd */
1.1       kristaps   97:        { NULL, NULL }, /* Cm */
                     98:        { NULL, NULL }, /* Dv */
                     99:        { NULL, NULL }, /* Er */
                    100:        { NULL, NULL }, /* Ev */
                    101:        { NULL, post_std }, /* Ex */
                    102:        { NULL, NULL }, /* Fa */
                    103:        { NULL, NULL }, /* Fd */
                    104:        { NULL, NULL }, /* Fl */
                    105:        { NULL, NULL }, /* Fn */
                    106:        { NULL, NULL }, /* Ft */
                    107:        { NULL, NULL }, /* Ic */
                    108:        { NULL, NULL }, /* In */
1.62      kristaps  109:        { NULL, post_li }, /* Li */
1.1       kristaps  110:        { NULL, NULL }, /* Nd */
                    111:        { NULL, post_nm }, /* Nm */
                    112:        { NULL, NULL }, /* Op */
                    113:        { NULL, NULL }, /* Ot */
1.46      kristaps  114:        { NULL, post_pa }, /* Pa */
1.1       kristaps  115:        { NULL, post_std }, /* Rv */
1.26      kristaps  116:        { NULL, post_st }, /* St */
1.1       kristaps  117:        { NULL, NULL }, /* Va */
                    118:        { NULL, NULL }, /* Vt */
                    119:        { NULL, NULL }, /* Xr */
                    120:        { NULL, NULL }, /* %A */
                    121:        { NULL, NULL }, /* %B */
                    122:        { NULL, NULL }, /* %D */
                    123:        { NULL, NULL }, /* %I */
                    124:        { NULL, NULL }, /* %J */
                    125:        { NULL, NULL }, /* %N */
                    126:        { NULL, NULL }, /* %O */
                    127:        { NULL, NULL }, /* %P */
                    128:        { NULL, NULL }, /* %R */
                    129:        { NULL, NULL }, /* %T */
                    130:        { NULL, NULL }, /* %V */
                    131:        { NULL, NULL }, /* Ac */
                    132:        { NULL, NULL }, /* Ao */
                    133:        { NULL, NULL }, /* Aq */
1.25      kristaps  134:        { NULL, post_at }, /* At */
1.1       kristaps  135:        { NULL, NULL }, /* Bc */
                    136:        { NULL, NULL }, /* Bf */
                    137:        { NULL, NULL }, /* Bo */
                    138:        { NULL, NULL }, /* Bq */
                    139:        { NULL, NULL }, /* Bsx */
                    140:        { NULL, NULL }, /* Bx */
                    141:        { NULL, NULL }, /* Db */
                    142:        { NULL, NULL }, /* Dc */
                    143:        { NULL, NULL }, /* Do */
                    144:        { NULL, NULL }, /* Dq */
                    145:        { NULL, NULL }, /* Ec */
                    146:        { NULL, NULL }, /* Ef */
                    147:        { NULL, NULL }, /* Em */
                    148:        { NULL, NULL }, /* Eo */
                    149:        { NULL, NULL }, /* Fx */
                    150:        { NULL, NULL }, /* Ms */
                    151:        { NULL, NULL }, /* No */
                    152:        { NULL, NULL }, /* Ns */
                    153:        { NULL, NULL }, /* Nx */
                    154:        { NULL, NULL }, /* Ox */
                    155:        { NULL, NULL }, /* Pc */
                    156:        { NULL, NULL }, /* Pf */
                    157:        { NULL, NULL }, /* Po */
                    158:        { NULL, NULL }, /* Pq */
                    159:        { NULL, NULL }, /* Qc */
                    160:        { NULL, NULL }, /* Ql */
                    161:        { NULL, NULL }, /* Qo */
                    162:        { NULL, NULL }, /* Qq */
                    163:        { NULL, NULL }, /* Re */
1.39      kristaps  164:        { NULL, post_rs }, /* Rs */
1.1       kristaps  165:        { NULL, NULL }, /* Sc */
                    166:        { NULL, NULL }, /* So */
                    167:        { NULL, NULL }, /* Sq */
                    168:        { NULL, NULL }, /* Sm */
                    169:        { NULL, NULL }, /* Sx */
                    170:        { NULL, NULL }, /* Sy */
                    171:        { NULL, NULL }, /* Tn */
                    172:        { NULL, NULL }, /* Ux */
                    173:        { NULL, NULL }, /* Xc */
                    174:        { NULL, NULL }, /* Xo */
                    175:        { NULL, NULL }, /* Fo */
                    176:        { NULL, NULL }, /* Fc */
                    177:        { NULL, NULL }, /* Oo */
                    178:        { NULL, NULL }, /* Oc */
                    179:        { NULL, NULL }, /* Bk */
                    180:        { NULL, NULL }, /* Ek */
                    181:        { NULL, NULL }, /* Bt */
                    182:        { NULL, NULL }, /* Hf */
                    183:        { NULL, NULL }, /* Fr */
                    184:        { NULL, NULL }, /* Ud */
1.27      kristaps  185:        { NULL, post_lb }, /* Lb */
1.1       kristaps  186:        { NULL, NULL }, /* Lp */
1.44      kristaps  187:        { NULL, NULL }, /* Lk */
1.1       kristaps  188:        { NULL, NULL }, /* Mt */
                    189:        { NULL, NULL }, /* Brq */
                    190:        { NULL, NULL }, /* Bro */
                    191:        { NULL, NULL }, /* Brc */
                    192:        { NULL, NULL }, /* %C */
                    193:        { NULL, NULL }, /* Es */
                    194:        { NULL, NULL }, /* En */
                    195:        { NULL, NULL }, /* Dx */
                    196:        { NULL, NULL }, /* %Q */
1.30      kristaps  197:        { NULL, NULL }, /* br */
                    198:        { NULL, NULL }, /* sp */
1.43      kristaps  199:        { NULL, NULL }, /* %U */
1.64      kristaps  200:        { NULL, NULL }, /* Ta */
1.1       kristaps  201: };
                    202:
1.45      kristaps  203: #define        RSORD_MAX 14
1.1       kristaps  204:
1.54      kristaps  205: static const enum mdoct rsord[RSORD_MAX] = {
1.39      kristaps  206:        MDOC__A,
                    207:        MDOC__T,
                    208:        MDOC__B,
                    209:        MDOC__I,
                    210:        MDOC__J,
                    211:        MDOC__R,
                    212:        MDOC__N,
                    213:        MDOC__V,
                    214:        MDOC__P,
                    215:        MDOC__Q,
                    216:        MDOC__D,
                    217:        MDOC__O,
1.45      kristaps  218:        MDOC__C,
                    219:        MDOC__U
1.39      kristaps  220: };
1.2       kristaps  221:
                    222:
1.1       kristaps  223: int
1.60      kristaps  224: mdoc_action_pre(struct mdoc *m, struct mdoc_node *n)
1.1       kristaps  225: {
                    226:
                    227:        switch (n->type) {
                    228:        case (MDOC_ROOT):
1.2       kristaps  229:                /* FALLTHROUGH */
1.1       kristaps  230:        case (MDOC_TEXT):
1.2       kristaps  231:                return(1);
                    232:        default:
1.1       kristaps  233:                break;
                    234:        }
1.2       kristaps  235:
1.6       kristaps  236:        if (NULL == mdoc_actions[n->tok].pre)
1.2       kristaps  237:                return(1);
1.6       kristaps  238:        return((*mdoc_actions[n->tok].pre)(m, n));
1.1       kristaps  239: }
                    240:
                    241:
                    242: int
                    243: mdoc_action_post(struct mdoc *m)
                    244: {
                    245:
                    246:        if (MDOC_ACTED & m->last->flags)
                    247:                return(1);
                    248:        m->last->flags |= MDOC_ACTED;
                    249:
                    250:        switch (m->last->type) {
                    251:        case (MDOC_TEXT):
1.2       kristaps  252:                /* FALLTHROUGH */
1.1       kristaps  253:        case (MDOC_ROOT):
1.2       kristaps  254:                return(1);
                    255:        default:
1.1       kristaps  256:                break;
                    257:        }
1.2       kristaps  258:
                    259:        if (NULL == mdoc_actions[m->last->tok].post)
                    260:                return(1);
1.35      kristaps  261:        return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.2       kristaps  262: }
                    263:
                    264:
1.46      kristaps  265: /*
                    266:  * Concatenate sibling nodes together.  All siblings must be of type
                    267:  * MDOC_TEXT or an assertion is raised.  Concatenation is separated by a
                    268:  * single whitespace.
                    269:  */
1.2       kristaps  270: static int
1.46      kristaps  271: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
1.2       kristaps  272: {
                    273:
1.46      kristaps  274:        assert(sz);
                    275:        p[0] = '\0';
1.2       kristaps  276:        for ( ; n; n = n->next) {
                    277:                assert(MDOC_TEXT == n->type);
1.59      kristaps  278:                /*
                    279:                 * XXX: yes, these can technically be resized, but it's
                    280:                 * highly unlikely that we're going to get here, so let
                    281:                 * it slip for now.
                    282:                 */
                    283:                if (strlcat(p, n->string, sz) >= sz) {
                    284:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    285:                        return(0);
                    286:                }
1.2       kristaps  287:                if (NULL == n->next)
                    288:                        continue;
1.59      kristaps  289:                if (strlcat(p, " ", sz) >= sz) {
                    290:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    291:                        return(0);
                    292:                }
1.2       kristaps  293:        }
                    294:
1.1       kristaps  295:        return(1);
                    296: }
                    297:
                    298:
1.46      kristaps  299: /*
                    300:  * Macros accepting `-std' as an argument have the name of the current
                    301:  * document (`Nm') filled in as the argument if it's not provided.
                    302:  */
1.1       kristaps  303: static int
                    304: post_std(POST_ARGS)
                    305: {
1.59      kristaps  306:        struct mdoc_node *nn;
1.1       kristaps  307:
1.35      kristaps  308:        if (n->child)
1.1       kristaps  309:                return(1);
1.59      kristaps  310:        if (NULL == m->meta.name)
                    311:                return(1);
1.35      kristaps  312:
                    313:        nn = n;
                    314:        m->next = MDOC_NEXT_CHILD;
1.59      kristaps  315:
1.35      kristaps  316:        if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
                    317:                return(0);
                    318:        m->last = nn;
1.1       kristaps  319:        return(1);
                    320: }
                    321:
                    322:
1.46      kristaps  323: /*
                    324:  * The `Nm' macro's first use sets the name of the document.  See also
                    325:  * post_std(), etc.
                    326:  */
1.1       kristaps  327: static int
                    328: post_nm(POST_ARGS)
                    329: {
1.46      kristaps  330:        char             buf[BUFSIZ];
1.1       kristaps  331:
                    332:        if (m->meta.name)
                    333:                return(1);
1.46      kristaps  334:        if ( ! concat(m, buf, n->child, BUFSIZ))
1.2       kristaps  335:                return(0);
1.46      kristaps  336:        m->meta.name = mandoc_strdup(buf);
1.1       kristaps  337:        return(1);
                    338: }
                    339:
                    340:
1.46      kristaps  341: /*
                    342:  * Look up the value of `Lb' for matching predefined strings.  If it has
                    343:  * one, then substitute the current value for the formatted value.  Note
                    344:  * that the lookup may fail (we can provide arbitrary strings).
                    345:  */
                    346: /* ARGSUSED */
1.1       kristaps  347: static int
1.27      kristaps  348: post_lb(POST_ARGS)
                    349: {
                    350:        const char      *p;
                    351:        char            *buf;
                    352:        size_t           sz;
                    353:
1.35      kristaps  354:        assert(MDOC_TEXT == n->child->type);
                    355:        p = mdoc_a2lib(n->child->string);
1.46      kristaps  356:
                    357:        if (p) {
1.35      kristaps  358:                free(n->child->string);
1.46      kristaps  359:                n->child->string = mandoc_strdup(p);
1.27      kristaps  360:                return(1);
                    361:        }
                    362:
1.46      kristaps  363:        sz = strlen(n->child->string) +
                    364:                2 + strlen("\\(lqlibrary\\(rq");
                    365:        buf = mandoc_malloc(sz);
                    366:        snprintf(buf, sz, "library \\(lq%s\\(rq", n->child->string);
1.35      kristaps  367:        free(n->child->string);
1.46      kristaps  368:        n->child->string = buf;
1.27      kristaps  369:        return(1);
                    370: }
                    371:
                    372:
1.46      kristaps  373: /*
                    374:  * Substitute the value of `St' for the corresponding formatted string.
                    375:  * We're guaranteed that this exists (it's been verified during the
                    376:  * validation phase).
                    377:  */
                    378: /* ARGSUSED */
1.27      kristaps  379: static int
1.26      kristaps  380: post_st(POST_ARGS)
                    381: {
                    382:        const char      *p;
                    383:
1.35      kristaps  384:        assert(MDOC_TEXT == n->child->type);
                    385:        p = mdoc_a2st(n->child->string);
1.56      kristaps  386:        if (p != NULL) {
                    387:                free(n->child->string);
                    388:                n->child->string = mandoc_strdup(p);
                    389:        }
1.26      kristaps  390:        return(1);
                    391: }
                    392:
                    393:
1.46      kristaps  394: /*
                    395:  * Look up the standard string in a table.  We know that it exists from
                    396:  * the validation phase, so assert on failure.  If a standard key wasn't
                    397:  * supplied, supply the default ``AT&T UNIX''.
                    398:  */
1.26      kristaps  399: static int
1.25      kristaps  400: post_at(POST_ARGS)
                    401: {
1.57      kristaps  402:        struct mdoc_node *nn;
                    403:        const char       *p, *q;
                    404:        char             *buf;
                    405:        size_t            sz;
1.25      kristaps  406:
1.35      kristaps  407:        if (n->child) {
                    408:                assert(MDOC_TEXT == n->child->type);
                    409:                p = mdoc_a2att(n->child->string);
1.57      kristaps  410:                if (p) {
                    411:                        free(n->child->string);
                    412:                        n->child->string = mandoc_strdup(p);
                    413:                } else {
                    414:                        p = "AT&T UNIX ";
                    415:                        q = n->child->string;
                    416:                        sz = strlen(p) + strlen(q) + 1;
                    417:                        buf = mandoc_malloc(sz);
                    418:                        strlcpy(buf, p, sz);
                    419:                        strlcat(buf, q, sz);
                    420:                        free(n->child->string);
                    421:                        n->child->string = buf;
                    422:                }
1.25      kristaps  423:                return(1);
                    424:        }
                    425:
1.35      kristaps  426:        nn = n;
1.25      kristaps  427:        m->next = MDOC_NEXT_CHILD;
1.35      kristaps  428:        if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
1.25      kristaps  429:                return(0);
1.35      kristaps  430:        m->last = nn;
1.25      kristaps  431:        return(1);
                    432: }
                    433:
                    434:
1.46      kristaps  435: /*
                    436:  * Mark the current section.  The ``named'' section (lastnamed) is set
                    437:  * whenever the current section isn't a custom section--we use this to
                    438:  * keep track of section ordering.  Also check that the section is
                    439:  * allowed within the document's manual section.
                    440:  */
1.25      kristaps  441: static int
1.1       kristaps  442: post_sh(POST_ARGS)
                    443: {
                    444:        enum mdoc_sec    sec;
1.46      kristaps  445:        char             buf[BUFSIZ];
1.1       kristaps  446:
1.35      kristaps  447:        if (MDOC_HEAD != n->type)
1.1       kristaps  448:                return(1);
1.5       kristaps  449:
1.46      kristaps  450:        if ( ! concat(m, buf, n->child, BUFSIZ))
1.2       kristaps  451:                return(0);
1.55      kristaps  452:        sec = mdoc_str2sec(buf);
1.52      kristaps  453:        /*
                    454:         * The first section should always make us move into a non-new
                    455:         * state.
                    456:         */
                    457:        if (SEC_NONE == m->lastnamed || SEC_CUSTOM != sec)
1.1       kristaps  458:                m->lastnamed = sec;
1.73      schwarze  459:
                    460:        /*
                    461:         * Switch the parser's SYNOPSIS mode, to be copied
                    462:         * into individual nodes when creating them.
                    463:         * Note that this mode can also be set and unset
                    464:         * using the roff nS register.
                    465:         */
                    466:        if (SEC_SYNOPSIS == sec)
                    467:                m->flags |= MDOC_SYNOPSIS;
                    468:        else
                    469:                m->flags &= ~MDOC_SYNOPSIS;
1.1       kristaps  470:
1.46      kristaps  471:        /* Some sections only live in certain manual sections. */
                    472:
1.1       kristaps  473:        switch ((m->lastsec = sec)) {
                    474:        case (SEC_RETURN_VALUES):
                    475:                /* FALLTHROUGH */
                    476:        case (SEC_ERRORS):
1.58      kristaps  477:                assert(m->meta.msec);
                    478:                if (*m->meta.msec == '2')
1.1       kristaps  479:                        break;
1.58      kristaps  480:                if (*m->meta.msec == '3')
                    481:                        break;
                    482:                if (*m->meta.msec == '9')
                    483:                        break;
1.59      kristaps  484:                return(mdoc_nmsg(m, n, MANDOCERR_SECMSEC));
1.1       kristaps  485:        default:
                    486:                break;
                    487:        }
                    488:        return(1);
                    489: }
                    490:
                    491:
1.46      kristaps  492: /*
                    493:  * Parse out the contents of `Dt'.  See in-line documentation for how we
                    494:  * handle the various fields of this macro.
                    495:  */
1.1       kristaps  496: static int
                    497: post_dt(POST_ARGS)
                    498: {
1.35      kristaps  499:        struct mdoc_node *nn;
1.1       kristaps  500:        const char       *cp;
                    501:
                    502:        if (m->meta.title)
                    503:                free(m->meta.title);
                    504:        if (m->meta.vol)
                    505:                free(m->meta.vol);
                    506:        if (m->meta.arch)
                    507:                free(m->meta.arch);
                    508:
                    509:        m->meta.title = m->meta.vol = m->meta.arch = NULL;
                    510:        /* Handles: `.Dt'
                    511:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                    512:         */
                    513:
1.35      kristaps  514:        if (NULL == (nn = n->child)) {
1.46      kristaps  515:                /* XXX: make these macro values. */
1.58      kristaps  516:                /* FIXME: warn about missing values. */
1.63      kristaps  517:                m->meta.title = mandoc_strdup("UNKNOWN");
                    518:                m->meta.vol = mandoc_strdup("LOCAL");
1.58      kristaps  519:                m->meta.msec = mandoc_strdup("1");
1.35      kristaps  520:                return(post_prol(m, n));
1.1       kristaps  521:        }
                    522:
                    523:        /* Handles: `.Dt TITLE'
                    524:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                    525:         */
                    526:
1.63      kristaps  527:        m->meta.title = mandoc_strdup
                    528:                ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
1.1       kristaps  529:
1.35      kristaps  530:        if (NULL == (nn = nn->next)) {
1.58      kristaps  531:                /* FIXME: warn about missing msec. */
1.46      kristaps  532:                /* XXX: make this a macro value. */
1.63      kristaps  533:                m->meta.vol = mandoc_strdup("LOCAL");
1.58      kristaps  534:                m->meta.msec = mandoc_strdup("1");
1.35      kristaps  535:                return(post_prol(m, n));
1.1       kristaps  536:        }
                    537:
                    538:        /* Handles: `.Dt TITLE SEC'
                    539:         *   --> title = TITLE, volume = SEC is msec ?
                    540:         *           format(msec) : SEC,
                    541:         *       msec = SEC is msec ? atoi(msec) : 0,
                    542:         *       arch = NULL
                    543:         */
                    544:
1.35      kristaps  545:        cp = mdoc_a2msec(nn->string);
1.1       kristaps  546:        if (cp) {
1.46      kristaps  547:                m->meta.vol = mandoc_strdup(cp);
1.58      kristaps  548:                m->meta.msec = mandoc_strdup(nn->string);
1.59      kristaps  549:        } else if (mdoc_nmsg(m, n, MANDOCERR_BADMSEC)) {
1.46      kristaps  550:                m->meta.vol = mandoc_strdup(nn->string);
1.58      kristaps  551:                m->meta.msec = mandoc_strdup(nn->string);
                    552:        } else
                    553:                return(0);
1.1       kristaps  554:
1.35      kristaps  555:        if (NULL == (nn = nn->next))
                    556:                return(post_prol(m, n));
1.1       kristaps  557:
                    558:        /* Handles: `.Dt TITLE SEC VOL'
                    559:         *   --> title = TITLE, volume = VOL is vol ?
                    560:         *       format(VOL) :
                    561:         *           VOL is arch ? format(arch) :
                    562:         *               VOL
                    563:         */
                    564:
1.35      kristaps  565:        cp = mdoc_a2vol(nn->string);
1.1       kristaps  566:        if (cp) {
                    567:                free(m->meta.vol);
1.46      kristaps  568:                m->meta.vol = mandoc_strdup(cp);
1.1       kristaps  569:        } else {
1.58      kristaps  570:                /* FIXME: warn about bad arch. */
1.35      kristaps  571:                cp = mdoc_a2arch(nn->string);
1.1       kristaps  572:                if (NULL == cp) {
                    573:                        free(m->meta.vol);
1.46      kristaps  574:                        m->meta.vol = mandoc_strdup(nn->string);
                    575:                } else
                    576:                        m->meta.arch = mandoc_strdup(cp);
1.1       kristaps  577:        }
                    578:
                    579:        /* Ignore any subsequent parameters... */
1.46      kristaps  580:        /* FIXME: warn about subsequent parameters. */
1.1       kristaps  581:
1.35      kristaps  582:        return(post_prol(m, n));
1.1       kristaps  583: }
                    584:
                    585:
1.46      kristaps  586: /*
                    587:  * Set the operating system by way of the `Os' macro.  Note that if an
                    588:  * argument isn't provided and -DOSNAME="\"foo\"" is provided during
                    589:  * compilation, this value will be used instead of filling in "sysname
                    590:  * release" from uname().
                    591:  */
1.1       kristaps  592: static int
                    593: post_os(POST_ARGS)
                    594: {
1.46      kristaps  595:        char              buf[BUFSIZ];
                    596: #ifndef OSNAME
1.1       kristaps  597:        struct utsname    utsname;
1.42      kristaps  598: #endif
                    599:
1.1       kristaps  600:        if (m->meta.os)
                    601:                free(m->meta.os);
1.4       kristaps  602:
1.46      kristaps  603:        if ( ! concat(m, buf, n->child, BUFSIZ))
1.2       kristaps  604:                return(0);
1.1       kristaps  605:
1.59      kristaps  606:        /* XXX: yes, these can all be dynamically-adjusted buffers, but
                    607:         * it's really not worth the extra hackery.
                    608:         */
                    609:
1.46      kristaps  610:        if ('\0' == buf[0]) {
                    611: #ifdef OSNAME
1.59      kristaps  612:                if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
                    613:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    614:                        return(0);
                    615:                }
1.46      kristaps  616: #else /*!OSNAME */
1.1       kristaps  617:                if (-1 == uname(&utsname))
1.59      kristaps  618:                        return(mdoc_nmsg(m, n, MANDOCERR_UTSNAME));
                    619:
                    620:                if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
                    621:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    622:                        return(0);
                    623:                }
                    624:                if (strlcat(buf, " ", 64) >= BUFSIZ) {
                    625:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    626:                        return(0);
                    627:                }
                    628:                if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
                    629:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    630:                        return(0);
                    631:                }
1.46      kristaps  632: #endif /*!OSNAME*/
1.1       kristaps  633:        }
                    634:
1.46      kristaps  635:        m->meta.os = mandoc_strdup(buf);
1.35      kristaps  636:        return(post_prol(m, n));
1.1       kristaps  637: }
                    638:
                    639:
                    640: /*
                    641:  * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
1.46      kristaps  642:  * Uses the first head macro.  NOTE AGAIN: this is ONLY if the -width
                    643:  * argument has NOT been provided.  See post_bl_width() for converting
                    644:  * the -width string.
1.1       kristaps  645:  */
                    646: static int
1.35      kristaps  647: post_bl_tagwidth(POST_ARGS)
1.1       kristaps  648: {
1.46      kristaps  649:        struct mdoc_node *nn;
1.69      kristaps  650:        size_t            sz, ssz;
1.46      kristaps  651:        int               i;
                    652:        char              buf[NUMSIZ];
1.1       kristaps  653:
1.69      kristaps  654:        sz = 10;
1.61      joerg     655:
                    656:        for (nn = n->body->child; nn; nn = nn->next) {
1.69      kristaps  657:                if (MDOC_It != nn->tok)
                    658:                        continue;
1.46      kristaps  659:
1.35      kristaps  660:                assert(MDOC_BLOCK == nn->type);
                    661:                nn = nn->head->child;
1.78    ! joerg     662:
        !           663:                if (nn == NULL) {
        !           664:                        /* No -width for .Bl and first .It is emtpy */
        !           665:                        if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
        !           666:                                return(0);
        !           667:                        break;
        !           668:                }
1.69      kristaps  669:
                    670:                if (MDOC_TEXT == nn->type) {
1.46      kristaps  671:                        sz = strlen(nn->string) + 1;
1.69      kristaps  672:                        break;
                    673:                }
                    674:
                    675:                if (0 != (ssz = mdoc_macro2len(nn->tok)))
                    676:                        sz = ssz;
                    677:                else if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
                    678:                        return(0);
                    679:
                    680:                break;
1.1       kristaps  681:        }
                    682:
1.69      kristaps  683:        /* Defaults to ten ens. */
                    684:
1.46      kristaps  685:        snprintf(buf, NUMSIZ, "%zun", sz);
1.1       kristaps  686:
                    687:        /*
                    688:         * We have to dynamically add this to the macro's argument list.
                    689:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    690:         */
                    691:
1.70      kristaps  692:        assert(n->args);
                    693:        i = (int)(n->args->argc)++;
                    694:
                    695:        n->args->argv = mandoc_realloc(n->args->argv,
                    696:                        n->args->argc * sizeof(struct mdoc_argv));
1.1       kristaps  697:
1.70      kristaps  698:        n->args->argv[i].arg = MDOC_Width;
                    699:        n->args->argv[i].line = n->line;
                    700:        n->args->argv[i].pos = n->pos;
                    701:        n->args->argv[i].sz = 1;
                    702:        n->args->argv[i].value = mandoc_malloc(sizeof(char *));
                    703:        n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1       kristaps  704:
1.70      kristaps  705:        /* Set our width! */
1.74      kristaps  706:        n->data.Bl->width = n->args->argv[i].value[0];
1.1       kristaps  707:        return(1);
                    708: }
                    709:
                    710:
1.46      kristaps  711: /*
                    712:  * Calculate the real width of a list from the -width string, which may
                    713:  * contain a macro (with a known default width), a literal string, or a
                    714:  * scaling width.
                    715:  */
1.1       kristaps  716: static int
1.70      kristaps  717: post_bl_width(POST_ARGS)
1.1       kristaps  718: {
                    719:        size_t            width;
1.70      kristaps  720:        int               i;
1.51      kristaps  721:        enum mdoct        tok;
1.46      kristaps  722:        char              buf[NUMSIZ];
1.1       kristaps  723:
                    724:        /*
                    725:         * If the value to -width is a macro, then we re-write it to be
                    726:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    727:         */
                    728:
1.74      kristaps  729:        if (0 == strcmp(n->data.Bl->width, "Ds"))
1.18      kristaps  730:                width = 6;
1.74      kristaps  731:        else if (MDOC_MAX == (tok = mdoc_hash_find(n->data.Bl->width)))
1.1       kristaps  732:                return(1);
                    733:        else if (0 == (width = mdoc_macro2len(tok)))
1.59      kristaps  734:                return(mdoc_nmsg(m, n, MANDOCERR_BADWIDTH));
1.1       kristaps  735:
                    736:        /* The value already exists: free and reallocate it. */
                    737:
1.70      kristaps  738:        assert(n->args);
                    739:
                    740:        for (i = 0; i < (int)n->args->argc; i++)
                    741:                if (MDOC_Width == n->args->argv[i].arg)
                    742:                        break;
                    743:
                    744:        assert(i < (int)n->args->argc);
                    745:
1.46      kristaps  746:        snprintf(buf, NUMSIZ, "%zun", width);
1.70      kristaps  747:        free(n->args->argv[i].value[0]);
                    748:        n->args->argv[i].value[0] = mandoc_strdup(buf);
                    749:
                    750:        /* Set our width! */
1.74      kristaps  751:        n->data.Bl->width = n->args->argv[i].value[0];
1.1       kristaps  752:        return(1);
                    753: }
                    754:
                    755:
1.46      kristaps  756: /*
                    757:  * Do processing for -column lists, which can have two distinct styles
                    758:  * of invocation.  Merge this two styles into a consistent form.
                    759:  */
1.35      kristaps  760: /* ARGSUSED */
1.1       kristaps  761: static int
1.14      kristaps  762: post_bl_head(POST_ARGS)
                    763: {
                    764:        int                      i, c;
1.35      kristaps  765:        struct mdoc_node        *np, *nn, *nnp;
1.14      kristaps  766:
1.74      kristaps  767:        if (LIST_column != n->data.Bl->type)
1.69      kristaps  768:                return(1);
                    769:        else if (NULL == n->child)
1.14      kristaps  770:                return(1);
                    771:
1.35      kristaps  772:        np = n->parent;
                    773:        assert(np->args);
1.14      kristaps  774:
1.35      kristaps  775:        for (c = 0; c < (int)np->args->argc; c++)
                    776:                if (MDOC_Column == np->args->argv[c].arg)
1.14      kristaps  777:                        break;
                    778:
1.69      kristaps  779:        assert(c < (int)np->args->argc);
1.35      kristaps  780:        assert(0 == np->args->argv[c].sz);
1.14      kristaps  781:
                    782:        /*
                    783:         * Accomodate for new-style groff column syntax.  Shuffle the
                    784:         * child nodes, all of which must be TEXT, as arguments for the
                    785:         * column field.  Then, delete the head children.
                    786:         */
                    787:
1.35      kristaps  788:        np->args->argv[c].sz = (size_t)n->nchild;
1.46      kristaps  789:        np->args->argv[c].value = mandoc_malloc
1.35      kristaps  790:                ((size_t)n->nchild * sizeof(char *));
1.75      kristaps  791:
                    792:        n->data.Bl->ncols = np->args->argv[c].sz;
                    793:        n->data.Bl->cols = (const char **)np->args->argv[c].value;
1.14      kristaps  794:
1.35      kristaps  795:        for (i = 0, nn = n->child; nn; i++) {
                    796:                np->args->argv[c].value[i] = nn->string;
1.14      kristaps  797:                nn->string = NULL;
                    798:                nnp = nn;
                    799:                nn = nn->next;
1.53      kristaps  800:                mdoc_node_delete(NULL, nnp);
1.14      kristaps  801:        }
                    802:
1.35      kristaps  803:        n->nchild = 0;
                    804:        n->child = NULL;
1.14      kristaps  805:        return(1);
                    806: }
                    807:
                    808:
                    809: static int
1.1       kristaps  810: post_bl(POST_ARGS)
                    811: {
                    812:
1.35      kristaps  813:        if (MDOC_HEAD == n->type)
                    814:                return(post_bl_head(m, n));
                    815:        if (MDOC_BLOCK != n->type)
1.1       kristaps  816:                return(1);
                    817:
                    818:        /*
                    819:         * These are fairly complicated, so we've broken them into two
                    820:         * functions.  post_bl_tagwidth() is called when a -tag is
                    821:         * specified, but no -width (it must be guessed).  The second
                    822:         * when a -width is specified (macro indicators must be
                    823:         * rewritten into real lengths).
                    824:         */
                    825:
1.74      kristaps  826:        if (LIST_tag == n->data.Bl->type && NULL == n->data.Bl->width) {
1.35      kristaps  827:                if ( ! post_bl_tagwidth(m, n))
1.1       kristaps  828:                        return(0);
1.74      kristaps  829:        } else if (NULL != n->data.Bl->width) {
1.70      kristaps  830:                if ( ! post_bl_width(m, n))
1.1       kristaps  831:                        return(0);
1.70      kristaps  832:        } else
                    833:                return(1);
                    834:
1.74      kristaps  835:        assert(n->data.Bl->width);
1.1       kristaps  836:        return(1);
                    837: }
                    838:
                    839:
1.46      kristaps  840: /*
                    841:  * The `Pa' macro defaults to a tilde if no value is provided as an
                    842:  * argument.
                    843:  */
1.1       kristaps  844: static int
1.46      kristaps  845: post_pa(POST_ARGS)
1.10      kristaps  846: {
1.35      kristaps  847:        struct mdoc_node *np;
1.10      kristaps  848:
1.35      kristaps  849:        if (n->child)
1.10      kristaps  850:                return(1);
                    851:
1.35      kristaps  852:        np = n;
1.10      kristaps  853:        m->next = MDOC_NEXT_CHILD;
1.35      kristaps  854:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.62      kristaps  855:                return(0);
                    856:        m->last = np;
                    857:        return(1);
                    858: }
                    859:
                    860:
                    861: /*
                    862:  * Empty `Li' macros get an empty string to make front-ends add an extra
                    863:  * space.
                    864:  */
                    865: static int
                    866: post_li(POST_ARGS)
                    867: {
                    868:        struct mdoc_node *np;
                    869:
                    870:        if (n->child)
                    871:                return(1);
                    872:
                    873:        np = n;
                    874:        m->next = MDOC_NEXT_CHILD;
                    875:        if ( ! mdoc_word_alloc(m, n->line, n->pos, ""))
1.10      kristaps  876:                return(0);
1.35      kristaps  877:        m->last = np;
1.10      kristaps  878:        return(1);
                    879: }
                    880:
                    881:
1.46      kristaps  882: /*
                    883:  * The `Ar' macro defaults to two strings "file ..." if no value is
                    884:  * provided as an argument.
                    885:  */
1.10      kristaps  886: static int
1.1       kristaps  887: post_ar(POST_ARGS)
                    888: {
1.35      kristaps  889:        struct mdoc_node *np;
1.1       kristaps  890:
1.35      kristaps  891:        if (n->child)
1.1       kristaps  892:                return(1);
                    893:
1.35      kristaps  894:        np = n;
1.1       kristaps  895:        m->next = MDOC_NEXT_CHILD;
1.46      kristaps  896:        /* XXX: make into macro values. */
1.35      kristaps  897:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1       kristaps  898:                return(0);
1.35      kristaps  899:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1       kristaps  900:                return(0);
1.35      kristaps  901:        m->last = np;
1.1       kristaps  902:        return(1);
                    903: }
                    904:
                    905:
1.46      kristaps  906: /*
1.49      kristaps  907:  * Parse the date field in `Dd'.
1.46      kristaps  908:  */
1.1       kristaps  909: static int
                    910: post_dd(POST_ARGS)
                    911: {
1.46      kristaps  912:        char            buf[DATESIZ];
1.77      kristaps  913:
                    914:        if (NULL == n->child) {
                    915:                m->meta.date = time(NULL);
                    916:                return(post_prol(m, n));
                    917:        }
1.1       kristaps  918:
1.46      kristaps  919:        if ( ! concat(m, buf, n->child, DATESIZ))
1.2       kristaps  920:                return(0);
1.1       kristaps  921:
1.49      kristaps  922:        m->meta.date = mandoc_a2time
                    923:                (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
                    924:
1.46      kristaps  925:        if (0 == m->meta.date) {
1.59      kristaps  926:                if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
1.1       kristaps  927:                        return(0);
                    928:                m->meta.date = time(NULL);
                    929:        }
                    930:
1.35      kristaps  931:        return(post_prol(m, n));
1.1       kristaps  932: }
                    933:
                    934:
1.46      kristaps  935: /*
                    936:  * Remove prologue macros from the document after they're processed.
                    937:  * The final document uses mdoc_meta for these values and discards the
                    938:  * originals.
                    939:  */
1.1       kristaps  940: static int
                    941: post_prol(POST_ARGS)
                    942: {
1.34      kristaps  943:
1.53      kristaps  944:        mdoc_node_delete(m, n);
1.34      kristaps  945:        if (m->meta.title && m->meta.date && m->meta.os)
                    946:                m->flags |= MDOC_PBODY;
1.1       kristaps  947:        return(1);
                    948: }
                    949:
                    950:
1.46      kristaps  951: /*
                    952:  * Trigger a literal context.
                    953:  */
1.1       kristaps  954: static int
                    955: pre_dl(PRE_ARGS)
                    956: {
                    957:
1.16      kristaps  958:        if (MDOC_BODY == n->type)
                    959:                m->flags |= MDOC_LITERAL;
1.65      kristaps  960:        return(1);
1.38      kristaps  961: }
                    962:
                    963:
                    964: static int
1.1       kristaps  965: pre_bd(PRE_ARGS)
                    966: {
                    967:
                    968:        if (MDOC_BODY != n->type)
                    969:                return(1);
                    970:
1.74      kristaps  971:        assert(n->data.Bd);
                    972:        if (DISP_literal == n->data.Bd->type)
1.66      kristaps  973:                m->flags |= MDOC_LITERAL;
1.74      kristaps  974:        if (DISP_unfilled == n->data.Bd->type)
1.66      kristaps  975:                m->flags |= MDOC_LITERAL;
1.1       kristaps  976:
                    977:        return(1);
                    978: }
                    979:
                    980:
                    981: static int
                    982: post_display(POST_ARGS)
                    983: {
                    984:
1.35      kristaps  985:        if (MDOC_BODY == n->type)
1.1       kristaps  986:                m->flags &= ~MDOC_LITERAL;
                    987:        return(1);
                    988: }
                    989:
                    990:
1.39      kristaps  991: static inline int
1.54      kristaps  992: order_rs(enum mdoct t)
1.39      kristaps  993: {
                    994:        int             i;
                    995:
1.54      kristaps  996:        for (i = 0; i < (int)RSORD_MAX; i++)
1.39      kristaps  997:                if (rsord[i] == t)
                    998:                        return(i);
                    999:
                   1000:        abort();
                   1001:        /* NOTREACHED */
                   1002: }
                   1003:
                   1004:
1.41      kristaps 1005: /* ARGSUSED */
1.39      kristaps 1006: static int
                   1007: post_rs(POST_ARGS)
                   1008: {
                   1009:        struct mdoc_node        *nn, *next, *prev;
                   1010:        int                      o;
                   1011:
                   1012:        if (MDOC_BLOCK != n->type)
                   1013:                return(1);
                   1014:
1.40      kristaps 1015:        assert(n->body->child);
1.39      kristaps 1016:        for (next = NULL, nn = n->body->child->next; nn; nn = next) {
                   1017:                o = order_rs(nn->tok);
                   1018:
                   1019:                /* Remove `nn' from the chain. */
                   1020:                next = nn->next;
                   1021:                if (next)
                   1022:                        next->prev = nn->prev;
                   1023:
                   1024:                prev = nn->prev;
                   1025:                if (prev)
                   1026:                        prev->next = nn->next;
                   1027:
                   1028:                nn->prev = nn->next = NULL;
                   1029:
                   1030:                /*
                   1031:                 * Scan back until we reach a node that's ordered before
                   1032:                 * us, then set ourselves as being the next.
                   1033:                 */
                   1034:                for ( ; prev; prev = prev->prev)
                   1035:                        if (order_rs(prev->tok) <= o)
                   1036:                                break;
                   1037:
                   1038:                nn->prev = prev;
                   1039:                if (prev) {
                   1040:                        if (prev->next)
                   1041:                                prev->next->prev = nn;
                   1042:                        nn->next = prev->next;
                   1043:                        prev->next = nn;
                   1044:                        continue;
                   1045:                }
                   1046:
                   1047:                n->body->child->prev = nn;
                   1048:                nn->next = n->body->child;
                   1049:                n->body->child = nn;
                   1050:        }
                   1051:        return(1);
                   1052: }

CVSweb