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

Annotation of mandoc/mdoc_validate.c, Revision 1.34

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

CVSweb