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

Annotation of mandoc/validate.c, Revision 1.69

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

CVSweb