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

Annotation of mandoc/mdoc_action.c, Revision 1.81

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

CVSweb