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

Annotation of mandoc/validate.c, Revision 1.75

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

CVSweb