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

Annotation of mandoc/validate.c, Revision 1.58

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

CVSweb