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

Annotation of mandoc/mdoc_validate.c, Revision 1.26

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

CVSweb