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

Annotation of mandoc/mdoc_action.c, Revision 1.66

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

CVSweb