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

Annotation of mandoc/mdoc_validate.c, Revision 1.155

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

CVSweb