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

Annotation of mandoc/mdoc_action.c, Revision 1.80

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

CVSweb