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

Annotation of mandoc/mdoc_action.c, Revision 1.84

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

CVSweb