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

Annotation of mandoc/validate.c, Revision 1.74

1.74    ! kristaps    1: /* $Id: validate.c,v 1.73 2009/03/08 12:40:27 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.9       kristaps  301: };
1.6       kristaps  302:
                    303:
1.57      kristaps  304: int
                    305: mdoc_valid_pre(struct mdoc *mdoc,
                    306:                const struct mdoc_node *node)
                    307: {
                    308:        v_pre           *p;
1.71      kristaps  309:        int              line, pos;
1.57      kristaps  310:        const char      *tp;
                    311:
                    312:        if (MDOC_TEXT == node->type) {
1.71      kristaps  313:                tp = node->string;
1.57      kristaps  314:                line = node->line;
                    315:                pos = node->pos;
                    316:                return(check_text(mdoc, line, pos, tp));
                    317:        }
                    318:
1.71      kristaps  319:        if ( ! check_args(mdoc, node))
                    320:                return(0);
1.57      kristaps  321:        if (NULL == mdoc_valids[node->tok].pre)
                    322:                return(1);
                    323:        for (p = mdoc_valids[node->tok].pre; *p; p++)
                    324:                if ( ! (*p)(mdoc, node))
                    325:                        return(0);
                    326:        return(1);
                    327: }
                    328:
                    329:
                    330: int
                    331: mdoc_valid_post(struct mdoc *mdoc)
                    332: {
                    333:        v_post          *p;
                    334:
                    335:        /*
                    336:         * This check occurs after the macro's children have been filled
                    337:         * in: postfix validation.  Since this happens when we're
                    338:         * rewinding the scope tree, it's possible to have multiple
                    339:         * invocations (as by design, for now), we set bit MDOC_VALID to
                    340:         * indicate that we've validated.
                    341:         */
                    342:
                    343:        if (MDOC_VALID & mdoc->last->flags)
                    344:                return(1);
                    345:        mdoc->last->flags |= MDOC_VALID;
                    346:
                    347:        if (MDOC_TEXT == mdoc->last->type)
                    348:                return(1);
                    349:        if (MDOC_ROOT == mdoc->last->type)
                    350:                return(post_root(mdoc));
                    351:
                    352:        if (NULL == mdoc_valids[mdoc->last->tok].post)
                    353:                return(1);
                    354:        for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
                    355:                if ( ! (*p)(mdoc))
                    356:                        return(0);
                    357:
                    358:        return(1);
                    359: }
                    360:
                    361:
1.71      kristaps  362: #define        merr(m, t) nerr((m), (m)->last, (t))
                    363: static int
                    364: nerr(struct mdoc *m, const struct mdoc_node *n, enum merr type)
                    365: {
                    366:        char             *p;
                    367:
                    368:        p = NULL;
                    369:
                    370:        switch (type) {
                    371:        case (ENESTDISP):
                    372:                p = "displays may not be nested";
                    373:                break;
                    374:        case (EBOOL):
                    375:                p = "expected boolean value";
                    376:                break;
                    377:        case (EARGREP):
                    378:                p = "argument repeated";
                    379:                break;
                    380:        case (EMULTIDISP):
                    381:                p = "multiple display types specified";
                    382:                break;
                    383:        case (EMULTILIST):
                    384:                p = "multiple list types specified";
                    385:                break;
                    386:        case (ELISTTYPE):
                    387:                p = "missing list type";
                    388:                break;
                    389:        case (EDISPTYPE):
                    390:                p = "missing display type";
                    391:                break;
                    392:        case (ELINE):
                    393:                p = "expected line arguments";
                    394:                break;
                    395:        case (ENOPROLOGUE):
                    396:                p = "document has no prologue";
                    397:                break;
                    398:        case (ENODATA):
                    399:                p = "document has no data";
                    400:                break;
                    401:        case (EATT):
                    402:                p = "expected valid AT&T symbol";
                    403:                break;
                    404:        case (ENAME):
                    405:                p = "default name not yet set";
                    406:                break;
                    407:        }
                    408:
                    409:        assert(p);
                    410:        return(mdoc_nerr(m, n, p));
                    411: }
                    412:
                    413:
                    414: #define        mwarn(m, t) nwarn((m), (m)->last, (t))
                    415: static int
                    416: nwarn(struct mdoc *m, const struct mdoc_node *n, enum mwarn type)
                    417: {
                    418:        char             *p;
                    419:        enum mdoc_warn    c;
                    420:
                    421:        c = WARN_SYNTAX;
                    422:        p = NULL;
                    423:
                    424:        switch (type) {
                    425:        case (WBADMSEC):
                    426:                p = "inappropriate manual section";
                    427:                c = WARN_COMPAT;
                    428:                break;
                    429:        case (WBADSEC):
                    430:                p = "inappropriate document section";
                    431:                c = WARN_COMPAT;
                    432:                break;
                    433:        case (WARGVAL):
                    434:                p = "argument value suggested";
                    435:                c = WARN_COMPAT;
                    436:                break;
                    437:        case (WPROLREP):
                    438:                p = "prologue macros repeated";
                    439:                c = WARN_COMPAT;
                    440:                break;
                    441:        case (WPROLOOO):
                    442:                p = "prologue macros out-of-order";
                    443:                c = WARN_COMPAT;
                    444:                break;
                    445:        case (WNOLINE):
                    446:                p = "suggested no line arguments";
                    447:                break;
                    448:        case (WLINE):
                    449:                p = "suggested line arguments";
                    450:                break;
                    451:        case (WMULTILINE):
                    452:                p = "suggested multi-line arguments";
                    453:                break;
                    454:        case (WNOMULTILINE):
                    455:                p = "suggested no multi-line arguments";
                    456:                break;
                    457:        case (WWRONGMSEC):
                    458:                p = "document section in wrong manual section";
                    459:                c = WARN_COMPAT;
                    460:                break;
                    461:        case (WSECOOO):
                    462:                p = "document section out of conventional order";
                    463:                break;
                    464:        case (WSECREP):
                    465:                p = "document section repeated";
                    466:                break;
                    467:        case (WBADSTAND):
                    468:                p = "unknown standard";
                    469:                break;
                    470:        case (WNAMESECINC):
                    471:                p = "NAME section contents incomplete/badly-ordered";
                    472:                break;
                    473:        }
                    474:        assert(p);
                    475:        return(mdoc_nwarn(m, n, c, p));
                    476: }
                    477:
                    478:
1.57      kristaps  479:
1.51      kristaps  480: static inline int
                    481: warn_count(struct mdoc *m, const char *k,
                    482:                int want, const char *v, int has)
                    483: {
                    484:
1.55      kristaps  485:        return(mdoc_warn(m, WARN_SYNTAX,
1.71      kristaps  486:                "suggests %s %s %d (has %d)", v, k, want, has));
1.51      kristaps  487: }
                    488:
                    489:
                    490: static inline int
                    491: err_count(struct mdoc *m, const char *k,
                    492:                int want, const char *v, int has)
                    493: {
                    494:
1.71      kristaps  495:        return(mdoc_err(m,
                    496:                "requires %s %s %d (has %d)", v, k, want, has));
1.51      kristaps  497: }
                    498:
                    499:
                    500: static inline int
                    501: count_child(struct mdoc *mdoc)
1.36      kristaps  502: {
1.51      kristaps  503:        int               i;
1.36      kristaps  504:        struct mdoc_node *n;
                    505:
                    506:        for (i = 0, n = mdoc->last->child; n; n = n->next, i++)
                    507:                /* Do nothing */ ;
1.53      kristaps  508:
1.36      kristaps  509:        return(i);
                    510: }
                    511:
                    512:
1.53      kristaps  513: /*
                    514:  * Build these up with macros because they're basically the same check
                    515:  * for different inequalities.  Yes, this could be done with functions,
                    516:  * but this is reasonable for now.
                    517:  */
1.36      kristaps  518:
1.53      kristaps  519: #define CHECK_CHILD_DEFN(lvl, name, ineq)                      \
                    520: static int                                                     \
                    521: lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz)   \
                    522: {                                                              \
                    523:        int i;                                                  \
                    524:        if ((i = count_child(mdoc)) ineq sz)                    \
                    525:                return(1);                                      \
                    526:        return(lvl##_count(mdoc, #ineq, sz, p, i));             \
                    527: }
                    528:
                    529: #define CHECK_BODY_DEFN(name, lvl, func, num)                  \
                    530: static int                                                     \
1.55      kristaps  531: b##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  532: {                                                              \
                    533:        if (MDOC_BODY != mdoc->last->type)                      \
                    534:                return(1);                                      \
1.71      kristaps  535:        return(func(mdoc, "multi-line arguments", (num)));      \
1.53      kristaps  536: }
                    537:
                    538: #define CHECK_ELEM_DEFN(name, lvl, func, num)                  \
                    539: static int                                                     \
1.55      kristaps  540: e##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  541: {                                                              \
                    542:        assert(MDOC_ELEM == mdoc->last->type);                  \
1.71      kristaps  543:        return(func(mdoc, "line arguments", (num)));            \
1.53      kristaps  544: }
                    545:
                    546: #define CHECK_HEAD_DEFN(name, lvl, func, num)                  \
                    547: static int                                                     \
1.55      kristaps  548: h##lvl##_##name(POST_ARGS)                                     \
1.53      kristaps  549: {                                                              \
                    550:        if (MDOC_HEAD != mdoc->last->type)                      \
                    551:                return(1);                                      \
1.71      kristaps  552:        return(func(mdoc, "line arguments", (num)));            \
1.36      kristaps  553: }
                    554:
                    555:
1.53      kristaps  556: CHECK_CHILD_DEFN(warn, gt, >)                  /* warn_child_gt() */
                    557: CHECK_CHILD_DEFN(err, gt, >)                   /* err_child_gt() */
                    558: CHECK_CHILD_DEFN(warn, eq, ==)                 /* warn_child_eq() */
                    559: CHECK_CHILD_DEFN(err, eq, ==)                  /* err_child_eq() */
                    560: CHECK_CHILD_DEFN(err, lt, <)                   /* err_child_lt() */
1.59      kristaps  561: CHECK_CHILD_DEFN(warn, lt, <)                  /* warn_child_lt() */
1.53      kristaps  562: CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0)   /* bwarn_ge1() */
                    563: CHECK_ELEM_DEFN(eq1, warn, warn_child_eq, 1)   /* ewarn_eq1() */
                    564: CHECK_ELEM_DEFN(eq0, warn, warn_child_eq, 0)   /* ewarn_eq0() */
                    565: CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0)   /* ewarn_gt1() */
                    566: CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1)     /* eerr_eq1() */
                    567: CHECK_ELEM_DEFN(le2, err, err_child_lt, 3)     /* eerr_le2() */
                    568: CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0)     /* eerr_eq0() */
                    569: CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0)     /* eerr_ge1() */
                    570: CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0)     /* herr_eq0() */
1.59      kristaps  571: CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2)   /* hwarn_le1() */
1.53      kristaps  572: CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0)     /* herr_ge1() */
1.58      kristaps  573: CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1)   /* hwarn_eq1() */
1.36      kristaps  574:
                    575:
                    576: static int
1.55      kristaps  577: check_stdarg(PRE_ARGS)
1.36      kristaps  578: {
                    579:
1.71      kristaps  580:        if (n->args && 1 == n->args->argc)
                    581:                if (MDOC_Std == n->args->argv[0].arg)
                    582:                        return(1);
                    583:        return(nwarn(mdoc, n, WARGVAL));
1.36      kristaps  584: }
                    585:
                    586:
                    587: static int
1.69      kristaps  588: check_sec(PRE_ARGS, ...)
                    589: {
                    590:        enum mdoc_sec    sec;
                    591:        va_list          ap;
                    592:
                    593:        va_start(ap, n);
                    594:
                    595:        for (;;) {
1.71      kristaps  596:                /* LINTED */
1.70      kristaps  597:                sec = (enum mdoc_sec)va_arg(ap, int);
                    598:                if (SEC_CUSTOM == sec)
1.69      kristaps  599:                        break;
                    600:                if (sec != mdoc->lastsec)
                    601:                        continue;
                    602:                va_end(ap);
                    603:                return(1);
                    604:        }
                    605:
                    606:        va_end(ap);
1.71      kristaps  607:        return(nwarn(mdoc, n, WBADSEC));
1.69      kristaps  608: }
                    609:
                    610:
                    611: static int
                    612: check_msec(PRE_ARGS, ...)
1.33      kristaps  613: {
1.69      kristaps  614:        va_list          ap;
                    615:        int              msec;
1.33      kristaps  616:
1.69      kristaps  617:        va_start(ap, n);
                    618:        for (;;) {
1.71      kristaps  619:                /* LINTED */
1.69      kristaps  620:                if (0 == (msec = va_arg(ap, int)))
                    621:                        break;
                    622:                if (msec != mdoc->meta.msec)
                    623:                        continue;
                    624:                va_end(ap);
                    625:                return(1);
                    626:        }
                    627:
                    628:        va_end(ap);
1.71      kristaps  629:        return(nwarn(mdoc, n, WBADMSEC));
1.55      kristaps  630: }
                    631:
                    632:
1.68      kristaps  633: static int
1.71      kristaps  634: check_args(struct mdoc *m, const struct mdoc_node *n)
                    635: {
                    636:        int              i;
                    637:
                    638:        if (NULL == n->args)
                    639:                return(1);
                    640:
1.73      kristaps  641:        assert(n->args->argc);
1.71      kristaps  642:        for (i = 0; i < (int)n->args->argc; i++)
                    643:                if ( ! check_argv(m, n, &n->args->argv[i]))
                    644:                        return(0);
                    645:
                    646:        return(1);
                    647: }
                    648:
                    649:
                    650: static int
                    651: check_argv(struct mdoc *m, const struct mdoc_node *n,
                    652:                const struct mdoc_argv *v)
1.68      kristaps  653: {
1.71      kristaps  654:        int              i;
1.68      kristaps  655:
1.71      kristaps  656:        for (i = 0; i < (int)v->sz; i++)
                    657:                if ( ! check_text(m, v->line, v->pos, v->value[i]))
                    658:                        return(0);
1.68      kristaps  659:
1.71      kristaps  660:        if (MDOC_Std == v->arg && MDOC_Ex == n->tok) {
                    661:                /* `Nm' name must be set. */
                    662:                if (v->sz || m->meta.name)
                    663:                        return(1);
                    664:                return(nerr(m, n, ENAME));
1.68      kristaps  665:        }
                    666:
                    667:        return(1);
                    668: }
                    669:
                    670:
1.55      kristaps  671: static int
1.63      kristaps  672: check_text(struct mdoc *mdoc, int line, int pos, const char *p)
1.55      kristaps  673: {
                    674:        size_t           c;
                    675:
1.62      kristaps  676:        /* XXX - indicate deprecated escapes \*(xx and \*x. */
                    677:
1.55      kristaps  678:        for ( ; *p; p++) {
1.68      kristaps  679:                if ( ! isprint((u_char)*p) && '\t' != *p)
1.56      kristaps  680:                        return(mdoc_perr(mdoc, line, pos,
1.71      kristaps  681:                                "invalid non-printing character"));
1.55      kristaps  682:                if ('\\' != *p)
                    683:                        continue;
                    684:                if ((c = mdoc_isescape(p))) {
1.63      kristaps  685:                        p += (int)c - 1;
1.55      kristaps  686:                        continue;
                    687:                }
1.74    ! kristaps  688:                if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags))
        !           689:                        return(mdoc_perr(mdoc, line, pos,
        !           690:                                        "invalid escape sequence"));
        !           691:                if ( ! mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX,
        !           692:                                        "invalid escape sequence"))
        !           693:                        return(0);
1.55      kristaps  694:        }
                    695:
                    696:        return(1);
1.14      kristaps  697: }
                    698:
                    699:
1.55      kristaps  700:
                    701:
1.14      kristaps  702: static int
1.55      kristaps  703: check_parent(PRE_ARGS, int tok, enum mdoc_type t)
1.54      kristaps  704: {
                    705:
                    706:        assert(n->parent);
                    707:        if ((MDOC_ROOT == t || tok == n->parent->tok) &&
                    708:                        (t == n->parent->type))
                    709:                return(1);
                    710:
                    711:        return(mdoc_nerr(mdoc, n, "require parent %s",
                    712:                MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
                    713: }
                    714:
                    715:
                    716:
                    717: static int
1.55      kristaps  718: pre_display(PRE_ARGS)
1.23      kristaps  719: {
1.55      kristaps  720:        struct mdoc_node *node;
1.23      kristaps  721:
1.53      kristaps  722:        /* Display elements (`Bd', `D1'...) cannot be nested. */
                    723:
1.55      kristaps  724:        if (MDOC_BLOCK != n->type)
1.24      kristaps  725:                return(1);
                    726:
1.38      kristaps  727:        /* LINTED */
1.55      kristaps  728:        for (node = mdoc->last->parent; node; node = node->parent)
                    729:                if (MDOC_BLOCK == node->type)
                    730:                        if (MDOC_Bd == node->tok)
1.23      kristaps  731:                                break;
1.55      kristaps  732:        if (NULL == node)
1.23      kristaps  733:                return(1);
1.53      kristaps  734:
1.71      kristaps  735:        return(nerr(mdoc, n, ENESTDISP));
1.23      kristaps  736: }
                    737:
                    738:
                    739: static int
1.55      kristaps  740: pre_bl(PRE_ARGS)
1.24      kristaps  741: {
1.71      kristaps  742:        int              i, type, width, offset;
1.24      kristaps  743:
1.55      kristaps  744:        if (MDOC_BLOCK != n->type)
1.24      kristaps  745:                return(1);
1.71      kristaps  746:        if (NULL == n->args)
                    747:                return(nerr(mdoc, n, ELISTTYPE));
1.24      kristaps  748:
1.53      kristaps  749:        /* Make sure that only one type of list is specified.  */
                    750:
1.61      kristaps  751:        type = offset = width = -1;
                    752:
1.38      kristaps  753:        /* LINTED */
1.71      kristaps  754:        for (i = 0; i < (int)n->args->argc; i++)
                    755:                switch (n->args->argv[i].arg) {
1.24      kristaps  756:                case (MDOC_Bullet):
                    757:                        /* FALLTHROUGH */
                    758:                case (MDOC_Dash):
                    759:                        /* FALLTHROUGH */
                    760:                case (MDOC_Enum):
                    761:                        /* FALLTHROUGH */
                    762:                case (MDOC_Hyphen):
                    763:                        /* FALLTHROUGH */
                    764:                case (MDOC_Item):
                    765:                        /* FALLTHROUGH */
                    766:                case (MDOC_Tag):
                    767:                        /* FALLTHROUGH */
                    768:                case (MDOC_Diag):
                    769:                        /* FALLTHROUGH */
                    770:                case (MDOC_Hang):
                    771:                        /* FALLTHROUGH */
                    772:                case (MDOC_Ohang):
                    773:                        /* FALLTHROUGH */
                    774:                case (MDOC_Inset):
1.26      kristaps  775:                        /* FALLTHROUGH */
                    776:                case (MDOC_Column):
1.61      kristaps  777:                        if (-1 == type) {
1.71      kristaps  778:                                type = n->args->argv[i].arg;
1.53      kristaps  779:                                break;
1.61      kristaps  780:                        }
1.71      kristaps  781:                        return(nerr(mdoc, n, EMULTILIST));
1.61      kristaps  782:                case (MDOC_Width):
                    783:                        if (-1 == width) {
1.71      kristaps  784:                                width = n->args->argv[i].arg;
1.61      kristaps  785:                                break;
                    786:                        }
1.71      kristaps  787:                        return(nerr(mdoc, n, EARGREP));
1.61      kristaps  788:                case (MDOC_Offset):
                    789:                        if (-1 == offset) {
1.71      kristaps  790:                                offset = n->args->argv[i].arg;
1.61      kristaps  791:                                break;
                    792:                        }
1.71      kristaps  793:                        return(nerr(mdoc, n, EARGREP));
1.24      kristaps  794:                default:
                    795:                        break;
                    796:                }
1.53      kristaps  797:
1.61      kristaps  798:        if (-1 == type)
1.71      kristaps  799:                return(nerr(mdoc, n, ELISTTYPE));
1.61      kristaps  800:
                    801:        switch (type) {
                    802:        case (MDOC_Column):
                    803:                /* FALLTHROUGH */
                    804:        case (MDOC_Diag):
                    805:                /* FALLTHROUGH */
                    806:        case (MDOC_Inset):
                    807:                /* FALLTHROUGH */
                    808:        case (MDOC_Item):
                    809:                if (-1 == width)
                    810:                        break;
                    811:                return(mdoc_nwarn(mdoc, n, WARN_SYNTAX,
1.71      kristaps  812:                                "superfluous %s argument",
1.61      kristaps  813:                                mdoc_argnames[MDOC_Width]));
                    814:        case (MDOC_Tag):
1.71      kristaps  815:                if (-1 != width)
                    816:                        break;
                    817:                return(mdoc_nerr(mdoc, n, "missing %s argument",
                    818:                                mdoc_argnames[MDOC_Width]));
1.61      kristaps  819:        default:
                    820:                break;
                    821:        }
                    822:
                    823:        return(1);
1.24      kristaps  824: }
                    825:
                    826:
                    827: static int
1.55      kristaps  828: pre_bd(PRE_ARGS)
1.24      kristaps  829: {
1.71      kristaps  830:        int              i, type, err;
1.24      kristaps  831:
1.55      kristaps  832:        if (MDOC_BLOCK != n->type)
1.24      kristaps  833:                return(1);
1.71      kristaps  834:        if (NULL == n->args)
                    835:                return(nerr(mdoc, n, EDISPTYPE));
1.24      kristaps  836:
1.53      kristaps  837:        /* Make sure that only one type of display is specified.  */
                    838:
1.38      kristaps  839:        /* LINTED */
1.71      kristaps  840:        for (i = 0, err = type = 0; ! err &&
                    841:                        i < (int)n->args->argc; i++)
                    842:                switch (n->args->argv[i].arg) {
1.24      kristaps  843:                case (MDOC_Ragged):
                    844:                        /* FALLTHROUGH */
                    845:                case (MDOC_Unfilled):
                    846:                        /* FALLTHROUGH */
1.29      kristaps  847:                case (MDOC_Filled):
                    848:                        /* FALLTHROUGH */
1.24      kristaps  849:                case (MDOC_Literal):
                    850:                        /* FALLTHROUGH */
                    851:                case (MDOC_File):
1.53      kristaps  852:                        if (0 == type++)
                    853:                                break;
1.71      kristaps  854:                        return(nerr(mdoc, n, EMULTIDISP));
1.24      kristaps  855:                default:
                    856:                        break;
                    857:                }
1.53      kristaps  858:
                    859:        if (type)
                    860:                return(1);
1.71      kristaps  861:        return(nerr(mdoc, n, EDISPTYPE));
1.24      kristaps  862: }
                    863:
                    864:
                    865: static int
1.55      kristaps  866: pre_ss(PRE_ARGS)
1.33      kristaps  867: {
                    868:
1.55      kristaps  869:        if (MDOC_BLOCK != n->type)
1.33      kristaps  870:                return(1);
1.55      kristaps  871:        return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
1.33      kristaps  872: }
                    873:
                    874:
                    875: static int
1.55      kristaps  876: pre_sh(PRE_ARGS)
1.33      kristaps  877: {
                    878:
1.55      kristaps  879:        if (MDOC_BLOCK != n->type)
1.33      kristaps  880:                return(1);
1.55      kristaps  881:        return(check_parent(mdoc, n, -1, MDOC_ROOT));
1.33      kristaps  882: }
                    883:
                    884:
                    885: static int
1.55      kristaps  886: pre_it(PRE_ARGS)
1.53      kristaps  887: {
                    888:
1.55      kristaps  889:        if (MDOC_BLOCK != n->type)
1.53      kristaps  890:                return(1);
1.55      kristaps  891:        return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
1.53      kristaps  892: }
                    893:
                    894:
                    895: static int
1.69      kristaps  896: pre_an(PRE_ARGS)
1.36      kristaps  897: {
                    898:
1.73      kristaps  899:        if (NULL == n->args || 1 == n->args->argc)
1.36      kristaps  900:                return(1);
1.71      kristaps  901:        return(mdoc_nerr(mdoc, n, "only one argument allowed"));
1.36      kristaps  902: }
                    903:
                    904:
                    905: static int
1.69      kristaps  906: pre_lb(PRE_ARGS)
1.35      kristaps  907: {
1.36      kristaps  908:
1.69      kristaps  909:        return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
1.35      kristaps  910: }
                    911:
                    912:
                    913: static int
1.55      kristaps  914: pre_rv(PRE_ARGS)
1.36      kristaps  915: {
                    916:
1.69      kristaps  917:        if ( ! check_msec(mdoc, n, 2, 3, 0))
1.36      kristaps  918:                return(0);
1.55      kristaps  919:        return(check_stdarg(mdoc, n));
1.36      kristaps  920: }
                    921:
                    922:
                    923: static int
1.55      kristaps  924: pre_ex(PRE_ARGS)
1.33      kristaps  925: {
1.35      kristaps  926:
1.69      kristaps  927:        if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
1.35      kristaps  928:                return(0);
1.55      kristaps  929:        return(check_stdarg(mdoc, n));
1.33      kristaps  930: }
                    931:
                    932:
                    933: static int
1.55      kristaps  934: pre_er(PRE_ARGS)
1.33      kristaps  935: {
                    936:
1.69      kristaps  937:        return(check_msec(mdoc, n, 2, 0));
1.33      kristaps  938: }
                    939:
                    940:
                    941: static int
1.55      kristaps  942: pre_cd(PRE_ARGS)
1.33      kristaps  943: {
                    944:
1.69      kristaps  945:        return(check_msec(mdoc, n, 4, 0));
1.33      kristaps  946: }
                    947:
                    948:
                    949: static int
1.55      kristaps  950: pre_prologue(PRE_ARGS)
1.20      kristaps  951: {
                    952:
1.69      kristaps  953:        return(check_sec(mdoc, n, SEC_PROLOGUE, SEC_CUSTOM));
                    954: }
1.20      kristaps  955:
                    956:
1.69      kristaps  957: static int
                    958: pre_dt(PRE_ARGS)
                    959: {
                    960:
                    961:        if (0 == mdoc->meta.date || mdoc->meta.os)
1.71      kristaps  962:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps  963:                        return(0);
                    964:        if (mdoc->meta.title)
1.71      kristaps  965:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps  966:                        return(0);
                    967:        return(1);
                    968: }
                    969:
                    970:
                    971: static int
                    972: pre_os(PRE_ARGS)
                    973: {
                    974:
                    975:        if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.71      kristaps  976:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps  977:                        return(0);
                    978:        if (mdoc->meta.os)
1.71      kristaps  979:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps  980:                        return(0);
                    981:        return(1);
                    982: }
1.20      kristaps  983:
                    984:
1.69      kristaps  985: static int
                    986: pre_dd(PRE_ARGS)
                    987: {
1.20      kristaps  988:
1.69      kristaps  989:        if (mdoc->meta.title || mdoc->meta.os)
1.71      kristaps  990:                if ( ! nwarn(mdoc, n, WPROLOOO))
1.69      kristaps  991:                        return(0);
                    992:        if (mdoc->meta.date)
1.71      kristaps  993:                if ( ! nwarn(mdoc, n, WPROLREP))
1.69      kristaps  994:                        return(0);
                    995:        return(1);
1.20      kristaps  996: }
                    997:
                    998:
1.35      kristaps  999: static int
1.55      kristaps 1000: post_bf(POST_ARGS)
1.41      kristaps 1001: {
                   1002:        char             *p;
                   1003:        struct mdoc_node *head;
                   1004:
                   1005:        if (MDOC_BLOCK != mdoc->last->type)
                   1006:                return(1);
1.53      kristaps 1007:
1.71      kristaps 1008:        head = mdoc->last->head;
1.41      kristaps 1009:
1.71      kristaps 1010:        if (NULL == mdoc->last->args) {
                   1011:                if (NULL == head->child ||
                   1012:                                MDOC_TEXT != head->child->type)
                   1013:                        return(mdoc_err(mdoc, "text argument expected"));
1.53      kristaps 1014:
1.71      kristaps 1015:                p = head->child->string;
1.53      kristaps 1016:                if (xstrcmp(p, "Em"))
                   1017:                        return(1);
                   1018:                else if (xstrcmp(p, "Li"))
                   1019:                        return(1);
                   1020:                else if (xstrcmp(p, "Sm"))
                   1021:                        return(1);
                   1022:                return(mdoc_nerr(mdoc, head->child, "invalid font"));
1.41      kristaps 1023:        }
1.53      kristaps 1024:
1.41      kristaps 1025:        if (head->child)
1.71      kristaps 1026:                return(mdoc_err(mdoc, "one argument expected"));
1.53      kristaps 1027:
1.71      kristaps 1028:        return(1);
1.41      kristaps 1029: }
                   1030:
                   1031:
                   1032: static int
1.55      kristaps 1033: post_nm(POST_ARGS)
1.37      kristaps 1034: {
                   1035:
                   1036:        if (mdoc->last->child)
                   1037:                return(1);
                   1038:        if (mdoc->meta.name)
                   1039:                return(1);
1.71      kristaps 1040:        return(merr(mdoc, ENAME));
1.37      kristaps 1041: }
                   1042:
                   1043:
                   1044: static int
1.55      kristaps 1045: post_at(POST_ARGS)
1.36      kristaps 1046: {
                   1047:
1.37      kristaps 1048:        if (NULL == mdoc->last->child)
                   1049:                return(1);
1.71      kristaps 1050:        if (MDOC_TEXT != mdoc->last->child->type)
                   1051:                return(merr(mdoc, EATT));
                   1052:        if (mdoc_a2att(mdoc->last->child->string))
1.36      kristaps 1053:                return(1);
1.71      kristaps 1054:        return(merr(mdoc, EATT));
1.36      kristaps 1055: }
                   1056:
                   1057:
                   1058: static int
1.55      kristaps 1059: post_an(POST_ARGS)
1.35      kristaps 1060: {
                   1061:
1.71      kristaps 1062:        if (mdoc->last->args) {
1.35      kristaps 1063:                if (NULL == mdoc->last->child)
                   1064:                        return(1);
1.71      kristaps 1065:                return(merr(mdoc, ELINE));
1.35      kristaps 1066:        }
                   1067:
                   1068:        if (mdoc->last->child)
                   1069:                return(1);
1.71      kristaps 1070:        return(merr(mdoc, ELINE));
1.35      kristaps 1071: }
                   1072:
                   1073:
                   1074: static int
1.55      kristaps 1075: post_ex(POST_ARGS)
1.35      kristaps 1076: {
                   1077:
1.71      kristaps 1078:        if (mdoc->last->args)
                   1079:                return(1);
                   1080:        return(merr(mdoc, ELINE));
1.35      kristaps 1081: }
                   1082:
                   1083:
1.25      kristaps 1084: static int
1.55      kristaps 1085: post_it(POST_ARGS)
1.25      kristaps 1086: {
1.71      kristaps 1087:        int               type, i, cols;
                   1088:        struct mdoc_node *n, *c;
1.25      kristaps 1089:
                   1090:        if (MDOC_BLOCK != mdoc->last->type)
                   1091:                return(1);
                   1092:
1.53      kristaps 1093:        n = mdoc->last->parent->parent;
1.71      kristaps 1094:        if (NULL == n->args)
                   1095:                return(merr(mdoc, ELISTTYPE));
1.25      kristaps 1096:
1.26      kristaps 1097:        /* Some types require block-head, some not. */
1.25      kristaps 1098:
1.38      kristaps 1099:        /* LINTED */
1.71      kristaps 1100:        for (cols = type = -1, i = 0; -1 == type &&
                   1101:                        i < (int)n->args->argc; i++)
                   1102:                switch (n->args->argv[i].arg) {
1.25      kristaps 1103:                case (MDOC_Tag):
                   1104:                        /* FALLTHROUGH */
                   1105:                case (MDOC_Diag):
                   1106:                        /* FALLTHROUGH */
                   1107:                case (MDOC_Hang):
                   1108:                        /* FALLTHROUGH */
                   1109:                case (MDOC_Ohang):
                   1110:                        /* FALLTHROUGH */
                   1111:                case (MDOC_Inset):
1.71      kristaps 1112:                        /* FALLTHROUGH */
1.25      kristaps 1113:                case (MDOC_Bullet):
                   1114:                        /* FALLTHROUGH */
                   1115:                case (MDOC_Dash):
                   1116:                        /* FALLTHROUGH */
                   1117:                case (MDOC_Enum):
                   1118:                        /* FALLTHROUGH */
                   1119:                case (MDOC_Hyphen):
                   1120:                        /* FALLTHROUGH */
                   1121:                case (MDOC_Item):
1.71      kristaps 1122:                        type = n->args->argv[i].arg;
1.47      kristaps 1123:                        break;
1.25      kristaps 1124:                case (MDOC_Column):
1.71      kristaps 1125:                        type = n->args->argv[i].arg;
                   1126:                        cols = (int)n->args->argv[i].sz;
1.25      kristaps 1127:                        break;
                   1128:                default:
                   1129:                        break;
                   1130:                }
                   1131:
1.71      kristaps 1132:        if (-1 == type)
                   1133:                return(merr(mdoc, ELISTTYPE));
1.25      kristaps 1134:
1.71      kristaps 1135:        switch (type) {
                   1136:        case (MDOC_Tag):
                   1137:                if (NULL == mdoc->last->head->child)
                   1138:                        if ( ! mwarn(mdoc, WLINE))
                   1139:                                return(0);
                   1140:                break;
                   1141:        case (MDOC_Hang):
                   1142:                /* FALLTHROUGH */
                   1143:        case (MDOC_Ohang):
                   1144:                /* FALLTHROUGH */
                   1145:        case (MDOC_Inset):
                   1146:                /* FALLTHROUGH */
                   1147:        case (MDOC_Diag):
                   1148:                if (NULL == mdoc->last->head->child)
                   1149:                        if ( ! mwarn(mdoc, WLINE))
1.25      kristaps 1150:                                return(0);
1.71      kristaps 1151:                if (NULL == mdoc->last->body->child)
                   1152:                        if ( ! mwarn(mdoc, WMULTILINE))
1.25      kristaps 1153:                                return(0);
1.71      kristaps 1154:                break;
                   1155:        case (MDOC_Bullet):
                   1156:                /* FALLTHROUGH */
                   1157:        case (MDOC_Dash):
                   1158:                /* FALLTHROUGH */
                   1159:        case (MDOC_Enum):
                   1160:                /* FALLTHROUGH */
                   1161:        case (MDOC_Hyphen):
                   1162:                /* FALLTHROUGH */
                   1163:        case (MDOC_Item):
                   1164:                if (mdoc->last->head->child)
                   1165:                        if ( ! mwarn(mdoc, WNOLINE))
1.47      kristaps 1166:                                return(0);
1.71      kristaps 1167:                if (NULL == mdoc->last->body->child)
                   1168:                        if ( ! mwarn(mdoc, WMULTILINE))
1.47      kristaps 1169:                                return(0);
1.71      kristaps 1170:                break;
                   1171:        case (MDOC_Column):
                   1172:                if (NULL == mdoc->last->head->child)
                   1173:                        if ( ! mwarn(mdoc, WLINE))
1.47      kristaps 1174:                                return(0);
1.71      kristaps 1175:                if (mdoc->last->body->child)
                   1176:                        if ( ! mwarn(mdoc, WNOMULTILINE))
1.47      kristaps 1177:                                return(0);
1.71      kristaps 1178:                c = mdoc->last->head->child;
                   1179:                for (i = 0; c; c = c->next)
                   1180:                        i++;
                   1181:                if (i == cols)
                   1182:                        break;
                   1183:                if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
                   1184:                                        "column mismatch (have %d, want %d)", i, cols))
                   1185:                        return(0);
                   1186:                break;
                   1187:        default:
                   1188:                break;
1.25      kristaps 1189:        }
                   1190:
1.71      kristaps 1191:        return(1);
1.25      kristaps 1192: }
                   1193:
                   1194:
1.24      kristaps 1195: static int
1.55      kristaps 1196: post_bl(POST_ARGS)
1.24      kristaps 1197: {
1.53      kristaps 1198:        struct mdoc_node        *n;
1.24      kristaps 1199:
                   1200:        if (MDOC_BODY != mdoc->last->type)
                   1201:                return(1);
1.71      kristaps 1202:        if (NULL == mdoc->last->child)
1.64      kristaps 1203:                return(1);
                   1204:
1.38      kristaps 1205:        /* LINTED */
1.24      kristaps 1206:        for (n = mdoc->last->child; n; n = n->next) {
                   1207:                if (MDOC_BLOCK == n->type)
1.25      kristaps 1208:                        if (MDOC_It == n->tok)
1.24      kristaps 1209:                                continue;
1.64      kristaps 1210:                return(mdoc_nerr(mdoc, n, "bad child of parent %s",
                   1211:                                mdoc_macronames[mdoc->last->tok]));
1.24      kristaps 1212:        }
1.53      kristaps 1213:
1.64      kristaps 1214:        return(1);
1.24      kristaps 1215: }
                   1216:
                   1217:
1.34      kristaps 1218: static int
1.37      kristaps 1219: ebool(struct mdoc *mdoc)
1.34      kristaps 1220: {
                   1221:        struct mdoc_node *n;
                   1222:
1.38      kristaps 1223:        /* LINTED */
1.34      kristaps 1224:        for (n = mdoc->last->child; n; n = n->next) {
                   1225:                if (MDOC_TEXT != n->type)
                   1226:                        break;
1.71      kristaps 1227:                if (xstrcmp(n->string, "on"))
1.34      kristaps 1228:                        continue;
1.71      kristaps 1229:                if (xstrcmp(n->string, "off"))
1.34      kristaps 1230:                        continue;
                   1231:                break;
                   1232:        }
1.53      kristaps 1233:
1.34      kristaps 1234:        if (NULL == n)
                   1235:                return(1);
1.71      kristaps 1236:        return(nerr(mdoc, n, EBOOL));
1.37      kristaps 1237: }
                   1238:
                   1239:
                   1240: static int
1.55      kristaps 1241: post_root(POST_ARGS)
1.37      kristaps 1242: {
                   1243:
1.46      kristaps 1244:        if (NULL == mdoc->first->child)
1.71      kristaps 1245:                return(merr(mdoc, ENODATA));
1.46      kristaps 1246:        if (SEC_PROLOGUE == mdoc->lastnamed)
1.71      kristaps 1247:                return(merr(mdoc, ENOPROLOGUE));
1.53      kristaps 1248:
1.46      kristaps 1249:        if (MDOC_BLOCK != mdoc->first->child->type)
1.71      kristaps 1250:                return(merr(mdoc, ENODATA));
1.46      kristaps 1251:        if (MDOC_Sh != mdoc->first->child->tok)
1.71      kristaps 1252:                return(merr(mdoc, ENODATA));
1.53      kristaps 1253:
1.37      kristaps 1254:        return(1);
1.34      kristaps 1255: }
                   1256:
                   1257:
1.20      kristaps 1258: static int
1.69      kristaps 1259: post_st(POST_ARGS)
                   1260: {
                   1261:
1.71      kristaps 1262:        if (mdoc_a2st(mdoc->last->child->string))
1.69      kristaps 1263:                return(1);
1.71      kristaps 1264:        return(mwarn(mdoc, WBADSTAND));
1.69      kristaps 1265: }
                   1266:
                   1267:
                   1268: static int
1.55      kristaps 1269: post_sh(POST_ARGS)
1.14      kristaps 1270: {
1.46      kristaps 1271:
                   1272:        if (MDOC_HEAD == mdoc->last->type)
                   1273:                return(post_sh_head(mdoc));
                   1274:        if (MDOC_BODY == mdoc->last->type)
                   1275:                return(post_sh_body(mdoc));
1.53      kristaps 1276:
1.46      kristaps 1277:        return(1);
                   1278: }
                   1279:
                   1280:
                   1281: static int
1.55      kristaps 1282: post_sh_body(POST_ARGS)
1.46      kristaps 1283: {
                   1284:        struct mdoc_node *n;
                   1285:
                   1286:        if (SEC_NAME != mdoc->lastnamed)
                   1287:                return(1);
                   1288:
1.51      kristaps 1289:        /*
                   1290:         * Warn if the NAME section doesn't contain the `Nm' and `Nd'
                   1291:         * macros (can have multiple `Nm' and one `Nd').  Note that the
                   1292:         * children of the BODY declaration can also be "text".
                   1293:         */
                   1294:
1.46      kristaps 1295:        if (NULL == (n = mdoc->last->child))
1.71      kristaps 1296:                return(mwarn(mdoc, WNAMESECINC));
1.51      kristaps 1297:
                   1298:        for ( ; n && n->next; n = n->next) {
                   1299:                if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
                   1300:                        continue;
                   1301:                if (MDOC_TEXT == n->type)
                   1302:                        continue;
1.71      kristaps 1303:                if ( ! mwarn(mdoc, WNAMESECINC))
1.51      kristaps 1304:                        return(0);
                   1305:        }
                   1306:
                   1307:        if (MDOC_ELEM == n->type && MDOC_Nd == n->tok)
1.46      kristaps 1308:                return(1);
1.71      kristaps 1309:        return(mwarn(mdoc, WNAMESECINC));
1.46      kristaps 1310: }
                   1311:
                   1312:
                   1313: static int
1.55      kristaps 1314: post_sh_head(POST_ARGS)
1.46      kristaps 1315: {
1.36      kristaps 1316:        char              buf[64];
1.21      kristaps 1317:        enum mdoc_sec     sec;
                   1318:
1.69      kristaps 1319:        /*
                   1320:         * Process a new section.  Sections are either "named" or
                   1321:         * "custom"; custom sections are user-defined, while named ones
                   1322:         * usually follow a conventional order and may only appear in
                   1323:         * certain manual sections.
                   1324:         */
                   1325:
1.25      kristaps 1326:        assert(MDOC_Sh == mdoc->last->tok);
1.21      kristaps 1327:
1.69      kristaps 1328:        (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1.14      kristaps 1329:
1.46      kristaps 1330:        sec = mdoc_atosec(buf);
                   1331:
1.69      kristaps 1332:        /* The NAME section should always be first. */
                   1333:
1.46      kristaps 1334:        if (SEC_BODY == mdoc->lastnamed && SEC_NAME != sec)
1.71      kristaps 1335:                return(mwarn(mdoc, WSECOOO));
1.46      kristaps 1336:        if (SEC_CUSTOM == sec)
1.21      kristaps 1337:                return(1);
1.69      kristaps 1338:
                   1339:        /* Check for repeated or out-of-order sections. */
                   1340:
1.46      kristaps 1341:        if (sec == mdoc->lastnamed)
1.71      kristaps 1342:                return(mwarn(mdoc, WSECREP));
1.46      kristaps 1343:        if (sec < mdoc->lastnamed)
1.71      kristaps 1344:                return(mwarn(mdoc, WSECOOO));
1.69      kristaps 1345:
                   1346:        /* Check particular section/manual section conventions. */
                   1347:
                   1348:        switch (sec) {
                   1349:        case (SEC_LIBRARY):
                   1350:                switch (mdoc->meta.msec) {
                   1351:                case (2):
                   1352:                        /* FALLTHROUGH */
                   1353:                case (3):
                   1354:                        break;
                   1355:                default:
1.71      kristaps 1356:                        return(mwarn(mdoc, WWRONGMSEC));
1.69      kristaps 1357:                }
                   1358:                break;
                   1359:        default:
                   1360:                break;
                   1361:        }
1.46      kristaps 1362:
                   1363:        return(1);
1.11      kristaps 1364: }
                   1365:
                   1366:
1.57      kristaps 1367: static int
1.69      kristaps 1368: pre_fd(PRE_ARGS)
1.11      kristaps 1369: {
1.39      kristaps 1370:
1.69      kristaps 1371:        return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1.11      kristaps 1372: }

CVSweb