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

Annotation of mandoc/mdoc_validate.c, Revision 1.156

1.156   ! kristaps    1: /*     $Id: mdoc_validate.c,v 1.155 2011/02/02 21:40:45 kristaps Exp $ */
1.1       kristaps    2: /*
1.151     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.5       kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.5       kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.56      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.132     kristaps   22: #ifndef        OSNAME
                     23: #include <sys/utsname.h>
                     24: #endif
                     25:
1.1       kristaps   26: #include <sys/types.h>
                     27:
                     28: #include <assert.h>
                     29: #include <ctype.h>
1.35      kristaps   30: #include <limits.h>
1.81      kristaps   31: #include <stdio.h>
1.1       kristaps   32: #include <stdlib.h>
1.2       kristaps   33: #include <string.h>
1.132     kristaps   34: #include <time.h>
1.1       kristaps   35:
1.79      kristaps   36: #include "mandoc.h"
1.1       kristaps   37: #include "libmdoc.h"
1.18      kristaps   38: #include "libmandoc.h"
1.1       kristaps   39:
                     40: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
                     41:
1.91      kristaps   42: #define        PRE_ARGS  struct mdoc *mdoc, struct mdoc_node *n
1.32      kristaps   43: #define        POST_ARGS struct mdoc *mdoc
1.1       kristaps   44:
1.131     kristaps   45: #define        NUMSIZ    32
1.135     kristaps   46: #define        DATESIZE  32
1.131     kristaps   47:
1.120     kristaps   48: enum   check_ineq {
                     49:        CHECK_LT,
                     50:        CHECK_GT,
                     51:        CHECK_EQ
                     52: };
                     53:
                     54: enum   check_lvl {
                     55:        CHECK_WARN,
                     56:        CHECK_ERROR,
                     57: };
                     58:
1.1       kristaps   59: typedef        int     (*v_pre)(PRE_ARGS);
                     60: typedef        int     (*v_post)(POST_ARGS);
                     61:
                     62: struct valids {
                     63:        v_pre   *pre;
                     64:        v_post  *post;
                     65: };
                     66:
1.120     kristaps   67: static int      check_count(struct mdoc *, enum mdoc_type,
                     68:                        enum check_lvl, enum check_ineq, int);
1.59      kristaps   69: static int      check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.133     kristaps   70: static void     check_text(struct mdoc *, int, int, char *);
                     71: static void     check_argv(struct mdoc *,
1.92      kristaps   72:                        struct mdoc_node *, struct mdoc_argv *);
1.133     kristaps   73: static void     check_args(struct mdoc *, struct mdoc_node *);
1.32      kristaps   74:
1.132     kristaps   75: static int      concat(struct mdoc *, char *,
                     76:                        const struct mdoc_node *, size_t);
                     77:
1.120     kristaps   78: static int      ebool(POST_ARGS);
1.32      kristaps   79: static int      berr_ge1(POST_ARGS);
                     80: static int      bwarn_ge1(POST_ARGS);
1.116     kristaps   81: static int      ewarn_eq0(POST_ARGS);
1.151     schwarze   82: static int      ewarn_eq1(POST_ARGS);
1.32      kristaps   83: static int      ewarn_ge1(POST_ARGS);
1.151     schwarze   84: static int      ewarn_le1(POST_ARGS);
1.120     kristaps   85: static int      hwarn_eq0(POST_ARGS);
1.32      kristaps   86: static int      hwarn_eq1(POST_ARGS);
1.151     schwarze   87: static int      hwarn_ge1(POST_ARGS);
1.32      kristaps   88: static int      hwarn_le1(POST_ARGS);
                     89:
                     90: static int      post_an(POST_ARGS);
                     91: static int      post_at(POST_ARGS);
                     92: static int      post_bf(POST_ARGS);
                     93: static int      post_bl(POST_ARGS);
1.131     kristaps   94: static int      post_bl_block(POST_ARGS);
                     95: static int      post_bl_block_width(POST_ARGS);
                     96: static int      post_bl_block_tag(POST_ARGS);
1.32      kristaps   97: static int      post_bl_head(POST_ARGS);
1.154     kristaps   98: static int      post_bx(POST_ARGS);
1.132     kristaps   99: static int      post_dd(POST_ARGS);
                    100: static int      post_dt(POST_ARGS);
1.123     kristaps  101: static int      post_defaults(POST_ARGS);
1.128     kristaps  102: static int      post_literal(POST_ARGS);
1.123     kristaps  103: static int      post_eoln(POST_ARGS);
1.32      kristaps  104: static int      post_it(POST_ARGS);
                    105: static int      post_lb(POST_ARGS);
                    106: static int      post_nm(POST_ARGS);
1.155     kristaps  107: static int      post_ns(POST_ARGS);
1.132     kristaps  108: static int      post_os(POST_ARGS);
1.140     kristaps  109: static int      post_ignpar(POST_ARGS);
1.132     kristaps  110: static int      post_prol(POST_ARGS);
1.32      kristaps  111: static int      post_root(POST_ARGS);
1.43      kristaps  112: static int      post_rs(POST_ARGS);
1.32      kristaps  113: static int      post_sh(POST_ARGS);
                    114: static int      post_sh_body(POST_ARGS);
                    115: static int      post_sh_head(POST_ARGS);
                    116: static int      post_st(POST_ARGS);
1.132     kristaps  117: static int      post_std(POST_ARGS);
1.57      kristaps  118: static int      post_vt(POST_ARGS);
1.32      kristaps  119: static int      pre_an(PRE_ARGS);
                    120: static int      pre_bd(PRE_ARGS);
                    121: static int      pre_bl(PRE_ARGS);
                    122: static int      pre_dd(PRE_ARGS);
                    123: static int      pre_display(PRE_ARGS);
                    124: static int      pre_dt(PRE_ARGS);
                    125: static int      pre_it(PRE_ARGS);
1.128     kristaps  126: static int      pre_literal(PRE_ARGS);
1.32      kristaps  127: static int      pre_os(PRE_ARGS);
1.124     kristaps  128: static int      pre_par(PRE_ARGS);
1.32      kristaps  129: static int      pre_sh(PRE_ARGS);
                    130: static int      pre_ss(PRE_ARGS);
1.133     kristaps  131: static int      pre_std(PRE_ARGS);
1.32      kristaps  132:
                    133: static v_post   posts_an[] = { post_an, NULL };
1.126     kristaps  134: static v_post   posts_at[] = { post_at, post_defaults, NULL };
1.128     kristaps  135: static v_post   posts_bd[] = { post_literal, hwarn_eq0, bwarn_ge1, NULL };
1.32      kristaps  136: static v_post   posts_bf[] = { hwarn_le1, post_bf, NULL };
1.128     kristaps  137: static v_post   posts_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
1.32      kristaps  138: static v_post   posts_bl[] = { bwarn_ge1, post_bl, NULL };
1.154     kristaps  139: static v_post   posts_bx[] = { post_bx, NULL };
1.151     schwarze  140: static v_post   posts_bool[] = { ebool, NULL };
1.84      kristaps  141: static v_post   posts_eoln[] = { post_eoln, NULL };
1.123     kristaps  142: static v_post   posts_defaults[] = { post_defaults, NULL };
1.132     kristaps  143: static v_post   posts_dd[] = { ewarn_ge1, post_dd, post_prol, NULL };
1.151     schwarze  144: static v_post   posts_dl[] = { post_literal, bwarn_ge1, NULL };
1.132     kristaps  145: static v_post   posts_dt[] = { post_dt, post_prol, NULL };
1.32      kristaps  146: static v_post   posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
1.143     kristaps  147: static v_post   posts_it[] = { post_it, NULL };
1.151     schwarze  148: static v_post   posts_lb[] = { post_lb, NULL };
1.32      kristaps  149: static v_post   posts_nd[] = { berr_ge1, NULL };
                    150: static v_post   posts_nm[] = { post_nm, NULL };
1.116     kristaps  151: static v_post   posts_notext[] = { ewarn_eq0, NULL };
1.155     kristaps  152: static v_post   posts_ns[] = { post_ns, NULL };
1.132     kristaps  153: static v_post   posts_os[] = { post_os, post_prol, NULL };
1.151     schwarze  154: static v_post   posts_rs[] = { post_rs, NULL };
                    155: static v_post   posts_sh[] = { post_ignpar, hwarn_ge1, bwarn_ge1, post_sh, NULL };
                    156: static v_post   posts_sp[] = { ewarn_le1, NULL };
                    157: static v_post   posts_ss[] = { post_ignpar, hwarn_ge1, bwarn_ge1, NULL };
                    158: static v_post   posts_st[] = { post_st, NULL };
1.132     kristaps  159: static v_post   posts_std[] = { post_std, NULL };
1.152     schwarze  160: static v_post   posts_text[] = { ewarn_ge1, NULL };
1.151     schwarze  161: static v_post   posts_text1[] = { ewarn_eq1, NULL };
1.57      kristaps  162: static v_post   posts_vt[] = { post_vt, NULL };
1.151     schwarze  163: static v_post   posts_wline[] = { bwarn_ge1, NULL };
1.32      kristaps  164: static v_pre    pres_an[] = { pre_an, NULL };
1.128     kristaps  165: static v_pre    pres_bd[] = { pre_display, pre_bd, pre_literal, pre_par, NULL };
1.124     kristaps  166: static v_pre    pres_bl[] = { pre_bl, pre_par, NULL };
1.32      kristaps  167: static v_pre    pres_d1[] = { pre_display, NULL };
1.128     kristaps  168: static v_pre    pres_dl[] = { pre_literal, pre_display, NULL };
1.32      kristaps  169: static v_pre    pres_dd[] = { pre_dd, NULL };
                    170: static v_pre    pres_dt[] = { pre_dt, NULL };
1.67      kristaps  171: static v_pre    pres_er[] = { NULL, NULL };
1.77      kristaps  172: static v_pre    pres_fd[] = { NULL, NULL };
1.141     kristaps  173: static v_pre    pres_it[] = { pre_it, pre_par, NULL };
1.32      kristaps  174: static v_pre    pres_os[] = { pre_os, NULL };
1.124     kristaps  175: static v_pre    pres_pp[] = { pre_par, NULL };
1.32      kristaps  176: static v_pre    pres_sh[] = { pre_sh, NULL };
                    177: static v_pre    pres_ss[] = { pre_ss, NULL };
1.133     kristaps  178: static v_pre    pres_std[] = { pre_std, NULL };
1.1       kristaps  179:
                    180: const  struct valids mdoc_valids[MDOC_MAX] = {
1.10      kristaps  181:        { NULL, NULL },                         /* Ap */
1.132     kristaps  182:        { pres_dd, posts_dd },                  /* Dd */
1.85      kristaps  183:        { pres_dt, posts_dt },                  /* Dt */
1.132     kristaps  184:        { pres_os, posts_os },                  /* Os */
1.1       kristaps  185:        { pres_sh, posts_sh },                  /* Sh */
                    186:        { pres_ss, posts_ss },                  /* Ss */
1.117     kristaps  187:        { pres_pp, posts_notext },              /* Pp */
1.1       kristaps  188:        { pres_d1, posts_wline },               /* D1 */
1.128     kristaps  189:        { pres_dl, posts_dl },                  /* Dl */
                    190:        { pres_bd, posts_bd },                  /* Bd */
1.1       kristaps  191:        { NULL, NULL },                         /* Ed */
                    192:        { pres_bl, posts_bl },                  /* Bl */
                    193:        { NULL, NULL },                         /* El */
                    194:        { pres_it, posts_it },                  /* It */
1.152     schwarze  195:        { NULL, NULL },                         /* Ad */
1.1       kristaps  196:        { pres_an, posts_an },                  /* An */
1.123     kristaps  197:        { NULL, posts_defaults },               /* Ar */
1.152     schwarze  198:        { NULL, NULL },                         /* Cd */
1.1       kristaps  199:        { NULL, NULL },                         /* Cm */
                    200:        { NULL, NULL },                         /* Dv */
1.152     schwarze  201:        { pres_er, NULL },                      /* Er */
1.1       kristaps  202:        { NULL, NULL },                         /* Ev */
1.133     kristaps  203:        { pres_std, posts_std },                /* Ex */
1.1       kristaps  204:        { NULL, NULL },                         /* Fa */
1.152     schwarze  205:        { pres_fd, posts_text },                /* Fd */
1.1       kristaps  206:        { NULL, NULL },                         /* Fl */
1.152     schwarze  207:        { NULL, NULL },                         /* Fn */
                    208:        { NULL, NULL },                         /* Ft */
                    209:        { NULL, NULL },                         /* Ic */
1.51      kristaps  210:        { NULL, posts_text1 },                  /* In */
1.123     kristaps  211:        { NULL, posts_defaults },               /* Li */
1.27      kristaps  212:        { NULL, posts_nd },                     /* Nd */
1.1       kristaps  213:        { NULL, posts_nm },                     /* Nm */
1.139     kristaps  214:        { NULL, NULL },                         /* Op */
1.1       kristaps  215:        { NULL, NULL },                         /* Ot */
1.129     kristaps  216:        { NULL, posts_defaults },               /* Pa */
1.133     kristaps  217:        { pres_std, posts_std },                /* Rv */
1.1       kristaps  218:        { NULL, posts_st },                     /* St */
                    219:        { NULL, NULL },                         /* Va */
1.57      kristaps  220:        { NULL, posts_vt },                     /* Vt */
1.152     schwarze  221:        { NULL, posts_text },                   /* Xr */
1.1       kristaps  222:        { NULL, posts_text },                   /* %A */
1.47      kristaps  223:        { NULL, posts_text },                   /* %B */ /* FIXME: can be used outside Rs/Re. */
1.54      kristaps  224:        { NULL, posts_text },                   /* %D */ /* FIXME: check date with mandoc_a2time(). */
1.1       kristaps  225:        { NULL, posts_text },                   /* %I */
                    226:        { NULL, posts_text },                   /* %J */
                    227:        { NULL, posts_text },                   /* %N */
                    228:        { NULL, posts_text },                   /* %O */
                    229:        { NULL, posts_text },                   /* %P */
                    230:        { NULL, posts_text },                   /* %R */
1.47      kristaps  231:        { NULL, posts_text },                   /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1       kristaps  232:        { NULL, posts_text },                   /* %V */
                    233:        { NULL, NULL },                         /* Ac */
                    234:        { NULL, NULL },                         /* Ao */
1.139     kristaps  235:        { NULL, NULL },                         /* Aq */
1.1       kristaps  236:        { NULL, posts_at },                     /* At */
                    237:        { NULL, NULL },                         /* Bc */
                    238:        { NULL, posts_bf },                     /* Bf */
                    239:        { NULL, NULL },                         /* Bo */
1.139     kristaps  240:        { NULL, NULL },                         /* Bq */
1.1       kristaps  241:        { NULL, NULL },                         /* Bsx */
1.154     kristaps  242:        { NULL, posts_bx },                     /* Bx */
1.1       kristaps  243:        { NULL, posts_bool },                   /* Db */
                    244:        { NULL, NULL },                         /* Dc */
                    245:        { NULL, NULL },                         /* Do */
1.139     kristaps  246:        { NULL, NULL },                         /* Dq */
1.1       kristaps  247:        { NULL, NULL },                         /* Ec */
                    248:        { NULL, NULL },                         /* Ef */
                    249:        { NULL, NULL },                         /* Em */
                    250:        { NULL, NULL },                         /* Eo */
                    251:        { NULL, NULL },                         /* Fx */
1.152     schwarze  252:        { NULL, NULL },                         /* Ms */
1.1       kristaps  253:        { NULL, posts_notext },                 /* No */
1.155     kristaps  254:        { NULL, posts_ns },                     /* Ns */
1.1       kristaps  255:        { NULL, NULL },                         /* Nx */
                    256:        { NULL, NULL },                         /* Ox */
                    257:        { NULL, NULL },                         /* Pc */
1.51      kristaps  258:        { NULL, posts_text1 },                  /* Pf */
1.1       kristaps  259:        { NULL, NULL },                         /* Po */
1.139     kristaps  260:        { NULL, NULL },                         /* Pq */
1.1       kristaps  261:        { NULL, NULL },                         /* Qc */
1.139     kristaps  262:        { NULL, NULL },                         /* Ql */
1.1       kristaps  263:        { NULL, NULL },                         /* Qo */
1.139     kristaps  264:        { NULL, NULL },                         /* Qq */
1.1       kristaps  265:        { NULL, NULL },                         /* Re */
1.43      kristaps  266:        { NULL, posts_rs },                     /* Rs */
1.1       kristaps  267:        { NULL, NULL },                         /* Sc */
                    268:        { NULL, NULL },                         /* So */
1.139     kristaps  269:        { NULL, NULL },                         /* Sq */
1.1       kristaps  270:        { NULL, posts_bool },                   /* Sm */
1.152     schwarze  271:        { NULL, NULL },                         /* Sx */
                    272:        { NULL, NULL },                         /* Sy */
                    273:        { NULL, NULL },                         /* Tn */
1.1       kristaps  274:        { NULL, NULL },                         /* Ux */
                    275:        { NULL, NULL },                         /* Xc */
                    276:        { NULL, NULL },                         /* Xo */
                    277:        { NULL, posts_fo },                     /* Fo */
                    278:        { NULL, NULL },                         /* Fc */
                    279:        { NULL, NULL },                         /* Oo */
                    280:        { NULL, NULL },                         /* Oc */
1.128     kristaps  281:        { NULL, posts_bk },                     /* Bk */
1.1       kristaps  282:        { NULL, NULL },                         /* Ek */
1.84      kristaps  283:        { NULL, posts_eoln },                   /* Bt */
1.1       kristaps  284:        { NULL, NULL },                         /* Hf */
                    285:        { NULL, NULL },                         /* Fr */
1.84      kristaps  286:        { NULL, posts_eoln },                   /* Ud */
1.83      kristaps  287:        { NULL, posts_lb },                     /* Lb */
1.34      kristaps  288:        { NULL, posts_notext },                 /* Lp */
1.152     schwarze  289:        { NULL, NULL },                         /* Lk */
1.123     kristaps  290:        { NULL, posts_defaults },               /* Mt */
1.139     kristaps  291:        { NULL, NULL },                         /* Brq */
1.1       kristaps  292:        { NULL, NULL },                         /* Bro */
                    293:        { NULL, NULL },                         /* Brc */
                    294:        { NULL, posts_text },                   /* %C */
                    295:        { NULL, NULL },                         /* Es */
                    296:        { NULL, NULL },                         /* En */
                    297:        { NULL, NULL },                         /* Dx */
                    298:        { NULL, posts_text },                   /* %Q */
1.33      kristaps  299:        { NULL, posts_notext },                 /* br */
1.119     schwarze  300:        { pres_pp, posts_sp },                  /* sp */
1.51      kristaps  301:        { NULL, posts_text1 },                  /* %U */
1.88      kristaps  302:        { NULL, NULL },                         /* Ta */
1.1       kristaps  303: };
                    304:
1.122     kristaps  305: #define        RSORD_MAX 14 /* Number of `Rs' blocks. */
                    306:
                    307: static const enum mdoct rsord[RSORD_MAX] = {
                    308:        MDOC__A,
                    309:        MDOC__T,
                    310:        MDOC__B,
                    311:        MDOC__I,
                    312:        MDOC__J,
                    313:        MDOC__R,
                    314:        MDOC__N,
                    315:        MDOC__V,
                    316:        MDOC__P,
                    317:        MDOC__Q,
                    318:        MDOC__D,
                    319:        MDOC__O,
                    320:        MDOC__C,
                    321:        MDOC__U
                    322: };
                    323:
1.1       kristaps  324:
                    325: int
1.91      kristaps  326: mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
1.1       kristaps  327: {
                    328:        v_pre           *p;
                    329:        int              line, pos;
1.92      kristaps  330:        char            *tp;
1.1       kristaps  331:
1.150     kristaps  332:        switch (n->type) {
                    333:        case (MDOC_TEXT):
1.1       kristaps  334:                tp = n->string;
                    335:                line = n->line;
                    336:                pos = n->pos;
1.133     kristaps  337:                check_text(mdoc, line, pos, tp);
1.150     kristaps  338:                /* FALLTHROUGH */
                    339:        case (MDOC_TBL):
1.156   ! kristaps  340:                /* FALLTHROUGH */
        !           341:        case (MDOC_EQN):
1.150     kristaps  342:                /* FALLTHROUGH */
                    343:        case (MDOC_ROOT):
1.133     kristaps  344:                return(1);
1.150     kristaps  345:        default:
                    346:                break;
1.1       kristaps  347:        }
                    348:
1.133     kristaps  349:        check_args(mdoc, n);
                    350:
1.1       kristaps  351:        if (NULL == mdoc_valids[n->tok].pre)
                    352:                return(1);
                    353:        for (p = mdoc_valids[n->tok].pre; *p; p++)
                    354:                if ( ! (*p)(mdoc, n))
                    355:                        return(0);
                    356:        return(1);
                    357: }
                    358:
                    359:
                    360: int
                    361: mdoc_valid_post(struct mdoc *mdoc)
                    362: {
                    363:        v_post          *p;
                    364:
                    365:        if (MDOC_VALID & mdoc->last->flags)
                    366:                return(1);
                    367:        mdoc->last->flags |= MDOC_VALID;
                    368:
1.150     kristaps  369:        switch (mdoc->last->type) {
                    370:        case (MDOC_TEXT):
                    371:                /* FALLTHROUGH */
                    372:        case (MDOC_TBL):
1.1       kristaps  373:                return(1);
1.150     kristaps  374:        case (MDOC_ROOT):
1.1       kristaps  375:                return(post_root(mdoc));
1.150     kristaps  376:        default:
                    377:                break;
                    378:        }
1.1       kristaps  379:
                    380:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    381:                return(1);
                    382:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    383:                if ( ! (*p)(mdoc))
                    384:                        return(0);
                    385:
                    386:        return(1);
                    387: }
                    388:
1.120     kristaps  389: static int
                    390: check_count(struct mdoc *m, enum mdoc_type type,
                    391:                enum check_lvl lvl, enum check_ineq ineq, int val)
                    392: {
                    393:        const char      *p;
1.151     schwarze  394:        enum mandocerr   t;
1.120     kristaps  395:
                    396:        if (m->last->type != type)
                    397:                return(1);
                    398:
                    399:        switch (ineq) {
                    400:        case (CHECK_LT):
                    401:                p = "less than ";
                    402:                if (m->last->nchild < val)
                    403:                        return(1);
                    404:                break;
                    405:        case (CHECK_GT):
1.151     schwarze  406:                p = "more than ";
1.120     kristaps  407:                if (m->last->nchild > val)
                    408:                        return(1);
                    409:                break;
                    410:        case (CHECK_EQ):
                    411:                p = "";
                    412:                if (val == m->last->nchild)
                    413:                        return(1);
                    414:                break;
1.133     kristaps  415:        default:
                    416:                abort();
                    417:                /* NOTREACHED */
1.120     kristaps  418:        }
                    419:
1.151     schwarze  420:        t = lvl == CHECK_WARN ? MANDOCERR_ARGCWARN : MANDOCERR_ARGCOUNT;
1.120     kristaps  421:
1.151     schwarze  422:        return(mdoc_vmsg(m, t, m->last->line, m->last->pos,
                    423:                        "want %s%d children (have %d)",
1.120     kristaps  424:                        p, val, m->last->nchild));
                    425: }
                    426:
                    427: static int
                    428: berr_ge1(POST_ARGS)
                    429: {
                    430:
1.151     schwarze  431:        return(check_count(mdoc, MDOC_BODY, CHECK_ERROR, CHECK_GT, 0));
1.120     kristaps  432: }
                    433:
                    434: static int
                    435: bwarn_ge1(POST_ARGS)
                    436: {
                    437:        return(check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0));
                    438: }
                    439:
                    440: static int
1.151     schwarze  441: ewarn_eq0(POST_ARGS)
1.120     kristaps  442: {
1.151     schwarze  443:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0));
1.120     kristaps  444: }
1.1       kristaps  445:
1.120     kristaps  446: static int
1.151     schwarze  447: ewarn_eq1(POST_ARGS)
1.1       kristaps  448: {
1.151     schwarze  449:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1));
1.120     kristaps  450: }
1.1       kristaps  451:
1.120     kristaps  452: static int
                    453: ewarn_ge1(POST_ARGS)
                    454: {
                    455:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0));
1.1       kristaps  456: }
                    457:
1.120     kristaps  458: static int
1.151     schwarze  459: ewarn_le1(POST_ARGS)
1.120     kristaps  460: {
1.151     schwarze  461:        return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_LT, 2));
1.120     kristaps  462: }
1.1       kristaps  463:
1.120     kristaps  464: static int
1.151     schwarze  465: hwarn_eq0(POST_ARGS)
1.120     kristaps  466: {
1.151     schwarze  467:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0));
1.120     kristaps  468: }
1.1       kristaps  469:
1.120     kristaps  470: static int
1.151     schwarze  471: hwarn_eq1(POST_ARGS)
1.120     kristaps  472: {
1.151     schwarze  473:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 1));
1.1       kristaps  474: }
                    475:
1.120     kristaps  476: static int
1.151     schwarze  477: hwarn_ge1(POST_ARGS)
1.120     kristaps  478: {
1.151     schwarze  479:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_GT, 0));
1.120     kristaps  480: }
1.1       kristaps  481:
1.120     kristaps  482: static int
                    483: hwarn_le1(POST_ARGS)
                    484: {
                    485:        return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_LT, 2));
                    486: }
1.1       kristaps  487:
1.133     kristaps  488: static void
1.92      kristaps  489: check_args(struct mdoc *m, struct mdoc_node *n)
1.1       kristaps  490: {
                    491:        int              i;
                    492:
                    493:        if (NULL == n->args)
1.133     kristaps  494:                return;
1.1       kristaps  495:
                    496:        assert(n->args->argc);
                    497:        for (i = 0; i < (int)n->args->argc; i++)
1.133     kristaps  498:                check_argv(m, n, &n->args->argv[i]);
1.1       kristaps  499: }
                    500:
1.133     kristaps  501: static void
1.92      kristaps  502: check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
1.1       kristaps  503: {
                    504:        int              i;
                    505:
                    506:        for (i = 0; i < (int)v->sz; i++)
1.133     kristaps  507:                check_text(m, v->line, v->pos, v->value[i]);
1.1       kristaps  508:
1.133     kristaps  509:        /* FIXME: move to post_std(). */
1.1       kristaps  510:
1.133     kristaps  511:        if (MDOC_Std == v->arg)
                    512:                if ( ! (v->sz || m->meta.name))
                    513:                        mdoc_nmsg(m, n, MANDOCERR_NONAME);
1.1       kristaps  514: }
                    515:
1.133     kristaps  516: static void
1.112     kristaps  517: check_text(struct mdoc *m, int ln, int pos, char *p)
1.1       kristaps  518: {
1.18      kristaps  519:        int              c;
1.112     kristaps  520:        size_t           sz;
1.102     kristaps  521:
1.112     kristaps  522:        for ( ; *p; p++, pos++) {
                    523:                sz = strcspn(p, "\t\\");
                    524:                p += (int)sz;
                    525:
                    526:                if ('\0' == *p)
                    527:                        break;
                    528:
                    529:                pos += (int)sz;
1.1       kristaps  530:
                    531:                if ('\t' == *p) {
1.133     kristaps  532:                        if ( ! (MDOC_LITERAL & m->flags))
                    533:                                mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB);
                    534:                        continue;
1.112     kristaps  535:                }
1.1       kristaps  536:
1.133     kristaps  537:                if (0 == (c = mandoc_special(p))) {
                    538:                        mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
                    539:                        continue;
                    540:                }
1.1       kristaps  541:
1.133     kristaps  542:                p += c - 1;
                    543:                pos += c - 1;
1.1       kristaps  544:        }
                    545: }
                    546:
                    547: static int
1.59      kristaps  548: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1       kristaps  549: {
                    550:
                    551:        assert(n->parent);
                    552:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    553:                        (t == n->parent->type))
                    554:                return(1);
                    555:
1.79      kristaps  556:        mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
                    557:                                n->line, n->pos, "want parent %s",
                    558:                                MDOC_ROOT == t ? "<root>" :
                    559:                                        mdoc_macronames[tok]);
                    560:        return(0);
1.1       kristaps  561: }
                    562:
                    563:
                    564: static int
                    565: pre_display(PRE_ARGS)
                    566: {
                    567:        struct mdoc_node *node;
                    568:
                    569:        if (MDOC_BLOCK != n->type)
                    570:                return(1);
                    571:
                    572:        for (node = mdoc->last->parent; node; node = node->parent)
                    573:                if (MDOC_BLOCK == node->type)
                    574:                        if (MDOC_Bd == node->tok)
                    575:                                break;
1.128     kristaps  576:
1.135     kristaps  577:        if (node)
                    578:                mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
1.1       kristaps  579:
1.135     kristaps  580:        return(1);
1.1       kristaps  581: }
                    582:
                    583:
                    584: static int
                    585: pre_bl(PRE_ARGS)
                    586: {
1.104     kristaps  587:        int               i, comp, dup;
                    588:        const char       *offs, *width;
                    589:        enum mdoc_list    lt;
                    590:        struct mdoc_node *np;
1.1       kristaps  591:
1.91      kristaps  592:        if (MDOC_BLOCK != n->type) {
1.104     kristaps  593:                if (ENDBODY_NOT != n->end) {
                    594:                        assert(n->pending);
                    595:                        np = n->pending->parent;
                    596:                } else
                    597:                        np = n->parent;
                    598:
                    599:                assert(np);
                    600:                assert(MDOC_BLOCK == np->type);
                    601:                assert(MDOC_Bl == np->tok);
1.1       kristaps  602:                return(1);
1.79      kristaps  603:        }
1.1       kristaps  604:
1.91      kristaps  605:        /*
                    606:         * First figure out which kind of list to use: bind ourselves to
                    607:         * the first mentioned list type and warn about any remaining
                    608:         * ones.  If we find no list type, we default to LIST_item.
                    609:         */
1.1       kristaps  610:
                    611:        /* LINTED */
1.91      kristaps  612:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    613:                lt = LIST__NONE;
1.97      kristaps  614:                dup = comp = 0;
1.99      kristaps  615:                width = offs = NULL;
1.91      kristaps  616:                switch (n->args->argv[i].arg) {
                    617:                /* Set list types. */
1.1       kristaps  618:                case (MDOC_Bullet):
1.91      kristaps  619:                        lt = LIST_bullet;
                    620:                        break;
1.1       kristaps  621:                case (MDOC_Dash):
1.91      kristaps  622:                        lt = LIST_dash;
                    623:                        break;
1.1       kristaps  624:                case (MDOC_Enum):
1.91      kristaps  625:                        lt = LIST_enum;
                    626:                        break;
1.1       kristaps  627:                case (MDOC_Hyphen):
1.91      kristaps  628:                        lt = LIST_hyphen;
                    629:                        break;
1.1       kristaps  630:                case (MDOC_Item):
1.91      kristaps  631:                        lt = LIST_item;
                    632:                        break;
1.1       kristaps  633:                case (MDOC_Tag):
1.91      kristaps  634:                        lt = LIST_tag;
                    635:                        break;
1.1       kristaps  636:                case (MDOC_Diag):
1.91      kristaps  637:                        lt = LIST_diag;
                    638:                        break;
1.1       kristaps  639:                case (MDOC_Hang):
1.91      kristaps  640:                        lt = LIST_hang;
                    641:                        break;
1.1       kristaps  642:                case (MDOC_Ohang):
1.91      kristaps  643:                        lt = LIST_ohang;
                    644:                        break;
1.1       kristaps  645:                case (MDOC_Inset):
1.91      kristaps  646:                        lt = LIST_inset;
                    647:                        break;
1.1       kristaps  648:                case (MDOC_Column):
1.91      kristaps  649:                        lt = LIST_column;
                    650:                        break;
                    651:                /* Set list arguments. */
1.45      kristaps  652:                case (MDOC_Compact):
1.147     kristaps  653:                        dup = n->norm->Bl.comp;
1.97      kristaps  654:                        comp = 1;
1.91      kristaps  655:                        break;
1.1       kristaps  656:                case (MDOC_Width):
1.147     kristaps  657:                        dup = (NULL != n->norm->Bl.width);
1.99      kristaps  658:                        width = n->args->argv[i].value[0];
1.8       kristaps  659:                        break;
1.1       kristaps  660:                case (MDOC_Offset):
1.98      kristaps  661:                        /* NB: this can be empty! */
                    662:                        if (n->args->argv[i].sz) {
                    663:                                offs = n->args->argv[i].value[0];
1.147     kristaps  664:                                dup = (NULL != n->norm->Bl.offs);
1.98      kristaps  665:                                break;
                    666:                        }
1.133     kristaps  667:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.1       kristaps  668:                        break;
1.113     kristaps  669:                default:
                    670:                        continue;
1.1       kristaps  671:                }
                    672:
1.91      kristaps  673:                /* Check: duplicate auxiliary arguments. */
                    674:
1.133     kristaps  675:                if (dup)
                    676:                        mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.97      kristaps  677:
                    678:                if (comp && ! dup)
1.147     kristaps  679:                        n->norm->Bl.comp = comp;
1.98      kristaps  680:                if (offs && ! dup)
1.147     kristaps  681:                        n->norm->Bl.offs = offs;
1.99      kristaps  682:                if (width && ! dup)
1.147     kristaps  683:                        n->norm->Bl.width = width;
1.91      kristaps  684:
                    685:                /* Check: multiple list types. */
                    686:
1.147     kristaps  687:                if (LIST__NONE != lt && n->norm->Bl.type != LIST__NONE)
1.133     kristaps  688:                        mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP);
1.91      kristaps  689:
                    690:                /* Assign list type. */
                    691:
1.147     kristaps  692:                if (LIST__NONE != lt && n->norm->Bl.type == LIST__NONE) {
                    693:                        n->norm->Bl.type = lt;
1.109     kristaps  694:                        /* Set column information, too. */
                    695:                        if (LIST_column == lt) {
1.147     kristaps  696:                                n->norm->Bl.ncols =
1.109     kristaps  697:                                        n->args->argv[i].sz;
1.147     kristaps  698:                                n->norm->Bl.cols = (const char **)
1.109     kristaps  699:                                        n->args->argv[i].value;
                    700:                        }
                    701:                }
1.91      kristaps  702:
                    703:                /* The list type should come first. */
                    704:
1.147     kristaps  705:                if (n->norm->Bl.type == LIST__NONE)
                    706:                        if (n->norm->Bl.width ||
                    707:                                        n->norm->Bl.offs ||
                    708:                                        n->norm->Bl.comp)
1.133     kristaps  709:                                mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST);
1.91      kristaps  710:
                    711:                continue;
                    712:        }
                    713:
                    714:        /* Allow lists to default to LIST_item. */
                    715:
1.147     kristaps  716:        if (LIST__NONE == n->norm->Bl.type) {
1.133     kristaps  717:                mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
1.147     kristaps  718:                n->norm->Bl.type = LIST_item;
1.79      kristaps  719:        }
1.1       kristaps  720:
1.8       kristaps  721:        /*
                    722:         * Validate the width field.  Some list types don't need width
                    723:         * types and should be warned about them.  Others should have it
                    724:         * and must also be warned.
                    725:         */
                    726:
1.147     kristaps  727:        switch (n->norm->Bl.type) {
1.91      kristaps  728:        case (LIST_tag):
1.147     kristaps  729:                if (n->norm->Bl.width)
1.91      kristaps  730:                        break;
1.133     kristaps  731:                mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
                    732:                break;
1.91      kristaps  733:        case (LIST_column):
1.1       kristaps  734:                /* FALLTHROUGH */
1.91      kristaps  735:        case (LIST_diag):
1.1       kristaps  736:                /* FALLTHROUGH */
1.91      kristaps  737:        case (LIST_ohang):
1.53      kristaps  738:                /* FALLTHROUGH */
1.91      kristaps  739:        case (LIST_inset):
1.1       kristaps  740:                /* FALLTHROUGH */
1.91      kristaps  741:        case (LIST_item):
1.147     kristaps  742:                if (n->norm->Bl.width)
1.137     kristaps  743:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.133     kristaps  744:                break;
1.8       kristaps  745:        default:
                    746:                break;
                    747:        }
                    748:
1.1       kristaps  749:        return(1);
                    750: }
                    751:
                    752:
                    753: static int
                    754: pre_bd(PRE_ARGS)
                    755: {
1.104     kristaps  756:        int               i, dup, comp;
                    757:        enum mdoc_disp    dt;
                    758:        const char       *offs;
                    759:        struct mdoc_node *np;
1.1       kristaps  760:
1.93      kristaps  761:        if (MDOC_BLOCK != n->type) {
1.104     kristaps  762:                if (ENDBODY_NOT != n->end) {
                    763:                        assert(n->pending);
                    764:                        np = n->pending->parent;
                    765:                } else
                    766:                        np = n->parent;
                    767:
                    768:                assert(np);
                    769:                assert(MDOC_BLOCK == np->type);
                    770:                assert(MDOC_Bd == np->tok);
1.1       kristaps  771:                return(1);
1.79      kristaps  772:        }
1.1       kristaps  773:
                    774:        /* LINTED */
1.93      kristaps  775:        for (i = 0; n->args && i < (int)n->args->argc; i++) {
                    776:                dt = DISP__NONE;
1.94      kristaps  777:                dup = comp = 0;
                    778:                offs = NULL;
                    779:
1.1       kristaps  780:                switch (n->args->argv[i].arg) {
1.49      kristaps  781:                case (MDOC_Centred):
1.93      kristaps  782:                        dt = DISP_centred;
                    783:                        break;
1.1       kristaps  784:                case (MDOC_Ragged):
1.93      kristaps  785:                        dt = DISP_ragged;
                    786:                        break;
1.1       kristaps  787:                case (MDOC_Unfilled):
1.93      kristaps  788:                        dt = DISP_unfilled;
                    789:                        break;
1.1       kristaps  790:                case (MDOC_Filled):
1.93      kristaps  791:                        dt = DISP_filled;
                    792:                        break;
1.1       kristaps  793:                case (MDOC_Literal):
1.93      kristaps  794:                        dt = DISP_literal;
1.79      kristaps  795:                        break;
1.93      kristaps  796:                case (MDOC_File):
                    797:                        mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
                    798:                        return(0);
                    799:                case (MDOC_Offset):
1.94      kristaps  800:                        /* NB: this can be empty! */
                    801:                        if (n->args->argv[i].sz) {
                    802:                                offs = n->args->argv[i].value[0];
1.147     kristaps  803:                                dup = (NULL != n->norm->Bd.offs);
1.94      kristaps  804:                                break;
                    805:                        }
1.133     kristaps  806:                        mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.94      kristaps  807:                        break;
1.93      kristaps  808:                case (MDOC_Compact):
1.94      kristaps  809:                        comp = 1;
1.147     kristaps  810:                        dup = n->norm->Bd.comp;
1.94      kristaps  811:                        break;
1.1       kristaps  812:                default:
1.94      kristaps  813:                        abort();
                    814:                        /* NOTREACHED */
1.1       kristaps  815:                }
                    816:
1.94      kristaps  817:                /* Check whether we have duplicates. */
                    818:
1.133     kristaps  819:                if (dup)
                    820:                        mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.94      kristaps  821:
                    822:                /* Make our auxiliary assignments. */
                    823:
                    824:                if (offs && ! dup)
1.147     kristaps  825:                        n->norm->Bd.offs = offs;
1.94      kristaps  826:                if (comp && ! dup)
1.147     kristaps  827:                        n->norm->Bd.comp = comp;
1.94      kristaps  828:
                    829:                /* Check whether a type has already been assigned. */
                    830:
1.147     kristaps  831:                if (DISP__NONE != dt && n->norm->Bd.type != DISP__NONE)
1.133     kristaps  832:                        mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP);
1.93      kristaps  833:
1.94      kristaps  834:                /* Make our type assignment. */
                    835:
1.147     kristaps  836:                if (DISP__NONE != dt && n->norm->Bd.type == DISP__NONE)
                    837:                        n->norm->Bd.type = dt;
1.93      kristaps  838:        }
                    839:
1.147     kristaps  840:        if (DISP__NONE == n->norm->Bd.type) {
1.133     kristaps  841:                mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
1.147     kristaps  842:                n->norm->Bd.type = DISP_ragged;
1.93      kristaps  843:        }
                    844:
                    845:        return(1);
1.1       kristaps  846: }
                    847:
                    848:
                    849: static int
                    850: pre_ss(PRE_ARGS)
                    851: {
                    852:
                    853:        if (MDOC_BLOCK != n->type)
                    854:                return(1);
                    855:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
                    856: }
                    857:
                    858:
                    859: static int
                    860: pre_sh(PRE_ARGS)
                    861: {
                    862:
                    863:        if (MDOC_BLOCK != n->type)
                    864:                return(1);
1.100     kristaps  865:
                    866:        mdoc->regs->regs[(int)REG_nS].set = 0;
1.70      kristaps  867:        return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1       kristaps  868: }
                    869:
                    870:
                    871: static int
                    872: pre_it(PRE_ARGS)
                    873: {
                    874:
                    875:        if (MDOC_BLOCK != n->type)
                    876:                return(1);
1.133     kristaps  877:
1.1       kristaps  878:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
                    879: }
                    880:
                    881:
                    882: static int
                    883: pre_an(PRE_ARGS)
                    884: {
1.121     kristaps  885:        int              i;
1.1       kristaps  886:
1.107     kristaps  887:        if (NULL == n->args)
1.1       kristaps  888:                return(1);
1.121     kristaps  889:
                    890:        for (i = 1; i < (int)n->args->argc; i++)
1.133     kristaps  891:                mdoc_pmsg(mdoc, n->args->argv[i].line,
                    892:                        n->args->argv[i].pos, MANDOCERR_IGNARGV);
1.120     kristaps  893:
1.107     kristaps  894:        if (MDOC_Split == n->args->argv[0].arg)
1.147     kristaps  895:                n->norm->An.auth = AUTH_split;
1.107     kristaps  896:        else if (MDOC_Nosplit == n->args->argv[0].arg)
1.147     kristaps  897:                n->norm->An.auth = AUTH_nosplit;
1.107     kristaps  898:        else
                    899:                abort();
                    900:
                    901:        return(1);
1.1       kristaps  902: }
                    903:
                    904: static int
1.133     kristaps  905: pre_std(PRE_ARGS)
1.1       kristaps  906: {
                    907:
1.133     kristaps  908:        if (n->args && 1 == n->args->argc)
                    909:                if (MDOC_Std == n->args->argv[0].arg)
                    910:                        return(1);
                    911:
                    912:        mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV);
                    913:        return(1);
1.1       kristaps  914: }
                    915:
1.85      kristaps  916: static int
1.1       kristaps  917: pre_dt(PRE_ARGS)
                    918: {
1.54      kristaps  919:
1.1       kristaps  920:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.133     kristaps  921:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    922:
1.1       kristaps  923:        if (mdoc->meta.title)
1.133     kristaps  924:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    925:
1.1       kristaps  926:        return(1);
                    927: }
                    928:
                    929: static int
                    930: pre_os(PRE_ARGS)
                    931: {
                    932:
                    933:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.133     kristaps  934:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    935:
1.1       kristaps  936:        if (mdoc->meta.os)
1.133     kristaps  937:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    938:
1.1       kristaps  939:        return(1);
                    940: }
                    941:
                    942: static int
                    943: pre_dd(PRE_ARGS)
                    944: {
                    945:
                    946:        if (mdoc->meta.title || mdoc->meta.os)
1.133     kristaps  947:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
                    948:
1.1       kristaps  949:        if (mdoc->meta.date)
1.133     kristaps  950:                mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
                    951:
1.1       kristaps  952:        return(1);
                    953: }
                    954:
                    955:
                    956: static int
                    957: post_bf(POST_ARGS)
                    958: {
1.105     kristaps  959:        struct mdoc_node *np;
1.113     kristaps  960:        enum mdocargt     arg;
1.105     kristaps  961:
                    962:        /*
                    963:         * Unlike other data pointers, these are "housed" by the HEAD
                    964:         * element, which contains the goods.
                    965:         */
1.1       kristaps  966:
1.105     kristaps  967:        if (MDOC_HEAD != mdoc->last->type) {
                    968:                if (ENDBODY_NOT != mdoc->last->end) {
                    969:                        assert(mdoc->last->pending);
                    970:                        np = mdoc->last->pending->parent->head;
                    971:                } else if (MDOC_BLOCK != mdoc->last->type) {
                    972:                        np = mdoc->last->parent->head;
                    973:                } else
                    974:                        np = mdoc->last->head;
                    975:
                    976:                assert(np);
                    977:                assert(MDOC_HEAD == np->type);
                    978:                assert(MDOC_Bf == np->tok);
1.1       kristaps  979:                return(1);
1.105     kristaps  980:        }
1.1       kristaps  981:
1.105     kristaps  982:        np = mdoc->last;
1.106     kristaps  983:        assert(MDOC_BLOCK == np->parent->type);
                    984:        assert(MDOC_Bf == np->parent->tok);
1.1       kristaps  985:
1.105     kristaps  986:        /*
                    987:         * Cannot have both argument and parameter.
                    988:         * If neither is specified, let it through with a warning.
                    989:         */
                    990:
1.106     kristaps  991:        if (np->parent->args && np->child) {
1.105     kristaps  992:                mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.79      kristaps  993:                return(0);
1.133     kristaps  994:        } else if (NULL == np->parent->args && NULL == np->child) {
                    995:                mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
                    996:                return(1);
                    997:        }
1.105     kristaps  998:
                    999:        /* Extract argument into data. */
                   1000:
1.106     kristaps 1001:        if (np->parent->args) {
                   1002:                arg = np->parent->args->argv[0].arg;
1.105     kristaps 1003:                if (MDOC_Emphasis == arg)
1.147     kristaps 1004:                        np->norm->Bf.font = FONT_Em;
1.105     kristaps 1005:                else if (MDOC_Literal == arg)
1.147     kristaps 1006:                        np->norm->Bf.font = FONT_Li;
1.105     kristaps 1007:                else if (MDOC_Symbolic == arg)
1.147     kristaps 1008:                        np->norm->Bf.font = FONT_Sy;
1.105     kristaps 1009:                else
                   1010:                        abort();
1.13      kristaps 1011:                return(1);
1.79      kristaps 1012:        }
1.1       kristaps 1013:
1.105     kristaps 1014:        /* Extract parameter into data. */
1.1       kristaps 1015:
1.105     kristaps 1016:        if (0 == strcmp(np->child->string, "Em"))
1.147     kristaps 1017:                np->norm->Bf.font = FONT_Em;
1.105     kristaps 1018:        else if (0 == strcmp(np->child->string, "Li"))
1.147     kristaps 1019:                np->norm->Bf.font = FONT_Li;
1.105     kristaps 1020:        else if (0 == strcmp(np->child->string, "Sy"))
1.147     kristaps 1021:                np->norm->Bf.font = FONT_Sy;
1.133     kristaps 1022:        else
                   1023:                mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
1.1       kristaps 1024:
1.105     kristaps 1025:        return(1);
1.1       kristaps 1026: }
                   1027:
                   1028: static int
1.31      kristaps 1029: post_lb(POST_ARGS)
                   1030: {
1.127     kristaps 1031:        const char      *p;
                   1032:        char            *buf;
                   1033:        size_t           sz;
                   1034:
1.151     schwarze 1035:        check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
                   1036:
1.127     kristaps 1037:        assert(mdoc->last->child);
                   1038:        assert(MDOC_TEXT == mdoc->last->child->type);
                   1039:
                   1040:        p = mdoc_a2lib(mdoc->last->child->string);
                   1041:
                   1042:        /* If lookup ok, replace with table value. */
1.31      kristaps 1043:
1.127     kristaps 1044:        if (p) {
                   1045:                free(mdoc->last->child->string);
                   1046:                mdoc->last->child->string = mandoc_strdup(p);
1.31      kristaps 1047:                return(1);
1.127     kristaps 1048:        }
                   1049:
                   1050:        /* If not, use "library ``xxxx''. */
                   1051:
                   1052:        sz = strlen(mdoc->last->child->string) +
                   1053:                2 + strlen("\\(lqlibrary\\(rq");
                   1054:        buf = mandoc_malloc(sz);
                   1055:        snprintf(buf, sz, "library \\(lq%s\\(rq",
                   1056:                        mdoc->last->child->string);
                   1057:        free(mdoc->last->child->string);
                   1058:        mdoc->last->child->string = buf;
                   1059:        return(1);
1.84      kristaps 1060: }
                   1061:
                   1062: static int
                   1063: post_eoln(POST_ARGS)
                   1064: {
                   1065:
1.133     kristaps 1066:        if (mdoc->last->child)
                   1067:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
                   1068:        return(1);
1.31      kristaps 1069: }
                   1070:
                   1071:
                   1072: static int
1.57      kristaps 1073: post_vt(POST_ARGS)
                   1074: {
                   1075:        const struct mdoc_node *n;
                   1076:
                   1077:        /*
                   1078:         * The Vt macro comes in both ELEM and BLOCK form, both of which
                   1079:         * have different syntaxes (yet more context-sensitive
1.152     schwarze 1080:         * behaviour).  ELEM types must have a child, which is already
                   1081:         * guaranteed by the in_line parsing routine; BLOCK types,
1.57      kristaps 1082:         * specifically the BODY, should only have TEXT children.
                   1083:         */
                   1084:
                   1085:        if (MDOC_BODY != mdoc->last->type)
                   1086:                return(1);
                   1087:
                   1088:        for (n = mdoc->last->child; n; n = n->next)
                   1089:                if (MDOC_TEXT != n->type)
1.133     kristaps 1090:                        mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1.57      kristaps 1091:
                   1092:        return(1);
                   1093: }
                   1094:
                   1095:
                   1096: static int
1.1       kristaps 1097: post_nm(POST_ARGS)
                   1098: {
1.132     kristaps 1099:        char             buf[BUFSIZ];
1.129     kristaps 1100:
                   1101:        /* If no child specified, make sure we have the meta name. */
1.1       kristaps 1102:
1.129     kristaps 1103:        if (NULL == mdoc->last->child && NULL == mdoc->meta.name) {
                   1104:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME);
1.1       kristaps 1105:                return(1);
1.129     kristaps 1106:        } else if (mdoc->meta.name)
1.1       kristaps 1107:                return(1);
1.129     kristaps 1108:
                   1109:        /* If no meta name, set it from the child. */
                   1110:
1.132     kristaps 1111:        if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
                   1112:                return(0);
1.129     kristaps 1113:
1.132     kristaps 1114:        mdoc->meta.name = mandoc_strdup(buf);
1.129     kristaps 1115:
                   1116:        return(1);
1.1       kristaps 1117: }
                   1118:
1.123     kristaps 1119: static int
1.128     kristaps 1120: post_literal(POST_ARGS)
                   1121: {
                   1122:
                   1123:        /*
                   1124:         * The `Dl' (note "el" not "one") and `Bd' macros unset the
                   1125:         * MDOC_LITERAL flag as they leave.  Note that `Bd' only sets
                   1126:         * this in literal mode, but it doesn't hurt to just switch it
                   1127:         * off in general since displays can't be nested.
                   1128:         */
                   1129:
                   1130:        if (MDOC_BODY == mdoc->last->type)
1.134     kristaps 1131:                mdoc->flags &= ~MDOC_LITERAL;
1.128     kristaps 1132:
                   1133:        return(1);
                   1134: }
                   1135:
                   1136: static int
1.123     kristaps 1137: post_defaults(POST_ARGS)
                   1138: {
                   1139:        struct mdoc_node *nn;
                   1140:
                   1141:        /*
                   1142:         * The `Ar' defaults to "file ..." if no value is provided as an
1.129     kristaps 1143:         * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
                   1144:         * gets an empty string.
1.123     kristaps 1145:         */
                   1146:
                   1147:        if (mdoc->last->child)
                   1148:                return(1);
                   1149:
                   1150:        nn = mdoc->last;
                   1151:        mdoc->next = MDOC_NEXT_CHILD;
                   1152:
                   1153:        switch (nn->tok) {
                   1154:        case (MDOC_Ar):
                   1155:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
                   1156:                        return(0);
                   1157:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
                   1158:                        return(0);
                   1159:                break;
1.126     kristaps 1160:        case (MDOC_At):
                   1161:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
                   1162:                        return(0);
                   1163:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
                   1164:                        return(0);
                   1165:                break;
1.123     kristaps 1166:        case (MDOC_Li):
                   1167:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
                   1168:                        return(0);
                   1169:                break;
1.129     kristaps 1170:        case (MDOC_Pa):
                   1171:                /* FALLTHROUGH */
1.123     kristaps 1172:        case (MDOC_Mt):
                   1173:                if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
                   1174:                        return(0);
                   1175:                break;
                   1176:        default:
                   1177:                abort();
                   1178:                /* NOTREACHED */
                   1179:        }
                   1180:
                   1181:        mdoc->last = nn;
                   1182:        return(1);
                   1183: }
1.1       kristaps 1184:
                   1185: static int
                   1186: post_at(POST_ARGS)
                   1187: {
1.126     kristaps 1188:        const char       *p, *q;
                   1189:        char             *buf;
                   1190:        size_t            sz;
1.1       kristaps 1191:
1.126     kristaps 1192:        /*
                   1193:         * If we have a child, look it up in the standard keys.  If a
                   1194:         * key exist, use that instead of the child; if it doesn't,
                   1195:         * prefix "AT&T UNIX " to the existing data.
                   1196:         */
                   1197:
1.1       kristaps 1198:        if (NULL == mdoc->last->child)
                   1199:                return(1);
1.126     kristaps 1200:
1.79      kristaps 1201:        assert(MDOC_TEXT == mdoc->last->child->type);
1.126     kristaps 1202:        p = mdoc_a2att(mdoc->last->child->string);
                   1203:
                   1204:        if (p) {
                   1205:                free(mdoc->last->child->string);
                   1206:                mdoc->last->child->string = mandoc_strdup(p);
                   1207:        } else {
                   1208:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
                   1209:                p = "AT&T UNIX ";
                   1210:                q = mdoc->last->child->string;
                   1211:                sz = strlen(p) + strlen(q) + 1;
                   1212:                buf = mandoc_malloc(sz);
                   1213:                strlcpy(buf, p, sz);
                   1214:                strlcat(buf, q, sz);
                   1215:                free(mdoc->last->child->string);
                   1216:                mdoc->last->child->string = buf;
                   1217:        }
                   1218:
                   1219:        return(1);
1.1       kristaps 1220: }
                   1221:
                   1222: static int
                   1223: post_an(POST_ARGS)
                   1224: {
1.107     kristaps 1225:        struct mdoc_node *np;
1.1       kristaps 1226:
1.107     kristaps 1227:        np = mdoc->last;
1.152     schwarze 1228:        if (AUTH__NONE == np->norm->An.auth) {
                   1229:                if (0 == np->child)
                   1230:                        check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0);
                   1231:        } else if (np->child)
1.151     schwarze 1232:                check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0);
1.133     kristaps 1233:
                   1234:        return(1);
1.1       kristaps 1235: }
                   1236:
                   1237:
                   1238: static int
                   1239: post_it(POST_ARGS)
                   1240: {
1.89      kristaps 1241:        int               i, cols, rc;
                   1242:        enum mdoc_list    lt;
1.1       kristaps 1243:        struct mdoc_node *n, *c;
1.89      kristaps 1244:        enum mandocerr    er;
1.1       kristaps 1245:
                   1246:        if (MDOC_BLOCK != mdoc->last->type)
                   1247:                return(1);
                   1248:
                   1249:        n = mdoc->last->parent->parent;
1.147     kristaps 1250:        lt = n->norm->Bl.type;
1.89      kristaps 1251:
                   1252:        if (LIST__NONE == lt) {
1.79      kristaps 1253:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
1.133     kristaps 1254:                return(1);
1.79      kristaps 1255:        }
1.1       kristaps 1256:
1.89      kristaps 1257:        switch (lt) {
                   1258:        case (LIST_tag):
                   1259:                if (mdoc->last->head->child)
1.1       kristaps 1260:                        break;
1.89      kristaps 1261:                /* FIXME: give this a dummy value. */
1.133     kristaps 1262:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1       kristaps 1263:                break;
1.89      kristaps 1264:        case (LIST_hang):
1.1       kristaps 1265:                /* FALLTHROUGH */
1.89      kristaps 1266:        case (LIST_ohang):
1.1       kristaps 1267:                /* FALLTHROUGH */
1.89      kristaps 1268:        case (LIST_inset):
1.1       kristaps 1269:                /* FALLTHROUGH */
1.89      kristaps 1270:        case (LIST_diag):
1.1       kristaps 1271:                if (NULL == mdoc->last->head->child)
1.133     kristaps 1272:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1       kristaps 1273:                break;
1.89      kristaps 1274:        case (LIST_bullet):
1.1       kristaps 1275:                /* FALLTHROUGH */
1.89      kristaps 1276:        case (LIST_dash):
1.1       kristaps 1277:                /* FALLTHROUGH */
1.89      kristaps 1278:        case (LIST_enum):
1.1       kristaps 1279:                /* FALLTHROUGH */
1.89      kristaps 1280:        case (LIST_hyphen):
1.108     schwarze 1281:                if (NULL == mdoc->last->body->child)
1.133     kristaps 1282:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.1       kristaps 1283:                /* FALLTHROUGH */
1.89      kristaps 1284:        case (LIST_item):
1.1       kristaps 1285:                if (mdoc->last->head->child)
1.133     kristaps 1286:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1.1       kristaps 1287:                break;
1.89      kristaps 1288:        case (LIST_column):
1.147     kristaps 1289:                cols = (int)n->norm->Bl.ncols;
1.89      kristaps 1290:
1.87      kristaps 1291:                assert(NULL == mdoc->last->head->child);
1.89      kristaps 1292:
1.87      kristaps 1293:                if (NULL == mdoc->last->body->child)
1.133     kristaps 1294:                        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.87      kristaps 1295:
1.89      kristaps 1296:                for (i = 0, c = mdoc->last->child; c; c = c->next)
1.87      kristaps 1297:                        if (MDOC_BODY == c->type)
                   1298:                                i++;
1.38      kristaps 1299:
1.89      kristaps 1300:                if (i < cols)
                   1301:                        er = MANDOCERR_ARGCOUNT;
                   1302:                else if (i == cols || i == cols + 1)
1.38      kristaps 1303:                        break;
1.89      kristaps 1304:                else
                   1305:                        er = MANDOCERR_SYNTARGCOUNT;
1.38      kristaps 1306:
1.89      kristaps 1307:                rc = mdoc_vmsg(mdoc, er,
1.79      kristaps 1308:                                mdoc->last->line, mdoc->last->pos,
                   1309:                                "columns == %d (have %d)", cols, i);
1.89      kristaps 1310:                return(rc);
1.1       kristaps 1311:        default:
                   1312:                break;
                   1313:        }
                   1314:
                   1315:        return(1);
                   1316: }
                   1317:
1.131     kristaps 1318: static int
                   1319: post_bl_block(POST_ARGS)
                   1320: {
                   1321:        struct mdoc_node *n;
                   1322:
                   1323:        /*
                   1324:         * These are fairly complicated, so we've broken them into two
                   1325:         * functions.  post_bl_block_tag() is called when a -tag is
                   1326:         * specified, but no -width (it must be guessed).  The second
                   1327:         * when a -width is specified (macro indicators must be
                   1328:         * rewritten into real lengths).
                   1329:         */
                   1330:
                   1331:        n = mdoc->last;
                   1332:
1.147     kristaps 1333:        if (LIST_tag == n->norm->Bl.type &&
                   1334:                        NULL == n->norm->Bl.width) {
1.131     kristaps 1335:                if ( ! post_bl_block_tag(mdoc))
                   1336:                        return(0);
1.147     kristaps 1337:        } else if (NULL != n->norm->Bl.width) {
1.131     kristaps 1338:                if ( ! post_bl_block_width(mdoc))
                   1339:                        return(0);
                   1340:        } else
                   1341:                return(1);
                   1342:
1.147     kristaps 1343:        assert(n->norm->Bl.width);
1.131     kristaps 1344:        return(1);
                   1345: }
                   1346:
                   1347: static int
                   1348: post_bl_block_width(POST_ARGS)
                   1349: {
                   1350:        size_t            width;
                   1351:        int               i;
                   1352:        enum mdoct        tok;
                   1353:        struct mdoc_node *n;
                   1354:        char              buf[NUMSIZ];
                   1355:
                   1356:        n = mdoc->last;
                   1357:
                   1358:        /*
                   1359:         * Calculate the real width of a list from the -width string,
                   1360:         * which may contain a macro (with a known default width), a
                   1361:         * literal string, or a scaling width.
                   1362:         *
                   1363:         * If the value to -width is a macro, then we re-write it to be
                   1364:         * the macro's width as set in share/tmac/mdoc/doc-common.
                   1365:         */
                   1366:
1.147     kristaps 1367:        if (0 == strcmp(n->norm->Bl.width, "Ds"))
1.131     kristaps 1368:                width = 6;
1.147     kristaps 1369:        else if (MDOC_MAX == (tok = mdoc_hash_find(n->norm->Bl.width)))
1.131     kristaps 1370:                return(1);
1.133     kristaps 1371:        else if (0 == (width = mdoc_macro2len(tok)))  {
                   1372:                mdoc_nmsg(mdoc, n, MANDOCERR_BADWIDTH);
                   1373:                return(1);
                   1374:        }
1.131     kristaps 1375:
                   1376:        /* The value already exists: free and reallocate it. */
                   1377:
                   1378:        assert(n->args);
                   1379:
                   1380:        for (i = 0; i < (int)n->args->argc; i++)
                   1381:                if (MDOC_Width == n->args->argv[i].arg)
                   1382:                        break;
                   1383:
                   1384:        assert(i < (int)n->args->argc);
                   1385:
                   1386:        snprintf(buf, NUMSIZ, "%zun", width);
                   1387:        free(n->args->argv[i].value[0]);
                   1388:        n->args->argv[i].value[0] = mandoc_strdup(buf);
                   1389:
                   1390:        /* Set our width! */
1.147     kristaps 1391:        n->norm->Bl.width = n->args->argv[i].value[0];
1.131     kristaps 1392:        return(1);
                   1393: }
                   1394:
                   1395: static int
                   1396: post_bl_block_tag(POST_ARGS)
                   1397: {
                   1398:        struct mdoc_node *n, *nn;
                   1399:        size_t            sz, ssz;
                   1400:        int               i;
                   1401:        char              buf[NUMSIZ];
                   1402:
                   1403:        /*
                   1404:         * Calculate the -width for a `Bl -tag' list if it hasn't been
                   1405:         * provided.  Uses the first head macro.  NOTE AGAIN: this is
                   1406:         * ONLY if the -width argument has NOT been provided.  See
                   1407:         * post_bl_block_width() for converting the -width string.
                   1408:         */
                   1409:
                   1410:        sz = 10;
                   1411:        n = mdoc->last;
                   1412:
                   1413:        for (nn = n->body->child; nn; nn = nn->next) {
                   1414:                if (MDOC_It != nn->tok)
                   1415:                        continue;
                   1416:
                   1417:                assert(MDOC_BLOCK == nn->type);
                   1418:                nn = nn->head->child;
                   1419:
1.138     kristaps 1420:                if (nn == NULL)
1.131     kristaps 1421:                        break;
                   1422:
                   1423:                if (MDOC_TEXT == nn->type) {
                   1424:                        sz = strlen(nn->string) + 1;
                   1425:                        break;
                   1426:                }
                   1427:
                   1428:                if (0 != (ssz = mdoc_macro2len(nn->tok)))
                   1429:                        sz = ssz;
                   1430:
                   1431:                break;
                   1432:        }
                   1433:
                   1434:        /* Defaults to ten ens. */
                   1435:
                   1436:        snprintf(buf, NUMSIZ, "%zun", sz);
                   1437:
                   1438:        /*
                   1439:         * We have to dynamically add this to the macro's argument list.
                   1440:         * We're guaranteed that a MDOC_Width doesn't already exist.
                   1441:         */
                   1442:
                   1443:        assert(n->args);
                   1444:        i = (int)(n->args->argc)++;
                   1445:
                   1446:        n->args->argv = mandoc_realloc(n->args->argv,
                   1447:                        n->args->argc * sizeof(struct mdoc_argv));
                   1448:
                   1449:        n->args->argv[i].arg = MDOC_Width;
                   1450:        n->args->argv[i].line = n->line;
                   1451:        n->args->argv[i].pos = n->pos;
                   1452:        n->args->argv[i].sz = 1;
                   1453:        n->args->argv[i].value = mandoc_malloc(sizeof(char *));
                   1454:        n->args->argv[i].value[0] = mandoc_strdup(buf);
                   1455:
                   1456:        /* Set our width! */
1.147     kristaps 1457:        n->norm->Bl.width = n->args->argv[i].value[0];
1.131     kristaps 1458:        return(1);
                   1459: }
                   1460:
1.1       kristaps 1461:
                   1462: static int
1.14      kristaps 1463: post_bl_head(POST_ARGS)
                   1464: {
1.130     kristaps 1465:        struct mdoc_node *np, *nn, *nnp;
                   1466:        int               i, j;
1.14      kristaps 1467:
1.147     kristaps 1468:        if (LIST_column != mdoc->last->norm->Bl.type)
1.130     kristaps 1469:                /* FIXME: this should be ERROR class... */
                   1470:                return(hwarn_eq0(mdoc));
1.14      kristaps 1471:
1.130     kristaps 1472:        /*
                   1473:         * Convert old-style lists, where the column width specifiers
                   1474:         * trail as macro parameters, to the new-style ("normal-form")
                   1475:         * lists where they're argument values following -column.
                   1476:         */
                   1477:
                   1478:        /* First, disallow both types and allow normal-form. */
                   1479:
                   1480:        /*
                   1481:         * TODO: technically, we can accept both and just merge the two
                   1482:         * lists, but I'll leave that for another day.
                   1483:         */
                   1484:
1.147     kristaps 1485:        if (mdoc->last->norm->Bl.ncols && mdoc->last->nchild) {
1.130     kristaps 1486:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_COLUMNS);
                   1487:                return(0);
                   1488:        } else if (NULL == mdoc->last->child)
1.90      kristaps 1489:                return(1);
1.130     kristaps 1490:
                   1491:        np = mdoc->last->parent;
                   1492:        assert(np->args);
                   1493:
                   1494:        for (j = 0; j < (int)np->args->argc; j++)
                   1495:                if (MDOC_Column == np->args->argv[j].arg)
                   1496:                        break;
                   1497:
                   1498:        assert(j < (int)np->args->argc);
                   1499:        assert(0 == np->args->argv[j].sz);
                   1500:
                   1501:        /*
                   1502:         * Accomodate for new-style groff column syntax.  Shuffle the
                   1503:         * child nodes, all of which must be TEXT, as arguments for the
                   1504:         * column field.  Then, delete the head children.
                   1505:         */
                   1506:
                   1507:        np->args->argv[j].sz = (size_t)mdoc->last->nchild;
                   1508:        np->args->argv[j].value = mandoc_malloc
                   1509:                ((size_t)mdoc->last->nchild * sizeof(char *));
                   1510:
1.147     kristaps 1511:        mdoc->last->norm->Bl.ncols = np->args->argv[j].sz;
                   1512:        mdoc->last->norm->Bl.cols = (const char **)np->args->argv[j].value;
1.130     kristaps 1513:
                   1514:        for (i = 0, nn = mdoc->last->child; nn; i++) {
                   1515:                np->args->argv[j].value[i] = nn->string;
                   1516:                nn->string = NULL;
                   1517:                nnp = nn;
                   1518:                nn = nn->next;
                   1519:                mdoc_node_delete(NULL, nnp);
1.65      kristaps 1520:        }
1.14      kristaps 1521:
1.130     kristaps 1522:        mdoc->last->nchild = 0;
                   1523:        mdoc->last->child = NULL;
                   1524:
                   1525:        return(1);
1.14      kristaps 1526: }
                   1527:
                   1528: static int
1.1       kristaps 1529: post_bl(POST_ARGS)
                   1530: {
                   1531:        struct mdoc_node        *n;
                   1532:
1.14      kristaps 1533:        if (MDOC_HEAD == mdoc->last->type)
                   1534:                return(post_bl_head(mdoc));
1.131     kristaps 1535:        if (MDOC_BLOCK == mdoc->last->type)
                   1536:                return(post_bl_block(mdoc));
1.1       kristaps 1537:        if (MDOC_BODY != mdoc->last->type)
                   1538:                return(1);
                   1539:
                   1540:        for (n = mdoc->last->child; n; n = n->next) {
1.141     kristaps 1541:                switch (n->tok) {
1.146     schwarze 1542:                case (MDOC_Lp):
1.141     kristaps 1543:                        /* FALLTHROUGH */
                   1544:                case (MDOC_Pp):
                   1545:                        mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1.146     schwarze 1546:                        /* FALLTHROUGH */
                   1547:                case (MDOC_It):
                   1548:                        /* FALLTHROUGH */
                   1549:                case (MDOC_Sm):
1.55      kristaps 1550:                        continue;
1.141     kristaps 1551:                default:
                   1552:                        break;
                   1553:                }
                   1554:
1.79      kristaps 1555:                mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
                   1556:                return(0);
1.1       kristaps 1557:        }
                   1558:
                   1559:        return(1);
                   1560: }
                   1561:
                   1562: static int
                   1563: ebool(struct mdoc *mdoc)
                   1564: {
                   1565:
1.151     schwarze 1566:        if (NULL == mdoc->last->child) {
                   1567:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
                   1568:                mdoc_node_delete(mdoc, mdoc->last);
1.133     kristaps 1569:                return(1);
1.151     schwarze 1570:        }
                   1571:        check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
1.133     kristaps 1572:
                   1573:        assert(MDOC_TEXT == mdoc->last->child->type);
1.1       kristaps 1574:
1.133     kristaps 1575:        if (0 == strcmp(mdoc->last->child->string, "on"))
1.1       kristaps 1576:                return(1);
1.133     kristaps 1577:        if (0 == strcmp(mdoc->last->child->string, "off"))
                   1578:                return(1);
                   1579:
                   1580:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADBOOL);
                   1581:        return(1);
1.1       kristaps 1582: }
                   1583:
                   1584: static int
                   1585: post_root(POST_ARGS)
                   1586: {
1.133     kristaps 1587:        int               erc;
                   1588:        struct mdoc_node *n;
1.1       kristaps 1589:
1.133     kristaps 1590:        erc = 0;
                   1591:
                   1592:        /* Check that we have a finished prologue. */
                   1593:
                   1594:        if ( ! (MDOC_PBODY & mdoc->flags)) {
                   1595:                erc++;
1.79      kristaps 1596:                mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
1.133     kristaps 1597:        }
                   1598:
                   1599:        n = mdoc->first;
                   1600:        assert(n);
                   1601:
                   1602:        /* Check that we begin with a proper `Sh'. */
                   1603:
                   1604:        if (NULL == n->child) {
                   1605:                erc++;
                   1606:                mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
                   1607:        } else if (MDOC_BLOCK != n->child->type ||
                   1608:                        MDOC_Sh != n->child->tok) {
                   1609:                erc++;
                   1610:                /* Can this be lifted?  See rxdebug.1 for example. */
                   1611:                mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
                   1612:        }
1.1       kristaps 1613:
1.133     kristaps 1614:        return(erc ? 0 : 1);
1.1       kristaps 1615: }
                   1616:
                   1617: static int
                   1618: post_st(POST_ARGS)
                   1619: {
1.151     schwarze 1620:        struct mdoc_node         *ch;
                   1621:        const char               *p;
1.128     kristaps 1622:
1.151     schwarze 1623:        if (NULL == (ch = mdoc->last->child)) {
                   1624:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
                   1625:                mdoc_node_delete(mdoc, mdoc->last);
                   1626:                return(1);
                   1627:        }
1.128     kristaps 1628:
1.151     schwarze 1629:        assert(MDOC_TEXT == ch->type);
1.128     kristaps 1630:
1.151     schwarze 1631:        if (NULL == (p = mdoc_a2st(ch->string))) {
1.128     kristaps 1632:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD);
                   1633:                mdoc_node_delete(mdoc, mdoc->last);
                   1634:        } else {
1.151     schwarze 1635:                free(ch->string);
                   1636:                ch->string = mandoc_strdup(p);
1.128     kristaps 1637:        }
1.1       kristaps 1638:
1.128     kristaps 1639:        return(1);
1.1       kristaps 1640: }
                   1641:
                   1642: static int
1.43      kristaps 1643: post_rs(POST_ARGS)
                   1644: {
1.122     kristaps 1645:        struct mdoc_node *nn, *next, *prev;
                   1646:        int               i, j;
1.43      kristaps 1647:
1.151     schwarze 1648:        switch (mdoc->last->type) {
                   1649:        case (MDOC_HEAD):
                   1650:                check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0);
                   1651:                return(1);
                   1652:        case (MDOC_BODY):
                   1653:                if (mdoc->last->child)
                   1654:                        break;
                   1655:                check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0);
                   1656:                return(1);
                   1657:        default:
1.43      kristaps 1658:                return(1);
1.151     schwarze 1659:        }
1.43      kristaps 1660:
1.122     kristaps 1661:        /*
                   1662:         * Make sure only certain types of nodes are allowed within the
                   1663:         * the `Rs' body.  Delete offending nodes and raise a warning.
                   1664:         * Do this before re-ordering for the sake of clarity.
                   1665:         */
                   1666:
                   1667:        next = NULL;
                   1668:        for (nn = mdoc->last->child; nn; nn = next) {
                   1669:                for (i = 0; i < RSORD_MAX; i++)
                   1670:                        if (nn->tok == rsord[i])
                   1671:                                break;
                   1672:
                   1673:                if (i < RSORD_MAX) {
1.153     kristaps 1674:                        if (MDOC__J == rsord[i] || MDOC__B == rsord[i])
                   1675:                                mdoc->last->norm->Rs.quote_T++;
1.122     kristaps 1676:                        next = nn->next;
                   1677:                        continue;
                   1678:                }
                   1679:
                   1680:                next = nn->next;
                   1681:                mdoc_nmsg(mdoc, nn, MANDOCERR_CHILD);
                   1682:                mdoc_node_delete(mdoc, nn);
                   1683:        }
                   1684:
                   1685:        /*
                   1686:         * The full `Rs' block needs special handling to order the
                   1687:         * sub-elements according to `rsord'.  Pick through each element
                   1688:         * and correctly order it.  This is a insertion sort.
                   1689:         */
                   1690:
                   1691:        next = NULL;
                   1692:        for (nn = mdoc->last->child->next; nn; nn = next) {
                   1693:                /* Determine order of `nn'. */
                   1694:                for (i = 0; i < RSORD_MAX; i++)
                   1695:                        if (rsord[i] == nn->tok)
                   1696:                                break;
                   1697:
                   1698:                /*
                   1699:                 * Remove `nn' from the chain.  This somewhat
                   1700:                 * repeats mdoc_node_unlink(), but since we're
                   1701:                 * just re-ordering, there's no need for the
                   1702:                 * full unlink process.
                   1703:                 */
                   1704:
                   1705:                if (NULL != (next = nn->next))
                   1706:                        next->prev = nn->prev;
                   1707:
                   1708:                if (NULL != (prev = nn->prev))
                   1709:                        prev->next = nn->next;
                   1710:
                   1711:                nn->prev = nn->next = NULL;
                   1712:
                   1713:                /*
                   1714:                 * Scan back until we reach a node that's
                   1715:                 * ordered before `nn'.
                   1716:                 */
                   1717:
                   1718:                for ( ; prev ; prev = prev->prev) {
                   1719:                        /* Determine order of `prev'. */
                   1720:                        for (j = 0; j < RSORD_MAX; j++)
                   1721:                                if (rsord[j] == prev->tok)
                   1722:                                        break;
                   1723:
                   1724:                        if (j <= i)
                   1725:                                break;
                   1726:                }
                   1727:
                   1728:                /*
                   1729:                 * Set `nn' back into its correct place in front
                   1730:                 * of the `prev' node.
                   1731:                 */
                   1732:
                   1733:                nn->prev = prev;
                   1734:
                   1735:                if (prev) {
                   1736:                        if (prev->next)
                   1737:                                prev->next->prev = nn;
                   1738:                        nn->next = prev->next;
                   1739:                        prev->next = nn;
                   1740:                } else {
                   1741:                        mdoc->last->child->prev = nn;
                   1742:                        nn->next = mdoc->last->child;
                   1743:                        mdoc->last->child = nn;
1.43      kristaps 1744:                }
1.122     kristaps 1745:        }
1.43      kristaps 1746:
1.155     kristaps 1747:        return(1);
                   1748: }
                   1749:
                   1750: static int
                   1751: post_ns(POST_ARGS)
                   1752: {
                   1753:
                   1754:        if (MDOC_LINE & mdoc->last->flags)
                   1755:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNNS);
1.43      kristaps 1756:        return(1);
                   1757: }
                   1758:
                   1759: static int
1.1       kristaps 1760: post_sh(POST_ARGS)
                   1761: {
                   1762:
                   1763:        if (MDOC_HEAD == mdoc->last->type)
                   1764:                return(post_sh_head(mdoc));
                   1765:        if (MDOC_BODY == mdoc->last->type)
                   1766:                return(post_sh_body(mdoc));
                   1767:
                   1768:        return(1);
                   1769: }
                   1770:
                   1771: static int
                   1772: post_sh_body(POST_ARGS)
                   1773: {
                   1774:        struct mdoc_node *n;
                   1775:
1.29      kristaps 1776:        if (SEC_NAME != mdoc->lastsec)
1.1       kristaps 1777:                return(1);
                   1778:
                   1779:        /*
                   1780:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1781:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1782:         * children of the BODY declaration can also be "text".
                   1783:         */
                   1784:
1.133     kristaps 1785:        if (NULL == (n = mdoc->last->child)) {
                   1786:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
                   1787:                return(1);
                   1788:        }
1.1       kristaps 1789:
                   1790:        for ( ; n && n->next; n = n->next) {
                   1791:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1792:                        continue;
1.28      kristaps 1793:                if (MDOC_TEXT == n->type)
                   1794:                        continue;
1.133     kristaps 1795:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1.1       kristaps 1796:        }
                   1797:
1.41      kristaps 1798:        assert(n);
1.27      kristaps 1799:        if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1       kristaps 1800:                return(1);
1.133     kristaps 1801:
                   1802:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
                   1803:        return(1);
1.1       kristaps 1804: }
                   1805:
                   1806: static int
                   1807: post_sh_head(POST_ARGS)
                   1808: {
1.132     kristaps 1809:        char             buf[BUFSIZ];
                   1810:        enum mdoc_sec    sec;
1.1       kristaps 1811:
                   1812:        /*
                   1813:         * Process a new section.  Sections are either "named" or
1.125     kristaps 1814:         * "custom".  Custom sections are user-defined, while named ones
                   1815:         * follow a conventional order and may only appear in certain
                   1816:         * manual sections.
1.1       kristaps 1817:         */
                   1818:
1.132     kristaps 1819:        if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
                   1820:                return(0);
1.1       kristaps 1821:
1.72      kristaps 1822:        sec = mdoc_str2sec(buf);
1.1       kristaps 1823:
1.125     kristaps 1824:        /* The NAME should be first. */
1.1       kristaps 1825:
1.72      kristaps 1826:        if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1.125     kristaps 1827:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST);
                   1828:
                   1829:        /* The SYNOPSIS gets special attention in other areas. */
                   1830:
                   1831:        if (SEC_SYNOPSIS == sec)
                   1832:                mdoc->flags |= MDOC_SYNOPSIS;
                   1833:        else
                   1834:                mdoc->flags &= ~MDOC_SYNOPSIS;
                   1835:
                   1836:        /* Mark our last section. */
                   1837:
                   1838:        mdoc->lastsec = sec;
                   1839:
                   1840:        /* We don't care about custom sections after this. */
1.72      kristaps 1841:
1.1       kristaps 1842:        if (SEC_CUSTOM == sec)
                   1843:                return(1);
1.72      kristaps 1844:
1.125     kristaps 1845:        /*
                   1846:         * Check whether our non-custom section is being repeated or is
                   1847:         * out of order.
                   1848:         */
                   1849:
1.1       kristaps 1850:        if (sec == mdoc->lastnamed)
1.125     kristaps 1851:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP);
1.72      kristaps 1852:
1.1       kristaps 1853:        if (sec < mdoc->lastnamed)
1.125     kristaps 1854:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO);
                   1855:
                   1856:        /* Mark the last named section. */
                   1857:
                   1858:        mdoc->lastnamed = sec;
                   1859:
                   1860:        /* Check particular section/manual conventions. */
1.1       kristaps 1861:
1.125     kristaps 1862:        assert(mdoc->meta.msec);
1.1       kristaps 1863:
                   1864:        switch (sec) {
1.125     kristaps 1865:        case (SEC_RETURN_VALUES):
                   1866:                /* FALLTHROUGH */
                   1867:        case (SEC_ERRORS):
                   1868:                /* FALLTHROUGH */
1.1       kristaps 1869:        case (SEC_LIBRARY):
1.78      kristaps 1870:                if (*mdoc->meta.msec == '2')
                   1871:                        break;
                   1872:                if (*mdoc->meta.msec == '3')
                   1873:                        break;
                   1874:                if (*mdoc->meta.msec == '9')
1.1       kristaps 1875:                        break;
1.125     kristaps 1876:                mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC);
                   1877:                break;
1.1       kristaps 1878:        default:
                   1879:                break;
                   1880:        }
1.140     kristaps 1881:
                   1882:        return(1);
                   1883: }
                   1884:
                   1885: static int
                   1886: post_ignpar(POST_ARGS)
                   1887: {
                   1888:        struct mdoc_node *np;
                   1889:
                   1890:        if (MDOC_BODY != mdoc->last->type)
                   1891:                return(1);
                   1892:
1.143     kristaps 1893:        if (NULL != (np = mdoc->last->child))
1.140     kristaps 1894:                if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
                   1895:                        mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
                   1896:                        mdoc_node_delete(mdoc, np);
                   1897:                }
                   1898:
                   1899:        if (NULL != (np = mdoc->last->last))
                   1900:                if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
                   1901:                        mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
                   1902:                        mdoc_node_delete(mdoc, np);
                   1903:                }
1.1       kristaps 1904:
1.117     kristaps 1905:        return(1);
                   1906: }
                   1907:
                   1908: static int
1.124     kristaps 1909: pre_par(PRE_ARGS)
1.117     kristaps 1910: {
                   1911:
1.118     kristaps 1912:        if (NULL == mdoc->last)
                   1913:                return(1);
1.141     kristaps 1914:        if (MDOC_ELEM != n->type && MDOC_BLOCK != n->type)
                   1915:                return(1);
1.118     kristaps 1916:
1.124     kristaps 1917:        /*
                   1918:         * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
                   1919:         * block:  `Lp', `Pp', or non-compact `Bd' or `Bl'.
                   1920:         */
1.118     kristaps 1921:
                   1922:        if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
1.117     kristaps 1923:                return(1);
1.147     kristaps 1924:        if (MDOC_Bl == n->tok && n->norm->Bl.comp)
1.117     kristaps 1925:                return(1);
1.147     kristaps 1926:        if (MDOC_Bd == n->tok && n->norm->Bd.comp)
1.141     kristaps 1927:                return(1);
1.147     kristaps 1928:        if (MDOC_It == n->tok && n->parent->norm->Bl.comp)
1.117     kristaps 1929:                return(1);
                   1930:
                   1931:        mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
                   1932:        mdoc_node_delete(mdoc, mdoc->last);
1.128     kristaps 1933:        return(1);
                   1934: }
                   1935:
                   1936: static int
                   1937: pre_literal(PRE_ARGS)
                   1938: {
                   1939:
                   1940:        if (MDOC_BODY != n->type)
                   1941:                return(1);
                   1942:
                   1943:        /*
                   1944:         * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
                   1945:         * -unfilled' macros set MDOC_LITERAL on entrance to the body.
                   1946:         */
                   1947:
                   1948:        switch (n->tok) {
                   1949:        case (MDOC_Dl):
                   1950:                mdoc->flags |= MDOC_LITERAL;
                   1951:                break;
                   1952:        case (MDOC_Bd):
1.147     kristaps 1953:                if (DISP_literal == n->norm->Bd.type)
1.128     kristaps 1954:                        mdoc->flags |= MDOC_LITERAL;
1.147     kristaps 1955:                if (DISP_unfilled == n->norm->Bd.type)
1.128     kristaps 1956:                        mdoc->flags |= MDOC_LITERAL;
                   1957:                break;
                   1958:        default:
                   1959:                abort();
                   1960:                /* NOTREACHED */
                   1961:        }
                   1962:
1.1       kristaps 1963:        return(1);
                   1964: }
1.132     kristaps 1965:
                   1966: static int
                   1967: post_dd(POST_ARGS)
                   1968: {
1.135     kristaps 1969:        char              buf[DATESIZE];
1.132     kristaps 1970:        struct mdoc_node *n;
                   1971:
                   1972:        n = mdoc->last;
                   1973:
                   1974:        if (NULL == n->child) {
                   1975:                mdoc->meta.date = time(NULL);
1.133     kristaps 1976:                return(1);
1.132     kristaps 1977:        }
                   1978:
1.135     kristaps 1979:        if ( ! concat(mdoc, buf, n->child, DATESIZE))
1.132     kristaps 1980:                return(0);
                   1981:
                   1982:        mdoc->meta.date = mandoc_a2time
                   1983:                (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
                   1984:
                   1985:        if (0 == mdoc->meta.date) {
1.133     kristaps 1986:                mdoc_nmsg(mdoc, n, MANDOCERR_BADDATE);
1.132     kristaps 1987:                mdoc->meta.date = time(NULL);
                   1988:        }
                   1989:
                   1990:        return(1);
                   1991: }
                   1992:
                   1993: static int
                   1994: post_dt(POST_ARGS)
                   1995: {
                   1996:        struct mdoc_node *nn, *n;
                   1997:        const char       *cp;
                   1998:        char             *p;
                   1999:
                   2000:        n = mdoc->last;
                   2001:
                   2002:        if (mdoc->meta.title)
                   2003:                free(mdoc->meta.title);
                   2004:        if (mdoc->meta.vol)
                   2005:                free(mdoc->meta.vol);
                   2006:        if (mdoc->meta.arch)
                   2007:                free(mdoc->meta.arch);
                   2008:
                   2009:        mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
                   2010:
                   2011:        /* First make all characters uppercase. */
                   2012:
                   2013:        if (NULL != (nn = n->child))
                   2014:                for (p = nn->string; *p; p++) {
                   2015:                        if (toupper((u_char)*p) == *p)
                   2016:                                continue;
1.133     kristaps 2017:
                   2018:                        /*
                   2019:                         * FIXME: don't be lazy: have this make all
                   2020:                         * characters be uppercase and just warn once.
                   2021:                         */
                   2022:                        mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE);
1.132     kristaps 2023:                        break;
                   2024:                }
                   2025:
                   2026:        /* Handles: `.Dt'
                   2027:         *   --> title = unknown, volume = local, msec = 0, arch = NULL
                   2028:         */
                   2029:
                   2030:        if (NULL == (nn = n->child)) {
                   2031:                /* XXX: make these macro values. */
                   2032:                /* FIXME: warn about missing values. */
                   2033:                mdoc->meta.title = mandoc_strdup("UNKNOWN");
                   2034:                mdoc->meta.vol = mandoc_strdup("LOCAL");
                   2035:                mdoc->meta.msec = mandoc_strdup("1");
                   2036:                return(1);
                   2037:        }
                   2038:
                   2039:        /* Handles: `.Dt TITLE'
                   2040:         *   --> title = TITLE, volume = local, msec = 0, arch = NULL
                   2041:         */
                   2042:
                   2043:        mdoc->meta.title = mandoc_strdup
                   2044:                ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
                   2045:
                   2046:        if (NULL == (nn = nn->next)) {
                   2047:                /* FIXME: warn about missing msec. */
                   2048:                /* XXX: make this a macro value. */
                   2049:                mdoc->meta.vol = mandoc_strdup("LOCAL");
                   2050:                mdoc->meta.msec = mandoc_strdup("1");
                   2051:                return(1);
                   2052:        }
                   2053:
                   2054:        /* Handles: `.Dt TITLE SEC'
                   2055:         *   --> title = TITLE, volume = SEC is msec ?
                   2056:         *           format(msec) : SEC,
                   2057:         *       msec = SEC is msec ? atoi(msec) : 0,
                   2058:         *       arch = NULL
                   2059:         */
                   2060:
                   2061:        cp = mdoc_a2msec(nn->string);
                   2062:        if (cp) {
                   2063:                mdoc->meta.vol = mandoc_strdup(cp);
                   2064:                mdoc->meta.msec = mandoc_strdup(nn->string);
1.133     kristaps 2065:        } else {
                   2066:                mdoc_nmsg(mdoc, n, MANDOCERR_BADMSEC);
1.132     kristaps 2067:                mdoc->meta.vol = mandoc_strdup(nn->string);
                   2068:                mdoc->meta.msec = mandoc_strdup(nn->string);
1.133     kristaps 2069:        }
1.132     kristaps 2070:
                   2071:        if (NULL == (nn = nn->next))
                   2072:                return(1);
                   2073:
                   2074:        /* Handles: `.Dt TITLE SEC VOL'
                   2075:         *   --> title = TITLE, volume = VOL is vol ?
                   2076:         *       format(VOL) :
                   2077:         *           VOL is arch ? format(arch) :
                   2078:         *               VOL
                   2079:         */
                   2080:
                   2081:        cp = mdoc_a2vol(nn->string);
                   2082:        if (cp) {
                   2083:                free(mdoc->meta.vol);
                   2084:                mdoc->meta.vol = mandoc_strdup(cp);
                   2085:        } else {
                   2086:                /* FIXME: warn about bad arch. */
                   2087:                cp = mdoc_a2arch(nn->string);
                   2088:                if (NULL == cp) {
                   2089:                        free(mdoc->meta.vol);
                   2090:                        mdoc->meta.vol = mandoc_strdup(nn->string);
                   2091:                } else
                   2092:                        mdoc->meta.arch = mandoc_strdup(cp);
                   2093:        }
                   2094:
                   2095:        /* Ignore any subsequent parameters... */
                   2096:        /* FIXME: warn about subsequent parameters. */
                   2097:
                   2098:        return(1);
                   2099: }
                   2100:
                   2101: static int
                   2102: post_prol(POST_ARGS)
                   2103: {
                   2104:        /*
                   2105:         * Remove prologue macros from the document after they're
                   2106:         * processed.  The final document uses mdoc_meta for these
                   2107:         * values and discards the originals.
                   2108:         */
                   2109:
                   2110:        mdoc_node_delete(mdoc, mdoc->last);
                   2111:        if (mdoc->meta.title && mdoc->meta.date && mdoc->meta.os)
                   2112:                mdoc->flags |= MDOC_PBODY;
1.154     kristaps 2113:
                   2114:        return(1);
                   2115: }
                   2116:
                   2117: static int
                   2118: post_bx(POST_ARGS)
                   2119: {
                   2120:        struct mdoc_node        *n;
                   2121:
                   2122:        /*
                   2123:         * Make `Bx's second argument always start with an uppercase
                   2124:         * letter.  Groff checks if it's an "accepted" term, but we just
                   2125:         * uppercase blindly.
                   2126:         */
                   2127:
                   2128:        n = mdoc->last->child;
                   2129:        if (n && NULL != (n = n->next))
                   2130:                *n->string = toupper((unsigned char)*n->string);
1.132     kristaps 2131:
                   2132:        return(1);
                   2133: }
                   2134:
                   2135: static int
                   2136: post_os(POST_ARGS)
                   2137: {
                   2138:        struct mdoc_node *n;
                   2139:        char              buf[BUFSIZ];
                   2140: #ifndef OSNAME
                   2141:        struct utsname    utsname;
                   2142: #endif
                   2143:
                   2144:        n = mdoc->last;
                   2145:
                   2146:        /*
                   2147:         * Set the operating system by way of the `Os' macro.  Note that
                   2148:         * if an argument isn't provided and -DOSNAME="\"foo\"" is
                   2149:         * provided during compilation, this value will be used instead
                   2150:         * of filling in "sysname release" from uname().
                   2151:         */
                   2152:
                   2153:        if (mdoc->meta.os)
                   2154:                free(mdoc->meta.os);
                   2155:
                   2156:        if ( ! concat(mdoc, buf, n->child, BUFSIZ))
                   2157:                return(0);
                   2158:
                   2159:        /* XXX: yes, these can all be dynamically-adjusted buffers, but
                   2160:         * it's really not worth the extra hackery.
                   2161:         */
                   2162:
                   2163:        if ('\0' == buf[0]) {
                   2164: #ifdef OSNAME
                   2165:                if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
                   2166:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2167:                        return(0);
                   2168:                }
                   2169: #else /*!OSNAME */
1.136     kristaps 2170:                if (uname(&utsname)) {
                   2171:                        mdoc_nmsg(mdoc, n, MANDOCERR_UNAME);
                   2172:                         mdoc->meta.os = mandoc_strdup("UNKNOWN");
                   2173:                         return(post_prol(mdoc));
                   2174:                 }
1.132     kristaps 2175:
                   2176:                if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
                   2177:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2178:                        return(0);
                   2179:                }
1.136     kristaps 2180:                if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
1.132     kristaps 2181:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2182:                        return(0);
                   2183:                }
                   2184:                if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
                   2185:                        mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
                   2186:                        return(0);
                   2187:                }
                   2188: #endif /*!OSNAME*/
                   2189:        }
                   2190:
                   2191:        mdoc->meta.os = mandoc_strdup(buf);
                   2192:        return(1);
                   2193: }
                   2194:
                   2195: static int
                   2196: post_std(POST_ARGS)
                   2197: {
                   2198:        struct mdoc_node *nn, *n;
                   2199:
                   2200:        n = mdoc->last;
                   2201:
                   2202:        /*
                   2203:         * Macros accepting `-std' as an argument have the name of the
                   2204:         * current document (`Nm') filled in as the argument if it's not
                   2205:         * provided.
                   2206:         */
                   2207:
                   2208:        if (n->child)
                   2209:                return(1);
1.133     kristaps 2210:
1.132     kristaps 2211:        if (NULL == mdoc->meta.name)
                   2212:                return(1);
                   2213:
                   2214:        nn = n;
                   2215:        mdoc->next = MDOC_NEXT_CHILD;
                   2216:
                   2217:        if ( ! mdoc_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name))
                   2218:                return(0);
1.133     kristaps 2219:
1.132     kristaps 2220:        mdoc->last = nn;
                   2221:        return(1);
                   2222: }
                   2223:
                   2224: static int
                   2225: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
                   2226: {
                   2227:
                   2228:        p[0] = '\0';
                   2229:
                   2230:        /*
                   2231:         * Concatenate sibling nodes together.  All siblings must be of
                   2232:         * type MDOC_TEXT or an assertion is raised.  Concatenation is
1.133     kristaps 2233:         * separated by a single whitespace.  Returns 0 on fatal (string
                   2234:         * overrun) error.
1.132     kristaps 2235:         */
                   2236:
                   2237:        for ( ; n; n = n->next) {
                   2238:                assert(MDOC_TEXT == n->type);
                   2239:
                   2240:                if (strlcat(p, n->string, sz) >= sz) {
                   2241:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                   2242:                        return(0);
                   2243:                }
                   2244:
                   2245:                if (NULL == n->next)
                   2246:                        continue;
                   2247:
                   2248:                if (strlcat(p, " ", sz) >= sz) {
                   2249:                        mdoc_nmsg(m, n, MANDOCERR_MEM);
                   2250:                        return(0);
                   2251:                }
                   2252:        }
                   2253:
                   2254:        return(1);
                   2255: }
                   2256:

CVSweb