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

Annotation of mandoc/mdoc_action.c, Revision 1.59

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

CVSweb