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

Annotation of mandoc/mdoc_action.c, Revision 1.71

1.71    ! kristaps    1: /*     $Id: mdoc_action.c,v 1.70 2010/06/13 21:02:48 kristaps Exp $ */
1.1       kristaps    2: /*
1.71    ! kristaps    3:  * Copyright (c) 2008, 2009 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.1       kristaps   34:
1.35      kristaps   35: #define        POST_ARGS struct mdoc *m, struct mdoc_node *n
1.60      kristaps   36: #define        PRE_ARGS  struct mdoc *m, struct mdoc_node *n
1.1       kristaps   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.70      kristaps   55: static int       post_bl_width(POST_ARGS);
1.1       kristaps   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.62      kristaps   60: static int       post_li(POST_ARGS);
1.1       kristaps   61: static int       post_nm(POST_ARGS);
                     62: static int       post_os(POST_ARGS);
1.46      kristaps   63: static int       post_pa(POST_ARGS);
1.1       kristaps   64: static int       post_prol(POST_ARGS);
1.39      kristaps   65: static int       post_rs(POST_ARGS);
1.1       kristaps   66: static int       post_sh(POST_ARGS);
1.26      kristaps   67: static int       post_st(POST_ARGS);
1.1       kristaps   68: static int       post_std(POST_ARGS);
                     69:
                     70: static int       pre_bd(PRE_ARGS);
                     71: static int       pre_dl(PRE_ARGS);
                     72:
1.37      kristaps   73: static const struct actions mdoc_actions[MDOC_MAX] = {
1.12      kristaps   74:        { NULL, NULL }, /* Ap */
1.1       kristaps   75:        { NULL, post_dd }, /* Dd */
                     76:        { NULL, post_dt }, /* Dt */
                     77:        { NULL, post_os }, /* Os */
                     78:        { NULL, post_sh }, /* Sh */
                     79:        { NULL, NULL }, /* Ss */
                     80:        { NULL, NULL }, /* Pp */
                     81:        { NULL, NULL }, /* D1 */
                     82:        { pre_dl, post_display }, /* Dl */
                     83:        { pre_bd, post_display }, /* Bd */
                     84:        { NULL, NULL }, /* Ed */
1.68      kristaps   85:        { NULL, post_bl }, /* Bl */
1.1       kristaps   86:        { NULL, NULL }, /* El */
                     87:        { NULL, NULL }, /* It */
                     88:        { NULL, NULL }, /* Ad */
                     89:        { NULL, NULL }, /* An */
                     90:        { NULL, post_ar }, /* Ar */
1.35      kristaps   91:        { NULL, NULL }, /* Cd */
1.1       kristaps   92:        { NULL, NULL }, /* Cm */
                     93:        { NULL, NULL }, /* Dv */
                     94:        { NULL, NULL }, /* Er */
                     95:        { NULL, NULL }, /* Ev */
                     96:        { NULL, post_std }, /* Ex */
                     97:        { NULL, NULL }, /* Fa */
                     98:        { NULL, NULL }, /* Fd */
                     99:        { NULL, NULL }, /* Fl */
                    100:        { NULL, NULL }, /* Fn */
                    101:        { NULL, NULL }, /* Ft */
                    102:        { NULL, NULL }, /* Ic */
                    103:        { NULL, NULL }, /* In */
1.62      kristaps  104:        { NULL, post_li }, /* Li */
1.1       kristaps  105:        { NULL, NULL }, /* Nd */
                    106:        { NULL, post_nm }, /* Nm */
                    107:        { NULL, NULL }, /* Op */
                    108:        { NULL, NULL }, /* Ot */
1.46      kristaps  109:        { NULL, post_pa }, /* Pa */
1.1       kristaps  110:        { NULL, post_std }, /* Rv */
1.26      kristaps  111:        { NULL, post_st }, /* St */
1.1       kristaps  112:        { NULL, NULL }, /* Va */
                    113:        { NULL, NULL }, /* Vt */
                    114:        { NULL, NULL }, /* Xr */
                    115:        { NULL, NULL }, /* %A */
                    116:        { NULL, NULL }, /* %B */
                    117:        { NULL, NULL }, /* %D */
                    118:        { NULL, NULL }, /* %I */
                    119:        { NULL, NULL }, /* %J */
                    120:        { NULL, NULL }, /* %N */
                    121:        { NULL, NULL }, /* %O */
                    122:        { NULL, NULL }, /* %P */
                    123:        { NULL, NULL }, /* %R */
                    124:        { NULL, NULL }, /* %T */
                    125:        { NULL, NULL }, /* %V */
                    126:        { NULL, NULL }, /* Ac */
                    127:        { NULL, NULL }, /* Ao */
                    128:        { NULL, NULL }, /* Aq */
1.25      kristaps  129:        { NULL, post_at }, /* At */
1.1       kristaps  130:        { NULL, NULL }, /* Bc */
                    131:        { NULL, NULL }, /* Bf */
                    132:        { NULL, NULL }, /* Bo */
                    133:        { NULL, NULL }, /* Bq */
                    134:        { NULL, NULL }, /* Bsx */
                    135:        { NULL, NULL }, /* Bx */
                    136:        { NULL, NULL }, /* Db */
                    137:        { NULL, NULL }, /* Dc */
                    138:        { NULL, NULL }, /* Do */
                    139:        { NULL, NULL }, /* Dq */
                    140:        { NULL, NULL }, /* Ec */
                    141:        { NULL, NULL }, /* Ef */
                    142:        { NULL, NULL }, /* Em */
                    143:        { NULL, NULL }, /* Eo */
                    144:        { NULL, NULL }, /* Fx */
                    145:        { NULL, NULL }, /* Ms */
                    146:        { NULL, NULL }, /* No */
                    147:        { NULL, NULL }, /* Ns */
                    148:        { NULL, NULL }, /* Nx */
                    149:        { NULL, NULL }, /* Ox */
                    150:        { NULL, NULL }, /* Pc */
                    151:        { NULL, NULL }, /* Pf */
                    152:        { NULL, NULL }, /* Po */
                    153:        { NULL, NULL }, /* Pq */
                    154:        { NULL, NULL }, /* Qc */
                    155:        { NULL, NULL }, /* Ql */
                    156:        { NULL, NULL }, /* Qo */
                    157:        { NULL, NULL }, /* Qq */
                    158:        { NULL, NULL }, /* Re */
1.39      kristaps  159:        { NULL, post_rs }, /* Rs */
1.1       kristaps  160:        { NULL, NULL }, /* Sc */
                    161:        { NULL, NULL }, /* So */
                    162:        { NULL, NULL }, /* Sq */
                    163:        { NULL, NULL }, /* Sm */
                    164:        { NULL, NULL }, /* Sx */
                    165:        { NULL, NULL }, /* Sy */
                    166:        { NULL, NULL }, /* Tn */
                    167:        { NULL, NULL }, /* Ux */
                    168:        { NULL, NULL }, /* Xc */
                    169:        { NULL, NULL }, /* Xo */
                    170:        { NULL, NULL }, /* Fo */
                    171:        { NULL, NULL }, /* Fc */
                    172:        { NULL, NULL }, /* Oo */
                    173:        { NULL, NULL }, /* Oc */
                    174:        { NULL, NULL }, /* Bk */
                    175:        { NULL, NULL }, /* Ek */
                    176:        { NULL, NULL }, /* Bt */
                    177:        { NULL, NULL }, /* Hf */
                    178:        { NULL, NULL }, /* Fr */
                    179:        { NULL, NULL }, /* Ud */
1.27      kristaps  180:        { NULL, post_lb }, /* Lb */
1.1       kristaps  181:        { NULL, NULL }, /* Lp */
1.44      kristaps  182:        { NULL, NULL }, /* Lk */
1.1       kristaps  183:        { NULL, NULL }, /* Mt */
                    184:        { NULL, NULL }, /* Brq */
                    185:        { NULL, NULL }, /* Bro */
                    186:        { NULL, NULL }, /* Brc */
                    187:        { NULL, NULL }, /* %C */
                    188:        { NULL, NULL }, /* Es */
                    189:        { NULL, NULL }, /* En */
                    190:        { NULL, NULL }, /* Dx */
                    191:        { NULL, NULL }, /* %Q */
1.30      kristaps  192:        { NULL, NULL }, /* br */
                    193:        { NULL, NULL }, /* sp */
1.43      kristaps  194:        { NULL, NULL }, /* %U */
1.64      kristaps  195:        { NULL, NULL }, /* Ta */
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
1.60      kristaps  219: mdoc_action_pre(struct mdoc *m, struct mdoc_node *n)
1.1       kristaps  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.63      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.63      kristaps  511:        m->meta.title = mandoc_strdup
                    512:                ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
1.1       kristaps  513:
1.35      kristaps  514:        if (NULL == (nn = nn->next)) {
1.58      kristaps  515:                /* FIXME: warn about missing msec. */
1.46      kristaps  516:                /* XXX: make this a macro value. */
1.63      kristaps  517:                m->meta.vol = mandoc_strdup("LOCAL");
1.58      kristaps  518:                m->meta.msec = mandoc_strdup("1");
1.35      kristaps  519:                return(post_prol(m, n));
1.1       kristaps  520:        }
                    521:
                    522:        /* Handles: `.Dt TITLE SEC'
                    523:         *   --> title = TITLE, volume = SEC is msec ?
                    524:         *           format(msec) : SEC,
                    525:         *       msec = SEC is msec ? atoi(msec) : 0,
                    526:         *       arch = NULL
                    527:         */
                    528:
1.35      kristaps  529:        cp = mdoc_a2msec(nn->string);
1.1       kristaps  530:        if (cp) {
1.46      kristaps  531:                m->meta.vol = mandoc_strdup(cp);
1.58      kristaps  532:                m->meta.msec = mandoc_strdup(nn->string);
1.59      kristaps  533:        } else if (mdoc_nmsg(m, n, MANDOCERR_BADMSEC)) {
1.46      kristaps  534:                m->meta.vol = mandoc_strdup(nn->string);
1.58      kristaps  535:                m->meta.msec = mandoc_strdup(nn->string);
                    536:        } else
                    537:                return(0);
1.1       kristaps  538:
1.35      kristaps  539:        if (NULL == (nn = nn->next))
                    540:                return(post_prol(m, n));
1.1       kristaps  541:
                    542:        /* Handles: `.Dt TITLE SEC VOL'
                    543:         *   --> title = TITLE, volume = VOL is vol ?
                    544:         *       format(VOL) :
                    545:         *           VOL is arch ? format(arch) :
                    546:         *               VOL
                    547:         */
                    548:
1.35      kristaps  549:        cp = mdoc_a2vol(nn->string);
1.1       kristaps  550:        if (cp) {
                    551:                free(m->meta.vol);
1.46      kristaps  552:                m->meta.vol = mandoc_strdup(cp);
1.1       kristaps  553:        } else {
1.58      kristaps  554:                /* FIXME: warn about bad arch. */
1.35      kristaps  555:                cp = mdoc_a2arch(nn->string);
1.1       kristaps  556:                if (NULL == cp) {
                    557:                        free(m->meta.vol);
1.46      kristaps  558:                        m->meta.vol = mandoc_strdup(nn->string);
                    559:                } else
                    560:                        m->meta.arch = mandoc_strdup(cp);
1.1       kristaps  561:        }
                    562:
                    563:        /* Ignore any subsequent parameters... */
1.46      kristaps  564:        /* FIXME: warn about subsequent parameters. */
1.1       kristaps  565:
1.35      kristaps  566:        return(post_prol(m, n));
1.1       kristaps  567: }
                    568:
                    569:
1.46      kristaps  570: /*
                    571:  * Set the operating system by way of the `Os' macro.  Note that if an
                    572:  * argument isn't provided and -DOSNAME="\"foo\"" is provided during
                    573:  * compilation, this value will be used instead of filling in "sysname
                    574:  * release" from uname().
                    575:  */
1.1       kristaps  576: static int
                    577: post_os(POST_ARGS)
                    578: {
1.46      kristaps  579:        char              buf[BUFSIZ];
                    580: #ifndef OSNAME
1.1       kristaps  581:        struct utsname    utsname;
1.42      kristaps  582: #endif
                    583:
1.1       kristaps  584:        if (m->meta.os)
                    585:                free(m->meta.os);
1.4       kristaps  586:
1.46      kristaps  587:        if ( ! concat(m, buf, n->child, BUFSIZ))
1.2       kristaps  588:                return(0);
1.1       kristaps  589:
1.59      kristaps  590:        /* XXX: yes, these can all be dynamically-adjusted buffers, but
                    591:         * it's really not worth the extra hackery.
                    592:         */
                    593:
1.46      kristaps  594:        if ('\0' == buf[0]) {
                    595: #ifdef OSNAME
1.59      kristaps  596:                if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
                    597:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    598:                        return(0);
                    599:                }
1.46      kristaps  600: #else /*!OSNAME */
1.1       kristaps  601:                if (-1 == uname(&utsname))
1.59      kristaps  602:                        return(mdoc_nmsg(m, n, MANDOCERR_UTSNAME));
                    603:
                    604:                if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
                    605:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    606:                        return(0);
                    607:                }
                    608:                if (strlcat(buf, " ", 64) >= BUFSIZ) {
                    609:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    610:                        return(0);
                    611:                }
                    612:                if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
                    613:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                    614:                        return(0);
                    615:                }
1.46      kristaps  616: #endif /*!OSNAME*/
1.1       kristaps  617:        }
                    618:
1.46      kristaps  619:        m->meta.os = mandoc_strdup(buf);
1.35      kristaps  620:        return(post_prol(m, n));
1.1       kristaps  621: }
                    622:
                    623:
                    624: /*
                    625:  * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
1.46      kristaps  626:  * Uses the first head macro.  NOTE AGAIN: this is ONLY if the -width
                    627:  * argument has NOT been provided.  See post_bl_width() for converting
                    628:  * the -width string.
1.1       kristaps  629:  */
                    630: static int
1.35      kristaps  631: post_bl_tagwidth(POST_ARGS)
1.1       kristaps  632: {
1.46      kristaps  633:        struct mdoc_node *nn;
1.69      kristaps  634:        size_t            sz, ssz;
1.46      kristaps  635:        int               i;
                    636:        char              buf[NUMSIZ];
1.1       kristaps  637:
1.69      kristaps  638:        sz = 10;
1.61      joerg     639:
                    640:        for (nn = n->body->child; nn; nn = nn->next) {
1.69      kristaps  641:                if (MDOC_It != nn->tok)
                    642:                        continue;
1.46      kristaps  643:
1.35      kristaps  644:                assert(MDOC_BLOCK == nn->type);
                    645:                nn = nn->head->child;
1.69      kristaps  646:
                    647:                if (MDOC_TEXT == nn->type) {
1.46      kristaps  648:                        sz = strlen(nn->string) + 1;
1.69      kristaps  649:                        break;
                    650:                }
                    651:
                    652:                if (0 != (ssz = mdoc_macro2len(nn->tok)))
                    653:                        sz = ssz;
                    654:                else if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
                    655:                        return(0);
                    656:
                    657:                break;
1.1       kristaps  658:        }
                    659:
1.69      kristaps  660:        /* Defaults to ten ens. */
                    661:
1.46      kristaps  662:        snprintf(buf, NUMSIZ, "%zun", sz);
1.1       kristaps  663:
                    664:        /*
                    665:         * We have to dynamically add this to the macro's argument list.
                    666:         * We're guaranteed that a MDOC_Width doesn't already exist.
                    667:         */
                    668:
1.70      kristaps  669:        assert(n->args);
                    670:        i = (int)(n->args->argc)++;
                    671:
                    672:        n->args->argv = mandoc_realloc(n->args->argv,
                    673:                        n->args->argc * sizeof(struct mdoc_argv));
1.1       kristaps  674:
1.70      kristaps  675:        n->args->argv[i].arg = MDOC_Width;
                    676:        n->args->argv[i].line = n->line;
                    677:        n->args->argv[i].pos = n->pos;
                    678:        n->args->argv[i].sz = 1;
                    679:        n->args->argv[i].value = mandoc_malloc(sizeof(char *));
                    680:        n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1       kristaps  681:
1.70      kristaps  682:        /* Set our width! */
                    683:        n->data.Bl.width = n->args->argv[i].value[0];
1.1       kristaps  684:        return(1);
                    685: }
                    686:
                    687:
1.46      kristaps  688: /*
                    689:  * Calculate the real width of a list from the -width string, which may
                    690:  * contain a macro (with a known default width), a literal string, or a
                    691:  * scaling width.
                    692:  */
1.1       kristaps  693: static int
1.70      kristaps  694: post_bl_width(POST_ARGS)
1.1       kristaps  695: {
                    696:        size_t            width;
1.70      kristaps  697:        int               i;
1.51      kristaps  698:        enum mdoct        tok;
1.46      kristaps  699:        char              buf[NUMSIZ];
1.1       kristaps  700:
                    701:        /*
                    702:         * If the value to -width is a macro, then we re-write it to be
                    703:         * the macro's width as set in share/tmac/mdoc/doc-common.
                    704:         */
                    705:
1.70      kristaps  706:        if (0 == strcmp(n->data.Bl.width, "Ds"))
1.18      kristaps  707:                width = 6;
1.70      kristaps  708:        else if (MDOC_MAX == (tok = mdoc_hash_find(n->data.Bl.width)))
1.1       kristaps  709:                return(1);
                    710:        else if (0 == (width = mdoc_macro2len(tok)))
1.59      kristaps  711:                return(mdoc_nmsg(m, n, MANDOCERR_BADWIDTH));
1.1       kristaps  712:
                    713:        /* The value already exists: free and reallocate it. */
                    714:
1.70      kristaps  715:        assert(n->args);
                    716:
                    717:        for (i = 0; i < (int)n->args->argc; i++)
                    718:                if (MDOC_Width == n->args->argv[i].arg)
                    719:                        break;
                    720:
                    721:        assert(i < (int)n->args->argc);
                    722:
1.46      kristaps  723:        snprintf(buf, NUMSIZ, "%zun", width);
1.70      kristaps  724:        free(n->args->argv[i].value[0]);
                    725:        n->args->argv[i].value[0] = mandoc_strdup(buf);
                    726:
                    727:        /* Set our width! */
                    728:        n->data.Bl.width = n->args->argv[i].value[0];
1.1       kristaps  729:        return(1);
                    730: }
                    731:
                    732:
1.46      kristaps  733: /*
                    734:  * Do processing for -column lists, which can have two distinct styles
                    735:  * of invocation.  Merge this two styles into a consistent form.
                    736:  */
1.35      kristaps  737: /* ARGSUSED */
1.1       kristaps  738: static int
1.14      kristaps  739: post_bl_head(POST_ARGS)
                    740: {
                    741:        int                      i, c;
1.35      kristaps  742:        struct mdoc_node        *np, *nn, *nnp;
1.14      kristaps  743:
1.69      kristaps  744:        if (LIST_column != n->data.Bl.type)
                    745:                return(1);
                    746:        else if (NULL == n->child)
1.14      kristaps  747:                return(1);
                    748:
1.35      kristaps  749:        np = n->parent;
                    750:        assert(np->args);
1.14      kristaps  751:
1.35      kristaps  752:        for (c = 0; c < (int)np->args->argc; c++)
                    753:                if (MDOC_Column == np->args->argv[c].arg)
1.14      kristaps  754:                        break;
                    755:
1.69      kristaps  756:        assert(c < (int)np->args->argc);
1.35      kristaps  757:        assert(0 == np->args->argv[c].sz);
1.14      kristaps  758:
                    759:        /*
                    760:         * Accomodate for new-style groff column syntax.  Shuffle the
                    761:         * child nodes, all of which must be TEXT, as arguments for the
                    762:         * column field.  Then, delete the head children.
                    763:         */
                    764:
1.35      kristaps  765:        np->args->argv[c].sz = (size_t)n->nchild;
1.46      kristaps  766:        np->args->argv[c].value = mandoc_malloc
1.35      kristaps  767:                ((size_t)n->nchild * sizeof(char *));
1.14      kristaps  768:
1.35      kristaps  769:        for (i = 0, nn = n->child; nn; i++) {
                    770:                np->args->argv[c].value[i] = nn->string;
1.14      kristaps  771:                nn->string = NULL;
                    772:                nnp = nn;
                    773:                nn = nn->next;
1.53      kristaps  774:                mdoc_node_delete(NULL, nnp);
1.14      kristaps  775:        }
                    776:
1.35      kristaps  777:        n->nchild = 0;
                    778:        n->child = NULL;
1.14      kristaps  779:        return(1);
                    780: }
                    781:
                    782:
                    783: static int
1.1       kristaps  784: post_bl(POST_ARGS)
                    785: {
1.70      kristaps  786:        struct mdoc_node *nn;
                    787:        const char       *ww;
1.1       kristaps  788:
1.35      kristaps  789:        if (MDOC_HEAD == n->type)
                    790:                return(post_bl_head(m, n));
                    791:        if (MDOC_BLOCK != n->type)
1.1       kristaps  792:                return(1);
                    793:
                    794:        /*
                    795:         * These are fairly complicated, so we've broken them into two
                    796:         * functions.  post_bl_tagwidth() is called when a -tag is
                    797:         * specified, but no -width (it must be guessed).  The second
                    798:         * when a -width is specified (macro indicators must be
                    799:         * rewritten into real lengths).
                    800:         */
                    801:
1.70      kristaps  802:        ww = n->data.Bl.width;
1.1       kristaps  803:
1.70      kristaps  804:        if (LIST_tag == n->data.Bl.type && NULL == n->data.Bl.width) {
1.35      kristaps  805:                if ( ! post_bl_tagwidth(m, n))
1.1       kristaps  806:                        return(0);
1.70      kristaps  807:        } else if (NULL != n->data.Bl.width) {
                    808:                if ( ! post_bl_width(m, n))
1.1       kristaps  809:                        return(0);
1.70      kristaps  810:        } else
                    811:                return(1);
                    812:
                    813:        assert(n->data.Bl.width);
                    814:
                    815:        /* If it has changed, propogate new width to children. */
                    816:
                    817:        if (ww == n->data.Bl.width)
                    818:                return(1);
                    819:
                    820:        for (nn = n->child; nn; nn = nn->next)
                    821:                if (MDOC_Bl == nn->tok)
                    822:                        nn->data.Bl.width = n->data.Bl.width;
1.1       kristaps  823:
                    824:        return(1);
                    825: }
                    826:
                    827:
1.46      kristaps  828: /*
                    829:  * The `Pa' macro defaults to a tilde if no value is provided as an
                    830:  * argument.
                    831:  */
1.1       kristaps  832: static int
1.46      kristaps  833: post_pa(POST_ARGS)
1.10      kristaps  834: {
1.35      kristaps  835:        struct mdoc_node *np;
1.10      kristaps  836:
1.35      kristaps  837:        if (n->child)
1.10      kristaps  838:                return(1);
                    839:
1.35      kristaps  840:        np = n;
1.10      kristaps  841:        m->next = MDOC_NEXT_CHILD;
1.35      kristaps  842:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.62      kristaps  843:                return(0);
                    844:        m->last = np;
                    845:        return(1);
                    846: }
                    847:
                    848:
                    849: /*
                    850:  * Empty `Li' macros get an empty string to make front-ends add an extra
                    851:  * space.
                    852:  */
                    853: static int
                    854: post_li(POST_ARGS)
                    855: {
                    856:        struct mdoc_node *np;
                    857:
                    858:        if (n->child)
                    859:                return(1);
                    860:
                    861:        np = n;
                    862:        m->next = MDOC_NEXT_CHILD;
                    863:        if ( ! mdoc_word_alloc(m, n->line, n->pos, ""))
1.10      kristaps  864:                return(0);
1.35      kristaps  865:        m->last = np;
1.10      kristaps  866:        return(1);
                    867: }
                    868:
                    869:
1.46      kristaps  870: /*
                    871:  * The `Ar' macro defaults to two strings "file ..." if no value is
                    872:  * provided as an argument.
                    873:  */
1.10      kristaps  874: static int
1.1       kristaps  875: post_ar(POST_ARGS)
                    876: {
1.35      kristaps  877:        struct mdoc_node *np;
1.1       kristaps  878:
1.35      kristaps  879:        if (n->child)
1.1       kristaps  880:                return(1);
                    881:
1.35      kristaps  882:        np = n;
1.1       kristaps  883:        m->next = MDOC_NEXT_CHILD;
1.46      kristaps  884:        /* XXX: make into macro values. */
1.35      kristaps  885:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1       kristaps  886:                return(0);
1.35      kristaps  887:        if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1       kristaps  888:                return(0);
1.35      kristaps  889:        m->last = np;
1.1       kristaps  890:        return(1);
                    891: }
                    892:
                    893:
1.46      kristaps  894: /*
1.49      kristaps  895:  * Parse the date field in `Dd'.
1.46      kristaps  896:  */
1.1       kristaps  897: static int
                    898: post_dd(POST_ARGS)
                    899: {
1.46      kristaps  900:        char            buf[DATESIZ];
1.1       kristaps  901:
1.46      kristaps  902:        if ( ! concat(m, buf, n->child, DATESIZ))
1.2       kristaps  903:                return(0);
1.1       kristaps  904:
1.49      kristaps  905:        m->meta.date = mandoc_a2time
                    906:                (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
                    907:
1.46      kristaps  908:        if (0 == m->meta.date) {
1.59      kristaps  909:                if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
1.1       kristaps  910:                        return(0);
                    911:                m->meta.date = time(NULL);
                    912:        }
                    913:
1.35      kristaps  914:        return(post_prol(m, n));
1.1       kristaps  915: }
                    916:
                    917:
1.46      kristaps  918: /*
                    919:  * Remove prologue macros from the document after they're processed.
                    920:  * The final document uses mdoc_meta for these values and discards the
                    921:  * originals.
                    922:  */
1.1       kristaps  923: static int
                    924: post_prol(POST_ARGS)
                    925: {
1.34      kristaps  926:
1.53      kristaps  927:        mdoc_node_delete(m, n);
1.34      kristaps  928:        if (m->meta.title && m->meta.date && m->meta.os)
                    929:                m->flags |= MDOC_PBODY;
1.1       kristaps  930:        return(1);
                    931: }
                    932:
                    933:
1.46      kristaps  934: /*
                    935:  * Trigger a literal context.
                    936:  */
1.1       kristaps  937: static int
                    938: pre_dl(PRE_ARGS)
                    939: {
                    940:
1.16      kristaps  941:        if (MDOC_BODY == n->type)
                    942:                m->flags |= MDOC_LITERAL;
1.65      kristaps  943:        return(1);
1.38      kristaps  944: }
                    945:
                    946:
                    947: static int
1.1       kristaps  948: pre_bd(PRE_ARGS)
                    949: {
                    950:
                    951:        if (MDOC_BODY != n->type)
                    952:                return(1);
                    953:
1.67      kristaps  954:        if (DISP_literal == n->data.Bd.type)
1.66      kristaps  955:                m->flags |= MDOC_LITERAL;
1.67      kristaps  956:        if (DISP_unfilled == n->data.Bd.type)
1.66      kristaps  957:                m->flags |= MDOC_LITERAL;
1.1       kristaps  958:
                    959:        return(1);
                    960: }
                    961:
                    962:
                    963: static int
                    964: post_display(POST_ARGS)
                    965: {
                    966:
1.35      kristaps  967:        if (MDOC_BODY == n->type)
1.1       kristaps  968:                m->flags &= ~MDOC_LITERAL;
                    969:        return(1);
                    970: }
                    971:
                    972:
1.39      kristaps  973: static inline int
1.54      kristaps  974: order_rs(enum mdoct t)
1.39      kristaps  975: {
                    976:        int             i;
                    977:
1.54      kristaps  978:        for (i = 0; i < (int)RSORD_MAX; i++)
1.39      kristaps  979:                if (rsord[i] == t)
                    980:                        return(i);
                    981:
                    982:        abort();
                    983:        /* NOTREACHED */
                    984: }
                    985:
                    986:
1.41      kristaps  987: /* ARGSUSED */
1.39      kristaps  988: static int
                    989: post_rs(POST_ARGS)
                    990: {
                    991:        struct mdoc_node        *nn, *next, *prev;
                    992:        int                      o;
                    993:
                    994:        if (MDOC_BLOCK != n->type)
                    995:                return(1);
                    996:
1.40      kristaps  997:        assert(n->body->child);
1.39      kristaps  998:        for (next = NULL, nn = n->body->child->next; nn; nn = next) {
                    999:                o = order_rs(nn->tok);
                   1000:
                   1001:                /* Remove `nn' from the chain. */
                   1002:                next = nn->next;
                   1003:                if (next)
                   1004:                        next->prev = nn->prev;
                   1005:
                   1006:                prev = nn->prev;
                   1007:                if (prev)
                   1008:                        prev->next = nn->next;
                   1009:
                   1010:                nn->prev = nn->next = NULL;
                   1011:
                   1012:                /*
                   1013:                 * Scan back until we reach a node that's ordered before
                   1014:                 * us, then set ourselves as being the next.
                   1015:                 */
                   1016:                for ( ; prev; prev = prev->prev)
                   1017:                        if (order_rs(prev->tok) <= o)
                   1018:                                break;
                   1019:
                   1020:                nn->prev = prev;
                   1021:                if (prev) {
                   1022:                        if (prev->next)
                   1023:                                prev->next->prev = nn;
                   1024:                        nn->next = prev->next;
                   1025:                        prev->next = nn;
                   1026:                        continue;
                   1027:                }
                   1028:
                   1029:                n->body->child->prev = nn;
                   1030:                nn->next = n->body->child;
                   1031:                n->body->child = nn;
                   1032:        }
                   1033:        return(1);
                   1034: }

CVSweb