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

Annotation of mandoc/mdoc_validate.c, Revision 1.23

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

CVSweb