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

Annotation of mandoc/mdoc_validate.c, Revision 1.76

1.76    ! kristaps    1: /*     $Id: mdoc_validate.c,v 1.75 2010/05/14 16:02:29 kristaps Exp $ */
1.1       kristaps    2: /*
1.7       kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.5       kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.5       kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.56      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.1       kristaps   21: #include <sys/types.h>
                     22:
                     23: #include <assert.h>
                     24: #include <ctype.h>
1.35      kristaps   25: #include <limits.h>
1.1       kristaps   26: #include <stdarg.h>
                     27: #include <stdlib.h>
1.2       kristaps   28: #include <string.h>
1.1       kristaps   29:
                     30: #include "libmdoc.h"
1.18      kristaps   31: #include "libmandoc.h"
1.1       kristaps   32:
                     33: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
                     34: /* TODO: ignoring Pp (it's superfluous in some invocations). */
                     35:
1.32      kristaps   36: #define        PRE_ARGS  struct mdoc *mdoc, const struct mdoc_node *n
                     37: #define        POST_ARGS struct mdoc *mdoc
1.1       kristaps   38:
                     39: typedef        int     (*v_pre)(PRE_ARGS);
                     40: typedef        int     (*v_post)(POST_ARGS);
                     41:
                     42: struct valids {
                     43:        v_pre   *pre;
                     44:        v_post  *post;
                     45: };
                     46:
1.59      kristaps   47: static int      check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.32      kristaps   48: static int      check_sec(PRE_ARGS, ...);
                     49: static int      check_stdarg(PRE_ARGS);
                     50: static int      check_text(struct mdoc *, int, int, const char *);
                     51: static int      check_argv(struct mdoc *,
1.1       kristaps   52:                        const struct mdoc_node *,
                     53:                        const struct mdoc_argv *);
1.32      kristaps   54: static int      check_args(struct mdoc *,
1.1       kristaps   55:                        const struct mdoc_node *);
1.32      kristaps   56: static int      err_child_lt(struct mdoc *, const char *, int);
                     57: static int      warn_child_lt(struct mdoc *, const char *, int);
                     58: static int      err_child_gt(struct mdoc *, const char *, int);
                     59: static int      warn_child_gt(struct mdoc *, const char *, int);
                     60: static int      err_child_eq(struct mdoc *, const char *, int);
                     61: static int      warn_child_eq(struct mdoc *, const char *, int);
                     62: static int      warn_count(struct mdoc *, const char *,
1.1       kristaps   63:                        int, const char *, int);
1.32      kristaps   64: static int      err_count(struct mdoc *, const char *,
1.1       kristaps   65:                        int, const char *, int);
1.32      kristaps   66:
                     67: static int      berr_ge1(POST_ARGS);
                     68: static int      bwarn_ge1(POST_ARGS);
                     69: static int      ebool(POST_ARGS);
                     70: static int      eerr_eq0(POST_ARGS);
                     71: static int      eerr_eq1(POST_ARGS);
                     72: static int      eerr_ge1(POST_ARGS);
1.46      kristaps   73: static int      eerr_le1(POST_ARGS);
1.32      kristaps   74: static int      ewarn_ge1(POST_ARGS);
                     75: static int      herr_eq0(POST_ARGS);
                     76: static int      herr_ge1(POST_ARGS);
                     77: static int      hwarn_eq1(POST_ARGS);
1.66      kristaps   78: static int      hwarn_eq0(POST_ARGS);
1.32      kristaps   79: static int      hwarn_le1(POST_ARGS);
                     80:
                     81: static int      post_an(POST_ARGS);
                     82: static int      post_at(POST_ARGS);
                     83: static int      post_bf(POST_ARGS);
                     84: static int      post_bl(POST_ARGS);
                     85: static int      post_bl_head(POST_ARGS);
                     86: static int      post_it(POST_ARGS);
                     87: static int      post_lb(POST_ARGS);
                     88: static int      post_nm(POST_ARGS);
                     89: static int      post_root(POST_ARGS);
1.43      kristaps   90: static int      post_rs(POST_ARGS);
1.32      kristaps   91: static int      post_sh(POST_ARGS);
                     92: static int      post_sh_body(POST_ARGS);
                     93: static int      post_sh_head(POST_ARGS);
                     94: static int      post_st(POST_ARGS);
1.57      kristaps   95: static int      post_vt(POST_ARGS);
1.32      kristaps   96: static int      pre_an(PRE_ARGS);
                     97: static int      pre_bd(PRE_ARGS);
                     98: static int      pre_bl(PRE_ARGS);
                     99: static int      pre_dd(PRE_ARGS);
                    100: static int      pre_display(PRE_ARGS);
                    101: static int      pre_dt(PRE_ARGS);
                    102: static int      pre_fd(PRE_ARGS);
                    103: static int      pre_it(PRE_ARGS);
                    104: static int      pre_lb(PRE_ARGS);
                    105: static int      pre_os(PRE_ARGS);
                    106: static int      pre_rv(PRE_ARGS);
                    107: static int      pre_sh(PRE_ARGS);
                    108: static int      pre_ss(PRE_ARGS);
                    109:
                    110: static v_post   posts_an[] = { post_an, NULL };
                    111: static v_post   posts_at[] = { post_at, NULL };
1.66      kristaps  112: static v_post   posts_bd[] = { hwarn_eq0, bwarn_ge1, NULL };
1.32      kristaps  113: static v_post   posts_bf[] = { hwarn_le1, post_bf, NULL };
                    114: static v_post   posts_bl[] = { bwarn_ge1, post_bl, NULL };
                    115: static v_post   posts_bool[] = { eerr_eq1, ebool, NULL };
                    116: static v_post   posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
                    117: static v_post   posts_it[] = { post_it, NULL };
                    118: static v_post   posts_lb[] = { eerr_eq1, post_lb, NULL };
                    119: static v_post   posts_nd[] = { berr_ge1, NULL };
                    120: static v_post   posts_nm[] = { post_nm, NULL };
                    121: static v_post   posts_notext[] = { eerr_eq0, NULL };
1.44      kristaps  122: static v_post   posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
1.32      kristaps  123: static v_post   posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
1.46      kristaps  124: static v_post   posts_sp[] = { eerr_le1, NULL };
1.32      kristaps  125: static v_post   posts_ss[] = { herr_ge1, NULL };
                    126: static v_post   posts_st[] = { eerr_eq1, post_st, NULL };
                    127: static v_post   posts_text[] = { eerr_ge1, NULL };
1.51      kristaps  128: static v_post   posts_text1[] = { eerr_eq1, NULL };
1.57      kristaps  129: static v_post   posts_vt[] = { post_vt, NULL };
1.32      kristaps  130: static v_post   posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
                    131: static v_post   posts_wtext[] = { ewarn_ge1, NULL };
1.63      kristaps  132: static v_post   posts_xr[] = { ewarn_ge1, NULL };
1.32      kristaps  133: static v_pre    pres_an[] = { pre_an, NULL };
                    134: static v_pre    pres_bd[] = { pre_display, pre_bd, NULL };
                    135: static v_pre    pres_bl[] = { pre_bl, NULL };
                    136: static v_pre    pres_d1[] = { pre_display, NULL };
                    137: static v_pre    pres_dd[] = { pre_dd, NULL };
                    138: static v_pre    pres_dt[] = { pre_dt, NULL };
1.67      kristaps  139: static v_pre    pres_er[] = { NULL, NULL };
1.76    ! kristaps  140: static v_pre    pres_ex[] = { NULL, NULL };
1.32      kristaps  141: static v_pre    pres_fd[] = { pre_fd, NULL };
                    142: static v_pre    pres_it[] = { pre_it, NULL };
                    143: static v_pre    pres_lb[] = { pre_lb, NULL };
                    144: static v_pre    pres_os[] = { pre_os, NULL };
                    145: static v_pre    pres_rv[] = { pre_rv, NULL };
                    146: static v_pre    pres_sh[] = { pre_sh, NULL };
                    147: static v_pre    pres_ss[] = { pre_ss, NULL };
1.1       kristaps  148:
                    149: const  struct valids mdoc_valids[MDOC_MAX] = {
1.10      kristaps  150:        { NULL, NULL },                         /* Ap */
1.1       kristaps  151:        { pres_dd, posts_text },                /* Dd */
                    152:        { pres_dt, NULL },                      /* Dt */
                    153:        { pres_os, NULL },                      /* Os */
                    154:        { pres_sh, posts_sh },                  /* Sh */
                    155:        { pres_ss, posts_ss },                  /* Ss */
1.34      kristaps  156:        { NULL, posts_notext },                 /* Pp */
1.1       kristaps  157:        { pres_d1, posts_wline },               /* D1 */
                    158:        { pres_d1, posts_wline },               /* Dl */
                    159:        { pres_bd, posts_bd },                  /* Bd */
                    160:        { NULL, NULL },                         /* Ed */
                    161:        { pres_bl, posts_bl },                  /* Bl */
                    162:        { NULL, NULL },                         /* El */
                    163:        { pres_it, posts_it },                  /* It */
                    164:        { NULL, posts_text },                   /* Ad */
                    165:        { pres_an, posts_an },                  /* An */
                    166:        { NULL, NULL },                         /* Ar */
1.69      kristaps  167:        { NULL, posts_text },                   /* Cd */
1.1       kristaps  168:        { NULL, NULL },                         /* Cm */
                    169:        { NULL, NULL },                         /* Dv */
                    170:        { pres_er, posts_text },                /* Er */
                    171:        { NULL, NULL },                         /* Ev */
1.42      kristaps  172:        { pres_ex, NULL },                      /* Ex */
1.1       kristaps  173:        { NULL, NULL },                         /* Fa */
                    174:        { pres_fd, posts_wtext },               /* Fd */
                    175:        { NULL, NULL },                         /* Fl */
                    176:        { NULL, posts_text },                   /* Fn */
                    177:        { NULL, posts_wtext },                  /* Ft */
                    178:        { NULL, posts_text },                   /* Ic */
1.51      kristaps  179:        { NULL, posts_text1 },                  /* In */
1.1       kristaps  180:        { NULL, NULL },                         /* Li */
1.27      kristaps  181:        { NULL, posts_nd },                     /* Nd */
1.1       kristaps  182:        { NULL, posts_nm },                     /* Nm */
                    183:        { NULL, posts_wline },                  /* Op */
                    184:        { NULL, NULL },                         /* Ot */
                    185:        { NULL, NULL },                         /* Pa */
1.42      kristaps  186:        { pres_rv, NULL },                      /* Rv */
1.1       kristaps  187:        { NULL, posts_st },                     /* St */
                    188:        { NULL, NULL },                         /* Va */
1.57      kristaps  189:        { NULL, posts_vt },                     /* Vt */
1.1       kristaps  190:        { NULL, posts_xr },                     /* Xr */
                    191:        { NULL, posts_text },                   /* %A */
1.47      kristaps  192:        { NULL, posts_text },                   /* %B */ /* FIXME: can be used outside Rs/Re. */
1.54      kristaps  193:        { NULL, posts_text },                   /* %D */ /* FIXME: check date with mandoc_a2time(). */
1.1       kristaps  194:        { NULL, posts_text },                   /* %I */
                    195:        { NULL, posts_text },                   /* %J */
                    196:        { NULL, posts_text },                   /* %N */
                    197:        { NULL, posts_text },                   /* %O */
                    198:        { NULL, posts_text },                   /* %P */
                    199:        { NULL, posts_text },                   /* %R */
1.47      kristaps  200:        { NULL, posts_text },                   /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1       kristaps  201:        { NULL, posts_text },                   /* %V */
                    202:        { NULL, NULL },                         /* Ac */
                    203:        { NULL, NULL },                         /* Ao */
                    204:        { NULL, posts_wline },                  /* Aq */
                    205:        { NULL, posts_at },                     /* At */
                    206:        { NULL, NULL },                         /* Bc */
                    207:        { NULL, posts_bf },                     /* Bf */
                    208:        { NULL, NULL },                         /* Bo */
                    209:        { NULL, posts_wline },                  /* Bq */
                    210:        { NULL, NULL },                         /* Bsx */
                    211:        { NULL, NULL },                         /* Bx */
                    212:        { NULL, posts_bool },                   /* Db */
                    213:        { NULL, NULL },                         /* Dc */
                    214:        { NULL, NULL },                         /* Do */
                    215:        { NULL, posts_wline },                  /* Dq */
                    216:        { NULL, NULL },                         /* Ec */
                    217:        { NULL, NULL },                         /* Ef */
                    218:        { NULL, NULL },                         /* Em */
                    219:        { NULL, NULL },                         /* Eo */
                    220:        { NULL, NULL },                         /* Fx */
                    221:        { NULL, posts_text },                   /* Ms */
                    222:        { NULL, posts_notext },                 /* No */
                    223:        { NULL, posts_notext },                 /* Ns */
                    224:        { NULL, NULL },                         /* Nx */
                    225:        { NULL, NULL },                         /* Ox */
                    226:        { NULL, NULL },                         /* Pc */
1.51      kristaps  227:        { NULL, posts_text1 },                  /* Pf */
1.1       kristaps  228:        { NULL, NULL },                         /* Po */
                    229:        { NULL, posts_wline },                  /* Pq */
                    230:        { NULL, NULL },                         /* Qc */
                    231:        { NULL, posts_wline },                  /* Ql */
                    232:        { NULL, NULL },                         /* Qo */
                    233:        { NULL, posts_wline },                  /* Qq */
                    234:        { NULL, NULL },                         /* Re */
1.43      kristaps  235:        { NULL, posts_rs },                     /* Rs */
1.1       kristaps  236:        { NULL, NULL },                         /* Sc */
                    237:        { NULL, NULL },                         /* So */
                    238:        { NULL, posts_wline },                  /* Sq */
                    239:        { NULL, posts_bool },                   /* Sm */
                    240:        { NULL, posts_text },                   /* Sx */
                    241:        { NULL, posts_text },                   /* Sy */
                    242:        { NULL, posts_text },                   /* Tn */
                    243:        { NULL, NULL },                         /* Ux */
                    244:        { NULL, NULL },                         /* Xc */
                    245:        { NULL, NULL },                         /* Xo */
                    246:        { NULL, posts_fo },                     /* Fo */
                    247:        { NULL, NULL },                         /* Fc */
                    248:        { NULL, NULL },                         /* Oo */
                    249:        { NULL, NULL },                         /* Oc */
                    250:        { NULL, posts_wline },                  /* Bk */
                    251:        { NULL, NULL },                         /* Ek */
                    252:        { NULL, posts_notext },                 /* Bt */
                    253:        { NULL, NULL },                         /* Hf */
                    254:        { NULL, NULL },                         /* Fr */
                    255:        { NULL, posts_notext },                 /* Ud */
                    256:        { pres_lb, posts_lb },                  /* Lb */
1.34      kristaps  257:        { NULL, posts_notext },                 /* Lp */
1.51      kristaps  258:        { NULL, posts_text },                   /* Lk */
1.1       kristaps  259:        { NULL, posts_text },                   /* Mt */
                    260:        { NULL, posts_wline },                  /* Brq */
                    261:        { NULL, NULL },                         /* Bro */
                    262:        { NULL, NULL },                         /* Brc */
                    263:        { NULL, posts_text },                   /* %C */
                    264:        { NULL, NULL },                         /* Es */
                    265:        { NULL, NULL },                         /* En */
                    266:        { NULL, NULL },                         /* Dx */
                    267:        { NULL, posts_text },                   /* %Q */
1.33      kristaps  268:        { NULL, posts_notext },                 /* br */
1.35      kristaps  269:        { NULL, posts_sp },                     /* sp */
1.51      kristaps  270:        { NULL, posts_text1 },                  /* %U */
1.1       kristaps  271: };
                    272:
                    273:
                    274: int
1.32      kristaps  275: mdoc_valid_pre(struct mdoc *mdoc, const struct mdoc_node *n)
1.1       kristaps  276: {
                    277:        v_pre           *p;
                    278:        int              line, pos;
                    279:        const char      *tp;
                    280:
                    281:        if (MDOC_TEXT == n->type) {
                    282:                tp = n->string;
                    283:                line = n->line;
                    284:                pos = n->pos;
                    285:                return(check_text(mdoc, line, pos, tp));
                    286:        }
                    287:
                    288:        if ( ! check_args(mdoc, n))
                    289:                return(0);
                    290:        if (NULL == mdoc_valids[n->tok].pre)
                    291:                return(1);
                    292:        for (p = mdoc_valids[n->tok].pre; *p; p++)
                    293:                if ( ! (*p)(mdoc, n))
                    294:                        return(0);
                    295:        return(1);
                    296: }
                    297:
                    298:
                    299: int
                    300: mdoc_valid_post(struct mdoc *mdoc)
                    301: {
                    302:        v_post          *p;
                    303:
                    304:        if (MDOC_VALID & mdoc->last->flags)
                    305:                return(1);
                    306:        mdoc->last->flags |= MDOC_VALID;
                    307:
                    308:        if (MDOC_TEXT == mdoc->last->type)
                    309:                return(1);
                    310:        if (MDOC_ROOT == mdoc->last->type)
                    311:                return(post_root(mdoc));
                    312:
                    313:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    314:                return(1);
                    315:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    316:                if ( ! (*p)(mdoc))
                    317:                        return(0);
                    318:
                    319:        return(1);
                    320: }
                    321:
                    322:
                    323: static inline int
                    324: warn_count(struct mdoc *m, const char *k,
                    325:                int want, const char *v, int has)
                    326: {
                    327:
1.23      kristaps  328:        return(mdoc_vwarn(m, m->last->line, m->last->pos,
                    329:                "suggests %s %s %d (has %d)", v, k, want, has));
1.1       kristaps  330: }
                    331:
                    332:
                    333: static inline int
                    334: err_count(struct mdoc *m, const char *k,
                    335:                int want, const char *v, int has)
                    336: {
                    337:
1.23      kristaps  338:        return(mdoc_verr(m, m->last->line, m->last->pos,
1.1       kristaps  339:                "requires %s %s %d (has %d)", v, k, want, has));
                    340: }
                    341:
                    342:
                    343: /*
                    344:  * Build these up with macros because they're basically the same check
                    345:  * for different inequalities.  Yes, this could be done with functions,
                    346:  * but this is reasonable for now.
                    347:  */
                    348:
                    349: #define CHECK_CHILD_DEFN(lvl, name, ineq)                      \
                    350: static int                                                     \
                    351: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz)   \
                    352: {                                                              \
1.16      kristaps  353:        if (mdoc->last->nchild ineq sz)                         \
1.1       kristaps  354:                return(1);                                      \
1.16      kristaps  355:        return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
1.1       kristaps  356: }
                    357:
                    358: #define CHECK_BODY_DEFN(name, lvl, func, num)                  \
                    359: static int                                                     \
                    360: b##lvl##_##name(POST_ARGS)                                     \
                    361: {                                                              \
                    362:        if (MDOC_BODY != mdoc->last->type)                      \
                    363:                return(1);                                      \
                    364:        return(func(mdoc, "multi-line arguments", (num)));      \
                    365: }
                    366:
                    367: #define CHECK_ELEM_DEFN(name, lvl, func, num)                  \
                    368: static int                                                     \
                    369: e##lvl##_##name(POST_ARGS)                                     \
                    370: {                                                              \
                    371:        assert(MDOC_ELEM == mdoc->last->type);                  \
                    372:        return(func(mdoc, "line arguments", (num)));            \
                    373: }
                    374:
                    375: #define CHECK_HEAD_DEFN(name, lvl, func, num)                  \
                    376: static int                                                     \
                    377: h##lvl##_##name(POST_ARGS)                                     \
                    378: {                                                              \
                    379:        if (MDOC_HEAD != mdoc->last->type)                      \
                    380:                return(1);                                      \
                    381:        return(func(mdoc, "line arguments", (num)));            \
                    382: }
                    383:
                    384:
                    385: CHECK_CHILD_DEFN(warn, gt, >)                  /* warn_child_gt() */
                    386: CHECK_CHILD_DEFN(err, gt, >)                   /* err_child_gt() */
                    387: CHECK_CHILD_DEFN(warn, eq, ==)                 /* warn_child_eq() */
                    388: CHECK_CHILD_DEFN(err, eq, ==)                  /* err_child_eq() */
                    389: CHECK_CHILD_DEFN(err, lt, <)                   /* err_child_lt() */
                    390: CHECK_CHILD_DEFN(warn, lt, <)                  /* warn_child_lt() */
                    391: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0)   /* bwarn_ge1() */
1.27      kristaps  392: CHECK_BODY_DEFN(ge1, err, err_child_gt, 0)     /* berr_ge1() */
1.1       kristaps  393: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0)   /* ewarn_gt1() */
                    394: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1)     /* eerr_eq1() */
1.46      kristaps  395: CHECK_ELEM_DEFN(le1, err, err_child_lt, 2)     /* eerr_le1() */
1.1       kristaps  396: CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0)     /* eerr_eq0() */
                    397: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0)     /* eerr_ge1() */
                    398: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0)     /* herr_eq0() */
                    399: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2)   /* hwarn_le1() */
                    400: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0)     /* herr_ge1() */
                    401: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1)   /* hwarn_eq1() */
1.66      kristaps  402: CHECK_HEAD_DEFN(eq0, warn, warn_child_eq, 0)   /* hwarn_eq0() */
1.1       kristaps  403:
                    404:
                    405: static int
                    406: check_stdarg(PRE_ARGS)
                    407: {
                    408:
                    409:        if (n->args && 1 == n->args->argc)
                    410:                if (MDOC_Std == n->args->argv[0].arg)
                    411:                        return(1);
1.23      kristaps  412:        return(mdoc_nwarn(mdoc, n, EARGVAL));
1.1       kristaps  413: }
                    414:
                    415:
                    416: static int
                    417: check_sec(PRE_ARGS, ...)
                    418: {
                    419:        enum mdoc_sec    sec;
                    420:        va_list          ap;
                    421:
                    422:        va_start(ap, n);
                    423:
                    424:        for (;;) {
                    425:                /* LINTED */
                    426:                sec = (enum mdoc_sec)va_arg(ap, int);
                    427:                if (SEC_CUSTOM == sec)
                    428:                        break;
                    429:                if (sec != mdoc->lastsec)
                    430:                        continue;
                    431:                va_end(ap);
                    432:                return(1);
                    433:        }
                    434:
                    435:        va_end(ap);
1.23      kristaps  436:        return(mdoc_nwarn(mdoc, n, EBADSEC));
1.1       kristaps  437: }
                    438:
                    439:
                    440: static int
                    441: check_args(struct mdoc *m, const struct mdoc_node *n)
                    442: {
                    443:        int              i;
                    444:
                    445:        if (NULL == n->args)
                    446:                return(1);
                    447:
                    448:        assert(n->args->argc);
                    449:        for (i = 0; i < (int)n->args->argc; i++)
                    450:                if ( ! check_argv(m, n, &n->args->argv[i]))
                    451:                        return(0);
                    452:
                    453:        return(1);
                    454: }
                    455:
                    456:
                    457: static int
                    458: check_argv(struct mdoc *m, const struct mdoc_node *n,
                    459:                const struct mdoc_argv *v)
                    460: {
                    461:        int              i;
                    462:
                    463:        for (i = 0; i < (int)v->sz; i++)
                    464:                if ( ! check_text(m, v->line, v->pos, v->value[i]))
                    465:                        return(0);
                    466:
                    467:        if (MDOC_Std == v->arg) {
                    468:                /* `Nm' name must be set. */
                    469:                if (v->sz || m->meta.name)
                    470:                        return(1);
1.23      kristaps  471:                return(mdoc_nerr(m, n, ENAME));
1.1       kristaps  472:        }
                    473:
                    474:        return(1);
                    475: }
                    476:
                    477:
                    478: static int
                    479: check_text(struct mdoc *mdoc, int line, int pos, const char *p)
                    480: {
1.18      kristaps  481:        int              c;
1.1       kristaps  482:
1.18      kristaps  483:        for ( ; *p; p++, pos++) {
1.1       kristaps  484:                if ('\t' == *p) {
                    485:                        if ( ! (MDOC_LITERAL & mdoc->flags))
1.71      kristaps  486:                                if ( ! mdoc_pwarn(mdoc, line, pos, EPRINT))
1.4       kristaps  487:                                        return(0);
1.1       kristaps  488:                } else if ( ! isprint((u_char)*p))
1.71      kristaps  489:                        if ( ! mdoc_pwarn(mdoc, line, pos, EPRINT))
1.4       kristaps  490:                                return(0);
1.1       kristaps  491:
                    492:                if ('\\' != *p)
                    493:                        continue;
                    494:
1.18      kristaps  495:                c = mandoc_special(p);
1.1       kristaps  496:                if (c) {
1.18      kristaps  497:                        p += c - 1;
                    498:                        pos += c - 1;
1.1       kristaps  499:                        continue;
                    500:                }
                    501:                if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags))
1.23      kristaps  502:                        return(mdoc_perr(mdoc, line, pos, EESCAPE));
1.32      kristaps  503:                if ( ! mdoc_pwarn(mdoc, line, pos, EESCAPE))
1.1       kristaps  504:                        return(0);
                    505:        }
                    506:
                    507:        return(1);
                    508: }
                    509:
                    510:
                    511:
                    512:
                    513: static int
1.59      kristaps  514: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1       kristaps  515: {
                    516:
                    517:        assert(n->parent);
                    518:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    519:                        (t == n->parent->type))
                    520:                return(1);
                    521:
1.23      kristaps  522:        return(mdoc_verr(mdoc, n->line, n->pos, "require parent %s",
1.1       kristaps  523:                MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
                    524: }
                    525:
                    526:
                    527:
                    528: static int
                    529: pre_display(PRE_ARGS)
                    530: {
                    531:        struct mdoc_node *node;
                    532:
                    533:        /* Display elements (`Bd', `D1'...) cannot be nested. */
                    534:
                    535:        if (MDOC_BLOCK != n->type)
                    536:                return(1);
                    537:
                    538:        /* LINTED */
                    539:        for (node = mdoc->last->parent; node; node = node->parent)
                    540:                if (MDOC_BLOCK == node->type)
                    541:                        if (MDOC_Bd == node->tok)
                    542:                                break;
                    543:        if (NULL == node)
                    544:                return(1);
                    545:
1.23      kristaps  546:        return(mdoc_nerr(mdoc, n, ENESTDISP));
1.1       kristaps  547: }
                    548:
                    549:
                    550: static int
                    551: pre_bl(PRE_ARGS)
                    552: {
1.15      kristaps  553:        int              pos, type, width, offset;
1.1       kristaps  554:
                    555:        if (MDOC_BLOCK != n->type)
                    556:                return(1);
                    557:        if (NULL == n->args)
1.23      kristaps  558:                return(mdoc_nerr(mdoc, n, ELISTTYPE));
1.1       kristaps  559:
                    560:        /* Make sure that only one type of list is specified.  */
                    561:
1.15      kristaps  562:        type = offset = width = -1;
1.1       kristaps  563:
                    564:        /* LINTED */
1.8       kristaps  565:        for (pos = 0; pos < (int)n->args->argc; pos++)
                    566:                switch (n->args->argv[pos].arg) {
1.1       kristaps  567:                case (MDOC_Bullet):
                    568:                        /* FALLTHROUGH */
                    569:                case (MDOC_Dash):
                    570:                        /* FALLTHROUGH */
                    571:                case (MDOC_Enum):
                    572:                        /* FALLTHROUGH */
                    573:                case (MDOC_Hyphen):
                    574:                        /* FALLTHROUGH */
                    575:                case (MDOC_Item):
                    576:                        /* FALLTHROUGH */
                    577:                case (MDOC_Tag):
                    578:                        /* FALLTHROUGH */
                    579:                case (MDOC_Diag):
                    580:                        /* FALLTHROUGH */
                    581:                case (MDOC_Hang):
                    582:                        /* FALLTHROUGH */
                    583:                case (MDOC_Ohang):
                    584:                        /* FALLTHROUGH */
                    585:                case (MDOC_Inset):
                    586:                        /* FALLTHROUGH */
                    587:                case (MDOC_Column):
1.61      kristaps  588:                        /*
                    589:                         * Note that if a duplicate is detected, we
                    590:                         * remove the duplicate instead of passing it
                    591:                         * over.  If we don't do this, mdoc_action will
                    592:                         * become confused when it scans over multiple
                    593:                         * types whilst setting its bitmasks.
1.62      kristaps  594:                         *
                    595:                         * FIXME: this should occur in mdoc_action.c.
1.61      kristaps  596:                         */
                    597:                        if (type >= 0) {
                    598:                                if ( ! mdoc_nwarn(mdoc, n, EMULTILIST))
                    599:                                        return(0);
                    600:                                mdoc_argn_free(n->args, pos);
                    601:                                break;
                    602:                        }
1.8       kristaps  603:                        type = n->args->argv[pos].arg;
                    604:                        break;
1.45      kristaps  605:                case (MDOC_Compact):
                    606:                        if (type < 0 && ! mdoc_nwarn(mdoc, n, ENOTYPE))
                    607:                                return(0);
                    608:                        break;
1.1       kristaps  609:                case (MDOC_Width):
1.45      kristaps  610:                        if (width >= 0)
1.23      kristaps  611:                                return(mdoc_nerr(mdoc, n, EARGREP));
1.45      kristaps  612:                        if (type < 0 && ! mdoc_nwarn(mdoc, n, ENOTYPE))
                    613:                                return(0);
1.8       kristaps  614:                        width = n->args->argv[pos].arg;
                    615:                        break;
1.1       kristaps  616:                case (MDOC_Offset):
1.45      kristaps  617:                        if (offset >= 0)
1.23      kristaps  618:                                return(mdoc_nerr(mdoc, n, EARGREP));
1.45      kristaps  619:                        if (type < 0 && ! mdoc_nwarn(mdoc, n, ENOTYPE))
                    620:                                return(0);
1.8       kristaps  621:                        offset = n->args->argv[pos].arg;
                    622:                        break;
1.1       kristaps  623:                default:
                    624:                        break;
                    625:                }
                    626:
1.45      kristaps  627:        if (type < 0)
1.23      kristaps  628:                return(mdoc_nerr(mdoc, n, ELISTTYPE));
1.1       kristaps  629:
1.8       kristaps  630:        /*
                    631:         * Validate the width field.  Some list types don't need width
                    632:         * types and should be warned about them.  Others should have it
                    633:         * and must also be warned.
                    634:         */
                    635:
1.1       kristaps  636:        switch (type) {
1.8       kristaps  637:        case (MDOC_Tag):
1.45      kristaps  638:                if (width < 0 && ! mdoc_nwarn(mdoc, n, EMISSWIDTH))
1.8       kristaps  639:                        return(0);
                    640:                break;
1.1       kristaps  641:        case (MDOC_Column):
                    642:                /* FALLTHROUGH */
                    643:        case (MDOC_Diag):
                    644:                /* FALLTHROUGH */
1.53      kristaps  645:        case (MDOC_Ohang):
                    646:                /* FALLTHROUGH */
1.1       kristaps  647:        case (MDOC_Inset):
                    648:                /* FALLTHROUGH */
                    649:        case (MDOC_Item):
1.45      kristaps  650:                if (width >= 0 && ! mdoc_nwarn(mdoc, n, ENOWIDTH))
1.8       kristaps  651:                        return(0);
                    652:                break;
                    653:        default:
                    654:                break;
                    655:        }
                    656:
1.1       kristaps  657:        return(1);
                    658: }
                    659:
                    660:
                    661: static int
                    662: pre_bd(PRE_ARGS)
                    663: {
                    664:        int              i, type, err;
                    665:
                    666:        if (MDOC_BLOCK != n->type)
                    667:                return(1);
                    668:        if (NULL == n->args)
1.23      kristaps  669:                return(mdoc_nerr(mdoc, n, EDISPTYPE));
1.1       kristaps  670:
                    671:        /* Make sure that only one type of display is specified.  */
                    672:
                    673:        /* LINTED */
                    674:        for (i = 0, err = type = 0; ! err &&
                    675:                        i < (int)n->args->argc; i++)
                    676:                switch (n->args->argv[i].arg) {
1.49      kristaps  677:                case (MDOC_Centred):
                    678:                        /* FALLTHROUGH */
1.1       kristaps  679:                case (MDOC_Ragged):
                    680:                        /* FALLTHROUGH */
                    681:                case (MDOC_Unfilled):
                    682:                        /* FALLTHROUGH */
                    683:                case (MDOC_Filled):
                    684:                        /* FALLTHROUGH */
                    685:                case (MDOC_Literal):
                    686:                        if (0 == type++)
                    687:                                break;
1.23      kristaps  688:                        return(mdoc_nerr(mdoc, n, EMULTIDISP));
1.1       kristaps  689:                default:
                    690:                        break;
                    691:                }
                    692:
                    693:        if (type)
                    694:                return(1);
1.23      kristaps  695:        return(mdoc_nerr(mdoc, n, EDISPTYPE));
1.1       kristaps  696: }
                    697:
                    698:
                    699: static int
                    700: pre_ss(PRE_ARGS)
                    701: {
                    702:
                    703:        if (MDOC_BLOCK != n->type)
                    704:                return(1);
                    705:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
                    706: }
                    707:
                    708:
                    709: static int
                    710: pre_sh(PRE_ARGS)
                    711: {
                    712:
                    713:        if (MDOC_BLOCK != n->type)
                    714:                return(1);
1.70      kristaps  715:        return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1       kristaps  716: }
                    717:
                    718:
                    719: static int
                    720: pre_it(PRE_ARGS)
                    721: {
                    722:
                    723:        if (MDOC_BLOCK != n->type)
                    724:                return(1);
                    725:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
                    726: }
                    727:
                    728:
                    729: static int
                    730: pre_an(PRE_ARGS)
                    731: {
                    732:
                    733:        if (NULL == n->args || 1 == n->args->argc)
                    734:                return(1);
1.23      kristaps  735:        return(mdoc_verr(mdoc, n->line, n->pos,
                    736:                                "only one argument allowed"));
1.1       kristaps  737: }
                    738:
                    739:
                    740: static int
                    741: pre_lb(PRE_ARGS)
                    742: {
                    743:
                    744:        return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
                    745: }
                    746:
                    747:
                    748: static int
                    749: pre_rv(PRE_ARGS)
                    750: {
                    751:
                    752:        return(check_stdarg(mdoc, n));
                    753: }
                    754:
                    755:
                    756: static int
                    757: pre_dt(PRE_ARGS)
                    758: {
                    759:
1.54      kristaps  760:        /* FIXME: make sure is capitalised. */
                    761:
1.1       kristaps  762:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.23      kristaps  763:                if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
1.1       kristaps  764:                        return(0);
                    765:        if (mdoc->meta.title)
1.23      kristaps  766:                if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
1.1       kristaps  767:                        return(0);
                    768:        return(1);
                    769: }
                    770:
                    771:
                    772: static int
                    773: pre_os(PRE_ARGS)
                    774: {
                    775:
                    776:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.23      kristaps  777:                if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
1.1       kristaps  778:                        return(0);
                    779:        if (mdoc->meta.os)
1.23      kristaps  780:                if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
1.1       kristaps  781:                        return(0);
                    782:        return(1);
                    783: }
                    784:
                    785:
                    786: static int
                    787: pre_dd(PRE_ARGS)
                    788: {
                    789:
                    790:        if (mdoc->meta.title || mdoc->meta.os)
1.23      kristaps  791:                if ( ! mdoc_nwarn(mdoc, n, EPROLOOO))
1.1       kristaps  792:                        return(0);
                    793:        if (mdoc->meta.date)
1.23      kristaps  794:                if ( ! mdoc_nwarn(mdoc, n, EPROLREP))
1.1       kristaps  795:                        return(0);
                    796:        return(1);
                    797: }
                    798:
                    799:
                    800: static int
                    801: post_bf(POST_ARGS)
                    802: {
                    803:        char             *p;
                    804:        struct mdoc_node *head;
                    805:
                    806:        if (MDOC_BLOCK != mdoc->last->type)
                    807:                return(1);
                    808:
                    809:        head = mdoc->last->head;
                    810:
1.12      kristaps  811:        if (mdoc->last->args && head->child)
1.24      kristaps  812:                return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.13      kristaps  813:        else if (mdoc->last->args)
                    814:                return(1);
1.12      kristaps  815:
1.13      kristaps  816:        if (NULL == head->child || MDOC_TEXT != head->child->type)
1.24      kristaps  817:                return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.1       kristaps  818:
1.13      kristaps  819:        p = head->child->string;
1.1       kristaps  820:
1.13      kristaps  821:        if (0 == strcmp(p, "Em"))
                    822:                return(1);
                    823:        else if (0 == strcmp(p, "Li"))
                    824:                return(1);
1.37      kristaps  825:        else if (0 == strcmp(p, "Sy"))
1.13      kristaps  826:                return(1);
1.1       kristaps  827:
1.23      kristaps  828:        return(mdoc_nerr(mdoc, head, EFONT));
1.1       kristaps  829: }
                    830:
                    831:
                    832: static int
1.31      kristaps  833: post_lb(POST_ARGS)
                    834: {
                    835:
                    836:        if (mdoc_a2lib(mdoc->last->child->string))
                    837:                return(1);
                    838:        return(mdoc_nwarn(mdoc, mdoc->last, ELIB));
                    839: }
                    840:
                    841:
                    842: static int
1.57      kristaps  843: post_vt(POST_ARGS)
                    844: {
                    845:        const struct mdoc_node *n;
                    846:
                    847:        /*
                    848:         * The Vt macro comes in both ELEM and BLOCK form, both of which
                    849:         * have different syntaxes (yet more context-sensitive
                    850:         * behaviour).  ELEM types must have a child; BLOCK types,
                    851:         * specifically the BODY, should only have TEXT children.
                    852:         */
                    853:
                    854:        if (MDOC_ELEM == mdoc->last->type)
                    855:                return(eerr_ge1(mdoc));
                    856:        if (MDOC_BODY != mdoc->last->type)
                    857:                return(1);
                    858:
                    859:        for (n = mdoc->last->child; n; n = n->next)
                    860:                if (MDOC_TEXT != n->type)
                    861:                        if ( ! mdoc_nwarn(mdoc, n, EBADCHILD))
                    862:                                return(0);
                    863:
                    864:        return(1);
                    865: }
                    866:
                    867:
                    868: static int
1.1       kristaps  869: post_nm(POST_ARGS)
                    870: {
                    871:
                    872:        if (mdoc->last->child)
                    873:                return(1);
                    874:        if (mdoc->meta.name)
                    875:                return(1);
1.23      kristaps  876:        return(mdoc_nerr(mdoc, mdoc->last, ENAME));
1.1       kristaps  877: }
                    878:
                    879:
                    880: static int
                    881: post_at(POST_ARGS)
                    882: {
                    883:
                    884:        if (NULL == mdoc->last->child)
                    885:                return(1);
                    886:        if (MDOC_TEXT != mdoc->last->child->type)
1.23      kristaps  887:                return(mdoc_nerr(mdoc, mdoc->last, EATT));
1.1       kristaps  888:        if (mdoc_a2att(mdoc->last->child->string))
                    889:                return(1);
1.75      kristaps  890:        return(mdoc_nwarn(mdoc, mdoc->last, EATT));
1.1       kristaps  891: }
                    892:
                    893:
                    894: static int
                    895: post_an(POST_ARGS)
                    896: {
                    897:
                    898:        if (mdoc->last->args) {
                    899:                if (NULL == mdoc->last->child)
                    900:                        return(1);
1.40      kristaps  901:                return(mdoc_nerr(mdoc, mdoc->last, ENOLINE));
1.1       kristaps  902:        }
                    903:
                    904:        if (mdoc->last->child)
                    905:                return(1);
1.23      kristaps  906:        return(mdoc_nerr(mdoc, mdoc->last, ELINE));
1.1       kristaps  907: }
                    908:
                    909:
                    910: static int
                    911: post_it(POST_ARGS)
                    912: {
                    913:        int               type, i, cols;
                    914:        struct mdoc_node *n, *c;
                    915:
                    916:        if (MDOC_BLOCK != mdoc->last->type)
                    917:                return(1);
                    918:
                    919:        n = mdoc->last->parent->parent;
                    920:        if (NULL == n->args)
1.23      kristaps  921:                return(mdoc_nerr(mdoc, mdoc->last, ELISTTYPE));
1.1       kristaps  922:
                    923:        /* Some types require block-head, some not. */
                    924:
                    925:        /* LINTED */
                    926:        for (cols = type = -1, i = 0; -1 == type &&
                    927:                        i < (int)n->args->argc; i++)
                    928:                switch (n->args->argv[i].arg) {
                    929:                case (MDOC_Tag):
                    930:                        /* FALLTHROUGH */
                    931:                case (MDOC_Diag):
                    932:                        /* FALLTHROUGH */
                    933:                case (MDOC_Hang):
                    934:                        /* FALLTHROUGH */
                    935:                case (MDOC_Ohang):
                    936:                        /* FALLTHROUGH */
                    937:                case (MDOC_Inset):
                    938:                        /* FALLTHROUGH */
                    939:                case (MDOC_Bullet):
                    940:                        /* FALLTHROUGH */
                    941:                case (MDOC_Dash):
                    942:                        /* FALLTHROUGH */
                    943:                case (MDOC_Enum):
                    944:                        /* FALLTHROUGH */
                    945:                case (MDOC_Hyphen):
                    946:                        /* FALLTHROUGH */
                    947:                case (MDOC_Item):
                    948:                        type = n->args->argv[i].arg;
                    949:                        break;
                    950:                case (MDOC_Column):
                    951:                        type = n->args->argv[i].arg;
                    952:                        cols = (int)n->args->argv[i].sz;
                    953:                        break;
                    954:                default:
                    955:                        break;
                    956:                }
                    957:
                    958:        if (-1 == type)
1.23      kristaps  959:                return(mdoc_nerr(mdoc, mdoc->last, ELISTTYPE));
1.1       kristaps  960:
                    961:        switch (type) {
                    962:        case (MDOC_Tag):
                    963:                if (NULL == mdoc->last->head->child)
1.23      kristaps  964:                        if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1.1       kristaps  965:                                return(0);
                    966:                break;
                    967:        case (MDOC_Hang):
                    968:                /* FALLTHROUGH */
                    969:        case (MDOC_Ohang):
                    970:                /* FALLTHROUGH */
                    971:        case (MDOC_Inset):
                    972:                /* FALLTHROUGH */
                    973:        case (MDOC_Diag):
                    974:                if (NULL == mdoc->last->head->child)
1.23      kristaps  975:                        if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1.1       kristaps  976:                                return(0);
                    977:                if (NULL == mdoc->last->body->child)
1.23      kristaps  978:                        if ( ! mdoc_nwarn(mdoc, mdoc->last, EMULTILINE))
1.1       kristaps  979:                                return(0);
                    980:                break;
                    981:        case (MDOC_Bullet):
                    982:                /* FALLTHROUGH */
                    983:        case (MDOC_Dash):
                    984:                /* FALLTHROUGH */
                    985:        case (MDOC_Enum):
                    986:                /* FALLTHROUGH */
                    987:        case (MDOC_Hyphen):
                    988:                /* FALLTHROUGH */
                    989:        case (MDOC_Item):
                    990:                if (mdoc->last->head->child)
1.23      kristaps  991:                        if ( ! mdoc_nwarn(mdoc, mdoc->last, ENOLINE))
1.1       kristaps  992:                                return(0);
                    993:                if (NULL == mdoc->last->body->child)
1.23      kristaps  994:                        if ( ! mdoc_nwarn(mdoc, mdoc->last, EMULTILINE))
1.1       kristaps  995:                                return(0);
                    996:                break;
                    997:        case (MDOC_Column):
                    998:                if (NULL == mdoc->last->head->child)
1.23      kristaps  999:                        if ( ! mdoc_nwarn(mdoc, mdoc->last, ELINE))
1.1       kristaps 1000:                                return(0);
                   1001:                if (mdoc->last->body->child)
1.23      kristaps 1002:                        if ( ! mdoc_nwarn(mdoc, mdoc->last, ENOMULTILINE))
1.1       kristaps 1003:                                return(0);
                   1004:                c = mdoc->last->child;
                   1005:                for (i = 0; c && MDOC_HEAD == c->type; c = c->next)
                   1006:                        i++;
1.38      kristaps 1007:
1.73      kristaps 1008:                if (i < cols) {
1.38      kristaps 1009:                        if ( ! mdoc_vwarn(mdoc, mdoc->last->line,
                   1010:                                        mdoc->last->pos, "column "
                   1011:                                        "mismatch: have %d, want %d",
                   1012:                                        i, cols))
                   1013:                                return(0);
1.1       kristaps 1014:                        break;
1.73      kristaps 1015:                } else if (i == cols || i == cols + 1)
1.38      kristaps 1016:                        break;
                   1017:
                   1018:                return(mdoc_verr(mdoc, mdoc->last->line,
                   1019:                                mdoc->last->pos, "column mismatch: "
                   1020:                                "have %d, want %d", i, cols));
1.1       kristaps 1021:        default:
                   1022:                break;
                   1023:        }
                   1024:
                   1025:        return(1);
                   1026: }
                   1027:
                   1028:
                   1029: static int
1.14      kristaps 1030: post_bl_head(POST_ARGS)
                   1031: {
                   1032:        int                     i;
                   1033:        const struct mdoc_node *n;
1.65      kristaps 1034:        const struct mdoc_argv *a;
1.14      kristaps 1035:
                   1036:        n = mdoc->last->parent;
                   1037:        assert(n->args);
                   1038:
1.65      kristaps 1039:        for (i = 0; i < (int)n->args->argc; i++) {
                   1040:                a = &n->args->argv[i];
                   1041:                if (a->arg == MDOC_Column) {
                   1042:                        if (a->sz && mdoc->last->nchild)
                   1043:                                return(mdoc_nerr(mdoc, n, ECOLMIS));
                   1044:                        return(1);
                   1045:                }
                   1046:        }
1.14      kristaps 1047:
1.65      kristaps 1048:        if (0 == (i = mdoc->last->nchild))
                   1049:                return(1);
                   1050:        return(warn_count(mdoc, "==", 0, "line arguments", i));
1.14      kristaps 1051: }
                   1052:
                   1053:
                   1054: static int
1.1       kristaps 1055: post_bl(POST_ARGS)
                   1056: {
                   1057:        struct mdoc_node        *n;
                   1058:
1.14      kristaps 1059:        if (MDOC_HEAD == mdoc->last->type)
                   1060:                return(post_bl_head(mdoc));
1.1       kristaps 1061:        if (MDOC_BODY != mdoc->last->type)
                   1062:                return(1);
                   1063:        if (NULL == mdoc->last->child)
                   1064:                return(1);
                   1065:
1.55      kristaps 1066:        /*
                   1067:         * We only allow certain children of `Bl'.  This is usually on
                   1068:         * `It', but apparently `Sm' occurs here and there, so we let
                   1069:         * that one through, too.
                   1070:         */
                   1071:
1.1       kristaps 1072:        /* LINTED */
                   1073:        for (n = mdoc->last->child; n; n = n->next) {
1.55      kristaps 1074:                if (MDOC_BLOCK == n->type && MDOC_It == n->tok)
                   1075:                        continue;
                   1076:                if (MDOC_Sm == n->tok)
                   1077:                        continue;
1.43      kristaps 1078:                return(mdoc_nerr(mdoc, n, EBADCHILD));
1.1       kristaps 1079:        }
                   1080:
                   1081:        return(1);
                   1082: }
                   1083:
                   1084:
                   1085: static int
                   1086: ebool(struct mdoc *mdoc)
                   1087: {
                   1088:        struct mdoc_node *n;
                   1089:
                   1090:        /* LINTED */
                   1091:        for (n = mdoc->last->child; n; n = n->next) {
                   1092:                if (MDOC_TEXT != n->type)
                   1093:                        break;
1.2       kristaps 1094:                if (0 == strcmp(n->string, "on"))
1.1       kristaps 1095:                        continue;
1.2       kristaps 1096:                if (0 == strcmp(n->string, "off"))
1.1       kristaps 1097:                        continue;
                   1098:                break;
                   1099:        }
                   1100:
                   1101:        if (NULL == n)
                   1102:                return(1);
1.23      kristaps 1103:        return(mdoc_nerr(mdoc, n, EBOOL));
1.1       kristaps 1104: }
                   1105:
                   1106:
                   1107: static int
                   1108: post_root(POST_ARGS)
                   1109: {
                   1110:
                   1111:        if (NULL == mdoc->first->child)
1.23      kristaps 1112:                return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1.12      kristaps 1113:        if ( ! (MDOC_PBODY & mdoc->flags))
1.23      kristaps 1114:                return(mdoc_nerr(mdoc, mdoc->first, ENOPROLOGUE));
1.1       kristaps 1115:
                   1116:        if (MDOC_BLOCK != mdoc->first->child->type)
1.23      kristaps 1117:                return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1.1       kristaps 1118:        if (MDOC_Sh != mdoc->first->child->tok)
1.23      kristaps 1119:                return(mdoc_nerr(mdoc, mdoc->first, ENODAT));
1.1       kristaps 1120:
                   1121:        return(1);
                   1122: }
                   1123:
                   1124:
                   1125: static int
                   1126: post_st(POST_ARGS)
                   1127: {
                   1128:
                   1129:        if (mdoc_a2st(mdoc->last->child->string))
                   1130:                return(1);
1.74      kristaps 1131:        return(mdoc_nwarn(mdoc, mdoc->last, EBADSTAND));
1.1       kristaps 1132: }
                   1133:
                   1134:
                   1135: static int
1.43      kristaps 1136: post_rs(POST_ARGS)
                   1137: {
                   1138:        struct mdoc_node        *nn;
                   1139:
                   1140:        if (MDOC_BODY != mdoc->last->type)
                   1141:                return(1);
                   1142:
                   1143:        for (nn = mdoc->last->child; nn; nn = nn->next)
                   1144:                switch (nn->tok) {
1.50      kristaps 1145:                case(MDOC__U):
                   1146:                        /* FALLTHROUGH */
1.43      kristaps 1147:                case(MDOC__Q):
                   1148:                        /* FALLTHROUGH */
                   1149:                case(MDOC__C):
                   1150:                        /* FALLTHROUGH */
                   1151:                case(MDOC__A):
                   1152:                        /* FALLTHROUGH */
                   1153:                case(MDOC__B):
                   1154:                        /* FALLTHROUGH */
                   1155:                case(MDOC__D):
                   1156:                        /* FALLTHROUGH */
                   1157:                case(MDOC__I):
                   1158:                        /* FALLTHROUGH */
                   1159:                case(MDOC__J):
                   1160:                        /* FALLTHROUGH */
                   1161:                case(MDOC__N):
                   1162:                        /* FALLTHROUGH */
                   1163:                case(MDOC__O):
                   1164:                        /* FALLTHROUGH */
                   1165:                case(MDOC__P):
                   1166:                        /* FALLTHROUGH */
                   1167:                case(MDOC__R):
                   1168:                        /* FALLTHROUGH */
                   1169:                case(MDOC__T):
                   1170:                        /* FALLTHROUGH */
                   1171:                case(MDOC__V):
                   1172:                        break;
                   1173:                default:
                   1174:                        return(mdoc_nerr(mdoc, nn, EBADCHILD));
                   1175:                }
                   1176:
                   1177:        return(1);
                   1178: }
                   1179:
                   1180:
                   1181: static int
1.1       kristaps 1182: post_sh(POST_ARGS)
                   1183: {
                   1184:
                   1185:        if (MDOC_HEAD == mdoc->last->type)
                   1186:                return(post_sh_head(mdoc));
                   1187:        if (MDOC_BODY == mdoc->last->type)
                   1188:                return(post_sh_body(mdoc));
                   1189:
                   1190:        return(1);
                   1191: }
                   1192:
                   1193:
                   1194: static int
                   1195: post_sh_body(POST_ARGS)
                   1196: {
                   1197:        struct mdoc_node *n;
                   1198:
1.29      kristaps 1199:        if (SEC_NAME != mdoc->lastsec)
1.1       kristaps 1200:                return(1);
                   1201:
                   1202:        /*
                   1203:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1204:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1205:         * children of the BODY declaration can also be "text".
                   1206:         */
                   1207:
                   1208:        if (NULL == (n = mdoc->last->child))
1.23      kristaps 1209:                return(mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC));
1.1       kristaps 1210:
                   1211:        for ( ; n && n->next; n = n->next) {
                   1212:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1213:                        continue;
1.28      kristaps 1214:                if (MDOC_TEXT == n->type)
                   1215:                        continue;
1.23      kristaps 1216:                if ( ! mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC))
1.1       kristaps 1217:                        return(0);
                   1218:        }
                   1219:
1.41      kristaps 1220:        assert(n);
1.27      kristaps 1221:        if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1       kristaps 1222:                return(1);
1.23      kristaps 1223:        return(mdoc_nwarn(mdoc, mdoc->last, ENAMESECINC));
1.1       kristaps 1224: }
                   1225:
                   1226:
                   1227: static int
                   1228: post_sh_head(POST_ARGS)
                   1229: {
1.2       kristaps 1230:        char                    buf[64];
                   1231:        enum mdoc_sec           sec;
                   1232:        const struct mdoc_node *n;
1.1       kristaps 1233:
                   1234:        /*
                   1235:         * Process a new section.  Sections are either "named" or
                   1236:         * "custom"; custom sections are user-defined, while named ones
                   1237:         * usually follow a conventional order and may only appear in
                   1238:         * certain manual sections.
                   1239:         */
                   1240:
1.12      kristaps 1241:        buf[0] = 0;
1.1       kristaps 1242:
1.2       kristaps 1243:        for (n = mdoc->last->child; n; n = n->next) {
1.12      kristaps 1244:                /* XXX - copied from compact(). */
1.2       kristaps 1245:                assert(MDOC_TEXT == n->type);
1.12      kristaps 1246:
1.2       kristaps 1247:                if (strlcat(buf, n->string, 64) >= 64)
1.23      kristaps 1248:                        return(mdoc_nerr(mdoc, n, ETOOLONG));
1.2       kristaps 1249:                if (NULL == n->next)
                   1250:                        continue;
                   1251:                if (strlcat(buf, " ", 64) >= 64)
1.23      kristaps 1252:                        return(mdoc_nerr(mdoc, n, ETOOLONG));
1.2       kristaps 1253:        }
1.1       kristaps 1254:
1.72      kristaps 1255:        sec = mdoc_str2sec(buf);
1.1       kristaps 1256:
1.12      kristaps 1257:        /*
                   1258:         * Check: NAME should always be first, CUSTOM has no roles,
                   1259:         * non-CUSTOM has a conventional order to be followed.
                   1260:         */
1.1       kristaps 1261:
1.72      kristaps 1262:        if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
                   1263:                if ( ! mdoc_nwarn(mdoc, mdoc->last, ESECNAME))
                   1264:                        return(0);
                   1265:
1.1       kristaps 1266:        if (SEC_CUSTOM == sec)
                   1267:                return(1);
1.72      kristaps 1268:
1.1       kristaps 1269:        if (sec == mdoc->lastnamed)
1.26      kristaps 1270:                if ( ! mdoc_nwarn(mdoc, mdoc->last, ESECREP))
                   1271:                        return(0);
1.72      kristaps 1272:
1.1       kristaps 1273:        if (sec < mdoc->lastnamed)
1.26      kristaps 1274:                if ( ! mdoc_nwarn(mdoc, mdoc->last, ESECOOO))
                   1275:                        return(0);
1.1       kristaps 1276:
1.12      kristaps 1277:        /*
                   1278:         * Check particular section/manual conventions.  LIBRARY can
                   1279:         * only occur in msec 2, 3 (TODO: are there more of these?).
                   1280:         */
1.1       kristaps 1281:
                   1282:        switch (sec) {
                   1283:        case (SEC_LIBRARY):
                   1284:                switch (mdoc->meta.msec) {
                   1285:                case (2):
                   1286:                        /* FALLTHROUGH */
                   1287:                case (3):
                   1288:                        break;
                   1289:                default:
1.23      kristaps 1290:                        return(mdoc_nwarn(mdoc, mdoc->last, EWRONGMSEC));
1.1       kristaps 1291:                }
                   1292:                break;
                   1293:        default:
                   1294:                break;
                   1295:        }
                   1296:
                   1297:        return(1);
                   1298: }
                   1299:
                   1300:
                   1301: static int
                   1302: pre_fd(PRE_ARGS)
                   1303: {
                   1304:
                   1305:        return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
                   1306: }

CVSweb